Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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_factor 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_factor,
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(
60  size_t output_stream_index,
61  const StreamData& stream_data);
62 
63  // Trick play factors. Note that there can be multiple trick play factors,
64  // e.g., 2, 4 and 8. That means, one input video stream will generate 3
65  // output trick play streams and original stream. Three trick play streams
66  // are:
67  // [key_frame_0, key_frame_2, key_frame_4, ...]
68  // [key_frame_0, key_frame_4, key_frame_8,...]
69  // [key_frame_0, key_frame_8, key_frame_16, ...].
70  std::vector<uint32_t> trick_play_factors_;
71 
72  TrickPlayHandler(const TrickPlayHandler&) = delete;
73  TrickPlayHandler& operator=(const TrickPlayHandler&) = delete;
74 
76  uint32_t total_key_frames_ = 0;
77 
78  // Num of frames received.
79  uint32_t total_frames_ = 0;
80 
81  // End timestamp of the previous processed media_sample, which is |dts| +
82  // |duration|. The duration of key frame in trick play stream is updated based
83  // on this timestamp.
84  int64_t prev_sample_end_timestamp_ = 0;
85 
86  // Record playback_rate for each trick play stream.
87  std::vector<uint32_t> playback_rates_;
88 
89  // The data in output streams should be in the same order as in the input
90  // stream. Cache the stream data before next key frame so that we can
91  // determine the duration for the current key frame. Since one key frame may
92  // be dispatched to different trick play stream, each trick play stream need
93  // its own queue to handle the synchronization.
94  // TODO(hmchen): Use one queue and multiple iterators, instead of multiple
95  // queues.
96  std::vector<std::deque<std::shared_ptr<StreamData>>> cached_stream_data_;
97 };
98 
99 } // namespace media
100 } // namespace shaka
101 
102 #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