Shaka Packager SDK
media_handler.cc
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 #include "packager/media/base/media_handler.h"
8 
9 namespace shaka {
10 namespace media {
11 
12 std::string StreamDataTypeToString(StreamDataType type) {
13  switch (type) {
14  case StreamDataType::kStreamInfo:
15  return "stream info";
16  case StreamDataType::kMediaSample:
17  return "media sample";
18  case StreamDataType::kTextSample:
19  return "text sample";
20  case StreamDataType::kSegmentInfo:
21  return "segment info";
22  case StreamDataType::kScte35Event:
23  return "scte35 event";
24  case StreamDataType::kCueEvent:
25  return "cue event";
26  case StreamDataType::kUnknown:
27  return "unknown";
28  }
29  return "unknown";
30 }
31 
32 Status MediaHandler::SetHandler(size_t output_stream_index,
33  std::shared_ptr<MediaHandler> handler) {
34  if (output_handlers_.find(output_stream_index) != output_handlers_.end()) {
35  return Status(error::ALREADY_EXISTS,
36  "The handler at the specified index already exists.");
37  }
38  output_handlers_[output_stream_index] =
39  std::make_pair(handler, handler->num_input_streams_++);
40  next_output_stream_index_ = output_stream_index + 1;
41  return Status::OK;
42 }
43 
45  if (initialized_)
46  return Status::OK;
47  Status status = InitializeInternal();
48  if (!status.ok())
49  return status;
50  for (auto& pair : output_handlers_) {
51  if (!ValidateOutputStreamIndex(pair.first))
52  return Status(error::INVALID_ARGUMENT, "Invalid output stream index");
53  status = pair.second.first->Initialize();
54  if (!status.ok())
55  return status;
56  }
57  initialized_ = true;
58  return Status::OK;
59 }
60 
61 Status MediaHandler::OnFlushRequest(size_t input_stream_index) {
62  // The default implementation treats the output stream index to be identical
63  // to the input stream index, which is true for most handlers.
64  const size_t output_stream_index = input_stream_index;
65  return FlushDownstream(output_stream_index);
66 }
67 
68 bool MediaHandler::ValidateOutputStreamIndex(size_t stream_index) const {
69  return stream_index < num_input_streams_;
70 }
71 
72 Status MediaHandler::Dispatch(std::unique_ptr<StreamData> stream_data) const {
73  size_t output_stream_index = stream_data->stream_index;
74  auto handler_it = output_handlers_.find(output_stream_index);
75  if (handler_it == output_handlers_.end()) {
76  return Status(error::NOT_FOUND,
77  "No output handler exist at the specified index.");
78  }
79  stream_data->stream_index = handler_it->second.second;
80  return handler_it->second.first->Process(std::move(stream_data));
81 }
82 
83 Status MediaHandler::FlushDownstream(size_t output_stream_index) {
84  auto handler_it = output_handlers_.find(output_stream_index);
85  if (handler_it == output_handlers_.end()) {
86  return Status(error::NOT_FOUND,
87  "No output handler exist at the specified index.");
88  }
89  return handler_it->second.first->OnFlushRequest(handler_it->second.second);
90 }
91 
93  for (const auto& pair : output_handlers_) {
94  Status status = pair.second.first->OnFlushRequest(pair.second.second);
95  if (!status.ok()) {
96  return status;
97  }
98  }
99  return Status::OK;
100 }
101 
102 } // namespace media
103 } // namespace shaka
virtual bool ValidateOutputStreamIndex(size_t stream_index) const
Validate if the stream at the specified index actually exists.
Status Dispatch(std::unique_ptr< StreamData > stream_data) const
virtual Status InitializeInternal()=0
virtual Status OnFlushRequest(size_t input_stream_index)
Event handler for flush request at the specific input stream index.
All the methods that are virtual are virtual for mocking.
Status FlushAllDownstreams()
Flush all connected downstream handlers.
Status FlushDownstream(size_t output_stream_index)
Flush the downstream connected at the specified output stream index.
Status SetHandler(size_t output_stream_index, std::shared_ptr< MediaHandler > handler)
Connect downstream handler at the specified output stream index.