7 #include "packager/media/demuxer/demuxer.h" 11 #include "packager/base/bind.h" 12 #include "packager/base/logging.h" 13 #include "packager/base/strings/string_number_conversions.h" 14 #include "packager/file/file.h" 15 #include "packager/media/base/decryptor_source.h" 16 #include "packager/media/base/key_source.h" 17 #include "packager/media/base/macros.h" 18 #include "packager/media/base/media_sample.h" 19 #include "packager/media/base/stream_info.h" 20 #include "packager/media/formats/mp2t/mp2t_media_parser.h" 21 #include "packager/media/formats/mp4/mp4_media_parser.h" 22 #include "packager/media/formats/webm/webm_media_parser.h" 23 #include "packager/media/formats/webvtt/webvtt_media_parser.h" 24 #include "packager/media/formats/wvm/wvm_media_parser.h" 28 const size_t kInitBufSize = 0x10000;
29 const size_t kBufSize = 0x200000;
33 const size_t kQueuedSamplesLimit = 10000;
34 const size_t kInvalidStreamIndex =
static_cast<size_t>(-1);
35 const size_t kBaseVideoOutputStreamIndex = 0x100;
36 const size_t kBaseAudioOutputStreamIndex = 0x200;
37 const size_t kBaseTextOutputStreamIndex = 0x300;
39 std::string GetStreamLabel(
size_t stream_index) {
40 switch (stream_index) {
41 case kBaseVideoOutputStreamIndex:
43 case kBaseAudioOutputStreamIndex:
45 case kBaseTextOutputStreamIndex:
48 return base::SizeTToString(stream_index);
52 bool GetStreamIndex(
const std::string& stream_label,
size_t* stream_index) {
54 if (stream_label ==
"video") {
55 *stream_index = kBaseVideoOutputStreamIndex;
56 }
else if (stream_label ==
"audio") {
57 *stream_index = kBaseAudioOutputStreamIndex;
58 }
else if (stream_label ==
"text") {
59 *stream_index = kBaseTextOutputStreamIndex;
62 if (!base::StringToSizeT(stream_label, stream_index)) {
63 LOG(ERROR) <<
"Invalid argument --stream=" << stream_label <<
"; " 64 <<
"should be 'audio', 'video', 'text', or a number";
77 : file_name_(file_name), buffer_(new uint8_t[kBufSize]) {}
85 key_source_ = std::move(key_source);
89 LOG(INFO) <<
"Demuxer::Run() on file '" << file_name_ <<
"'.";
90 Status status = InitializeParser();
93 while (!all_streams_ready_ && status.ok())
97 if (all_streams_ready_ && output_handlers().empty())
99 if (!init_event_status_.ok())
100 return init_event_status_;
104 for (
const auto& pair : output_handlers()) {
105 if (std::find(stream_indexes_.begin(), stream_indexes_.end(), pair.first) ==
106 stream_indexes_.end()) {
107 LOG(ERROR) <<
"Invalid argument, stream=" << GetStreamLabel(pair.first)
108 <<
" not available.";
109 return Status(error::INVALID_ARGUMENT,
"Stream not available");
113 while (!cancelled_ && status.ok())
115 if (cancelled_ && status.ok())
116 return Status(error::CANCELLED,
"Demuxer run cancelled");
118 if (status.error_code() == error::END_OF_STREAM) {
119 for (
size_t stream_index : stream_indexes_) {
134 std::shared_ptr<MediaHandler> handler) {
135 size_t stream_index = kInvalidStreamIndex;
136 if (!GetStreamIndex(stream_label, &stream_index)) {
137 return Status(error::INVALID_ARGUMENT,
138 "Invalid stream: " + stream_label);
144 const std::string& language_override) {
145 size_t stream_index = kInvalidStreamIndex;
146 if (!GetStreamIndex(stream_label, &stream_index))
147 LOG(WARNING) <<
"Invalid stream for language override " << stream_label;
148 language_overrides_[stream_index] = language_override;
151 Demuxer::QueuedSample::QueuedSample(uint32_t local_track_id,
152 std::shared_ptr<MediaSample> local_sample)
153 : track_id(local_track_id), sample(local_sample) {}
155 Demuxer::QueuedSample::~QueuedSample() {}
157 Status Demuxer::InitializeParser() {
158 DCHECK(!media_file_);
159 DCHECK(!all_streams_ready_);
161 LOG(INFO) <<
"Initialize Demuxer for file '" << file_name_ <<
"'.";
163 media_file_ =
File::Open(file_name_.c_str(),
"r");
165 return Status(error::FILE_FAILURE,
166 "Cannot open file for reading " + file_name_);
170 int64_t bytes_read = 0;
171 while (static_cast<size_t>(bytes_read) < kInitBufSize) {
172 int64_t read_result =
173 media_file_->
Read(buffer_.get() + bytes_read, kInitBufSize);
175 return Status(error::FILE_FAILURE,
"Cannot read file " + file_name_);
176 if (read_result == 0)
178 bytes_read += read_result;
180 container_name_ = DetermineContainer(buffer_.get(), bytes_read);
183 switch (container_name_) {
187 case CONTAINER_MPEG2TS:
195 case CONTAINER_MPEG2PS:
196 FALLTHROUGH_INTENDED;
203 case CONTAINER_WEBVTT:
208 return Status(error::UNIMPLEMENTED,
"Container not supported.");
211 parser_->Init(base::Bind(&Demuxer::ParserInitEvent, base::Unretained(
this)),
212 base::Bind(&Demuxer::NewSampleEvent, base::Unretained(
this)),
216 if (container_name_ == CONTAINER_MOV)
218 if (!parser_->Parse(buffer_.get(), bytes_read)) {
219 return Status(error::PARSER_FAILURE,
220 "Cannot parse media file " + file_name_);
225 void Demuxer::ParserInitEvent(
226 const std::vector<std::shared_ptr<StreamInfo>>& stream_infos) {
227 if (dump_stream_info_) {
228 printf(
"\nFile \"%s\":\n", file_name_.c_str());
229 printf(
"Found %zu stream(s).\n", stream_infos.size());
230 for (
size_t i = 0; i < stream_infos.size(); ++i)
231 printf(
"Stream [%zu] %s\n", i, stream_infos[i]->ToString().c_str());
234 int base_stream_index = 0;
235 bool video_handler_set =
236 output_handlers().find(kBaseVideoOutputStreamIndex) !=
237 output_handlers().end();
238 bool audio_handler_set =
239 output_handlers().find(kBaseAudioOutputStreamIndex) !=
240 output_handlers().end();
241 bool text_handler_set =
242 output_handlers().find(kBaseTextOutputStreamIndex) !=
243 output_handlers().end();
244 for (
const std::shared_ptr<StreamInfo>& stream_info : stream_infos) {
245 size_t stream_index = base_stream_index;
246 if (video_handler_set && stream_info->stream_type() == kStreamVideo) {
247 stream_index = kBaseVideoOutputStreamIndex;
249 video_handler_set =
false;
251 if (audio_handler_set && stream_info->stream_type() == kStreamAudio) {
252 stream_index = kBaseAudioOutputStreamIndex;
254 audio_handler_set =
false;
256 if (text_handler_set && stream_info->stream_type() == kStreamText) {
257 stream_index = kBaseTextOutputStreamIndex;
258 text_handler_set =
false;
261 const bool handler_set =
262 output_handlers().find(stream_index) != output_handlers().end();
264 track_id_to_stream_index_map_[stream_info->track_id()] = stream_index;
265 stream_indexes_.push_back(stream_index);
266 auto iter = language_overrides_.find(stream_index);
267 if (iter != language_overrides_.end() &&
268 stream_info->stream_type() != kStreamVideo) {
269 stream_info->set_language(iter->second);
271 if (stream_info->is_encrypted()) {
272 init_event_status_.
Update(
Status(error::INVALID_ARGUMENT,
273 "A decryption key source is not " 274 "provided for an encrypted stream."));
276 init_event_status_.
Update(
280 track_id_to_stream_index_map_[stream_info->track_id()] =
285 all_streams_ready_ =
true;
288 bool Demuxer::NewSampleEvent(uint32_t track_id,
289 const std::shared_ptr<MediaSample>& sample) {
290 if (!all_streams_ready_) {
291 if (queued_samples_.size() >= kQueuedSamplesLimit) {
292 LOG(ERROR) <<
"Queued samples limit reached: " << kQueuedSamplesLimit;
295 queued_samples_.push_back(QueuedSample(track_id, sample));
298 if (!init_event_status_.ok()) {
301 while (!queued_samples_.empty()) {
302 if (!PushSample(queued_samples_.front().track_id,
303 queued_samples_.front().sample)) {
306 queued_samples_.pop_front();
308 return PushSample(track_id, sample);
311 bool Demuxer::PushSample(uint32_t track_id,
312 const std::shared_ptr<MediaSample>& sample) {
313 auto stream_index_iter = track_id_to_stream_index_map_.find(track_id);
314 if (stream_index_iter == track_id_to_stream_index_map_.end()) {
315 LOG(ERROR) <<
"Track " << track_id <<
" not found.";
318 if (stream_index_iter->second == kInvalidStreamIndex)
322 LOG(ERROR) <<
"Failed to process sample " << stream_index_iter->second
333 int64_t bytes_read = media_file_->
Read(buffer_.get(), kBufSize);
334 if (bytes_read == 0) {
335 if (!parser_->Flush())
336 return Status(error::PARSER_FAILURE,
"Failed to flush.");
337 return Status(error::END_OF_STREAM,
"");
338 }
else if (bytes_read < 0) {
339 return Status(error::FILE_FAILURE,
"Cannot read file " + file_name_);
342 return parser_->Parse(buffer_.get(), bytes_read)
344 :
Status(error::PARSER_FAILURE,
345 "Cannot parse media file " + file_name_);
virtual int64_t Read(void *buffer, uint64_t length)=0
All the methods that are virtual are virtual for mocking.
void Update(Status new_status)
virtual bool Open()=0
Internal open. Should not be used directly.