Shaka Packager SDK
Loading...
Searching...
No Matches
chunking_handler.h
1// Copyright 2017 Google LLC. 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 <optional>
12#include <queue>
13
14#include <absl/log/log.h>
15
16#include <packager/chunking_params.h>
17#include <packager/media/base/media_handler.h>
18#include <packager/media/base/timestamp_util.h>
19
20namespace shaka {
21namespace media {
22
42 public:
43 explicit ChunkingHandler(const ChunkingParams& chunking_params);
44 ~ChunkingHandler() override = default;
45
46 protected:
49 Status InitializeInternal() override;
50 Status Process(std::unique_ptr<StreamData> stream_data) override;
51 Status OnFlushRequest(size_t input_stream_index) override;
53
54 private:
55 friend class ChunkingHandlerTest;
56
57 ChunkingHandler(const ChunkingHandler&) = delete;
58 ChunkingHandler& operator=(const ChunkingHandler&) = delete;
59
60 Status OnStreamInfo(std::shared_ptr<const StreamInfo> info);
61 Status OnCueEvent(std::shared_ptr<const CueEvent> event);
62 Status OnMediaSample(std::shared_ptr<const MediaSample> sample);
63
64 Status EndSegmentIfStarted();
65 Status EndSubsegmentIfStarted() const;
66
67 bool IsSubsegmentEnabled() {
68 return subsegment_duration_ > 0 &&
69 subsegment_duration_ != segment_duration_;
70 }
71
72 const ChunkingParams chunking_params_;
73
74 // Segment and subsegment duration in stream's time scale.
75 int64_t segment_duration_ = 0;
76 int64_t subsegment_duration_ = 0;
77
78 // Segment number that keeps monotically increasing.
79 // Set to start_segment_number in constructor.
80 int64_t segment_number_ = 1;
81
82 // Current segment index, useful to determine where to do chunking.
83 int64_t current_segment_index_ = -1;
84
85 // Current subsegment index, useful to determine where to do chunking.
86 int64_t current_subsegment_index_ = -1;
87
88 std::optional<int64_t> segment_start_time_;
89 std::optional<int64_t> subsegment_start_time_;
90 int64_t max_segment_time_ = 0;
91 int32_t time_scale_ = 0;
92
93 // The offset is applied to sample timestamps so a full segment is generated
94 // after cue points.
95 int64_t cue_offset_ = 0;
96
97 // Unwraps 33-bit PTS/DTS timestamps to 64-bit monotonically increasing
98 // values, handling wrap-around at 2^33. This ensures SegmentInfo timestamps
99 // are always increasing even when input timestamps wrap around.
100 PtsUnwrapper pts_unwrapper_;
101};
102
103} // namespace media
104} // namespace shaka
105
106#endif // PACKAGER_MEDIA_CHUNKING_CHUNKING_HANDLER_
Status Process(std::unique_ptr< StreamData > stream_data) override
Status OnFlushRequest(size_t input_stream_index) override
Event handler for flush request at the specific input stream index.
All the methods that are virtual are virtual for mocking.