DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations 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 <memory>
14 #include <vector>
15 
16 #include "packager/base/callback_forward.h"
17 #include "packager/base/memory/ref_counted.h"
18 #include "packager/media/base/decryptor_source.h"
19 #include "packager/media/base/media_parser.h"
20 #include "packager/media/base/offset_byte_queue.h"
21 
22 namespace shaka {
23 namespace media {
24 namespace mp4 {
25 
26 class BoxReader;
27 class TrackRunIterator;
28 struct Movie;
29 struct ProtectionSystemSpecificHeader;
30 
31 class MP4MediaParser : public MediaParser {
32  public:
34  ~MP4MediaParser() override;
35 
38  void Init(const InitCB& init_cb,
39  const NewSampleCB& new_sample_cb,
40  KeySource* decryption_key_source) override;
41  bool Flush() override WARN_UNUSED_RESULT;
42  bool Parse(const uint8_t* buf, int size) override WARN_UNUSED_RESULT;
44 
51  bool LoadMoov(const std::string& file_path);
52 
53  private:
54  enum State {
55  kWaitingForInit,
56  kParsingBoxes,
57  kEmittingSamples,
58  kError
59  };
60 
61  bool ParseBox(bool* err);
62  bool ParseMoov(mp4::BoxReader* reader);
63  bool ParseMoof(mp4::BoxReader* reader);
64 
65  bool FetchKeysIfNecessary(
66  const std::vector<ProtectionSystemSpecificHeader>& headers);
67 
68  // To retain proper framing, each 'mdat' box must be read; to limit memory
69  // usage, the box's data needs to be discarded incrementally as frames are
70  // extracted from the stream. This function discards data from the stream up
71  // to |offset|, updating the |mdat_tail_| value so that framing can be
72  // retained after all 'mdat' information has been read.
73  // Returns 'true' on success, 'false' if there was an error.
74  bool ReadAndDiscardMDATsUntil(const int64_t offset);
75 
76  void ChangeState(State new_state);
77 
78  bool EmitConfigs();
79 
80  bool EnqueueSample(bool* err);
81 
82  void Reset();
83 
84  State state_;
85  InitCB init_cb_;
86  NewSampleCB new_sample_cb_;
87  KeySource* decryption_key_source_;
88  std::unique_ptr<DecryptorSource> decryptor_source_;
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_t 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_t mdat_tail_;
101 
102  std::unique_ptr<Movie> moov_;
103  std::unique_ptr<TrackRunIterator> runs_;
104 
105  DISALLOW_COPY_AND_ASSIGN(MP4MediaParser);
106 };
107 
108 } // namespace mp4
109 } // namespace media
110 } // namespace shaka
111 
112 #endif // MEDIA_FORMATS_MP4_MP4_MEDIA_PARSER_H_
base::Callback< void(const std::vector< scoped_refptr< StreamInfo > > &stream_info)> InitCB
Definition: media_parser.h:34
bool Flush() override WARN_UNUSED_RESULT
bool Parse(const uint8_t *buf, int size) override WARN_UNUSED_RESULT
Class for reading MP4 boxes.
Definition: box_reader.h:25
base::Callback< bool(uint32_t track_id, const scoped_refptr< MediaSample > &media_sample)> NewSampleCB
Definition: media_parser.h:43
bool LoadMoov(const std::string &file_path)
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:30
void Init(const InitCB &init_cb, const NewSampleCB &new_sample_cb, KeySource *decryption_key_source) override