Home | History | Annotate | Download | only in mp4
      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 AddTextTrackCB& add_text_track_cb,
     36                     const NewMediaSegmentCB& new_segment_cb,
     37                     const base::Closure& end_of_segment_cb,
     38                     const LogCB& log_cb) OVERRIDE;
     39   virtual void Flush() OVERRIDE;
     40   virtual bool Parse(const uint8* buf, int size) OVERRIDE;
     41 
     42  private:
     43   enum State {
     44     kWaitingForInit,
     45     kParsingBoxes,
     46     kEmittingSamples,
     47     kError
     48   };
     49 
     50   bool ParseBox(bool* err);
     51   bool ParseMoov(mp4::BoxReader* reader);
     52   bool ParseMoof(mp4::BoxReader* reader);
     53 
     54   void EmitNeedKeyIfNecessary(
     55       const std::vector<ProtectionSystemSpecificHeader>& headers);
     56 
     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);
     64 
     65   void ChangeState(State new_state);
     66 
     67   bool EmitConfigs();
     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,
     76                      bool* err);
     77   bool SendAndFlushSamples(BufferQueue* audio_buffers,
     78                            BufferQueue* video_buffers);
     79 
     80   void Reset();
     81 
     82   State state_;
     83   InitCB init_cb_;
     84   NewConfigCB config_cb_;
     85   NewBuffersCB new_buffers_cb_;
     86   NeedKeyCB need_key_cb_;
     87   NewMediaSegmentCB new_segment_cb_;
     88   base::Closure end_of_segment_cb_;
     89   LogCB log_cb_;
     90 
     91   OffsetByteQueue queue_;
     92 
     93   // These two parameters are only valid in the |kEmittingSegments| state.
     94   //
     95   // |moof_head_| is the offset of the start of the most recently parsed moof
     96   // block. All byte offsets in sample information are relative to this offset,
     97   // as mandated by the Media Source spec.
     98   int64 moof_head_;
     99   // |mdat_tail_| is the stream offset of the end of the current 'mdat' box.
    100   // Valid iff it is greater than the head of the queue.
    101   int64 mdat_tail_;
    102 
    103   scoped_ptr<mp4::Movie> moov_;
    104   scoped_ptr<mp4::TrackRunIterator> runs_;
    105 
    106   bool has_audio_;
    107   bool has_video_;
    108   uint32 audio_track_id_;
    109   uint32 video_track_id_;
    110   // The object types allowed for audio tracks.
    111   std::set<int> audio_object_types_;
    112   bool has_sbr_;
    113   bool is_audio_track_encrypted_;
    114   bool is_video_track_encrypted_;
    115 
    116   DISALLOW_COPY_AND_ASSIGN(MP4StreamParser);
    117 };
    118 
    119 }  // namespace mp4
    120 }  // namespace media
    121 
    122 #endif  // MEDIA_MP4_MP4_STREAM_PARSER_H_
    123