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 
90  bool IsConnected() { return num_input_streams_ > 0; }
91 
92  protected:
95  virtual Status InitializeInternal() = 0;
96 
101  virtual Status Process(std::unique_ptr<StreamData> stream_data) = 0;
102 
104  virtual Status OnFlushRequest(size_t input_stream_index);
105 
107  virtual bool ValidateOutputStreamIndex(size_t stream_index) const;
108 
111  Status Dispatch(std::unique_ptr<StreamData> stream_data);
112 
114  Status DispatchPeriodInfo(size_t stream_index,
115  std::shared_ptr<PeriodInfo> period_info) {
116  std::unique_ptr<StreamData> stream_data(new StreamData);
117  stream_data->stream_index = stream_index;
118  stream_data->stream_data_type = StreamDataType::kPeriodInfo;
119  stream_data->period_info = std::move(period_info);
120  return Dispatch(std::move(stream_data));
121  }
122 
124  Status DispatchStreamInfo(size_t stream_index,
125  std::shared_ptr<StreamInfo> stream_info) {
126  std::unique_ptr<StreamData> stream_data(new StreamData);
127  stream_data->stream_index = stream_index;
128  stream_data->stream_data_type = StreamDataType::kStreamInfo;
129  stream_data->stream_info = std::move(stream_info);
130  return Dispatch(std::move(stream_data));
131  }
132 
134  Status DispatchMediaSample(size_t stream_index,
135  std::shared_ptr<MediaSample> media_sample) {
136  std::unique_ptr<StreamData> stream_data(new StreamData);
137  stream_data->stream_index = stream_index;
138  stream_data->stream_data_type = StreamDataType::kMediaSample;
139  stream_data->media_sample = std::move(media_sample);
140  return Dispatch(std::move(stream_data));
141  }
142 
144  Status DispatchMediaEvent(size_t stream_index,
145  std::shared_ptr<MediaEvent> media_event) {
146  std::unique_ptr<StreamData> stream_data(new StreamData);
147  stream_data->stream_index = stream_index;
148  stream_data->stream_data_type = StreamDataType::kMediaEvent;
149  stream_data->media_event = std::move(media_event);
150  return Dispatch(std::move(stream_data));
151  }
152 
154  Status DispatchSegmentInfo(size_t stream_index,
155  std::shared_ptr<SegmentInfo> segment_info) {
156  std::unique_ptr<StreamData> stream_data(new StreamData);
157  stream_data->stream_index = stream_index;
158  stream_data->stream_data_type = StreamDataType::kSegmentInfo;
159  stream_data->segment_info = std::move(segment_info);
160  return Dispatch(std::move(stream_data));
161  }
162 
164  Status FlushDownstream(size_t output_stream_index);
165 
168 
169  bool initialized() { return initialized_; }
170  size_t num_input_streams() const { return num_input_streams_; }
171  size_t next_output_stream_index() const { return next_output_stream_index_; }
172  const std::map<size_t, std::pair<std::shared_ptr<MediaHandler>, size_t>>&
173  output_handlers() {
174  return output_handlers_;
175  }
176 
177  private:
178  MediaHandler(const MediaHandler&) = delete;
179  MediaHandler& operator=(const MediaHandler&) = delete;
180 
181  bool initialized_ = false;
182  // Number of input streams.
183  size_t num_input_streams_ = 0;
184  // The next available output stream index, used by AddHandler.
185  size_t next_output_stream_index_ = 0;
186  // output stream index -> {output handler, output handler input stream index}
187  // map.
188  std::map<size_t, std::pair<std::shared_ptr<MediaHandler>, size_t>>
189  output_handlers_;
190 };
191 
192 } // namespace media
193 } // namespace shaka
194 
195 #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.
bool IsConnected()
Validate if the handler is connected to its upstream handler.
Definition: media_handler.h:90
virtual Status Process(std::unique_ptr< StreamData > stream_data)=0
Status FlushAllDownstreams()
Flush all connected downstreams.
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.