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/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 MediaStream;
30 class StreamInfo;
31 
34 class Demuxer : public MediaHandler {
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();
51 
54  void Cancel();
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  protected:
82  Status InitializeInternal() override { return Status::OK; }
83  Status Process(std::unique_ptr<StreamData> stream_data) override {
84  return Status(error::INTERNAL_ERROR,
85  "Demuxer should not be the downstream handler.");
86  }
87  bool ValidateOutputStreamIndex(size_t stream_index) const override {
88  // We don't know if the stream is valid or not when setting up the graph.
89  // Will validate the stream index later when stream info is available.
90  return true;
91  }
93 
94  private:
95  Demuxer(const Demuxer&) = delete;
96  Demuxer& operator=(const Demuxer&) = delete;
97 
98  struct QueuedSample {
99  QueuedSample(uint32_t track_id, std::shared_ptr<MediaSample> sample);
100  ~QueuedSample();
101 
102  uint32_t track_id;
103  std::shared_ptr<MediaSample> sample;
104  };
105 
106  // Initialize the parser. This method primes the demuxer by parsing portions
107  // of the media file to extract stream information.
108  // @return OK on success.
109  Status InitializeParser();
110 
111  // Parser init event.
112  void ParserInitEvent(const std::vector<std::shared_ptr<StreamInfo>>& streams);
113  // Parser new sample event handler. Queues the samples if init event has not
114  // been received, otherwise calls PushSample() to push the sample to
115  // corresponding stream.
116  bool NewSampleEvent(uint32_t track_id,
117  const std::shared_ptr<MediaSample>& sample);
118  // Helper function to push the sample to corresponding stream.
119  bool PushSample(uint32_t track_id,
120  const std::shared_ptr<MediaSample>& sample);
121 
122  // Read from the source and send it to the parser.
123  Status Parse();
124 
125  std::string file_name_;
126  File* media_file_ = nullptr;
127  // A stream is considered ready after receiving the stream info.
128  bool all_streams_ready_ = false;
129  // Queued samples received in NewSampleEvent() before ParserInitEvent().
130  std::deque<QueuedSample> queued_samples_;
131  std::unique_ptr<MediaParser> parser_;
132  // TrackId -> StreamIndex map.
133  std::map<uint32_t, size_t> track_id_to_stream_index_map_;
134  // The list of stream indexes in the above map (in the same order as the input
135  // stream info vector).
136  std::vector<size_t> stream_indexes_;
137  // StreamIndex -> language_override map.
138  std::map<size_t, std::string> language_overrides_;
139  MediaContainerName container_name_ = CONTAINER_UNKNOWN;
140  std::unique_ptr<uint8_t[]> buffer_;
141  std::unique_ptr<KeySource> key_source_;
142  bool cancelled_ = false;
143  // Whether to dump stream info when it is received.
144  bool dump_stream_info_ = false;
145  Status init_event_status_;
146 };
147 
148 } // namespace media
149 } // namespace shaka
150 
151 #endif // MEDIA_BASE_DEMUXER_H_
MediaContainerName container_name()
Definition: demuxer.h:58
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:87
void SetLanguageOverride(const std::string &stream_label, const std::string &language_override)
Definition: demuxer.cc:142
void SetKeySource(std::unique_ptr< KeySource > key_source)
Definition: demuxer.cc:83
Status InitializeInternal() override
Definition: demuxer.h:82
Status Process(std::unique_ptr< StreamData > stream_data) override
Definition: demuxer.h:83
Demuxer(const std::string &file_name)
Definition: demuxer.cc:75