• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef MEDIA_FORMATS_MP4_MP4_STREAM_PARSER_H_
6 #define MEDIA_FORMATS_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/formats/common/offset_byte_queue.h"
18 #include "media/formats/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                     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;
40 
41  private:
42   enum State {
43     kWaitingForInit,
44     kParsingBoxes,
45     kWaitingForSampleData,
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 |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);
66 
67   void ChangeState(State new_state);
68 
69   bool EmitConfigs();
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,
78                      bool* err);
79   bool SendAndFlushSamples(BufferQueue* audio_buffers,
80                            BufferQueue* video_buffers);
81 
82   void Reset();
83 
84   // Checks to see if we have enough data in |queue_| to transition to
85   // kEmittingSamples and start enqueuing samples.
86   bool HaveEnoughDataToEnqueueSamples();
87 
88   // Sets |highest_end_offset_| based on the data in |moov_|
89   // and |moof|. Returns true if |highest_end_offset_| was successfully
90   // computed.
91   bool ComputeHighestEndOffset(const MovieFragment& moof);
92 
93   State state_;
94   InitCB init_cb_;
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_;
100   LogCB log_cb_;
101 
102   OffsetByteQueue queue_;
103 
104   // These two parameters are only valid in the |kEmittingSegments| state.
105   //
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.
109   int64 moof_head_;
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.
112   int64 mdat_tail_;
113 
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_;
118 
119   scoped_ptr<mp4::Movie> moov_;
120   scoped_ptr<mp4::TrackRunIterator> runs_;
121 
122   bool has_audio_;
123   bool has_video_;
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_;
128   bool has_sbr_;
129   bool is_audio_track_encrypted_;
130   bool is_video_track_encrypted_;
131 
132   DISALLOW_COPY_AND_ASSIGN(MP4StreamParser);
133 };
134 
135 }  // namespace mp4
136 }  // namespace media
137 
138 #endif  // MEDIA_FORMATS_MP4_MP4_STREAM_PARSER_H_
139