Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 Status MediaHandler::SetHandler(size_t output_stream_index,
13  std::shared_ptr<MediaHandler> handler) {
14  if (output_handlers_.find(output_stream_index) != output_handlers_.end()) {
15  return Status(error::ALREADY_EXISTS,
16  "The handler at the specified index already exists.");
17  }
18  output_handlers_[output_stream_index] =
19  std::make_pair(handler, handler->num_input_streams_++);
20  next_output_stream_index_ = output_stream_index + 1;
21  return Status::OK;
22 }
23 
25  if (initialized_)
26  return Status::OK;
27  Status status = InitializeInternal();
28  if (!status.ok())
29  return status;
30  for (auto& pair : output_handlers_) {
31  if (!ValidateOutputStreamIndex(pair.first))
32  return Status(error::INVALID_ARGUMENT, "Invalid output stream index");
33  status = pair.second.first->Initialize();
34  if (!status.ok())
35  return status;
36  }
37  initialized_ = true;
38  return Status::OK;
39 }
40 
41 Status MediaHandler::OnFlushRequest(size_t input_stream_index) {
42  // The default implementation treats the output stream index to be identical
43  // to the input stream index, which is true for most handlers.
44  const size_t output_stream_index = input_stream_index;
45  return FlushDownstream(output_stream_index);
46 }
47 
48 bool MediaHandler::ValidateOutputStreamIndex(size_t stream_index) const {
49  return stream_index < num_input_streams_;
50 }
51 
52 Status MediaHandler::Dispatch(std::unique_ptr<StreamData> stream_data) {
53  size_t output_stream_index = stream_data->stream_index;
54  auto handler_it = output_handlers_.find(output_stream_index);
55  if (handler_it == output_handlers_.end()) {
56  return Status(error::NOT_FOUND,
57  "No output handler exist at the specified index.");
58  }
59  stream_data->stream_index = handler_it->second.second;
60  return handler_it->second.first->Process(std::move(stream_data));
61 }
62 
63 Status MediaHandler::FlushDownstream(size_t output_stream_index) {
64  auto handler_it = output_handlers_.find(output_stream_index);
65  if (handler_it == output_handlers_.end()) {
66  return Status(error::NOT_FOUND,
67  "No output handler exist at the specified index.");
68  }
69  return handler_it->second.first->OnFlushRequest(handler_it->second.second);
70 }
71 
73  for (const auto& pair : output_handlers_) {
74  Status status = pair.second.first->OnFlushRequest(pair.second.second);
75  if (!status.ok()) {
76  return status;
77  }
78  }
79  return Status::OK;
80 }
81 
82 } // namespace media
83 } // namespace shaka
virtual bool ValidateOutputStreamIndex(size_t stream_index) const
Validate if the stream at the specified index actually exists.
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.
Status FlushAllDownstreams()
Flush all connected downstreams.
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.