7 #include "packager/media/file/file.h"
11 #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/threaded_io_file.h"
16 #include "packager/media/file/udp_file.h"
17 #include "packager/base/strings/string_util.h"
19 DEFINE_uint64(io_cache_size,
21 "Size of the threaded I/O cache, in bytes. Specify 0 to disable "
23 DEFINE_uint64(io_block_size,
25 "Size of the block size used for threaded I/O, in bytes.");
27 namespace edash_packager {
30 const char* kLocalFilePrefix =
"file://";
31 const char* kUdpFilePrefix =
"udp://";
35 typedef File* (*FileFactoryFunction)(
const char* file_name,
const char* mode);
36 typedef bool (*FileDeleteFunction)(
const char* file_name);
38 struct SupportedTypeInfo {
41 const FileFactoryFunction factory_function;
42 const FileDeleteFunction delete_function;
45 File* CreateLocalFile(
const char* file_name,
const char* mode) {
46 return new LocalFile(file_name, mode);
49 bool DeleteLocalFile(
const char* file_name) {
53 File* CreateUdpFile(
const char* file_name,
const char* mode) {
54 if (base::strcasecmp(mode,
"r")) {
55 NOTIMPLEMENTED() <<
"UdpFile only supports read (receive) mode.";
58 return new UdpFile(file_name);
61 static const SupportedTypeInfo kSupportedTypeInfo[] = {
64 strlen(kLocalFilePrefix),
70 strlen(kUdpFilePrefix),
78 File* File::Create(
const char* file_name,
const char* mode) {
79 scoped_ptr<File, FileCloser> internal_file(
80 CreateInternalFile(file_name, mode));
82 if (FLAGS_io_cache_size) {
84 if (!strcmp(mode,
"r")) {
85 return new ThreadedIoFile(internal_file.Pass(),
86 ThreadedIoFile::kInputMode,
89 }
else if (!strcmp(mode,
"w") || !strcmp(mode,
"a")) {
90 return new ThreadedIoFile(internal_file.Pass(),
91 ThreadedIoFile::kOutputMode,
98 DLOG(WARNING) <<
"Threaded I/O is disabled. Performance may be decreased.";
99 return internal_file.release();
102 File* File::CreateInternalFile(
const char* file_name,
const char* mode) {
103 scoped_ptr<File, FileCloser> internal_file;
104 for (
size_t i = 0; i < arraysize(kSupportedTypeInfo); ++i) {
105 const SupportedTypeInfo& type_info = kSupportedTypeInfo[i];
106 if (strncmp(type_info.type, file_name, type_info.type_length) == 0) {
107 internal_file.reset(type_info.factory_function(
108 file_name + type_info.type_length, mode));
113 internal_file.reset(CreateLocalFile(file_name, mode));
115 return internal_file.release();
119 File* file = File::Create(file_name, mode);
130 File* file = File::CreateInternalFile(file_name, mode);
141 for (
size_t i = 0; i < arraysize(kSupportedTypeInfo); ++i) {
142 const SupportedTypeInfo& type_info = kSupportedTypeInfo[i];
143 if (strncmp(type_info.type, file_name, type_info.type_length) == 0) {
144 return type_info.delete_function ?
145 type_info.delete_function(file_name + type_info.type_length) :
150 return DeleteLocalFile(file_name);
157 int64_t res = file->
Size();
169 const size_t kBufferSize = 0x40000;
170 scoped_ptr<char[]> buf(
new char[kBufferSize]);
173 while ((len = file->
Read(buf.get(), kBufferSize)) > 0)
174 contents->append(buf.get(), len);
180 bool File::Copy(
const char* from_file_name,
const char* to_file_name) {
183 LOG(ERROR) <<
"Failed to open file " << from_file_name;
187 scoped_ptr<edash_packager::media::File, edash_packager::media::FileCloser>
190 LOG(ERROR) <<
"Failed to write to " << to_file_name;
194 uint64_t bytes_left = content.size();
195 uint64_t total_bytes_written = 0;
196 const char* content_cstr = content.c_str();
197 while (bytes_left > total_bytes_written) {
198 const int64_t bytes_written =
199 output_file->Write(content_cstr + total_bytes_written, bytes_left);
200 if (bytes_written < 0) {
201 LOG(ERROR) <<
"Failure while writing to " << to_file_name;
205 total_bytes_written += bytes_written;
211 return CopyFile(source, destination, kWholeFile);
218 max_copy = std::numeric_limits<int64_t>::max();
220 const int64_t kBufferSize = 0x40000;
221 scoped_ptr<uint8_t[]> buffer(
new uint8_t[kBufferSize]);
222 int64_t bytes_copied = 0;
223 while (bytes_copied < max_copy) {
224 const int64_t size = std::min(kBufferSize, max_copy - bytes_copied);
225 const int64_t bytes_read = source->
Read(buffer.get(), size);
231 int64_t total_bytes_written = 0;
232 while (total_bytes_written < bytes_read) {
233 const int64_t bytes_written = destination->
Write(
234 buffer.get() + total_bytes_written, bytes_read - total_bytes_written);
235 if (bytes_written < 0)
236 return bytes_written;
238 total_bytes_written += bytes_written;
241 DCHECK_EQ(total_bytes_written, bytes_read);
242 bytes_copied += bytes_read;