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  kMediaSample,
26  kMediaEvent,
27  kSegmentInfo,
28 };
29 
30 // TODO(kqyang): Define these structures.
31 struct PeriodInfo {};
32 struct MediaEvent {};
33 struct SegmentInfo {
34  bool is_subsegment = false;
35  bool is_encrypted = false;
36  int64_t start_timestamp = -1;
37  int64_t duration = 0;
38  // This is only available if key rotation is enabled. Note that we may have
39  // a |key_rotation_encryption_config| even if the segment is not encrypted,
40  // which is the case for clear lead.
41  std::shared_ptr<EncryptionConfig> key_rotation_encryption_config;
42 };
43 
44 // TODO(kqyang): Should we use protobuf?
45 struct StreamData {
46  size_t stream_index = static_cast<size_t>(-1);
47  StreamDataType stream_data_type = StreamDataType::kUnknown;
48 
49  std::shared_ptr<PeriodInfo> period_info;
50  std::shared_ptr<StreamInfo> stream_info;
51  std::shared_ptr<MediaSample> media_sample;
52  std::shared_ptr<MediaEvent> media_event;
53  std::shared_ptr<SegmentInfo> segment_info;
54 };
55 
71 class MediaHandler {
72  public:
73  MediaHandler() = default;
74  virtual ~MediaHandler() = default;
75 
77  Status SetHandler(size_t output_stream_index,
78  std::shared_ptr<MediaHandler> handler);
79 
81  Status AddHandler(std::shared_ptr<MediaHandler> handler) {
82  return SetHandler(next_output_stream_index_, handler);
83  }
84 
88 
89  protected:
92  virtual Status InitializeInternal() = 0;
93 
98  virtual Status Process(std::unique_ptr<StreamData> stream_data) = 0;
99 
101  virtual Status OnFlushRequest(size_t input_stream_index);
102 
104  virtual bool ValidateOutputStreamIndex(size_t stream_index) const;
105 
108  Status Dispatch(std::unique_ptr<StreamData> stream_data);
109 
111  Status DispatchPeriodInfo(size_t stream_index,
112  std::shared_ptr<PeriodInfo> period_info) {
113  std::unique_ptr<StreamData> stream_data(new StreamData);
114  stream_data->stream_index = stream_index;
115  stream_data->stream_data_type = StreamDataType::kPeriodInfo;
116  stream_data->period_info = std::move(period_info);
117  return Dispatch(std::move(stream_data));
118  }
119 
121  Status DispatchStreamInfo(size_t stream_index,
122  std::shared_ptr<StreamInfo> stream_info) {
123  std::unique_ptr<StreamData> stream_data(new StreamData);
124  stream_data->stream_index = stream_index;
125  stream_data->stream_data_type = StreamDataType::kStreamInfo;
126  stream_data->stream_info = std::move(stream_info);
127  return Dispatch(std::move(stream_data));
128  }
129 
131  Status DispatchMediaSample(size_t stream_index,
132  std::shared_ptr<MediaSample> media_sample) {
133  std::unique_ptr<StreamData> stream_data(new StreamData);
134  stream_data->stream_index = stream_index;
135  stream_data->stream_data_type = StreamDataType::kMediaSample;
136  stream_data->media_sample = std::move(media_sample);
137  return Dispatch(std::move(stream_data));
138  }
139 
141  Status DispatchMediaEvent(size_t stream_index,
142  std::shared_ptr<MediaEvent> media_event) {
143  std::unique_ptr<StreamData> stream_data(new StreamData);
144  stream_data->stream_index = stream_index;
145  stream_data->stream_data_type = StreamDataType::kMediaEvent;
146  stream_data->media_event = std::move(media_event);
147  return Dispatch(std::move(stream_data));
148  }
149 
151  Status DispatchSegmentInfo(size_t stream_index,
152  std::shared_ptr<SegmentInfo> segment_info) {
153  std::unique_ptr<StreamData> stream_data(new StreamData);
154  stream_data->stream_index = stream_index;
155  stream_data->stream_data_type = StreamDataType::kSegmentInfo;
156  stream_data->segment_info = std::move(segment_info);
157  return Dispatch(std::move(stream_data));
158  }
159 
161  Status FlushDownstream(size_t output_stream_index);
162 
163  bool initialized() { return initialized_; }
164  size_t num_input_streams() const { return num_input_streams_; }
165  size_t next_output_stream_index() const { return next_output_stream_index_; }
166  const std::map<size_t, std::pair<std::shared_ptr<MediaHandler>, size_t>>&
167  output_handlers() {
168  return output_handlers_;
169  }
170 
171  private:
172  MediaHandler(const MediaHandler&) = delete;
173  MediaHandler& operator=(const MediaHandler&) = delete;
174 
175  bool initialized_ = false;
176  // Number of input streams.
177  size_t num_input_streams_ = 0;
178  // The next available output stream index, used by AddHandler.
179  size_t next_output_stream_index_ = 0;
180  // output stream index -> {output handler, output handler input stream index}
181  // map.
182  std::map<size_t, std::pair<std::shared_ptr<MediaHandler>, size_t>>
183  output_handlers_;
184 };
185 
186 } // namespace media
187 } // namespace shaka
188 
189 #endif // PACKAGER_MEDIA_BASE_MEDIA_HANDLER_H_
virtual bool ValidateOutputStreamIndex(size_t stream_index) const
Validate if the stream at the specified index actually exists.
Status DispatchMediaSample(size_t stream_index, std::shared_ptr< MediaSample > media_sample)
Dispatch the media sample to downstream handlers.
virtual Status InitializeInternal()=0
Status Dispatch(std::unique_ptr< StreamData > stream_data)
virtual Status OnFlushRequest(size_t input_stream_index)
Event handler for flush request at the specific input stream index.
virtual Status Process(std::unique_ptr< StreamData > stream_data)=0
Status DispatchMediaEvent(size_t stream_index, std::shared_ptr< MediaEvent > media_event)
Dispatch the media event to downstream handlers.
Status DispatchStreamInfo(size_t stream_index, std::shared_ptr< StreamInfo > stream_info)
Dispatch the stream info to downstream handlers.
Status DispatchPeriodInfo(size_t stream_index, std::shared_ptr< PeriodInfo > period_info)
Dispatch the period info to downstream handlers.
Status AddHandler(std::shared_ptr< MediaHandler > handler)
Connect downstream handler to the next availble output stream index.
Definition: media_handler.h:81
Status FlushDownstream(size_t output_stream_index)
Flush the downstream connected at the specified output stream index.
Status DispatchSegmentInfo(size_t stream_index, std::shared_ptr< SegmentInfo > segment_info)
Dispatch the segment info to downstream handlers.
Status SetHandler(size_t output_stream_index, std::shared_ptr< MediaHandler > handler)
Connect downstream handler at the specified output stream index.