7 #include "packager/media/base/demuxer.h"
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/wvm/wvm_media_parser.h"
23 const size_t kInitBufSize = 0x10000;
25 const size_t kBufSize = 0x200000;
28 namespace edash_packager {
32 : file_name_(file_name),
34 init_event_received_(false),
35 container_name_(CONTAINER_UNKNOWN),
36 buffer_(new uint8_t[kBufSize]),
43 STLDeleteElements(&streams_);
47 key_source_ = key_source.Pass();
52 DCHECK(!init_event_received_);
54 LOG(INFO) <<
"Initialize Demuxer for file '" << file_name_ <<
"'.";
56 media_file_ =
File::Open(file_name_.c_str(),
"r");
58 return Status(error::FILE_FAILURE,
59 "Cannot open file for reading " + file_name_);
63 size_t bytes_read = 0;
64 while (bytes_read < kInitBufSize) {
66 media_file_->
Read(buffer_.get() + bytes_read, kInitBufSize);
68 return Status(error::FILE_FAILURE,
"Cannot read file " + file_name_);
71 bytes_read += read_result;
73 container_name_ = DetermineContainer(buffer_.get(), bytes_read);
76 switch (container_name_) {
80 case CONTAINER_MPEG2TS:
83 case CONTAINER_MPEG2PS:
88 return Status(error::UNIMPLEMENTED,
"Container not supported.");
91 parser_->Init(base::Bind(&Demuxer::ParserInitEvent, base::Unretained(
this)),
92 base::Bind(&Demuxer::NewSampleEvent, base::Unretained(
this)),
96 if (container_name_ == CONTAINER_MOV)
99 if (!parser_->Parse(buffer_.get(), bytes_read)) {
100 init_parsing_status_ =
101 Status(error::PARSER_FAILURE,
"Cannot parse media file " + file_name_);
105 while (!init_event_received_ && init_parsing_status_.ok())
106 init_parsing_status_ =
Parse();
108 return init_event_received_ ? Status::OK : init_parsing_status_;
111 void Demuxer::ParserInitEvent(
112 const std::vector<scoped_refptr<StreamInfo> >& streams) {
113 init_event_received_ =
true;
115 std::vector<scoped_refptr<StreamInfo> >::const_iterator it =
streams.begin();
116 for (; it !=
streams.end(); ++it) {
121 bool Demuxer::NewSampleEvent(uint32_t track_id,
122 const scoped_refptr<MediaSample>& sample) {
123 std::vector<MediaStream*>::iterator it = streams_.begin();
124 for (; it != streams_.end(); ++it) {
125 if (track_id == (*it)->info()->track_id()) {
126 return (*it)->PushSample(sample).ok();
135 LOG(INFO) <<
"Demuxer::Run() on file '" << file_name_ <<
"'.";
138 for (std::vector<MediaStream*>::iterator it = streams_.begin();
139 it != streams_.end();
141 status = (*it)->Start(MediaStream::kPush);
146 while (!cancelled_ && (status =
Parse()).ok())
149 if (cancelled_ && status.ok())
150 return Status(error::CANCELLED,
"Demuxer run cancelled");
152 if (status.error_code() == error::END_OF_STREAM) {
155 for (std::vector<MediaStream*>::iterator it = streams_.begin();
156 it != streams_.end();
158 status = (*it)->PushSample(sample);
173 if (!init_parsing_status_.ok())
174 return init_parsing_status_;
176 int64_t bytes_read = media_file_->
Read(buffer_.get(), kBufSize);
177 if (bytes_read == 0) {
179 return Status(error::END_OF_STREAM,
"");
180 }
else if (bytes_read < 0) {
181 return Status(error::FILE_FAILURE,
"Cannot read file " + file_name_);
184 return parser_->Parse(buffer_.get(), bytes_read)
186 :
Status(error::PARSER_FAILURE,
187 "Cannot parse media file " + file_name_);