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
;
45 kWaitingForSampleData
,
50 bool ParseBox(bool* err
);
51 bool ParseMoov(mp4::BoxReader
* reader
);
52 bool ParseMoof(mp4::BoxReader
* reader
);
54 void 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 |max_clear_offset|, updating the |mdat_tail_| value so that framing can
61 // be retained after all 'mdat' information has been read. |max_clear_offset|
62 // is the upper bound on what can be removed from |queue_|. Anything below
63 // this offset is no longer needed by the parser.
64 // Returns 'true' on success, 'false' if there was an error.
65 bool ReadAndDiscardMDATsUntil(int64 max_clear_offset
);
67 void ChangeState(State new_state
);
70 bool PrepareAVCBuffer(const AVCDecoderConfigurationRecord
& avc_config
,
71 std::vector
<uint8
>* frame_buf
,
72 std::vector
<SubsampleEntry
>* subsamples
) const;
73 bool PrepareAACBuffer(const AAC
& aac_config
,
74 std::vector
<uint8
>* frame_buf
,
75 std::vector
<SubsampleEntry
>* subsamples
) const;
76 bool EnqueueSample(BufferQueue
* audio_buffers
,
77 BufferQueue
* video_buffers
,
79 bool SendAndFlushSamples(BufferQueue
* audio_buffers
,
80 BufferQueue
* video_buffers
);
84 // Checks to see if we have enough data in |queue_| to transition to
85 // kEmittingSamples and start enqueuing samples.
86 bool HaveEnoughDataToEnqueueSamples();
88 // Sets |highest_end_offset_| based on the data in |moov_|
89 // and |moof|. Returns true if |highest_end_offset_| was successfully
91 bool ComputeHighestEndOffset(const MovieFragment
& moof
);
95 NewConfigCB config_cb_
;
96 NewBuffersCB new_buffers_cb_
;
97 NeedKeyCB need_key_cb_
;
98 NewMediaSegmentCB new_segment_cb_
;
99 base::Closure end_of_segment_cb_
;
102 OffsetByteQueue queue_
;
104 // These two parameters are only valid in the |kEmittingSegments| state.
106 // |moof_head_| is the offset of the start of the most recently parsed moof
107 // block. All byte offsets in sample information are relative to this offset,
108 // as mandated by the Media Source spec.
110 // |mdat_tail_| is the stream offset of the end of the current 'mdat' box.
111 // Valid iff it is greater than the head of the queue.
114 // The highest end offset in the current moof. This offset is
115 // relative to |moof_head_|. This value is used to make sure we have collected
116 // enough bytes to parse all samples and aux_info in the current moof.
117 int64 highest_end_offset_
;
119 scoped_ptr
<mp4::Movie
> moov_
;
120 scoped_ptr
<mp4::TrackRunIterator
> runs_
;
124 uint32 audio_track_id_
;
125 uint32 video_track_id_
;
126 // The object types allowed for audio tracks.
127 std::set
<int> audio_object_types_
;
129 bool is_audio_track_encrypted_
;
130 bool is_video_track_encrypted_
;
132 DISALLOW_COPY_AND_ASSIGN(MP4StreamParser
);
138 #endif // MEDIA_FORMATS_MP4_MP4_STREAM_PARSER_H_