2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-11-12 20:37:58 +00:00
|
|
|
//
|
|
|
|
// Defines the muxer interface.
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_BASE_MUXER_H_
|
|
|
|
#define PACKAGER_MEDIA_BASE_MUXER_H_
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2013-10-11 21:44:55 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/media/base/media_handler.h>
|
|
|
|
#include <packager/media/base/muxer_options.h>
|
|
|
|
#include <packager/media/event/muxer_listener.h>
|
|
|
|
#include <packager/media/event/progress_listener.h>
|
|
|
|
#include <packager/mpd/base/mpd_builder.h>
|
|
|
|
#include <packager/status.h>
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-10-11 21:44:55 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
class MediaSample;
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Muxer is responsible for taking elementary stream samples and producing
|
2014-08-20 23:51:15 +00:00
|
|
|
/// media containers. An optional KeySource can be provided to Muxer
|
2014-04-16 01:09:32 +00:00
|
|
|
/// to generate encrypted outputs.
|
2017-02-21 18:36:50 +00:00
|
|
|
class Muxer : public MediaHandler {
|
2013-10-11 21:44:55 +00:00
|
|
|
public:
|
2014-01-14 01:38:34 +00:00
|
|
|
explicit Muxer(const MuxerOptions& options);
|
2013-10-11 21:44:55 +00:00
|
|
|
virtual ~Muxer();
|
|
|
|
|
2015-02-09 18:22:28 +00:00
|
|
|
/// Cancel a muxing job in progress. Will cause @a Run to exit with an error
|
|
|
|
/// status of type CANCELLED.
|
|
|
|
void Cancel();
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Set a MuxerListener event handler for this object.
|
|
|
|
/// @param muxer_listener should not be NULL.
|
2016-08-17 17:41:40 +00:00
|
|
|
void SetMuxerListener(std::unique_ptr<MuxerListener> muxer_listener);
|
2013-12-12 23:49:31 +00:00
|
|
|
|
2015-05-11 21:07:10 +00:00
|
|
|
/// Set a ProgressListener event handler for this object.
|
|
|
|
/// @param progress_listener should not be NULL.
|
2016-08-17 17:41:40 +00:00
|
|
|
void SetProgressListener(std::unique_ptr<ProgressListener> progress_listener);
|
2015-05-11 21:07:10 +00:00
|
|
|
|
2017-09-12 17:24:24 +00:00
|
|
|
const std::vector<std::shared_ptr<const StreamInfo>>& streams() const {
|
2017-02-21 18:36:50 +00:00
|
|
|
return streams_;
|
|
|
|
}
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Inject clock, mainly used for testing.
|
|
|
|
/// The injected clock will be used to generate the creation time-stamp and
|
|
|
|
/// modification time-stamp of the muxer output.
|
2023-12-01 17:32:19 +00:00
|
|
|
/// If no clock is injected, the code uses std::chrone::system_clock::now()
|
|
|
|
/// to generate the time-stamps.
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @param clock is the Clock to be injected.
|
2023-12-01 17:32:19 +00:00
|
|
|
void set_clock(std::shared_ptr<Clock> clock) { clock_ = clock; }
|
2014-02-28 02:39:52 +00:00
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
protected:
|
2017-02-21 18:36:50 +00:00
|
|
|
/// @name MediaHandler implementation overrides.
|
|
|
|
/// @{
|
|
|
|
Status InitializeInternal() override { return Status::OK; }
|
|
|
|
Status Process(std::unique_ptr<StreamData> stream_data) override;
|
2018-05-23 00:26:18 +00:00
|
|
|
Status OnFlushRequest(size_t input_stream_index) override;
|
2017-02-21 18:36:50 +00:00
|
|
|
/// @}
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
const MuxerOptions& options() const { return options_; }
|
2015-05-07 21:06:16 +00:00
|
|
|
MuxerListener* muxer_listener() { return muxer_listener_.get(); }
|
2015-05-11 21:07:10 +00:00
|
|
|
ProgressListener* progress_listener() { return progress_listener_.get(); }
|
2023-12-01 17:32:19 +00:00
|
|
|
|
|
|
|
uint64_t Now() const {
|
|
|
|
auto duration = clock_->now().time_since_epoch();
|
|
|
|
auto seconds =
|
|
|
|
std::chrono::duration_cast<std::chrono::seconds>(duration).count();
|
|
|
|
return static_cast<uint64_t>(seconds);
|
|
|
|
}
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
private:
|
2018-05-23 00:26:18 +00:00
|
|
|
Muxer(const Muxer&) = delete;
|
|
|
|
Muxer& operator=(const Muxer&) = delete;
|
|
|
|
|
2018-05-30 22:02:33 +00:00
|
|
|
// Initialize the muxer. InitializeMuxer may be called multiple times with
|
|
|
|
// |options()| updated between calls, which is used to support separate file
|
|
|
|
// per Representation per Period for Ad Insertion.
|
2017-02-21 18:36:50 +00:00
|
|
|
virtual Status InitializeMuxer() = 0;
|
2014-04-09 17:34:55 +00:00
|
|
|
|
|
|
|
// Final clean up.
|
|
|
|
virtual Status Finalize() = 0;
|
|
|
|
|
2020-07-30 21:26:45 +00:00
|
|
|
// Add a new media sample. This does nothing by default; so subclasses that
|
|
|
|
// handle media samples will need to replace this.
|
|
|
|
virtual Status AddMediaSample(size_t stream_id, const MediaSample& sample);
|
|
|
|
|
|
|
|
// Add a new text sample. This does nothing by default; so subclasses that
|
|
|
|
// handle text samples will need to replace this.
|
|
|
|
virtual Status AddTextSample(size_t stream_id, const TextSample& sample);
|
2017-02-24 01:17:47 +00:00
|
|
|
|
|
|
|
// Finalize the segment or subsegment.
|
2017-09-12 17:24:24 +00:00
|
|
|
virtual Status FinalizeSegment(
|
|
|
|
size_t stream_id,
|
|
|
|
const SegmentInfo& segment_info) = 0;
|
2014-04-09 17:34:55 +00:00
|
|
|
|
2018-05-30 22:02:33 +00:00
|
|
|
// Re-initialize Muxer. Could be called on StreamInfo or CueEvent.
|
|
|
|
// |timestamp| may be used to set the output file name.
|
|
|
|
Status ReinitializeMuxer(int64_t timestamp);
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
MuxerOptions options_;
|
2017-09-12 17:24:24 +00:00
|
|
|
std::vector<std::shared_ptr<const StreamInfo>> streams_;
|
2017-06-20 23:30:03 +00:00
|
|
|
std::vector<uint8_t> current_key_id_;
|
2017-03-11 02:49:55 +00:00
|
|
|
bool encryption_started_ = false;
|
2018-05-23 00:26:18 +00:00
|
|
|
bool cancelled_ = false;
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MuxerListener> muxer_listener_;
|
|
|
|
std::unique_ptr<ProgressListener> progress_listener_;
|
2023-12-01 17:32:19 +00:00
|
|
|
std::shared_ptr<Clock> clock_;
|
2013-12-12 23:49:31 +00:00
|
|
|
|
2018-05-23 00:26:18 +00:00
|
|
|
// In VOD single segment case with Ad Cues, |output_file_name| is allowed to
|
|
|
|
// be a template. In this case, there will be NumAdCues + 1 files generated.
|
|
|
|
std::string output_file_template_;
|
2024-05-02 20:25:49 +00:00
|
|
|
size_t output_file_index_ = 1;
|
2013-10-11 21:44:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#endif // PACKAGER_MEDIA_BASE_MUXER_H_
|