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