blob: dc3ae5bf7fef96722de7631099db77573106147e [file] [log] [blame]
James Dongc371a022011-04-06 12:16:07 -07001/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18//#define LOG_NDEBUG 0
19#define LOG_TAG "MediaPlayer-JNI"
20#include "utils/Log.h"
21
22#include <media/mediaplayer.h>
Andreas Huberd2506a52014-01-29 10:32:46 -080023#include <media/IMediaHTTPService.h>
Nicolas Catania20cb94e2009-05-12 23:25:55 -070024#include <media/MediaPlayerInterface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <stdio.h>
26#include <assert.h>
27#include <limits.h>
28#include <unistd.h>
29#include <fcntl.h>
30#include <utils/threads.h>
31#include "jni.h"
32#include "JNIHelp.h"
33#include "android_runtime/AndroidRuntime.h"
Ted Bonkenburg1ee60112011-07-26 09:51:18 -070034#include "android_runtime/android_view_Surface.h"
Ruben Brunk87eac992013-09-09 17:44:59 -070035#include "android_runtime/Log.h"
The Android Open Source Project4df24232009-03-05 14:34:35 -080036#include "utils/Errors.h" // for status_t
Andreas Huber25643002010-01-28 11:19:57 -080037#include "utils/KeyedVector.h"
38#include "utils/String8.h"
James Dong79f407c2011-05-05 12:50:04 -070039#include "android_media_Utils.h"
40
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -080041#include "android_os_Parcel.h"
Nicolas Catania20cb94e2009-05-12 23:25:55 -070042#include "android_util_Binder.h"
43#include <binder/Parcel.h>
Andy McFaddend47f7d82012-12-18 09:48:38 -080044#include <gui/IGraphicBufferProducer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080045#include <gui/Surface.h>
Gloria Wangd211f412011-02-19 18:37:57 -080046#include <binder/IPCThreadState.h>
47#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Andreas Huberd2506a52014-01-29 10:32:46 -080049#include "android_util_Binder.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050// ----------------------------------------------------------------------------
51
52using namespace android;
53
54// ----------------------------------------------------------------------------
55
56struct fields_t {
57 jfieldID context;
Ted Bonkenburg1ee60112011-07-26 09:51:18 -070058 jfieldID surface_texture;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60 jmethodID post_event;
Andreas Huberd5f9fa52013-05-28 14:39:39 -070061
62 jmethodID proxyConfigGetHost;
63 jmethodID proxyConfigGetPort;
64 jmethodID proxyConfigGetExclusionList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065};
66static fields_t fields;
67
68static Mutex sLock;
69
70// ----------------------------------------------------------------------------
71// ref-counted object for callbacks
72class JNIMediaPlayerListener: public MediaPlayerListener
73{
74public:
75 JNIMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz);
76 ~JNIMediaPlayerListener();
Gloria Wang162ee492011-04-11 17:23:27 -070077 virtual void notify(int msg, int ext1, int ext2, const Parcel *obj = NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078private:
79 JNIMediaPlayerListener();
80 jclass mClass; // Reference to MediaPlayer class
81 jobject mObject; // Weak ref to MediaPlayer Java object to call on
82};
83
84JNIMediaPlayerListener::JNIMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz)
85{
86
87 // Hold onto the MediaPlayer class for use in calling the static method
88 // that posts events to the application thread.
89 jclass clazz = env->GetObjectClass(thiz);
90 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +000091 ALOGE("Can't find android/media/MediaPlayer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 jniThrowException(env, "java/lang/Exception", NULL);
93 return;
94 }
95 mClass = (jclass)env->NewGlobalRef(clazz);
96
97 // We use a weak reference so the MediaPlayer object can be garbage collected.
98 // The reference is only used as a proxy for callbacks.
99 mObject = env->NewGlobalRef(weak_thiz);
100}
101
102JNIMediaPlayerListener::~JNIMediaPlayerListener()
103{
104 // remove global references
105 JNIEnv *env = AndroidRuntime::getJNIEnv();
106 env->DeleteGlobalRef(mObject);
107 env->DeleteGlobalRef(mClass);
108}
109
Gloria Wang162ee492011-04-11 17:23:27 -0700110void JNIMediaPlayerListener::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111{
112 JNIEnv *env = AndroidRuntime::getJNIEnv();
Gloria Wang162ee492011-04-11 17:23:27 -0700113 if (obj && obj->dataSize() > 0) {
Insun Kang333c0992012-07-10 12:47:03 +0900114 jobject jParcel = createJavaParcelObject(env);
115 if (jParcel != NULL) {
116 Parcel* nativeParcel = parcelForJavaObject(env, jParcel);
Insun Kang89020972012-05-01 14:13:19 +0900117 nativeParcel->setData(obj->data(), obj->dataSize());
Gloria Wang162ee492011-04-11 17:23:27 -0700118 env->CallStaticVoidMethod(mClass, fields.post_event, mObject,
Insun Kang333c0992012-07-10 12:47:03 +0900119 msg, ext1, ext2, jParcel);
Elliott Hughes99f75212013-11-13 15:10:40 -0800120 env->DeleteLocalRef(jParcel);
Gloria Wang162ee492011-04-11 17:23:27 -0700121 }
122 } else {
123 env->CallStaticVoidMethod(mClass, fields.post_event, mObject,
124 msg, ext1, ext2, NULL);
125 }
Insun Kang89020972012-05-01 14:13:19 +0900126 if (env->ExceptionCheck()) {
127 ALOGW("An exception occurred while notifying an event.");
128 LOGW_EX(env);
129 env->ExceptionClear();
130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131}
132
133// ----------------------------------------------------------------------------
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135static sp<MediaPlayer> getMediaPlayer(JNIEnv* env, jobject thiz)
136{
137 Mutex::Autolock l(sLock);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000138 MediaPlayer* const p = (MediaPlayer*)env->GetLongField(thiz, fields.context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 return sp<MediaPlayer>(p);
140}
141
142static sp<MediaPlayer> setMediaPlayer(JNIEnv* env, jobject thiz, const sp<MediaPlayer>& player)
143{
144 Mutex::Autolock l(sLock);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000145 sp<MediaPlayer> old = (MediaPlayer*)env->GetLongField(thiz, fields.context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 if (player.get()) {
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800147 player->incStrong((void*)setMediaPlayer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 }
149 if (old != 0) {
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800150 old->decStrong((void*)setMediaPlayer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 }
Ashok Bhat075e9a12014-01-06 13:45:09 +0000152 env->SetLongField(thiz, fields.context, (jlong)player.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 return old;
154}
155
Nicolas Catania32f82772009-06-11 16:33:49 -0700156// If exception is NULL and opStatus is not OK, this method sends an error
157// event to the client application; otherwise, if exception is not NULL and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158// opStatus is not OK, this method throws the given exception to the client
159// application.
160static void process_media_player_call(JNIEnv *env, jobject thiz, status_t opStatus, const char* exception, const char *message)
161{
162 if (exception == NULL) { // Don't throw exception. Instead, send an event.
163 if (opStatus != (status_t) OK) {
164 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
165 if (mp != 0) mp->notify(MEDIA_ERROR, opStatus, 0);
166 }
167 } else { // Throw exception!
168 if ( opStatus == (status_t) INVALID_OPERATION ) {
169 jniThrowException(env, "java/lang/IllegalStateException", NULL);
Dave Burkefc301b02011-08-30 14:39:17 +0100170 } else if ( opStatus == (status_t) PERMISSION_DENIED ) {
171 jniThrowException(env, "java/lang/SecurityException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 } else if ( opStatus != (status_t) OK ) {
173 if (strlen(message) > 230) {
174 // if the message is too long, don't bother displaying the status code
175 jniThrowException( env, exception, message);
176 } else {
177 char msg[256];
178 // append the status code to the message
179 sprintf(msg, "%s: status=0x%X", message, opStatus);
180 jniThrowException( env, exception, msg);
181 }
182 }
183 }
184}
185
186static void
Andreas Huber25643002010-01-28 11:19:57 -0800187android_media_MediaPlayer_setDataSourceAndHeaders(
Andreas Huberd2506a52014-01-29 10:32:46 -0800188 JNIEnv *env, jobject thiz, jobject httpServiceBinderObj, jstring path,
James Dong17524dc2011-05-04 13:41:58 -0700189 jobjectArray keys, jobjectArray values) {
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
192 if (mp == NULL ) {
193 jniThrowException(env, "java/lang/IllegalStateException", NULL);
194 return;
195 }
196
197 if (path == NULL) {
198 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
199 return;
200 }
201
James Dongc371a022011-04-06 12:16:07 -0700202 const char *tmp = env->GetStringUTFChars(path, NULL);
203 if (tmp == NULL) { // Out of memory
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 return;
205 }
Steve Block71f2cf12011-10-20 11:56:00 +0100206 ALOGV("setDataSource: path %s", tmp);
Andreas Huber25643002010-01-28 11:19:57 -0800207
James Dongc371a022011-04-06 12:16:07 -0700208 String8 pathStr(tmp);
209 env->ReleaseStringUTFChars(path, tmp);
210 tmp = NULL;
211
James Dong17524dc2011-05-04 13:41:58 -0700212 // We build a KeyedVector out of the key and val arrays
Andreas Huber25643002010-01-28 11:19:57 -0800213 KeyedVector<String8, String8> headersVector;
James Dong79f407c2011-05-05 12:50:04 -0700214 if (!ConvertKeyValueArraysToKeyedVector(
215 env, keys, values, &headersVector)) {
216 return;
Andreas Huber25643002010-01-28 11:19:57 -0800217 }
218
Andreas Huberd2506a52014-01-29 10:32:46 -0800219 sp<IMediaHTTPService> httpService;
220 if (httpServiceBinderObj != NULL) {
221 sp<IBinder> binder = ibinderForJavaObject(env, httpServiceBinderObj);
222 httpService = interface_cast<IMediaHTTPService>(binder);
223 }
224
Andreas Huber25643002010-01-28 11:19:57 -0800225 status_t opStatus =
226 mp->setDataSource(
Andreas Huberd2506a52014-01-29 10:32:46 -0800227 httpService,
James Dongc371a022011-04-06 12:16:07 -0700228 pathStr,
James Dong79f407c2011-05-05 12:50:04 -0700229 headersVector.size() > 0? &headersVector : NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
Andreas Huber25643002010-01-28 11:19:57 -0800231 process_media_player_call(
232 env, thiz, opStatus, "java/io/IOException",
233 "setDataSource failed." );
234}
235
236static void
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237android_media_MediaPlayer_setDataSourceFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length)
238{
239 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
240 if (mp == NULL ) {
241 jniThrowException(env, "java/lang/IllegalStateException", NULL);
242 return;
243 }
244
245 if (fileDescriptor == NULL) {
246 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
247 return;
248 }
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700249 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
Steve Block71f2cf12011-10-20 11:56:00 +0100250 ALOGV("setDataSourceFD: fd %d", fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 process_media_player_call( env, thiz, mp->setDataSource(fd, offset, length), "java/io/IOException", "setDataSourceFD failed." );
252}
253
Andy McFaddend47f7d82012-12-18 09:48:38 -0800254static sp<IGraphicBufferProducer>
Ted Bonkenburg1ee60112011-07-26 09:51:18 -0700255getVideoSurfaceTexture(JNIEnv* env, jobject thiz) {
Ashok Bhat075e9a12014-01-06 13:45:09 +0000256 IGraphicBufferProducer * const p = (IGraphicBufferProducer*)env->GetLongField(thiz, fields.surface_texture);
Andy McFaddend47f7d82012-12-18 09:48:38 -0800257 return sp<IGraphicBufferProducer>(p);
Dave Sparks8b0b1742009-05-29 09:01:20 -0700258}
259
260static void
Gloria Wangd59310d2011-09-14 13:59:45 -0700261decVideoSurfaceRef(JNIEnv *env, jobject thiz)
262{
Gloria Wange828beb2011-09-15 15:28:43 -0700263 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
264 if (mp == NULL) {
265 return;
266 }
267
Andy McFaddend47f7d82012-12-18 09:48:38 -0800268 sp<IGraphicBufferProducer> old_st = getVideoSurfaceTexture(env, thiz);
Gloria Wangd59310d2011-09-14 13:59:45 -0700269 if (old_st != NULL) {
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800270 old_st->decStrong((void*)decVideoSurfaceRef);
Gloria Wangd59310d2011-09-14 13:59:45 -0700271 }
272}
273
274static void
James Dong43ef9132011-08-12 11:33:27 -0700275setVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface, jboolean mediaPlayerMustBeAlive)
Dave Sparks8b0b1742009-05-29 09:01:20 -0700276{
277 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
James Dong43ef9132011-08-12 11:33:27 -0700278 if (mp == NULL) {
279 if (mediaPlayerMustBeAlive) {
280 jniThrowException(env, "java/lang/IllegalStateException", NULL);
281 }
Dave Sparks8b0b1742009-05-29 09:01:20 -0700282 return;
283 }
Ted Bonkenburg1ee60112011-07-26 09:51:18 -0700284
Gloria Wangd59310d2011-09-14 13:59:45 -0700285 decVideoSurfaceRef(env, thiz);
286
Andy McFaddend47f7d82012-12-18 09:48:38 -0800287 sp<IGraphicBufferProducer> new_st;
Ted Bonkenburg1ee60112011-07-26 09:51:18 -0700288 if (jsurface) {
Jeff Brown64a55af2012-08-26 02:47:39 -0700289 sp<Surface> surface(android_view_Surface_getSurface(env, jsurface));
Jamie Gennisf76afc82011-10-14 19:06:55 -0700290 if (surface != NULL) {
Mathias Agopian52800612013-02-14 17:11:20 -0800291 new_st = surface->getIGraphicBufferProducer();
James Dong097922b2012-10-04 09:16:40 -0700292 if (new_st == NULL) {
293 jniThrowException(env, "java/lang/IllegalArgumentException",
294 "The surface does not have a binding SurfaceTexture!");
295 return;
296 }
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800297 new_st->incStrong((void*)decVideoSurfaceRef);
Jamie Gennisf76afc82011-10-14 19:06:55 -0700298 } else {
299 jniThrowException(env, "java/lang/IllegalArgumentException",
300 "The surface has been released");
301 return;
302 }
Ted Bonkenburg1ee60112011-07-26 09:51:18 -0700303 }
Gloria Wangd59310d2011-09-14 13:59:45 -0700304
Ashok Bhat075e9a12014-01-06 13:45:09 +0000305 env->SetLongField(thiz, fields.surface_texture, (jlong)new_st.get());
Ted Bonkenburg1ee60112011-07-26 09:51:18 -0700306
307 // This will fail if the media player has not been initialized yet. This
308 // can be the case if setDisplay() on MediaPlayer.java has been called
309 // before setDataSource(). The redundant call to setVideoSurfaceTexture()
310 // in prepare/prepareAsync covers for this case.
311 mp->setVideoSurfaceTexture(new_st);
Dave Sparks8b0b1742009-05-29 09:01:20 -0700312}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313
314static void
James Dong43ef9132011-08-12 11:33:27 -0700315android_media_MediaPlayer_setVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface)
316{
317 setVideoSurface(env, thiz, jsurface, true /* mediaPlayerMustBeAlive */);
318}
319
320static void
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321android_media_MediaPlayer_prepare(JNIEnv *env, jobject thiz)
322{
323 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
324 if (mp == NULL ) {
325 jniThrowException(env, "java/lang/IllegalStateException", NULL);
326 return;
327 }
Ted Bonkenburg1ee60112011-07-26 09:51:18 -0700328
329 // Handle the case where the display surface was set before the mp was
330 // initialized. We try again to make it stick.
Andy McFaddend47f7d82012-12-18 09:48:38 -0800331 sp<IGraphicBufferProducer> st = getVideoSurfaceTexture(env, thiz);
Ted Bonkenburg1ee60112011-07-26 09:51:18 -0700332 mp->setVideoSurfaceTexture(st);
333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 process_media_player_call( env, thiz, mp->prepare(), "java/io/IOException", "Prepare failed." );
335}
336
337static void
338android_media_MediaPlayer_prepareAsync(JNIEnv *env, jobject thiz)
339{
340 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
341 if (mp == NULL ) {
342 jniThrowException(env, "java/lang/IllegalStateException", NULL);
343 return;
344 }
Ted Bonkenburg1ee60112011-07-26 09:51:18 -0700345
346 // Handle the case where the display surface was set before the mp was
347 // initialized. We try again to make it stick.
Andy McFaddend47f7d82012-12-18 09:48:38 -0800348 sp<IGraphicBufferProducer> st = getVideoSurfaceTexture(env, thiz);
Ted Bonkenburg1ee60112011-07-26 09:51:18 -0700349 mp->setVideoSurfaceTexture(st);
350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 process_media_player_call( env, thiz, mp->prepareAsync(), "java/io/IOException", "Prepare Async failed." );
352}
353
354static void
355android_media_MediaPlayer_start(JNIEnv *env, jobject thiz)
356{
Steve Block71f2cf12011-10-20 11:56:00 +0100357 ALOGV("start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
359 if (mp == NULL ) {
360 jniThrowException(env, "java/lang/IllegalStateException", NULL);
361 return;
362 }
363 process_media_player_call( env, thiz, mp->start(), NULL, NULL );
364}
365
366static void
367android_media_MediaPlayer_stop(JNIEnv *env, jobject thiz)
368{
Steve Block71f2cf12011-10-20 11:56:00 +0100369 ALOGV("stop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
371 if (mp == NULL ) {
372 jniThrowException(env, "java/lang/IllegalStateException", NULL);
373 return;
374 }
Nicolas Catania32f82772009-06-11 16:33:49 -0700375 process_media_player_call( env, thiz, mp->stop(), NULL, NULL );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376}
377
378static void
379android_media_MediaPlayer_pause(JNIEnv *env, jobject thiz)
380{
Steve Block71f2cf12011-10-20 11:56:00 +0100381 ALOGV("pause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
383 if (mp == NULL ) {
384 jniThrowException(env, "java/lang/IllegalStateException", NULL);
385 return;
386 }
387 process_media_player_call( env, thiz, mp->pause(), NULL, NULL );
388}
389
390static jboolean
391android_media_MediaPlayer_isPlaying(JNIEnv *env, jobject thiz)
392{
393 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
394 if (mp == NULL ) {
395 jniThrowException(env, "java/lang/IllegalStateException", NULL);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000396 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 }
The Android Open Source Project4df24232009-03-05 14:34:35 -0800398 const jboolean is_playing = mp->isPlaying();
399
Steve Block71f2cf12011-10-20 11:56:00 +0100400 ALOGV("isPlaying: %d", is_playing);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800401 return is_playing;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402}
403
404static void
Ashok Bhat075e9a12014-01-06 13:45:09 +0000405android_media_MediaPlayer_seekTo(JNIEnv *env, jobject thiz, jint msec)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406{
407 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
408 if (mp == NULL ) {
409 jniThrowException(env, "java/lang/IllegalStateException", NULL);
410 return;
411 }
Steve Block71f2cf12011-10-20 11:56:00 +0100412 ALOGV("seekTo: %d(msec)", msec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 process_media_player_call( env, thiz, mp->seekTo(msec), NULL, NULL );
414}
415
Ashok Bhat075e9a12014-01-06 13:45:09 +0000416static jint
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417android_media_MediaPlayer_getVideoWidth(JNIEnv *env, jobject thiz)
418{
419 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
420 if (mp == NULL ) {
421 jniThrowException(env, "java/lang/IllegalStateException", NULL);
422 return 0;
423 }
424 int w;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800425 if (0 != mp->getVideoWidth(&w)) {
Steve Block3762c312012-01-06 19:20:56 +0000426 ALOGE("getVideoWidth failed");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800427 w = 0;
428 }
Steve Block71f2cf12011-10-20 11:56:00 +0100429 ALOGV("getVideoWidth: %d", w);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000430 return (jint) w;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431}
432
Ashok Bhat075e9a12014-01-06 13:45:09 +0000433static jint
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434android_media_MediaPlayer_getVideoHeight(JNIEnv *env, jobject thiz)
435{
436 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
437 if (mp == NULL ) {
438 jniThrowException(env, "java/lang/IllegalStateException", NULL);
439 return 0;
440 }
441 int h;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800442 if (0 != mp->getVideoHeight(&h)) {
Steve Block3762c312012-01-06 19:20:56 +0000443 ALOGE("getVideoHeight failed");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800444 h = 0;
445 }
Steve Block71f2cf12011-10-20 11:56:00 +0100446 ALOGV("getVideoHeight: %d", h);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000447 return (jint) h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448}
449
450
Ashok Bhat075e9a12014-01-06 13:45:09 +0000451static jint
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452android_media_MediaPlayer_getCurrentPosition(JNIEnv *env, jobject thiz)
453{
454 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
455 if (mp == NULL ) {
456 jniThrowException(env, "java/lang/IllegalStateException", NULL);
457 return 0;
458 }
459 int msec;
460 process_media_player_call( env, thiz, mp->getCurrentPosition(&msec), NULL, NULL );
Steve Block71f2cf12011-10-20 11:56:00 +0100461 ALOGV("getCurrentPosition: %d (msec)", msec);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000462 return (jint) msec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463}
464
Ashok Bhat075e9a12014-01-06 13:45:09 +0000465static jint
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466android_media_MediaPlayer_getDuration(JNIEnv *env, jobject thiz)
467{
468 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
469 if (mp == NULL ) {
470 jniThrowException(env, "java/lang/IllegalStateException", NULL);
471 return 0;
472 }
473 int msec;
474 process_media_player_call( env, thiz, mp->getDuration(&msec), NULL, NULL );
Steve Block71f2cf12011-10-20 11:56:00 +0100475 ALOGV("getDuration: %d (msec)", msec);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000476 return (jint) msec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477}
478
479static void
480android_media_MediaPlayer_reset(JNIEnv *env, jobject thiz)
481{
Steve Block71f2cf12011-10-20 11:56:00 +0100482 ALOGV("reset");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
484 if (mp == NULL ) {
485 jniThrowException(env, "java/lang/IllegalStateException", NULL);
486 return;
487 }
488 process_media_player_call( env, thiz, mp->reset(), NULL, NULL );
489}
490
491static void
Ashok Bhat075e9a12014-01-06 13:45:09 +0000492android_media_MediaPlayer_setAudioStreamType(JNIEnv *env, jobject thiz, jint streamtype)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493{
Steve Block71f2cf12011-10-20 11:56:00 +0100494 ALOGV("setAudioStreamType: %d", streamtype);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
496 if (mp == NULL ) {
497 jniThrowException(env, "java/lang/IllegalStateException", NULL);
498 return;
499 }
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800500 process_media_player_call( env, thiz, mp->setAudioStreamType((audio_stream_type_t) streamtype) , NULL, NULL );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501}
502
503static void
504android_media_MediaPlayer_setLooping(JNIEnv *env, jobject thiz, jboolean looping)
505{
Steve Block71f2cf12011-10-20 11:56:00 +0100506 ALOGV("setLooping: %d", looping);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
508 if (mp == NULL ) {
509 jniThrowException(env, "java/lang/IllegalStateException", NULL);
510 return;
511 }
512 process_media_player_call( env, thiz, mp->setLooping(looping), NULL, NULL );
513}
514
515static jboolean
516android_media_MediaPlayer_isLooping(JNIEnv *env, jobject thiz)
517{
Steve Block71f2cf12011-10-20 11:56:00 +0100518 ALOGV("isLooping");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
520 if (mp == NULL ) {
521 jniThrowException(env, "java/lang/IllegalStateException", NULL);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000522 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 }
Ashok Bhat075e9a12014-01-06 13:45:09 +0000524 return mp->isLooping() ? JNI_TRUE : JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525}
526
527static void
Ashok Bhat075e9a12014-01-06 13:45:09 +0000528android_media_MediaPlayer_setVolume(JNIEnv *env, jobject thiz, jfloat leftVolume, jfloat rightVolume)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529{
Ashok Bhat075e9a12014-01-06 13:45:09 +0000530 ALOGV("setVolume: left %f right %f", (float) leftVolume, (float) rightVolume);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
532 if (mp == NULL ) {
533 jniThrowException(env, "java/lang/IllegalStateException", NULL);
534 return;
535 }
Ashok Bhat075e9a12014-01-06 13:45:09 +0000536 process_media_player_call( env, thiz, mp->setVolume((float) leftVolume, (float) rightVolume), NULL, NULL );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537}
538
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700539// Sends the request and reply parcels to the media player via the
540// binder interface.
541static jint
542android_media_MediaPlayer_invoke(JNIEnv *env, jobject thiz,
543 jobject java_request, jobject java_reply)
544{
545 sp<MediaPlayer> media_player = getMediaPlayer(env, thiz);
546 if (media_player == NULL ) {
547 jniThrowException(env, "java/lang/IllegalStateException", NULL);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700548 return UNKNOWN_ERROR;
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700549 }
550
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700551 Parcel *request = parcelForJavaObject(env, java_request);
552 Parcel *reply = parcelForJavaObject(env, java_reply);
553
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700554 // Don't use process_media_player_call which use the async loop to
555 // report errors, instead returns the status.
Ashok Bhat075e9a12014-01-06 13:45:09 +0000556 return (jint) media_player->invoke(*request, reply);
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700557}
558
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700559// Sends the new filter to the client.
560static jint
561android_media_MediaPlayer_setMetadataFilter(JNIEnv *env, jobject thiz, jobject request)
562{
563 sp<MediaPlayer> media_player = getMediaPlayer(env, thiz);
564 if (media_player == NULL ) {
565 jniThrowException(env, "java/lang/IllegalStateException", NULL);
566 return UNKNOWN_ERROR;
567 }
568
569 Parcel *filter = parcelForJavaObject(env, request);
570
Nicolas Catania5d55c712009-07-09 09:21:33 -0700571 if (filter == NULL ) {
572 jniThrowException(env, "java/lang/RuntimeException", "Filter is null");
573 return UNKNOWN_ERROR;
574 }
575
Ashok Bhat075e9a12014-01-06 13:45:09 +0000576 return (jint) media_player->setMetadataFilter(*filter);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700577}
578
Nicolas Catania5d55c712009-07-09 09:21:33 -0700579static jboolean
580android_media_MediaPlayer_getMetadata(JNIEnv *env, jobject thiz, jboolean update_only,
581 jboolean apply_filter, jobject reply)
582{
583 sp<MediaPlayer> media_player = getMediaPlayer(env, thiz);
584 if (media_player == NULL ) {
585 jniThrowException(env, "java/lang/IllegalStateException", NULL);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000586 return JNI_FALSE;
Nicolas Catania5d55c712009-07-09 09:21:33 -0700587 }
588
589 Parcel *metadata = parcelForJavaObject(env, reply);
590
591 if (metadata == NULL ) {
592 jniThrowException(env, "java/lang/RuntimeException", "Reply parcel is null");
Ashok Bhat075e9a12014-01-06 13:45:09 +0000593 return JNI_FALSE;
Nicolas Catania5d55c712009-07-09 09:21:33 -0700594 }
595
596 metadata->freeData();
597 // On return metadata is positioned at the beginning of the
598 // metadata. Note however that the parcel actually starts with the
599 // return code so you should not rewind the parcel using
600 // setDataPosition(0).
Ashok Bhat075e9a12014-01-06 13:45:09 +0000601 if (media_player->getMetadata(update_only, apply_filter, metadata) == OK) {
602 return JNI_TRUE;
603 } else {
604 return JNI_FALSE;
605 }
Nicolas Catania5d55c712009-07-09 09:21:33 -0700606}
607
Marco Nelissen4935d052009-08-03 11:12:58 -0700608// This function gets some field IDs, which in turn causes class initialization.
609// It is called from a static block in MediaPlayer, which won't run until the
610// first time an instance of this class is used.
611static void
612android_media_MediaPlayer_native_init(JNIEnv *env)
613{
614 jclass clazz;
615
616 clazz = env->FindClass("android/media/MediaPlayer");
617 if (clazz == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700618 return;
619 }
620
Ashok Bhat075e9a12014-01-06 13:45:09 +0000621 fields.context = env->GetFieldID(clazz, "mNativeContext", "J");
Marco Nelissen4935d052009-08-03 11:12:58 -0700622 if (fields.context == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700623 return;
624 }
625
626 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
627 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
628 if (fields.post_event == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700629 return;
630 }
631
Ashok Bhat075e9a12014-01-06 13:45:09 +0000632 fields.surface_texture = env->GetFieldID(clazz, "mNativeSurfaceTexture", "J");
Ted Bonkenburg1ee60112011-07-26 09:51:18 -0700633 if (fields.surface_texture == NULL) {
Glenn Kastencc562a32011-02-08 17:26:17 -0800634 return;
635 }
Andreas Huberd5f9fa52013-05-28 14:39:39 -0700636
637 clazz = env->FindClass("android/net/ProxyProperties");
638 if (clazz == NULL) {
639 return;
640 }
641
642 fields.proxyConfigGetHost =
643 env->GetMethodID(clazz, "getHost", "()Ljava/lang/String;");
644
645 fields.proxyConfigGetPort =
646 env->GetMethodID(clazz, "getPort", "()I");
647
648 fields.proxyConfigGetExclusionList =
649 env->GetMethodID(clazz, "getExclusionList", "()Ljava/lang/String;");
Marco Nelissen4935d052009-08-03 11:12:58 -0700650}
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652static void
653android_media_MediaPlayer_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
654{
Steve Block71f2cf12011-10-20 11:56:00 +0100655 ALOGV("native_setup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 sp<MediaPlayer> mp = new MediaPlayer();
657 if (mp == NULL) {
658 jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
659 return;
660 }
661
662 // create new listener and give it to MediaPlayer
663 sp<JNIMediaPlayerListener> listener = new JNIMediaPlayerListener(env, thiz, weak_this);
664 mp->setListener(listener);
665
666 // Stow our new C++ MediaPlayer in an opaque field in the Java object.
667 setMediaPlayer(env, thiz, mp);
668}
669
670static void
671android_media_MediaPlayer_release(JNIEnv *env, jobject thiz)
672{
Steve Block71f2cf12011-10-20 11:56:00 +0100673 ALOGV("release");
Gloria Wangd59310d2011-09-14 13:59:45 -0700674 decVideoSurfaceRef(env, thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 sp<MediaPlayer> mp = setMediaPlayer(env, thiz, 0);
676 if (mp != NULL) {
677 // this prevents native callbacks after the object is released
678 mp->setListener(0);
679 mp->disconnect();
680 }
681}
682
683static void
684android_media_MediaPlayer_native_finalize(JNIEnv *env, jobject thiz)
685{
Steve Block71f2cf12011-10-20 11:56:00 +0100686 ALOGV("native_finalize");
Marco Nelissen8dc20842011-09-28 09:21:11 -0700687 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
688 if (mp != NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000689 ALOGW("MediaPlayer finalized without being released");
Marco Nelissen8dc20842011-09-28 09:21:11 -0700690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 android_media_MediaPlayer_release(env, thiz);
692}
693
Eric Laurent619346f2010-06-21 09:27:30 -0700694static void android_media_MediaPlayer_set_audio_session_id(JNIEnv *env, jobject thiz, jint sessionId) {
Steve Block71f2cf12011-10-20 11:56:00 +0100695 ALOGV("set_session_id(): %d", sessionId);
Eric Laurent619346f2010-06-21 09:27:30 -0700696 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
697 if (mp == NULL ) {
698 jniThrowException(env, "java/lang/IllegalStateException", NULL);
699 return;
700 }
701 process_media_player_call( env, thiz, mp->setAudioSessionId(sessionId), NULL, NULL );
702}
703
704static jint android_media_MediaPlayer_get_audio_session_id(JNIEnv *env, jobject thiz) {
Steve Block71f2cf12011-10-20 11:56:00 +0100705 ALOGV("get_session_id()");
Eric Laurent619346f2010-06-21 09:27:30 -0700706 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
707 if (mp == NULL ) {
708 jniThrowException(env, "java/lang/IllegalStateException", NULL);
709 return 0;
710 }
711
Ashok Bhat075e9a12014-01-06 13:45:09 +0000712 return (jint) mp->getAudioSessionId();
Eric Laurent619346f2010-06-21 09:27:30 -0700713}
714
Eric Laurent7070b362010-07-16 07:43:46 -0700715static void
716android_media_MediaPlayer_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level)
717{
Steve Block71f2cf12011-10-20 11:56:00 +0100718 ALOGV("setAuxEffectSendLevel: level %f", level);
Eric Laurent7070b362010-07-16 07:43:46 -0700719 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
720 if (mp == NULL ) {
721 jniThrowException(env, "java/lang/IllegalStateException", NULL);
722 return;
723 }
724 process_media_player_call( env, thiz, mp->setAuxEffectSendLevel(level), NULL, NULL );
725}
726
727static void android_media_MediaPlayer_attachAuxEffect(JNIEnv *env, jobject thiz, jint effectId) {
Steve Block71f2cf12011-10-20 11:56:00 +0100728 ALOGV("attachAuxEffect(): %d", effectId);
Eric Laurent7070b362010-07-16 07:43:46 -0700729 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
730 if (mp == NULL ) {
731 jniThrowException(env, "java/lang/IllegalStateException", NULL);
732 return;
733 }
734 process_media_player_call( env, thiz, mp->attachAuxEffect(effectId), NULL, NULL );
735}
736
Gloria Wangd211f412011-02-19 18:37:57 -0800737static jint
Andreas Huberd2506a52014-01-29 10:32:46 -0800738android_media_MediaPlayer_pullBatteryData(
739 JNIEnv *env, jobject /* thiz */, jobject java_reply)
Gloria Wangd211f412011-02-19 18:37:57 -0800740{
741 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.player"));
742 sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
743 if (service.get() == NULL) {
744 jniThrowException(env, "java/lang/RuntimeException", "cannot get MediaPlayerService");
745 return UNKNOWN_ERROR;
746 }
747
748 Parcel *reply = parcelForJavaObject(env, java_reply);
749
Ashok Bhat075e9a12014-01-06 13:45:09 +0000750 return (jint) service->pullBatteryData(reply);
Gloria Wangd211f412011-02-19 18:37:57 -0800751}
752
John Grossman720aa282012-02-22 15:38:35 -0800753static jint
754android_media_MediaPlayer_setRetransmitEndpoint(JNIEnv *env, jobject thiz,
755 jstring addrString, jint port) {
756 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
757 if (mp == NULL ) {
758 jniThrowException(env, "java/lang/IllegalStateException", NULL);
759 return INVALID_OPERATION;
760 }
761
762 const char *cAddrString = NULL;
763
764 if (NULL != addrString) {
765 cAddrString = env->GetStringUTFChars(addrString, NULL);
766 if (cAddrString == NULL) { // Out of memory
767 return NO_MEMORY;
768 }
769 }
770 ALOGV("setRetransmitEndpoint: %s:%d",
771 cAddrString ? cAddrString : "(null)", port);
772
773 status_t ret;
774 if (cAddrString && (port > 0xFFFF)) {
775 ret = BAD_VALUE;
776 } else {
777 ret = mp->setRetransmitEndpoint(cAddrString,
778 static_cast<uint16_t>(port));
779 }
780
781 if (NULL != addrString) {
782 env->ReleaseStringUTFChars(addrString, cAddrString);
783 }
784
785 if (ret == INVALID_OPERATION ) {
786 jniThrowException(env, "java/lang/IllegalStateException", NULL);
787 }
788
Ashok Bhat075e9a12014-01-06 13:45:09 +0000789 return (jint) ret;
John Grossman720aa282012-02-22 15:38:35 -0800790}
791
Marco Nelissen84b83202012-02-28 16:07:44 -0800792static void
793android_media_MediaPlayer_setNextMediaPlayer(JNIEnv *env, jobject thiz, jobject java_player)
794{
795 ALOGV("setNextMediaPlayer");
796 sp<MediaPlayer> thisplayer = getMediaPlayer(env, thiz);
797 if (thisplayer == NULL) {
798 jniThrowException(env, "java/lang/IllegalStateException", "This player not initialized");
799 return;
800 }
801 sp<MediaPlayer> nextplayer = (java_player == NULL) ? NULL : getMediaPlayer(env, java_player);
802 if (nextplayer == NULL && java_player != NULL) {
803 jniThrowException(env, "java/lang/IllegalStateException", "That player not initialized");
804 return;
805 }
806
807 if (nextplayer == thisplayer) {
808 jniThrowException(env, "java/lang/IllegalArgumentException", "Next player can't be self");
809 return;
810 }
811 // tie the two players together
812 process_media_player_call(
813 env, thiz, thisplayer->setNextMediaPlayer(nextplayer),
814 "java/lang/IllegalArgumentException",
815 "setNextMediaPlayer failed." );
816 ;
817}
818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819// ----------------------------------------------------------------------------
820
821static JNINativeMethod gMethods[] = {
James Dong17524dc2011-05-04 13:41:58 -0700822 {
Andreas Huberd2506a52014-01-29 10:32:46 -0800823 "nativeSetDataSource",
824 "(Landroid/os/IBinder;Ljava/lang/String;[Ljava/lang/String;"
825 "[Ljava/lang/String;)V",
James Dong17524dc2011-05-04 13:41:58 -0700826 (void *)android_media_MediaPlayer_setDataSourceAndHeaders
827 },
828
Andreas Huberd5f9fa52013-05-28 14:39:39 -0700829 {"_setDataSource", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaPlayer_setDataSourceFD},
Ted Bonkenburg1ee60112011-07-26 09:51:18 -0700830 {"_setVideoSurface", "(Landroid/view/Surface;)V", (void *)android_media_MediaPlayer_setVideoSurface},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 {"prepare", "()V", (void *)android_media_MediaPlayer_prepare},
832 {"prepareAsync", "()V", (void *)android_media_MediaPlayer_prepareAsync},
833 {"_start", "()V", (void *)android_media_MediaPlayer_start},
834 {"_stop", "()V", (void *)android_media_MediaPlayer_stop},
835 {"getVideoWidth", "()I", (void *)android_media_MediaPlayer_getVideoWidth},
836 {"getVideoHeight", "()I", (void *)android_media_MediaPlayer_getVideoHeight},
837 {"seekTo", "(I)V", (void *)android_media_MediaPlayer_seekTo},
838 {"_pause", "()V", (void *)android_media_MediaPlayer_pause},
839 {"isPlaying", "()Z", (void *)android_media_MediaPlayer_isPlaying},
840 {"getCurrentPosition", "()I", (void *)android_media_MediaPlayer_getCurrentPosition},
841 {"getDuration", "()I", (void *)android_media_MediaPlayer_getDuration},
842 {"_release", "()V", (void *)android_media_MediaPlayer_release},
843 {"_reset", "()V", (void *)android_media_MediaPlayer_reset},
844 {"setAudioStreamType", "(I)V", (void *)android_media_MediaPlayer_setAudioStreamType},
845 {"setLooping", "(Z)V", (void *)android_media_MediaPlayer_setLooping},
846 {"isLooping", "()Z", (void *)android_media_MediaPlayer_isLooping},
847 {"setVolume", "(FF)V", (void *)android_media_MediaPlayer_setVolume},
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700848 {"native_invoke", "(Landroid/os/Parcel;Landroid/os/Parcel;)I",(void *)android_media_MediaPlayer_invoke},
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700849 {"native_setMetadataFilter", "(Landroid/os/Parcel;)I", (void *)android_media_MediaPlayer_setMetadataFilter},
Nicolas Catania5d55c712009-07-09 09:21:33 -0700850 {"native_getMetadata", "(ZZLandroid/os/Parcel;)Z", (void *)android_media_MediaPlayer_getMetadata},
Marco Nelissen4935d052009-08-03 11:12:58 -0700851 {"native_init", "()V", (void *)android_media_MediaPlayer_native_init},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 {"native_setup", "(Ljava/lang/Object;)V", (void *)android_media_MediaPlayer_native_setup},
853 {"native_finalize", "()V", (void *)android_media_MediaPlayer_native_finalize},
Eric Laurent619346f2010-06-21 09:27:30 -0700854 {"getAudioSessionId", "()I", (void *)android_media_MediaPlayer_get_audio_session_id},
855 {"setAudioSessionId", "(I)V", (void *)android_media_MediaPlayer_set_audio_session_id},
Eric Laurent7070b362010-07-16 07:43:46 -0700856 {"setAuxEffectSendLevel", "(F)V", (void *)android_media_MediaPlayer_setAuxEffectSendLevel},
857 {"attachAuxEffect", "(I)V", (void *)android_media_MediaPlayer_attachAuxEffect},
Gloria Wangd211f412011-02-19 18:37:57 -0800858 {"native_pullBatteryData", "(Landroid/os/Parcel;)I", (void *)android_media_MediaPlayer_pullBatteryData},
John Grossman720aa282012-02-22 15:38:35 -0800859 {"native_setRetransmitEndpoint", "(Ljava/lang/String;I)I", (void *)android_media_MediaPlayer_setRetransmitEndpoint},
Marco Nelissen84b83202012-02-28 16:07:44 -0800860 {"setNextMediaPlayer", "(Landroid/media/MediaPlayer;)V", (void *)android_media_MediaPlayer_setNextMediaPlayer},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861};
862
863static const char* const kClassPathName = "android/media/MediaPlayer";
864
Marco Nelissen4935d052009-08-03 11:12:58 -0700865// This function only registers the native methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866static int register_android_media_MediaPlayer(JNIEnv *env)
867{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 return AndroidRuntime::registerNativeMethods(env,
869 "android/media/MediaPlayer", gMethods, NELEM(gMethods));
870}
871
Zhijun He212e78d2013-06-07 11:36:23 -0700872extern int register_android_media_ImageReader(JNIEnv *env);
Andreas Huber8240d922012-04-04 14:06:32 -0700873extern int register_android_media_Crypto(JNIEnv *env);
Jeff Tinker8a0c80f2013-02-08 10:20:44 -0800874extern int register_android_media_Drm(JNIEnv *env);
Andreas Huber88572f72012-02-21 11:47:18 -0800875extern int register_android_media_MediaCodec(JNIEnv *env);
876extern int register_android_media_MediaExtractor(JNIEnv *env);
Andreas Huber5a04bf32012-03-29 16:41:38 -0700877extern int register_android_media_MediaCodecList(JNIEnv *env);
Andreas Huberd2506a52014-01-29 10:32:46 -0800878extern int register_android_media_MediaHTTPConnection(JNIEnv *env);
Andreas Huberbfb9fb12009-12-03 11:31:19 -0800879extern int register_android_media_MediaMetadataRetriever(JNIEnv *env);
ztenghui68ccf102013-02-13 14:07:02 -0800880extern int register_android_media_MediaMuxer(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881extern int register_android_media_MediaRecorder(JNIEnv *env);
882extern int register_android_media_MediaScanner(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883extern int register_android_media_ResampleInputStream(JNIEnv *env);
James Dongc3711942010-01-19 17:45:38 -0800884extern int register_android_media_MediaProfiles(JNIEnv *env);
Andreas Huberbfb9fb12009-12-03 11:31:19 -0800885extern int register_android_media_AmrInputStream(JNIEnv *env);
Mike Lockwood0cd01362010-12-30 11:54:33 -0500886extern int register_android_mtp_MtpDatabase(JNIEnv *env);
Mike Lockwood8182e722010-12-30 15:38:45 -0500887extern int register_android_mtp_MtpDevice(JNIEnv *env);
Mike Lockwood0cd01362010-12-30 11:54:33 -0500888extern int register_android_mtp_MtpServer(JNIEnv *env);
Andreas Huberbfb9fb12009-12-03 11:31:19 -0800889
Andreas Huberd2506a52014-01-29 10:32:46 -0800890jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891{
892 JNIEnv* env = NULL;
893 jint result = -1;
894
895 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +0000896 ALOGE("ERROR: GetEnv failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 goto bail;
898 }
899 assert(env != NULL);
900
Zhijun He212e78d2013-06-07 11:36:23 -0700901 if (register_android_media_ImageReader(env) < 0) {
902 ALOGE("ERROR: ImageReader native registration failed");
903 goto bail;
904 }
905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 if (register_android_media_MediaPlayer(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000907 ALOGE("ERROR: MediaPlayer native registration failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 goto bail;
909 }
910
911 if (register_android_media_MediaRecorder(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000912 ALOGE("ERROR: MediaRecorder native registration failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 goto bail;
914 }
915
916 if (register_android_media_MediaScanner(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000917 ALOGE("ERROR: MediaScanner native registration failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 goto bail;
919 }
920
921 if (register_android_media_MediaMetadataRetriever(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000922 ALOGE("ERROR: MediaMetadataRetriever native registration failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 goto bail;
924 }
925
926 if (register_android_media_AmrInputStream(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000927 ALOGE("ERROR: AmrInputStream native registration failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 goto bail;
929 }
930
931 if (register_android_media_ResampleInputStream(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000932 ALOGE("ERROR: ResampleInputStream native registration failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 goto bail;
934 }
935
James Dongc3711942010-01-19 17:45:38 -0800936 if (register_android_media_MediaProfiles(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000937 ALOGE("ERROR: MediaProfiles native registration failed");
James Dongc3711942010-01-19 17:45:38 -0800938 goto bail;
939 }
940
Mike Lockwood0cd01362010-12-30 11:54:33 -0500941 if (register_android_mtp_MtpDatabase(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000942 ALOGE("ERROR: MtpDatabase native registration failed");
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400943 goto bail;
944 }
945
Mike Lockwood8182e722010-12-30 15:38:45 -0500946 if (register_android_mtp_MtpDevice(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000947 ALOGE("ERROR: MtpDevice native registration failed");
Mike Lockwood8182e722010-12-30 15:38:45 -0500948 goto bail;
949 }
950
Mike Lockwood0cd01362010-12-30 11:54:33 -0500951 if (register_android_mtp_MtpServer(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000952 ALOGE("ERROR: MtpServer native registration failed");
Mike Lockwood81ea83d2010-06-30 17:49:41 -0400953 goto bail;
954 }
955
Andreas Huber88572f72012-02-21 11:47:18 -0800956 if (register_android_media_MediaCodec(env) < 0) {
957 ALOGE("ERROR: MediaCodec native registration failed");
958 goto bail;
959 }
960
961 if (register_android_media_MediaExtractor(env) < 0) {
962 ALOGE("ERROR: MediaCodec native registration failed");
963 goto bail;
964 }
965
ztenghui68ccf102013-02-13 14:07:02 -0800966 if (register_android_media_MediaMuxer(env) < 0) {
967 ALOGE("ERROR: MediaMuxer native registration failed");
968 goto bail;
969 }
970
Andreas Huber5a04bf32012-03-29 16:41:38 -0700971 if (register_android_media_MediaCodecList(env) < 0) {
972 ALOGE("ERROR: MediaCodec native registration failed");
973 goto bail;
974 }
975
Andreas Huber8240d922012-04-04 14:06:32 -0700976 if (register_android_media_Crypto(env) < 0) {
977 ALOGE("ERROR: MediaCodec native registration failed");
978 goto bail;
979 }
980
Jeff Tinker8a0c80f2013-02-08 10:20:44 -0800981 if (register_android_media_Drm(env) < 0) {
982 ALOGE("ERROR: MediaDrm native registration failed");
983 goto bail;
984 }
985
Andreas Huberd2506a52014-01-29 10:32:46 -0800986 if (register_android_media_MediaHTTPConnection(env) < 0) {
987 ALOGE("ERROR: MediaHTTPConnection native registration failed");
988 goto bail;
989 }
990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 /* success -- return valid version number */
992 result = JNI_VERSION_1_4;
993
994bail:
995 return result;
996}
997
998// KTHXBYE