blob: a7ca3daeaacb57def1fe12855f58848b44dac393 [file] [log] [blame]
Andreas Huber20111aa2009-07-14 16:56:47 -07001/*
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 Dongf1d5aa12012-02-06 23:46:37 -080017#include <media/stagefright/foundation/ADebug.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070018#include <media/stagefright/FileSource.h>
James Dongc7fc37a2010-11-16 14:04:54 -080019#include <sys/types.h>
20#include <unistd.h>
James Dong674ebd02010-11-18 20:59:13 -080021#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070024
25namespace android {
26
27FileSource::FileSource(const char *filename)
James Dong674ebd02010-11-18 20:59:13 -080028 : mFd(-1),
Andreas Huber03475f52009-11-16 15:34:01 -080029 mOffset(0),
Gloria Wangdcd25ef2010-06-22 13:55:38 -070030 mLength(-1),
31 mDecryptHandle(NULL),
32 mDrmManagerClient(NULL),
33 mDrmBufOffset(0),
34 mDrmBufSize(0),
35 mDrmBuf(NULL){
James Dong674ebd02010-11-18 20:59:13 -080036
37 mFd = open(filename, O_LARGEFILE | O_RDONLY);
Andreas Huber4ee31e22012-04-11 09:22:52 -070038
39 if (mFd >= 0) {
40 mLength = lseek64(mFd, 0, SEEK_END);
Andreas Huberf4b7d942012-04-11 11:52:03 -070041 } else {
42 ALOGE("Failed to open file '%s'. (%s)", filename, strerror(errno));
Andreas Huber4ee31e22012-04-11 09:22:52 -070043 }
Andreas Huber03475f52009-11-16 15:34:01 -080044}
45
46FileSource::FileSource(int fd, int64_t offset, int64_t length)
James Dong674ebd02010-11-18 20:59:13 -080047 : mFd(fd),
Andreas Huber03475f52009-11-16 15:34:01 -080048 mOffset(offset),
Gloria Wangdcd25ef2010-06-22 13:55:38 -070049 mLength(length),
50 mDecryptHandle(NULL),
51 mDrmManagerClient(NULL),
52 mDrmBufOffset(0),
53 mDrmBufSize(0),
54 mDrmBuf(NULL){
Andreas Huber03475f52009-11-16 15:34:01 -080055 CHECK(offset >= 0);
56 CHECK(length >= 0);
Andreas Huber20111aa2009-07-14 16:56:47 -070057}
58
59FileSource::~FileSource() {
James Dong674ebd02010-11-18 20:59:13 -080060 if (mFd >= 0) {
61 close(mFd);
62 mFd = -1;
Andreas Huber20111aa2009-07-14 16:56:47 -070063 }
Gloria Wangdcd25ef2010-06-22 13:55:38 -070064
65 if (mDrmBuf != NULL) {
66 delete[] mDrmBuf;
67 mDrmBuf = NULL;
68 }
Gloria Wang889b3402011-02-07 11:41:11 -080069
70 if (mDecryptHandle != NULL) {
71 // To release mDecryptHandle
Gloria Wang8f641342011-02-08 13:24:08 -080072 CHECK(mDrmManagerClient);
Gloria Wang889b3402011-02-07 11:41:11 -080073 mDrmManagerClient->closeDecryptSession(mDecryptHandle);
74 mDecryptHandle = NULL;
75 }
76
77 if (mDrmManagerClient != NULL) {
78 delete mDrmManagerClient;
79 mDrmManagerClient = NULL;
80 }
Andreas Huber20111aa2009-07-14 16:56:47 -070081}
82
Andreas Huber34769bc2009-10-23 10:22:30 -070083status_t FileSource::initCheck() const {
James Dong674ebd02010-11-18 20:59:13 -080084 return mFd >= 0 ? OK : NO_INIT;
Andreas Huber20111aa2009-07-14 16:56:47 -070085}
86
James Dongc7fc37a2010-11-16 14:04:54 -080087ssize_t FileSource::readAt(off64_t offset, void *data, size_t size) {
James Dong674ebd02010-11-18 20:59:13 -080088 if (mFd < 0) {
Andreas Huber3d8055a2010-05-24 09:18:36 -070089 return NO_INIT;
90 }
91
Andreas Huber20111aa2009-07-14 16:56:47 -070092 Mutex::Autolock autoLock(mLock);
93
Andreas Huber03475f52009-11-16 15:34:01 -080094 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 Wangdcd25ef2010-06-22 13:55:38 -0700104 if (mDecryptHandle != NULL && DecryptApiType::CONTAINER_BASED
105 == mDecryptHandle->decryptApiType) {
106 return readAtDRM(offset, data, size);
107 } else {
James Dongc7fc37a2010-11-16 14:04:54 -0800108 off64_t result = lseek64(mFd, offset + mOffset, SEEK_SET);
109 if (result == -1) {
Steve Block29357bc2012-01-06 19:20:56 +0000110 ALOGE("seek to %lld failed", offset + mOffset);
Gloria Wangdcd25ef2010-06-22 13:55:38 -0700111 return UNKNOWN_ERROR;
112 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700113
James Dongc7fc37a2010-11-16 14:04:54 -0800114 return ::read(mFd, data, size);
Gloria Wangdcd25ef2010-06-22 13:55:38 -0700115 }
Andreas Huber03475f52009-11-16 15:34:01 -0800116}
Andreas Huber20111aa2009-07-14 16:56:47 -0700117
James Dongc7fc37a2010-11-16 14:04:54 -0800118status_t FileSource::getSize(off64_t *size) {
Andreas Huber3a13fad2011-09-02 09:34:51 -0700119 Mutex::Autolock autoLock(mLock);
120
James Dong674ebd02010-11-18 20:59:13 -0800121 if (mFd < 0) {
Andreas Huber3d8055a2010-05-24 09:18:36 -0700122 return NO_INIT;
123 }
124
Andreas Huber4ee31e22012-04-11 09:22:52 -0700125 *size = mLength;
Andreas Huber03475f52009-11-16 15:34:01 -0800126
127 return OK;
Andreas Huber20111aa2009-07-14 16:56:47 -0700128}
129
James Dong9d2f3862012-01-10 08:24:37 -0800130sp<DecryptHandle> FileSource::DrmInitialization(const char *mime) {
Gloria Wang889b3402011-02-07 11:41:11 -0800131 if (mDrmManagerClient == NULL) {
132 mDrmManagerClient = new DrmManagerClient();
133 }
134
135 if (mDrmManagerClient == NULL) {
Gloria Wangb3714262010-11-01 15:53:16 -0700136 return NULL;
137 }
Gloria Wangb3714262010-11-01 15:53:16 -0700138
Gloria Wangdcd25ef2010-06-22 13:55:38 -0700139 if (mDecryptHandle == NULL) {
140 mDecryptHandle = mDrmManagerClient->openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800141 mFd, mOffset, mLength, mime);
Gloria Wangdcd25ef2010-06-22 13:55:38 -0700142 }
143
144 if (mDecryptHandle == NULL) {
Gloria Wang889b3402011-02-07 11:41:11 -0800145 delete mDrmManagerClient;
Gloria Wangdcd25ef2010-06-22 13:55:38 -0700146 mDrmManagerClient = NULL;
147 }
148
149 return mDecryptHandle;
150}
151
Gloria Wangb5ce3612011-02-24 16:40:57 -0800152void FileSource::getDrmInfo(sp<DecryptHandle> &handle, DrmManagerClient **client) {
153 handle = mDecryptHandle;
Gloria Wangdcd25ef2010-06-22 13:55:38 -0700154
155 *client = mDrmManagerClient;
156}
157
James Dongc7fc37a2010-11-16 14:04:54 -0800158ssize_t FileSource::readAtDRM(off64_t offset, void *data, size_t size) {
Gloria Wangdcd25ef2010-06-22 13:55:38 -0700159 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 Huber20111aa2009-07-14 16:56:47 -0700187} // namespace android