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/wvm/wvm_media_parser.h" 27 const size_t kInitBufSize = 0x10000;
28 const size_t kBufSize = 0x200000;
32 const size_t kQueuedSamplesLimit = 10000;
33 const size_t kInvalidStreamIndex =
static_cast<size_t>(-1);
34 const size_t kBaseVideoOutputStreamIndex = 0x100;
35 const size_t kBaseAudioOutputStreamIndex = 0x200;
36 const size_t kBaseTextOutputStreamIndex = 0x300;
38 std::string GetStreamLabel(
size_t stream_index) {
39 switch (stream_index) {
40 case kBaseVideoOutputStreamIndex:
42 case kBaseAudioOutputStreamIndex:
44 case kBaseTextOutputStreamIndex:
47 return base::SizeTToString(stream_index);
51 bool GetStreamIndex(
const std::string& stream_label,
size_t* stream_index) {
53 if (stream_label ==
"video") {
54 *stream_index = kBaseVideoOutputStreamIndex;
55 }
else if (stream_label ==
"audio") {
56 *stream_index = kBaseAudioOutputStreamIndex;
57 }
else if (stream_label ==
"text") {
58 *stream_index = kBaseTextOutputStreamIndex;
61 if (!base::StringToSizeT(stream_label, stream_index)) {
62 LOG(ERROR) <<
"Invalid argument --stream=" << stream_label <<
"; " 63 <<
"should be 'audio', 'video', 'text', or a number";
76 : file_name_(file_name), buffer_(new uint8_t[kBufSize]) {}
84 key_source_ = std::move(key_source);
88 LOG(INFO) <<
"Demuxer::Run() on file '" << file_name_ <<
"'.";
89 Status status = InitializeParser();
92 while (!all_streams_ready_ && status.ok())
96 if (all_streams_ready_ && output_handlers().empty())
98 if (!init_event_status_.ok())
99 return init_event_status_;
103 for (
const auto& pair : output_handlers()) {
104 if (std::find(stream_indexes_.begin(), stream_indexes_.end(), pair.first) ==
105 stream_indexes_.end()) {
106 LOG(ERROR) <<
"Invalid argument, stream=" << GetStreamLabel(pair.first)
107 <<
" not available.";
108 return Status(error::INVALID_ARGUMENT,
"Stream not available");
112 while (!cancelled_ && status.ok())
114 if (cancelled_ && status.ok())
115 return Status(error::CANCELLED,
"Demuxer run cancelled");
117 if (status.error_code() == error::END_OF_STREAM) {
118 for (
size_t stream_index : stream_indexes_) {
133 std::shared_ptr<MediaHandler> handler) {
134 size_t stream_index = kInvalidStreamIndex;
135 if (!GetStreamIndex(stream_label, &stream_index)) {
136 return Status(error::INVALID_ARGUMENT,
137 "Invalid stream: " + stream_label);
143 const std::string& language_override) {
144 size_t stream_index = kInvalidStreamIndex;
145 if (!GetStreamIndex(stream_label, &stream_index))
146 LOG(WARNING) <<
"Invalid stream for language override " << stream_label;
147 language_overrides_[stream_index] = language_override;
150 Demuxer::QueuedSample::QueuedSample(uint32_t local_track_id,
151 std::shared_ptr<MediaSample> local_sample)
152 : track_id(local_track_id), sample(local_sample) {}
154 Demuxer::QueuedSample::~QueuedSample() {}
156 Status Demuxer::InitializeParser() {
157 DCHECK(!media_file_);
158 DCHECK(!all_streams_ready_);
160 LOG(INFO) <<
"Initialize Demuxer for file '" << file_name_ <<
"'.";
162 media_file_ =
File::Open(file_name_.c_str(),
"r");
164 return Status(error::FILE_FAILURE,
165 "Cannot open file for reading " + file_name_);
169 int64_t bytes_read = 0;
170 while (static_cast<size_t>(bytes_read) < kInitBufSize) {
171 int64_t read_result =
172 media_file_->
Read(buffer_.get() + bytes_read, kInitBufSize);
174 return Status(error::FILE_FAILURE,
"Cannot read file " + file_name_);
175 if (read_result == 0)
177 bytes_read += read_result;
179 container_name_ = DetermineContainer(buffer_.get(), bytes_read);
182 switch (container_name_) {
186 case CONTAINER_MPEG2TS:
194 case CONTAINER_MPEG2PS:
195 FALLTHROUGH_INTENDED;
205 return Status(error::UNIMPLEMENTED,
"Container not supported.");
208 parser_->Init(base::Bind(&Demuxer::ParserInitEvent, base::Unretained(
this)),
209 base::Bind(&Demuxer::NewSampleEvent, base::Unretained(
this)),
213 if (container_name_ == CONTAINER_MOV)
215 if (!parser_->Parse(buffer_.get(), bytes_read)) {
216 return Status(error::PARSER_FAILURE,
217 "Cannot parse media file " + file_name_);
222 void Demuxer::ParserInitEvent(
223 const std::vector<std::shared_ptr<StreamInfo>>& stream_infos) {
224 if (dump_stream_info_) {
225 printf(
"\nFile \"%s\":\n", file_name_.c_str());
226 printf(
"Found %zu stream(s).\n", stream_infos.size());
227 for (
size_t i = 0; i < stream_infos.size(); ++i)
228 printf(
"Stream [%zu] %s\n", i, stream_infos[i]->ToString().c_str());
231 int base_stream_index = 0;
232 bool video_handler_set =
233 output_handlers().find(kBaseVideoOutputStreamIndex) !=
234 output_handlers().end();
235 bool audio_handler_set =
236 output_handlers().find(kBaseAudioOutputStreamIndex) !=
237 output_handlers().end();
238 bool text_handler_set =
239 output_handlers().find(kBaseTextOutputStreamIndex) !=
240 output_handlers().end();
241 for (
const std::shared_ptr<StreamInfo>& stream_info : stream_infos) {
242 size_t stream_index = base_stream_index;
243 if (video_handler_set && stream_info->stream_type() == kStreamVideo) {
244 stream_index = kBaseVideoOutputStreamIndex;
246 video_handler_set =
false;
248 if (audio_handler_set && stream_info->stream_type() == kStreamAudio) {
249 stream_index = kBaseAudioOutputStreamIndex;
251 audio_handler_set =
false;
253 if (text_handler_set && stream_info->stream_type() == kStreamText) {
254 stream_index = kBaseTextOutputStreamIndex;
255 text_handler_set =
false;
258 const bool handler_set =
259 output_handlers().find(stream_index) != output_handlers().end();
261 track_id_to_stream_index_map_[stream_info->track_id()] = stream_index;
262 stream_indexes_.push_back(stream_index);
263 auto iter = language_overrides_.find(stream_index);
264 if (iter != language_overrides_.end() &&
265 stream_info->stream_type() != kStreamVideo) {
266 stream_info->set_language(iter->second);
268 if (stream_info->is_encrypted()) {
269 init_event_status_.
Update(
Status(error::INVALID_ARGUMENT,
270 "A decryption key source is not " 271 "provided for an encrypted stream."));
273 init_event_status_.
Update(
277 track_id_to_stream_index_map_[stream_info->track_id()] =
282 all_streams_ready_ =
true;
285 bool Demuxer::NewSampleEvent(uint32_t track_id,
286 const std::shared_ptr<MediaSample>& sample) {
287 if (!all_streams_ready_) {
288 if (queued_samples_.size() >= kQueuedSamplesLimit) {
289 LOG(ERROR) <<
"Queued samples limit reached: " << kQueuedSamplesLimit;
292 queued_samples_.push_back(QueuedSample(track_id, sample));
295 if (!init_event_status_.ok()) {
298 while (!queued_samples_.empty()) {
299 if (!PushSample(queued_samples_.front().track_id,
300 queued_samples_.front().sample)) {
303 queued_samples_.pop_front();
305 return PushSample(track_id, sample);
308 bool Demuxer::PushSample(uint32_t track_id,
309 const std::shared_ptr<MediaSample>& sample) {
310 auto stream_index_iter = track_id_to_stream_index_map_.find(track_id);
311 if (stream_index_iter == track_id_to_stream_index_map_.end()) {
312 LOG(ERROR) <<
"Track " << track_id <<
" not found.";
315 if (stream_index_iter->second == kInvalidStreamIndex)
319 LOG(ERROR) <<
"Failed to process sample " << stream_index_iter->second
330 int64_t bytes_read = media_file_->
Read(buffer_.get(), kBufSize);
331 if (bytes_read == 0) {
332 if (!parser_->Flush())
333 return Status(error::PARSER_FAILURE,
"Failed to flush.");
334 return Status(error::END_OF_STREAM,
"");
335 }
else if (bytes_read < 0) {
336 return Status(error::FILE_FAILURE,
"Cannot read file " + file_name_);
339 return parser_->Parse(buffer_.get(), bytes_read)
341 :
Status(error::PARSER_FAILURE,
342 "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.