Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "MidiIoWrapper" |
| 19 | #include <utils/Log.h> |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 20 | |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
Tom Cherry | 7ae7825 | 2020-04-13 15:20:50 -0700 | [diff] [blame^] | 22 | #include <sys/stat.h> |
| 23 | #include <unistd.h> |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 24 | |
Pawin Vongmasa | 255735a | 2017-07-19 11:24:56 -0700 | [diff] [blame] | 25 | #include <media/MidiIoWrapper.h> |
Marco Nelissen | cec44d0 | 2018-06-17 22:21:09 -0700 | [diff] [blame] | 26 | #include <media/MediaExtractorPluginApi.h> |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 27 | |
| 28 | static int readAt(void *handle, void *buffer, int pos, int size) { |
| 29 | return ((android::MidiIoWrapper*)handle)->readAt(buffer, pos, size); |
| 30 | } |
| 31 | static int size(void *handle) { |
| 32 | return ((android::MidiIoWrapper*)handle)->size(); |
| 33 | } |
| 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | MidiIoWrapper::MidiIoWrapper(const char *path) { |
| 38 | ALOGV("MidiIoWrapper(%s)", path); |
| 39 | mFd = open(path, O_RDONLY | O_LARGEFILE); |
| 40 | mBase = 0; |
| 41 | mLength = lseek(mFd, 0, SEEK_END); |
Dongwon Kang | 04ce77f | 2018-02-13 00:27:24 -0800 | [diff] [blame] | 42 | mDataSource = nullptr; |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | MidiIoWrapper::MidiIoWrapper(int fd, off64_t offset, int64_t size) { |
| 46 | ALOGV("MidiIoWrapper(fd=%d)", fd); |
Andy Hung | b20688e | 2015-12-04 17:20:50 -0800 | [diff] [blame] | 47 | mFd = fd < 0 ? -1 : dup(fd); |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 48 | mBase = offset; |
| 49 | mLength = size; |
Dongwon Kang | 04ce77f | 2018-02-13 00:27:24 -0800 | [diff] [blame] | 50 | mDataSource = nullptr; |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Harish Mahendrakar | c39661e | 2020-01-31 11:08:47 -0800 | [diff] [blame] | 53 | class MidiIoWrapper::DataSourceUnwrapper { |
Marco Nelissen | cec44d0 | 2018-06-17 22:21:09 -0700 | [diff] [blame] | 54 | |
| 55 | public: |
| 56 | explicit DataSourceUnwrapper(CDataSource *csource) { |
| 57 | mSource = csource; |
| 58 | } |
Marco Nelissen | be9768e | 2018-12-19 13:10:35 -0800 | [diff] [blame] | 59 | |
| 60 | virtual ~DataSourceUnwrapper() {} |
| 61 | |
Marco Nelissen | cec44d0 | 2018-06-17 22:21:09 -0700 | [diff] [blame] | 62 | virtual status_t initCheck() const { return OK; } |
| 63 | |
| 64 | // Returns the number of bytes read, or -1 on failure. It's not an error if |
| 65 | // this returns zero; it just means the given offset is equal to, or |
| 66 | // beyond, the end of the source. |
| 67 | virtual ssize_t readAt(off64_t offset, void *data, size_t size) { |
| 68 | return mSource->readAt(mSource->handle, offset, data, size); |
| 69 | } |
| 70 | |
| 71 | // May return ERROR_UNSUPPORTED. |
| 72 | virtual status_t getSize(off64_t *size) { |
| 73 | return mSource->getSize(mSource->handle, size); |
| 74 | } |
| 75 | |
| 76 | virtual bool getUri(char * /*uriString*/, size_t /*bufferSize*/) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | virtual uint32_t flags() { |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | virtual void close() {}; |
| 85 | private: |
| 86 | CDataSource *mSource; |
| 87 | }; |
| 88 | |
| 89 | MidiIoWrapper::MidiIoWrapper(CDataSource *csource) { |
| 90 | ALOGV("MidiIoWrapper(CDataSource)"); |
| 91 | mFd = -1; |
Marco Nelissen | be9768e | 2018-12-19 13:10:35 -0800 | [diff] [blame] | 92 | mBase = 0; |
Marco Nelissen | cec44d0 | 2018-06-17 22:21:09 -0700 | [diff] [blame] | 93 | mDataSource = new DataSourceUnwrapper(csource); |
| 94 | off64_t l; |
| 95 | if (mDataSource->getSize(&l) == OK) { |
| 96 | mLength = l; |
| 97 | } else { |
| 98 | mLength = 0; |
| 99 | } |
| 100 | } |
| 101 | |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 102 | MidiIoWrapper::~MidiIoWrapper() { |
| 103 | ALOGV("~MidiIoWrapper"); |
Andy Hung | b20688e | 2015-12-04 17:20:50 -0800 | [diff] [blame] | 104 | if (mFd >= 0) { |
| 105 | close(mFd); |
| 106 | } |
Marco Nelissen | be9768e | 2018-12-19 13:10:35 -0800 | [diff] [blame] | 107 | delete mDataSource; |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | int MidiIoWrapper::readAt(void *buffer, int offset, int size) { |
| 111 | ALOGV("readAt(%p, %d, %d)", buffer, offset, size); |
Marco Nelissen | 0e8928b | 2015-01-08 13:40:53 -0800 | [diff] [blame] | 112 | |
| 113 | if (mDataSource != NULL) { |
| 114 | return mDataSource->readAt(offset, buffer, size); |
| 115 | } |
Andy Hung | b20688e | 2015-12-04 17:20:50 -0800 | [diff] [blame] | 116 | if (mFd < 0) { |
| 117 | errno = EBADF; |
| 118 | return -1; // as per failed read. |
| 119 | } |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 120 | lseek(mFd, mBase + offset, SEEK_SET); |
| 121 | if (offset + size > mLength) { |
| 122 | size = mLength - offset; |
| 123 | } |
| 124 | return read(mFd, buffer, size); |
| 125 | } |
| 126 | |
| 127 | int MidiIoWrapper::size() { |
Marco Nelissen | 13b97d6 | 2015-01-29 11:16:17 -0800 | [diff] [blame] | 128 | ALOGV("size() = %d", int(mLength)); |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 129 | return mLength; |
| 130 | } |
| 131 | |
| 132 | EAS_FILE_LOCATOR MidiIoWrapper::getLocator() { |
| 133 | mEasFile.handle = this; |
| 134 | mEasFile.readAt = ::readAt; |
| 135 | mEasFile.size = ::size; |
| 136 | return &mEasFile; |
| 137 | } |
| 138 | |
| 139 | } // namespace android |