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.");
31 const char* kLocalFilePrefix =
"file://";
32 const char* kUdpFilePrefix =
"udp://";
33 const char* kMemoryFilePrefix =
"memory://";
37 typedef File* (*FileFactoryFunction)(
const char* file_name,
const char* mode);
38 typedef bool (*FileDeleteFunction)(
const char* file_name);
40 struct SupportedTypeInfo {
43 const FileFactoryFunction factory_function;
44 const FileDeleteFunction delete_function;
47 File* CreateLocalFile(
const char* file_name,
const char* mode) {
48 return new LocalFile(file_name, mode);
51 bool DeleteLocalFile(
const char* file_name) {
55 File* CreateUdpFile(
const char* file_name,
const char* mode) {
56 if (base::strcasecmp(mode,
"r")) {
57 NOTIMPLEMENTED() <<
"UdpFile only supports read (receive) mode.";
60 return new UdpFile(file_name);
63 File* CreateMemoryFile(
const char* file_name,
const char* mode) {
64 return new MemoryFile(file_name, mode);
67 bool DeleteMemoryFile(
const char* file_name) {
72 static const SupportedTypeInfo kSupportedTypeInfo[] = {
75 strlen(kLocalFilePrefix),
81 strlen(kUdpFilePrefix),
87 strlen(kMemoryFilePrefix),
95 File* File::Create(
const char* file_name,
const char* mode) {
96 scoped_ptr<File, FileCloser> internal_file(
97 CreateInternalFile(file_name, mode));
99 if (!strncmp(file_name, kMemoryFilePrefix, strlen(kMemoryFilePrefix))) {
101 return internal_file.release();
104 if (FLAGS_io_cache_size) {
106 if (!strcmp(mode,
"r")) {
107 return new ThreadedIoFile(internal_file.Pass(),
108 ThreadedIoFile::kInputMode,
110 FLAGS_io_block_size);
111 }
else if (!strcmp(mode,
"w") || !strcmp(mode,
"a")) {
112 return new ThreadedIoFile(internal_file.Pass(),
113 ThreadedIoFile::kOutputMode,
115 FLAGS_io_block_size);
120 DLOG(WARNING) <<
"Threaded I/O is disabled. Performance may be decreased.";
121 return internal_file.release();
124 File* File::CreateInternalFile(
const char* file_name,
const char* mode) {
125 scoped_ptr<File, FileCloser> internal_file;
126 for (
size_t i = 0; i < arraysize(kSupportedTypeInfo); ++i) {
127 const SupportedTypeInfo& type_info = kSupportedTypeInfo[i];
128 if (strncmp(type_info.type, file_name, type_info.type_length) == 0) {
129 internal_file.reset(type_info.factory_function(
130 file_name + type_info.type_length, mode));
135 internal_file.reset(CreateLocalFile(file_name, mode));
137 return internal_file.release();
141 File* file = File::Create(file_name, mode);
152 File* file = File::CreateInternalFile(file_name, mode);
163 for (
size_t i = 0; i < arraysize(kSupportedTypeInfo); ++i) {
164 const SupportedTypeInfo& type_info = kSupportedTypeInfo[i];
165 if (strncmp(type_info.type, file_name, type_info.type_length) == 0) {
166 return type_info.delete_function ?
167 type_info.delete_function(file_name + type_info.type_length) :
172 return DeleteLocalFile(file_name);
179 int64_t res = file->
Size();
191 const size_t kBufferSize = 0x40000;
192 scoped_ptr<char[]> buf(
new char[kBufferSize]);
195 while ((len = file->
Read(buf.get(), kBufferSize)) > 0)
196 contents->append(buf.get(), len);
202 bool File::Copy(
const char* from_file_name,
const char* to_file_name) {
205 LOG(ERROR) <<
"Failed to open file " << from_file_name;
209 scoped_ptr<File, FileCloser> output_file(
File::Open(to_file_name,
"w"));
211 LOG(ERROR) <<
"Failed to write to " << to_file_name;
215 uint64_t bytes_left = content.size();
216 uint64_t total_bytes_written = 0;
217 const char* content_cstr = content.c_str();
218 while (bytes_left > total_bytes_written) {
219 const int64_t bytes_written =
220 output_file->Write(content_cstr + total_bytes_written, bytes_left);
221 if (bytes_written < 0) {
222 LOG(ERROR) <<
"Failure while writing to " << to_file_name;
226 total_bytes_written += bytes_written;
232 return CopyFile(source, destination, kWholeFile);
239 max_copy = std::numeric_limits<int64_t>::max();
241 const int64_t kBufferSize = 0x40000;
242 scoped_ptr<uint8_t[]> buffer(
new uint8_t[kBufferSize]);
243 int64_t bytes_copied = 0;
244 while (bytes_copied < max_copy) {
245 const int64_t size = std::min(kBufferSize, max_copy - bytes_copied);
246 const int64_t bytes_read = source->
Read(buffer.get(), size);
252 int64_t total_bytes_written = 0;
253 while (total_bytes_written < bytes_read) {
254 const int64_t bytes_written = destination->
Write(
255 buffer.get() + total_bytes_written, bytes_read - total_bytes_written);
256 if (bytes_written < 0)
257 return bytes_written;
259 total_bytes_written += bytes_written;
262 DCHECK_EQ(total_bytes_written, bytes_read);
263 bytes_copied += bytes_read;