blob: 4e5d67f2b029ffe8daaf65095062c7a4823572ba [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 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);
41}
42
43MidiIoWrapper::MidiIoWrapper(int fd, off64_t offset, int64_t size) {
44 ALOGV("MidiIoWrapper(fd=%d)", fd);
Andy Hungb20688e2015-12-04 17:20:50 -080045 mFd = fd < 0 ? -1 : dup(fd);
Marco Nelissenbc11e712015-01-08 12:26:36 -080046 mBase = offset;
47 mLength = size;
48}
49
Marco Nelissen0e8928b2015-01-08 13:40:53 -080050MidiIoWrapper::MidiIoWrapper(const sp<DataSource> &source) {
Marco Nelissen13b97d62015-01-29 11:16:17 -080051 ALOGV("MidiIoWrapper(DataSource)");
52 mFd = -1;
Marco Nelissen0e8928b2015-01-08 13:40:53 -080053 mDataSource = source;
54 off64_t l;
55 if (mDataSource->getSize(&l) == OK) {
56 mLength = l;
57 } else {
58 mLength = 0;
59 }
60}
61
Marco Nelissenbc11e712015-01-08 12:26:36 -080062MidiIoWrapper::~MidiIoWrapper() {
63 ALOGV("~MidiIoWrapper");
Andy Hungb20688e2015-12-04 17:20:50 -080064 if (mFd >= 0) {
65 close(mFd);
66 }
Marco Nelissenbc11e712015-01-08 12:26:36 -080067}
68
69int MidiIoWrapper::readAt(void *buffer, int offset, int size) {
70 ALOGV("readAt(%p, %d, %d)", buffer, offset, size);
Marco Nelissen0e8928b2015-01-08 13:40:53 -080071
72 if (mDataSource != NULL) {
73 return mDataSource->readAt(offset, buffer, size);
74 }
Andy Hungb20688e2015-12-04 17:20:50 -080075 if (mFd < 0) {
76 errno = EBADF;
77 return -1; // as per failed read.
78 }
Marco Nelissenbc11e712015-01-08 12:26:36 -080079 lseek(mFd, mBase + offset, SEEK_SET);
80 if (offset + size > mLength) {
81 size = mLength - offset;
82 }
83 return read(mFd, buffer, size);
84}
85
86int MidiIoWrapper::size() {
Marco Nelissen13b97d62015-01-29 11:16:17 -080087 ALOGV("size() = %d", int(mLength));
Marco Nelissenbc11e712015-01-08 12:26:36 -080088 return mLength;
89}
90
91EAS_FILE_LOCATOR MidiIoWrapper::getLocator() {
92 mEasFile.handle = this;
93 mEasFile.readAt = ::readAt;
94 mEasFile.size = ::size;
95 return &mEasFile;
96}
97
98} // namespace android