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 #include "packager/media/public/chunking_params.h"
14 
15 namespace shaka {
16 namespace media {
17 
44 class ChunkingHandler : public MediaHandler {
45  public:
46  explicit ChunkingHandler(const ChunkingParams& chunking_params);
47  ~ChunkingHandler() override;
48 
49  protected:
52  Status InitializeInternal() override;
53  Status Process(std::unique_ptr<StreamData> stream_data) override;
54  Status OnFlushRequest(size_t input_stream_index) override;
56 
57  private:
58  friend class ChunkingHandlerTest;
59 
60  ChunkingHandler(const ChunkingHandler&) = delete;
61  ChunkingHandler& operator=(const ChunkingHandler&) = delete;
62 
63  // Processes media sample and apply chunking if needed.
64  Status ProcessMediaSample(const MediaSample* sample);
65 
66  // Dispatch cached non main stream samples before |timestamp_threshold|.
67  Status DispatchNonMainSamples(int64_t timestamp_threshold);
68 
69  // The (sub)segments are aligned and dispatched together.
70  Status DispatchSegmentInfoForAllStreams();
71  Status DispatchSubsegmentInfoForAllStreams();
72 
73  const ChunkingParams chunking_params_;
74 
75  // The inputs are expected to come from the same thread.
76  std::atomic<int64_t> thread_id_;
77 
78  // The video stream is the main stream; if there is only one stream, it is the
79  // main stream. The chunking is based on the main stream.
80  const size_t kInvalidStreamIndex = static_cast<size_t>(-1);
81  size_t main_stream_index_ = kInvalidStreamIndex;
82  // Segment and subsegment duration in main stream's time scale.
83  int64_t segment_duration_ = 0;
84  int64_t subsegment_duration_ = 0;
85 
86  // The streams are expected to be synchronized. Cache non main (video) stream
87  // samples so we can determine whether the next segment should include these
88  // samples. The samples will be dispatched after seeing the next main stream
89  // sample.
90  std::deque<std::unique_ptr<StreamData>> non_main_samples_;
91 
92  // Current segment index, useful to determine where to do chunking.
93  int64_t current_segment_index_ = -1;
94  // Current subsegment index, useful to determine where to do chunking.
95  int64_t current_subsegment_index_ = -1;
96 
97  std::vector<std::shared_ptr<SegmentInfo>> segment_info_;
98  std::vector<std::shared_ptr<SegmentInfo>> subsegment_info_;
99  std::vector<uint32_t> time_scales_;
100  // The end timestamp of the last dispatched sample.
101  std::vector<int64_t> last_sample_end_timestamps_;
102 };
103 
104 } // namespace media
105 } // namespace shaka
106 
107 #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.
Chunking (segmentation) related parameters.
Class to hold a media sample.
Definition: media_sample.h:22