blob: 3b517f1eafe073222234b578b4229e4c77575480 [file] [log] [blame]
Jeff Browncbad9762012-09-04 21:57:59 -07001/*
2 * Copyright (C) 2012 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_TAG "RemoteDisplay"
18
19#include "jni.h"
Steven Moreland2279b252017-07-19 09:50:45 -070020#include <nativehelper/JNIHelp.h>
Jeff Browncbad9762012-09-04 21:57:59 -070021
22#include "android_os_Parcel.h"
23#include "android_util_Binder.h"
24
Andreas Gampeed6b9df2014-11-20 22:02:20 -080025#include "core_jni_helpers.h"
Jeff Browncbad9762012-09-04 21:57:59 -070026#include <android_runtime/android_view_Surface.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070027#include <android_runtime/Log.h>
Jeff Browncbad9762012-09-04 21:57:59 -070028
29#include <binder/IServiceManager.h>
30
Andy McFaddend47f7d82012-12-18 09:48:38 -080031#include <gui/IGraphicBufferProducer.h>
Jeff Browncbad9762012-09-04 21:57:59 -070032
33#include <media/IMediaPlayerService.h>
34#include <media/IRemoteDisplay.h>
35#include <media/IRemoteDisplayClient.h>
36
37#include <utils/Log.h>
38
Steven Moreland2279b252017-07-19 09:50:45 -070039#include <nativehelper/ScopedUtfChars.h>
Jeff Browncbad9762012-09-04 21:57:59 -070040
41namespace android {
42
43static struct {
44 jmethodID notifyDisplayConnected;
45 jmethodID notifyDisplayDisconnected;
46 jmethodID notifyDisplayError;
47} gRemoteDisplayClassInfo;
48
49// ----------------------------------------------------------------------------
50
51class NativeRemoteDisplayClient : public BnRemoteDisplayClient {
52public:
53 NativeRemoteDisplayClient(JNIEnv* env, jobject remoteDisplayObj) :
54 mRemoteDisplayObjGlobal(env->NewGlobalRef(remoteDisplayObj)) {
55 }
56
57protected:
58 ~NativeRemoteDisplayClient() {
59 JNIEnv* env = AndroidRuntime::getJNIEnv();
60 env->DeleteGlobalRef(mRemoteDisplayObjGlobal);
61 }
62
63public:
Andy McFaddend47f7d82012-12-18 09:48:38 -080064 virtual void onDisplayConnected(const sp<IGraphicBufferProducer>& bufferProducer,
Chong Zhang1f3ecaa2013-05-03 15:55:36 -070065 uint32_t width, uint32_t height, uint32_t flags, uint32_t session) {
Jeff Browncbad9762012-09-04 21:57:59 -070066 JNIEnv* env = AndroidRuntime::getJNIEnv();
67
Mathias Agopian29479eb2013-02-14 14:36:04 -080068 jobject surfaceObj = android_view_Surface_createFromIGraphicBufferProducer(env, bufferProducer);
Jeff Browncbad9762012-09-04 21:57:59 -070069 if (surfaceObj == NULL) {
70 ALOGE("Could not create Surface from surface texture %p provided by media server.",
Andy McFaddend47f7d82012-12-18 09:48:38 -080071 bufferProducer.get());
Jeff Browncbad9762012-09-04 21:57:59 -070072 return;
73 }
74
75 env->CallVoidMethod(mRemoteDisplayObjGlobal,
76 gRemoteDisplayClassInfo.notifyDisplayConnected,
Chong Zhang1f3ecaa2013-05-03 15:55:36 -070077 surfaceObj, width, height, flags, session);
Jeff Browncbad9762012-09-04 21:57:59 -070078 env->DeleteLocalRef(surfaceObj);
79 checkAndClearExceptionFromCallback(env, "notifyDisplayConnected");
80 }
81
82 virtual void onDisplayDisconnected() {
83 JNIEnv* env = AndroidRuntime::getJNIEnv();
84
85 env->CallVoidMethod(mRemoteDisplayObjGlobal,
86 gRemoteDisplayClassInfo.notifyDisplayDisconnected);
87 checkAndClearExceptionFromCallback(env, "notifyDisplayDisconnected");
88 }
89
90 virtual void onDisplayError(int32_t error) {
91 JNIEnv* env = AndroidRuntime::getJNIEnv();
92
93 env->CallVoidMethod(mRemoteDisplayObjGlobal,
94 gRemoteDisplayClassInfo.notifyDisplayError, error);
95 checkAndClearExceptionFromCallback(env, "notifyDisplayError");
96 }
97
98private:
99 jobject mRemoteDisplayObjGlobal;
100
101 static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
102 if (env->ExceptionCheck()) {
103 ALOGE("An exception was thrown by callback '%s'.", methodName);
104 LOGE_EX(env);
105 env->ExceptionClear();
106 }
107 }
108};
109
110class NativeRemoteDisplay {
111public:
112 NativeRemoteDisplay(const sp<IRemoteDisplay>& display,
113 const sp<NativeRemoteDisplayClient>& client) :
114 mDisplay(display), mClient(client) {
115 }
116
117 ~NativeRemoteDisplay() {
118 mDisplay->dispose();
119 }
120
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700121 void pause() {
122 mDisplay->pause();
123 }
124
125 void resume() {
126 mDisplay->resume();
127 }
128
Jeff Browncbad9762012-09-04 21:57:59 -0700129private:
130 sp<IRemoteDisplay> mDisplay;
131 sp<NativeRemoteDisplayClient> mClient;
132};
133
134
135// ----------------------------------------------------------------------------
136
Svet Ganovfa5ecdc2015-04-28 12:03:28 -0700137static jlong nativeListen(JNIEnv* env, jobject remoteDisplayObj, jstring ifaceStr,
138 jstring opPackageNameStr) {
Jeff Browncbad9762012-09-04 21:57:59 -0700139 ScopedUtfChars iface(env, ifaceStr);
Svet Ganovfa5ecdc2015-04-28 12:03:28 -0700140 ScopedUtfChars opPackageName(env, opPackageNameStr);
Jeff Browncbad9762012-09-04 21:57:59 -0700141
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");
147 return 0;
148 }
149
150 sp<NativeRemoteDisplayClient> client(new NativeRemoteDisplayClient(env, remoteDisplayObj));
Svet Ganovfa5ecdc2015-04-28 12:03:28 -0700151 sp<IRemoteDisplay> display = service->listenForRemoteDisplay(String16(opPackageName.c_str()),
Jeff Browncbad9762012-09-04 21:57:59 -0700152 client, String8(iface.c_str()));
153 if (display == NULL) {
154 ALOGE("Media player service rejected request to listen for remote display '%s'.",
155 iface.c_str());
156 return 0;
157 }
158
159 NativeRemoteDisplay* wrapper = new NativeRemoteDisplay(display, client);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000160 return reinterpret_cast<jlong>(wrapper);
Jeff Browncbad9762012-09-04 21:57:59 -0700161}
162
Ashok Bhat075e9a12014-01-06 13:45:09 +0000163static void nativePause(JNIEnv* env, jobject remoteDisplayObj, jlong ptr) {
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700164 NativeRemoteDisplay* wrapper = reinterpret_cast<NativeRemoteDisplay*>(ptr);
165 wrapper->pause();
166}
167
Ashok Bhat075e9a12014-01-06 13:45:09 +0000168static void nativeResume(JNIEnv* env, jobject remoteDisplayObj, jlong ptr) {
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700169 NativeRemoteDisplay* wrapper = reinterpret_cast<NativeRemoteDisplay*>(ptr);
170 wrapper->resume();
171}
172
Ashok Bhat075e9a12014-01-06 13:45:09 +0000173static void nativeDispose(JNIEnv* env, jobject remoteDisplayObj, jlong ptr) {
Jeff Browncbad9762012-09-04 21:57:59 -0700174 NativeRemoteDisplay* wrapper = reinterpret_cast<NativeRemoteDisplay*>(ptr);
175 delete wrapper;
176}
177
178// ----------------------------------------------------------------------------
179
Daniel Micay76f6a862015-09-19 17:31:01 -0400180static const JNINativeMethod gMethods[] = {
Svet Ganovfa5ecdc2015-04-28 12:03:28 -0700181 {"nativeListen", "(Ljava/lang/String;Ljava/lang/String;)J",
Jeff Browncbad9762012-09-04 21:57:59 -0700182 (void*)nativeListen },
Ashok Bhat075e9a12014-01-06 13:45:09 +0000183 {"nativeDispose", "(J)V",
Jeff Browncbad9762012-09-04 21:57:59 -0700184 (void*)nativeDispose },
Ashok Bhat075e9a12014-01-06 13:45:09 +0000185 {"nativePause", "(J)V",
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700186 (void*)nativePause },
Ashok Bhat075e9a12014-01-06 13:45:09 +0000187 {"nativeResume", "(J)V",
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700188 (void*)nativeResume },
Jeff Browncbad9762012-09-04 21:57:59 -0700189};
190
191int register_android_media_RemoteDisplay(JNIEnv* env)
192{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800193 int err = RegisterMethodsOrDie(env, "android/media/RemoteDisplay", gMethods, NELEM(gMethods));
Jeff Browncbad9762012-09-04 21:57:59 -0700194
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800195 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");
Jeff Browncbad9762012-09-04 21:57:59 -0700202 return err;
203}
204
205};