#ifndef MEDIA_FORMATS_MP4_MP4_STREAM_PARSER_H_
#define MEDIA_FORMATS_MP4_MP4_STREAM_PARSER_H_
#include <set>
#include <vector>
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "media/base/media_export.h"
#include "media/base/stream_parser.h"
#include "media/formats/common/offset_byte_queue.h"
#include "media/formats/mp4/track_run_iterator.h"
namespace media {
namespace mp4 {
struct Movie;
class BoxReader;
class MEDIA_EXPORT MP4StreamParser : public StreamParser {
public:
MP4StreamParser(const std::set<int>& audio_object_types, bool has_sbr);
virtual ~MP4StreamParser();
virtual void Init(const InitCB& init_cb, const NewConfigCB& config_cb,
const NewBuffersCB& new_buffers_cb,
bool ignore_text_tracks,
const NeedKeyCB& need_key_cb,
const NewMediaSegmentCB& new_segment_cb,
const base::Closure& end_of_segment_cb,
const LogCB& log_cb) OVERRIDE;
virtual void Flush() OVERRIDE;
virtual bool Parse(const uint8* buf, int size) OVERRIDE;
private:
enum State {
kWaitingForInit,
kParsingBoxes,
kEmittingSamples,
kError
};
bool ParseBox(bool* err);
bool ParseMoov(mp4::BoxReader* reader);
bool ParseMoof(mp4::BoxReader* reader);
void EmitNeedKeyIfNecessary(
const std::vector<ProtectionSystemSpecificHeader>& headers);
bool ReadAndDiscardMDATsUntil(const int64 offset);
void ChangeState(State new_state);
bool EmitConfigs();
bool PrepareAVCBuffer(const AVCDecoderConfigurationRecord& avc_config,
std::vector<uint8>* frame_buf,
std::vector<SubsampleEntry>* subsamples) const;
bool PrepareAACBuffer(const AAC& aac_config,
std::vector<uint8>* frame_buf,
std::vector<SubsampleEntry>* subsamples) const;
bool EnqueueSample(BufferQueue* audio_buffers,
BufferQueue* video_buffers,
bool* err);
bool SendAndFlushSamples(BufferQueue* audio_buffers,
BufferQueue* video_buffers);
void Reset();
State state_;
InitCB init_cb_;
NewConfigCB config_cb_;
NewBuffersCB new_buffers_cb_;
NeedKeyCB need_key_cb_;
NewMediaSegmentCB new_segment_cb_;
base::Closure end_of_segment_cb_;
LogCB log_cb_;
OffsetByteQueue queue_;
int64 moof_head_;
int64 mdat_tail_;
scoped_ptr<mp4::Movie> moov_;
scoped_ptr<mp4::TrackRunIterator> runs_;
bool has_audio_;
bool has_video_;
uint32 audio_track_id_;
uint32 video_track_id_;
std::set<int> audio_object_types_;
bool has_sbr_;
bool is_audio_track_encrypted_;
bool is_video_track_encrypted_;
DISALLOW_COPY_AND_ASSIGN(MP4StreamParser);
};
}
}
#endif