blob: d8ef9cfd563a84101b55531e5e438b6433549151 [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 Nelissencec44d02018-06-17 22:21:09 -070053class DataSourceUnwrapper : public DataSourceBase {
54
55public:
56 explicit DataSourceUnwrapper(CDataSource *csource) {
57 mSource = csource;
58 }
Marco Nelissenbe9768e2018-12-19 13:10:35 -080059
60 virtual ~DataSourceUnwrapper() {}
61
Marco Nelissencec44d02018-06-17 22:21:09 -070062 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() {};
85private:
86 CDataSource *mSource;
87};
88
89MidiIoWrapper::MidiIoWrapper(CDataSource *csource) {
90 ALOGV("MidiIoWrapper(CDataSource)");
91 mFd = -1;
Marco Nelissenbe9768e2018-12-19 13:10:35 -080092 mBase = 0;
Marco Nelissencec44d02018-06-17 22:21:09 -070093 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 Nelissenbc11e712015-01-08 12:26:36 -0800102MidiIoWrapper::~MidiIoWrapper() {
103 ALOGV("~MidiIoWrapper");
Andy Hungb20688e2015-12-04 17:20:50 -0800104 if (mFd >= 0) {
105 close(mFd);
106 }
Marco Nelissenbe9768e2018-12-19 13:10:35 -0800107 delete mDataSource;
Marco Nelissenbc11e712015-01-08 12:26:36 -0800108}
109
110int MidiIoWrapper::readAt(void *buffer, int offset, int size) {
111 ALOGV("readAt(%p, %d, %d)", buffer, offset, size);
Marco Nelissen0e8928b2015-01-08 13:40:53 -0800112
113 if (mDataSource != NULL) {
114 return mDataSource->readAt(offset, buffer, size);
115 }
Andy Hungb20688e2015-12-04 17:20:50 -0800116 if (mFd < 0) {
117 errno = EBADF;
118 return -1; // as per failed read.
119 }
Marco Nelissenbc11e712015-01-08 12:26:36 -0800120 lseek(mFd, mBase + offset, SEEK_SET);
121 if (offset + size > mLength) {
122 size = mLength - offset;
123 }
124 return read(mFd, buffer, size);
125}
126
127int MidiIoWrapper::size() {
Marco Nelissen13b97d62015-01-29 11:16:17 -0800128 ALOGV("size() = %d", int(mLength));
Marco Nelissenbc11e712015-01-08 12:26:36 -0800129 return mLength;
130}
131
132EAS_FILE_LOCATOR MidiIoWrapper::getLocator() {
133 mEasFile.handle = this;
134 mEasFile.readAt = ::readAt;
135 mEasFile.size = ::size;
136 return &mEasFile;
137}
138
139} // namespace android