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 NewMediaSampleCB& new_media_sample_cb,
39  const NewTextSampleCB& new_text_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  NewMediaSampleCB 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 // PACKAGER_MEDIA_FORMATS_MP4_MP4_MEDIA_PARSER_H_
shaka::media::MediaParser
Definition: media_parser.h:25
shaka::media::mp4::MP4MediaParser
Definition: mp4_media_parser.h:30
shaka::media::mp4::MP4MediaParser::LoadMoov
bool LoadMoov(const std::string &file_path)
Definition: mp4_media_parser.cc:246
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::media::MediaParser::NewMediaSampleCB
base::Callback< bool(uint32_t track_id, std::shared_ptr< MediaSample > media_sample)> NewMediaSampleCB
Definition: media_parser.h:44
shaka::media::MediaParser::InitCB
base::Callback< void(const std::vector< std::shared_ptr< StreamInfo > > &stream_info)> InitCB
Definition: media_parser.h:35
shaka::media::mp4::MP4MediaParser::Init
void Init(const InitCB &init_cb, const NewMediaSampleCB &new_media_sample_cb, const NewTextSampleCB &new_text_sample_cb, KeySource *decryption_key_source) override
Definition: mp4_media_parser.cc:181
shaka::media::mp4::BoxReader
Class for reading MP4 boxes.
Definition: box_reader.h:25
shaka::media::mp4::MP4MediaParser::Parse
bool Parse(const uint8_t *buf, int size) override WARN_UNUSED_RESULT
Definition: mp4_media_parser.cc:212
shaka::media::KeySource
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:51
shaka::media::MediaParser::NewTextSampleCB
base::Callback< bool(uint32_t track_id, std::shared_ptr< TextSample > text_sample)> NewTextSampleCB
Definition: media_parser.h:53
shaka::media::OffsetByteQueue
Definition: offset_byte_queue.h:19
shaka::media::mp4::MP4MediaParser::Flush
bool Flush() override WARN_UNUSED_RESULT
Definition: mp4_media_parser.cc:205