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