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