DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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 
12 #include "packager/media/base/media_handler.h"
13 
14 namespace shaka {
15 namespace media {
16 
20 
24 
27  bool segment_sap_aligned = true;
28 
33 };
34 
61 class ChunkingHandler : public MediaHandler {
62  public:
63  explicit ChunkingHandler(const ChunkingOptions& chunking_options);
64  ~ChunkingHandler() override;
65 
66  protected:
69  Status InitializeInternal() override;
70  Status Process(std::unique_ptr<StreamData> stream_data) override;
71  Status OnFlushRequest(int input_stream_index) override;
73 
74  private:
75  friend class ChunkingHandlerTest;
76 
77  ChunkingHandler(const ChunkingHandler&) = delete;
78  ChunkingHandler& operator=(const ChunkingHandler&) = delete;
79 
80  // Processes media sample and apply chunking if needed.
81  Status ProcessMediaSample(const MediaSample* sample);
82 
83  // Dispatch cached non main stream samples before |timestamp_threshold|.
84  Status DispatchNonMainSamples(int64_t timestamp_threshold);
85 
86  // The (sub)segments are aligned and dispatched together.
87  Status DispatchSegmentInfoForAllStreams();
88  Status DispatchSubsegmentInfoForAllStreams();
89 
90  const ChunkingOptions chunking_options_;
91 
92  // The inputs are expected to come from the same thread.
93  std::atomic<int64_t> thread_id_;
94 
95  // The video stream is the main stream; if there is only one stream, it is the
96  // main stream. The chunking is based on the main stream.
97  int main_stream_index_ = -1;
98  // Segment and subsegment duration in main stream's time scale.
99  int64_t segment_duration_ = 0;
100  int64_t subsegment_duration_ = 0;
101 
102  // The streams are expected to be synchronized. Cache non main (video) stream
103  // samples so we can determine whether the next segment should include these
104  // samples. The samples will be dispatched after seeing the next main stream
105  // sample.
106  std::deque<std::unique_ptr<StreamData>> non_main_samples_;
107 
108  // Current segment index, useful to determine where to do chunking.
109  int64_t current_segment_index_ = -1;
110  // Current subsegment index, useful to determine where to do chunking.
111  int64_t current_subsegment_index_ = -1;
112 
113  std::vector<std::shared_ptr<SegmentInfo>> segment_info_;
114  std::vector<std::shared_ptr<SegmentInfo>> subsegment_info_;
115  std::vector<uint32_t> time_scales_;
116  // The end timestamp of the last dispatched sample.
117  std::vector<int64_t> last_sample_end_timestamps_;
118 };
119 
120 } // namespace media
121 } // namespace shaka
122 
123 #endif // PACKAGER_MEDIA_CHUNKING_CHUNKING_HANDLER_
Status Process(std::unique_ptr< StreamData > stream_data) override
Status InitializeInternal() override
Class to hold a media sample.
Definition: media_sample.h:22
Status OnFlushRequest(int input_stream_index) override
Event handler for flush request at the specific input stream index.
double segment_duration_in_seconds
Segment duration in seconds.