| 1 | // Copyright (c) 2014 Thomas Goyne <[email protected]> |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | #include "filehandle.h" |
| 22 | |
| 23 | #include "utils.h" |
| 24 | |
| 25 | #include <cstdarg> |
| 26 | |
| 27 | extern "C" { |
| 28 | #include <libavformat/avio.h> |
| 29 | } |
| 30 | |
| 31 | static AVIOContext *ffms_fopen(const char *filename, const char *mode) { |
| 32 | int flags = 0; |
| 33 | if (strchr(mode, 'r')) |
| 34 | flags |= AVIO_FLAG_READ; |
| 35 | if (strchr(mode, 'w')) |
| 36 | flags |= AVIO_FLAG_WRITE; |
| 37 | |
| 38 | AVIOContext *ctx; |
| 39 | int ret = avio_open2(&ctx, filename, flags, nullptr, nullptr); |
| 40 | if (ret < 0) |
| 41 | return nullptr; |
| 42 | return ctx; |
| 43 | } |
| 44 | |
| 45 | FileHandle::FileHandle(const char *filename, const char *mode, int error_source, int error_cause) |
| 46 | : avio(ffms_fopen(filename, mode)) |
| 47 | , filename(filename) |
| 48 | , error_source(error_source) |
| 49 | , error_cause(error_cause) { |
| 50 | if (!avio) |
| 51 | throw FFMS_Exception(error_source, FFMS_ERROR_NO_FILE, |
| 52 | "Failed to open '" + this->filename + "'"); |
| 53 | } |
| 54 | |
| 55 | FileHandle::~FileHandle() { |
| 56 | avio_close(avio); |
| 57 | } |
| 58 | |
| 59 | void FileHandle::Seek(int64_t offset, int origin) { |
| 60 | int64_t ret = avio_seek(avio, offset, origin); |
| 61 | if (ret < 0) |
| 62 | throw FFMS_Exception(error_source, error_cause, |
| 63 | "Failed to seek in '" + filename + "': " + AVErrorToString(ret)); |
| 64 | } |
| 65 | |
| 66 | int64_t FileHandle::Tell() { |
| 67 | int64_t ret = avio_tell(avio); |
| 68 | if (ret < 0) |
| 69 | throw FFMS_Exception(error_source, error_cause, |
| 70 | "Failed to read position in '" + filename + "': " + AVErrorToString(ret)); |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | size_t FileHandle::Read(char *buffer, size_t size) { |
| 75 | int count = avio_read(avio, (unsigned char *)buffer, size); |
| 76 | if (count < 0) { |
| 77 | throw FFMS_Exception(error_source, FFMS_ERROR_FILE_READ, |
| 78 | "Failed to read from '" + filename + "': " + AVErrorToString(count)); |
| 79 | } else if (avio_feof(avio)) { |
| 80 | // "Similar to feof() but also returns nonzero on read errors" -- FFmpeg docs |
| 81 | if (avio->error != 0 && avio->error != AVERROR_EOF) { |
| 82 | throw FFMS_Exception(error_source, FFMS_ERROR_FILE_READ, |
| 83 | "Failed to read from '" + filename + "': " + AVErrorToString(avio->error)); |
| 84 | } |
| 85 | } |
| 86 | return (size_t)count; |
| 87 | } |
| 88 | |
| 89 | size_t FileHandle::Write(const char *buffer, size_t size) { |
| 90 | avio_write(avio, (const unsigned char *)buffer, size); |
| 91 | avio_flush(avio); |
| 92 | if (avio->error < 0) |
| 93 | throw FFMS_Exception(error_source, FFMS_ERROR_FILE_WRITE, |
| 94 | "Failed to write to '" + filename + "': " + AVErrorToString(avio->error)); |
| 95 | return size; |
| 96 | } |
| 97 | |
| 98 | int64_t FileHandle::Size() { |
| 99 | int64_t size = avio_size(avio); |
| 100 | if (size < 0) |
| 101 | throw FFMS_Exception(error_source, FFMS_ERROR_FILE_READ, |
| 102 | "Failed to get file size for '" + filename + "': " + AVErrorToString(size)); |
| 103 | return size; |
| 104 | } |
| 105 | |
| 106 | int FileHandle::Printf(const char *fmt, ...) { |
| 107 | va_list args; |
| 108 | va_start(args, fmt); |
| 109 | |
| 110 | std::vector<char> OutBuffer(100); |
| 111 | int ret = -1; |
| 112 | while (OutBuffer.size() < 1024 * 1024) { |
| 113 | ret = vsnprintf(OutBuffer.data(), OutBuffer.size(), fmt, args); |
| 114 | if (ret > 0 && ret < (int)OutBuffer.size()) |
| 115 | break; |
| 116 | OutBuffer.resize(OutBuffer.size() * 2); |
| 117 | } |
| 118 | |
| 119 | va_end(args); |
| 120 | |
| 121 | avio_write(avio, reinterpret_cast<const unsigned char *>(OutBuffer.data()), ret); |
| 122 | avio_flush(avio); |
| 123 | |
| 124 | return avio->error < 0 ? avio->error : ret; |
| 125 | } |