DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
trick_play_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/trick_play/trick_play_handler.h"
8 
9 #include "packager/base/logging.h"
10 #include "packager/media/base/video_stream_info.h"
11 
12 namespace shaka {
13 namespace media {
14 
15 TrickPlayHandler::TrickPlayHandler(const TrickPlayOptions& trick_play_option)
16  : trick_play_options_(trick_play_option),
17  cached_stream_data_(trick_play_option.trick_play_rates.size()) {
18  for (auto rate : trick_play_option.trick_play_rates) {
19  CHECK_GT(rate, 0);
20  }
21 }
22 
23 TrickPlayHandler::~TrickPlayHandler() {}
24 
26  return Status::OK;
27 }
28 
30  std::unique_ptr<StreamData> input_stream_data) {
31  // The non-trick play stream is dispatched at index 0.
32  // The trick-play streams are dispatched to index 1, index 2 and so on.
33  DCHECK_EQ(input_stream_data->stream_index, 0u);
34  std::unique_ptr<StreamData> output_stream_data(new StreamData());
35  *output_stream_data = *input_stream_data;
36  Status status = Dispatch(std::move(output_stream_data));
37  if (!status.ok()) {
38  return status;
39  }
40 
41  std::shared_ptr<StreamData> stream_data(std::move(input_stream_data));
42  if (stream_data->stream_data_type == StreamDataType::kStreamInfo) {
43  if (stream_data->stream_info->stream_type() != kStreamVideo) {
44  status.SetError(error::TRICK_PLAY_ERROR,
45  "Trick play does not support non-video stream");
46  return status;
47  }
48  const VideoStreamInfo& video_stream_info =
49  static_cast<const VideoStreamInfo&>(*stream_data->stream_info);
50  if (video_stream_info.trick_play_rate() > 0) {
51  status.SetError(error::TRICK_PLAY_ERROR,
52  "This stream is alreay a trick play stream.");
53  return status;
54  }
55  }
56 
57  if (stream_data->stream_data_type != StreamDataType::kMediaSample) {
58  // Non media sample stream data needs to be dispatched to every output
59  // stream. It is just cached in every queue until a new key frame comes or
60  // the stream is flushed.
61  for (size_t i = 0; i < cached_stream_data_.size(); ++i)
62  cached_stream_data_[i].push_back(stream_data);
63  return Status::OK;
64  }
65 
66  if (stream_data->media_sample->is_key_frame()) {
67  // For a new key frame, some of the trick play streams may include it.
68  // The cached data in those trick play streams will be processed.
69  DCHECK_EQ(trick_play_options_.trick_play_rates.size(),
70  cached_stream_data_.size());
71  for (size_t i = 0; i < cached_stream_data_.size(); ++i) {
72  int16_t rate = trick_play_options_.trick_play_rates[i];
73  if (total_key_frames_ % rate == 0) {
74  if (!cached_stream_data_[i].empty()) {
75  Status status =
76  ProcessCachedStreamData(i + 1, &cached_stream_data_[i]);
77  if (!status.ok())
78  return status;
79  }
80  cached_stream_data_[i].push_back(stream_data);
81  }
82  }
83 
84  total_key_frames_++;
85  }
86 
87  prev_sample_end_timestamp_ =
88  stream_data->media_sample->dts() + stream_data->media_sample->duration();
89  return Status::OK;
90 }
91 
92 bool TrickPlayHandler::ValidateOutputStreamIndex(size_t stream_index) const {
93  // Output stream index should be less than the number of trick play
94  // streams + one original stream.
95  return stream_index <= trick_play_options_.trick_play_rates.size();
96 };
97 
98 Status TrickPlayHandler::OnFlushRequest(size_t input_stream_index) {
99  DCHECK_EQ(input_stream_index, 0u)
100  << "Trick Play Handler should only have single input.";
101  for (size_t i = 0; i < cached_stream_data_.size(); ++i) {
102  ProcessCachedStreamData(i + 1, &cached_stream_data_[i]);
103  }
104  return MediaHandler::FlushDownstream(input_stream_index);
105 }
106 
107 Status TrickPlayHandler::ProcessCachedStreamData(
108  size_t output_stream_index,
109  std::deque<std::shared_ptr<StreamData>>* cached_stream_data) {
110  while (!cached_stream_data->empty()) {
111  Status status =
112  ProcessOneStreamData(output_stream_index, cached_stream_data->front());
113  if (!status.ok()) {
114  return status;
115  }
116  cached_stream_data->pop_front();
117  }
118  return Status::OK;
119 }
120 
121 Status TrickPlayHandler::ProcessOneStreamData(
122  size_t output_stream_index,
123  const std::shared_ptr<StreamData>& stream_data) {
124  uint32_t trick_play_rate =
125  trick_play_options_.trick_play_rates[output_stream_index - 1];
126  Status status;
127  switch (stream_data->stream_data_type) {
128  // trick_play_rate in StreamInfo should be modified.
129  case StreamDataType::kStreamInfo: {
130  const VideoStreamInfo& video_stream_info =
131  static_cast<const VideoStreamInfo&>(*stream_data->stream_info);
132  std::shared_ptr<VideoStreamInfo> trick_play_video_stream_info(
133  new VideoStreamInfo(video_stream_info));
134  trick_play_video_stream_info->set_trick_play_rate(trick_play_rate);
135  status =
136  DispatchStreamInfo(output_stream_index, trick_play_video_stream_info);
137  break;
138  }
139  case StreamDataType::kMediaSample: {
140  if (stream_data->media_sample->is_key_frame()) {
141  std::shared_ptr<MediaSample> trick_play_media_sample =
142  MediaSample::CopyFrom(*(stream_data->media_sample));
143  trick_play_media_sample->set_duration(prev_sample_end_timestamp_ -
144  stream_data->media_sample->dts());
145 
146  status =
147  DispatchMediaSample(output_stream_index, trick_play_media_sample);
148  }
149  break;
150  }
151  default:
152  std::unique_ptr<StreamData> new_stream_data(new StreamData(*stream_data));
153  new_stream_data->stream_index = output_stream_index;
154  status = Dispatch(std::move(new_stream_data));
155  break;
156  }
157  return status;
158 }
159 
160 } // namespace media
161 } // namespace shaka
void SetError(error::Code error_code, const std::string &error_message)
Definition: status.h:113
bool ValidateOutputStreamIndex(size_t stream_index) const override
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 OnFlushRequest(size_t input_stream_index) override
Event handler for flush request at the specific input stream index.
static std::shared_ptr< MediaSample > CopyFrom(const uint8_t *data, size_t size, bool is_key_frame)
Definition: media_sample.cc:45
Holds video stream information.
Status Process(std::unique_ptr< StreamData > stream_data) override