Shaka Packager SDK
chunking_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_CHUNKING_CHUNKING_HANDLER_
8 #define PACKAGER_MEDIA_CHUNKING_CHUNKING_HANDLER_
9 
10 #include <atomic>
11 #include <queue>
12 
13 #include "packager/base/logging.h"
14 #include "packager/media/base/media_handler.h"
15 #include "packager/media/public/chunking_params.h"
16 
17 namespace shaka {
18 namespace media {
19 
46 class ChunkingHandler : public MediaHandler {
47  public:
48  explicit ChunkingHandler(const ChunkingParams& chunking_params);
49  ~ChunkingHandler() override;
50 
51  protected:
54  Status InitializeInternal() override;
55  Status Process(std::unique_ptr<StreamData> stream_data) override;
56  Status OnFlushRequest(size_t input_stream_index) override;
58 
59  private:
60  friend class ChunkingHandlerTest;
61 
62  ChunkingHandler(const ChunkingHandler&) = delete;
63  ChunkingHandler& operator=(const ChunkingHandler&) = delete;
64 
65  // Processes main media sample and apply chunking if needed.
66  Status ProcessMainMediaSample(const MediaSample* sample);
67 
68  // Processes and dispatches media sample.
69  Status ProcessMediaSampleStreamData(const StreamData& media_data);
70 
71  // The (sub)segments are aligned and dispatched together.
72  Status DispatchSegmentInfoForAllStreams();
73  Status DispatchSubsegmentInfoForAllStreams();
74  Status DispatchCueEventForAllStreams(std::shared_ptr<CueEvent> cue_event);
75 
76  const ChunkingParams chunking_params_;
77 
78  // The inputs are expected to come from the same thread.
79  std::atomic<int64_t> thread_id_;
80 
81  // The video stream is the main stream; if there is only one stream, it is the
82  // main stream. The chunking is based on the main stream.
83  const size_t kInvalidStreamIndex = static_cast<size_t>(-1);
84  size_t main_stream_index_ = kInvalidStreamIndex;
85  // Segment and subsegment duration in main stream's time scale.
86  int64_t segment_duration_ = 0;
87  int64_t subsegment_duration_ = 0;
88 
89  class MediaSampleTimestampGreater {
90  public:
91  explicit MediaSampleTimestampGreater(
92  const ChunkingHandler* const chunking_handler);
93 
94  // Comparison operator. Used by |media_samples_| priority queue below to
95  // sort the media samples.
96  bool operator()(const std::unique_ptr<StreamData>& lhs,
97  const std::unique_ptr<StreamData>& rhs) const;
98 
99  private:
100  double GetSampleTimeInSeconds(
101  const StreamData& media_sample_stream_data) const;
102 
103  const ChunkingHandler* const chunking_handler_ = nullptr;
104  };
105  MediaSampleTimestampGreater media_sample_comparator_;
106  // Caches media samples and sort the samples.
107  std::priority_queue<std::unique_ptr<StreamData>,
108  std::vector<std::unique_ptr<StreamData>>,
109  MediaSampleTimestampGreater>
110  cached_media_sample_stream_data_;
111  // Tracks number of cached samples in input streams.
112  std::vector<size_t> num_cached_samples_;
113 
114  // Current segment index, useful to determine where to do chunking.
115  int64_t current_segment_index_ = -1;
116  // Current subsegment index, useful to determine where to do chunking.
117  int64_t current_subsegment_index_ = -1;
118 
119  std::vector<std::shared_ptr<SegmentInfo>> segment_info_;
120  std::vector<std::shared_ptr<SegmentInfo>> subsegment_info_;
121  std::vector<uint32_t> time_scales_;
122  // The end timestamp of the last dispatched sample.
123  std::vector<int64_t> last_sample_end_timestamps_;
124 
125  struct Scte35EventTimestampGreater {
126  bool operator()(const std::unique_ptr<StreamData>& lhs,
127  const std::unique_ptr<StreamData>& rhs) const;
128  };
129  // Captures all incoming SCTE35 events to identify chunking points. Events
130  // will be removed from this queue one at a time as soon as the correct
131  // chunking point is identified in the incoming samples.
132  std::priority_queue<std::unique_ptr<StreamData>,
133  std::vector<std::unique_ptr<StreamData>>,
134  Scte35EventTimestampGreater>
135  scte35_events_;
136 };
137 
138 } // namespace media
139 } // namespace shaka
140 
141 #endif // PACKAGER_MEDIA_CHUNKING_CHUNKING_HANDLER_
Status Process(std::unique_ptr< StreamData > stream_data) override
Status InitializeInternal() override
All the methods that are virtual are virtual for mocking.
Status OnFlushRequest(size_t input_stream_index) override
Event handler for flush request at the specific input stream index.
Chunking (segmentation) related parameters.
Class to hold a media sample.
Definition: media_sample.h:22