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. 4 5 #ifndef MEDIA_MP4_MP4_STREAM_PARSER_H_ 6 #define MEDIA_MP4_MP4_STREAM_PARSER_H_ 7 8 #include <set> 9 #include <vector> 10 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" 19 20 namespace media { 21 namespace mp4 { 22 23 struct Movie; 24 class BoxReader; 25 26 class MEDIA_EXPORT MP4StreamParser : public StreamParser { 27 public: 28 MP4StreamParser(const std::set<int>& audio_object_types, bool has_sbr); 29 virtual ~MP4StreamParser(); 30 31 virtual void Init(const InitCB& init_cb, const NewConfigCB& config_cb, 32 const NewBuffersCB& new_buffers_cb, 33 const NewTextBuffersCB& text_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; 40 41 private: 42 enum State { 43 kWaitingForInit, 44 kParsingBoxes, 45 kEmittingSamples, 46 kError 47 }; 48 49 bool ParseBox(bool* err); 50 bool ParseMoov(mp4::BoxReader* reader); 51 bool ParseMoof(mp4::BoxReader* reader); 52 53 void EmitNeedKeyIfNecessary( 54 const std::vector<ProtectionSystemSpecificHeader>& headers); 55 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); 63 64 void ChangeState(State new_state); 65 66 bool EmitConfigs(); 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, 75 bool* err); 76 bool SendAndFlushSamples(BufferQueue* audio_buffers, 77 BufferQueue* video_buffers); 78 79 void Reset(); 80 81 State state_; 82 InitCB init_cb_; 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_; 88 LogCB log_cb_; 89 90 OffsetByteQueue queue_; 91 92 // These two parameters are only valid in the |kEmittingSegments| state. 93 // 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. 97 int64 moof_head_; 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. 100 int64 mdat_tail_; 101 102 scoped_ptr<mp4::Movie> moov_; 103 scoped_ptr<mp4::TrackRunIterator> runs_; 104 105 bool has_audio_; 106 bool has_video_; 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_; 111 bool has_sbr_; 112 bool is_audio_track_encrypted_; 113 bool is_video_track_encrypted_; 114 115 DISALLOW_COPY_AND_ASSIGN(MP4StreamParser); 116 }; 117 118 } // namespace mp4 119 } // namespace media 120 121 #endif // MEDIA_MP4_MP4_STREAM_PARSER_H_ 122