2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/file/file.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/base/memory/scoped_ptr.h"
|
|
|
|
#include "packager/media/file/local_file.h"
|
|
|
|
#include "packager/media/file/udp_file.h"
|
|
|
|
#include "packager/base/strings/string_util.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2013-10-11 21:44:55 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
const char* kLocalFilePrefix = "file://";
|
2014-04-30 23:22:15 +00:00
|
|
|
const char* kUdpFilePrefix = "udp://";
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-01-15 22:29:56 +00:00
|
|
|
typedef File* (*FileFactoryFunction)(const char* file_name, const char* mode);
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
struct SupportedTypeInfo {
|
|
|
|
const char* type;
|
2014-07-02 16:58:40 +00:00
|
|
|
size_t type_length;
|
2013-10-11 21:44:55 +00:00
|
|
|
const FileFactoryFunction factory_function;
|
|
|
|
};
|
|
|
|
|
2014-01-15 22:29:56 +00:00
|
|
|
static File* CreateLocalFile(const char* file_name, const char* mode) {
|
|
|
|
return new LocalFile(file_name, mode);
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 23:22:15 +00:00
|
|
|
static File* CreateUdpFile(const char* file_name, const char* mode) {
|
|
|
|
if (base::strcasecmp(mode, "r")) {
|
|
|
|
NOTIMPLEMENTED() << "UdpFile only supports read (receive) mode.";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return new UdpFile(file_name);
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:44:55 +00:00
|
|
|
static const SupportedTypeInfo kSupportedTypeInfo[] = {
|
|
|
|
{ kLocalFilePrefix, strlen(kLocalFilePrefix), &CreateLocalFile },
|
2014-04-30 23:22:15 +00:00
|
|
|
{ kUdpFilePrefix, strlen(kUdpFilePrefix), &CreateUdpFile },
|
2013-10-11 21:44:55 +00:00
|
|
|
};
|
|
|
|
|
2014-01-15 22:29:56 +00:00
|
|
|
File* File::Create(const char* file_name, const char* mode) {
|
2013-10-11 21:44:55 +00:00
|
|
|
for (size_t i = 0; i < arraysize(kSupportedTypeInfo); ++i) {
|
|
|
|
const SupportedTypeInfo& type_info = kSupportedTypeInfo[i];
|
2014-01-15 22:29:56 +00:00
|
|
|
if (strncmp(type_info.type, file_name, type_info.type_length) == 0) {
|
|
|
|
return type_info.factory_function(file_name + type_info.type_length,
|
|
|
|
mode);
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Otherwise we assume it is a local file
|
2014-01-15 22:29:56 +00:00
|
|
|
return CreateLocalFile(file_name, mode);
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 22:29:56 +00:00
|
|
|
File* File::Open(const char* file_name, const char* mode) {
|
|
|
|
File* file = File::Create(file_name, mode);
|
|
|
|
if (!file)
|
2013-10-11 21:44:55 +00:00
|
|
|
return NULL;
|
|
|
|
if (!file->Open()) {
|
|
|
|
delete file;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t File::GetFileSize(const char* file_name) {
|
2014-01-15 22:29:56 +00:00
|
|
|
File* file = File::Open(file_name, "r");
|
|
|
|
if (!file)
|
2013-10-11 21:44:55 +00:00
|
|
|
return -1;
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t res = file->Size();
|
2014-01-15 22:29:56 +00:00
|
|
|
file->Close();
|
2013-10-11 21:44:55 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-01-15 22:29:56 +00:00
|
|
|
bool File::ReadFileToString(const char* file_name, std::string* contents) {
|
|
|
|
DCHECK(contents);
|
|
|
|
|
|
|
|
File* file = File::Open(file_name, "r");
|
|
|
|
if (!file)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const size_t kBufferSize = 0x40000; // 256KB.
|
|
|
|
scoped_ptr<char[]> buf(new char[kBufferSize]);
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t len;
|
2014-01-15 22:29:56 +00:00
|
|
|
while ((len = file->Read(buf.get(), kBufferSize)) > 0)
|
|
|
|
contents->append(buf.get(), len);
|
|
|
|
|
|
|
|
file->Close();
|
|
|
|
return len == 0;
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:44:55 +00:00
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|