| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/bench/file_protocol.h" | 5 #include "media/bench/file_protocol.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include "build/build_config.h" |
| 8 |
| 9 #if defined(OS_WIN) |
| 10 #include <io.h> |
| 11 #else |
| 12 #include <unistd.h> |
| 13 #endif |
| 14 #include <fcntl.h> |
| 15 |
| 16 #include "base/compiler_specific.h" |
| 8 #include "base/file_util.h" | 17 #include "base/file_util.h" |
| 9 #include "base/logging.h" | 18 #include "base/logging.h" |
| 10 #include "media/filters/ffmpeg_common.h" | 19 #include "media/filters/ffmpeg_common.h" |
| 11 | 20 |
| 21 // warning C4996: 'open': The POSIX name for this item is deprecated. |
| 22 MSVC_PUSH_DISABLE_WARNING(4996) |
| 23 |
| 12 namespace { | 24 namespace { |
| 13 | 25 |
| 14 FILE* ToFile(void* data) { | 26 int GetHandle(URLContext *h) { |
| 15 return reinterpret_cast<FILE*>(data); | 27 return static_cast<int>(reinterpret_cast<intptr_t>(h->priv_data)); |
| 16 } | 28 } |
| 17 | 29 |
| 18 // FFmpeg protocol interface. | 30 // FFmpeg protocol interface. |
| 19 int OpenContext(URLContext* h, const char* filename, int flags) { | 31 int OpenContext(URLContext* h, const char* filename, int flags) { |
| 20 FILE* file = file_util::OpenFile(filename, "rb"); | 32 int access = O_RDONLY; |
| 21 if (!file) | 33 if (flags & URL_RDWR) { |
| 22 return AVERROR_IO; | 34 access = O_CREAT | O_TRUNC | O_RDWR; |
| 23 | 35 } else if (flags & URL_WRONLY) { |
| 24 h->priv_data = file; | 36 access = O_CREAT | O_TRUNC | O_WRONLY; |
| 25 h->flags = URL_RDONLY; | 37 } |
| 38 #ifdef O_BINARY |
| 39 access |= O_BINARY; |
| 40 #endif |
| 41 int f = open(filename, access, 0666); |
| 42 if (f == -1) |
| 43 return AVERROR(ENOENT); |
| 44 h->priv_data = reinterpret_cast<void*>(static_cast<intptr_t>(f)); |
| 26 h->is_streamed = false; | 45 h->is_streamed = false; |
| 27 return 0; | 46 return 0; |
| 28 } | 47 } |
| 29 | 48 |
| 30 int ReadContext(URLContext* h, unsigned char* buf, int size) { | 49 int ReadContext(URLContext* h, unsigned char* buf, int size) { |
| 31 return fread(buf, 1, size, ToFile(h->priv_data)); | 50 return read(GetHandle(h), buf, size); |
| 32 } | 51 } |
| 33 | 52 |
| 34 int WriteContext(URLContext* h, unsigned char* buf, int size) { | 53 int WriteContext(URLContext* h, unsigned char* buf, int size) { |
| 35 NOTIMPLEMENTED(); | 54 return write(GetHandle(h), buf, size); |
| 36 return AVERROR_IO; | |
| 37 } | 55 } |
| 38 | 56 |
| 39 offset_t SeekContext(URLContext* h, offset_t offset, int whence) { | 57 offset_t SeekContext(URLContext* h, offset_t offset, int whence) { |
| 40 #if defined(OS_WIN) | 58 #if defined(OS_WIN) |
| 41 return static_cast<offset_t> (_fseeki64(ToFile(h->priv_data), | 59 return lseek(GetHandle(h), static_cast<long>(offset), whence); |
| 42 static_cast<int64>(offset), | |
| 43 whence)); | |
| 44 #else | 60 #else |
| 45 return fseek(ToFile(h->priv_data), offset, whence); | 61 return lseek(GetHandle(h), offset, whence); |
| 46 #endif | 62 #endif |
| 47 } | 63 } |
| 48 | 64 |
| 49 int CloseContext(URLContext* h) { | 65 int CloseContext(URLContext* h) { |
| 50 if (file_util::CloseFile(ToFile(h->priv_data))) | 66 return close(GetHandle(h)); |
| 51 return 0; | |
| 52 return AVERROR_IO; | |
| 53 } | 67 } |
| 54 | 68 |
| 55 } // namespace | 69 } // namespace |
| 56 | 70 |
| 71 MSVC_POP_WARNING() |
| 72 |
| 57 URLProtocol kFFmpegFileProtocol = { | 73 URLProtocol kFFmpegFileProtocol = { |
| 58 "file", | 74 "file", |
| 59 &OpenContext, | 75 &OpenContext, |
| 60 &ReadContext, | 76 &ReadContext, |
| 61 &WriteContext, | 77 &WriteContext, |
| 62 &SeekContext, | 78 &SeekContext, |
| 63 &CloseContext, | 79 &CloseContext, |
| 80 NULL, // *next |
| 81 NULL, // url_read_pause |
| 82 NULL, // url_read_seek |
| 83 &GetHandle |
| 64 }; | 84 }; |
| 85 |
| OLD | NEW |