DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
media_handler.h
1 // Copyright 2017 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_MEDIA_HANDLER_H_
8 #define PACKAGER_MEDIA_BASE_MEDIA_HANDLER_H_
9 
10 #include <map>
11 #include <memory>
12 #include <utility>
13 
14 #include "packager/media/base/media_sample.h"
15 #include "packager/media/base/status.h"
16 #include "packager/media/base/stream_info.h"
17 
18 namespace shaka {
19 namespace media {
20 
21 enum class StreamDataType {
22  kUnknown,
23  kPeriodInfo,
24  kStreamInfo,
25  kEncryptionConfig,
26  kMediaSample,
27  kMediaEvent,
28  kSegmentInfo,
29 };
30 
31 // TODO(kqyang): Define these structures.
32 struct PeriodInfo {};
33 struct EncryptionConfig {};
34 struct MediaEvent {};
35 struct SegmentInfo {
36  bool is_subsegment = false;
37  bool is_encrypted = false;
38  uint64_t start_timestamp = 0;
39  uint64_t duration = 0;
40 };
41 
42 // TODO(kqyang): Should we use protobuf?
43 struct StreamData {
44  int stream_index;
45  StreamDataType stream_data_type;
46 
47  std::unique_ptr<PeriodInfo> period_info;
48  std::unique_ptr<StreamInfo> stream_info;
49  std::unique_ptr<EncryptionConfig> encryption_config;
50  std::unique_ptr<MediaSample> media_sample;
51  std::unique_ptr<MediaEvent> media_event;
52  std::unique_ptr<SegmentInfo> segment_info;
53 };
54 
70 class MediaHandler {
71  public:
72  MediaHandler() = default;
73  virtual ~MediaHandler() = default;
74 
76  Status SetHandler(int output_stream_index,
77  std::shared_ptr<MediaHandler> handler);
78 
80  Status AddHandler(std::shared_ptr<MediaHandler> handler) {
81  return SetHandler(next_output_stream_index_, handler);
82  }
83 
87 
88  protected:
91  virtual Status InitializeInternal() = 0;
92 
97  virtual Status Process(std::unique_ptr<StreamData> stream_data) = 0;
98 
100  virtual Status FlushStream(int input_stream_index);
101 
103  virtual bool ValidateOutputStreamIndex(int stream_index) const;
104 
105  bool initialized() { return initialized_; }
106  int num_input_streams() { return num_input_streams_; }
107 
110  Status Dispatch(std::unique_ptr<StreamData> stream_data);
111 
113  Status DispatchPeriodInfo(int stream_index,
114  std::unique_ptr<PeriodInfo> period_info) {
115  std::unique_ptr<StreamData> stream_data(new StreamData);
116  stream_data->stream_index = stream_index;
117  stream_data->period_info = std::move(period_info);
118  return Dispatch(std::move(stream_data));
119  }
120 
122  Status DispatchStreamInfo(int stream_index,
123  std::unique_ptr<StreamInfo> stream_info) {
124  std::unique_ptr<StreamData> stream_data(new StreamData);
125  stream_data->stream_index = stream_index;
126  stream_data->stream_info = std::move(stream_info);
127  return Dispatch(std::move(stream_data));
128  }
129 
132  int stream_index,
133  std::unique_ptr<EncryptionConfig> encryption_config) {
134  std::unique_ptr<StreamData> stream_data(new StreamData);
135  stream_data->stream_index = stream_index;
136  stream_data->encryption_config = std::move(encryption_config);
137  return Dispatch(std::move(stream_data));
138  }
139 
141  Status DispatchMediaSample(int stream_index,
142  std::unique_ptr<MediaSample> media_sample) {
143  std::unique_ptr<StreamData> stream_data(new StreamData);
144  stream_data->stream_index = stream_index;
145  stream_data->media_sample = std::move(media_sample);
146  return Dispatch(std::move(stream_data));
147  }
148 
150  Status DispatchMediaEvent(int stream_index,
151  std::unique_ptr<MediaEvent> media_event) {
152  std::unique_ptr<StreamData> stream_data(new StreamData);
153  stream_data->stream_index = stream_index;
154  stream_data->media_event = std::move(media_event);
155  return Dispatch(std::move(stream_data));
156  }
157 
159  Status DispatchSegmentInfo(int stream_index,
160  std::unique_ptr<SegmentInfo> segment_info) {
161  std::unique_ptr<StreamData> stream_data(new StreamData);
162  stream_data->stream_index = stream_index;
163  stream_data->segment_info = std::move(segment_info);
164  return Dispatch(std::move(stream_data));
165  }
166 
167  int num_input_streams() const { return num_input_streams_; }
168  int next_output_stream_index() const { return next_output_stream_index_; }
169 
170  private:
171  MediaHandler(const MediaHandler&) = delete;
172  MediaHandler& operator=(const MediaHandler&) = delete;
173 
174  bool initialized_ = false;
175  // Number of input streams.
176  int num_input_streams_ = 0;
177  // The next available output stream index, used by AddHandler.
178  int next_output_stream_index_ = 0;
179  // output stream index -> {output handler, output handler input stream index}
180  // map.
181  std::map<int, std::pair<std::shared_ptr<MediaHandler>, int>> output_handlers_;
182 };
183 
184 } // namespace media
185 } // namespace shaka
186 
187 #endif // PACKAGER_MEDIA_BASE_MEDIA_HANDLER_H_
Status DispatchSegmentInfo(int stream_index, std::unique_ptr< SegmentInfo > segment_info)
Dispatch the segment info to downstream handlers.
Status SetHandler(int output_stream_index, std::shared_ptr< MediaHandler > handler)
Connect downstream handler at the specified output stream index.
virtual Status InitializeInternal()=0
Status DispatchPeriodInfo(int stream_index, std::unique_ptr< PeriodInfo > period_info)
Dispatch the period info to downstream handlers.
Status Dispatch(std::unique_ptr< StreamData > stream_data)
Status DispatchEncryptionConfig(int stream_index, std::unique_ptr< EncryptionConfig > encryption_config)
Dispatch the encryption config to downstream handlers.
virtual bool ValidateOutputStreamIndex(int stream_index) const
Validate if the stream at the specified index actually exists.
virtual Status Process(std::unique_ptr< StreamData > stream_data)=0
Status AddHandler(std::shared_ptr< MediaHandler > handler)
Connect downstream handler to the next availble output stream index.
Definition: media_handler.h:80
Status DispatchMediaSample(int stream_index, std::unique_ptr< MediaSample > media_sample)
Dispatch the media sample to downstream handlers.
virtual Status FlushStream(int input_stream_index)
Flush the stream at the specified input stream index.
Status DispatchStreamInfo(int stream_index, std::unique_ptr< StreamInfo > stream_info)
Dispatch the stream info to downstream handlers.
Status DispatchMediaEvent(int stream_index, std::unique_ptr< MediaEvent > media_event)
Dispatch the media event to downstream handlers.