From 95089593fc05af4c297e6edd02d70379a284cd78 Mon Sep 17 00:00:00 2001 From: Jacob Trimble Date: Thu, 4 Feb 2021 14:02:33 -0800 Subject: [PATCH] Don't re-open WebVTT file to determine size. Change-Id: Id92226adce813b7d0c4c741e47e36dbf8f208797 --- packager/media/formats/webvtt/webvtt_file_buffer.cc | 4 +++- packager/media/formats/webvtt/webvtt_file_buffer.h | 2 +- packager/media/formats/webvtt/webvtt_muxer.cc | 5 +---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/packager/media/formats/webvtt/webvtt_file_buffer.cc b/packager/media/formats/webvtt/webvtt_file_buffer.cc index d73a4657d6..02507c1858 100644 --- a/packager/media/formats/webvtt/webvtt_file_buffer.cc +++ b/packager/media/formats/webvtt/webvtt_file_buffer.cc @@ -73,10 +73,12 @@ void WebVttFileBuffer::Append(const TextSample& sample) { buffer_.append("\n"); // end of sample } -bool WebVttFileBuffer::WriteTo(File* file) { +bool WebVttFileBuffer::WriteTo(File* file, uint64_t* size) { DCHECK(file); DCHECK_GT(buffer_.size(), 0u) << "The buffer should at least have a header"; + if (size) + *size = buffer_.size(); const int written = file->Write(buffer_.c_str(), buffer_.size()); if (written < 0) { return false; diff --git a/packager/media/formats/webvtt/webvtt_file_buffer.h b/packager/media/formats/webvtt/webvtt_file_buffer.h index 04f8cede61..5656ba93d5 100644 --- a/packager/media/formats/webvtt/webvtt_file_buffer.h +++ b/packager/media/formats/webvtt/webvtt_file_buffer.h @@ -27,7 +27,7 @@ class WebVttFileBuffer { void Reset(); void Append(const TextSample& sample); - bool WriteTo(File* file); + bool WriteTo(File* file, uint64_t* size); // Get the number of samples that have been appended to this file. size_t sample_count() const { return sample_count_; } diff --git a/packager/media/formats/webvtt/webvtt_muxer.cc b/packager/media/formats/webvtt/webvtt_muxer.cc index b3cb7865b6..b4c031ff51 100644 --- a/packager/media/formats/webvtt/webvtt_muxer.cc +++ b/packager/media/formats/webvtt/webvtt_muxer.cc @@ -45,15 +45,12 @@ Status WebVttMuxer::WriteToFile(const std::string& filename, uint64_t* size) { return Status(error::FILE_FAILURE, "Failed to open " + filename); } - buffer_->WriteTo(file.get()); + buffer_->WriteTo(file.get(), size); buffer_->Reset(); if (!file.release()->Close()) { return Status(error::FILE_FAILURE, "Failed to close " + filename); } - if (size) { - *size = File::GetFileSize(filename.c_str()); - } return Status::OK; }