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