blob: 1150d61c77229a3f08aa55c7722e760a1948fba0 [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>
20#include <utils/RefBase.h>
21
22#include <sys/stat.h>
23#include <fcntl.h>
24
Pawin Vongmasa255735a2017-07-19 11:24:56 -070025#include <media/MidiIoWrapper.h>
Marco Nelissencec44d02018-06-17 22:21:09 -070026#include <media/MediaExtractorPluginApi.h>
Marco Nelissenbc11e712015-01-08 12:26:36 -080027
28static int readAt(void *handle, void *buffer, int pos, int size) {
29 return ((android::MidiIoWrapper*)handle)->readAt(buffer, pos, size);
30}
31static int size(void *handle) {
32 return ((android::MidiIoWrapper*)handle)->size();
33}
34
35namespace android {
36
37MidiIoWrapper::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 Kang04ce77f2018-02-13 00:27:24 -080042 mDataSource = nullptr;
Marco Nelissenbc11e712015-01-08 12:26:36 -080043}
44
45MidiIoWrapper::MidiIoWrapper(int fd, off64_t offset, int64_t size) {
46 ALOGV("MidiIoWrapper(fd=%d)", fd);
Andy Hungb20688e2015-12-04 17:20:50 -080047 mFd = fd < 0 ? -1 : dup(fd);
Marco Nelissenbc11e712015-01-08 12:26:36 -080048 mBase = offset;
49 mLength = size;
Dongwon Kang04ce77f2018-02-13 00:27:24 -080050 mDataSource = nullptr;
Marco Nelissenbc11e712015-01-08 12:26:36 -080051}
52
Marco Nelissen2a243f02018-01-30 08:29:57 -080053MidiIoWrapper::MidiIoWrapper(DataSourceBase *source) {
Marco Nelissen13b97d62015-01-29 11:16:17 -080054 ALOGV("MidiIoWrapper(DataSource)");
55 mFd = -1;
Marco Nelissen0e8928b2015-01-08 13:40:53 -080056 mDataSource = source;
57 off64_t l;
58 if (mDataSource->getSize(&l) == OK) {
59 mLength = l;
60 } else {
61 mLength = 0;
62 }
63}
64
Marco Nelissencec44d02018-06-17 22:21:09 -070065class DataSourceUnwrapper : public DataSourceBase {
66
67public:
68 explicit DataSourceUnwrapper(CDataSource *csource) {
69 mSource = csource;
70 }
71 virtual status_t initCheck() const { return OK; }
72
73 // Returns the number of bytes read, or -1 on failure. It's not an error if
74 // this returns zero; it just means the given offset is equal to, or
75 // beyond, the end of the source.
76 virtual ssize_t readAt(off64_t offset, void *data, size_t size) {
77 return mSource->readAt(mSource->handle, offset, data, size);
78 }
79
80 // May return ERROR_UNSUPPORTED.
81 virtual status_t getSize(off64_t *size) {
82 return mSource->getSize(mSource->handle, size);
83 }
84
85 virtual bool getUri(char * /*uriString*/, size_t /*bufferSize*/) {
86 return false;
87 }
88
89 virtual uint32_t flags() {
90 return 0;
91 }
92
93 virtual void close() {};
94private:
95 CDataSource *mSource;
96};
97
98MidiIoWrapper::MidiIoWrapper(CDataSource *csource) {
99 ALOGV("MidiIoWrapper(CDataSource)");
100 mFd = -1;
101 mDataSource = new DataSourceUnwrapper(csource);
102 off64_t l;
103 if (mDataSource->getSize(&l) == OK) {
104 mLength = l;
105 } else {
106 mLength = 0;
107 }
108}
109
Marco Nelissenbc11e712015-01-08 12:26:36 -0800110MidiIoWrapper::~MidiIoWrapper() {
111 ALOGV("~MidiIoWrapper");
Andy Hungb20688e2015-12-04 17:20:50 -0800112 if (mFd >= 0) {
113 close(mFd);
114 }
Marco Nelissenbc11e712015-01-08 12:26:36 -0800115}
116
117int MidiIoWrapper::readAt(void *buffer, int offset, int size) {
118 ALOGV("readAt(%p, %d, %d)", buffer, offset, size);
Marco Nelissen0e8928b2015-01-08 13:40:53 -0800119
120 if (mDataSource != NULL) {
121 return mDataSource->readAt(offset, buffer, size);
122 }
Andy Hungb20688e2015-12-04 17:20:50 -0800123 if (mFd < 0) {
124 errno = EBADF;
125 return -1; // as per failed read.
126 }
Marco Nelissenbc11e712015-01-08 12:26:36 -0800127 lseek(mFd, mBase + offset, SEEK_SET);
128 if (offset + size > mLength) {
129 size = mLength - offset;
130 }
131 return read(mFd, buffer, size);
132}
133
134int MidiIoWrapper::size() {
Marco Nelissen13b97d62015-01-29 11:16:17 -0800135 ALOGV("size() = %d", int(mLength));
Marco Nelissenbc11e712015-01-08 12:26:36 -0800136 return mLength;
137}
138
139EAS_FILE_LOCATOR MidiIoWrapper::getLocator() {
140 mEasFile.handle = this;
141 mEasFile.readAt = ::readAt;
142 mEasFile.size = ::size;
143 return &mEasFile;
144}
145
146} // namespace android