2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 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
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp4/single_segment_segmenter.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2016-10-13 22:54:32 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2017-07-10 18:26:22 +00:00
|
|
|
#include "packager/file/file.h"
|
|
|
|
#include "packager/file/file_util.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/buffer_writer.h"
|
|
|
|
#include "packager/media/base/muxer_options.h"
|
2015-05-11 21:07:10 +00:00
|
|
|
#include "packager/media/event/progress_listener.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp4/box_definitions.h"
|
2018-02-01 20:25:07 +00:00
|
|
|
#include "packager/media/formats/mp4/key_frame_info.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-11-12 20:37:58 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
SingleSegmentSegmenter::SingleSegmentSegmenter(const MuxerOptions& options,
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<FileType> ftyp,
|
|
|
|
std::unique_ptr<Movie> moov)
|
|
|
|
: Segmenter(options, std::move(ftyp), std::move(moov)) {}
|
2015-03-11 19:18:17 +00:00
|
|
|
|
|
|
|
SingleSegmentSegmenter::~SingleSegmentSegmenter() {
|
|
|
|
if (temp_file_)
|
|
|
|
temp_file_.release()->Close();
|
|
|
|
if (!temp_file_name_.empty()) {
|
|
|
|
if (!File::Delete(temp_file_name_.c_str()))
|
|
|
|
LOG(ERROR) << "Unable to delete temporary file " << temp_file_name_;
|
|
|
|
}
|
|
|
|
}
|
2014-04-08 20:21:07 +00:00
|
|
|
|
2014-04-18 22:00:30 +00:00
|
|
|
bool SingleSegmentSegmenter::GetInitRange(size_t* offset, size_t* size) {
|
|
|
|
// In Finalize, ftyp and moov gets written first so offset must be 0.
|
|
|
|
*offset = 0;
|
|
|
|
*size = ftyp()->ComputeSize() + moov()->ComputeSize();
|
|
|
|
return true;
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 22:00:30 +00:00
|
|
|
bool SingleSegmentSegmenter::GetIndexRange(size_t* offset, size_t* size) {
|
|
|
|
// Index range is right after init range so the offset must be the size of
|
|
|
|
// ftyp and moov.
|
|
|
|
*offset = ftyp()->ComputeSize() + moov()->ComputeSize();
|
2020-12-07 08:09:04 +00:00
|
|
|
*size = options().mp4_params.generate_sidx_in_media_segments
|
|
|
|
? vod_sidx_->ComputeSize()
|
|
|
|
: 0;
|
2014-04-18 22:00:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2017-05-01 23:17:09 +00:00
|
|
|
std::vector<Range> SingleSegmentSegmenter::GetSegmentRanges() {
|
|
|
|
std::vector<Range> ranges;
|
2020-12-07 08:09:04 +00:00
|
|
|
uint64_t next_offset = ftyp()->ComputeSize() + moov()->ComputeSize() +
|
|
|
|
(options().mp4_params.generate_sidx_in_media_segments
|
|
|
|
? vod_sidx_->ComputeSize()
|
|
|
|
: 0) +
|
|
|
|
vod_sidx_->first_offset;
|
2017-05-01 23:17:09 +00:00
|
|
|
for (const SegmentReference& segment_reference : vod_sidx_->references) {
|
|
|
|
Range r;
|
|
|
|
r.start = next_offset;
|
|
|
|
// Ranges are inclusive, so -1 to the size.
|
|
|
|
r.end = r.start + segment_reference.referenced_size - 1;
|
|
|
|
next_offset = r.end + 1;
|
|
|
|
ranges.push_back(r);
|
|
|
|
}
|
|
|
|
return ranges;
|
|
|
|
}
|
|
|
|
|
2014-04-18 22:00:30 +00:00
|
|
|
Status SingleSegmentSegmenter::DoInitialize() {
|
2015-05-11 21:07:10 +00:00
|
|
|
// Single segment segmentation involves two stages:
|
|
|
|
// Stage 1: Create media subsegments from media samples
|
|
|
|
// Stage 2: Update media header (moov) which involves copying of media
|
|
|
|
// subsegments
|
|
|
|
// Assumes stage 2 takes similar amount of time as stage 1. The previous
|
|
|
|
// progress_target was set for stage 1. Times two to account for stage 2.
|
|
|
|
set_progress_target(progress_target() * 2);
|
|
|
|
|
2016-10-04 22:46:02 +00:00
|
|
|
if (!TempFilePath(options().temp_dir, &temp_file_name_))
|
|
|
|
return Status(error::FILE_FAILURE, "Unable to create temporary file.");
|
2014-05-08 21:02:36 +00:00
|
|
|
temp_file_.reset(File::Open(temp_file_name_.c_str(), "w"));
|
2014-04-18 22:00:30 +00:00
|
|
|
return temp_file_
|
|
|
|
? Status::OK
|
|
|
|
: Status(error::FILE_FAILURE,
|
2014-05-08 21:02:36 +00:00
|
|
|
"Cannot open file to write " + temp_file_name_);
|
2014-04-18 22:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status SingleSegmentSegmenter::DoFinalize() {
|
2014-01-18 00:03:21 +00:00
|
|
|
DCHECK(temp_file_);
|
|
|
|
DCHECK(ftyp());
|
|
|
|
DCHECK(moov());
|
|
|
|
DCHECK(vod_sidx_);
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
// Close the temp file to prepare for reading later.
|
|
|
|
if (!temp_file_.release()->Close()) {
|
2017-09-07 23:05:20 +00:00
|
|
|
return Status(
|
|
|
|
error::FILE_FAILURE,
|
|
|
|
"Cannot close the temp file " + temp_file_name_ +
|
|
|
|
", possibly file permission issue or running out of disk space.");
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> file(
|
2013-11-12 20:37:58 +00:00
|
|
|
File::Open(options().output_file_name.c_str(), "w"));
|
|
|
|
if (file == NULL) {
|
|
|
|
return Status(error::FILE_FAILURE,
|
|
|
|
"Cannot open file to write " + options().output_file_name);
|
|
|
|
}
|
|
|
|
|
2015-05-12 00:52:53 +00:00
|
|
|
LOG(INFO) << "Update media header (moov) and rewrite the file to '"
|
|
|
|
<< options().output_file_name << "'.";
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
// Write ftyp, moov and sidx to output file.
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<BufferWriter> buffer(new BufferWriter());
|
2013-11-12 20:37:58 +00:00
|
|
|
ftyp()->Write(buffer.get());
|
|
|
|
moov()->Write(buffer.get());
|
2020-12-07 08:09:04 +00:00
|
|
|
|
|
|
|
if (options().mp4_params.generate_sidx_in_media_segments)
|
|
|
|
vod_sidx_->Write(buffer.get());
|
|
|
|
|
2014-04-18 22:00:30 +00:00
|
|
|
Status status = buffer->WriteToFile(file.get());
|
2013-11-12 20:37:58 +00:00
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
|
|
|
|
// Load the temp file and write to output file.
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> temp_file(
|
2014-05-08 21:02:36 +00:00
|
|
|
File::Open(temp_file_name_.c_str(), "r"));
|
2013-11-12 20:37:58 +00:00
|
|
|
if (temp_file == NULL) {
|
|
|
|
return Status(error::FILE_FAILURE,
|
2014-05-08 21:02:36 +00:00
|
|
|
"Cannot open file to read " + temp_file_name_);
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 21:07:10 +00:00
|
|
|
// The target of 2nd stage of single segment segmentation.
|
|
|
|
const uint64_t re_segment_progress_target = progress_target() * 0.5;
|
|
|
|
|
2015-02-09 23:01:10 +00:00
|
|
|
const int kBufSize = 0x200000; // 2MB.
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufSize]);
|
2015-03-17 20:59:12 +00:00
|
|
|
while (true) {
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t size = temp_file->Read(buf.get(), kBufSize);
|
2015-03-17 20:59:12 +00:00
|
|
|
if (size == 0) {
|
|
|
|
break;
|
|
|
|
} else if (size < 0) {
|
2013-11-12 20:37:58 +00:00
|
|
|
return Status(error::FILE_FAILURE,
|
2014-05-08 21:02:36 +00:00
|
|
|
"Failed to read file " + temp_file_name_);
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t size_written = file->Write(buf.get(), size);
|
2013-11-12 20:37:58 +00:00
|
|
|
if (size_written != size) {
|
|
|
|
return Status(error::FILE_FAILURE,
|
|
|
|
"Failed to write file " + options().output_file_name);
|
|
|
|
}
|
2015-05-11 21:07:10 +00:00
|
|
|
UpdateProgress(static_cast<double>(size) / temp_file->Size() *
|
|
|
|
re_segment_progress_target);
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
2017-09-07 23:05:20 +00:00
|
|
|
if (!temp_file.release()->Close()) {
|
|
|
|
return Status(error::FILE_FAILURE, "Cannot close the temp file " +
|
|
|
|
temp_file_name_ + " after reading.");
|
|
|
|
}
|
|
|
|
if (!file.release()->Close()) {
|
|
|
|
return Status(
|
|
|
|
error::FILE_FAILURE,
|
|
|
|
"Cannot close file " + options().output_file_name +
|
|
|
|
", possibly file permission issue or running out of disk space.");
|
|
|
|
}
|
2015-05-11 21:07:10 +00:00
|
|
|
SetComplete();
|
2013-11-12 20:37:58 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2014-04-18 22:00:30 +00:00
|
|
|
Status SingleSegmentSegmenter::DoFinalizeSegment() {
|
2014-01-23 00:13:41 +00:00
|
|
|
DCHECK(sidx());
|
|
|
|
DCHECK(fragment_buffer());
|
2013-11-12 20:37:58 +00:00
|
|
|
// sidx() contains pre-generated segment references with one reference per
|
|
|
|
// fragment. In VOD, this segment is converted into a subsegment, i.e. one
|
|
|
|
// reference, which contains all the fragments in sidx().
|
|
|
|
std::vector<SegmentReference>& refs = sidx()->references;
|
|
|
|
SegmentReference& vod_ref = refs[0];
|
2014-09-30 21:52:21 +00:00
|
|
|
uint64_t first_sap_time =
|
2013-11-12 20:37:58 +00:00
|
|
|
refs[0].sap_delta_time + refs[0].earliest_presentation_time;
|
2015-08-04 00:28:11 +00:00
|
|
|
for (uint32_t i = 1; i < refs.size(); ++i) {
|
2013-11-12 20:37:58 +00:00
|
|
|
vod_ref.referenced_size += refs[i].referenced_size;
|
2014-03-26 22:09:43 +00:00
|
|
|
// NOTE: We calculate subsegment duration based on the total duration of
|
|
|
|
// this subsegment instead of subtracting earliest_presentation_time as
|
|
|
|
// indicated in the spec.
|
2013-11-12 20:37:58 +00:00
|
|
|
vod_ref.subsegment_duration += refs[i].subsegment_duration;
|
|
|
|
vod_ref.earliest_presentation_time = std::min(
|
|
|
|
vod_ref.earliest_presentation_time, refs[i].earliest_presentation_time);
|
|
|
|
|
|
|
|
if (vod_ref.sap_type == SegmentReference::TypeUnknown &&
|
|
|
|
refs[i].sap_type != SegmentReference::TypeUnknown) {
|
|
|
|
vod_ref.sap_type = refs[i].sap_type;
|
|
|
|
first_sap_time =
|
|
|
|
refs[i].sap_delta_time + refs[i].earliest_presentation_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Calculate sap delta time w.r.t. earliest_presentation_time.
|
|
|
|
if (vod_ref.sap_type != SegmentReference::TypeUnknown) {
|
|
|
|
vod_ref.sap_delta_time =
|
|
|
|
first_sap_time - vod_ref.earliest_presentation_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create segment if it does not exist yet.
|
|
|
|
if (vod_sidx_ == NULL) {
|
|
|
|
vod_sidx_.reset(new SegmentIndex());
|
|
|
|
vod_sidx_->reference_id = sidx()->reference_id;
|
|
|
|
vod_sidx_->timescale = sidx()->timescale;
|
2018-03-27 16:27:17 +00:00
|
|
|
vod_sidx_->earliest_presentation_time = vod_ref.earliest_presentation_time;
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
vod_sidx_->references.push_back(vod_ref);
|
|
|
|
|
2018-02-01 20:25:07 +00:00
|
|
|
if (muxer_listener()) {
|
|
|
|
for (const KeyFrameInfo& key_frame_info : key_frame_infos()) {
|
|
|
|
// Unlike multisegment-segmenter, there is no (sub)segment header (styp,
|
|
|
|
// sidx), so this is already the offset within the (sub)segment.
|
|
|
|
muxer_listener()->OnKeyFrame(key_frame_info.timestamp,
|
|
|
|
key_frame_info.start_byte_offset,
|
|
|
|
key_frame_info.size);
|
|
|
|
}
|
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
// Append fragment buffer to temp file.
|
2015-01-13 00:48:38 +00:00
|
|
|
size_t segment_size = fragment_buffer()->Size();
|
|
|
|
Status status = fragment_buffer()->WriteToFile(temp_file_.get());
|
|
|
|
if (!status.ok()) return status;
|
2015-05-11 21:07:10 +00:00
|
|
|
|
|
|
|
UpdateProgress(vod_ref.subsegment_duration);
|
2015-01-13 00:48:38 +00:00
|
|
|
if (muxer_listener()) {
|
2015-06-15 21:12:42 +00:00
|
|
|
muxer_listener()->OnSampleDurationReady(sample_duration());
|
2016-03-28 08:23:20 +00:00
|
|
|
muxer_listener()->OnNewSegment(options().output_file_name,
|
|
|
|
vod_ref.earliest_presentation_time,
|
2015-01-13 00:48:38 +00:00
|
|
|
vod_ref.subsegment_duration, segment_size);
|
|
|
|
}
|
|
|
|
return Status::OK;
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|