Shaka Packager SDK
Loading...
Searching...
No Matches
ts_segmenter.h
1// Copyright 2016 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_FORMATS_MP2T_TS_SEGMENTER_H_
8#define PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
9
10#include <cstdint>
11#include <memory>
12#include <vector>
13
14#include <packager/macros/classes.h>
15#include <packager/media/base/buffer_writer.h>
16#include <packager/media/base/media_sample.h>
17#include <packager/media/base/muxer_options.h>
18#include <packager/media/base/stream_info.h>
19#include <packager/media/formats/mp2t/pes_packet_generator.h>
20#include <packager/media/formats/mp2t/ts_writer.h>
21#include <packager/status.h>
22
23namespace shaka {
24namespace media {
25
26class KeySource;
27class MuxerListener;
28
29namespace mp2t {
30
32 public:
33 // TODO(rkuroiwa): Add progress listener?
38 TsSegmenter(const MuxerOptions& options, MuxerListener* listener);
40
44 Status Initialize(const StreamInfo& stream_info);
45
48 Status Finalize();
49
52 Status AddSample(const MediaSample& sample);
53
62 // TODO(kqyang): Remove the usage of segment start timestamp and duration in
63 // xx_segmenter, which could cause confusions on which is the source of truth
64 // as the segment start timestamp and duration could be tracked locally.
65 Status FinalizeSegment(int64_t start_timestamp, int64_t duration);
66
68 void InjectTsWriterForTesting(std::unique_ptr<TsWriter> writer);
69
72 std::unique_ptr<PesPacketGenerator> generator);
73
75 void SetSegmentStartedForTesting(bool value);
76
77 int64_t segment_start_timestamp() const { return segment_start_timestamp_; }
78 BufferWriter* segment_buffer() { return &segment_buffer_; }
79 void set_segment_started(bool value) { segment_started_ = value; }
80 bool segment_started() const { return segment_started_; }
81
82 double timescale() const { return timescale_scale_; }
83 uint32_t transport_stream_timestamp_offset() const {
84 return transport_stream_timestamp_offset_;
85 }
86
87 private:
88 Status StartSegmentIfNeeded(int64_t next_pts);
89
90 // Writes PES packets (carried in TsPackets) to a buffer.
91 Status WritePesPackets();
92
93 MuxerListener* const listener_;
94
95 // Codec for the stream.
96 Codec codec_ = kUnknownCodec;
97 std::vector<uint8_t> audio_codec_config_;
98
99 const int32_t transport_stream_timestamp_offset_ = 0;
100 // Scale used to scale the input stream to TS's timesccale (which is 90000).
101
102 // Used for calculating the duration in seconds fo the current segment.
103 double timescale_scale_ = 1.0;
104
105 std::unique_ptr<TsWriter> ts_writer_;
106
107 BufferWriter segment_buffer_;
108
109 // Set to true if segment_buffer_ is initialized, set to false after
110 // FinalizeSegment() succeeds in ts_muxer.
111 bool segment_started_ = false;
112 std::unique_ptr<PesPacketGenerator> pes_packet_generator_;
113
114 int64_t segment_start_timestamp_ = -1;
115 DISALLOW_COPY_AND_ASSIGN(TsSegmenter);
116};
117
118} // namespace mp2t
119} // namespace media
120} // namespace shaka
121#endif // PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
Class to hold a media sample.
Abstract class holds stream information.
Definition stream_info.h:73
Status FinalizeSegment(int64_t start_timestamp, int64_t duration)
void SetSegmentStartedForTesting(bool value)
Only for testing.
Status Initialize(const StreamInfo &stream_info)
void InjectPesPacketGeneratorForTesting(std::unique_ptr< PesPacketGenerator > generator)
Only for testing.
Status AddSample(const MediaSample &sample)
void InjectTsWriterForTesting(std::unique_ptr< TsWriter > writer)
Only for testing.
All the methods that are virtual are virtual for mocking.
This structure contains the list of configuration options for Muxer.