DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
trick_play_handler.h
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 #ifndef PACKAGER_MEDIA_BASE_TRICK_PLAY_HANDLER_H_
8 #define PACKAGER_MEDIA_BASE_TRICK_PLAY_HANDLER_H_
9 
10 #include "packager/media/base/media_handler.h"
11 
12 namespace shaka {
13 namespace media {
14 
17 // The stream data in trick play stream is not a simple duplicate. Some
18 // information need to be updated, including trick_play_rate in
19 // VideoStreamInfo, the duration in MediaSample (which makes sure there is no
20 // gap between the media sample dts). Since the duration information can be
21 // determined after getting the next media sample, a queue is used to cache the
22 // input stream data before the next key frame.
24  public:
26  ~TrickPlayHandler() override;
27 
28  void SetHandlerForMainStream(std::shared_ptr<MediaHandler> handler);
29  void SetHandlerForTrickPlay(uint32_t trick_play_rate,
30  std::shared_ptr<MediaHandler> handler);
31 
32  protected:
35  Status InitializeInternal() override;
36  Status Process(std::unique_ptr<StreamData> stream_data) override;
37  bool ValidateOutputStreamIndex(size_t stream_index) const override;
38  Status OnFlushRequest(size_t input_stream_index) override;
40 
41  private:
42  friend class TrickPlayHandlerTest;
43 
44  // Returns true if the trick play handler has main stream output handler
45  // connected, otherwise returns false.
46  bool HasMainStream();
47 
48  // Process the cached stream data for one trick play stream.
49  // The cached data is dispatched to the |output_stream_index|.
50  Status ProcessCachedStreamData(
51  size_t output_stream_index,
52  std::deque<std::shared_ptr<StreamData>>* cached_stream_data);
53 
54  // Process a single stream data. Depending on the stream data type, some
55  // information needs to be updated.
56  // Decoding timestamp for current key media sample. It is used for calculating
57  // the duration of previous key media sample, to make sure there is no gap
58  // between two key media samples.
59  Status ProcessOneStreamData(size_t output_stream_index,
60  const std::shared_ptr<StreamData>& stream_data);
61 
62  // Trick play rates. Note that there can be multiple trick play rates,
63  // e.g., 2, 4 and 8. That means, one input video stream will generate 3
64  // output trick play streams and original stream. Three trick play streams
65  // are:
66  // [key_frame_0, key_frame_2, key_frame_4, ...]
67  // [key_frame_0, key_frame_4, key_frame_8,...]
68  // [key_frame_0, key_frame_8, key_frame_16, ...].
69  std::vector<uint32_t> trick_play_rates_;
70 
71  TrickPlayHandler(const TrickPlayHandler&) = delete;
72  TrickPlayHandler& operator=(const TrickPlayHandler&) = delete;
73 
75  uint32_t total_key_frames_ = 0;
76 
77  // Num of frames received.
78  uint32_t total_frames_ = 0;
79 
80  // End timestamp of the previous processed media_sample, which is |dts| +
81  // |duration|. The duration of key frame in trick play stream is updated based
82  // on this timestamp.
83  int64_t prev_sample_end_timestamp_ = 0;
84 
85  // Record playback_rate for each trick play stream.
86  std::vector<uint32_t> playback_rates_;
87 
88  // The data in output streams should be in the same order as in the input
89  // stream. Cache the stream data before next key frame so that we can
90  // determine the duration for the current key frame. Since one key frame may
91  // be dispatched to different trick play stream, each trick play stream need
92  // its own queue to handle the synchronization.
93  // TODO(hmchen): Use one queue and multiple iterators, instead of multiple
94  // queues.
95  std::vector<std::deque<std::shared_ptr<StreamData>>> cached_stream_data_;
96 };
97 
98 } // namespace media
99 } // namespace shaka
100 
101 #endif // PACKAGER_MEDIA_BASE_TRICK_PLAY_HANDLER_H_
bool ValidateOutputStreamIndex(size_t stream_index) const override
Validate if the stream at the specified index actually exists.
Status OnFlushRequest(size_t input_stream_index) override
Event handler for flush request at the specific input stream index.
Status Process(std::unique_ptr< StreamData > stream_data) override