DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
demuxer.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/demuxer.h"
8 
9 #include "packager/base/bind.h"
10 #include "packager/base/logging.h"
11 #include "packager/base/stl_util.h"
12 #include "packager/media/base/decryptor_source.h"
13 #include "packager/media/base/key_source.h"
14 #include "packager/media/base/media_sample.h"
15 #include "packager/media/base/media_stream.h"
16 #include "packager/media/base/stream_info.h"
17 #include "packager/media/file/file.h"
18 #include "packager/media/formats/mp2t/mp2t_media_parser.h"
19 #include "packager/media/formats/mp4/mp4_media_parser.h"
20 #include "packager/media/formats/webm/webm_media_parser.h"
21 #include "packager/media/formats/wvm/wvm_media_parser.h"
22 
23 namespace {
24 const size_t kInitBufSize = 0x10000; // 65KB, sufficient to determine the
25  // container and likely all init data.
26 const size_t kBufSize = 0x200000; // 2MB
27 }
28 
29 namespace edash_packager {
30 namespace media {
31 
32 Demuxer::Demuxer(const std::string& file_name)
33  : file_name_(file_name),
34  media_file_(NULL),
35  init_event_received_(false),
36  container_name_(CONTAINER_UNKNOWN),
37  buffer_(new uint8_t[kBufSize]),
38  cancelled_(false) {
39 }
40 
41 Demuxer::~Demuxer() {
42  if (media_file_)
43  media_file_->Close();
44  STLDeleteElements(&streams_);
45 }
46 
47 void Demuxer::SetKeySource(scoped_ptr<KeySource> key_source) {
48  key_source_ = key_source.Pass();
49 }
50 
52  DCHECK(!media_file_);
53  DCHECK(!init_event_received_);
54 
55  LOG(INFO) << "Initialize Demuxer for file '" << file_name_ << "'.";
56 
57  media_file_ = File::Open(file_name_.c_str(), "r");
58  if (!media_file_) {
59  return Status(error::FILE_FAILURE,
60  "Cannot open file for reading " + file_name_);
61  }
62 
63  // Read enough bytes before detecting the container.
64  size_t bytes_read = 0;
65  while (bytes_read < kInitBufSize) {
66  int64_t read_result =
67  media_file_->Read(buffer_.get() + bytes_read, kInitBufSize);
68  if (read_result < 0)
69  return Status(error::FILE_FAILURE, "Cannot read file " + file_name_);
70  if (read_result == 0)
71  break;
72  bytes_read += read_result;
73  }
74  container_name_ = DetermineContainer(buffer_.get(), bytes_read);
75 
76  // Initialize media parser.
77  switch (container_name_) {
78  case CONTAINER_MOV:
79  parser_.reset(new mp4::MP4MediaParser());
80  break;
81  case CONTAINER_MPEG2TS:
82  parser_.reset(new mp2t::Mp2tMediaParser());
83  break;
84  case CONTAINER_MPEG2PS:
85  parser_.reset(new wvm::WvmMediaParser());
86  break;
87  case CONTAINER_WEBM:
88  parser_.reset(new WebMMediaParser());
89  break;
90  default:
91  NOTIMPLEMENTED();
92  return Status(error::UNIMPLEMENTED, "Container not supported.");
93  }
94 
95  parser_->Init(base::Bind(&Demuxer::ParserInitEvent, base::Unretained(this)),
96  base::Bind(&Demuxer::NewSampleEvent, base::Unretained(this)),
97  key_source_.get());
98 
99  // Handle trailing 'moov'.
100  if (container_name_ == CONTAINER_MOV)
101  static_cast<mp4::MP4MediaParser*>(parser_.get())->LoadMoov(file_name_);
102 
103  if (!parser_->Parse(buffer_.get(), bytes_read)) {
104  init_parsing_status_ =
105  Status(error::PARSER_FAILURE, "Cannot parse media file " + file_name_);
106  }
107 
108  // Parse until init event received or on error.
109  while (!init_event_received_ && init_parsing_status_.ok())
110  init_parsing_status_ = Parse();
111  // Defer error reporting if init completed successfully.
112  return init_event_received_ ? Status::OK : init_parsing_status_;
113 }
114 
115 void Demuxer::ParserInitEvent(
116  const std::vector<scoped_refptr<StreamInfo> >& streams) {
117  init_event_received_ = true;
118 
119  std::vector<scoped_refptr<StreamInfo> >::const_iterator it = streams.begin();
120  for (; it != streams.end(); ++it) {
121  streams_.push_back(new MediaStream(*it, this));
122  }
123 }
124 
125 bool Demuxer::NewSampleEvent(uint32_t track_id,
126  const scoped_refptr<MediaSample>& sample) {
127  std::vector<MediaStream*>::iterator it = streams_.begin();
128  for (; it != streams_.end(); ++it) {
129  if (track_id == (*it)->info()->track_id()) {
130  return (*it)->PushSample(sample).ok();
131  }
132  }
133  return false;
134 }
135 
137  Status status;
138 
139  LOG(INFO) << "Demuxer::Run() on file '" << file_name_ << "'.";
140 
141  // Start the streams.
142  for (std::vector<MediaStream*>::iterator it = streams_.begin();
143  it != streams_.end();
144  ++it) {
145  status = (*it)->Start(MediaStream::kPush);
146  if (!status.ok())
147  return status;
148  }
149 
150  while (!cancelled_ && (status = Parse()).ok())
151  continue;
152 
153  if (cancelled_ && status.ok())
154  return Status(error::CANCELLED, "Demuxer run cancelled");
155 
156  if (status.error_code() == error::END_OF_STREAM) {
157  // Push EOS sample to muxer to indicate end of stream.
158  const scoped_refptr<MediaSample>& sample = MediaSample::CreateEOSBuffer();
159  for (std::vector<MediaStream*>::iterator it = streams_.begin();
160  it != streams_.end();
161  ++it) {
162  status = (*it)->PushSample(sample);
163  if (!status.ok())
164  return status;
165  }
166  }
167  return status;
168 }
169 
171  DCHECK(media_file_);
172  DCHECK(parser_);
173  DCHECK(buffer_);
174 
175  // Return early and avoid call Parse(...) again if it has already failed at
176  // the initialization.
177  if (!init_parsing_status_.ok())
178  return init_parsing_status_;
179 
180  int64_t bytes_read = media_file_->Read(buffer_.get(), kBufSize);
181  if (bytes_read == 0) {
182  parser_->Flush();
183  return Status(error::END_OF_STREAM, "");
184  } else if (bytes_read < 0) {
185  return Status(error::FILE_FAILURE, "Cannot read file " + file_name_);
186  }
187 
188  return parser_->Parse(buffer_.get(), bytes_read)
189  ? Status::OK
190  : Status(error::PARSER_FAILURE,
191  "Cannot parse media file " + file_name_);
192 }
193 
195  cancelled_ = true;
196 }
197 
198 } // namespace media
199 } // namespace edash_packager
void SetKeySource(scoped_ptr< KeySource > key_source)
Definition: demuxer.cc:47
virtual int64_t Read(void *buffer, uint64_t length)=0
const std::vector< MediaStream * > & streams()
Definition: demuxer.h:66
virtual bool Open()=0
Internal open. Should not be used directly.
static scoped_refptr< MediaSample > CreateEOSBuffer()
Definition: media_sample.cc:75
Status Parse()
Read from the source and send it to the parser.
Definition: demuxer.cc:170
Demuxer(const std::string &file_name)
Definition: demuxer.cc:32