CallbackDataSource.cpp revision 06ec7f3ef28ee68714d323ca9e2d3cab2165dfe8
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
29namespace android {
30
31CallbackDataSource::CallbackDataSource(
32    const sp<IDataSource>& binderDataSource)
33    : mIDataSource(binderDataSource) {
34    // Set up the buffer to read into.
35    mMemory = mIDataSource->getIMemory();
36}
37
38CallbackDataSource::~CallbackDataSource() {
39    ALOGV("~CallbackDataSource");
40    mIDataSource->close();
41}
42
43status_t CallbackDataSource::initCheck() const {
44    if (mMemory == NULL) {
45        return UNKNOWN_ERROR;
46    }
47    return OK;
48}
49
50ssize_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
85status_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
98TinyCacheSource::TinyCacheSource(const sp<DataSource>& source)
99    : mSource(source), mCachedOffset(0), mCachedSize(0) {
100}
101
102status_t TinyCacheSource::initCheck() const {
103    return mSource->initCheck();
104}
105
106ssize_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 && offset < mCachedOffset + mCachedSize) {
113        if (offset + size <= mCachedOffset + mCachedSize) {
114            memcpy(data, &mCache[offset - mCachedOffset], size);
115            return size;
116        } else {
117            // If the cache hits only partially, flush the cache and read the
118            // remainder.
119
120            // This value is guaranteed to be greater than 0 because of the
121            // enclosing if statement.
122            const ssize_t remaining = mCachedOffset + mCachedSize - offset;
123            memcpy(data, &mCache[offset - mCachedOffset], remaining);
124            const ssize_t readMore = readAt(offset + remaining,
125                    (uint8_t*)data + remaining, size - remaining);
126            if (readMore < 0) {
127                return readMore;
128            }
129            return remaining + readMore;
130        }
131    }
132
133    // Fill the cache and copy to the caller.
134    const ssize_t numRead = mSource->readAt(offset, mCache, kCacheSize);
135    if (numRead <= 0) {
136        return numRead;
137    }
138    if ((size_t)numRead > kCacheSize) {
139        return ERROR_OUT_OF_RANGE;
140    }
141
142    mCachedSize = numRead;
143    mCachedOffset = offset;
144    CHECK(mCachedSize <= kCacheSize && mCachedOffset >= 0);
145    const size_t numToReturn = std::min(size, (size_t)numRead);
146    memcpy(data, mCache, numToReturn);
147
148    return numToReturn;
149}
150
151status_t TinyCacheSource::getSize(off64_t *size) {
152    return mSource->getSize(size);
153}
154
155uint32_t TinyCacheSource::flags() {
156    return mSource->flags();
157}
158
159} // namespace android
160