Shaka Packager SDK
Loading...
Searching...
No Matches
demuxer.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_BASE_DEMUXER_H_
8#define PACKAGER_MEDIA_BASE_DEMUXER_H_
9
10#include <deque>
11#include <memory>
12#include <vector>
13
14#include <packager/macros/classes.h>
15#include <packager/media/base/container_names.h>
16#include <packager/media/origin/origin_handler.h>
17#include <packager/status.h>
18
19namespace shaka {
20
21class File;
22
23namespace media {
24
25class Decryptor;
26class KeySource;
27class MediaParser;
28class MediaSample;
29class StreamInfo;
30
33class Demuxer : public OriginHandler {
34 public:
38 explicit Demuxer(const std::string& file_name);
39 ~Demuxer();
40
45 void SetKeySource(std::unique_ptr<KeySource> key_source);
46
49 Status Run() override;
50
53 void Cancel() override;
54
57 MediaContainerName container_name() { return container_name_; }
58
63 Status SetHandler(const std::string& stream_label,
64 std::shared_ptr<MediaHandler> handler);
65
71 void SetLanguageOverride(const std::string& stream_label,
72 const std::string& language_override);
73
74 void set_dump_stream_info(bool dump_stream_info) {
75 dump_stream_info_ = dump_stream_info;
76 }
77
78 void set_input_format(std::string input_format) {
79 input_format_ = input_format;
80 }
81
82 protected:
85 Status InitializeInternal() override { return Status::OK; }
86 Status Process(std::unique_ptr<StreamData> stream_data) override {
87 return Status(error::INTERNAL_ERROR,
88 "Demuxer should not be the downstream handler.");
89 }
90 bool ValidateOutputStreamIndex(size_t stream_index) const override {
91 // We don't know if the stream is valid or not when setting up the graph.
92 // Will validate the stream index later when stream info is available.
93 return true;
94 }
96
97 private:
98 Demuxer(const Demuxer&) = delete;
99 Demuxer& operator=(const Demuxer&) = delete;
100
101 template <typename T>
102 struct QueuedSample {
103 QueuedSample(uint32_t track_id, std::shared_ptr<T> sample)
104 : track_id(track_id), sample(sample) {}
105
106 ~QueuedSample() {}
107
108 uint32_t track_id;
109 std::shared_ptr<T> sample;
110 };
111
112 // Initialize the parser. This method primes the demuxer by parsing portions
113 // of the media file to extract stream information.
114 // @return OK on success.
115 Status InitializeParser();
116
117 // Parser init event.
118 void ParserInitEvent(const std::vector<std::shared_ptr<StreamInfo>>& streams);
119 // Parser new sample event handler. Queues the samples if init event has not
120 // been received, otherwise calls PushSample() to push the sample to
121 // corresponding stream.
122 bool NewMediaSampleEvent(uint32_t track_id,
123 std::shared_ptr<MediaSample> sample);
124 bool NewTextSampleEvent(uint32_t track_id,
125 std::shared_ptr<TextSample> sample);
126 // Helper function to push the sample to corresponding stream.
127 bool PushMediaSample(uint32_t track_id, std::shared_ptr<MediaSample> sample);
128 bool PushTextSample(uint32_t track_id, std::shared_ptr<TextSample> sample);
129
130 // Read from the source and send it to the parser.
131 Status Parse();
132
133 std::string file_name_;
134 File* media_file_ = nullptr;
135 // A stream is considered ready after receiving the stream info.
136 bool all_streams_ready_ = false;
137 // Queued samples received in NewSampleEvent() before ParserInitEvent().
138 std::deque<QueuedSample<MediaSample>> queued_media_samples_;
139 std::deque<QueuedSample<TextSample>> queued_text_samples_;
140 std::unique_ptr<MediaParser> parser_;
141 // TrackId -> StreamIndex map.
142 std::map<uint32_t, size_t> track_id_to_stream_index_map_;
143 // The list of stream indexes in the above map (in the same order as the input
144 // stream info vector).
145 std::vector<size_t> stream_indexes_;
146 // StreamIndex -> language_override map.
147 std::map<size_t, std::string> language_overrides_;
148 MediaContainerName container_name_ = CONTAINER_UNKNOWN;
149 std::unique_ptr<uint8_t[]> buffer_;
150 std::unique_ptr<KeySource> key_source_;
151 bool cancelled_ = false;
152 // Whether to dump stream info when it is received.
153 bool dump_stream_info_ = false;
154 Status init_event_status_;
155 // Explicitly defined input format, for avoiding autodetection.
156 std::string input_format_;
157};
158
159} // namespace media
160} // namespace shaka
161
162#endif // PACKAGER_MEDIA_BASE_DEMUXER_H_
Status Run() override
Definition demuxer.cc:93
Status SetHandler(const std::string &stream_label, std::shared_ptr< MediaHandler > handler)
Definition demuxer.cc:138
MediaContainerName container_name()
Definition demuxer.h:57
void Cancel() override
Definition demuxer.cc:134
Status InitializeInternal() override
Definition demuxer.h:85
Status Process(std::unique_ptr< StreamData > stream_data) override
Definition demuxer.h:86
bool ValidateOutputStreamIndex(size_t stream_index) const override
Validate if the stream at the specified index actually exists.
Definition demuxer.h:90
void SetLanguageOverride(const std::string &stream_label, const std::string &language_override)
Definition demuxer.cc:148
void SetKeySource(std::unique_ptr< KeySource > key_source)
Definition demuxer.cc:89
All the methods that are virtual are virtual for mocking.