Shaka Packager SDK
Loading...
Searching...
No Matches
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
23ABSL_DECLARE_FLAG(bool, use_dovi_supplemental_codecs);
24
25namespace shaka {
26namespace media {
27namespace mp4 {
28
29class BoxReader;
30class TrackRunIterator;
31struct Movie;
32struct ProtectionSystemSpecificHeader;
33
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 { kWaitingForInit, kParsingBoxes, kEmittingSamples, kError };
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 NewMediaSampleCB 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_
KeySource is responsible for encryption key acquisition.
Definition key_source.h:53
std::function< bool(uint32_t track_id, std::shared_ptr< MediaSample > media_sample)> NewMediaSampleCB
std::function< bool(uint32_t track_id, std::shared_ptr< TextSample > text_sample)> NewTextSampleCB
std::function< void(const std::vector< std::shared_ptr< StreamInfo > > &stream_info)> InitCB
Class for reading MP4 boxes.
Definition box_reader.h:29
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.