blob: e71ea2c9f6223471eadf76c60c69d5664af4b58f [file] [log] [blame]
Marco Nelissenbc11e712015-01-08 12:26:36 -08001/*
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 Nelissenbc11e712015-01-08 12:26:36 -080020
21#include <sys/stat.h>
22#include <fcntl.h>
23
Pawin Vongmasa255735a2017-07-19 11:24:56 -070024#include <media/MidiIoWrapper.h>
Marco Nelissencec44d02018-06-17 22:21:09 -070025#include <media/MediaExtractorPluginApi.h>
Marco Nelissenbc11e712015-01-08 12:26:36 -080026
27static int readAt(void *handle, void *buffer, int pos, int size) {
28 return ((android::MidiIoWrapper*)handle)->readAt(buffer, pos, size);
29}
30static int size(void *handle) {
31 return ((android::MidiIoWrapper*)handle)->size();
32}
33
34namespace android {
35
36MidiIoWrapper::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 Kang04ce77f2018-02-13 00:27:24 -080041 mDataSource = nullptr;
Marco Nelissenbc11e712015-01-08 12:26:36 -080042}
43
44MidiIoWrapper::MidiIoWrapper(int fd, off64_t offset, int64_t size) {
45 ALOGV("MidiIoWrapper(fd=%d)", fd);
Andy Hungb20688e2015-12-04 17:20:50 -080046 mFd = fd < 0 ? -1 : dup(fd);
Marco Nelissenbc11e712015-01-08 12:26:36 -080047 mBase = offset;
48 mLength = size;
Dongwon Kang04ce77f2018-02-13 00:27:24 -080049 mDataSource = nullptr;
Marco Nelissenbc11e712015-01-08 12:26:36 -080050}
51
Harish Mahendrakarc39661e2020-01-31 11:08:47 -080052class MidiIoWrapper::DataSourceUnwrapper {
Marco Nelissencec44d02018-06-17 22:21:09 -070053
54public:
55 explicit DataSourceUnwrapper(CDataSource *csource) {
56 mSource = csource;
57 }
Marco Nelissenbe9768e2018-12-19 13:10:35 -080058
59 virtual ~DataSourceUnwrapper() {}
60
Marco Nelissencec44d02018-06-17 22:21:09 -070061 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() {};
84private:
85 CDataSource *mSource;
86};
87
88MidiIoWrapper::MidiIoWrapper(CDataSource *csource) {
89 ALOGV("MidiIoWrapper(CDataSource)");
90 mFd = -1;
Marco Nelissenbe9768e2018-12-19 13:10:35 -080091 mBase = 0;
Marco Nelissencec44d02018-06-17 22:21:09 -070092 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 Nelissenbc11e712015-01-08 12:26:36 -0800101MidiIoWrapper::~MidiIoWrapper() {
102 ALOGV("~MidiIoWrapper");
Andy Hungb20688e2015-12-04 17:20:50 -0800103 if (mFd >= 0) {
104 close(mFd);
105 }
Marco Nelissenbe9768e2018-12-19 13:10:35 -0800106 delete mDataSource;
Marco Nelissenbc11e712015-01-08 12:26:36 -0800107}
108
109int MidiIoWrapper::readAt(void *buffer, int offset, int size) {
110 ALOGV("readAt(%p, %d, %d)", buffer, offset, size);
Marco Nelissen0e8928b2015-01-08 13:40:53 -0800111
112 if (mDataSource != NULL) {
113 return mDataSource->readAt(offset, buffer, size);
114 }
Andy Hungb20688e2015-12-04 17:20:50 -0800115 if (mFd < 0) {
116 errno = EBADF;
117 return -1; // as per failed read.
118 }
Marco Nelissenbc11e712015-01-08 12:26:36 -0800119 lseek(mFd, mBase + offset, SEEK_SET);
120 if (offset + size > mLength) {
121 size = mLength - offset;
122 }
123 return read(mFd, buffer, size);
124}
125
126int MidiIoWrapper::size() {
Marco Nelissen13b97d62015-01-29 11:16:17 -0800127 ALOGV("size() = %d", int(mLength));
Marco Nelissenbc11e712015-01-08 12:26:36 -0800128 return mLength;
129}
130
131EAS_FILE_LOCATOR MidiIoWrapper::getLocator() {
132 mEasFile.handle = this;
133 mEasFile.readAt = ::readAt;
134 mEasFile.size = ::size;
135 return &mEasFile;
136}
137
138} // namespace android