Write to memory file before writing to file for WebM segments generation (#803)

The change is needed to address #554.
This commit is contained in:
sr90 2020-07-27 21:32:56 -07:00 committed by GitHub
parent a8ea7fd085
commit 11d69896c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 8 deletions

View File

@ -32,17 +32,28 @@ Status MultiSegmentSegmenter::FinalizeSegment(uint64_t start_timestamp,
return Status(error::FILE_FAILURE, "Error finalizing segment."); return Status(error::FILE_FAILURE, "Error finalizing segment.");
if (!is_subsegment) { if (!is_subsegment) {
const std::string segment_name = writer_->file()->file_name(); std::string segment_name =
GetSegmentName(options().segment_template, start_timestamp,
num_segment_, options().bandwidth);
// Close the file, which also does flushing, to make sure the file is // Close the file, which also does flushing, to make sure the file is
// written before manifest is updated. // written before manifest is updated.
RETURN_IF_ERROR(writer_->Close()); RETURN_IF_ERROR(writer_->Close());
if (!File::Copy(temp_file_name_.c_str(), segment_name.c_str()))
return Status(error::FILE_FAILURE, "Failure to copy memory file.");
if (!File::Delete(temp_file_name_.c_str()))
return Status(error::FILE_FAILURE, "Failure to delete memory file.");
num_segment_++;
if (muxer_listener()) { if (muxer_listener()) {
const uint64_t size = cluster()->Size(); const uint64_t size = cluster()->Size();
muxer_listener()->OnNewSegment(segment_name, start_timestamp, muxer_listener()->OnNewSegment(segment_name, start_timestamp,
duration_timestamp, size); duration_timestamp, size);
} }
VLOG(1) << "WEBM file '" << writer_->file()->file_name() << "' finalized."; VLOG(1) << "WEBM file '" << segment_name << "' finalized.";
} }
return Status::OK; return Status::OK;
} }
@ -77,15 +88,16 @@ Status MultiSegmentSegmenter::DoFinalize() {
Status MultiSegmentSegmenter::NewSegment(uint64_t start_timestamp, Status MultiSegmentSegmenter::NewSegment(uint64_t start_timestamp,
bool is_subsegment) { bool is_subsegment) {
if (!is_subsegment) { if (!is_subsegment) {
// Create a new file for the new segment. temp_file_name_ =
std::string segment_name = "memory://" + GetSegmentName(options().segment_template,
GetSegmentName(options().segment_template, start_timestamp, start_timestamp, num_segment_,
num_segment_, options().bandwidth); options().bandwidth);
writer_.reset(new MkvWriter); writer_.reset(new MkvWriter);
Status status = writer_->Open(segment_name); Status status = writer_->Open(temp_file_name_);
if (!status.ok()) if (!status.ok())
return status; return status;
num_segment_++;
} }
const uint64_t start_timecode = FromBmffTimestamp(start_timestamp); const uint64_t start_timecode = FromBmffTimestamp(start_timestamp);

View File

@ -8,6 +8,7 @@
#define PACKAGER_MEDIA_FORMATS_WEBM_MULTI_SEGMENT_SEGMENTER_H_ #define PACKAGER_MEDIA_FORMATS_WEBM_MULTI_SEGMENT_SEGMENTER_H_
#include <memory> #include <memory>
#include "packager/media/formats/webm/mkv_writer.h" #include "packager/media/formats/webm/mkv_writer.h"
#include "packager/media/formats/webm/segmenter.h" #include "packager/media/formats/webm/segmenter.h"
#include "packager/status.h" #include "packager/status.h"
@ -47,6 +48,7 @@ class MultiSegmentSegmenter : public Segmenter {
std::unique_ptr<MkvWriter> writer_; std::unique_ptr<MkvWriter> writer_;
uint32_t num_segment_; uint32_t num_segment_;
std::string temp_file_name_;
DISALLOW_COPY_AND_ASSIGN(MultiSegmentSegmenter); DISALLOW_COPY_AND_ASSIGN(MultiSegmentSegmenter);
}; };