2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2016 Google LLC. All rights reserved.
|
2016-03-21 19:16:58 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
#ifndef PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
|
|
|
|
#define PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2023-12-01 17:32:19 +00:00
|
|
|
|
|
|
|
#include <packager/file.h>
|
|
|
|
#include <packager/macros/classes.h>
|
|
|
|
#include <packager/media/base/muxer_options.h>
|
|
|
|
#include <packager/media/formats/mp2t/pes_packet_generator.h>
|
|
|
|
#include <packager/media/formats/mp2t/ts_writer.h>
|
|
|
|
#include <packager/status.h>
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-03-21 19:16:58 +00:00
|
|
|
namespace media {
|
2016-04-16 22:58:47 +00:00
|
|
|
|
2016-04-27 22:04:50 +00:00
|
|
|
class KeySource;
|
2016-04-16 22:58:47 +00:00
|
|
|
class MuxerListener;
|
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
namespace mp2t {
|
|
|
|
|
|
|
|
class TsSegmenter {
|
|
|
|
public:
|
2016-04-27 22:04:50 +00:00
|
|
|
// TODO(rkuroiwa): Add progress listener?
|
2016-03-21 19:16:58 +00:00
|
|
|
/// @param options is the options for this muxer. This must stay valid
|
|
|
|
/// throughout the life time of the instance.
|
2016-04-16 22:58:47 +00:00
|
|
|
/// @param listener is the MuxerListener that should be used to notify events.
|
|
|
|
/// This may be null, in which case no events are sent.
|
|
|
|
TsSegmenter(const MuxerOptions& options, MuxerListener* listener);
|
2016-03-21 19:16:58 +00:00
|
|
|
~TsSegmenter();
|
|
|
|
|
|
|
|
/// Initialize the object.
|
|
|
|
/// @param stream_info is the stream info for the segmenter.
|
|
|
|
/// @return OK on success.
|
2017-03-11 02:49:55 +00:00
|
|
|
Status Initialize(const StreamInfo& stream_info);
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
/// Finalize the segmenter.
|
|
|
|
/// @return OK on success.
|
|
|
|
Status Finalize();
|
|
|
|
|
|
|
|
/// @param sample gets added to this object.
|
|
|
|
/// @return OK on success.
|
2017-09-12 17:24:24 +00:00
|
|
|
Status AddSample(const MediaSample& sample);
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2017-02-24 01:17:47 +00:00
|
|
|
/// Flush all the samples that are (possibly) buffered and write them to the
|
|
|
|
/// current segment, this will close the file. If a file is not already opened
|
|
|
|
/// before calling this, this will open one and write them to file.
|
|
|
|
/// @param start_timestamp is the segment's start timestamp in the input
|
|
|
|
/// stream's time scale.
|
|
|
|
/// @param duration is the segment's duration in the input stream's time
|
|
|
|
/// scale.
|
|
|
|
// TODO(kqyang): Remove the usage of segment start timestamp and duration in
|
|
|
|
// xx_segmenter, which could cause confusions on which is the source of truth
|
|
|
|
// as the segment start timestamp and duration could be tracked locally.
|
2021-08-04 18:56:44 +00:00
|
|
|
Status FinalizeSegment(int64_t start_timestamp, int64_t duration);
|
2017-02-24 01:17:47 +00:00
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
/// Only for testing.
|
2016-08-17 17:41:40 +00:00
|
|
|
void InjectTsWriterForTesting(std::unique_ptr<TsWriter> writer);
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
/// Only for testing.
|
|
|
|
void InjectPesPacketGeneratorForTesting(
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<PesPacketGenerator> generator);
|
2016-03-21 19:16:58 +00:00
|
|
|
|
|
|
|
/// Only for testing.
|
2020-07-04 22:18:30 +00:00
|
|
|
void SetSegmentStartedForTesting(bool value);
|
2021-02-02 18:51:50 +00:00
|
|
|
|
2024-02-23 23:28:11 +00:00
|
|
|
int64_t segment_start_timestamp() const { return segment_start_timestamp_; }
|
|
|
|
BufferWriter* segment_buffer() { return &segment_buffer_; }
|
|
|
|
void set_segment_started(bool value) { segment_started_ = value; }
|
|
|
|
bool segment_started() const { return segment_started_; }
|
|
|
|
|
|
|
|
double timescale() const { return timescale_scale_; }
|
|
|
|
uint32_t transport_stream_timestamp_offset() const {
|
|
|
|
return transport_stream_timestamp_offset_;
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
private:
|
2020-07-04 22:18:30 +00:00
|
|
|
Status StartSegmentIfNeeded(int64_t next_pts);
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
// Writes PES packets (carried in TsPackets) to a buffer.
|
|
|
|
Status WritePesPackets();
|
2016-04-06 05:00:05 +00:00
|
|
|
|
2016-04-16 22:58:47 +00:00
|
|
|
MuxerListener* const listener_;
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2017-10-23 22:59:30 +00:00
|
|
|
// Codec for the stream.
|
|
|
|
Codec codec_ = kUnknownCodec;
|
|
|
|
std::vector<uint8_t> audio_codec_config_;
|
|
|
|
|
2021-08-04 18:56:44 +00:00
|
|
|
const int32_t transport_stream_timestamp_offset_ = 0;
|
2016-04-19 01:00:06 +00:00
|
|
|
// Scale used to scale the input stream to TS's timesccale (which is 90000).
|
2024-02-23 23:28:11 +00:00
|
|
|
|
2016-04-19 01:00:06 +00:00
|
|
|
// Used for calculating the duration in seconds fo the current segment.
|
|
|
|
double timescale_scale_ = 1.0;
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<TsWriter> ts_writer_;
|
2021-02-02 18:51:50 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
BufferWriter segment_buffer_;
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
// Set to true if segment_buffer_ is initialized, set to false after
|
2024-02-23 23:28:11 +00:00
|
|
|
// FinalizeSegment() succeeds in ts_muxer.
|
2020-07-04 22:18:30 +00:00
|
|
|
bool segment_started_ = false;
|
|
|
|
std::unique_ptr<PesPacketGenerator> pes_packet_generator_;
|
2016-04-16 22:58:47 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
int64_t segment_start_timestamp_ = -1;
|
2016-03-21 19:16:58 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(TsSegmenter);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mp2t
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2016-03-21 19:16:58 +00:00
|
|
|
#endif // PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
|