2017-01-23 22:51:00 +00:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
#include "packager/media/base/media_handler.h"
|
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
|
2017-03-03 00:10:30 +00:00
|
|
|
Status MediaHandler::SetHandler(size_t output_stream_index,
|
2017-01-23 22:51:00 +00:00
|
|
|
std::shared_ptr<MediaHandler> handler) {
|
|
|
|
if (output_handlers_.find(output_stream_index) != output_handlers_.end()) {
|
|
|
|
return Status(error::ALREADY_EXISTS,
|
|
|
|
"The handler at the specified index already exists.");
|
|
|
|
}
|
|
|
|
output_handlers_[output_stream_index] =
|
|
|
|
std::make_pair(handler, handler->num_input_streams_++);
|
|
|
|
next_output_stream_index_ = output_stream_index + 1;
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2017-02-02 18:28:29 +00:00
|
|
|
Status MediaHandler::Initialize() {
|
|
|
|
if (initialized_)
|
|
|
|
return Status::OK;
|
|
|
|
Status status = InitializeInternal();
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
for (auto& pair : output_handlers_) {
|
2017-02-24 01:17:47 +00:00
|
|
|
if (!ValidateOutputStreamIndex(pair.first))
|
|
|
|
return Status(error::INVALID_ARGUMENT, "Invalid output stream index");
|
2017-02-02 18:28:29 +00:00
|
|
|
status = pair.second.first->Initialize();
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
initialized_ = true;
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2017-03-03 00:10:30 +00:00
|
|
|
Status MediaHandler::OnFlushRequest(size_t input_stream_index) {
|
2017-01-23 22:51:00 +00:00
|
|
|
// The default implementation treats the output stream index to be identical
|
|
|
|
// to the input stream index, which is true for most handlers.
|
2017-03-03 00:10:30 +00:00
|
|
|
const size_t output_stream_index = input_stream_index;
|
2017-02-22 20:14:26 +00:00
|
|
|
return FlushDownstream(output_stream_index);
|
2017-01-23 22:51:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 00:10:30 +00:00
|
|
|
bool MediaHandler::ValidateOutputStreamIndex(size_t stream_index) const {
|
|
|
|
return stream_index < num_input_streams_;
|
2017-01-23 22:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status MediaHandler::Dispatch(std::unique_ptr<StreamData> stream_data) {
|
2017-03-03 00:10:30 +00:00
|
|
|
size_t output_stream_index = stream_data->stream_index;
|
2017-01-23 22:51:00 +00:00
|
|
|
auto handler_it = output_handlers_.find(output_stream_index);
|
|
|
|
if (handler_it == output_handlers_.end()) {
|
|
|
|
return Status(error::NOT_FOUND,
|
|
|
|
"No output handler exist at the specified index.");
|
|
|
|
}
|
|
|
|
stream_data->stream_index = handler_it->second.second;
|
|
|
|
return handler_it->second.first->Process(std::move(stream_data));
|
|
|
|
}
|
|
|
|
|
2017-03-03 00:10:30 +00:00
|
|
|
Status MediaHandler::FlushDownstream(size_t output_stream_index) {
|
2017-02-22 20:14:26 +00:00
|
|
|
auto handler_it = output_handlers_.find(output_stream_index);
|
|
|
|
if (handler_it == output_handlers_.end()) {
|
|
|
|
return Status(error::NOT_FOUND,
|
|
|
|
"No output handler exist at the specified index.");
|
|
|
|
}
|
|
|
|
return handler_it->second.first->OnFlushRequest(handler_it->second.second);
|
|
|
|
}
|
|
|
|
|
2017-03-21 23:14:46 +00:00
|
|
|
Status MediaHandler::FlushAllDownstreams() {
|
|
|
|
for (const auto& pair : output_handlers_) {
|
|
|
|
Status status = pair.second.first->OnFlushRequest(pair.second.second);
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2017-01-23 22:51:00 +00:00
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|