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