1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_FORMATS_MP4_MP4_STREAM_PARSER_H_
6 #define MEDIA_FORMATS_MP4_MP4_STREAM_PARSER_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "media/base/media_export.h"
16 #include "media/base/stream_parser.h"
17 #include "media/formats/common/offset_byte_queue.h"
18 #include "media/formats/mp4/track_run_iterator.h"
26 class MEDIA_EXPORT MP4StreamParser : public StreamParser {
28 MP4StreamParser(const std::set<int>& audio_object_types, bool has_sbr);
29 virtual ~MP4StreamParser();
31 virtual void Init(const InitCB& init_cb, const NewConfigCB& config_cb,
32 const NewBuffersCB& new_buffers_cb,
33 bool ignore_text_tracks,
34 const NeedKeyCB& need_key_cb,
35 const NewMediaSegmentCB& new_segment_cb,
36 const base::Closure& end_of_segment_cb,
37 const LogCB& log_cb) OVERRIDE;
38 virtual void Flush() OVERRIDE;
39 virtual bool Parse(const uint8* buf, int size) OVERRIDE;
49 bool ParseBox(bool* err);
50 bool ParseMoov(mp4::BoxReader* reader);
51 bool ParseMoof(mp4::BoxReader* reader);
53 void EmitNeedKeyIfNecessary(
54 const std::vector<ProtectionSystemSpecificHeader>& headers);
56 // To retain proper framing, each 'mdat' atom must be read; to limit memory
57 // usage, the atom's data needs to be discarded incrementally as frames are
58 // extracted from the stream. This function discards data from the stream up
59 // to |offset|, updating the |mdat_tail_| value so that framing can be
60 // retained after all 'mdat' information has been read.
61 // Returns 'true' on success, 'false' if there was an error.
62 bool ReadAndDiscardMDATsUntil(const int64 offset);
64 void ChangeState(State new_state);
67 bool PrepareAVCBuffer(const AVCDecoderConfigurationRecord& avc_config,
68 std::vector<uint8>* frame_buf,
69 std::vector<SubsampleEntry>* subsamples) const;
70 bool PrepareAACBuffer(const AAC& aac_config,
71 std::vector<uint8>* frame_buf,
72 std::vector<SubsampleEntry>* subsamples) const;
73 bool EnqueueSample(BufferQueue* audio_buffers,
74 BufferQueue* video_buffers,
76 bool SendAndFlushSamples(BufferQueue* audio_buffers,
77 BufferQueue* video_buffers);
83 NewConfigCB config_cb_;
84 NewBuffersCB new_buffers_cb_;
85 NeedKeyCB need_key_cb_;
86 NewMediaSegmentCB new_segment_cb_;
87 base::Closure end_of_segment_cb_;
90 OffsetByteQueue queue_;
92 // These two parameters are only valid in the |kEmittingSegments| state.
94 // |moof_head_| is the offset of the start of the most recently parsed moof
95 // block. All byte offsets in sample information are relative to this offset,
96 // as mandated by the Media Source spec.
98 // |mdat_tail_| is the stream offset of the end of the current 'mdat' box.
99 // Valid iff it is greater than the head of the queue.
102 scoped_ptr<mp4::Movie> moov_;
103 scoped_ptr<mp4::TrackRunIterator> runs_;
107 uint32 audio_track_id_;
108 uint32 video_track_id_;
109 // The object types allowed for audio tracks.
110 std::set<int> audio_object_types_;
112 bool is_audio_track_encrypted_;
113 bool is_video_track_encrypted_;
115 DISALLOW_COPY_AND_ASSIGN(MP4StreamParser);
121 #endif // MEDIA_FORMATS_MP4_MP4_STREAM_PARSER_H_