Shaka Packager SDK
muxer.cc
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/base/muxer.h"
8 
9 #include <algorithm>
10 
11 #include "packager/media/base/media_sample.h"
12 #include "packager/media/base/muxer_util.h"
13 #include "packager/status_macros.h"
14 
15 namespace shaka {
16 namespace media {
17 namespace {
18 const bool kInitialEncryptionInfo = true;
19 const int64_t kStartTime = 0;
20 } // namespace
21 
22 Muxer::Muxer(const MuxerOptions& options) : options_(options) {
23  // "$" is only allowed if the output file name is a template, which is used to
24  // support one file per Representation per Period when there are Ad Cues.
25  if (options_.output_file_name.find("$") != std::string::npos)
26  output_file_template_ = options_.output_file_name;
27 }
28 
29 Muxer::~Muxer() {}
30 
31 void Muxer::Cancel() {
32  cancelled_ = true;
33 }
34 
35 void Muxer::SetMuxerListener(std::unique_ptr<MuxerListener> muxer_listener) {
36  muxer_listener_ = std::move(muxer_listener);
37 }
38 
40  std::unique_ptr<ProgressListener> progress_listener) {
41  progress_listener_ = std::move(progress_listener);
42 }
43 
44 Status Muxer::Process(std::unique_ptr<StreamData> stream_data) {
45  Status status;
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) {
53  const EncryptionConfig* encryption_config =
54  segment_info.key_rotation_encryption_config.get();
55  // Only call OnEncryptionInfoReady again when key updates.
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;
62  }
63  if (!encryption_started_) {
64  encryption_started_ = true;
65  muxer_listener_->OnEncryptionStart();
66  }
67  }
68  return FinalizeSegment(stream_data->stream_index, segment_info);
69  }
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);
81 
82  // Finalize and re-initialize Muxer to generate different content files.
83  if (!output_file_template_.empty()) {
84  RETURN_IF_ERROR(Finalize());
85  RETURN_IF_ERROR(ReinitializeMuxer(scaled_time));
86  }
87  }
88  break;
89  default:
90  VLOG(3) << "Stream data type "
91  << static_cast<int>(stream_data->stream_data_type) << " ignored.";
92  break;
93  }
94  // No dispatch for muxer.
95  return Status::OK;
96 }
97 
98 Status Muxer::OnFlushRequest(size_t input_stream_index) {
99  return Finalize();
100 }
101 
102 Status Muxer::ReinitializeMuxer(int64_t timestamp) {
103  if (muxer_listener_ && streams_.back()->is_encrypted()) {
104  const EncryptionConfig& encryption_config =
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;
111  }
112  if (!output_file_template_.empty()) {
113  // Update |output_file_name| with an actual file name, which will be used by
114  // the subclasses.
115  options_.output_file_name =
116  GetSegmentName(output_file_template_, timestamp, output_file_index_++,
117  options_.bandwidth);
118  }
119  return InitializeMuxer();
120 }
121 
122 } // namespace media
123 } // namespace shaka
Status OnFlushRequest(size_t input_stream_index) override
Event handler for flush request at the specific input stream index.
Definition: muxer.cc:98
All the methods that are virtual are virtual for mocking.
void SetProgressListener(std::unique_ptr< ProgressListener > progress_listener)
Definition: muxer.cc:39
Status Process(std::unique_ptr< StreamData > stream_data) override
Definition: muxer.cc:44
void SetMuxerListener(std::unique_ptr< MuxerListener > muxer_listener)
Definition: muxer.cc:35