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"
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
;
42 int f
= open(filename
, access
, 0666);
44 return AVERROR(ENOENT
);
46 h
->priv_data
= reinterpret_cast<void*>(static_cast<intptr_t>(f
));
47 h
->is_streamed
= false;
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
) {
58 static int WriteContext(URLContext
* h
, unsigned char* buf
, int size
) {
60 return HANDLE_EINTR(write(GetHandle(h
), buf
, size
));
63 static int64
SeekContext(URLContext
* h
, int64 offset
, int whence
) {
65 return _lseeki64(GetHandle(h
), static_cast<__int64
>(offset
), whence
);
67 COMPILE_ASSERT(sizeof(off_t
) == 8, off_t_not_64_bit
);
68 return lseek(GetHandle(h
), static_cast<off_t
>(offset
), whence
);
72 static int CloseContext(URLContext
* h
) {
73 return HANDLE_EINTR(close(GetHandle(h
)));
78 URLProtocol kFFmpegFileProtocol
= {
87 NULL
, // url_read_pause
88 NULL
, // url_read_seek