7 #include "packager/media/base/muxer.h" 11 #include "packager/media/base/media_sample.h" 12 #include "packager/media/base/muxer_util.h" 13 #include "packager/status_macros.h" 18 const bool kInitialEncryptionInfo =
true;
19 const int64_t kStartTime = 0;
22 Muxer::Muxer(
const MuxerOptions& options) : options_(options) {
25 if (options_.output_file_name.find(
"$") != std::string::npos)
26 output_file_template_ = options_.output_file_name;
36 muxer_listener_ = std::move(muxer_listener);
40 std::unique_ptr<ProgressListener> progress_listener) {
41 progress_listener_ = std::move(progress_listener);
46 switch (stream_data->stream_data_type) {
47 case StreamDataType::kStreamInfo:
48 streams_.push_back(std::move(stream_data->stream_info));
49 return ReinitializeMuxer(kStartTime);
50 case StreamDataType::kSegmentInfo: {
51 const auto& segment_info = *stream_data->segment_info;
52 if (muxer_listener_ && segment_info.is_encrypted) {
54 segment_info.key_rotation_encryption_config.get();
56 if (encryption_config && encryption_config->key_id != current_key_id_) {
57 muxer_listener_->OnEncryptionInfoReady(
58 !kInitialEncryptionInfo, encryption_config->protection_scheme,
59 encryption_config->key_id, encryption_config->constant_iv,
60 encryption_config->key_system_info);
61 current_key_id_ = encryption_config->key_id;
63 if (!encryption_started_) {
64 encryption_started_ =
true;
65 muxer_listener_->OnEncryptionStart();
68 return FinalizeSegment(stream_data->stream_index, segment_info);
70 case StreamDataType::kMediaSample:
71 return AddSample(stream_data->stream_index, *stream_data->media_sample);
72 case StreamDataType::kCueEvent:
73 if (muxer_listener_) {
74 const int64_t time_scale =
75 streams_[stream_data->stream_index]->time_scale();
76 const double time_in_seconds = stream_data->cue_event->time_in_seconds;
77 const int64_t scaled_time =
78 static_cast<int64_t
>(time_in_seconds * time_scale);
79 muxer_listener_->OnCueEvent(scaled_time,
80 stream_data->cue_event->cue_data);
83 if (!output_file_template_.empty()) {
84 RETURN_IF_ERROR(Finalize());
85 RETURN_IF_ERROR(ReinitializeMuxer(scaled_time));
90 VLOG(3) <<
"Stream data type " 91 <<
static_cast<int>(stream_data->stream_data_type) <<
" ignored.";
102 Status Muxer::ReinitializeMuxer(int64_t timestamp) {
103 if (muxer_listener_ && streams_.back()->is_encrypted()) {
105 streams_.back()->encryption_config();
106 muxer_listener_->OnEncryptionInfoReady(
107 kInitialEncryptionInfo, encryption_config.protection_scheme,
108 encryption_config.key_id, encryption_config.constant_iv,
109 encryption_config.key_system_info);
110 current_key_id_ = encryption_config.key_id;
112 if (!output_file_template_.empty()) {
117 GetSegmentName(output_file_template_, timestamp, output_file_index_++,
120 return InitializeMuxer();
All the methods that are virtual are virtual for mocking.