2016-03-21 19:16:58 +00:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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>
|
|
|
|
|
2016-04-27 22:04:50 +00:00
|
|
|
#include "packager/media/base/aes_encryptor.h"
|
|
|
|
#include "packager/media/base/key_source.h"
|
2016-03-21 19:16:58 +00:00
|
|
|
#include "packager/media/base/muxer_util.h"
|
|
|
|
#include "packager/media/base/status.h"
|
2016-04-27 22:04:50 +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"
|
2016-04-27 22:04:50 +00:00
|
|
|
#include "packager/media/event/progress_listener.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;
|
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),
|
2016-03-21 19:16:58 +00:00
|
|
|
ts_writer_(new TsWriter()),
|
|
|
|
pes_packet_generator_(new PesPacketGenerator()) {}
|
|
|
|
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.");
|
2016-06-29 21:24:40 +00:00
|
|
|
if (!ts_writer_->Initialize(stream_info))
|
2016-03-21 19:16:58 +00:00
|
|
|
return Status(error::MUXER_FAILURE, "Failed to initialize TsWriter.");
|
|
|
|
if (!pes_packet_generator_->Initialize(stream_info)) {
|
|
|
|
return Status(error::MUXER_FAILURE,
|
|
|
|
"Failed to initialize PesPacketGenerator.");
|
|
|
|
}
|
|
|
|
|
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-01-24 00:55:02 +00:00
|
|
|
Status TsSegmenter::AddSample(std::shared_ptr<MediaSample> sample) {
|
2017-03-11 02:49:55 +00:00
|
|
|
if (sample->is_encrypted())
|
|
|
|
ts_writer_->SignalEncrypted();
|
|
|
|
|
2016-04-06 05:00:05 +00:00
|
|
|
if (!ts_writer_file_opened_ && !sample->is_key_frame())
|
|
|
|
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.");
|
|
|
|
}
|
2016-04-06 05:00:05 +00:00
|
|
|
return WritePesPacketsToFile();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void TsSegmenter::SetTsWriterFileOpenedForTesting(bool value) {
|
|
|
|
ts_writer_file_opened_ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status TsSegmenter::OpenNewSegmentIfClosed(uint32_t next_pts) {
|
|
|
|
if (ts_writer_file_opened_)
|
|
|
|
return Status::OK;
|
|
|
|
const std::string segment_name =
|
|
|
|
GetSegmentName(muxer_options_.segment_template, next_pts,
|
|
|
|
segment_number_++, muxer_options_.bandwidth);
|
|
|
|
if (!ts_writer_->NewSegment(segment_name))
|
|
|
|
return Status(error::MUXER_FAILURE, "Failed to initilize TsPacketWriter.");
|
2016-04-16 22:58:47 +00:00
|
|
|
current_segment_path_ = segment_name;
|
2016-03-21 19:16:58 +00:00
|
|
|
ts_writer_file_opened_ = true;
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2016-04-06 05:00:05 +00:00
|
|
|
Status TsSegmenter::WritePesPacketsToFile() {
|
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();
|
|
|
|
|
|
|
|
Status status = OpenNewSegmentIfClosed(pes_packet->pts());
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
if (!ts_writer_->AddPesPacket(std::move(pes_packet)))
|
2016-03-21 19:16:58 +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
|
|
|
|
2017-02-24 01:17:47 +00:00
|
|
|
Status TsSegmenter::FinalizeSegment(uint64_t start_timestamp,
|
|
|
|
uint64_t duration) {
|
2016-04-06 05:00:05 +00:00
|
|
|
if (!pes_packet_generator_->Flush()) {
|
|
|
|
return Status(error::MUXER_FAILURE,
|
|
|
|
"Failed to flush PesPacketGenerator.");
|
|
|
|
}
|
|
|
|
Status status = WritePesPacketsToFile();
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
2016-03-21 19:16:58 +00:00
|
|
|
|
2016-04-06 05:00:05 +00:00
|
|
|
// This method may be called from Finalize() so ts_writer_file_opened_ could
|
|
|
|
// be false.
|
|
|
|
if (ts_writer_file_opened_) {
|
|
|
|
if (!ts_writer_->FinalizeSegment()) {
|
|
|
|
return Status(error::MUXER_FAILURE, "Failed to finalize TsWriter.");
|
|
|
|
}
|
2016-04-16 22:58:47 +00:00
|
|
|
if (listener_) {
|
|
|
|
const int64_t file_size =
|
|
|
|
File::GetFileSize(current_segment_path_.c_str());
|
2017-02-24 01:17:47 +00:00
|
|
|
listener_->OnNewSegment(current_segment_path_,
|
|
|
|
start_timestamp * timescale_scale_,
|
|
|
|
duration * timescale_scale_, file_size);
|
2016-04-16 22:58:47 +00:00
|
|
|
}
|
2016-04-06 05:00:05 +00:00
|
|
|
ts_writer_file_opened_ = false;
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
2016-04-16 22:58:47 +00:00
|
|
|
current_segment_path_.clear();
|
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
|