FileSource.cpp revision 889b340ec736a9d3e3e690256d305cc8740f0c4b
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    if (mDecryptHandle != NULL) {
65        // To release mDecryptHandle
66        mDrmManagerClient->closeDecryptSession(mDecryptHandle);
67        mDecryptHandle = NULL;
68    }
69
70    if (mDrmManagerClient != NULL) {
71        delete mDrmManagerClient;
72        mDrmManagerClient = NULL;
73    }
74}
75
76status_t FileSource::initCheck() const {
77    return mFd >= 0 ? OK : NO_INIT;
78}
79
80ssize_t FileSource::readAt(off64_t offset, void *data, size_t size) {
81    if (mFd < 0) {
82        return NO_INIT;
83    }
84
85    Mutex::Autolock autoLock(mLock);
86
87    if (mLength >= 0) {
88        if (offset >= mLength) {
89            return 0;  // read beyond EOF.
90        }
91        int64_t numAvailable = mLength - offset;
92        if ((int64_t)size > numAvailable) {
93            size = numAvailable;
94        }
95    }
96
97    if (mDecryptHandle != NULL && DecryptApiType::CONTAINER_BASED
98            == mDecryptHandle->decryptApiType) {
99        return readAtDRM(offset, data, size);
100   } else {
101        off64_t result = lseek64(mFd, offset + mOffset, SEEK_SET);
102        if (result == -1) {
103            LOGE("seek to %lld failed", offset + mOffset);
104            return UNKNOWN_ERROR;
105        }
106
107        return ::read(mFd, data, size);
108    }
109}
110
111status_t FileSource::getSize(off64_t *size) {
112    if (mFd < 0) {
113        return NO_INIT;
114    }
115
116    if (mLength >= 0) {
117        *size = mLength;
118
119        return OK;
120    }
121
122    *size = lseek64(mFd, 0, SEEK_END);
123
124    return OK;
125}
126
127DecryptHandle* FileSource::DrmInitialization() {
128    if (mDrmManagerClient == NULL) {
129        mDrmManagerClient = new DrmManagerClient();
130    }
131
132    if (mDrmManagerClient == NULL) {
133        return NULL;
134    }
135
136    if (mDecryptHandle == NULL) {
137        mDecryptHandle = mDrmManagerClient->openDecryptSession(
138                mFd, mOffset, mLength);
139    }
140
141    if (mDecryptHandle == NULL) {
142        delete mDrmManagerClient;
143        mDrmManagerClient = NULL;
144    }
145
146    return mDecryptHandle;
147}
148
149void FileSource::getDrmInfo(DecryptHandle **handle, DrmManagerClient **client) {
150    *handle = mDecryptHandle;
151
152    *client = mDrmManagerClient;
153}
154
155ssize_t FileSource::readAtDRM(off64_t offset, void *data, size_t size) {
156    size_t DRM_CACHE_SIZE = 1024;
157    if (mDrmBuf == NULL) {
158        mDrmBuf = new unsigned char[DRM_CACHE_SIZE];
159    }
160
161    if (mDrmBuf != NULL && mDrmBufSize > 0 && (offset + mOffset) >= mDrmBufOffset
162            && (offset + mOffset + size) <= (mDrmBufOffset + mDrmBufSize)) {
163        /* Use buffered data */
164        memcpy(data, (void*)(mDrmBuf+(offset+mOffset-mDrmBufOffset)), size);
165        return size;
166    } else if (size <= DRM_CACHE_SIZE) {
167        /* Buffer new data */
168        mDrmBufOffset =  offset + mOffset;
169        mDrmBufSize = mDrmManagerClient->pread(mDecryptHandle, mDrmBuf,
170                DRM_CACHE_SIZE, offset + mOffset);
171        if (mDrmBufSize > 0) {
172            int64_t dataRead = 0;
173            dataRead = size > mDrmBufSize ? mDrmBufSize : size;
174            memcpy(data, (void*)mDrmBuf, dataRead);
175            return dataRead;
176        } else {
177            return mDrmBufSize;
178        }
179    } else {
180        /* Too big chunk to cache. Call DRM directly */
181        return mDrmManagerClient->pread(mDecryptHandle, data, size, offset + mOffset);
182    }
183}
184}  // namespace android
185