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/media_parser.h"
21 #include "packager/media/base/offset_byte_queue.h"
22 
23 namespace edash_packager {
24 namespace media {
25 
26 class AesCtrEncryptor;
27 class DecryptConfig;
28 
29 namespace mp4 {
30 
31 class BoxReader;
32 class TrackRunIterator;
33 struct Movie;
34 struct ProtectionSystemSpecificHeader;
35 
36 class MP4MediaParser : public MediaParser {
37  public:
39  ~MP4MediaParser() override;
40 
43  void Init(const InitCB& init_cb,
44  const NewSampleCB& new_sample_cb,
45  KeySource* decryption_key_source) override;
46  void Flush() override;
47  bool Parse(const uint8_t* buf, int size) override;
49 
56  bool LoadMoov(const std::string& file_path);
57 
58  private:
59  enum State {
60  kWaitingForInit,
61  kParsingBoxes,
62  kEmittingSamples,
63  kError
64  };
65 
66  bool ParseBox(bool* err);
67  bool ParseMoov(mp4::BoxReader* reader);
68  bool ParseMoof(mp4::BoxReader* reader);
69 
70  bool FetchKeysIfNecessary(
71  const std::vector<ProtectionSystemSpecificHeader>& headers);
72 
73  bool DecryptSampleBuffer(const DecryptConfig* decrypt_config,
74  uint8_t* buffer,
75  size_t buffer_size);
76 
77  // To retain proper framing, each 'mdat' atom must be read; to limit memory
78  // usage, the atom's data needs to be discarded incrementally as frames are
79  // extracted from the stream. This function discards data from the stream up
80  // to |offset|, updating the |mdat_tail_| value so that framing can be
81  // retained after all 'mdat' information has been read.
82  // Returns 'true' on success, 'false' if there was an error.
83  bool ReadAndDiscardMDATsUntil(const int64_t offset);
84 
85  void ChangeState(State new_state);
86 
87  bool EmitConfigs();
88 
89  bool EnqueueSample(bool* err);
90 
91  void Reset();
92 
93  State state_;
94  InitCB init_cb_;
95  NewSampleCB new_sample_cb_;
96  KeySource* decryption_key_source_;
97 
98  OffsetByteQueue queue_;
99 
100  // These two parameters are only valid in the |kEmittingSegments| state.
101  //
102  // |moof_head_| is the offset of the start of the most recently parsed moof
103  // block. All byte offsets in sample information are relative to this offset,
104  // as mandated by the Media Source spec.
105  int64_t moof_head_;
106  // |mdat_tail_| is the stream offset of the end of the current 'mdat' box.
107  // Valid iff it is greater than the head of the queue.
108  int64_t mdat_tail_;
109 
110  scoped_ptr<Movie> moov_;
111  scoped_ptr<TrackRunIterator> runs_;
112 
113  typedef std::map<std::vector<uint8_t>, AesCtrEncryptor*> DecryptorMap;
114  DecryptorMap decryptor_map_;
115 
116  DISALLOW_COPY_AND_ASSIGN(MP4MediaParser);
117 };
118 
119 } // namespace mp4
120 } // namespace media
121 } // namespace edash_packager
122 
123 #endif // MEDIA_FORMATS_MP4_MP4_MEDIA_PARSER_H_
bool Parse(const uint8_t *buf, int size) override
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:43
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:29
base::Callback< void(const std::vector< scoped_refptr< StreamInfo > > &stream_info)> InitCB
Definition: media_parser.h:34
void Init(const InitCB &init_cb, const NewSampleCB &new_sample_cb, KeySource *decryption_key_source) override
Class for reading MP4 boxes.
Definition: box_reader.h:24