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