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//#define LOG_NDEBUG 0
18#define LOG_TAG "FileSource"
19#include <utils/Log.h>
20
21#include <media/stagefright/foundation/ADebug.h>
22#include <media/stagefright/FileSource.h>
23#include <media/stagefright/Utils.h>
24#include <sys/types.h>
25#include <unistd.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <fcntl.h>
29
30namespace android {
31
32FileSource::FileSource(const char *filename)
33    : mFd(-1),
34      mOffset(0),
35      mLength(-1),
36      mName("<null>"),
37      mDecryptHandle(NULL),
38      mDrmManagerClient(NULL),
39      mDrmBufOffset(0),
40      mDrmBufSize(0),
41      mDrmBuf(NULL){
42
43    if (filename) {
44        mName = String8::format("FileSource(%s)", filename);
45    }
46    ALOGV("%s", filename);
47    mFd = open(filename, O_LARGEFILE | O_RDONLY);
48
49    if (mFd >= 0) {
50        mLength = lseek64(mFd, 0, SEEK_END);
51    } else {
52        ALOGE("Failed to open file '%s'. (%s)", filename, strerror(errno));
53    }
54}
55
56FileSource::FileSource(int fd, int64_t offset, int64_t length)
57    : mFd(fd),
58      mOffset(offset),
59      mLength(length),
60      mName("<null>"),
61      mDecryptHandle(NULL),
62      mDrmManagerClient(NULL),
63      mDrmBufOffset(0),
64      mDrmBufSize(0),
65      mDrmBuf(NULL) {
66    ALOGV("fd=%d (%s), offset=%lld, length=%lld",
67            fd, nameForFd(fd).c_str(), (long long) offset, (long long) length);
68
69    if (mOffset < 0) {
70        mOffset = 0;
71    }
72    if (mLength < 0) {
73        mLength = 0;
74    }
75    if (mLength > INT64_MAX - mOffset) {
76        mLength = INT64_MAX - mOffset;
77    }
78    struct stat s;
79    if (fstat(fd, &s) == 0) {
80        if (mOffset > s.st_size) {
81            mOffset = s.st_size;
82            mLength = 0;
83        }
84        if (mOffset + mLength > s.st_size) {
85            mLength = s.st_size - mOffset;
86        }
87    }
88    if (mOffset != offset || mLength != length) {
89        ALOGW("offset/length adjusted from %lld/%lld to %lld/%lld",
90                (long long) offset, (long long) length,
91                (long long) mOffset, (long long) mLength);
92    }
93
94    mName = String8::format(
95            "FileSource(fd(%s), %lld, %lld)",
96            nameForFd(fd).c_str(),
97            (long long) mOffset,
98            (long long) mLength);
99
100}
101
102FileSource::~FileSource() {
103    if (mFd >= 0) {
104        ::close(mFd);
105        mFd = -1;
106    }
107
108    if (mDrmBuf != NULL) {
109        delete[] mDrmBuf;
110        mDrmBuf = NULL;
111    }
112
113    if (mDecryptHandle != NULL) {
114        // To release mDecryptHandle
115        CHECK(mDrmManagerClient);
116        mDrmManagerClient->closeDecryptSession(mDecryptHandle);
117        mDecryptHandle = NULL;
118    }
119
120    if (mDrmManagerClient != NULL) {
121        delete mDrmManagerClient;
122        mDrmManagerClient = NULL;
123    }
124}
125
126status_t FileSource::initCheck() const {
127    return mFd >= 0 ? OK : NO_INIT;
128}
129
130ssize_t FileSource::readAt(off64_t offset, void *data, size_t size) {
131    if (mFd < 0) {
132        return NO_INIT;
133    }
134
135    Mutex::Autolock autoLock(mLock);
136
137    if (mLength >= 0) {
138        if (offset >= mLength) {
139            return 0;  // read beyond EOF.
140        }
141        uint64_t numAvailable = mLength - offset;
142        if ((uint64_t)size > numAvailable) {
143            size = numAvailable;
144        }
145    }
146
147    if (mDecryptHandle != NULL && DecryptApiType::CONTAINER_BASED
148            == mDecryptHandle->decryptApiType) {
149        return readAtDRM(offset, data, size);
150   } else {
151        off64_t result = lseek64(mFd, offset + mOffset, SEEK_SET);
152        if (result == -1) {
153            ALOGE("seek to %lld failed", (long long)(offset + mOffset));
154            return UNKNOWN_ERROR;
155        }
156
157        return ::read(mFd, data, size);
158    }
159}
160
161status_t FileSource::getSize(off64_t *size) {
162    Mutex::Autolock autoLock(mLock);
163
164    if (mFd < 0) {
165        return NO_INIT;
166    }
167
168    *size = mLength;
169
170    return OK;
171}
172
173sp<DecryptHandle> FileSource::DrmInitialization(const char *mime) {
174    if (mDrmManagerClient == NULL) {
175        mDrmManagerClient = new DrmManagerClient();
176    }
177
178    if (mDrmManagerClient == NULL) {
179        return NULL;
180    }
181
182    if (mDecryptHandle == NULL) {
183        mDecryptHandle = mDrmManagerClient->openDecryptSession(
184                mFd, mOffset, mLength, mime);
185    }
186
187    if (mDecryptHandle == NULL) {
188        delete mDrmManagerClient;
189        mDrmManagerClient = NULL;
190    }
191
192    return mDecryptHandle;
193}
194
195void FileSource::getDrmInfo(sp<DecryptHandle> &handle, DrmManagerClient **client) {
196    handle = mDecryptHandle;
197
198    *client = mDrmManagerClient;
199}
200
201ssize_t FileSource::readAtDRM(off64_t offset, void *data, size_t size) {
202    size_t DRM_CACHE_SIZE = 1024;
203    if (mDrmBuf == NULL) {
204        mDrmBuf = new unsigned char[DRM_CACHE_SIZE];
205    }
206
207    if (mDrmBuf != NULL && mDrmBufSize > 0 && (offset + mOffset) >= mDrmBufOffset
208            && (offset + mOffset + size) <= static_cast<size_t>(mDrmBufOffset + mDrmBufSize)) {
209        /* Use buffered data */
210        memcpy(data, (void*)(mDrmBuf+(offset+mOffset-mDrmBufOffset)), size);
211        return size;
212    } else if (size <= DRM_CACHE_SIZE) {
213        /* Buffer new data */
214        mDrmBufOffset =  offset + mOffset;
215        mDrmBufSize = mDrmManagerClient->pread(mDecryptHandle, mDrmBuf,
216                DRM_CACHE_SIZE, offset + mOffset);
217        if (mDrmBufSize > 0) {
218            int64_t dataRead = 0;
219            dataRead = size > static_cast<size_t>(mDrmBufSize) ? mDrmBufSize : size;
220            memcpy(data, (void*)mDrmBuf, dataRead);
221            return dataRead;
222        } else {
223            return mDrmBufSize;
224        }
225    } else {
226        /* Too big chunk to cache. Call DRM directly */
227        return mDrmManagerClient->pread(mDecryptHandle, data, size, offset + mOffset);
228    }
229}
230}  // namespace android
231