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 | |
Marco Nelissen | 4f82a25 | 2014-12-22 10:18:15 -0800 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "FileSource" |
| 19 | #include <utils/Log.h> |
| 20 | |
Marco Nelissen | fa8be7d | 2019-09-23 12:15:57 -0700 | [diff] [blame^] | 21 | #include <datasource/FileSource.h> |
James Dong | f1d5aa1 | 2012-02-06 23:46:37 -0800 | [diff] [blame] | 22 | #include <media/stagefright/foundation/ADebug.h> |
Andy Hung | d49dbd6 | 2016-07-07 14:20:35 -0700 | [diff] [blame] | 23 | #include <private/android_filesystem_config.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | FileSource::FileSource(const char *filename) |
Dongwon Kang | 1e1bcaa | 2018-08-01 12:44:46 -0700 | [diff] [blame] | 28 | : ClearFileSource(filename), |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 29 | mDecryptHandle(NULL), |
| 30 | mDrmManagerClient(NULL), |
| 31 | mDrmBufOffset(0), |
| 32 | mDrmBufSize(0), |
| 33 | mDrmBuf(NULL){ |
Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | FileSource::FileSource(int fd, int64_t offset, int64_t length) |
Dongwon Kang | 1e1bcaa | 2018-08-01 12:44:46 -0700 | [diff] [blame] | 37 | : ClearFileSource(fd, offset, length), |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 38 | mDecryptHandle(NULL), |
| 39 | mDrmManagerClient(NULL), |
| 40 | mDrmBufOffset(0), |
| 41 | mDrmBufSize(0), |
Marco Nelissen | 63690d1 | 2016-04-06 09:28:05 -0700 | [diff] [blame] | 42 | mDrmBuf(NULL) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | FileSource::~FileSource() { |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 46 | if (mDrmBuf != NULL) { |
| 47 | delete[] mDrmBuf; |
| 48 | mDrmBuf = NULL; |
| 49 | } |
Gloria Wang | 889b340 | 2011-02-07 11:41:11 -0800 | [diff] [blame] | 50 | |
| 51 | if (mDecryptHandle != NULL) { |
| 52 | // To release mDecryptHandle |
Gloria Wang | 8f64134 | 2011-02-08 13:24:08 -0800 | [diff] [blame] | 53 | CHECK(mDrmManagerClient); |
Gloria Wang | 889b340 | 2011-02-07 11:41:11 -0800 | [diff] [blame] | 54 | mDrmManagerClient->closeDecryptSession(mDecryptHandle); |
| 55 | mDecryptHandle = NULL; |
| 56 | } |
| 57 | |
| 58 | if (mDrmManagerClient != NULL) { |
| 59 | delete mDrmManagerClient; |
| 60 | mDrmManagerClient = NULL; |
| 61 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 62 | } |
| 63 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 64 | ssize_t FileSource::readAt(off64_t offset, void *data, size_t size) { |
James Dong | 674ebd0 | 2010-11-18 20:59:13 -0800 | [diff] [blame] | 65 | if (mFd < 0) { |
Andreas Huber | 3d8055a | 2010-05-24 09:18:36 -0700 | [diff] [blame] | 66 | return NO_INIT; |
| 67 | } |
| 68 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 69 | Mutex::Autolock autoLock(mLock); |
| 70 | |
Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 71 | if (mLength >= 0) { |
| 72 | if (offset >= mLength) { |
| 73 | return 0; // read beyond EOF. |
| 74 | } |
Wei Jia | 0eaf820 | 2015-08-19 16:48:47 -0700 | [diff] [blame] | 75 | uint64_t numAvailable = mLength - offset; |
| 76 | if ((uint64_t)size > numAvailable) { |
Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 77 | size = numAvailable; |
| 78 | } |
| 79 | } |
| 80 | |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 81 | if (mDecryptHandle != NULL && DecryptApiType::CONTAINER_BASED |
| 82 | == mDecryptHandle->decryptApiType) { |
Dongwon Kang | 1e1bcaa | 2018-08-01 12:44:46 -0700 | [diff] [blame] | 83 | return readAtDRM_l(offset, data, size); |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 84 | } else { |
Dongwon Kang | 1e1bcaa | 2018-08-01 12:44:46 -0700 | [diff] [blame] | 85 | return readAt_l(offset, data, size); |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 86 | } |
Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 87 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 88 | |
James Dong | 9d2f386 | 2012-01-10 08:24:37 -0800 | [diff] [blame] | 89 | sp<DecryptHandle> FileSource::DrmInitialization(const char *mime) { |
Andy Hung | d49dbd6 | 2016-07-07 14:20:35 -0700 | [diff] [blame] | 90 | if (getuid() == AID_MEDIA_EX) return nullptr; // no DRM in media extractor |
Gloria Wang | 889b340 | 2011-02-07 11:41:11 -0800 | [diff] [blame] | 91 | if (mDrmManagerClient == NULL) { |
| 92 | mDrmManagerClient = new DrmManagerClient(); |
| 93 | } |
| 94 | |
| 95 | if (mDrmManagerClient == NULL) { |
Gloria Wang | b371426 | 2010-11-01 15:53:16 -0700 | [diff] [blame] | 96 | return NULL; |
| 97 | } |
Gloria Wang | b371426 | 2010-11-01 15:53:16 -0700 | [diff] [blame] | 98 | |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 99 | if (mDecryptHandle == NULL) { |
| 100 | mDecryptHandle = mDrmManagerClient->openDecryptSession( |
James Dong | 9d2f386 | 2012-01-10 08:24:37 -0800 | [diff] [blame] | 101 | mFd, mOffset, mLength, mime); |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | if (mDecryptHandle == NULL) { |
Gloria Wang | 889b340 | 2011-02-07 11:41:11 -0800 | [diff] [blame] | 105 | delete mDrmManagerClient; |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 106 | mDrmManagerClient = NULL; |
| 107 | } |
| 108 | |
| 109 | return mDecryptHandle; |
| 110 | } |
| 111 | |
Dongwon Kang | 1e1bcaa | 2018-08-01 12:44:46 -0700 | [diff] [blame] | 112 | ssize_t FileSource::readAtDRM_l(off64_t offset, void *data, size_t size) { |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 113 | size_t DRM_CACHE_SIZE = 1024; |
| 114 | if (mDrmBuf == NULL) { |
| 115 | mDrmBuf = new unsigned char[DRM_CACHE_SIZE]; |
| 116 | } |
| 117 | |
| 118 | if (mDrmBuf != NULL && mDrmBufSize > 0 && (offset + mOffset) >= mDrmBufOffset |
Patrik Lindgren | d066bc8 | 2014-12-22 09:06:21 +0100 | [diff] [blame] | 119 | && (offset + mOffset + size) <= static_cast<size_t>(mDrmBufOffset + mDrmBufSize)) { |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 120 | /* Use buffered data */ |
| 121 | memcpy(data, (void*)(mDrmBuf+(offset+mOffset-mDrmBufOffset)), size); |
| 122 | return size; |
| 123 | } else if (size <= DRM_CACHE_SIZE) { |
| 124 | /* Buffer new data */ |
| 125 | mDrmBufOffset = offset + mOffset; |
| 126 | mDrmBufSize = mDrmManagerClient->pread(mDecryptHandle, mDrmBuf, |
| 127 | DRM_CACHE_SIZE, offset + mOffset); |
| 128 | if (mDrmBufSize > 0) { |
| 129 | int64_t dataRead = 0; |
Patrik Lindgren | d066bc8 | 2014-12-22 09:06:21 +0100 | [diff] [blame] | 130 | dataRead = size > static_cast<size_t>(mDrmBufSize) ? mDrmBufSize : size; |
Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 131 | memcpy(data, (void*)mDrmBuf, dataRead); |
| 132 | return dataRead; |
| 133 | } else { |
| 134 | return mDrmBufSize; |
| 135 | } |
| 136 | } else { |
| 137 | /* Too big chunk to cache. Call DRM directly */ |
| 138 | return mDrmManagerClient->pread(mDecryptHandle, data, size, offset + mOffset); |
| 139 | } |
| 140 | } |
Andy Hung | d49dbd6 | 2016-07-07 14:20:35 -0700 | [diff] [blame] | 141 | |
| 142 | /* static */ |
| 143 | bool FileSource::requiresDrm(int fd, int64_t offset, int64_t length, const char *mime) { |
| 144 | std::unique_ptr<DrmManagerClient> drmClient(new DrmManagerClient()); |
| 145 | sp<DecryptHandle> decryptHandle = |
| 146 | drmClient->openDecryptSession(fd, offset, length, mime); |
| 147 | bool requiresDrm = false; |
| 148 | if (decryptHandle != nullptr) { |
| 149 | requiresDrm = decryptHandle->decryptApiType == DecryptApiType::CONTAINER_BASED; |
| 150 | drmClient->closeDecryptSession(decryptHandle); |
| 151 | } |
| 152 | return requiresDrm; |
| 153 | } |
| 154 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 155 | } // namespace android |