Improve performance of registering font preferences
[chromium-blink-merge.git] / media / ffmpeg / file_protocol.cc
blob49b9f1c0a3fc872bbee903cb371ae3648471ac1b
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/ffmpeg/file_protocol.h"
7 #include "build/build_config.h"
9 #if defined(OS_WIN)
10 #include <io.h>
11 #else
12 #include <unistd.h>
13 #endif
14 #include <fcntl.h>
16 #include "base/compiler_specific.h"
17 #include "base/eintr_wrapper.h"
18 #include "base/file_util.h"
19 #include "media/ffmpeg/ffmpeg_common.h"
21 // warning C4996: 'open': The POSIX name for this item is deprecated.
22 MSVC_PUSH_DISABLE_WARNING(4996)
24 static int GetHandle(URLContext *h) {
25 return static_cast<int>(reinterpret_cast<intptr_t>(h->priv_data));
28 // FFmpeg protocol interface.
29 static int OpenContext(URLContext* h, const char* filename, int flags) {
30 int access = O_RDONLY;
32 if ((flags & AVIO_FLAG_WRITE) && (flags & AVIO_FLAG_READ)) {
33 access = O_CREAT | O_TRUNC | O_RDWR;
34 } else if (flags & AVIO_FLAG_WRITE) {
35 access = O_CREAT | O_TRUNC | O_WRONLY;
38 #ifdef O_BINARY
39 access |= O_BINARY;
40 #endif
42 int f = open(filename, access, 0666);
43 if (f == -1)
44 return AVERROR(ENOENT);
46 h->priv_data = reinterpret_cast<void*>(static_cast<intptr_t>(f));
47 h->is_streamed = false;
48 return 0;
51 static int ReadContext(URLContext* h, unsigned char* buf, int size) {
52 return HANDLE_EINTR(read(GetHandle(h), buf, size));
55 #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(52, 68, 0)
56 static int WriteContext(URLContext* h, const unsigned char* buf, int size) {
57 #else
58 static int WriteContext(URLContext* h, unsigned char* buf, int size) {
59 #endif
60 return HANDLE_EINTR(write(GetHandle(h), buf, size));
63 static int64 SeekContext(URLContext* h, int64 offset, int whence) {
64 #if defined(OS_WIN)
65 return _lseeki64(GetHandle(h), static_cast<__int64>(offset), whence);
66 #else
67 COMPILE_ASSERT(sizeof(off_t) == 8, off_t_not_64_bit);
68 return lseek(GetHandle(h), static_cast<off_t>(offset), whence);
69 #endif
72 static int CloseContext(URLContext* h) {
73 return HANDLE_EINTR(close(GetHandle(h)));
76 MSVC_POP_WARNING()
78 URLProtocol kFFmpegFileProtocol = {
79 "file",
80 &OpenContext,
81 NULL, // url_open2
82 &ReadContext,
83 &WriteContext,
84 &SeekContext,
85 &CloseContext,
86 NULL, // *next
87 NULL, // url_read_pause
88 NULL, // url_read_seek
89 &GetHandle