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