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/base/optional.h"
15 #include "packager/media/base/media_handler.h"
16 #include "packager/media/public/chunking_params.h"
17 
18 namespace shaka {
19 namespace media {
20 
39 class ChunkingHandler : public MediaHandler {
40  public:
41  explicit ChunkingHandler(const ChunkingParams& chunking_params);
42  ~ChunkingHandler() override = default;
43 
44  protected:
47  Status InitializeInternal() override;
48  Status Process(std::unique_ptr<StreamData> stream_data) override;
49  Status OnFlushRequest(size_t input_stream_index) override;
51 
52  private:
53  friend class ChunkingHandlerTest;
54 
55  ChunkingHandler(const ChunkingHandler&) = delete;
56  ChunkingHandler& operator=(const ChunkingHandler&) = delete;
57 
58  Status OnStreamInfo(std::shared_ptr<const StreamInfo> info);
59  Status OnCueEvent(std::shared_ptr<const CueEvent> event);
60  Status OnMediaSample(std::shared_ptr<const MediaSample> sample);
61 
62  Status EndSegmentIfStarted() const;
63  Status EndSubsegmentIfStarted() const;
64 
65  bool IsSubsegmentEnabled() {
66  return subsegment_duration_ > 0 &&
67  subsegment_duration_ != segment_duration_;
68  }
69 
70  const ChunkingParams chunking_params_;
71 
72  // Segment and subsegment duration in stream's time scale.
73  int64_t segment_duration_ = 0;
74  int64_t subsegment_duration_ = 0;
75 
76  // Current segment index, useful to determine where to do chunking.
77  int64_t current_segment_index_ = -1;
78  // Current subsegment index, useful to determine where to do chunking.
79  int64_t current_subsegment_index_ = -1;
80 
81  base::Optional<int64_t> segment_start_time_;
82  base::Optional<int64_t> subsegment_start_time_;
83  int64_t max_segment_time_ = 0;
84  uint32_t time_scale_ = 0;
85 
86  // The offset is applied to sample timestamps so a full segment is generated
87  // after cue points.
88  int64_t cue_offset_ = 0;
89 };
90 
91 } // namespace media
92 } // namespace shaka
93 
94 #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.