DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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 MEDIA_BASE_DEMUXER_H_
8 #define 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/base/media_handler.h"
17 #include "packager/media/base/status.h"
18 
19 namespace shaka {
20 namespace media {
21 
22 class Decryptor;
23 class File;
24 class KeySource;
25 class MediaParser;
26 class MediaSample;
27 class MediaStream;
28 class StreamInfo;
29 
32 class Demuxer : public MediaHandler {
33  public:
37  explicit Demuxer(const std::string& file_name);
38  ~Demuxer();
39 
44  void SetKeySource(std::unique_ptr<KeySource> key_source);
45 
48  Status Run();
49 
52  void Cancel();
53 
56  MediaContainerName container_name() { return container_name_; }
57 
62  Status SetHandler(const std::string& stream_label,
63  std::shared_ptr<MediaHandler> handler);
64 
70  void SetLanguageOverride(const std::string& stream_label,
71  const std::string& language_override);
72 
73  void set_dump_stream_info(bool dump_stream_info) {
74  dump_stream_info_ = dump_stream_info;
75  }
76 
77  protected:
80  Status InitializeInternal() override { return Status::OK; }
81  Status Process(std::unique_ptr<StreamData> stream_data) override {
82  return Status(error::INTERNAL_ERROR,
83  "Demuxer should not be the downstream handler.");
84  }
85  bool ValidateOutputStreamIndex(size_t stream_index) const override {
86  // We don't know if the stream is valid or not when setting up the graph.
87  // Will validate the stream index later when stream info is available.
88  return true;
89  }
91 
92  private:
93  Demuxer(const Demuxer&) = delete;
94  Demuxer& operator=(const Demuxer&) = delete;
95 
96  struct QueuedSample {
97  QueuedSample(uint32_t track_id, std::shared_ptr<MediaSample> sample);
98  ~QueuedSample();
99 
100  uint32_t track_id;
101  std::shared_ptr<MediaSample> sample;
102  };
103 
104  // Initialize the parser. This method primes the demuxer by parsing portions
105  // of the media file to extract stream information.
106  // @return OK on success.
107  Status InitializeParser();
108 
109  // Parser init event.
110  void ParserInitEvent(const std::vector<std::shared_ptr<StreamInfo>>& streams);
111  // Parser new sample event handler. Queues the samples if init event has not
112  // been received, otherwise calls PushSample() to push the sample to
113  // corresponding stream.
114  bool NewSampleEvent(uint32_t track_id,
115  const std::shared_ptr<MediaSample>& sample);
116  // Helper function to push the sample to corresponding stream.
117  bool PushSample(uint32_t track_id,
118  const std::shared_ptr<MediaSample>& sample);
119 
120  // Read from the source and send it to the parser.
121  Status Parse();
122 
123  std::string file_name_;
124  File* media_file_ = nullptr;
125  // A stream is considered ready after receiving the stream info.
126  bool all_streams_ready_ = false;
127  // Queued samples received in NewSampleEvent() before ParserInitEvent().
128  std::deque<QueuedSample> queued_samples_;
129  std::unique_ptr<MediaParser> parser_;
130  // TrackId -> StreamIndex map.
131  std::map<uint32_t, size_t> track_id_to_stream_index_map_;
132  // The list of stream indexes in the above map (in the same order as the input
133  // stream info vector).
134  std::vector<size_t> stream_indexes_;
135  // StreamIndex -> language_override map.
136  std::map<size_t, std::string> language_overrides_;
137  MediaContainerName container_name_ = CONTAINER_UNKNOWN;
138  std::unique_ptr<uint8_t[]> buffer_;
139  std::unique_ptr<KeySource> key_source_;
140  bool cancelled_ = false;
141  // Whether to dump stream info when it is received.
142  bool dump_stream_info_ = false;
143 };
144 
145 } // namespace media
146 } // namespace shaka
147 
148 #endif // MEDIA_BASE_DEMUXER_H_
MediaContainerName container_name()
Definition: demuxer.h:56
Status SetHandler(const std::string &stream_label, std::shared_ptr< MediaHandler > handler)
Definition: demuxer.cc:130
bool ValidateOutputStreamIndex(size_t stream_index) const override
Validate if the stream at the specified index actually exists.
Definition: demuxer.h:85
void SetLanguageOverride(const std::string &stream_label, const std::string &language_override)
Definition: demuxer.cc:140
void SetKeySource(std::unique_ptr< KeySource > key_source)
Definition: demuxer.cc:83
Status InitializeInternal() override
Definition: demuxer.h:80
Status Process(std::unique_ptr< StreamData > stream_data) override
Definition: demuxer.h:81
Demuxer(const std::string &file_name)
Definition: demuxer.cc:75