2022-08-26 15:44:59 +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
|
|
|
|
|
|
|
|
#include "packager/media/formats/mp2t/ts_segmenter.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2017-10-23 22:59:30 +00:00
|
|
|
#include "packager/media/base/audio_stream_info.h"
|
2016-03-21 19:16:58 +00:00
|
|
|
#include "packager/media/base/muxer_util.h"
|
2017-10-23 22:59:30 +00:00
|
|
|
#include "packager/media/base/video_stream_info.h"
|
2016-04-16 22:58:47 +00:00
|
|
|
#include "packager/media/event/muxer_listener.h"
|
2017-10-23 22:59:30 +00:00
|
|
|
#include "packager/media/formats/mp2t/pes_packet.h"
|
|
|
|
#include "packager/media/formats/mp2t/program_map_table_writer.h"
|
2023-08-31 23:59:46 +00:00
|
|
|
#include "packager/status/status.h"
|
|
|
|
#include "packager/status/status_macros.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 {
|
|
|
|
namespace mp2t {
|
|
|
|
|
|
|
|
namespace {
|
2016-04-19 01:00:06 +00:00
|
|
|
const double kTsTimescale = 90000;
|
2017-10-23 22:59:30 +00:00
|
|
|
|
|
|
|
bool IsAudioCodec(Codec codec) {
|
|
|
|
return codec >= kCodecAudio && codec < kCodecAudioMaxPlusOne;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsVideoCodec(Codec codec) {
|
|
|
|
return codec >= kCodecVideo && codec < kCodecVideoMaxPlusOne;
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
} // namespace
|
|
|
|
|
2016-04-16 22:58:47 +00:00
|
|
|
TsSegmenter::TsSegmenter(const MuxerOptions& options, MuxerListener* listener)
|
2016-03-21 19:16:58 +00:00
|
|
|
: muxer_options_(options),
|
2016-04-16 22:58:47 +00:00
|
|
|
listener_(listener),
|
2018-07-03 23:52:05 +00:00
|
|
|
transport_stream_timestamp_offset_(
|
|
|
|
options.transport_stream_timestamp_offset_ms * kTsTimescale / 1000),
|
|
|
|
pes_packet_generator_(
|
|
|
|
new PesPacketGenerator(transport_stream_timestamp_offset_)) {}
|
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
TsSegmenter::~TsSegmenter() {}
|
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
Status TsSegmenter::Initialize(const StreamInfo& stream_info) {
|
2016-03-21 19:16:58 +00:00
|
|
|
if (muxer_options_.segment_template.empty())
|
|
|
|
return Status(error::MUXER_FAILURE, "Segment template not specified.");
|
|
|
|
if (!pes_packet_generator_->Initialize(stream_info)) {
|
|
|
|
return Status(error::MUXER_FAILURE,
|
|
|
|
"Failed to initialize PesPacketGenerator.");
|
|
|
|
}
|
|
|
|
|
2017-10-23 22:59:30 +00:00
|
|
|
const StreamType stream_type = stream_info.stream_type();
|
|
|
|
if (stream_type != StreamType::kStreamVideo &&
|
|
|
|
stream_type != StreamType::kStreamAudio) {
|
|
|
|
LOG(ERROR) << "TsWriter cannot handle stream type " << stream_type
|
|
|
|
<< " yet.";
|
|
|
|
return Status(error::MUXER_FAILURE, "Unsupported stream type.");
|
|
|
|
}
|
|
|
|
|
|
|
|
codec_ = stream_info.codec();
|
|
|
|
if (stream_type == StreamType::kStreamAudio)
|
|
|
|
audio_codec_config_ = stream_info.codec_config();
|
|
|
|
|
2016-04-19 01:00:06 +00:00
|
|
|
timescale_scale_ = kTsTimescale / stream_info.time_scale();
|
2016-03-21 19:16:58 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status TsSegmenter::Finalize() {
|
2017-02-24 01:17:47 +00:00
|
|
|
return Status::OK;
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 17:24:24 +00:00
|
|
|
Status TsSegmenter::AddSample(const MediaSample& sample) {
|
2017-10-23 22:59:30 +00:00
|
|
|
if (!ts_writer_) {
|
|
|
|
std::unique_ptr<ProgramMapTableWriter> pmt_writer;
|
|
|
|
if (codec_ == kCodecAC3) {
|
|
|
|
// https://goo.gl/N7Tvqi MPEG-2 Stream Encryption Format for HTTP Live
|
|
|
|
// Streaming 2.3.2.2 AC-3 Setup: For AC-3, the setup_data in the
|
|
|
|
// audio_setup_information is the first 10 bytes of the audio data (the
|
|
|
|
// syncframe()).
|
|
|
|
// For unencrypted AC3, the setup_data is not used, so what is in there
|
|
|
|
// does not matter.
|
|
|
|
const size_t kSetupDataSize = 10u;
|
|
|
|
if (sample.data_size() < kSetupDataSize) {
|
|
|
|
LOG(ERROR) << "Sample is too small for AC3: " << sample.data_size();
|
|
|
|
return Status(error::MUXER_FAILURE, "Sample is too small for AC3.");
|
|
|
|
}
|
|
|
|
const std::vector<uint8_t> setup_data(sample.data(),
|
|
|
|
sample.data() + kSetupDataSize);
|
|
|
|
pmt_writer.reset(new AudioProgramMapTableWriter(codec_, setup_data));
|
|
|
|
} else if (IsAudioCodec(codec_)) {
|
|
|
|
pmt_writer.reset(
|
|
|
|
new AudioProgramMapTableWriter(codec_, audio_codec_config_));
|
|
|
|
} else {
|
|
|
|
DCHECK(IsVideoCodec(codec_));
|
|
|
|
pmt_writer.reset(new VideoProgramMapTableWriter(codec_));
|
|
|
|
}
|
|
|
|
ts_writer_.reset(new TsWriter(std::move(pmt_writer)));
|
|
|
|
}
|
|
|
|
|
2017-09-12 17:24:24 +00:00
|
|
|
if (sample.is_encrypted())
|
2017-03-11 02:49:55 +00:00
|
|
|
ts_writer_->SignalEncrypted();
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
if (!segment_started_ && !sample.is_key_frame())
|
2016-04-06 05:00:05 +00:00
|
|
|
LOG(WARNING) << "A segment will start with a non key frame.";
|
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
if (!pes_packet_generator_->PushSample(sample)) {
|
|
|
|
return Status(error::MUXER_FAILURE,
|
|
|
|
"Failed to add sample to PesPacketGenerator.");
|
|
|
|
}
|
2020-07-04 22:18:30 +00:00
|
|
|
return WritePesPackets();
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
void TsSegmenter::InjectTsWriterForTesting(std::unique_ptr<TsWriter> writer) {
|
|
|
|
ts_writer_ = std::move(writer);
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TsSegmenter::InjectPesPacketGeneratorForTesting(
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<PesPacketGenerator> generator) {
|
|
|
|
pes_packet_generator_ = std::move(generator);
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
void TsSegmenter::SetSegmentStartedForTesting(bool value) {
|
|
|
|
segment_started_ = value;
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
Status TsSegmenter::StartSegmentIfNeeded(int64_t next_pts) {
|
|
|
|
if (segment_started_)
|
2016-03-21 19:16:58 +00:00
|
|
|
return Status::OK;
|
2020-07-04 22:18:30 +00:00
|
|
|
segment_start_timestamp_ = next_pts;
|
|
|
|
if (!ts_writer_->NewSegment(&segment_buffer_))
|
|
|
|
return Status(error::MUXER_FAILURE, "Failed to initialize new segment.");
|
|
|
|
segment_started_ = true;
|
2016-03-21 19:16:58 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
Status TsSegmenter::WritePesPackets() {
|
2016-03-21 19:16:58 +00:00
|
|
|
while (pes_packet_generator_->NumberOfReadyPesPackets() > 0u) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<PesPacket> pes_packet =
|
2016-03-21 19:16:58 +00:00
|
|
|
pes_packet_generator_->GetNextPesPacket();
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
Status status = StartSegmentIfNeeded(pes_packet->pts());
|
2016-03-21 19:16:58 +00:00
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
|
2018-02-01 20:25:07 +00:00
|
|
|
if (listener_ && IsVideoCodec(codec_) && pes_packet->is_key_frame()) {
|
|
|
|
|
2021-02-02 18:51:50 +00:00
|
|
|
uint64_t start_pos = segment_buffer_.Size();
|
2018-02-01 20:25:07 +00:00
|
|
|
const int64_t timestamp = pes_packet->pts();
|
2020-07-04 22:18:30 +00:00
|
|
|
if (!ts_writer_->AddPesPacket(std::move(pes_packet), &segment_buffer_))
|
2018-02-01 20:25:07 +00:00
|
|
|
return Status(error::MUXER_FAILURE, "Failed to add PES packet.");
|
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
uint64_t end_pos = segment_buffer_.Size();
|
2021-02-02 18:51:50 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
listener_->OnKeyFrame(timestamp, start_pos, end_pos - start_pos);
|
2018-02-01 20:25:07 +00:00
|
|
|
} else {
|
2020-07-04 22:18:30 +00:00
|
|
|
if (!ts_writer_->AddPesPacket(std::move(pes_packet), &segment_buffer_))
|
2018-02-01 20:25:07 +00:00
|
|
|
return Status(error::MUXER_FAILURE, "Failed to add PES packet.");
|
|
|
|
}
|
2016-04-06 05:00:05 +00:00
|
|
|
}
|
|
|
|
return Status::OK;
|
|
|
|
}
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2021-08-04 18:56:44 +00:00
|
|
|
Status TsSegmenter::FinalizeSegment(int64_t start_timestamp, int64_t duration) {
|
2016-04-06 05:00:05 +00:00
|
|
|
if (!pes_packet_generator_->Flush()) {
|
2020-07-04 22:18:30 +00:00
|
|
|
return Status(error::MUXER_FAILURE, "Failed to flush PesPacketGenerator.");
|
2016-04-06 05:00:05 +00:00
|
|
|
}
|
2020-07-04 22:18:30 +00:00
|
|
|
Status status = WritePesPackets();
|
2016-04-06 05:00:05 +00:00
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
// This method may be called from Finalize() so segment_started_ could
|
2016-04-06 05:00:05 +00:00
|
|
|
// be false.
|
2020-07-04 22:18:30 +00:00
|
|
|
if (!segment_started_)
|
|
|
|
return Status::OK;
|
|
|
|
std::string segment_path =
|
|
|
|
GetSegmentName(muxer_options_.segment_template, segment_start_timestamp_,
|
|
|
|
segment_number_++, muxer_options_.bandwidth);
|
|
|
|
|
|
|
|
const int64_t file_size = segment_buffer_.Size();
|
2021-02-02 18:51:50 +00:00
|
|
|
std::unique_ptr<File, FileCloser> segment_file;
|
2020-07-04 22:18:30 +00:00
|
|
|
segment_file.reset(File::Open(segment_path.c_str(), "w"));
|
|
|
|
if (!segment_file) {
|
|
|
|
return Status(error::FILE_FAILURE,
|
2021-02-02 18:51:50 +00:00
|
|
|
"Cannot open file for write " + segment_path);
|
2020-07-04 22:18:30 +00:00
|
|
|
}
|
2021-02-02 18:51:50 +00:00
|
|
|
|
2020-07-04 22:18:30 +00:00
|
|
|
RETURN_IF_ERROR(segment_buffer_.WriteToFile(segment_file.get()));
|
|
|
|
|
|
|
|
if (!segment_file.release()->Close()) {
|
|
|
|
return Status(
|
|
|
|
error::FILE_FAILURE,
|
|
|
|
"Cannot close file " + segment_path +
|
2021-02-02 18:51:50 +00:00
|
|
|
", possibly file permission issue or running out of disk space.");
|
2020-07-04 22:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (listener_) {
|
|
|
|
listener_->OnNewSegment(segment_path,
|
|
|
|
start_timestamp * timescale_scale_ +
|
|
|
|
transport_stream_timestamp_offset_,
|
|
|
|
duration * timescale_scale_, file_size);
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
2020-07-04 22:18:30 +00:00
|
|
|
segment_started_ = false;
|
2021-02-02 18:51:50 +00:00
|
|
|
|
2016-03-21 19:16:58 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp2t
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|