DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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/fourccs.h"
12 #include "packager/media/base/media_sample.h"
13 #include "packager/media/base/media_stream.h"
14 
15 namespace shaka {
16 namespace media {
17 
18 Muxer::Muxer(const MuxerOptions& options)
19  : options_(options),
20  initialized_(false),
21  encryption_key_source_(NULL),
22  max_sd_pixels_(0),
23  max_hd_pixels_(0),
24  max_uhd1_pixels_(0),
25  clear_lead_in_seconds_(0),
26  crypto_period_duration_in_seconds_(0),
27  protection_scheme_(FOURCC_NULL),
28  cancelled_(false),
29  clock_(NULL) {}
30 
31 Muxer::~Muxer() {}
32 
33 void Muxer::SetKeySource(KeySource* encryption_key_source,
34  uint32_t max_sd_pixels,
35  uint32_t max_hd_pixels,
36  uint32_t max_uhd1_pixels,
37  double clear_lead_in_seconds,
38  double crypto_period_duration_in_seconds,
39  FourCC protection_scheme) {
40  DCHECK(encryption_key_source);
41  encryption_key_source_ = encryption_key_source;
42  max_sd_pixels_ = max_sd_pixels;
43  max_hd_pixels_ = max_hd_pixels;
44  max_uhd1_pixels_ = max_uhd1_pixels;
45  clear_lead_in_seconds_ = clear_lead_in_seconds;
46  crypto_period_duration_in_seconds_ = crypto_period_duration_in_seconds;
47  protection_scheme_ = protection_scheme;
48 }
49 
51  DCHECK(stream);
52  stream->Connect(this);
53  streams_.push_back(stream);
54 }
55 
57  DCHECK(!streams_.empty());
58 
59  Status status;
60  // Start the streams.
61  for (std::vector<MediaStream*>::iterator it = streams_.begin();
62  it != streams_.end();
63  ++it) {
64  status = (*it)->Start(MediaStream::kPull);
65  if (!status.ok())
66  return status;
67  }
68 
69  uint32_t current_stream_id = 0;
70  while (status.ok()) {
71  if (cancelled_)
72  return Status(error::CANCELLED, "muxer run cancelled");
73 
74  scoped_refptr<MediaSample> sample;
75  status = streams_[current_stream_id]->PullSample(&sample);
76  if (!status.ok())
77  break;
78  status = AddSample(streams_[current_stream_id], sample);
79 
80  // Switch to next stream if the current stream is ready for fragmentation.
81  if (status.error_code() == error::FRAGMENT_FINALIZED) {
82  current_stream_id = (current_stream_id + 1) % streams_.size();
83  status.Clear();
84  }
85  }
86  // Finalize the muxer after reaching end of stream.
87  return status.error_code() == error::END_OF_STREAM ? Finalize() : status;
88 }
89 
90 void Muxer::Cancel() {
91  cancelled_ = true;
92 }
93 
94 void Muxer::SetMuxerListener(std::unique_ptr<MuxerListener> muxer_listener) {
95  muxer_listener_ = std::move(muxer_listener);
96 }
97 
99  std::unique_ptr<ProgressListener> progress_listener) {
100  progress_listener_ = std::move(progress_listener);
101 }
102 
103 Status Muxer::AddSample(const MediaStream* stream,
104  scoped_refptr<MediaSample> sample) {
105  DCHECK(std::find(streams_.begin(), streams_.end(), stream) != streams_.end());
106 
107  if (!initialized_) {
108  Status status = Initialize();
109  if (!status.ok())
110  return status;
111  initialized_ = true;
112  }
113  if (sample->end_of_stream()) {
114  // EOS sample should be sent only when the sample was pushed from Demuxer
115  // to Muxer. In this case, there should be only one stream in Muxer.
116  DCHECK_EQ(1u, streams_.size());
117  return Finalize();
118  } else if (sample->is_encrypted()) {
119  LOG(ERROR) << "Unable to multiplex encrypted media sample";
120  return Status(error::INTERNAL_ERROR, "Encrypted media sample.");
121  }
122  return DoAddSample(stream, sample);
123 }
124 
125 } // namespace media
126 } // namespace shaka
void AddStream(MediaStream *stream)
Add video/audio stream.
Definition: muxer.cc:50
Status Run()
Drive the remuxing from muxer side (pull).
Definition: muxer.cc:56
void Connect(Muxer *muxer)
Definition: media_stream.cc:55
void SetProgressListener(std::unique_ptr< ProgressListener > progress_listener)
Definition: muxer.cc:98
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:30
void SetKeySource(KeySource *encryption_key_source, uint32_t max_sd_pixels, uint32_t max_hd_pixels, uint32_t max_uhd1_pixels, double clear_lead_in_seconds, double crypto_period_duration_in_seconds, FourCC protection_scheme)
Definition: muxer.cc:33
void SetMuxerListener(std::unique_ptr< MuxerListener > muxer_listener)
Definition: muxer.cc:94