CallbackDataSource.cpp revision ee4e1b1a63758941460ae79a064249d3a5189443
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        // Sanity check.
74        CHECK((size_t)numRead <= numToRead && numRead >= 0 &&
75                (size_t)numRead <= bufferSize);
76        memcpy(((uint8_t*)data) + totalNumRead, mMemory->pointer(), numRead);
77        numLeft -= numRead;
78        totalNumRead += numRead;
79    }
80
81    return totalNumRead;
82}
83
84status_t CallbackDataSource::getSize(off64_t *size) {
85    status_t err = mIDataSource->getSize(size);
86    if (err != OK) {
87        return err;
88    }
89    if (*size < 0) {
90        // IDataSource will set size to -1 to indicate unknown size, but
91        // DataSource returns ERROR_UNSUPPORTED for that.
92        return ERROR_UNSUPPORTED;
93    }
94    return OK;
95}
96
97} // namespace android
98