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 | |
| 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
| 23 | |
Pawin Vongmasa | 255735a | 2017-07-19 11:24:56 -0700 | [diff] [blame] | 24 | #include <media/MidiIoWrapper.h> |
Marco Nelissen | cec44d0 | 2018-06-17 22:21:09 -0700 | [diff] [blame] | 25 | #include <media/MediaExtractorPluginApi.h> |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 26 | |
| 27 | static int readAt(void *handle, void *buffer, int pos, int size) { |
| 28 | return ((android::MidiIoWrapper*)handle)->readAt(buffer, pos, size); |
| 29 | } |
| 30 | static int size(void *handle) { |
| 31 | return ((android::MidiIoWrapper*)handle)->size(); |
| 32 | } |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | MidiIoWrapper::MidiIoWrapper(const char *path) { |
| 37 | ALOGV("MidiIoWrapper(%s)", path); |
| 38 | mFd = open(path, O_RDONLY | O_LARGEFILE); |
| 39 | mBase = 0; |
| 40 | mLength = lseek(mFd, 0, SEEK_END); |
Dongwon Kang | 04ce77f | 2018-02-13 00:27:24 -0800 | [diff] [blame] | 41 | mDataSource = nullptr; |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | MidiIoWrapper::MidiIoWrapper(int fd, off64_t offset, int64_t size) { |
| 45 | ALOGV("MidiIoWrapper(fd=%d)", fd); |
Andy Hung | b20688e | 2015-12-04 17:20:50 -0800 | [diff] [blame] | 46 | mFd = fd < 0 ? -1 : dup(fd); |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 47 | mBase = offset; |
| 48 | mLength = size; |
Dongwon Kang | 04ce77f | 2018-02-13 00:27:24 -0800 | [diff] [blame] | 49 | mDataSource = nullptr; |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Harish Mahendrakar | c39661e | 2020-01-31 11:08:47 -0800 | [diff] [blame^] | 52 | class MidiIoWrapper::DataSourceUnwrapper { |
Marco Nelissen | cec44d0 | 2018-06-17 22:21:09 -0700 | [diff] [blame] | 53 | |
| 54 | public: |
| 55 | explicit DataSourceUnwrapper(CDataSource *csource) { |
| 56 | mSource = csource; |
| 57 | } |
Marco Nelissen | be9768e | 2018-12-19 13:10:35 -0800 | [diff] [blame] | 58 | |
| 59 | virtual ~DataSourceUnwrapper() {} |
| 60 | |
Marco Nelissen | cec44d0 | 2018-06-17 22:21:09 -0700 | [diff] [blame] | 61 | virtual status_t initCheck() const { return OK; } |
| 62 | |
| 63 | // Returns the number of bytes read, or -1 on failure. It's not an error if |
| 64 | // this returns zero; it just means the given offset is equal to, or |
| 65 | // beyond, the end of the source. |
| 66 | virtual ssize_t readAt(off64_t offset, void *data, size_t size) { |
| 67 | return mSource->readAt(mSource->handle, offset, data, size); |
| 68 | } |
| 69 | |
| 70 | // May return ERROR_UNSUPPORTED. |
| 71 | virtual status_t getSize(off64_t *size) { |
| 72 | return mSource->getSize(mSource->handle, size); |
| 73 | } |
| 74 | |
| 75 | virtual bool getUri(char * /*uriString*/, size_t /*bufferSize*/) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | virtual uint32_t flags() { |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | virtual void close() {}; |
| 84 | private: |
| 85 | CDataSource *mSource; |
| 86 | }; |
| 87 | |
| 88 | MidiIoWrapper::MidiIoWrapper(CDataSource *csource) { |
| 89 | ALOGV("MidiIoWrapper(CDataSource)"); |
| 90 | mFd = -1; |
Marco Nelissen | be9768e | 2018-12-19 13:10:35 -0800 | [diff] [blame] | 91 | mBase = 0; |
Marco Nelissen | cec44d0 | 2018-06-17 22:21:09 -0700 | [diff] [blame] | 92 | mDataSource = new DataSourceUnwrapper(csource); |
| 93 | off64_t l; |
| 94 | if (mDataSource->getSize(&l) == OK) { |
| 95 | mLength = l; |
| 96 | } else { |
| 97 | mLength = 0; |
| 98 | } |
| 99 | } |
| 100 | |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 101 | MidiIoWrapper::~MidiIoWrapper() { |
| 102 | ALOGV("~MidiIoWrapper"); |
Andy Hung | b20688e | 2015-12-04 17:20:50 -0800 | [diff] [blame] | 103 | if (mFd >= 0) { |
| 104 | close(mFd); |
| 105 | } |
Marco Nelissen | be9768e | 2018-12-19 13:10:35 -0800 | [diff] [blame] | 106 | delete mDataSource; |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | int MidiIoWrapper::readAt(void *buffer, int offset, int size) { |
| 110 | ALOGV("readAt(%p, %d, %d)", buffer, offset, size); |
Marco Nelissen | 0e8928b | 2015-01-08 13:40:53 -0800 | [diff] [blame] | 111 | |
| 112 | if (mDataSource != NULL) { |
| 113 | return mDataSource->readAt(offset, buffer, size); |
| 114 | } |
Andy Hung | b20688e | 2015-12-04 17:20:50 -0800 | [diff] [blame] | 115 | if (mFd < 0) { |
| 116 | errno = EBADF; |
| 117 | return -1; // as per failed read. |
| 118 | } |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 119 | lseek(mFd, mBase + offset, SEEK_SET); |
| 120 | if (offset + size > mLength) { |
| 121 | size = mLength - offset; |
| 122 | } |
| 123 | return read(mFd, buffer, size); |
| 124 | } |
| 125 | |
| 126 | int MidiIoWrapper::size() { |
Marco Nelissen | 13b97d6 | 2015-01-29 11:16:17 -0800 | [diff] [blame] | 127 | ALOGV("size() = %d", int(mLength)); |
Marco Nelissen | bc11e71 | 2015-01-08 12:26:36 -0800 | [diff] [blame] | 128 | return mLength; |
| 129 | } |
| 130 | |
| 131 | EAS_FILE_LOCATOR MidiIoWrapper::getLocator() { |
| 132 | mEasFile.handle = this; |
| 133 | mEasFile.readAt = ::readAt; |
| 134 | mEasFile.size = ::size; |
| 135 | return &mEasFile; |
| 136 | } |
| 137 | |
| 138 | } // namespace android |