Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / formats / mp4 / mp4_stream_parser.h
blobdad7df16c78d6ba28f574c87c32a55096a380b24
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_
8 #include <set>
9 #include <vector>
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"
20 namespace media {
21 namespace mp4 {
23 struct Movie;
24 class BoxReader;
26 class MEDIA_EXPORT MP4StreamParser : public StreamParser {
27 public:
28 MP4StreamParser(const std::set<int>& audio_object_types, bool has_sbr);
29 ~MP4StreamParser() override;
31 void Init(const InitCB& init_cb,
32 const NewConfigCB& config_cb,
33 const NewBuffersCB& new_buffers_cb,
34 bool ignore_text_tracks,
35 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
36 const NewMediaSegmentCB& new_segment_cb,
37 const base::Closure& end_of_segment_cb,
38 const scoped_refptr<MediaLog>& media_log) override;
39 void Flush() override;
40 bool Parse(const uint8* buf, int size) override;
42 private:
43 enum State {
44 kWaitingForInit,
45 kParsingBoxes,
46 kWaitingForSampleData,
47 kEmittingSamples,
48 kError
51 bool ParseBox(bool* err);
52 bool ParseMoov(mp4::BoxReader* reader);
53 bool ParseMoof(mp4::BoxReader* reader);
55 void OnEncryptedMediaInitData(
56 const std::vector<ProtectionSystemSpecificHeader>& headers);
58 // To retain proper framing, each 'mdat' atom must be read; to limit memory
59 // usage, the atom's data needs to be discarded incrementally as frames are
60 // extracted from the stream. This function discards data from the stream up
61 // to |max_clear_offset|, updating the |mdat_tail_| value so that framing can
62 // be retained after all 'mdat' information has been read. |max_clear_offset|
63 // is the upper bound on what can be removed from |queue_|. Anything below
64 // this offset is no longer needed by the parser.
65 // Returns 'true' on success, 'false' if there was an error.
66 bool ReadAndDiscardMDATsUntil(int64 max_clear_offset);
68 void ChangeState(State new_state);
70 bool EmitConfigs();
71 bool PrepareAVCBuffer(const AVCDecoderConfigurationRecord& avc_config,
72 std::vector<uint8>* frame_buf,
73 std::vector<SubsampleEntry>* subsamples) const;
74 bool PrepareAACBuffer(const AAC& aac_config,
75 std::vector<uint8>* frame_buf,
76 std::vector<SubsampleEntry>* subsamples) const;
77 bool EnqueueSample(BufferQueue* audio_buffers,
78 BufferQueue* video_buffers,
79 bool* err);
80 bool SendAndFlushSamples(BufferQueue* audio_buffers,
81 BufferQueue* video_buffers);
83 void Reset();
85 // Checks to see if we have enough data in |queue_| to transition to
86 // kEmittingSamples and start enqueuing samples.
87 bool HaveEnoughDataToEnqueueSamples();
89 // Sets |highest_end_offset_| based on the data in |moov_|
90 // and |moof|. Returns true if |highest_end_offset_| was successfully
91 // computed.
92 bool ComputeHighestEndOffset(const MovieFragment& moof);
94 State state_;
95 InitCB init_cb_;
96 NewConfigCB config_cb_;
97 NewBuffersCB new_buffers_cb_;
98 EncryptedMediaInitDataCB encrypted_media_init_data_cb_;
99 NewMediaSegmentCB new_segment_cb_;
100 base::Closure end_of_segment_cb_;
101 scoped_refptr<MediaLog> media_log_;
103 OffsetByteQueue queue_;
105 // These two parameters are only valid in the |kEmittingSegments| state.
107 // |moof_head_| is the offset of the start of the most recently parsed moof
108 // block. All byte offsets in sample information are relative to this offset,
109 // as mandated by the Media Source spec.
110 int64 moof_head_;
111 // |mdat_tail_| is the stream offset of the end of the current 'mdat' box.
112 // Valid iff it is greater than the head of the queue.
113 int64 mdat_tail_;
115 // The highest end offset in the current moof. This offset is
116 // relative to |moof_head_|. This value is used to make sure we have collected
117 // enough bytes to parse all samples and aux_info in the current moof.
118 int64 highest_end_offset_;
120 scoped_ptr<mp4::Movie> moov_;
121 scoped_ptr<mp4::TrackRunIterator> runs_;
123 bool has_audio_;
124 bool has_video_;
125 uint32 audio_track_id_;
126 uint32 video_track_id_;
127 // The object types allowed for audio tracks.
128 std::set<int> audio_object_types_;
129 bool has_sbr_;
130 bool is_audio_track_encrypted_;
131 bool is_video_track_encrypted_;
133 // Tracks the number of MEDIA_LOGs for skipping top level boxes. Useful to
134 // prevent log spam.
135 int num_top_level_box_skipped_;
137 DISALLOW_COPY_AND_ASSIGN(MP4StreamParser);
140 } // namespace mp4
141 } // namespace media
143 #endif // MEDIA_FORMATS_MP4_MP4_STREAM_PARSER_H_