2017-02-27 17:22:25 +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/trick_play/trick_play_handler.h"
|
|
|
|
|
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/media/base/video_stream_info.h"
|
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
|
2017-03-21 23:14:46 +00:00
|
|
|
namespace {
|
|
|
|
const size_t kMainStreamIndex = 0;
|
2017-02-27 17:22:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 23:14:46 +00:00
|
|
|
TrickPlayHandler::TrickPlayHandler() {}
|
|
|
|
|
2017-02-27 17:22:25 +00:00
|
|
|
TrickPlayHandler::~TrickPlayHandler() {}
|
|
|
|
|
2017-03-21 23:14:46 +00:00
|
|
|
void TrickPlayHandler::SetHandlerForMainStream(
|
|
|
|
std::shared_ptr<MediaHandler> handler) {
|
|
|
|
SetHandler(kMainStreamIndex, std::move(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrickPlayHandler::SetHandlerForTrickPlay(
|
2017-05-15 16:22:06 +00:00
|
|
|
uint32_t trick_play_factor,
|
2017-03-21 23:14:46 +00:00
|
|
|
std::shared_ptr<MediaHandler> handler) {
|
2017-05-15 16:22:06 +00:00
|
|
|
trick_play_factors_.push_back(trick_play_factor);
|
2017-03-21 23:14:46 +00:00
|
|
|
// Trick play streams start from index 1.
|
2017-05-15 16:22:06 +00:00
|
|
|
SetHandler(trick_play_factors_.size(), std::move(handler));
|
2017-03-21 23:14:46 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 17:22:25 +00:00
|
|
|
Status TrickPlayHandler::InitializeInternal() {
|
2017-03-21 23:14:46 +00:00
|
|
|
if (!HasMainStream()) {
|
|
|
|
return Status(error::TRICK_PLAY_ERROR,
|
|
|
|
"Trick play does not have main stream");
|
|
|
|
}
|
2017-05-15 16:22:06 +00:00
|
|
|
if (trick_play_factors_.empty()) {
|
2017-03-21 23:14:46 +00:00
|
|
|
return Status(error::TRICK_PLAY_ERROR,
|
2017-05-15 16:22:06 +00:00
|
|
|
"Trick play factors are not specified.");
|
2017-03-21 23:14:46 +00:00
|
|
|
}
|
2017-05-15 16:22:06 +00:00
|
|
|
size_t num_trick_play_factors = trick_play_factors_.size();
|
|
|
|
cached_stream_data_.resize(num_trick_play_factors);
|
|
|
|
playback_rates_.resize(num_trick_play_factors, 0);
|
2017-03-21 23:14:46 +00:00
|
|
|
|
2017-02-27 17:22:25 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status TrickPlayHandler::Process(
|
|
|
|
std::unique_ptr<StreamData> input_stream_data) {
|
|
|
|
// The non-trick play stream is dispatched at index 0.
|
|
|
|
// The trick-play streams are dispatched to index 1, index 2 and so on.
|
|
|
|
DCHECK_EQ(input_stream_data->stream_index, 0u);
|
|
|
|
std::unique_ptr<StreamData> output_stream_data(new StreamData());
|
|
|
|
*output_stream_data = *input_stream_data;
|
|
|
|
Status status = Dispatch(std::move(output_stream_data));
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<StreamData> stream_data(std::move(input_stream_data));
|
|
|
|
if (stream_data->stream_data_type == StreamDataType::kStreamInfo) {
|
|
|
|
if (stream_data->stream_info->stream_type() != kStreamVideo) {
|
|
|
|
status.SetError(error::TRICK_PLAY_ERROR,
|
|
|
|
"Trick play does not support non-video stream");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
const VideoStreamInfo& video_stream_info =
|
|
|
|
static_cast<const VideoStreamInfo&>(*stream_data->stream_info);
|
2017-05-15 16:22:06 +00:00
|
|
|
if (video_stream_info.trick_play_factor() > 0) {
|
2017-02-27 17:22:25 +00:00
|
|
|
status.SetError(error::TRICK_PLAY_ERROR,
|
|
|
|
"This stream is alreay a trick play stream.");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 23:14:46 +00:00
|
|
|
if (stream_data->stream_data_type == StreamDataType::kSegmentInfo) {
|
|
|
|
for (auto& cached_data : cached_stream_data_) {
|
|
|
|
// It is possible that trick play stream has large frame duration that
|
|
|
|
// some segments in the main stream are skipped. To avoid empty segments,
|
|
|
|
// only cache SegementInfo with MediaSample before it.
|
|
|
|
if (!cached_data.empty() &&
|
|
|
|
cached_data.back()->stream_data_type == StreamDataType::kMediaSample)
|
|
|
|
cached_data.push_back(stream_data);
|
|
|
|
}
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2017-02-27 17:22:25 +00:00
|
|
|
if (stream_data->stream_data_type != StreamDataType::kMediaSample) {
|
|
|
|
// Non media sample stream data needs to be dispatched to every output
|
|
|
|
// stream. It is just cached in every queue until a new key frame comes or
|
|
|
|
// the stream is flushed.
|
|
|
|
for (size_t i = 0; i < cached_stream_data_.size(); ++i)
|
|
|
|
cached_stream_data_[i].push_back(stream_data);
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream_data->media_sample->is_key_frame()) {
|
|
|
|
// For a new key frame, some of the trick play streams may include it.
|
|
|
|
// The cached data in those trick play streams will be processed.
|
2017-05-15 16:22:06 +00:00
|
|
|
DCHECK_EQ(trick_play_factors_.size(), cached_stream_data_.size());
|
2017-02-27 17:22:25 +00:00
|
|
|
for (size_t i = 0; i < cached_stream_data_.size(); ++i) {
|
2017-05-15 16:22:06 +00:00
|
|
|
uint32_t factor = trick_play_factors_[i];
|
|
|
|
if (total_key_frames_ % factor == 0) {
|
2017-03-21 23:14:46 +00:00
|
|
|
// Delay processing cached stream data until receiving the second key
|
|
|
|
// frame so that the GOP size could be derived.
|
|
|
|
if (!cached_stream_data_[i].empty() && total_key_frames_ > 0) {
|
|
|
|
// Num of frames between first two key frames in the trick play
|
|
|
|
// streams. Use this as the playback_rate.
|
|
|
|
if (playback_rates_[i] == 0)
|
|
|
|
playback_rates_[i] = total_frames_;
|
|
|
|
|
2017-02-27 17:22:25 +00:00
|
|
|
Status status =
|
|
|
|
ProcessCachedStreamData(i + 1, &cached_stream_data_[i]);
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
cached_stream_data_[i].push_back(stream_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
total_key_frames_++;
|
|
|
|
}
|
|
|
|
|
2017-03-21 23:14:46 +00:00
|
|
|
total_frames_++;
|
2017-02-27 17:22:25 +00:00
|
|
|
prev_sample_end_timestamp_ =
|
|
|
|
stream_data->media_sample->dts() + stream_data->media_sample->duration();
|
2017-03-21 23:14:46 +00:00
|
|
|
|
2017-02-27 17:22:25 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TrickPlayHandler::ValidateOutputStreamIndex(size_t stream_index) const {
|
|
|
|
// Output stream index should be less than the number of trick play
|
|
|
|
// streams + one original stream.
|
2017-05-15 16:22:06 +00:00
|
|
|
return stream_index <= trick_play_factors_.size();
|
2017-02-27 17:22:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Status TrickPlayHandler::OnFlushRequest(size_t input_stream_index) {
|
|
|
|
DCHECK_EQ(input_stream_index, 0u)
|
|
|
|
<< "Trick Play Handler should only have single input.";
|
|
|
|
for (size_t i = 0; i < cached_stream_data_.size(); ++i) {
|
2017-03-21 23:14:46 +00:00
|
|
|
LOG_IF(WARNING, playback_rates_[i] == 0)
|
2017-05-15 16:22:06 +00:00
|
|
|
<< "Max playout rate for trick play factor " << trick_play_factors_[i]
|
2017-03-21 23:14:46 +00:00
|
|
|
<< " is not determined. "
|
|
|
|
<< "Specify it as total number of frames: " << total_frames_ << ".";
|
|
|
|
playback_rates_[i] = total_frames_;
|
2017-02-27 17:22:25 +00:00
|
|
|
ProcessCachedStreamData(i + 1, &cached_stream_data_[i]);
|
|
|
|
}
|
2017-03-21 23:14:46 +00:00
|
|
|
return MediaHandler::FlushAllDownstreams();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TrickPlayHandler::HasMainStream() {
|
|
|
|
const auto& handlers = output_handlers();
|
|
|
|
const auto& main_stream_handler = handlers.find(kMainStreamIndex);
|
|
|
|
if (main_stream_handler == handlers.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return main_stream_handler->second.first != nullptr;
|
2017-02-27 17:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status TrickPlayHandler::ProcessCachedStreamData(
|
2017-03-28 06:04:29 +00:00
|
|
|
size_t output_stream_index,
|
2017-02-27 17:22:25 +00:00
|
|
|
std::deque<std::shared_ptr<StreamData>>* cached_stream_data) {
|
|
|
|
while (!cached_stream_data->empty()) {
|
|
|
|
Status status =
|
|
|
|
ProcessOneStreamData(output_stream_index, cached_stream_data->front());
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
cached_stream_data->pop_front();
|
|
|
|
}
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status TrickPlayHandler::ProcessOneStreamData(
|
2017-03-28 06:04:29 +00:00
|
|
|
size_t output_stream_index,
|
2017-02-27 17:22:25 +00:00
|
|
|
const std::shared_ptr<StreamData>& stream_data) {
|
2017-03-21 23:14:46 +00:00
|
|
|
size_t trick_play_index = output_stream_index - 1;
|
2017-05-15 16:22:06 +00:00
|
|
|
uint32_t trick_play_factor = trick_play_factors_[trick_play_index];
|
2017-02-27 17:22:25 +00:00
|
|
|
Status status;
|
|
|
|
switch (stream_data->stream_data_type) {
|
2017-05-15 16:22:06 +00:00
|
|
|
// trick_play_factor in StreamInfo should be modified.
|
2017-02-27 17:22:25 +00:00
|
|
|
case StreamDataType::kStreamInfo: {
|
|
|
|
const VideoStreamInfo& video_stream_info =
|
|
|
|
static_cast<const VideoStreamInfo&>(*stream_data->stream_info);
|
|
|
|
std::shared_ptr<VideoStreamInfo> trick_play_video_stream_info(
|
|
|
|
new VideoStreamInfo(video_stream_info));
|
2017-05-15 16:22:06 +00:00
|
|
|
trick_play_video_stream_info->set_trick_play_factor(trick_play_factor);
|
2017-03-21 23:14:46 +00:00
|
|
|
DCHECK_GT(playback_rates_[trick_play_index], 0u);
|
|
|
|
trick_play_video_stream_info->set_playback_rate(
|
|
|
|
playback_rates_[trick_play_index]);
|
2017-02-27 17:22:25 +00:00
|
|
|
status =
|
|
|
|
DispatchStreamInfo(output_stream_index, trick_play_video_stream_info);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case StreamDataType::kMediaSample: {
|
|
|
|
if (stream_data->media_sample->is_key_frame()) {
|
|
|
|
std::shared_ptr<MediaSample> trick_play_media_sample =
|
|
|
|
MediaSample::CopyFrom(*(stream_data->media_sample));
|
|
|
|
trick_play_media_sample->set_duration(prev_sample_end_timestamp_ -
|
|
|
|
stream_data->media_sample->dts());
|
|
|
|
status =
|
|
|
|
DispatchMediaSample(output_stream_index, trick_play_media_sample);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
std::unique_ptr<StreamData> new_stream_data(new StreamData(*stream_data));
|
|
|
|
new_stream_data->stream_index = output_stream_index;
|
|
|
|
status = Dispatch(std::move(new_stream_data));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|