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>
|
|
|
|
|
|
|
|
#include "packager/media/base/muxer_util.h"
|
|
|
|
#include "packager/media/base/status.h"
|
|
|
|
|
|
|
|
namespace edash_packager {
|
|
|
|
namespace media {
|
|
|
|
namespace mp2t {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
const int kTsTimescale = 90000;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TsSegmenter::TsSegmenter(const MuxerOptions& options)
|
|
|
|
: muxer_options_(options),
|
|
|
|
ts_writer_(new TsWriter()),
|
|
|
|
pes_packet_generator_(new PesPacketGenerator()) {}
|
|
|
|
TsSegmenter::~TsSegmenter() {}
|
|
|
|
|
|
|
|
Status TsSegmenter::Initialize(const StreamInfo& stream_info) {
|
|
|
|
if (muxer_options_.segment_template.empty())
|
|
|
|
return Status(error::MUXER_FAILURE, "Segment template not specified.");
|
|
|
|
if (!ts_writer_->Initialize(stream_info))
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status TsSegmenter::Finalize() {
|
2016-04-06 05:00:05 +00:00
|
|
|
return Flush();
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 05:00:05 +00:00
|
|
|
// First checks whether the sample is a key frame. If so and the segment has
|
|
|
|
// passed the segment duration, then flush the generator and write all the data
|
|
|
|
// to file.
|
2016-03-21 19:16:58 +00:00
|
|
|
Status TsSegmenter::AddSample(scoped_refptr<MediaSample> sample) {
|
2016-04-06 05:00:05 +00:00
|
|
|
const bool passed_segment_duration =
|
|
|
|
current_segment_total_sample_duration_ > muxer_options_.segment_duration;
|
|
|
|
if (sample->is_key_frame() && passed_segment_duration) {
|
|
|
|
Status status = Flush();
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
current_segment_total_sample_duration_ += sample->duration() / kTsTimescale;
|
|
|
|
|
|
|
|
return WritePesPacketsToFile();
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TsSegmenter::InjectTsWriterForTesting(scoped_ptr<TsWriter> writer) {
|
|
|
|
ts_writer_ = writer.Pass();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TsSegmenter::InjectPesPacketGeneratorForTesting(
|
|
|
|
scoped_ptr<PesPacketGenerator> generator) {
|
|
|
|
pes_packet_generator_ = generator.Pass();
|
|
|
|
}
|
|
|
|
|
|
|
|
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.");
|
|
|
|
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) {
|
|
|
|
scoped_ptr<PesPacket> pes_packet =
|
|
|
|
pes_packet_generator_->GetNextPesPacket();
|
|
|
|
|
|
|
|
Status status = OpenNewSegmentIfClosed(pes_packet->pts());
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if (!ts_writer_->AddPesPacket(pes_packet.Pass()))
|
|
|
|
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
|
|
|
|
2016-04-06 05:00:05 +00:00
|
|
|
Status TsSegmenter::Flush() {
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
ts_writer_file_opened_ = false;
|
2016-03-21 19:16:58 +00:00
|
|
|
}
|
2016-04-06 05:00:05 +00:00
|
|
|
current_segment_total_sample_duration_ = 0.0;
|
2016-03-21 19:16:58 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp2t
|
|
|
|
} // namespace media
|
|
|
|
} // namespace edash_packager
|