7 #include "packager/media/file/file.h"
9 #include <gflags/gflags.h>
12 #include "packager/base/logging.h"
13 #include "packager/base/memory/scoped_ptr.h"
14 #include "packager/media/file/local_file.h"
15 #include "packager/media/file/memory_file.h"
16 #include "packager/media/file/threaded_io_file.h"
17 #include "packager/media/file/udp_file.h"
18 #include "packager/base/strings/string_util.h"
20 DEFINE_uint64(io_cache_size,
22 "Size of the threaded I/O cache, in bytes. Specify 0 to disable "
24 DEFINE_uint64(io_block_size,
26 "Size of the block size used for threaded I/O, in bytes.");
36 const char* kLocalFilePrefix =
"file://";
37 const char* kUdpFilePrefix =
"udp://";
38 const char* kMemoryFilePrefix =
"memory://";
42 typedef File* (*FileFactoryFunction)(
const char* file_name,
const char* mode);
43 typedef bool (*FileDeleteFunction)(
const char* file_name);
45 struct SupportedTypeInfo {
48 const FileFactoryFunction factory_function;
49 const FileDeleteFunction delete_function;
52 File* CreateLocalFile(
const char* file_name,
const char* mode) {
53 return new LocalFile(file_name, mode);
56 bool DeleteLocalFile(
const char* file_name) {
60 File* CreateUdpFile(
const char* file_name,
const char* mode) {
61 if (base::strcasecmp(mode,
"r")) {
62 NOTIMPLEMENTED() <<
"UdpFile only supports read (receive) mode.";
65 return new UdpFile(file_name);
68 File* CreateMemoryFile(
const char* file_name,
const char* mode) {
69 return new MemoryFile(file_name, mode);
72 bool DeleteMemoryFile(
const char* file_name) {
77 static const SupportedTypeInfo kSupportedTypeInfo[] = {
80 strlen(kLocalFilePrefix),
86 strlen(kUdpFilePrefix),
92 strlen(kMemoryFilePrefix),
100 File* File::Create(
const char* file_name,
const char* mode) {
101 scoped_ptr<File, FileCloser> internal_file(
102 CreateInternalFile(file_name, mode));
104 if (!strncmp(file_name, kMemoryFilePrefix, strlen(kMemoryFilePrefix))) {
106 return internal_file.release();
109 if (FLAGS_io_cache_size) {
111 if (!strcmp(mode,
"r")) {
112 return new ThreadedIoFile(internal_file.Pass(),
113 ThreadedIoFile::kInputMode,
115 FLAGS_io_block_size);
116 }
else if (!strcmp(mode,
"w") || !strcmp(mode,
"a")) {
117 return new ThreadedIoFile(internal_file.Pass(),
118 ThreadedIoFile::kOutputMode,
120 FLAGS_io_block_size);
125 DLOG(WARNING) <<
"Threaded I/O is disabled. Performance may be decreased.";
126 return internal_file.release();
129 File* File::CreateInternalFile(
const char* file_name,
const char* mode) {
130 scoped_ptr<File, FileCloser> internal_file;
131 for (
size_t i = 0; i < arraysize(kSupportedTypeInfo); ++i) {
132 const SupportedTypeInfo& type_info = kSupportedTypeInfo[i];
133 if (strncmp(type_info.type, file_name, type_info.type_length) == 0) {
134 internal_file.reset(type_info.factory_function(
135 file_name + type_info.type_length, mode));
140 internal_file.reset(CreateLocalFile(file_name, mode));
142 return internal_file.release();
146 File* file = File::Create(file_name, mode);
157 File* file = File::CreateInternalFile(file_name, mode);
168 for (
size_t i = 0; i < arraysize(kSupportedTypeInfo); ++i) {
169 const SupportedTypeInfo& type_info = kSupportedTypeInfo[i];
170 if (strncmp(type_info.type, file_name, type_info.type_length) == 0) {
171 return type_info.delete_function ?
172 type_info.delete_function(file_name + type_info.type_length) :
177 return DeleteLocalFile(file_name);
184 int64_t res = file->
Size();
196 const size_t kBufferSize = 0x40000;
197 scoped_ptr<char[]> buf(
new char[kBufferSize]);
200 while ((len = file->
Read(buf.get(), kBufferSize)) > 0)
201 contents->append(buf.get(), len);
207 bool File::Copy(
const char* from_file_name,
const char* to_file_name) {
210 LOG(ERROR) <<
"Failed to open file " << from_file_name;
214 scoped_ptr<File, FileCloser> output_file(
File::Open(to_file_name,
"w"));
216 LOG(ERROR) <<
"Failed to write to " << to_file_name;
220 uint64_t bytes_left = content.size();
221 uint64_t total_bytes_written = 0;
222 const char* content_cstr = content.c_str();
223 while (bytes_left > total_bytes_written) {
224 const int64_t bytes_written =
225 output_file->Write(content_cstr + total_bytes_written, bytes_left);
226 if (bytes_written < 0) {
227 LOG(ERROR) <<
"Failure while writing to " << to_file_name;
231 total_bytes_written += bytes_written;
237 return CopyFile(source, destination, kWholeFile);
244 max_copy = std::numeric_limits<int64_t>::max();
246 const int64_t kBufferSize = 0x40000;
247 scoped_ptr<uint8_t[]> buffer(
new uint8_t[kBufferSize]);
248 int64_t bytes_copied = 0;
249 while (bytes_copied < max_copy) {
250 const int64_t size = std::min(kBufferSize, max_copy - bytes_copied);
251 const int64_t bytes_read = source->
Read(buffer.get(), size);
257 int64_t total_bytes_written = 0;
258 while (total_bytes_written < bytes_read) {
259 const int64_t bytes_written = destination->
Write(
260 buffer.get() + total_bytes_written, bytes_read - total_bytes_written);
261 if (bytes_written < 0)
262 return bytes_written;
264 total_bytes_written += bytes_written;
267 DCHECK_EQ(total_bytes_written, bytes_read);
268 bytes_copied += bytes_read;