DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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(int output_stream_index,
13  std::shared_ptr<MediaHandler> handler) {
14  if (!ValidateOutputStreamIndex(output_stream_index))
15  return Status(error::INVALID_ARGUMENT, "Invalid output stream index");
16  if (output_handlers_.find(output_stream_index) != output_handlers_.end()) {
17  return Status(error::ALREADY_EXISTS,
18  "The handler at the specified index already exists.");
19  }
20  output_handlers_[output_stream_index] =
21  std::make_pair(handler, handler->num_input_streams_++);
22  next_output_stream_index_ = output_stream_index + 1;
23  return Status::OK;
24 }
25 
26 Status MediaHandler::FlushStream(int input_stream_index) {
27  // The default implementation treats the output stream index to be identical
28  // to the input stream index, which is true for most handlers.
29  auto handler_it = output_handlers_.find(input_stream_index);
30  if (handler_it == output_handlers_.end()) {
31  return Status(error::NOT_FOUND,
32  "No output handler exist at the specified index.");
33  }
34  return handler_it->second.first->FlushStream(handler_it->second.second);
35 }
36 
37 bool MediaHandler::ValidateOutputStreamIndex(int stream_index) const {
38  return stream_index >= 0 && stream_index < num_input_streams_;
39 }
40 
41 Status MediaHandler::Dispatch(std::unique_ptr<StreamData> stream_data) {
42  int output_stream_index = stream_data->stream_index;
43  auto handler_it = output_handlers_.find(output_stream_index);
44  if (handler_it == output_handlers_.end()) {
45  return Status(error::NOT_FOUND,
46  "No output handler exist at the specified index.");
47  }
48  stream_data->stream_index = handler_it->second.second;
49  return handler_it->second.first->Process(std::move(stream_data));
50 }
51 
52 } // namespace media
53 } // namespace shaka
Status SetHandler(int output_stream_index, std::shared_ptr< MediaHandler > handler)
Connect downstream handler at the specified output stream index.
Status Dispatch(std::unique_ptr< StreamData > stream_data)
virtual bool ValidateOutputStreamIndex(int stream_index) const
Validate if the stream at the specified index actually exists.
virtual Status FlushStream(int input_stream_index)
Flush the stream at the specified input stream index.