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