1 // Copyright (c) 2012 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_MP4_MP4_STREAM_PARSER_H_
6 #define MEDIA_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/mp4/offset_byte_queue.h"
18 #include "media/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
& audio_cb
,
33 const NewBuffersCB
& video_cb
,
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 // Returns 'true' if sent or not required, 'false' if there was an error.
54 bool EmitNeedKeyIfNecessary(
55 const std::vector
<ProtectionSystemSpecificHeader
>& headers
);
57 // To retain proper framing, each 'mdat' atom must be read; to limit memory
58 // usage, the atom's data needs to be discarded incrementally as frames are
59 // extracted from the stream. This function discards data from the stream up
60 // to |offset|, updating the |mdat_tail_| value so that framing can be
61 // retained after all 'mdat' information has been read.
62 // Returns 'true' on success, 'false' if there was an error.
63 bool ReadAndDiscardMDATsUntil(const int64 offset
);
65 void ChangeState(State new_state
);
68 bool PrepareAVCBuffer(const AVCDecoderConfigurationRecord
& avc_config
,
69 std::vector
<uint8
>* frame_buf
,
70 std::vector
<SubsampleEntry
>* subsamples
) const;
71 bool PrepareAACBuffer(const AAC
& aac_config
,
72 std::vector
<uint8
>* frame_buf
,
73 std::vector
<SubsampleEntry
>* subsamples
) const;
74 bool EnqueueSample(BufferQueue
* audio_buffers
,
75 BufferQueue
* video_buffers
,
77 bool SendAndFlushSamples(BufferQueue
* audio_buffers
,
78 BufferQueue
* video_buffers
);
84 NewConfigCB config_cb_
;
85 NewBuffersCB audio_cb_
;
86 NewBuffersCB video_cb_
;
87 NeedKeyCB need_key_cb_
;
88 NewMediaSegmentCB new_segment_cb_
;
89 base::Closure end_of_segment_cb_
;
92 OffsetByteQueue queue_
;
94 // These two parameters are only valid in the |kEmittingSegments| state.
96 // |moof_head_| is the offset of the start of the most recently parsed moof
97 // block. All byte offsets in sample information are relative to this offset,
98 // as mandated by the Media Source spec.
100 // |mdat_tail_| is the stream offset of the end of the current 'mdat' box.
101 // Valid iff it is greater than the head of the queue.
104 scoped_ptr
<mp4::Movie
> moov_
;
105 scoped_ptr
<mp4::TrackRunIterator
> runs_
;
109 uint32 audio_track_id_
;
110 uint32 video_track_id_
;
111 // The object types allowed for audio tracks.
112 std::set
<int> audio_object_types_
;
114 bool is_audio_track_encrypted_
;
115 bool is_video_track_encrypted_
;
117 DISALLOW_COPY_AND_ASSIGN(MP4StreamParser
);
123 #endif // MEDIA_MP4_MP4_STREAM_PARSER_H_