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 <binder/IMemory.h>
24 #include <binder/IPCThreadState.h>
25 #include <media/IDataSource.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().string());
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->pointer(), numRead);
85 numLeft -= numRead;
86 totalNumRead += numRead;
87 }
88
89 return totalNumRead;
90 }
91
getSize(off64_t * size)92 status_t CallbackDataSource::getSize(off64_t *size) {
93 status_t err = mIDataSource->getSize(size);
94 if (err != OK) {
95 return err;
96 }
97 if (*size < 0) {
98 // IDataSource will set size to -1 to indicate unknown size, but
99 // DataSource returns ERROR_UNSUPPORTED for that.
100 return ERROR_UNSUPPORTED;
101 }
102 return OK;
103 }
104
flags()105 uint32_t CallbackDataSource::flags() {
106 return mIDataSource->getFlags();
107 }
108
close()109 void CallbackDataSource::close() {
110 if (!mIsClosed) {
111 mIDataSource->close();
112 mIsClosed = true;
113 }
114 }
115
DrmInitialization(const char * mime)116 sp<DecryptHandle> CallbackDataSource::DrmInitialization(const char *mime) {
117 return mIDataSource->DrmInitialization(mime);
118 }
119
getIDataSource() const120 sp<IDataSource> CallbackDataSource::getIDataSource() const {
121 return mIDataSource;
122 }
123
TinyCacheSource(const sp<DataSource> & source)124 TinyCacheSource::TinyCacheSource(const sp<DataSource>& source)
125 : mSource(source), mCachedOffset(0), mCachedSize(0) {
126 mName = String8::format("TinyCacheSource(%s)", mSource->toString().string());
127 }
128
initCheck() const129 status_t TinyCacheSource::initCheck() const {
130 return mSource->initCheck();
131 }
132
readAt(off64_t offset,void * data,size_t size)133 ssize_t TinyCacheSource::readAt(off64_t offset, void* data, size_t size) {
134 // Check if the cache satisfies the read.
135 if (mCachedOffset <= offset
136 && offset < (off64_t) (mCachedOffset + mCachedSize)) {
137 if (offset + size <= mCachedOffset + mCachedSize) {
138 memcpy(data, &mCache[offset - mCachedOffset], size);
139 return size;
140 } else {
141 // If the cache hits only partially, flush the cache and read the
142 // remainder.
143
144 // This value is guaranteed to be greater than 0 because of the
145 // enclosing if statement.
146 const ssize_t remaining = mCachedOffset + mCachedSize - offset;
147 memcpy(data, &mCache[offset - mCachedOffset], remaining);
148 const ssize_t readMore = readAt(offset + remaining,
149 (uint8_t*)data + remaining, size - remaining);
150 if (readMore < 0) {
151 return readMore;
152 }
153 return remaining + readMore;
154 }
155 }
156
157 if (size >= kCacheSize) {
158 return mSource->readAt(offset, data, size);
159 }
160
161 // Fill the cache and copy to the caller.
162 const ssize_t numRead = mSource->readAt(offset, mCache, kCacheSize);
163 if (numRead <= 0) {
164 // Flush cache on error
165 mCachedSize = 0;
166 mCachedOffset = 0;
167 return numRead;
168 }
169 if ((size_t)numRead > kCacheSize) {
170 // Flush cache on error
171 mCachedSize = 0;
172 mCachedOffset = 0;
173 return ERROR_OUT_OF_RANGE;
174 }
175
176 mCachedSize = numRead;
177 mCachedOffset = offset;
178 CHECK(mCachedSize <= kCacheSize && mCachedOffset >= 0);
179 const size_t numToReturn = std::min(size, (size_t)numRead);
180 memcpy(data, mCache, numToReturn);
181
182 return numToReturn;
183 }
184
getSize(off64_t * size)185 status_t TinyCacheSource::getSize(off64_t *size) {
186 return mSource->getSize(size);
187 }
188
flags()189 uint32_t TinyCacheSource::flags() {
190 return mSource->flags();
191 }
192
DrmInitialization(const char * mime)193 sp<DecryptHandle> TinyCacheSource::DrmInitialization(const char *mime) {
194 // flush cache when DrmInitialization occurs since decrypted
195 // data may differ from what is in cache.
196 mCachedOffset = 0;
197 mCachedSize = 0;
198 return mSource->DrmInitialization(mime);
199 }
200
getIDataSource() const201 sp<IDataSource> TinyCacheSource::getIDataSource() const {
202 return mSource->getIDataSource();
203 }
204
205 } // namespace android
206