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