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
|
|
|
|
|
|
|
#include "media/base/demuxer.h"
|
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/stl_util.h"
|
|
|
|
#include "media/base/container_names.h"
|
|
|
|
#include "media/base/decryptor_source.h"
|
2014-04-09 17:34:55 +00:00
|
|
|
#include "media/base/media_sample.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
#include "media/base/media_stream.h"
|
|
|
|
#include "media/base/stream_info.h"
|
|
|
|
#include "media/file/file.h"
|
2014-04-15 23:51:32 +00:00
|
|
|
#include "media/formats/mp2t/mp2t_media_parser.h"
|
2014-04-10 21:42:38 +00:00
|
|
|
#include "media/formats/mp4/mp4_media_parser.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
namespace {
|
2014-03-21 17:26:49 +00:00
|
|
|
const size_t kBufSize = 0x40000; // 256KB.
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
Demuxer::Demuxer(const std::string& file_name,
|
|
|
|
DecryptorSource* decryptor_source)
|
|
|
|
: decryptor_source_(decryptor_source),
|
|
|
|
file_name_(file_name),
|
|
|
|
media_file_(NULL),
|
|
|
|
init_event_received_(false),
|
|
|
|
buffer_(new uint8[kBufSize]) {}
|
|
|
|
|
|
|
|
Demuxer::~Demuxer() {
|
|
|
|
if (media_file_)
|
|
|
|
media_file_->Close();
|
|
|
|
STLDeleteElements(&streams_);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Demuxer::Initialize() {
|
2014-04-09 17:34:55 +00:00
|
|
|
DCHECK(!media_file_);
|
2013-10-11 21:44:55 +00:00
|
|
|
DCHECK(!init_event_received_);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Determine media container.
|
|
|
|
int64 bytes_read = media_file_->Read(buffer_.get(), kBufSize);
|
|
|
|
if (bytes_read <= 0)
|
|
|
|
return Status(error::FILE_FAILURE, "Cannot read file " + file_name_);
|
|
|
|
MediaContainerName container = DetermineContainer(buffer_.get(), bytes_read);
|
|
|
|
|
|
|
|
// Initialize media parser.
|
|
|
|
switch (container) {
|
|
|
|
case CONTAINER_MOV:
|
|
|
|
parser_.reset(new mp4::MP4MediaParser());
|
|
|
|
break;
|
2014-04-15 23:51:32 +00:00
|
|
|
case CONTAINER_MPEG2TS:
|
|
|
|
parser_.reset(new mp2t::MediaParser());
|
|
|
|
break;
|
2013-10-11 21:44:55 +00:00
|
|
|
default:
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
return Status(error::UNIMPLEMENTED, "Container not supported.");
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:32:44 +00:00
|
|
|
parser_->Init(base::Bind(&Demuxer::ParserInitEvent, base::Unretained(this)),
|
|
|
|
base::Bind(&Demuxer::NewSampleEvent, base::Unretained(this)),
|
|
|
|
base::Bind(&Demuxer::KeyNeededEvent, base::Unretained(this)));
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
if (!parser_->Parse(buffer_.get(), bytes_read))
|
|
|
|
return Status(error::PARSER_FAILURE,
|
|
|
|
"Cannot parse media file " + file_name_);
|
|
|
|
|
|
|
|
Status status;
|
|
|
|
while (!init_event_received_) {
|
2013-11-12 20:32:44 +00:00
|
|
|
if (!(status = Parse()).ok())
|
|
|
|
break;
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Demuxer::ParserInitEvent(
|
|
|
|
const std::vector<scoped_refptr<StreamInfo> >& streams) {
|
|
|
|
init_event_received_ = true;
|
|
|
|
|
|
|
|
std::vector<scoped_refptr<StreamInfo> >::const_iterator it = streams.begin();
|
|
|
|
for (; it != streams.end(); ++it) {
|
|
|
|
streams_.push_back(new MediaStream(*it, this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:32:44 +00:00
|
|
|
bool Demuxer::NewSampleEvent(uint32 track_id,
|
|
|
|
const scoped_refptr<MediaSample>& sample) {
|
2013-10-11 21:44:55 +00:00
|
|
|
std::vector<MediaStream*>::iterator it = streams_.begin();
|
|
|
|
for (; it != streams_.end(); ++it) {
|
|
|
|
if (track_id == (*it)->info()->track_id()) {
|
|
|
|
return (*it)->PushSample(sample).ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:32:44 +00:00
|
|
|
void Demuxer::KeyNeededEvent(MediaContainerName container,
|
|
|
|
scoped_ptr<uint8[]> init_data,
|
|
|
|
int init_data_size) {
|
2013-10-11 21:44:55 +00:00
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Demuxer::Run() {
|
|
|
|
Status status;
|
|
|
|
|
|
|
|
// Start the streams.
|
|
|
|
for (std::vector<MediaStream*>::iterator it = streams_.begin();
|
|
|
|
it != streams_.end();
|
|
|
|
++it) {
|
|
|
|
status = (*it)->Start(MediaStream::kPush);
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:32:44 +00:00
|
|
|
while ((status = Parse()).ok())
|
|
|
|
continue;
|
2014-04-09 17:34:55 +00:00
|
|
|
|
|
|
|
if (status.error_code() == error::END_OF_STREAM) {
|
|
|
|
// Push EOS sample to muxer to indicate end of stream.
|
|
|
|
const scoped_refptr<MediaSample>& sample = MediaSample::CreateEOSBuffer();
|
|
|
|
for (std::vector<MediaStream*>::iterator it = streams_.begin();
|
|
|
|
it != streams_.end();
|
|
|
|
++it) {
|
|
|
|
status = (*it)->PushSample(sample);
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
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
|
|
|
|
|
|
|
int64 bytes_read = media_file_->Read(buffer_.get(), kBufSize);
|
|
|
|
if (bytes_read <= 0) {
|
2013-11-12 20:32:44 +00:00
|
|
|
return media_file_->Eof()
|
|
|
|
? Status(error::END_OF_STREAM, "End of stream.")
|
|
|
|
: 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
|