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