1 /*
2 * Copyright 2015 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 "CallbackDataSource"
19 #include <utils/Log.h>
20
21 #include "include/CallbackDataSource.h"
22
23 #include <android/IDataSource.h>
24 #include <binder/IMemory.h>
25 #include <binder/IPCThreadState.h>
26 #include <media/stagefright/foundation/ADebug.h>
27
28 #include <algorithm>
29
30 namespace android {
31
CallbackDataSource(const sp<IDataSource> & binderDataSource)32 CallbackDataSource::CallbackDataSource(
33 const sp<IDataSource>& binderDataSource)
34 : mIDataSource(binderDataSource),
35 mIsClosed(false) {
36 // Set up the buffer to read into.
37 mMemory = mIDataSource->getIMemory();
38 mName = String8::format("CallbackDataSource(%d->%d, %s)",
39 getpid(),
40 IPCThreadState::self()->getCallingPid(),
41 mIDataSource->toString().c_str());
42
43 }
44
~CallbackDataSource()45 CallbackDataSource::~CallbackDataSource() {
46 ALOGV("~CallbackDataSource");
47 close();
48 }
49
initCheck() const50 status_t CallbackDataSource::initCheck() const {
51 if (mMemory == NULL) {
52 return UNKNOWN_ERROR;
53 }
54 return OK;
55 }
56
readAt(off64_t offset,void * data,size_t size)57 ssize_t CallbackDataSource::readAt(off64_t offset, void* data, size_t size) {
58 if (mMemory == NULL || data == NULL) {
59 return -1;
60 }
61
62 // IDataSource can only read up to mMemory->size() bytes at a time, but this
63 // method should be able to read any number of bytes, so read in a loop.
64 size_t totalNumRead = 0;
65 size_t numLeft = size;
66 const size_t bufferSize = mMemory->size();
67
68 while (numLeft > 0) {
69 size_t numToRead = std::min(numLeft, bufferSize);
70 ssize_t numRead =
71 mIDataSource->readAt(offset + totalNumRead, numToRead);
72 // A negative return value represents an error. Pass it on.
73 if (numRead < 0) {
74 return numRead == ERROR_END_OF_STREAM && totalNumRead > 0 ? totalNumRead : numRead;
75 }
76 // A zero return value signals EOS. Return the bytes read so far.
77 if (numRead == 0) {
78 return totalNumRead;
79 }
80 if ((size_t)numRead > numToRead) {
81 return ERROR_OUT_OF_RANGE;
82 }
83 CHECK(numRead >= 0 && (size_t)numRead <= bufferSize);
84 memcpy(((uint8_t*)data) + totalNumRead, mMemory->unsecurePointer(),
85 numRead);
86 numLeft -= numRead;
87 totalNumRead += numRead;
88 }
89
90 return totalNumRead;
91 }
92
getSize(off64_t * size)93 status_t CallbackDataSource::getSize(off64_t *size) {
94 status_t err = mIDataSource->getSize(size);
95 if (err != OK) {
96 return err;
97 }
98 if (*size < 0) {
99 // IDataSource will set size to -1 to indicate unknown size, but
100 // DataSource returns ERROR_UNSUPPORTED for that.
101 return ERROR_UNSUPPORTED;
102 }
103 return OK;
104 }
105
flags()106 uint32_t CallbackDataSource::flags() {
107 return mIDataSource->getFlags();
108 }
109
close()110 void CallbackDataSource::close() {
111 if (!mIsClosed) {
112 mIDataSource->close();
113 mIsClosed = true;
114 }
115 }
116
getIDataSource() const117 sp<IDataSource> CallbackDataSource::getIDataSource() const {
118 return mIDataSource;
119 }
120
TinyCacheSource(const sp<DataSource> & source)121 TinyCacheSource::TinyCacheSource(const sp<DataSource>& source)
122 : mSource(source), mCachedOffset(0), mCachedSize(0) {
123 mName = String8::format("TinyCacheSource(%s)", mSource->toString().c_str());
124 }
125
initCheck() const126 status_t TinyCacheSource::initCheck() const {
127 return mSource->initCheck();
128 }
129
readAt(off64_t offset,void * data,size_t size)130 ssize_t TinyCacheSource::readAt(off64_t offset, void* data, size_t size) {
131 // Check if the cache satisfies the read.
132 if (mCachedOffset <= offset
133 && offset < (off64_t) (mCachedOffset + mCachedSize)) {
134 if (offset + size <= mCachedOffset + mCachedSize) {
135 memcpy(data, &mCache[offset - mCachedOffset], size);
136 return size;
137 } else {
138 // If the cache hits only partially, flush the cache and read the
139 // remainder.
140
141 // This value is guaranteed to be greater than 0 because of the
142 // enclosing if statement.
143 const ssize_t remaining = mCachedOffset + mCachedSize - offset;
144 memcpy(data, &mCache[offset - mCachedOffset], remaining);
145 const ssize_t readMore = readAt(offset + remaining,
146 (uint8_t*)data + remaining, size - remaining);
147 if (readMore < 0) {
148 return readMore;
149 }
150 return remaining + readMore;
151 }
152 }
153
154 if (size >= kCacheSize) {
155 return mSource->readAt(offset, data, size);
156 }
157
158 // Fill the cache and copy to the caller.
159 const ssize_t numRead = mSource->readAt(offset, mCache, kCacheSize);
160 if (numRead <= 0) {
161 // Flush cache on error
162 mCachedSize = 0;
163 mCachedOffset = 0;
164 return numRead;
165 }
166 if ((size_t)numRead > kCacheSize) {
167 // Flush cache on error
168 mCachedSize = 0;
169 mCachedOffset = 0;
170 return ERROR_OUT_OF_RANGE;
171 }
172
173 mCachedSize = numRead;
174 mCachedOffset = offset;
175 CHECK(mCachedSize <= kCacheSize && mCachedOffset >= 0);
176 const size_t numToReturn = std::min(size, (size_t)numRead);
177 memcpy(data, mCache, numToReturn);
178
179 return numToReturn;
180 }
181
getSize(off64_t * size)182 status_t TinyCacheSource::getSize(off64_t *size) {
183 return mSource->getSize(size);
184 }
185
flags()186 uint32_t TinyCacheSource::flags() {
187 return mSource->flags();
188 }
189
getIDataSource() const190 sp<IDataSource> TinyCacheSource::getIDataSource() const {
191 return mSource->getIDataSource();
192 }
193
194 } // namespace android
195