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  struct QueuedSample {
98  QueuedSample(uint32_t track_id, std::shared_ptr<MediaSample> sample);
99  ~QueuedSample();
100 
101  uint32_t track_id;
102  std::shared_ptr<MediaSample> sample;
103  };
104 
105  // Initialize the parser. This method primes the demuxer by parsing portions
106  // of the media file to extract stream information.
107  // @return OK on success.
108  Status InitializeParser();
109 
110  // Parser init event.
111  void ParserInitEvent(const std::vector<std::shared_ptr<StreamInfo>>& streams);
112  // Parser new sample event handler. Queues the samples if init event has not
113  // been received, otherwise calls PushSample() to push the sample to
114  // corresponding stream.
115  bool NewSampleEvent(uint32_t track_id,
116  const std::shared_ptr<MediaSample>& sample);
117  // Helper function to push the sample to corresponding stream.
118  bool PushSample(uint32_t track_id,
119  const std::shared_ptr<MediaSample>& sample);
120 
121  // Read from the source and send it to the parser.
122  Status Parse();
123 
124  std::string file_name_;
125  File* media_file_ = nullptr;
126  // A stream is considered ready after receiving the stream info.
127  bool all_streams_ready_ = false;
128  // Queued samples received in NewSampleEvent() before ParserInitEvent().
129  std::deque<QueuedSample> queued_samples_;
130  std::unique_ptr<MediaParser> parser_;
131  // TrackId -> StreamIndex map.
132  std::map<uint32_t, size_t> track_id_to_stream_index_map_;
133  // The list of stream indexes in the above map (in the same order as the input
134  // stream info vector).
135  std::vector<size_t> stream_indexes_;
136  // StreamIndex -> language_override map.
137  std::map<size_t, std::string> language_overrides_;
138  MediaContainerName container_name_ = CONTAINER_UNKNOWN;
139  std::unique_ptr<uint8_t[]> buffer_;
140  std::unique_ptr<KeySource> key_source_;
141  bool cancelled_ = false;
142  // Whether to dump stream info when it is received.
143  bool dump_stream_info_ = false;
144  Status init_event_status_;
145 };
146 
147 } // namespace media
148 } // namespace shaka
149 
150 #endif // PACKAGER_MEDIA_BASE_DEMUXER_H_
MediaContainerName container_name()
Definition: demuxer.h:57
Status SetHandler(const std::string &stream_label, std::shared_ptr< MediaHandler > handler)
Definition: demuxer.cc:132
bool ValidateOutputStreamIndex(size_t stream_index) const override
Validate if the stream at the specified index actually exists.
Definition: demuxer.h:86
Status Run() override
Definition: demuxer.cc:87
Define an abstract file interface.
Definition: file.h:26
void SetLanguageOverride(const std::string &stream_label, const std::string &language_override)
Definition: demuxer.cc:142
All the methods that are virtual are virtual for mocking.
void SetKeySource(std::unique_ptr< KeySource > key_source)
Definition: demuxer.cc:83
Status InitializeInternal() override
Definition: demuxer.h:81
Status Process(std::unique_ptr< StreamData > stream_data) override
Definition: demuxer.h:82
Demuxer(const std::string &file_name)
Definition: demuxer.cc:75
void Cancel() override
Definition: demuxer.cc:128