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::OnFlushRequest(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  const int output_stream_index = input_stream_index;
45  return FlushDownstream(output_stream_index);
46 }
47 
48 bool MediaHandler::ValidateOutputStreamIndex(int stream_index) const {
49  return stream_index >= 0 && stream_index < num_input_streams_;
50 }
51 
52 Status MediaHandler::Dispatch(std::unique_ptr<StreamData> stream_data) {
53  int 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(int 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 
72 } // namespace media
73 } // 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)
Status FlushDownstream(int output_stream_index)
Flush the downstream connected at the specified output stream index.
virtual bool ValidateOutputStreamIndex(int stream_index) const
Validate if the stream at the specified index actually exists.
virtual Status OnFlushRequest(int input_stream_index)
Event handler for flush request at the specific input stream index.