DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
mp4_media_parser.h
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef MEDIA_FORMATS_MP4_MP4_MEDIA_PARSER_H_
8 #define MEDIA_FORMATS_MP4_MP4_MEDIA_PARSER_H_
9 
10 #include <stdint.h>
11 
12 #include <map>
13 #include <vector>
14 
15 #include "packager/base/callback_forward.h"
16 #include "packager/base/compiler_specific.h"
17 #include "packager/base/memory/scoped_ptr.h"
18 #include "packager/base/memory/ref_counted.h"
19 #include "packager/base/memory/scoped_ptr.h"
20 #include "packager/media/base/decryptor_source.h"
21 #include "packager/media/base/media_parser.h"
22 #include "packager/media/base/offset_byte_queue.h"
23 
24 namespace edash_packager {
25 namespace media {
26 namespace mp4 {
27 
28 class BoxReader;
29 class TrackRunIterator;
30 struct Movie;
31 struct ProtectionSystemSpecificHeader;
32 
33 class MP4MediaParser : public MediaParser {
34  public:
36  ~MP4MediaParser() override;
37 
40  void Init(const InitCB& init_cb,
41  const NewSampleCB& new_sample_cb,
42  KeySource* decryption_key_source) override;
43  bool Flush() override WARN_UNUSED_RESULT;
44  bool Parse(const uint8_t* buf, int size) override WARN_UNUSED_RESULT;
46 
53  bool LoadMoov(const std::string& file_path);
54 
55  private:
56  enum State {
57  kWaitingForInit,
58  kParsingBoxes,
59  kEmittingSamples,
60  kError
61  };
62 
63  bool ParseBox(bool* err);
64  bool ParseMoov(mp4::BoxReader* reader);
65  bool ParseMoof(mp4::BoxReader* reader);
66 
67  bool FetchKeysIfNecessary(
68  const std::vector<ProtectionSystemSpecificHeader>& headers);
69 
70  // To retain proper framing, each 'mdat' box must be read; to limit memory
71  // usage, the box's data needs to be discarded incrementally as frames are
72  // extracted from the stream. This function discards data from the stream up
73  // to |offset|, updating the |mdat_tail_| value so that framing can be
74  // retained after all 'mdat' information has been read.
75  // Returns 'true' on success, 'false' if there was an error.
76  bool ReadAndDiscardMDATsUntil(const int64_t offset);
77 
78  void ChangeState(State new_state);
79 
80  bool EmitConfigs();
81 
82  bool EnqueueSample(bool* err);
83 
84  void Reset();
85 
86  State state_;
87  InitCB init_cb_;
88  NewSampleCB new_sample_cb_;
89  KeySource* decryption_key_source_;
90  scoped_ptr<DecryptorSource> decryptor_source_;
91 
92  OffsetByteQueue queue_;
93 
94  // These two parameters are only valid in the |kEmittingSegments| state.
95  //
96  // |moof_head_| is the offset of the start of the most recently parsed moof
97  // block. All byte offsets in sample information are relative to this offset,
98  // as mandated by the Media Source spec.
99  int64_t moof_head_;
100  // |mdat_tail_| is the stream offset of the end of the current 'mdat' box.
101  // Valid iff it is greater than the head of the queue.
102  int64_t mdat_tail_;
103 
104  scoped_ptr<Movie> moov_;
105  scoped_ptr<TrackRunIterator> runs_;
106 
107  DISALLOW_COPY_AND_ASSIGN(MP4MediaParser);
108 };
109 
110 } // namespace mp4
111 } // namespace media
112 } // namespace edash_packager
113 
114 #endif // MEDIA_FORMATS_MP4_MP4_MEDIA_PARSER_H_
bool LoadMoov(const std::string &file_path)
base::Callback< bool(uint32_t track_id, const scoped_refptr< MediaSample > &media_sample)> NewSampleCB
Definition: media_parser.h:44
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:31
base::Callback< void(const std::vector< scoped_refptr< StreamInfo > > &stream_info)> InitCB
Definition: media_parser.h:35
bool Flush() override WARN_UNUSED_RESULT
void Init(const InitCB &init_cb, const NewSampleCB &new_sample_cb, KeySource *decryption_key_source) override
bool Parse(const uint8_t *buf, int size) override WARN_UNUSED_RESULT
Class for reading MP4 boxes.
Definition: box_reader.h:24