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 
27  if (initialized_)
28  return Status::OK;
29  Status status = InitializeInternal();
30  if (!status.ok())
31  return status;
32  for (auto& pair : output_handlers_) {
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::FlushStream(int 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  auto handler_it = output_handlers_.find(input_stream_index);
45  if (handler_it == output_handlers_.end()) {
46  return Status(error::NOT_FOUND,
47  "No output handler exist at the specified index.");
48  }
49  return handler_it->second.first->FlushStream(handler_it->second.second);
50 }
51 
52 bool MediaHandler::ValidateOutputStreamIndex(int stream_index) const {
53  return stream_index >= 0 && stream_index < num_input_streams_;
54 }
55 
56 Status MediaHandler::Dispatch(std::unique_ptr<StreamData> stream_data) {
57  int output_stream_index = stream_data->stream_index;
58  auto handler_it = output_handlers_.find(output_stream_index);
59  if (handler_it == output_handlers_.end()) {
60  return Status(error::NOT_FOUND,
61  "No output handler exist at the specified index.");
62  }
63  stream_data->stream_index = handler_it->second.second;
64  return handler_it->second.first->Process(std::move(stream_data));
65 }
66 
67 } // namespace media
68 } // namespace shaka
Status SetHandler(int output_stream_index, std::shared_ptr< MediaHandler > handler)
Connect downstream handler at the specified output stream index.
virtual Status InitializeInternal()=0
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.