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 
13 namespace shaka {
14 namespace media {
15 namespace {
16 const bool kInitialEncryptionInfo = true;
17 } // namespace
18 
19 Muxer::Muxer(const MuxerOptions& options)
20  : options_(options), cancelled_(false), clock_(NULL) {}
21 
22 Muxer::~Muxer() {}
23 
24 void Muxer::Cancel() {
25  cancelled_ = true;
26 }
27 
28 void Muxer::SetMuxerListener(std::unique_ptr<MuxerListener> muxer_listener) {
29  muxer_listener_ = std::move(muxer_listener);
30 }
31 
33  std::unique_ptr<ProgressListener> progress_listener) {
34  progress_listener_ = std::move(progress_listener);
35 }
36 
37 Status Muxer::Process(std::unique_ptr<StreamData> stream_data) {
38  Status status;
39  switch (stream_data->stream_data_type) {
40  case StreamDataType::kStreamInfo:
41  streams_.push_back(std::move(stream_data->stream_info));
42  if (muxer_listener_ && streams_.back()->is_encrypted()) {
43  const EncryptionConfig& encryption_config =
44  streams_.back()->encryption_config();
45  muxer_listener_->OnEncryptionInfoReady(
46  kInitialEncryptionInfo, encryption_config.protection_scheme,
47  encryption_config.key_id, encryption_config.constant_iv,
48  encryption_config.key_system_info);
49  current_key_id_ = encryption_config.key_id;
50  }
51  return InitializeMuxer();
52  case StreamDataType::kSegmentInfo: {
53  const auto& segment_info = *stream_data->segment_info;
54  if (muxer_listener_ && segment_info.is_encrypted) {
55  const EncryptionConfig* encryption_config =
56  segment_info.key_rotation_encryption_config.get();
57  // Only call OnEncryptionInfoReady again when key updates.
58  if (encryption_config && encryption_config->key_id != current_key_id_) {
59  muxer_listener_->OnEncryptionInfoReady(
60  !kInitialEncryptionInfo, encryption_config->protection_scheme,
61  encryption_config->key_id, encryption_config->constant_iv,
62  encryption_config->key_system_info);
63  current_key_id_ = encryption_config->key_id;
64  }
65  if (!encryption_started_) {
66  encryption_started_ = true;
67  muxer_listener_->OnEncryptionStart();
68  }
69  }
70  return FinalizeSegment(stream_data->stream_index, segment_info);
71  }
72  case StreamDataType::kMediaSample:
73  return AddSample(stream_data->stream_index, *stream_data->media_sample);
74  case StreamDataType::kCueEvent:
75  if (muxer_listener_) {
76  const int64_t time_scale =
77  streams_[stream_data->stream_index]->time_scale();
78  const double time_in_seconds = stream_data->cue_event->time_in_seconds;
79  const int64_t scaled_time =
80  static_cast<int64_t>(time_in_seconds * time_scale);
81  muxer_listener_->OnCueEvent(scaled_time,
82  stream_data->cue_event->cue_data);
83  }
84  break;
85  default:
86  VLOG(3) << "Stream data type "
87  << static_cast<int>(stream_data->stream_data_type) << " ignored.";
88  break;
89  }
90  // No dispatch for muxer.
91  return Status::OK;
92 }
93 
94 } // namespace media
95 } // namespace shaka
All the methods that are virtual are virtual for mocking.
void SetProgressListener(std::unique_ptr< ProgressListener > progress_listener)
Definition: muxer.cc:32
Status Process(std::unique_ptr< StreamData > stream_data) override
Definition: muxer.cc:37
void SetMuxerListener(std::unique_ptr< MuxerListener > muxer_listener)
Definition: muxer.cc:28