1 /*
2  * Copyright (C) 2018 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 "NdkMediaDataSource"
19 
20 #include "NdkJavaVMHelperPriv.h"
21 #include "NdkMediaDataSourcePriv.h"
22 
23 #include <inttypes.h>
24 #include <jni.h>
25 #include <unistd.h>
26 
27 #include <android_runtime/AndroidRuntime.h>
28 #include <android_util_Binder.h>
29 #include <cutils/properties.h>
30 #include <datasource/DataSourceFactory.h>
31 #include <datasource/HTTPBase.h>
32 #include <datasource/NuCachedSource2.h>
33 #include <media/IMediaHTTPService.h>
34 #include <media/NdkMediaError.h>
35 #include <media/NdkMediaDataSource.h>
36 #include <media/stagefright/InterfaceUtils.h>
37 #include <utils/Log.h>
38 #include <utils/StrongPointer.h>
39 
40 #include "NdkMediaDataSourceCallbacksPriv.h"
41 
42 
43 using namespace android;
44 
45 struct AMediaDataSource {
46     void *userdata;
47     AMediaDataSourceReadAt readAt;
48     AMediaDataSourceGetSize getSize;
49     AMediaDataSourceClose close;
50     AMediaDataSourceGetAvailableSize getAvailableSize;
51     sp<DataSource> mImpl;
52     uint32_t mFlags;
53 };
54 
NdkDataSource(AMediaDataSource * dataSource)55 NdkDataSource::NdkDataSource(AMediaDataSource *dataSource)
56     : mDataSource(AMediaDataSource_new()) {
57     AMediaDataSource_setReadAt(mDataSource, dataSource->readAt);
58     AMediaDataSource_setGetSize(mDataSource, dataSource->getSize);
59     AMediaDataSource_setClose(mDataSource, dataSource->close);
60     AMediaDataSource_setUserdata(mDataSource, dataSource->userdata);
61     AMediaDataSource_setGetAvailableSize(mDataSource, dataSource->getAvailableSize);
62     mDataSource->mImpl = dataSource->mImpl;
63     mDataSource->mFlags = dataSource->mFlags;
64 }
65 
~NdkDataSource()66 NdkDataSource::~NdkDataSource() {
67     AMediaDataSource_delete(mDataSource);
68 }
69 
initCheck() const70 status_t NdkDataSource::initCheck() const {
71     return OK;
72 }
73 
flags()74 uint32_t NdkDataSource::flags() {
75     return mDataSource->mFlags;
76 }
77 
readAt(off64_t offset,void * data,size_t size)78 ssize_t NdkDataSource::readAt(off64_t offset, void *data, size_t size) {
79     Mutex::Autolock l(mLock);
80     if (mDataSource->readAt == NULL || mDataSource->userdata == NULL) {
81         return -1;
82     }
83     return mDataSource->readAt(mDataSource->userdata, offset, data, size);
84 }
85 
getSize(off64_t * size)86 status_t NdkDataSource::getSize(off64_t *size) {
87     Mutex::Autolock l(mLock);
88     if (mDataSource->getSize == NULL || mDataSource->userdata == NULL) {
89         return NO_INIT;
90     }
91     if (size != NULL) {
92         *size = mDataSource->getSize(mDataSource->userdata);
93     }
94     return OK;
95 }
96 
toString()97 String8 NdkDataSource::toString() {
98     return String8::format("NdkDataSource(pid %d, uid %d)", getpid(), getuid());
99 }
100 
getMIMEType() const101 String8 NdkDataSource::getMIMEType() const {
102     return String8("application/octet-stream");
103 }
104 
close()105 void NdkDataSource::close() {
106     if (mDataSource->close != NULL && mDataSource->userdata != NULL) {
107         mDataSource->close(mDataSource->userdata);
108     }
109 }
110 
getAvailableSize(off64_t offset,off64_t * sizeptr)111 status_t NdkDataSource::getAvailableSize(off64_t offset, off64_t *sizeptr) {
112     off64_t size = -1;
113     if (mDataSource->getAvailableSize != NULL
114             && mDataSource->userdata != NULL
115             && sizeptr != NULL) {
116         size = mDataSource->getAvailableSize(mDataSource->userdata, offset);
117         *sizeptr = size;
118     }
119     return size >= 0 ? OK : UNKNOWN_ERROR;
120 }
121 
createMediaHttpServiceFromJavaObj(JNIEnv * env,jobject obj)122 static sp<MediaHTTPService> createMediaHttpServiceFromJavaObj(JNIEnv *env, jobject obj) {
123     if (obj == NULL) {
124         return NULL;
125     }
126     return interface_cast<IMediaHTTPService>(ibinderForJavaObject(env, obj));
127 }
128 
createMediaHttpServiceTemplate(JNIEnv * env,const char * uri,const char * clazz,const char * method,const char * signature)129 static sp<MediaHTTPService> createMediaHttpServiceTemplate(
130         JNIEnv *env,
131         const char *uri,
132         const char *clazz,
133         const char *method,
134         const char *signature) {
135     jobject service = NULL;
136     if (env == NULL) {
137         ALOGE("http service must be created from Java thread");
138         return NULL;
139     }
140 
141     jclass mediahttpclass = env->FindClass(clazz);
142     if (mediahttpclass == NULL) {
143         ALOGE("can't find Media(2)HttpService");
144         env->ExceptionClear();
145         return NULL;
146     }
147 
148     jmethodID mediaHttpCreateMethod = env->GetStaticMethodID(mediahttpclass, method, signature);
149     if (mediaHttpCreateMethod == NULL) {
150         ALOGE("can't find method");
151         env->ExceptionClear();
152         return NULL;
153     }
154 
155     jstring juri = env->NewStringUTF(uri);
156 
157     service = env->CallStaticObjectMethod(mediahttpclass, mediaHttpCreateMethod, juri);
158     env->DeleteLocalRef(juri);
159 
160     env->ExceptionClear();
161     sp<MediaHTTPService> httpService = createMediaHttpServiceFromJavaObj(env, service);
162     return httpService;
163 
164 }
165 
createMediaHttpService(const char * uri)166 sp<MediaHTTPService> createMediaHttpService(const char *uri) {
167 
168     JNIEnv *env;
169     const char *clazz, *method, *signature;
170 
171     env = NdkJavaVMHelper::getJNIEnv();
172 
173     clazz = "android/media/MediaHTTPService";
174     method = "createHttpServiceBinderIfNecessary";
175     signature = "(Ljava/lang/String;)Landroid/os/IBinder;";
176 
177     return createMediaHttpServiceTemplate(env, uri, clazz, method, signature);
178 
179 }
180 
181 extern "C" {
182 
183 EXPORT
AMediaDataSource_new()184 AMediaDataSource* AMediaDataSource_new() {
185     AMediaDataSource *mSource = new AMediaDataSource();
186     mSource->userdata = NULL;
187     mSource->readAt = NULL;
188     mSource->getSize = NULL;
189     mSource->close = NULL;
190     return mSource;
191 }
192 
193 EXPORT
AMediaDataSource_newUri(const char * uri,int numheaders,const char * const * key_values)194 AMediaDataSource* AMediaDataSource_newUri(
195         const char *uri,
196         int numheaders,
197         const char * const *key_values) {
198 
199     sp<MediaHTTPService> service = createMediaHttpService(uri);
200     KeyedVector<String8, String8> headers;
201     for (int i = 0; i < numheaders; ++i) {
202         String8 key8(key_values[i * 2]);
203         String8 value8(key_values[i * 2 + 1]);
204         headers.add(key8, value8);
205     }
206 
207     sp<DataSource> source = DataSourceFactory::getInstance()->CreateFromURI(service, uri, &headers);
208     if (source == NULL) {
209         ALOGE("AMediaDataSource_newUri source is null");
210         return NULL;
211     }
212     ALOGI("AMediaDataSource_newUri source %s flags %u", source->toString().c_str(), source->flags());
213     AMediaDataSource* aSource = convertDataSourceToAMediaDataSource(source);
214     aSource->mImpl = source;
215     aSource->mFlags = source->flags();
216     return aSource;
217 }
218 
219 EXPORT
AMediaDataSource_delete(AMediaDataSource * mSource)220 void AMediaDataSource_delete(AMediaDataSource *mSource) {
221     ALOGV("dtor");
222     if (mSource != NULL) {
223         delete mSource;
224     }
225 }
226 
227 EXPORT
AMediaDataSource_setUserdata(AMediaDataSource * mSource,void * userdata)228 void AMediaDataSource_setUserdata(AMediaDataSource *mSource, void *userdata) {
229     mSource->userdata = userdata;
230 }
231 
232 EXPORT
AMediaDataSource_setReadAt(AMediaDataSource * mSource,AMediaDataSourceReadAt readAt)233 void AMediaDataSource_setReadAt(AMediaDataSource *mSource, AMediaDataSourceReadAt readAt) {
234     mSource->readAt = readAt;
235 }
236 
237 EXPORT
AMediaDataSource_setGetSize(AMediaDataSource * mSource,AMediaDataSourceGetSize getSize)238 void AMediaDataSource_setGetSize(AMediaDataSource *mSource, AMediaDataSourceGetSize getSize) {
239     mSource->getSize = getSize;
240 }
241 
242 EXPORT
AMediaDataSource_setClose(AMediaDataSource * mSource,AMediaDataSourceClose close)243 void AMediaDataSource_setClose(AMediaDataSource *mSource, AMediaDataSourceClose close) {
244     mSource->close = close;
245 }
246 
247 EXPORT
AMediaDataSource_close(AMediaDataSource * mSource)248 void AMediaDataSource_close(AMediaDataSource *mSource) {
249     return mSource->close(mSource->userdata);
250 }
251 
252 EXPORT
AMediaDataSource_setGetAvailableSize(AMediaDataSource * mSource,AMediaDataSourceGetAvailableSize getAvailableSize)253 void AMediaDataSource_setGetAvailableSize(AMediaDataSource *mSource,
254         AMediaDataSourceGetAvailableSize getAvailableSize) {
255     mSource->getAvailableSize = getAvailableSize;
256 }
257 
258 } // extern "C"
259 
260