1 /*
2  * Copyright (C) 2014 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 "HTTPHelper"
19 #include <utils/Log.h>
20 
21 #include "HTTPHelper.h"
22 
23 #include "android_runtime/AndroidRuntime.h"
24 #include "android_util_Binder.h"
25 #include <media/IMediaHTTPService.h>
26 #include <media/stagefright/foundation/ADebug.h>
27 #include <nativehelper/ScopedLocalRef.h>
28 #include "jni.h"
29 
30 namespace android {
31 
CreateHTTPServiceInCurrentJavaContext()32 sp<IMediaHTTPService> CreateHTTPServiceInCurrentJavaContext() {
33     if (AndroidRuntime::getJavaVM() == NULL) {
34         ALOGE("CreateHTTPServiceInCurrentJavaContext called outside "
35               "JAVA environment.");
36         return NULL;
37     }
38 
39     JNIEnv *env = AndroidRuntime::getJNIEnv();
40 
41     ScopedLocalRef<jclass> clazz(
42             env, env->FindClass("android/media/MediaHTTPService"));
43     CHECK(clazz.get() != NULL);
44 
45     jmethodID constructID = env->GetMethodID(clazz.get(), "<init>", "(Ljava/util/List;)V");
46     CHECK(constructID != NULL);
47 
48     ScopedLocalRef<jobject> httpServiceObj(
49             env, env->NewObject(clazz.get(), constructID, NULL));
50 
51     sp<IMediaHTTPService> httpService;
52     if (httpServiceObj.get() != NULL) {
53         jmethodID asBinderID =
54             env->GetMethodID(clazz.get(), "asBinder", "()Landroid/os/IBinder;");
55         CHECK(asBinderID != NULL);
56 
57         ScopedLocalRef<jobject> httpServiceBinderObj(
58                 env, env->CallObjectMethod(httpServiceObj.get(), asBinderID));
59         CHECK(httpServiceBinderObj.get() != NULL);
60 
61         sp<IBinder> binder =
62             ibinderForJavaObject(env, httpServiceBinderObj.get());
63 
64         httpService = interface_cast<IMediaHTTPService>(binder);
65     }
66 
67     return httpService;
68 }
69 
70 }  // namespace android
71