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 <media/IDataSource.h>
25 #include <media/stagefright/foundation/ADebug.h>
26
27 #include <algorithm>
28
29 namespace android {
30
CallbackDataSource(const sp<IDataSource> & binderDataSource)31 CallbackDataSource::CallbackDataSource(
32 const sp<IDataSource>& binderDataSource)
33 : mIDataSource(binderDataSource) {
34 // Set up the buffer to read into.
35 mMemory = mIDataSource->getIMemory();
36 }
37
~CallbackDataSource()38 CallbackDataSource::~CallbackDataSource() {
39 ALOGV("~CallbackDataSource");
40 mIDataSource->close();
41 }
42
initCheck() const43 status_t CallbackDataSource::initCheck() const {
44 if (mMemory == NULL) {
45 return UNKNOWN_ERROR;
46 }
47 return OK;
48 }
49
readAt(off64_t offset,void * data,size_t size)50 ssize_t CallbackDataSource::readAt(off64_t offset, void* data, size_t size) {
51 if (mMemory == NULL) {
52 return -1;
53 }
54
55 // IDataSource can only read up to mMemory->size() bytes at a time, but this
56 // method should be able to read any number of bytes, so read in a loop.
57 size_t totalNumRead = 0;
58 size_t numLeft = size;
59 const size_t bufferSize = mMemory->size();
60
61 while (numLeft > 0) {
62 size_t numToRead = std::min(numLeft, bufferSize);
63 ssize_t numRead =
64 mIDataSource->readAt(offset + totalNumRead, numToRead);
65 // A negative return value represents an error. Pass it on.
66 if (numRead < 0) {
67 return numRead;
68 }
69 // A zero return value signals EOS. Return the bytes read so far.
70 if (numRead == 0) {
71 return totalNumRead;
72 }
73 if ((size_t)numRead > numToRead) {
74 return ERROR_OUT_OF_RANGE;
75 }
76 CHECK(numRead >= 0 && (size_t)numRead <= bufferSize);
77 memcpy(((uint8_t*)data) + totalNumRead, mMemory->pointer(), numRead);
78 numLeft -= numRead;
79 totalNumRead += numRead;
80 }
81
82 return totalNumRead;
83 }
84
getSize(off64_t * size)85 status_t CallbackDataSource::getSize(off64_t *size) {
86 status_t err = mIDataSource->getSize(size);
87 if (err != OK) {
88 return err;
89 }
90 if (*size < 0) {
91 // IDataSource will set size to -1 to indicate unknown size, but
92 // DataSource returns ERROR_UNSUPPORTED for that.
93 return ERROR_UNSUPPORTED;
94 }
95 return OK;
96 }
97
TinyCacheSource(const sp<DataSource> & source)98 TinyCacheSource::TinyCacheSource(const sp<DataSource>& source)
99 : mSource(source), mCachedOffset(0), mCachedSize(0) {
100 }
101
initCheck() const102 status_t TinyCacheSource::initCheck() const {
103 return mSource->initCheck();
104 }
105
readAt(off64_t offset,void * data,size_t size)106 ssize_t TinyCacheSource::readAt(off64_t offset, void* data, size_t size) {
107 if (size >= kCacheSize) {
108 return mSource->readAt(offset, data, size);
109 }
110
111 // Check if the cache satisfies the read.
112 if (mCachedOffset <= offset
113 && offset < (off64_t) (mCachedOffset + mCachedSize)) {
114 if (offset + size <= mCachedOffset + mCachedSize) {
115 memcpy(data, &mCache[offset - mCachedOffset], size);
116 return size;
117 } else {
118 // If the cache hits only partially, flush the cache and read the
119 // remainder.
120
121 // This value is guaranteed to be greater than 0 because of the
122 // enclosing if statement.
123 const ssize_t remaining = mCachedOffset + mCachedSize - offset;
124 memcpy(data, &mCache[offset - mCachedOffset], remaining);
125 const ssize_t readMore = readAt(offset + remaining,
126 (uint8_t*)data + remaining, size - remaining);
127 if (readMore < 0) {
128 return readMore;
129 }
130 return remaining + readMore;
131 }
132 }
133
134 // Fill the cache and copy to the caller.
135 const ssize_t numRead = mSource->readAt(offset, mCache, kCacheSize);
136 if (numRead <= 0) {
137 return numRead;
138 }
139 if ((size_t)numRead > kCacheSize) {
140 return ERROR_OUT_OF_RANGE;
141 }
142
143 mCachedSize = numRead;
144 mCachedOffset = offset;
145 CHECK(mCachedSize <= kCacheSize && mCachedOffset >= 0);
146 const size_t numToReturn = std::min(size, (size_t)numRead);
147 memcpy(data, mCache, numToReturn);
148
149 return numToReturn;
150 }
151
getSize(off64_t * size)152 status_t TinyCacheSource::getSize(off64_t *size) {
153 return mSource->getSize(size);
154 }
155
flags()156 uint32_t TinyCacheSource::flags() {
157 return mSource->flags();
158 }
159
160 } // namespace android
161