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