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