Shaka Packager SDK
mp4_media_parser.h
1 // Copyright 2014 Google LLC. 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 PACKAGER_MEDIA_FORMATS_MP4_MP4_MEDIA_PARSER_H_
8 #define PACKAGER_MEDIA_FORMATS_MP4_MP4_MEDIA_PARSER_H_
9 
10 #include <cstdint>
11 #include <map>
12 #include <memory>
13 #include <vector>
14 
15 #include <absl/flags/declare.h>
16 #include <absl/flags/flag.h>
17 
18 #include <packager/macros/classes.h>
19 #include <packager/media/base/decryptor_source.h>
20 #include <packager/media/base/media_parser.h>
21 #include <packager/media/base/offset_byte_queue.h>
22 
23 ABSL_DECLARE_FLAG(bool, use_dovi_supplemental_codecs);
24 
25 namespace shaka {
26 namespace media {
27 namespace mp4 {
28 
29 class BoxReader;
30 class TrackRunIterator;
31 struct Movie;
32 struct ProtectionSystemSpecificHeader;
33 
34 class MP4MediaParser : public MediaParser {
35  public:
37  ~MP4MediaParser() override;
38 
41  void Init(const InitCB& init_cb,
42  const NewMediaSampleCB& new_media_sample_cb,
43  const NewTextSampleCB& new_text_sample_cb,
44  KeySource* decryption_key_source) override;
45  [[nodiscard]] bool Flush() override;
46  [[nodiscard]] bool Parse(const uint8_t* buf, int size) override;
48 
55  bool LoadMoov(const std::string& file_path);
56 
57  private:
58  enum State {
59  kWaitingForInit,
60  kParsingBoxes,
61  kEmittingSamples,
62  kError
63  };
64 
65  bool ParseBox(bool* err);
66  bool ParseMoov(mp4::BoxReader* reader);
67  bool ParseMoof(mp4::BoxReader* reader);
68 
69  bool FetchKeysIfNecessary(
70  const std::vector<ProtectionSystemSpecificHeader>& headers);
71 
72  // To retain proper framing, each 'mdat' box must be read; to limit memory
73  // usage, the box's data needs to be discarded incrementally as frames are
74  // extracted from the stream. This function discards data from the stream up
75  // to |offset|, updating the |mdat_tail_| value so that framing can be
76  // retained after all 'mdat' information has been read.
77  // Returns 'true' on success, 'false' if there was an error.
78  bool ReadAndDiscardMDATsUntil(const int64_t offset);
79 
80  void ChangeState(State new_state);
81 
82  bool EmitConfigs();
83 
84  bool EnqueueSample(bool* err);
85 
86  void Reset();
87 
88  State state_;
89  InitCB init_cb_;
90  NewMediaSampleCB new_sample_cb_;
91  KeySource* decryption_key_source_;
92  std::unique_ptr<DecryptorSource> decryptor_source_;
93 
94  OffsetByteQueue queue_;
95 
96  // These two parameters are only valid in the |kEmittingSegments| state.
97  //
98  // |moof_head_| is the offset of the start of the most recently parsed moof
99  // block. All byte offsets in sample information are relative to this offset,
100  // as mandated by the Media Source spec.
101  int64_t moof_head_;
102  // |mdat_tail_| is the stream offset of the end of the current 'mdat' box.
103  // Valid iff it is greater than the head of the queue.
104  int64_t mdat_tail_;
105 
106  std::unique_ptr<Movie> moov_;
107  std::unique_ptr<TrackRunIterator> runs_;
108 
109  DISALLOW_COPY_AND_ASSIGN(MP4MediaParser);
110 };
111 
112 } // namespace mp4
113 } // namespace media
114 } // namespace shaka
115 
116 #endif // PACKAGER_MEDIA_FORMATS_MP4_MP4_MEDIA_PARSER_H_
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:52
std::function< bool(uint32_t track_id, std::shared_ptr< MediaSample > media_sample)> NewMediaSampleCB
Definition: media_parser.h:45
std::function< bool(uint32_t track_id, std::shared_ptr< TextSample > text_sample)> NewTextSampleCB
Definition: media_parser.h:54
std::function< void(const std::vector< std::shared_ptr< StreamInfo > > &stream_info)> InitCB
Definition: media_parser.h:36
Class for reading MP4 boxes.
Definition: box_reader.h:28
bool Parse(const uint8_t *buf, int size) override
void Init(const InitCB &init_cb, const NewMediaSampleCB &new_media_sample_cb, const NewTextSampleCB &new_text_sample_cb, KeySource *decryption_key_source) override
bool LoadMoov(const std::string &file_path)
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66