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/status.h"
15 
16 namespace shaka {
17 namespace media {
18 
34 class MediaHandler {
35  public:
36  MediaHandler() = default;
37  virtual ~MediaHandler() = default;
38 
40  Status SetHandler(int output_stream_index,
41  std::shared_ptr<MediaHandler> handler);
42 
44  Status AddHandler(std::shared_ptr<MediaHandler> handler) {
45  return SetHandler(next_output_stream_index_, handler);
46  }
47 
48  protected:
49  enum class StreamDataType {
50  kUnknown,
51  kPeriodInfo,
52  kStreamInfo,
53  kEncryptionConfig,
54  kMediaSample,
55  kMediaEvent,
56  kSegmentInfo,
57  };
58 
59  // TODO(kqyang): Define these structures.
60  struct PeriodInfo {};
61  struct StreamInfo {};
62  struct EncryptionConfig {};
63  struct MediaSample {};
64  struct MediaEvent {};
65  struct SegmentInfo {};
66 
67  // TODO(kqyang): Should we use protobuf?
68  struct StreamData {
69  int stream_index;
70  StreamDataType stream_data_type;
71 
72  std::unique_ptr<PeriodInfo> period_info;
73  std::unique_ptr<StreamInfo> stream_info;
74  std::unique_ptr<EncryptionConfig> encryption_config;
75  std::unique_ptr<MediaSample> media_sample;
76  std::unique_ptr<MediaEvent> media_event;
77  std::unique_ptr<SegmentInfo> segment_info;
78  };
79 
84  virtual Status Process(std::unique_ptr<StreamData> stream_data) = 0;
85 
87  virtual Status FlushStream(int input_stream_index);
88 
90  virtual bool ValidateOutputStreamIndex(int stream_index) const;
91 
94  Status Dispatch(std::unique_ptr<StreamData> stream_data);
95 
97  Status DispatchPeriodInfo(int stream_index,
98  std::unique_ptr<PeriodInfo> period_info) {
99  std::unique_ptr<StreamData> stream_data(new StreamData);
100  stream_data->stream_index = stream_index;
101  stream_data->period_info = std::move(period_info);
102  return Dispatch(std::move(stream_data));
103  }
104 
106  Status DispatchStreamInfo(int stream_index,
107  std::unique_ptr<StreamInfo> stream_info) {
108  std::unique_ptr<StreamData> stream_data(new StreamData);
109  stream_data->stream_index = stream_index;
110  stream_data->stream_info = std::move(stream_info);
111  return Dispatch(std::move(stream_data));
112  }
113 
116  int stream_index,
117  std::unique_ptr<EncryptionConfig> encryption_config) {
118  std::unique_ptr<StreamData> stream_data(new StreamData);
119  stream_data->stream_index = stream_index;
120  stream_data->encryption_config = std::move(encryption_config);
121  return Dispatch(std::move(stream_data));
122  }
123 
125  Status DispatchMediaSample(int stream_index,
126  std::unique_ptr<MediaSample> media_sample) {
127  std::unique_ptr<StreamData> stream_data(new StreamData);
128  stream_data->stream_index = stream_index;
129  stream_data->media_sample = std::move(media_sample);
130  return Dispatch(std::move(stream_data));
131  }
132 
134  Status DispatchMediaEvent(int stream_index,
135  std::unique_ptr<MediaEvent> media_event) {
136  std::unique_ptr<StreamData> stream_data(new StreamData);
137  stream_data->stream_index = stream_index;
138  stream_data->media_event = std::move(media_event);
139  return Dispatch(std::move(stream_data));
140  }
141 
143  Status DispatchSegmentInfo(int stream_index,
144  std::unique_ptr<SegmentInfo> segment_info) {
145  std::unique_ptr<StreamData> stream_data(new StreamData);
146  stream_data->stream_index = stream_index;
147  stream_data->segment_info = std::move(segment_info);
148  return Dispatch(std::move(stream_data));
149  }
150 
151  int num_input_streams() const { return num_input_streams_; }
152 
153  private:
154  MediaHandler(const MediaHandler&) = delete;
155  MediaHandler& operator=(const MediaHandler&) = delete;
156 
157  // Number of input streams.
158  int num_input_streams_ = 0;
159  // The next available output stream index, used by AddHandler.
160  int next_output_stream_index_ = 0;
161  // output stream index -> {output handler, output handler input stream index}
162  // map.
163  std::map<int, std::pair<std::shared_ptr<MediaHandler>, int>> output_handlers_;
164 };
165 
166 } // namespace media
167 } // namespace shaka
168 
169 #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.
Status DispatchPeriodInfo(int stream_index, std::unique_ptr< PeriodInfo > period_info)
Dispatch the period info to downstream handlers.
Definition: media_handler.h:97
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:44
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.