From a82bab9f3ff7c7fe9eb4622b70f889022176a2c7 Mon Sep 17 00:00:00 2001 From: Thomas Inskip Date: Wed, 11 Mar 2015 12:18:17 -0700 Subject: [PATCH] Added File::Delete as well as deletion of temporary files created by SingleSegmentSegmenter. Change-Id: Id0aec2f83955f81d2a059c2d077e8381ec7ec1f7 --- packager/media/file/file.cc | 41 +++++++++++++++++-- packager/media/file/file.h | 5 +++ packager/media/file/local_file.cc | 4 ++ packager/media/file/local_file.h | 6 ++- .../formats/mp4/single_segment_segmenter.cc | 10 ++++- 5 files changed, 60 insertions(+), 6 deletions(-) diff --git a/packager/media/file/file.cc b/packager/media/file/file.cc index 045a74f50f..878de3584c 100644 --- a/packager/media/file/file.cc +++ b/packager/media/file/file.cc @@ -18,19 +18,27 @@ namespace media { const char* kLocalFilePrefix = "file://"; const char* kUdpFilePrefix = "udp://"; +namespace { + typedef File* (*FileFactoryFunction)(const char* file_name, const char* mode); +typedef bool (*FileDeleteFunction)(const char* file_name); struct SupportedTypeInfo { const char* type; size_t type_length; const FileFactoryFunction factory_function; + const FileDeleteFunction delete_function; }; -static File* CreateLocalFile(const char* file_name, const char* mode) { +File* CreateLocalFile(const char* file_name, const char* mode) { return new LocalFile(file_name, mode); } -static File* CreateUdpFile(const char* file_name, const char* mode) { +bool DeleteLocalFile(const char* file_name) { + return LocalFile::Delete(file_name); +} + +File* CreateUdpFile(const char* file_name, const char* mode) { if (base::strcasecmp(mode, "r")) { NOTIMPLEMENTED() << "UdpFile only supports read (receive) mode."; return NULL; @@ -39,10 +47,22 @@ static File* CreateUdpFile(const char* file_name, const char* mode) { } static const SupportedTypeInfo kSupportedTypeInfo[] = { - { kLocalFilePrefix, strlen(kLocalFilePrefix), &CreateLocalFile }, - { kUdpFilePrefix, strlen(kUdpFilePrefix), &CreateUdpFile }, + { + kLocalFilePrefix, + strlen(kLocalFilePrefix), + &CreateLocalFile, + &DeleteLocalFile + }, + { + kUdpFilePrefix, + strlen(kUdpFilePrefix), + &CreateUdpFile, + NULL + }, }; +} // namespace + File* File::Create(const char* file_name, const char* mode) { for (size_t i = 0; i < arraysize(kSupportedTypeInfo); ++i) { const SupportedTypeInfo& type_info = kSupportedTypeInfo[i]; @@ -66,6 +86,19 @@ File* File::Open(const char* file_name, const char* mode) { return file; } +bool File::Delete(const char* file_name) { + for (size_t i = 0; i < arraysize(kSupportedTypeInfo); ++i) { + const SupportedTypeInfo& type_info = kSupportedTypeInfo[i]; + if (strncmp(type_info.type, file_name, type_info.type_length) == 0) { + return type_info.delete_function ? + type_info.delete_function(file_name + type_info.type_length) : + false; + } + } + // Otherwise we assume it is a local file + return DeleteLocalFile(file_name); +} + int64_t File::GetFileSize(const char* file_name) { File* file = File::Open(file_name, "r"); if (!file) diff --git a/packager/media/file/file.h b/packager/media/file/file.h index 9b1dbe3485..cd50bcfafe 100644 --- a/packager/media/file/file.h +++ b/packager/media/file/file.h @@ -29,6 +29,11 @@ class File { /// @return A File pointer on success, false otherwise. static File* Open(const char* file_name, const char* mode); + /// Delete the specified file. + /// @param file_name contains the path of the file to be deleted. + /// @return true if successful, false otherwise. + static bool Delete(const char* file_name); + /// Flush() and de-allocate resources associated with this file, and /// delete this File object. THIS IS THE ONE TRUE WAY TO DEALLOCATE /// THIS OBJECT. diff --git a/packager/media/file/local_file.cc b/packager/media/file/local_file.cc index de5cd478c6..e3840f5122 100644 --- a/packager/media/file/local_file.cc +++ b/packager/media/file/local_file.cc @@ -72,5 +72,9 @@ bool LocalFile::Open() { return (internal_file_ != NULL); } +bool LocalFile::Delete(const char* file_name) { + return base::DeleteFile(base::FilePath(file_name), false); +} + } // namespace media } // namespace edash_packager diff --git a/packager/media/file/local_file.h b/packager/media/file/local_file.h index c28a9d7ae1..98929a44eb 100644 --- a/packager/media/file/local_file.h +++ b/packager/media/file/local_file.h @@ -35,6 +35,11 @@ class LocalFile : public File { virtual bool Eof() OVERRIDE; /// @} + /// Delete a local file. + /// @param file_name is the path of the file to be deleted. + /// @return true if successful, or false otherwise. + static bool Delete(const char* file_name); + protected: virtual ~LocalFile(); @@ -51,4 +56,3 @@ class LocalFile : public File { } // namespace edash_packager #endif // PACKAGER_FILE_LOCAL_FILE_H_ - diff --git a/packager/media/formats/mp4/single_segment_segmenter.cc b/packager/media/formats/mp4/single_segment_segmenter.cc index 635d538be7..eef1aee99b 100644 --- a/packager/media/formats/mp4/single_segment_segmenter.cc +++ b/packager/media/formats/mp4/single_segment_segmenter.cc @@ -35,7 +35,15 @@ SingleSegmentSegmenter::SingleSegmentSegmenter(const MuxerOptions& options, scoped_ptr ftyp, scoped_ptr moov) : Segmenter(options, ftyp.Pass(), moov.Pass()) {} -SingleSegmentSegmenter::~SingleSegmentSegmenter() {} + +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_; + } +} bool SingleSegmentSegmenter::GetInitRange(size_t* offset, size_t* size) { // In Finalize, ftyp and moov gets written first so offset must be 0.