7 #include "packager/media/file/file.h"
9 #include <gflags/gflags.h>
12 #include "packager/base/logging.h"
13 #include "packager/media/file/local_file.h"
14 #include "packager/media/file/memory_file.h"
15 #include "packager/media/file/threaded_io_file.h"
16 #include "packager/media/file/udp_file.h"
18 DEFINE_uint64(io_cache_size,
20 "Size of the threaded I/O cache, in bytes. Specify 0 to disable "
22 DEFINE_uint64(io_block_size,
24 "Size of the block size used for threaded I/O, in bytes.");
34 const char* kLocalFilePrefix =
"file://";
35 const char* kUdpFilePrefix =
"udp://";
36 const char* kMemoryFilePrefix =
"memory://";
40 typedef File* (*FileFactoryFunction)(
const char* file_name,
const char* mode);
41 typedef bool (*FileDeleteFunction)(
const char* file_name);
43 struct SupportedTypeInfo {
46 const FileFactoryFunction factory_function;
47 const FileDeleteFunction delete_function;
50 File* CreateLocalFile(
const char* file_name,
const char* mode) {
51 return new LocalFile(file_name, mode);
54 bool DeleteLocalFile(
const char* file_name) {
58 File* CreateUdpFile(
const char* file_name,
const char* mode) {
59 if (strcmp(mode,
"r")) {
60 NOTIMPLEMENTED() <<
"UdpFile only supports read (receive) mode.";
63 return new UdpFile(file_name);
66 File* CreateMemoryFile(
const char* file_name,
const char* mode) {
67 return new MemoryFile(file_name, mode);
70 bool DeleteMemoryFile(
const char* file_name) {
75 static const SupportedTypeInfo kSupportedTypeInfo[] = {
78 strlen(kLocalFilePrefix),
84 strlen(kUdpFilePrefix),
90 strlen(kMemoryFilePrefix),
98 File* File::Create(
const char* file_name,
const char* mode) {
99 std::unique_ptr<File, FileCloser> internal_file(
100 CreateInternalFile(file_name, mode));
102 if (!strncmp(file_name, kMemoryFilePrefix, strlen(kMemoryFilePrefix))) {
104 return internal_file.release();
107 if (FLAGS_io_cache_size) {
109 if (!strcmp(mode,
"r")) {
110 return new ThreadedIoFile(std::move(internal_file),
111 ThreadedIoFile::kInputMode, FLAGS_io_cache_size,
112 FLAGS_io_block_size);
113 }
else if (!strcmp(mode,
"w") || !strcmp(mode,
"a")) {
114 return new ThreadedIoFile(std::move(internal_file),
115 ThreadedIoFile::kOutputMode,
116 FLAGS_io_cache_size, FLAGS_io_block_size);
121 DLOG(WARNING) <<
"Threaded I/O is disabled. Performance may be decreased.";
122 return internal_file.release();
125 File* File::CreateInternalFile(
const char* file_name,
const char* mode) {
126 std::unique_ptr<File, FileCloser> internal_file;
127 for (
size_t i = 0; i < arraysize(kSupportedTypeInfo); ++i) {
128 const SupportedTypeInfo& type_info = kSupportedTypeInfo[i];
129 if (strncmp(type_info.type, file_name, type_info.type_length) == 0) {
130 internal_file.reset(type_info.factory_function(
131 file_name + type_info.type_length, mode));
136 internal_file.reset(CreateLocalFile(file_name, mode));
138 return internal_file.release();
142 File* file = File::Create(file_name, mode);
153 File* file = File::CreateInternalFile(file_name, mode);
164 for (
size_t i = 0; i < arraysize(kSupportedTypeInfo); ++i) {
165 const SupportedTypeInfo& type_info = kSupportedTypeInfo[i];
166 if (strncmp(type_info.type, file_name, type_info.type_length) == 0) {
167 return type_info.delete_function ?
168 type_info.delete_function(file_name + type_info.type_length) :
173 return DeleteLocalFile(file_name);
180 int64_t res = file->
Size();
192 const size_t kBufferSize = 0x40000;
193 std::unique_ptr<char[]> buf(
new char[kBufferSize]);
196 while ((len = file->
Read(buf.get(), kBufferSize)) > 0)
197 contents->append(buf.get(), len);
203 bool File::Copy(
const char* from_file_name,
const char* to_file_name) {
206 LOG(ERROR) <<
"Failed to open file " << from_file_name;
210 std::unique_ptr<File, FileCloser> output_file(
File::Open(to_file_name,
"w"));
212 LOG(ERROR) <<
"Failed to write to " << to_file_name;
216 uint64_t bytes_left = content.size();
217 uint64_t total_bytes_written = 0;
218 const char* content_cstr = content.c_str();
219 while (bytes_left > total_bytes_written) {
220 const int64_t bytes_written =
221 output_file->Write(content_cstr + total_bytes_written, bytes_left);
222 if (bytes_written < 0) {
223 LOG(ERROR) <<
"Failure while writing to " << to_file_name;
227 total_bytes_written += bytes_written;
233 return CopyFile(source, destination, kWholeFile);
240 max_copy = std::numeric_limits<int64_t>::max();
242 const int64_t kBufferSize = 0x40000;
243 std::unique_ptr<uint8_t[]> buffer(
new uint8_t[kBufferSize]);
244 int64_t bytes_copied = 0;
245 while (bytes_copied < max_copy) {
246 const int64_t size = std::min(kBufferSize, max_copy - bytes_copied);
247 const int64_t bytes_read = source->
Read(buffer.get(), size);
253 int64_t total_bytes_written = 0;
254 while (total_bytes_written < bytes_read) {
255 const int64_t bytes_written = destination->
Write(
256 buffer.get() + total_bytes_written, bytes_read - total_bytes_written);
257 if (bytes_written < 0)
258 return bytes_written;
260 total_bytes_written += bytes_written;
263 DCHECK_EQ(total_bytes_written, bytes_read);
264 bytes_copied += bytes_read;