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