2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "RemoteDisplay"
22 #include "android_os_Parcel.h"
23 #include "android_util_Binder.h"
25 #include "core_jni_helpers.h"
26 #include <android_runtime/android_view_Surface.h>
27 #include <android_runtime/Log.h>
29 #include <binder/IServiceManager.h>
31 #include <gui/IGraphicBufferProducer.h>
33 #include <media/IMediaPlayerService.h>
34 #include <media/IRemoteDisplay.h>
35 #include <media/IRemoteDisplayClient.h>
37 #include <utils/Log.h>
39 #include <ScopedUtfChars.h>
44 jmethodID notifyDisplayConnected;
45 jmethodID notifyDisplayDisconnected;
46 jmethodID notifyDisplayError;
47 } gRemoteDisplayClassInfo;
49 // ----------------------------------------------------------------------------
51 class NativeRemoteDisplayClient : public BnRemoteDisplayClient {
53 NativeRemoteDisplayClient(JNIEnv* env, jobject remoteDisplayObj) :
54 mRemoteDisplayObjGlobal(env->NewGlobalRef(remoteDisplayObj)) {
58 ~NativeRemoteDisplayClient() {
59 JNIEnv* env = AndroidRuntime::getJNIEnv();
60 env->DeleteGlobalRef(mRemoteDisplayObjGlobal);
64 virtual void onDisplayConnected(const sp<IGraphicBufferProducer>& bufferProducer,
65 uint32_t width, uint32_t height, uint32_t flags, uint32_t session) {
66 JNIEnv* env = AndroidRuntime::getJNIEnv();
68 jobject surfaceObj = android_view_Surface_createFromIGraphicBufferProducer(env, bufferProducer);
69 if (surfaceObj == NULL) {
70 ALOGE("Could not create Surface from surface texture %p provided by media server.",
71 bufferProducer.get());
75 env->CallVoidMethod(mRemoteDisplayObjGlobal,
76 gRemoteDisplayClassInfo.notifyDisplayConnected,
77 surfaceObj, width, height, flags, session);
78 env->DeleteLocalRef(surfaceObj);
79 checkAndClearExceptionFromCallback(env, "notifyDisplayConnected");
82 virtual void onDisplayDisconnected() {
83 JNIEnv* env = AndroidRuntime::getJNIEnv();
85 env->CallVoidMethod(mRemoteDisplayObjGlobal,
86 gRemoteDisplayClassInfo.notifyDisplayDisconnected);
87 checkAndClearExceptionFromCallback(env, "notifyDisplayDisconnected");
90 virtual void onDisplayError(int32_t error) {
91 JNIEnv* env = AndroidRuntime::getJNIEnv();
93 env->CallVoidMethod(mRemoteDisplayObjGlobal,
94 gRemoteDisplayClassInfo.notifyDisplayError, error);
95 checkAndClearExceptionFromCallback(env, "notifyDisplayError");
99 jobject mRemoteDisplayObjGlobal;
101 static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
102 if (env->ExceptionCheck()) {
103 ALOGE("An exception was thrown by callback '%s'.", methodName);
105 env->ExceptionClear();
110 class NativeRemoteDisplay {
112 NativeRemoteDisplay(const sp<IRemoteDisplay>& display,
113 const sp<NativeRemoteDisplayClient>& client) :
114 mDisplay(display), mClient(client) {
117 ~NativeRemoteDisplay() {
130 sp<IRemoteDisplay> mDisplay;
131 sp<NativeRemoteDisplayClient> mClient;
135 // ----------------------------------------------------------------------------
137 static jlong nativeListen(JNIEnv* env, jobject remoteDisplayObj, jstring ifaceStr,
138 jstring opPackageNameStr) {
139 ScopedUtfChars iface(env, ifaceStr);
140 ScopedUtfChars opPackageName(env, opPackageNameStr);
142 sp<IServiceManager> sm = defaultServiceManager();
143 sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(
144 sm->getService(String16("media.player")));
145 if (service == NULL) {
146 ALOGE("Could not obtain IMediaPlayerService from service manager");
150 sp<NativeRemoteDisplayClient> client(new NativeRemoteDisplayClient(env, remoteDisplayObj));
151 sp<IRemoteDisplay> display = service->listenForRemoteDisplay(String16(opPackageName.c_str()),
152 client, String8(iface.c_str()));
153 if (display == NULL) {
154 ALOGE("Media player service rejected request to listen for remote display '%s'.",
159 NativeRemoteDisplay* wrapper = new NativeRemoteDisplay(display, client);
160 return reinterpret_cast<jlong>(wrapper);
163 static void nativePause(JNIEnv* env, jobject remoteDisplayObj, jlong ptr) {
164 NativeRemoteDisplay* wrapper = reinterpret_cast<NativeRemoteDisplay*>(ptr);
168 static void nativeResume(JNIEnv* env, jobject remoteDisplayObj, jlong ptr) {
169 NativeRemoteDisplay* wrapper = reinterpret_cast<NativeRemoteDisplay*>(ptr);
173 static void nativeDispose(JNIEnv* env, jobject remoteDisplayObj, jlong ptr) {
174 NativeRemoteDisplay* wrapper = reinterpret_cast<NativeRemoteDisplay*>(ptr);
178 // ----------------------------------------------------------------------------
180 static JNINativeMethod gMethods[] = {
181 {"nativeListen", "(Ljava/lang/String;Ljava/lang/String;)J",
182 (void*)nativeListen },
183 {"nativeDispose", "(J)V",
184 (void*)nativeDispose },
185 {"nativePause", "(J)V",
186 (void*)nativePause },
187 {"nativeResume", "(J)V",
188 (void*)nativeResume },
191 int register_android_media_RemoteDisplay(JNIEnv* env)
193 int err = RegisterMethodsOrDie(env, "android/media/RemoteDisplay", gMethods, NELEM(gMethods));
195 jclass clazz = FindClassOrDie(env, "android/media/RemoteDisplay");
196 gRemoteDisplayClassInfo.notifyDisplayConnected = GetMethodIDOrDie(env,
197 clazz, "notifyDisplayConnected", "(Landroid/view/Surface;IIII)V");
198 gRemoteDisplayClassInfo.notifyDisplayDisconnected = GetMethodIDOrDie(env,
199 clazz, "notifyDisplayDisconnected", "()V");
200 gRemoteDisplayClassInfo.notifyDisplayError = GetMethodIDOrDie(env,
201 clazz, "notifyDisplayError", "(I)V");