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