2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2017-03-28 19:31:32 +00:00
|
|
|
#include "packager/media/demuxer/demuxer.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
|
2017-02-21 18:36:50 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/bind.h"
|
|
|
|
#include "packager/base/logging.h"
|
2017-02-21 18:36:50 +00:00
|
|
|
#include "packager/base/strings/string_number_conversions.h"
|
2017-07-10 18:26:22 +00:00
|
|
|
#include "packager/file/file.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/decryptor_source.h"
|
|
|
|
#include "packager/media/base/key_source.h"
|
2018-01-10 00:04:40 +00:00
|
|
|
#include "packager/media/base/macros.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/media_sample.h"
|
|
|
|
#include "packager/media/base/stream_info.h"
|
|
|
|
#include "packager/media/formats/mp2t/mp2t_media_parser.h"
|
|
|
|
#include "packager/media/formats/mp4/mp4_media_parser.h"
|
2015-10-26 20:50:22 +00:00
|
|
|
#include "packager/media/formats/webm/webm_media_parser.h"
|
2020-07-07 21:29:43 +00:00
|
|
|
#include "packager/media/formats/webvtt/webvtt_parser.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/wvm/wvm_media_parser.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
namespace {
|
2016-01-21 18:30:29 +00:00
|
|
|
// 65KB, sufficient to determine the container and likely all init data.
|
|
|
|
const size_t kInitBufSize = 0x10000;
|
|
|
|
const size_t kBufSize = 0x200000; // 2MB
|
|
|
|
// Maximum number of allowed queued samples. If we are receiving a lot of
|
|
|
|
// samples before seeing init_event, something is not right. The number
|
|
|
|
// set here is arbitrary though.
|
|
|
|
const size_t kQueuedSamplesLimit = 10000;
|
2017-03-03 00:10:30 +00:00
|
|
|
const size_t kInvalidStreamIndex = static_cast<size_t>(-1);
|
|
|
|
const size_t kBaseVideoOutputStreamIndex = 0x100;
|
|
|
|
const size_t kBaseAudioOutputStreamIndex = 0x200;
|
2015-11-23 23:12:04 +00:00
|
|
|
const size_t kBaseTextOutputStreamIndex = 0x300;
|
2017-02-21 18:36:50 +00:00
|
|
|
|
2017-03-03 00:10:30 +00:00
|
|
|
std::string GetStreamLabel(size_t stream_index) {
|
2017-02-21 18:36:50 +00:00
|
|
|
switch (stream_index) {
|
|
|
|
case kBaseVideoOutputStreamIndex:
|
|
|
|
return "video";
|
|
|
|
case kBaseAudioOutputStreamIndex:
|
|
|
|
return "audio";
|
2015-11-23 23:12:04 +00:00
|
|
|
case kBaseTextOutputStreamIndex:
|
|
|
|
return "text";
|
2017-02-21 18:36:50 +00:00
|
|
|
default:
|
2017-03-03 00:10:30 +00:00
|
|
|
return base::SizeTToString(stream_index);
|
2017-02-21 18:36:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 00:10:30 +00:00
|
|
|
bool GetStreamIndex(const std::string& stream_label, size_t* stream_index) {
|
2017-02-21 18:36:50 +00:00
|
|
|
DCHECK(stream_index);
|
|
|
|
if (stream_label == "video") {
|
|
|
|
*stream_index = kBaseVideoOutputStreamIndex;
|
|
|
|
} else if (stream_label == "audio") {
|
|
|
|
*stream_index = kBaseAudioOutputStreamIndex;
|
2015-11-23 23:12:04 +00:00
|
|
|
} else if (stream_label == "text") {
|
|
|
|
*stream_index = kBaseTextOutputStreamIndex;
|
2017-02-21 18:36:50 +00:00
|
|
|
} else {
|
|
|
|
// Expect stream_label to be a zero based stream id.
|
2017-03-03 00:10:30 +00:00
|
|
|
if (!base::StringToSizeT(stream_label, stream_index)) {
|
2017-02-21 18:36:50 +00:00
|
|
|
LOG(ERROR) << "Invalid argument --stream=" << stream_label << "; "
|
2015-11-23 23:12:04 +00:00
|
|
|
<< "should be 'audio', 'video', 'text', or a number";
|
2017-02-21 18:36:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-10-11 21:44:55 +00:00
|
|
|
namespace media {
|
|
|
|
|
2014-08-25 22:51:19 +00:00
|
|
|
Demuxer::Demuxer(const std::string& file_name)
|
2017-02-21 18:36:50 +00:00
|
|
|
: file_name_(file_name), buffer_(new uint8_t[kBufSize]) {}
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
Demuxer::~Demuxer() {
|
|
|
|
if (media_file_)
|
|
|
|
media_file_->Close();
|
|
|
|
}
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
void Demuxer::SetKeySource(std::unique_ptr<KeySource> key_source) {
|
|
|
|
key_source_ = std::move(key_source);
|
2014-08-25 22:51:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 18:36:50 +00:00
|
|
|
Status Demuxer::Run() {
|
|
|
|
LOG(INFO) << "Demuxer::Run() on file '" << file_name_ << "'.";
|
|
|
|
Status status = InitializeParser();
|
|
|
|
// ParserInitEvent callback is called after a few calls to Parse(), which sets
|
|
|
|
// up the streams. Only after that, we can verify the outputs below.
|
|
|
|
while (!all_streams_ready_ && status.ok())
|
|
|
|
status.Update(Parse());
|
|
|
|
// If no output is defined, then return success after receiving all stream
|
|
|
|
// info.
|
|
|
|
if (all_streams_ready_ && output_handlers().empty())
|
|
|
|
return Status::OK;
|
2017-05-31 18:14:11 +00:00
|
|
|
if (!init_event_status_.ok())
|
|
|
|
return init_event_status_;
|
2017-03-28 19:31:32 +00:00
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
2017-02-21 18:36:50 +00:00
|
|
|
// Check if all specified outputs exists.
|
|
|
|
for (const auto& pair : output_handlers()) {
|
|
|
|
if (std::find(stream_indexes_.begin(), stream_indexes_.end(), pair.first) ==
|
|
|
|
stream_indexes_.end()) {
|
|
|
|
LOG(ERROR) << "Invalid argument, stream=" << GetStreamLabel(pair.first)
|
|
|
|
<< " not available.";
|
|
|
|
return Status(error::INVALID_ARGUMENT, "Stream not available");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!cancelled_ && status.ok())
|
|
|
|
status.Update(Parse());
|
|
|
|
if (cancelled_ && status.ok())
|
|
|
|
return Status(error::CANCELLED, "Demuxer run cancelled");
|
|
|
|
|
|
|
|
if (status.error_code() == error::END_OF_STREAM) {
|
2017-03-03 00:10:30 +00:00
|
|
|
for (size_t stream_index : stream_indexes_) {
|
2017-02-22 20:14:26 +00:00
|
|
|
status = FlushDownstream(stream_index);
|
2017-02-21 18:36:50 +00:00
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Demuxer::Cancel() {
|
|
|
|
cancelled_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Demuxer::SetHandler(const std::string& stream_label,
|
|
|
|
std::shared_ptr<MediaHandler> handler) {
|
2017-03-03 00:10:30 +00:00
|
|
|
size_t stream_index = kInvalidStreamIndex;
|
2017-02-21 18:36:50 +00:00
|
|
|
if (!GetStreamIndex(stream_label, &stream_index)) {
|
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"Invalid stream: " + stream_label);
|
|
|
|
}
|
|
|
|
return MediaHandler::SetHandler(stream_index, std::move(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Demuxer::SetLanguageOverride(const std::string& stream_label,
|
|
|
|
const std::string& language_override) {
|
2017-03-03 00:10:30 +00:00
|
|
|
size_t stream_index = kInvalidStreamIndex;
|
2017-02-21 18:36:50 +00:00
|
|
|
if (!GetStreamIndex(stream_label, &stream_index))
|
|
|
|
LOG(WARNING) << "Invalid stream for language override " << stream_label;
|
|
|
|
language_overrides_[stream_index] = language_override;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Demuxer::InitializeParser() {
|
2014-04-09 17:34:55 +00:00
|
|
|
DCHECK(!media_file_);
|
2017-02-21 18:36:50 +00:00
|
|
|
DCHECK(!all_streams_ready_);
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2015-05-12 00:52:53 +00:00
|
|
|
LOG(INFO) << "Initialize Demuxer for file '" << file_name_ << "'.";
|
|
|
|
|
2013-10-11 21:44:55 +00:00
|
|
|
media_file_ = File::Open(file_name_.c_str(), "r");
|
2014-04-09 17:34:55 +00:00
|
|
|
if (!media_file_) {
|
2013-10-11 21:44:55 +00:00
|
|
|
return Status(error::FILE_FAILURE,
|
2014-04-09 17:34:55 +00:00
|
|
|
"Cannot open file for reading " + file_name_);
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 18:45:49 +00:00
|
|
|
// Read enough bytes before detecting the container.
|
2017-03-31 21:37:02 +00:00
|
|
|
int64_t bytes_read = 0;
|
|
|
|
while (static_cast<size_t>(bytes_read) < kInitBufSize) {
|
2015-03-20 18:45:49 +00:00
|
|
|
int64_t read_result =
|
|
|
|
media_file_->Read(buffer_.get() + bytes_read, kInitBufSize);
|
|
|
|
if (read_result < 0)
|
|
|
|
return Status(error::FILE_FAILURE, "Cannot read file " + file_name_);
|
|
|
|
if (read_result == 0)
|
|
|
|
break;
|
|
|
|
bytes_read += read_result;
|
|
|
|
}
|
2015-06-02 21:41:49 +00:00
|
|
|
container_name_ = DetermineContainer(buffer_.get(), bytes_read);
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
// Initialize media parser.
|
2015-06-02 21:41:49 +00:00
|
|
|
switch (container_name_) {
|
2013-10-11 21:44:55 +00:00
|
|
|
case CONTAINER_MOV:
|
|
|
|
parser_.reset(new mp4::MP4MediaParser());
|
|
|
|
break;
|
2014-04-15 23:51:32 +00:00
|
|
|
case CONTAINER_MPEG2TS:
|
2014-04-16 23:22:31 +00:00
|
|
|
parser_.reset(new mp2t::Mp2tMediaParser());
|
2014-04-15 23:51:32 +00:00
|
|
|
break;
|
2018-01-10 00:04:40 +00:00
|
|
|
// Widevine classic (WVM) is derived from MPEG2PS. We do not support
|
|
|
|
// non-WVM MPEG2PS file, thus we do not differentiate between the two.
|
|
|
|
// Every MPEG2PS file is assumed to be WVM file. If it turns out not the
|
|
|
|
// case, an error will be reported when trying to parse the file as WVM
|
|
|
|
// file.
|
2014-07-14 21:35:57 +00:00
|
|
|
case CONTAINER_MPEG2PS:
|
2018-01-10 00:04:40 +00:00
|
|
|
FALLTHROUGH_INTENDED;
|
|
|
|
case CONTAINER_WVM:
|
2014-07-14 21:35:57 +00:00
|
|
|
parser_.reset(new wvm::WvmMediaParser());
|
|
|
|
break;
|
2015-10-26 20:50:22 +00:00
|
|
|
case CONTAINER_WEBM:
|
|
|
|
parser_.reset(new WebMMediaParser());
|
|
|
|
break;
|
2020-07-07 21:29:43 +00:00
|
|
|
case CONTAINER_WEBVTT:
|
|
|
|
parser_.reset(new WebVttParser());
|
|
|
|
break;
|
2018-11-09 01:06:05 +00:00
|
|
|
case CONTAINER_UNKNOWN: {
|
|
|
|
const int64_t kDumpSizeLimit = 512;
|
|
|
|
LOG(ERROR) << "Failed to detect the container type from the buffer: "
|
|
|
|
<< base::HexEncode(buffer_.get(),
|
|
|
|
std::min(bytes_read, kDumpSizeLimit));
|
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"Failed to detect the container type.");
|
|
|
|
}
|
2013-10-11 21:44:55 +00:00
|
|
|
default:
|
2018-11-09 01:06:05 +00:00
|
|
|
NOTIMPLEMENTED() << "Container " << container_name_
|
|
|
|
<< " is not supported.";
|
2013-10-11 21:44:55 +00:00
|
|
|
return Status(error::UNIMPLEMENTED, "Container not supported.");
|
|
|
|
}
|
|
|
|
|
2020-07-01 22:43:44 +00:00
|
|
|
parser_->Init(
|
|
|
|
base::Bind(&Demuxer::ParserInitEvent, base::Unretained(this)),
|
|
|
|
base::Bind(&Demuxer::NewMediaSampleEvent, base::Unretained(this)),
|
|
|
|
base::Bind(&Demuxer::NewTextSampleEvent, base::Unretained(this)),
|
|
|
|
key_source_.get());
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2015-05-21 00:38:09 +00:00
|
|
|
// Handle trailing 'moov'.
|
2019-10-18 22:15:31 +00:00
|
|
|
if (container_name_ == CONTAINER_MOV &&
|
|
|
|
File::IsLocalRegularFile(file_name_.c_str())) {
|
|
|
|
// TODO(kqyang): Investigate whether we can reuse the existing file
|
|
|
|
// descriptor |media_file_| instead of opening the same file again.
|
2015-05-21 00:38:09 +00:00
|
|
|
static_cast<mp4::MP4MediaParser*>(parser_.get())->LoadMoov(file_name_);
|
2019-10-18 22:15:31 +00:00
|
|
|
}
|
2014-10-14 02:34:29 +00:00
|
|
|
if (!parser_->Parse(buffer_.get(), bytes_read)) {
|
2017-02-21 18:36:50 +00:00
|
|
|
return Status(error::PARSER_FAILURE,
|
|
|
|
"Cannot parse media file " + file_name_);
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
2017-02-21 18:36:50 +00:00
|
|
|
return Status::OK;
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Demuxer::ParserInitEvent(
|
2017-01-24 00:55:02 +00:00
|
|
|
const std::vector<std::shared_ptr<StreamInfo>>& stream_infos) {
|
2017-02-21 18:36:50 +00:00
|
|
|
if (dump_stream_info_) {
|
|
|
|
printf("\nFile \"%s\":\n", file_name_.c_str());
|
|
|
|
printf("Found %zu stream(s).\n", stream_infos.size());
|
|
|
|
for (size_t i = 0; i < stream_infos.size(); ++i)
|
|
|
|
printf("Stream [%zu] %s\n", i, stream_infos[i]->ToString().c_str());
|
|
|
|
}
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2017-02-21 18:36:50 +00:00
|
|
|
int base_stream_index = 0;
|
|
|
|
bool video_handler_set =
|
|
|
|
output_handlers().find(kBaseVideoOutputStreamIndex) !=
|
|
|
|
output_handlers().end();
|
|
|
|
bool audio_handler_set =
|
|
|
|
output_handlers().find(kBaseAudioOutputStreamIndex) !=
|
|
|
|
output_handlers().end();
|
2015-11-23 23:12:04 +00:00
|
|
|
bool text_handler_set =
|
|
|
|
output_handlers().find(kBaseTextOutputStreamIndex) !=
|
|
|
|
output_handlers().end();
|
2017-02-21 18:36:50 +00:00
|
|
|
for (const std::shared_ptr<StreamInfo>& stream_info : stream_infos) {
|
2017-03-03 00:10:30 +00:00
|
|
|
size_t stream_index = base_stream_index;
|
2017-02-21 18:36:50 +00:00
|
|
|
if (video_handler_set && stream_info->stream_type() == kStreamVideo) {
|
|
|
|
stream_index = kBaseVideoOutputStreamIndex;
|
|
|
|
// Only for the first video stream.
|
|
|
|
video_handler_set = false;
|
|
|
|
}
|
|
|
|
if (audio_handler_set && stream_info->stream_type() == kStreamAudio) {
|
|
|
|
stream_index = kBaseAudioOutputStreamIndex;
|
|
|
|
// Only for the first audio stream.
|
|
|
|
audio_handler_set = false;
|
|
|
|
}
|
2015-11-23 23:12:04 +00:00
|
|
|
if (text_handler_set && stream_info->stream_type() == kStreamText) {
|
|
|
|
stream_index = kBaseTextOutputStreamIndex;
|
|
|
|
text_handler_set = false;
|
|
|
|
}
|
2017-02-21 18:36:50 +00:00
|
|
|
|
|
|
|
const bool handler_set =
|
|
|
|
output_handlers().find(stream_index) != output_handlers().end();
|
|
|
|
if (handler_set) {
|
|
|
|
track_id_to_stream_index_map_[stream_info->track_id()] = stream_index;
|
|
|
|
stream_indexes_.push_back(stream_index);
|
|
|
|
auto iter = language_overrides_.find(stream_index);
|
|
|
|
if (iter != language_overrides_.end() &&
|
|
|
|
stream_info->stream_type() != kStreamVideo) {
|
|
|
|
stream_info->set_language(iter->second);
|
|
|
|
}
|
2017-05-31 18:14:11 +00:00
|
|
|
if (stream_info->is_encrypted()) {
|
2018-01-23 00:09:14 +00:00
|
|
|
init_event_status_.Update(Status(error::INVALID_ARGUMENT,
|
|
|
|
"A decryption key source is not "
|
|
|
|
"provided for an encrypted stream."));
|
2017-05-31 18:14:11 +00:00
|
|
|
} else {
|
|
|
|
init_event_status_.Update(
|
|
|
|
DispatchStreamInfo(stream_index, stream_info));
|
|
|
|
}
|
2017-02-21 18:36:50 +00:00
|
|
|
} else {
|
|
|
|
track_id_to_stream_index_map_[stream_info->track_id()] =
|
|
|
|
kInvalidStreamIndex;
|
|
|
|
}
|
|
|
|
++base_stream_index;
|
|
|
|
}
|
|
|
|
all_streams_ready_ = true;
|
|
|
|
}
|
2016-01-21 18:30:29 +00:00
|
|
|
|
2020-07-01 22:43:44 +00:00
|
|
|
bool Demuxer::NewMediaSampleEvent(uint32_t track_id,
|
|
|
|
std::shared_ptr<MediaSample> sample) {
|
|
|
|
if (!all_streams_ready_) {
|
|
|
|
if (queued_media_samples_.size() >= kQueuedSamplesLimit) {
|
|
|
|
LOG(ERROR) << "Queued samples limit reached: " << kQueuedSamplesLimit;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
queued_media_samples_.emplace_back(track_id, sample);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!init_event_status_.ok()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!queued_media_samples_.empty()) {
|
|
|
|
if (!PushMediaSample(queued_media_samples_.front().track_id,
|
|
|
|
queued_media_samples_.front().sample)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
queued_media_samples_.pop_front();
|
|
|
|
}
|
|
|
|
return PushMediaSample(track_id, sample);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Demuxer::NewTextSampleEvent(uint32_t track_id,
|
|
|
|
std::shared_ptr<TextSample> sample) {
|
2017-02-21 18:36:50 +00:00
|
|
|
if (!all_streams_ready_) {
|
2020-07-01 22:43:44 +00:00
|
|
|
if (queued_text_samples_.size() >= kQueuedSamplesLimit) {
|
2016-01-21 18:30:29 +00:00
|
|
|
LOG(ERROR) << "Queued samples limit reached: " << kQueuedSamplesLimit;
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-01 22:43:44 +00:00
|
|
|
queued_text_samples_.emplace_back(track_id, sample);
|
2016-01-21 18:30:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-05-31 18:14:11 +00:00
|
|
|
if (!init_event_status_.ok()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-01 22:43:44 +00:00
|
|
|
|
|
|
|
while (!queued_text_samples_.empty()) {
|
|
|
|
if (!PushTextSample(queued_text_samples_.front().track_id,
|
|
|
|
queued_text_samples_.front().sample)) {
|
2016-01-21 18:30:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-01 22:43:44 +00:00
|
|
|
queued_text_samples_.pop_front();
|
2016-01-21 18:30:29 +00:00
|
|
|
}
|
2020-07-01 22:43:44 +00:00
|
|
|
return PushTextSample(track_id, sample);
|
2016-01-21 18:30:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 22:43:44 +00:00
|
|
|
bool Demuxer::PushMediaSample(uint32_t track_id,
|
|
|
|
std::shared_ptr<MediaSample> sample) {
|
2017-02-21 18:36:50 +00:00
|
|
|
auto stream_index_iter = track_id_to_stream_index_map_.find(track_id);
|
|
|
|
if (stream_index_iter == track_id_to_stream_index_map_.end()) {
|
|
|
|
LOG(ERROR) << "Track " << track_id << " not found.";
|
|
|
|
return false;
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
2017-02-21 18:36:50 +00:00
|
|
|
if (stream_index_iter->second == kInvalidStreamIndex)
|
|
|
|
return true;
|
|
|
|
Status status = DispatchMediaSample(stream_index_iter->second, sample);
|
|
|
|
if (!status.ok()) {
|
|
|
|
LOG(ERROR) << "Failed to process sample " << stream_index_iter->second
|
|
|
|
<< " " << status;
|
2020-07-01 22:43:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Demuxer::PushTextSample(uint32_t track_id,
|
|
|
|
std::shared_ptr<TextSample> sample) {
|
|
|
|
auto stream_index_iter = track_id_to_stream_index_map_.find(track_id);
|
|
|
|
if (stream_index_iter == track_id_to_stream_index_map_.end()) {
|
|
|
|
LOG(ERROR) << "Track " << track_id << " not found.";
|
|
|
|
return false;
|
2014-04-09 17:34:55 +00:00
|
|
|
}
|
2020-07-01 22:43:44 +00:00
|
|
|
if (stream_index_iter->second == kInvalidStreamIndex)
|
|
|
|
return true;
|
|
|
|
Status status = DispatchTextSample(stream_index_iter->second, sample);
|
|
|
|
if (!status.ok()) {
|
|
|
|
LOG(ERROR) << "Failed to process sample " << stream_index_iter->second
|
|
|
|
<< " " << status;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status Demuxer::Parse() {
|
2014-04-09 17:34:55 +00:00
|
|
|
DCHECK(media_file_);
|
|
|
|
DCHECK(parser_);
|
|
|
|
DCHECK(buffer_);
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t bytes_read = media_file_->Read(buffer_.get(), kBufSize);
|
2015-03-17 20:59:12 +00:00
|
|
|
if (bytes_read == 0) {
|
2016-01-21 18:30:29 +00:00
|
|
|
if (!parser_->Flush())
|
|
|
|
return Status(error::PARSER_FAILURE, "Failed to flush.");
|
2015-03-17 20:59:12 +00:00
|
|
|
return Status(error::END_OF_STREAM, "");
|
|
|
|
} else if (bytes_read < 0) {
|
2014-04-24 18:37:33 +00:00
|
|
|
return Status(error::FILE_FAILURE, "Cannot read file " + file_name_);
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return parser_->Parse(buffer_.get(), bytes_read)
|
|
|
|
? Status::OK
|
|
|
|
: Status(error::PARSER_FAILURE,
|
|
|
|
"Cannot parse media file " + file_name_);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|