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(size_t 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  const size_t kInvalidStreamIndex = static_cast<size_t>(-1);
98  size_t main_stream_index_ = kInvalidStreamIndex;
99  // Segment and subsegment duration in main stream's time scale.
100  int64_t segment_duration_ = 0;
101  int64_t subsegment_duration_ = 0;
102 
103  // The streams are expected to be synchronized. Cache non main (video) stream
104  // samples so we can determine whether the next segment should include these
105  // samples. The samples will be dispatched after seeing the next main stream
106  // sample.
107  std::deque<std::unique_ptr<StreamData>> non_main_samples_;
108 
109  // Current segment index, useful to determine where to do chunking.
110  int64_t current_segment_index_ = -1;
111  // Current subsegment index, useful to determine where to do chunking.
112  int64_t current_subsegment_index_ = -1;
113 
114  std::vector<std::shared_ptr<SegmentInfo>> segment_info_;
115  std::vector<std::shared_ptr<SegmentInfo>> subsegment_info_;
116  std::vector<uint32_t> time_scales_;
117  // The end timestamp of the last dispatched sample.
118  std::vector<int64_t> last_sample_end_timestamps_;
119 };
120 
121 } // namespace media
122 } // namespace shaka
123 
124 #endif // PACKAGER_MEDIA_CHUNKING_CHUNKING_HANDLER_
Status Process(std::unique_ptr< StreamData > stream_data) override
Status InitializeInternal() override
Status OnFlushRequest(size_t input_stream_index) override
Event handler for flush request at the specific input stream index.
Class to hold a media sample.
Definition: media_sample.h:22
double segment_duration_in_seconds
Segment duration in seconds.