Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 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 | |
James Dong | f1d5aa1 | 2012-02-06 23:46:37 -0800 | [diff] [blame] | 17 | #include <media/stagefright/foundation/ADebug.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 18 | #include <media/stagefright/FileSource.h> |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <unistd.h> |
James Dong | 674ebd0 | 2010-11-18 20:59:13 -0800 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <fcntl.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | FileSource::FileSource(const char *filename) |
James Dong | 674ebd0 | 2010-11-18 20:59:13 -0800 | [diff] [blame] | 28 | : mFd(-1), |
Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 29 | mOffset(0), |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 30 | mLength(-1), |
| 31 | mDecryptHandle(NULL), |
| 32 | mDrmManagerClient(NULL), |
| 33 | mDrmBufOffset(0), |
| 34 | mDrmBufSize(0), |
| 35 | mDrmBuf(NULL){ |
James Dong | 674ebd0 | 2010-11-18 20:59:13 -0800 | [diff] [blame] | 36 | |
| 37 | mFd = open(filename, O_LARGEFILE | O_RDONLY); |
Andreas Huber | 4ee31e2 | 2012-04-11 09:22:52 -0700 | [diff] [blame] | 38 | |
| 39 | if (mFd >= 0) { |
| 40 | mLength = lseek64(mFd, 0, SEEK_END); |
Andreas Huber | f4b7d94 | 2012-04-11 11:52:03 -0700 | [diff] [blame] | 41 | } else { |
| 42 | ALOGE("Failed to open file '%s'. (%s)", filename, strerror(errno)); |
Andreas Huber | 4ee31e2 | 2012-04-11 09:22:52 -0700 | [diff] [blame] | 43 | } |
Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | FileSource::FileSource(int fd, int64_t offset, int64_t length) |
James Dong | 674ebd0 | 2010-11-18 20:59:13 -0800 | [diff] [blame] | 47 | : mFd(fd), |
Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 48 | mOffset(offset), |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 49 | mLength(length), |
| 50 | mDecryptHandle(NULL), |
| 51 | mDrmManagerClient(NULL), |
| 52 | mDrmBufOffset(0), |
| 53 | mDrmBufSize(0), |
| 54 | mDrmBuf(NULL){ |
Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 55 | CHECK(offset >= 0); |
| 56 | CHECK(length >= 0); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | FileSource::~FileSource() { |
James Dong | 674ebd0 | 2010-11-18 20:59:13 -0800 | [diff] [blame] | 60 | if (mFd >= 0) { |
| 61 | close(mFd); |
| 62 | mFd = -1; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 63 | } |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 64 | |
| 65 | if (mDrmBuf != NULL) { |
| 66 | delete[] mDrmBuf; |
| 67 | mDrmBuf = NULL; |
| 68 | } |
Gloria Wang | 889b340 | 2011-02-07 11:41:11 -0800 | [diff] [blame] | 69 | |
| 70 | if (mDecryptHandle != NULL) { |
| 71 | // To release mDecryptHandle |
Gloria Wang | 8f64134 | 2011-02-08 13:24:08 -0800 | [diff] [blame] | 72 | CHECK(mDrmManagerClient); |
Gloria Wang | 889b340 | 2011-02-07 11:41:11 -0800 | [diff] [blame] | 73 | mDrmManagerClient->closeDecryptSession(mDecryptHandle); |
| 74 | mDecryptHandle = NULL; |
| 75 | } |
| 76 | |
| 77 | if (mDrmManagerClient != NULL) { |
| 78 | delete mDrmManagerClient; |
| 79 | mDrmManagerClient = NULL; |
| 80 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 83 | status_t FileSource::initCheck() const { |
James Dong | 674ebd0 | 2010-11-18 20:59:13 -0800 | [diff] [blame] | 84 | return mFd >= 0 ? OK : NO_INIT; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 85 | } |
| 86 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 87 | ssize_t FileSource::readAt(off64_t offset, void *data, size_t size) { |
James Dong | 674ebd0 | 2010-11-18 20:59:13 -0800 | [diff] [blame] | 88 | if (mFd < 0) { |
Andreas Huber | 3d8055a | 2010-05-24 09:18:36 -0700 | [diff] [blame] | 89 | return NO_INIT; |
| 90 | } |
| 91 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 92 | Mutex::Autolock autoLock(mLock); |
| 93 | |
Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 94 | if (mLength >= 0) { |
| 95 | if (offset >= mLength) { |
| 96 | return 0; // read beyond EOF. |
| 97 | } |
| 98 | int64_t numAvailable = mLength - offset; |
| 99 | if ((int64_t)size > numAvailable) { |
| 100 | size = numAvailable; |
| 101 | } |
| 102 | } |
| 103 | |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 104 | if (mDecryptHandle != NULL && DecryptApiType::CONTAINER_BASED |
| 105 | == mDecryptHandle->decryptApiType) { |
| 106 | return readAtDRM(offset, data, size); |
| 107 | } else { |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 108 | off64_t result = lseek64(mFd, offset + mOffset, SEEK_SET); |
| 109 | if (result == -1) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 110 | ALOGE("seek to %lld failed", offset + mOffset); |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 111 | return UNKNOWN_ERROR; |
| 112 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 113 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 114 | return ::read(mFd, data, size); |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 115 | } |
Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 116 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 117 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 118 | status_t FileSource::getSize(off64_t *size) { |
Andreas Huber | 3a13fad | 2011-09-02 09:34:51 -0700 | [diff] [blame] | 119 | Mutex::Autolock autoLock(mLock); |
| 120 | |
James Dong | 674ebd0 | 2010-11-18 20:59:13 -0800 | [diff] [blame] | 121 | if (mFd < 0) { |
Andreas Huber | 3d8055a | 2010-05-24 09:18:36 -0700 | [diff] [blame] | 122 | return NO_INIT; |
| 123 | } |
| 124 | |
Andreas Huber | 4ee31e2 | 2012-04-11 09:22:52 -0700 | [diff] [blame] | 125 | *size = mLength; |
Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 126 | |
| 127 | return OK; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 128 | } |
| 129 | |
James Dong | 9d2f386 | 2012-01-10 08:24:37 -0800 | [diff] [blame] | 130 | sp<DecryptHandle> FileSource::DrmInitialization(const char *mime) { |
Gloria Wang | 889b340 | 2011-02-07 11:41:11 -0800 | [diff] [blame] | 131 | if (mDrmManagerClient == NULL) { |
| 132 | mDrmManagerClient = new DrmManagerClient(); |
| 133 | } |
| 134 | |
| 135 | if (mDrmManagerClient == NULL) { |
Gloria Wang | b371426 | 2010-11-01 15:53:16 -0700 | [diff] [blame] | 136 | return NULL; |
| 137 | } |
Gloria Wang | b371426 | 2010-11-01 15:53:16 -0700 | [diff] [blame] | 138 | |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 139 | if (mDecryptHandle == NULL) { |
| 140 | mDecryptHandle = mDrmManagerClient->openDecryptSession( |
James Dong | 9d2f386 | 2012-01-10 08:24:37 -0800 | [diff] [blame] | 141 | mFd, mOffset, mLength, mime); |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (mDecryptHandle == NULL) { |
Gloria Wang | 889b340 | 2011-02-07 11:41:11 -0800 | [diff] [blame] | 145 | delete mDrmManagerClient; |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 146 | mDrmManagerClient = NULL; |
| 147 | } |
| 148 | |
| 149 | return mDecryptHandle; |
| 150 | } |
| 151 | |
Gloria Wang | b5ce361 | 2011-02-24 16:40:57 -0800 | [diff] [blame] | 152 | void FileSource::getDrmInfo(sp<DecryptHandle> &handle, DrmManagerClient **client) { |
| 153 | handle = mDecryptHandle; |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 154 | |
| 155 | *client = mDrmManagerClient; |
| 156 | } |
| 157 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 158 | ssize_t FileSource::readAtDRM(off64_t offset, void *data, size_t size) { |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 159 | size_t DRM_CACHE_SIZE = 1024; |
| 160 | if (mDrmBuf == NULL) { |
| 161 | mDrmBuf = new unsigned char[DRM_CACHE_SIZE]; |
| 162 | } |
| 163 | |
| 164 | if (mDrmBuf != NULL && mDrmBufSize > 0 && (offset + mOffset) >= mDrmBufOffset |
| 165 | && (offset + mOffset + size) <= (mDrmBufOffset + mDrmBufSize)) { |
| 166 | /* Use buffered data */ |
| 167 | memcpy(data, (void*)(mDrmBuf+(offset+mOffset-mDrmBufOffset)), size); |
| 168 | return size; |
| 169 | } else if (size <= DRM_CACHE_SIZE) { |
| 170 | /* Buffer new data */ |
| 171 | mDrmBufOffset = offset + mOffset; |
| 172 | mDrmBufSize = mDrmManagerClient->pread(mDecryptHandle, mDrmBuf, |
| 173 | DRM_CACHE_SIZE, offset + mOffset); |
| 174 | if (mDrmBufSize > 0) { |
| 175 | int64_t dataRead = 0; |
| 176 | dataRead = size > mDrmBufSize ? mDrmBufSize : size; |
| 177 | memcpy(data, (void*)mDrmBuf, dataRead); |
| 178 | return dataRead; |
| 179 | } else { |
| 180 | return mDrmBufSize; |
| 181 | } |
| 182 | } else { |
| 183 | /* Too big chunk to cache. Call DRM directly */ |
| 184 | return mDrmManagerClient->pread(mDecryptHandle, data, size, offset + mOffset); |
| 185 | } |
| 186 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 187 | } // namespace android |