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