FileSource.cpp revision 674ebd0b4e1143e38392a4e3bb38b4679a4577bc
1/*
2 * Copyright (C) 2009 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#include <media/stagefright/FileSource.h>
18#include <media/stagefright/MediaDebug.h>
19#include <sys/types.h>
20#include <unistd.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24
25namespace android {
26
27FileSource::FileSource(const char *filename)
28    : mFd(-1),
29      mOffset(0),
30      mLength(-1),
31      mDecryptHandle(NULL),
32      mDrmManagerClient(NULL),
33      mDrmBufOffset(0),
34      mDrmBufSize(0),
35      mDrmBuf(NULL){
36
37    mFd = open(filename, O_LARGEFILE | O_RDONLY);
38}
39
40FileSource::FileSource(int fd, int64_t offset, int64_t length)
41    : mFd(fd),
42      mOffset(offset),
43      mLength(length),
44      mDecryptHandle(NULL),
45      mDrmManagerClient(NULL),
46      mDrmBufOffset(0),
47      mDrmBufSize(0),
48      mDrmBuf(NULL){
49    CHECK(offset >= 0);
50    CHECK(length >= 0);
51}
52
53FileSource::~FileSource() {
54    if (mFd >= 0) {
55        close(mFd);
56        mFd = -1;
57    }
58
59    if (mDrmBuf != NULL) {
60        delete[] mDrmBuf;
61        mDrmBuf = NULL;
62    }
63}
64
65status_t FileSource::initCheck() const {
66    return mFd >= 0 ? OK : NO_INIT;
67}
68
69ssize_t FileSource::readAt(off64_t offset, void *data, size_t size) {
70    if (mFd < 0) {
71        return NO_INIT;
72    }
73
74    Mutex::Autolock autoLock(mLock);
75
76    if (mLength >= 0) {
77        if (offset >= mLength) {
78            return 0;  // read beyond EOF.
79        }
80        int64_t numAvailable = mLength - offset;
81        if ((int64_t)size > numAvailable) {
82            size = numAvailable;
83        }
84    }
85
86    if (mDecryptHandle != NULL && DecryptApiType::CONTAINER_BASED
87            == mDecryptHandle->decryptApiType) {
88        return readAtDRM(offset, data, size);
89   } else {
90        off64_t result = lseek64(mFd, offset + mOffset, SEEK_SET);
91        if (result == -1) {
92            LOGE("seek to %lld failed", offset + mOffset);
93            return UNKNOWN_ERROR;
94        }
95
96        return ::read(mFd, data, size);
97    }
98}
99
100status_t FileSource::getSize(off64_t *size) {
101    if (mFd < 0) {
102        return NO_INIT;
103    }
104
105    if (mLength >= 0) {
106        *size = mLength;
107
108        return OK;
109    }
110
111    *size = lseek64(mFd, 0, SEEK_END);
112
113    return OK;
114}
115
116DecryptHandle* FileSource::DrmInitialization(DrmManagerClient* client) {
117    if (client == NULL) {
118        return NULL;
119    }
120    mDrmManagerClient = client;
121
122    if (mDecryptHandle == NULL) {
123        mDecryptHandle = mDrmManagerClient->openDecryptSession(
124                mFd, mOffset, mLength);
125    }
126
127    if (mDecryptHandle == NULL) {
128        mDrmManagerClient = NULL;
129    }
130
131    return mDecryptHandle;
132}
133
134void FileSource::getDrmInfo(DecryptHandle **handle, DrmManagerClient **client) {
135    *handle = mDecryptHandle;
136
137    *client = mDrmManagerClient;
138}
139
140ssize_t FileSource::readAtDRM(off64_t offset, void *data, size_t size) {
141    size_t DRM_CACHE_SIZE = 1024;
142    if (mDrmBuf == NULL) {
143        mDrmBuf = new unsigned char[DRM_CACHE_SIZE];
144    }
145
146    if (mDrmBuf != NULL && mDrmBufSize > 0 && (offset + mOffset) >= mDrmBufOffset
147            && (offset + mOffset + size) <= (mDrmBufOffset + mDrmBufSize)) {
148        /* Use buffered data */
149        memcpy(data, (void*)(mDrmBuf+(offset+mOffset-mDrmBufOffset)), size);
150        return size;
151    } else if (size <= DRM_CACHE_SIZE) {
152        /* Buffer new data */
153        mDrmBufOffset =  offset + mOffset;
154        mDrmBufSize = mDrmManagerClient->pread(mDecryptHandle, mDrmBuf,
155                DRM_CACHE_SIZE, offset + mOffset);
156        if (mDrmBufSize > 0) {
157            int64_t dataRead = 0;
158            dataRead = size > mDrmBufSize ? mDrmBufSize : size;
159            memcpy(data, (void*)mDrmBuf, dataRead);
160            return dataRead;
161        } else {
162            return mDrmBufSize;
163        }
164    } else {
165        /* Too big chunk to cache. Call DRM directly */
166        return mDrmManagerClient->pread(mDecryptHandle, data, size, offset + mOffset);
167    }
168}
169}  // namespace android
170