1c371a02e7cf504e9a926ca29c33e63b658c2cef7James Dong/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** Copyright 2007, The Android Open Source Project
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** you may not use this file except in compliance with the License.
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** You may obtain a copy of the License at
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**     http://www.apache.org/licenses/LICENSE-2.0
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** Unless required by applicable law or agreed to in writing, software
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** distributed under the License is distributed on an "AS IS" BASIS,
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** See the License for the specific language governing permissions and
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** limitations under the License.
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project*/
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//#define LOG_NDEBUG 0
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define LOG_TAG "MediaPlayer-JNI"
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "utils/Log.h"
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <media/mediaplayer.h>
2320cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania#include <media/MediaPlayerInterface.h>
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <stdio.h>
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <assert.h>
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <limits.h>
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <unistd.h>
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <fcntl.h>
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/threads.h>
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "jni.h"
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "JNIHelp.h"
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "android_runtime/AndroidRuntime.h"
331ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg#include "android_runtime/android_view_Surface.h"
344df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project#include "utils/Errors.h"  // for status_t
35256430093679e1d62b54fb0c852126e54d162f6fAndreas Huber#include "utils/KeyedVector.h"
36256430093679e1d62b54fb0c852126e54d162f6fAndreas Huber#include "utils/String8.h"
3779f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong#include "android_media_Utils.h"
3879f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong
39d84e1ce0b535128f03416145554fb405f9fade3eJeff Sharkey#include "android_os_Parcel.h"
4020cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania#include "android_util_Binder.h"
4120cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania#include <binder/Parcel.h>
42cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten#include <gui/ISurfaceTexture.h>
438335f1ccccedb6655d96d9d5b697a7f0938235ddMathias Agopian#include <gui/Surface.h>
44d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang#include <binder/IPCThreadState.h>
45d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang#include <binder/IServiceManager.h>
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectusing namespace android;
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct fields_t {
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID    context;
551ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    jfieldID    surface_texture;
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jmethodID   post_event;
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic fields_t fields;
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic Mutex sLock;
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ref-counted object for callbacks
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass JNIMediaPlayerListener: public MediaPlayerListener
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic:
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JNIMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz);
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ~JNIMediaPlayerListener();
70162ee49e1ce8800de80697fdd0e0e42ad7e9374eGloria Wang    virtual void notify(int msg, int ext1, int ext2, const Parcel *obj = NULL);
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectprivate:
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JNIMediaPlayerListener();
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass      mClass;     // Reference to MediaPlayer class
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jobject     mObject;    // Weak ref to MediaPlayer Java object to call on
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectJNIMediaPlayerListener::JNIMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz)
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Hold onto the MediaPlayer class for use in calling the static method
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // that posts events to the application thread.
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass clazz = env->GetObjectClass(thiz);
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (clazz == NULL) {
843762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Can't find android/media/MediaPlayer");
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/Exception", NULL);
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mClass = (jclass)env->NewGlobalRef(clazz);
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // We use a weak reference so the MediaPlayer object can be garbage collected.
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // The reference is only used as a proxy for callbacks.
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mObject  = env->NewGlobalRef(weak_thiz);
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectJNIMediaPlayerListener::~JNIMediaPlayerListener()
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // remove global references
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JNIEnv *env = AndroidRuntime::getJNIEnv();
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->DeleteGlobalRef(mObject);
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->DeleteGlobalRef(mClass);
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
103162ee49e1ce8800de80697fdd0e0e42ad7e9374eGloria Wangvoid JNIMediaPlayerListener::notify(int msg, int ext1, int ext2, const Parcel *obj)
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JNIEnv *env = AndroidRuntime::getJNIEnv();
106162ee49e1ce8800de80697fdd0e0e42ad7e9374eGloria Wang    if (obj && obj->dataSize() > 0) {
107333c09945e7399c6a2b1f4d9fdf981508e38db52Insun Kang        jobject jParcel = createJavaParcelObject(env);
108333c09945e7399c6a2b1f4d9fdf981508e38db52Insun Kang        if (jParcel != NULL) {
109333c09945e7399c6a2b1f4d9fdf981508e38db52Insun Kang            Parcel* nativeParcel = parcelForJavaObject(env, jParcel);
1108902097bb686752ff207e3bda12713be1a8c74ebInsun Kang            nativeParcel->setData(obj->data(), obj->dataSize());
111162ee49e1ce8800de80697fdd0e0e42ad7e9374eGloria Wang            env->CallStaticVoidMethod(mClass, fields.post_event, mObject,
112333c09945e7399c6a2b1f4d9fdf981508e38db52Insun Kang                    msg, ext1, ext2, jParcel);
113162ee49e1ce8800de80697fdd0e0e42ad7e9374eGloria Wang        }
114162ee49e1ce8800de80697fdd0e0e42ad7e9374eGloria Wang    } else {
115162ee49e1ce8800de80697fdd0e0e42ad7e9374eGloria Wang        env->CallStaticVoidMethod(mClass, fields.post_event, mObject,
116162ee49e1ce8800de80697fdd0e0e42ad7e9374eGloria Wang                msg, ext1, ext2, NULL);
117162ee49e1ce8800de80697fdd0e0e42ad7e9374eGloria Wang    }
1188902097bb686752ff207e3bda12713be1a8c74ebInsun Kang    if (env->ExceptionCheck()) {
1198902097bb686752ff207e3bda12713be1a8c74ebInsun Kang        ALOGW("An exception occurred while notifying an event.");
1208902097bb686752ff207e3bda12713be1a8c74ebInsun Kang        LOGW_EX(env);
1218902097bb686752ff207e3bda12713be1a8c74ebInsun Kang        env->ExceptionClear();
1228902097bb686752ff207e3bda12713be1a8c74ebInsun Kang    }
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic sp<MediaPlayer> getMediaPlayer(JNIEnv* env, jobject thiz)
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Mutex::Autolock l(sLock);
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    MediaPlayer* const p = (MediaPlayer*)env->GetIntField(thiz, fields.context);
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return sp<MediaPlayer>(p);
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic sp<MediaPlayer> setMediaPlayer(JNIEnv* env, jobject thiz, const sp<MediaPlayer>& player)
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Mutex::Autolock l(sLock);
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> old = (MediaPlayer*)env->GetIntField(thiz, fields.context);
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (player.get()) {
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        player->incStrong(thiz);
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (old != 0) {
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        old->decStrong(thiz);
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(thiz, fields.context, (int)player.get());
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return old;
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14832f82774884bdd709789ab9f3ccdf5b972ff7681Nicolas Catania// If exception is NULL and opStatus is not OK, this method sends an error
14932f82774884bdd709789ab9f3ccdf5b972ff7681Nicolas Catania// event to the client application; otherwise, if exception is not NULL and
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// opStatus is not OK, this method throws the given exception to the client
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// application.
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void process_media_player_call(JNIEnv *env, jobject thiz, status_t opStatus, const char* exception, const char *message)
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (exception == NULL) {  // Don't throw exception. Instead, send an event.
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (opStatus != (status_t) OK) {
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (mp != 0) mp->notify(MEDIA_ERROR, opStatus, 0);
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else {  // Throw exception!
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ( opStatus == (status_t) INVALID_OPERATION ) {
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jniThrowException(env, "java/lang/IllegalStateException", NULL);
162fc301b0bb5c635c6bb51b48c504a8db5f9010e5cDave Burke        } else if ( opStatus == (status_t) PERMISSION_DENIED ) {
163fc301b0bb5c635c6bb51b48c504a8db5f9010e5cDave Burke            jniThrowException(env, "java/lang/SecurityException", NULL);
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if ( opStatus != (status_t) OK ) {
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (strlen(message) > 230) {
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project               // if the message is too long, don't bother displaying the status code
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project               jniThrowException( env, exception, message);
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project               char msg[256];
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                // append the status code to the message
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project               sprintf(msg, "%s: status=0x%X", message, opStatus);
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project               jniThrowException( env, exception, msg);
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
179256430093679e1d62b54fb0c852126e54d162f6fAndreas Huberandroid_media_MediaPlayer_setDataSourceAndHeaders(
18017524dc0d296146c8ffb3f692dc8ab05fee5b1e0James Dong        JNIEnv *env, jobject thiz, jstring path,
18117524dc0d296146c8ffb3f692dc8ab05fee5b1e0James Dong        jobjectArray keys, jobjectArray values) {
18217524dc0d296146c8ffb3f692dc8ab05fee5b1e0James Dong
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (path == NULL) {
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
194c371a02e7cf504e9a926ca29c33e63b658c2cef7James Dong    const char *tmp = env->GetStringUTFChars(path, NULL);
195c371a02e7cf504e9a926ca29c33e63b658c2cef7James Dong    if (tmp == NULL) {  // Out of memory
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
19871f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setDataSource: path %s", tmp);
199256430093679e1d62b54fb0c852126e54d162f6fAndreas Huber
200c371a02e7cf504e9a926ca29c33e63b658c2cef7James Dong    String8 pathStr(tmp);
201c371a02e7cf504e9a926ca29c33e63b658c2cef7James Dong    env->ReleaseStringUTFChars(path, tmp);
202c371a02e7cf504e9a926ca29c33e63b658c2cef7James Dong    tmp = NULL;
203c371a02e7cf504e9a926ca29c33e63b658c2cef7James Dong
20417524dc0d296146c8ffb3f692dc8ab05fee5b1e0James Dong    // We build a KeyedVector out of the key and val arrays
20517524dc0d296146c8ffb3f692dc8ab05fee5b1e0James Dong    KeyedVector<String8, String8> headersVector;
20679f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong    if (!ConvertKeyValueArraysToKeyedVector(
20779f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong            env, keys, values, &headersVector)) {
20879f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong        return;
209256430093679e1d62b54fb0c852126e54d162f6fAndreas Huber    }
210256430093679e1d62b54fb0c852126e54d162f6fAndreas Huber
211256430093679e1d62b54fb0c852126e54d162f6fAndreas Huber    status_t opStatus =
212256430093679e1d62b54fb0c852126e54d162f6fAndreas Huber        mp->setDataSource(
213c371a02e7cf504e9a926ca29c33e63b658c2cef7James Dong                pathStr,
21479f407cc6c5ae34fc6f04d52fd034b49d1f002c4James Dong                headersVector.size() > 0? &headersVector : NULL);
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
216256430093679e1d62b54fb0c852126e54d162f6fAndreas Huber    process_media_player_call(
217256430093679e1d62b54fb0c852126e54d162f6fAndreas Huber            env, thiz, opStatus, "java/io/IOException",
218256430093679e1d62b54fb0c852126e54d162f6fAndreas Huber            "setDataSource failed." );
219256430093679e1d62b54fb0c852126e54d162f6fAndreas Huber}
220256430093679e1d62b54fb0c852126e54d162f6fAndreas Huber
221256430093679e1d62b54fb0c852126e54d162f6fAndreas Huberstatic void
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_setDataSourceFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length)
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (fileDescriptor == NULL) {
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
234a3804cf77f0edd93f6247a055cdafb856b117eecElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
23571f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setDataSourceFD: fd %d", fd);
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_player_call( env, thiz, mp->setDataSource(fd, offset, length), "java/io/IOException", "setDataSourceFD failed." );
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2391ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburgstatic sp<ISurfaceTexture>
2401ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted BonkenburggetVideoSurfaceTexture(JNIEnv* env, jobject thiz) {
2411ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    ISurfaceTexture * const p = (ISurfaceTexture*)env->GetIntField(thiz, fields.surface_texture);
2421ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    return sp<ISurfaceTexture>(p);
2438b0b174198793cabb2b3fcc015f9bfdc9d5082b5Dave Sparks}
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
246d59310d08637c8531dc7949fc166b0daeede53c0Gloria WangdecVideoSurfaceRef(JNIEnv *env, jobject thiz)
247d59310d08637c8531dc7949fc166b0daeede53c0Gloria Wang{
248e828beb3866e2275cd26acd2a58f9f8a3967ba30Gloria Wang    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
249e828beb3866e2275cd26acd2a58f9f8a3967ba30Gloria Wang    if (mp == NULL) {
250e828beb3866e2275cd26acd2a58f9f8a3967ba30Gloria Wang        return;
251e828beb3866e2275cd26acd2a58f9f8a3967ba30Gloria Wang    }
252e828beb3866e2275cd26acd2a58f9f8a3967ba30Gloria Wang
253d59310d08637c8531dc7949fc166b0daeede53c0Gloria Wang    sp<ISurfaceTexture> old_st = getVideoSurfaceTexture(env, thiz);
254d59310d08637c8531dc7949fc166b0daeede53c0Gloria Wang    if (old_st != NULL) {
255d59310d08637c8531dc7949fc166b0daeede53c0Gloria Wang        old_st->decStrong(thiz);
256d59310d08637c8531dc7949fc166b0daeede53c0Gloria Wang    }
257d59310d08637c8531dc7949fc166b0daeede53c0Gloria Wang}
258d59310d08637c8531dc7949fc166b0daeede53c0Gloria Wang
259d59310d08637c8531dc7949fc166b0daeede53c0Gloria Wangstatic void
26043ef913815ec84d04877a698614a31e129cd97e3James DongsetVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface, jboolean mediaPlayerMustBeAlive)
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
26343ef913815ec84d04877a698614a31e129cd97e3James Dong    if (mp == NULL) {
26443ef913815ec84d04877a698614a31e129cd97e3James Dong        if (mediaPlayerMustBeAlive) {
26543ef913815ec84d04877a698614a31e129cd97e3James Dong            jniThrowException(env, "java/lang/IllegalStateException", NULL);
26643ef913815ec84d04877a698614a31e129cd97e3James Dong        }
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2691ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg
270d59310d08637c8531dc7949fc166b0daeede53c0Gloria Wang    decVideoSurfaceRef(env, thiz);
271d59310d08637c8531dc7949fc166b0daeede53c0Gloria Wang
2721ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    sp<ISurfaceTexture> new_st;
2731ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    if (jsurface) {
27464a55af0ac700baecb0877235eb42caac59a3560Jeff Brown        sp<Surface> surface(android_view_Surface_getSurface(env, jsurface));
275f76afc80d229cf0c918e849acf5d8c4f5d4ffd07Jamie Gennis        if (surface != NULL) {
276f76afc80d229cf0c918e849acf5d8c4f5d4ffd07Jamie Gennis            new_st = surface->getSurfaceTexture();
277097922b9c1e0821beb107024bcd0dc14144c0920James Dong            if (new_st == NULL) {
278097922b9c1e0821beb107024bcd0dc14144c0920James Dong                jniThrowException(env, "java/lang/IllegalArgumentException",
279097922b9c1e0821beb107024bcd0dc14144c0920James Dong                    "The surface does not have a binding SurfaceTexture!");
280097922b9c1e0821beb107024bcd0dc14144c0920James Dong                return;
281097922b9c1e0821beb107024bcd0dc14144c0920James Dong            }
282f76afc80d229cf0c918e849acf5d8c4f5d4ffd07Jamie Gennis            new_st->incStrong(thiz);
283f76afc80d229cf0c918e849acf5d8c4f5d4ffd07Jamie Gennis        } else {
284f76afc80d229cf0c918e849acf5d8c4f5d4ffd07Jamie Gennis            jniThrowException(env, "java/lang/IllegalArgumentException",
285f76afc80d229cf0c918e849acf5d8c4f5d4ffd07Jamie Gennis                    "The surface has been released");
286f76afc80d229cf0c918e849acf5d8c4f5d4ffd07Jamie Gennis            return;
287f76afc80d229cf0c918e849acf5d8c4f5d4ffd07Jamie Gennis        }
2881ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    }
289d59310d08637c8531dc7949fc166b0daeede53c0Gloria Wang
2901ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    env->SetIntField(thiz, fields.surface_texture, (int)new_st.get());
2911ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg
2921ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    // This will fail if the media player has not been initialized yet. This
2931ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    // can be the case if setDisplay() on MediaPlayer.java has been called
2941ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    // before setDataSource(). The redundant call to setVideoSurfaceTexture()
2951ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    // in prepare/prepareAsync covers for this case.
2961ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    mp->setVideoSurfaceTexture(new_st);
2978b0b174198793cabb2b3fcc015f9bfdc9d5082b5Dave Sparks}
2988b0b174198793cabb2b3fcc015f9bfdc9d5082b5Dave Sparks
2998b0b174198793cabb2b3fcc015f9bfdc9d5082b5Dave Sparksstatic void
30043ef913815ec84d04877a698614a31e129cd97e3James Dongandroid_media_MediaPlayer_setVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface)
30143ef913815ec84d04877a698614a31e129cd97e3James Dong{
30243ef913815ec84d04877a698614a31e129cd97e3James Dong    setVideoSurface(env, thiz, jsurface, true /* mediaPlayerMustBeAlive */);
30343ef913815ec84d04877a698614a31e129cd97e3James Dong}
30443ef913815ec84d04877a698614a31e129cd97e3James Dong
30543ef913815ec84d04877a698614a31e129cd97e3James Dongstatic void
3068b0b174198793cabb2b3fcc015f9bfdc9d5082b5Dave Sparksandroid_media_MediaPlayer_prepare(JNIEnv *env, jobject thiz)
3078b0b174198793cabb2b3fcc015f9bfdc9d5082b5Dave Sparks{
3088b0b174198793cabb2b3fcc015f9bfdc9d5082b5Dave Sparks    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
3098b0b174198793cabb2b3fcc015f9bfdc9d5082b5Dave Sparks    if (mp == NULL ) {
3108b0b174198793cabb2b3fcc015f9bfdc9d5082b5Dave Sparks        jniThrowException(env, "java/lang/IllegalStateException", NULL);
3118b0b174198793cabb2b3fcc015f9bfdc9d5082b5Dave Sparks        return;
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3131ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg
3141ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    // Handle the case where the display surface was set before the mp was
3151ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    // initialized. We try again to make it stick.
3161ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    sp<ISurfaceTexture> st = getVideoSurfaceTexture(env, thiz);
3171ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    mp->setVideoSurfaceTexture(st);
3181ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_player_call( env, thiz, mp->prepare(), "java/io/IOException", "Prepare failed." );
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_prepareAsync(JNIEnv *env, jobject thiz)
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3301ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg
3311ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    // Handle the case where the display surface was set before the mp was
3321ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    // initialized. We try again to make it stick.
3331ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    sp<ISurfaceTexture> st = getVideoSurfaceTexture(env, thiz);
3341ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    mp->setVideoSurfaceTexture(st);
3351ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_player_call( env, thiz, mp->prepareAsync(), "java/io/IOException", "Prepare Async failed." );
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_start(JNIEnv *env, jobject thiz)
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
34271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("start");
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_player_call( env, thiz, mp->start(), NULL, NULL );
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_stop(JNIEnv *env, jobject thiz)
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
35471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("stop");
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
36032f82774884bdd709789ab9f3ccdf5b972ff7681Nicolas Catania    process_media_player_call( env, thiz, mp->stop(), NULL, NULL );
3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_pause(JNIEnv *env, jobject thiz)
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
36671f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("pause");
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_player_call( env, thiz, mp->pause(), NULL, NULL );
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jboolean
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_isPlaying(JNIEnv *env, jobject thiz)
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3834df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    const jboolean is_playing = mp->isPlaying();
3844df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project
38571f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("isPlaying: %d", is_playing);
3864df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    return is_playing;
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_seekTo(JNIEnv *env, jobject thiz, int msec)
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
39771f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("seekTo: %d(msec)", msec);
3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_player_call( env, thiz, mp->seekTo(msec), NULL, NULL );
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_getVideoWidth(JNIEnv *env, jobject thiz)
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int w;
4104df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    if (0 != mp->getVideoWidth(&w)) {
4113762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("getVideoWidth failed");
4124df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project        w = 0;
4134df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    }
41471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("getVideoWidth: %d", w);
4154df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    return w;
4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_getVideoHeight(JNIEnv *env, jobject thiz)
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int h;
4274df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    if (0 != mp->getVideoHeight(&h)) {
4283762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("getVideoHeight failed");
4294df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project        h = 0;
4304df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    }
43171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("getVideoHeight: %d", h);
4324df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    return h;
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_getCurrentPosition(JNIEnv *env, jobject thiz)
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int msec;
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_player_call( env, thiz, mp->getCurrentPosition(&msec), NULL, NULL );
44671f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("getCurrentPosition: %d (msec)", msec);
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return msec;
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int
4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_getDuration(JNIEnv *env, jobject thiz)
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int msec;
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_player_call( env, thiz, mp->getDuration(&msec), NULL, NULL );
46071f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("getDuration: %d (msec)", msec);
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return msec;
4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_reset(JNIEnv *env, jobject thiz)
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
46771f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("reset");
4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_player_call( env, thiz, mp->reset(), NULL, NULL );
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_setAudioStreamType(JNIEnv *env, jobject thiz, int streamtype)
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
47971f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setAudioStreamType: %d", streamtype);
4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
485bc1d77b6cbce23fbe25f7231651037ae195bc90eGlenn Kasten    process_media_player_call( env, thiz, mp->setAudioStreamType((audio_stream_type_t) streamtype) , NULL, NULL );
4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_setLooping(JNIEnv *env, jobject thiz, jboolean looping)
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
49171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setLooping: %d", looping);
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_player_call( env, thiz, mp->setLooping(looping), NULL, NULL );
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jboolean
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_isLooping(JNIEnv *env, jobject thiz)
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
50371f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("isLooping");
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return mp->isLooping();
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_setVolume(JNIEnv *env, jobject thiz, float leftVolume, float rightVolume)
5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
51571f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setVolume: left %f  right %f", leftVolume, rightVolume);
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL ) {
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException", NULL);
5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
5209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    process_media_player_call( env, thiz, mp->setVolume(leftVolume, rightVolume), NULL, NULL );
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// FIXME: deprecated
5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jobject
5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_getFrameAt(JNIEnv *env, jobject thiz, jint msec)
5279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NULL;
5299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
53120cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania
53220cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania// Sends the request and reply parcels to the media player via the
53320cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania// binder interface.
53420cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Cataniastatic jint
53520cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Cataniaandroid_media_MediaPlayer_invoke(JNIEnv *env, jobject thiz,
53620cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania                                 jobject java_request, jobject java_reply)
53720cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania{
53820cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania    sp<MediaPlayer> media_player = getMediaPlayer(env, thiz);
53920cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania    if (media_player == NULL ) {
54020cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania        jniThrowException(env, "java/lang/IllegalStateException", NULL);
541b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania        return UNKNOWN_ERROR;
54220cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania    }
54320cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania
54420cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania    Parcel *request = parcelForJavaObject(env, java_request);
54520cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania    Parcel *reply = parcelForJavaObject(env, java_reply);
54620cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania
54720cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania    // Don't use process_media_player_call which use the async loop to
54820cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania    // report errors, instead returns the status.
549b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania    return media_player->invoke(*request, reply);
55020cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania}
55120cb94eeb5b9672573fc86bf51e09bd66a774581Nicolas Catania
552b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania// Sends the new filter to the client.
553b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Cataniastatic jint
554b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Cataniaandroid_media_MediaPlayer_setMetadataFilter(JNIEnv *env, jobject thiz, jobject request)
555b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania{
556b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania    sp<MediaPlayer> media_player = getMediaPlayer(env, thiz);
557b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania    if (media_player == NULL ) {
558b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania        jniThrowException(env, "java/lang/IllegalStateException", NULL);
559b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania        return UNKNOWN_ERROR;
560b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania    }
561b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania
562b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania    Parcel *filter = parcelForJavaObject(env, request);
563b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania
5645d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania    if (filter == NULL ) {
5655d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania        jniThrowException(env, "java/lang/RuntimeException", "Filter is null");
5665d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania        return UNKNOWN_ERROR;
5675d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania    }
5685d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania
569b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania    return media_player->setMetadataFilter(*filter);
570b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania}
571b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania
5725d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Cataniastatic jboolean
5735d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Cataniaandroid_media_MediaPlayer_getMetadata(JNIEnv *env, jobject thiz, jboolean update_only,
5745d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania                                      jboolean apply_filter, jobject reply)
5755d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania{
5765d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania    sp<MediaPlayer> media_player = getMediaPlayer(env, thiz);
5775d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania    if (media_player == NULL ) {
5785d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania        jniThrowException(env, "java/lang/IllegalStateException", NULL);
5795d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania        return false;
5805d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania    }
5815d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania
5825d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania    Parcel *metadata = parcelForJavaObject(env, reply);
5835d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania
5845d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania    if (metadata == NULL ) {
5855d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania        jniThrowException(env, "java/lang/RuntimeException", "Reply parcel is null");
5865d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania        return false;
5875d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania    }
5885d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania
5895d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania    metadata->freeData();
5905d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania    // On return metadata is positioned at the beginning of the
5915d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania    // metadata. Note however that the parcel actually starts with the
5925d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania    // return code so you should not rewind the parcel using
5935d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania    // setDataPosition(0).
5945d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania    return media_player->getMetadata(update_only, apply_filter, metadata) == OK;
5955d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania}
5965d55c7119820ee9bb06fc072e416fe98ba77cd28Nicolas Catania
5974935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen// This function gets some field IDs, which in turn causes class initialization.
5984935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen// It is called from a static block in MediaPlayer, which won't run until the
5994935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen// first time an instance of this class is used.
6004935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissenstatic void
6014935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissenandroid_media_MediaPlayer_native_init(JNIEnv *env)
6024935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen{
6034935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    jclass clazz;
6044935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen
6054935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    clazz = env->FindClass("android/media/MediaPlayer");
6064935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    if (clazz == NULL) {
6074935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen        return;
6084935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    }
6094935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen
6104935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    fields.context = env->GetFieldID(clazz, "mNativeContext", "I");
6114935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    if (fields.context == NULL) {
6124935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen        return;
6134935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    }
6144935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen
6154935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
6164935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen                                               "(Ljava/lang/Object;IIILjava/lang/Object;)V");
6174935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    if (fields.post_event == NULL) {
6184935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen        return;
6194935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen    }
6204935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen
6211ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    fields.surface_texture = env->GetFieldID(clazz, "mNativeSurfaceTexture", "I");
6221ee60119c4fa51ebfa781cf5fdc33f192e8551b8Ted Bonkenburg    if (fields.surface_texture == NULL) {
623cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten        return;
624cc562a3576a6a8096626387472e05e8bee03352aGlenn Kasten    }
6254935d05eaa306cef88cf0ab13eca386f270409ecMarco Nelissen}
626b2c693919be966f179080a9ec70a7a82dbf57627Nicolas Catania
6279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
6289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
6299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
63071f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("native_setup");
6319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = new MediaPlayer();
6329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp == NULL) {
6339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
6359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // create new listener and give it to MediaPlayer
6389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<JNIMediaPlayerListener> listener = new JNIMediaPlayerListener(env, thiz, weak_this);
6399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mp->setListener(listener);
6409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Stow our new C++ MediaPlayer in an opaque field in the Java object.
6429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    setMediaPlayer(env, thiz, mp);
6439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
6469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_release(JNIEnv *env, jobject thiz)
6479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
64871f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("release");
649d59310d08637c8531dc7949fc166b0daeede53c0Gloria Wang    decVideoSurfaceRef(env, thiz);
6509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<MediaPlayer> mp = setMediaPlayer(env, thiz, 0);
6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mp != NULL) {
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // this prevents native callbacks after the object is released
6539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mp->setListener(0);
6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mp->disconnect();
6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_MediaPlayer_native_finalize(JNIEnv *env, jobject thiz)
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
66171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("native_finalize");
6628dc208476f1fbbf6cd0b8cc3d48487a62cb72883Marco Nelissen    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
6638dc208476f1fbbf6cd0b8cc3d48487a62cb72883Marco Nelissen    if (mp != NULL) {
6648564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("MediaPlayer finalized without being released");
6658dc208476f1fbbf6cd0b8cc3d48487a62cb72883Marco Nelissen    }
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    android_media_MediaPlayer_release(env, thiz);
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
669619346f902241736d933657a4fe10f10c50a1ba8Eric Laurentstatic void android_media_MediaPlayer_set_audio_session_id(JNIEnv *env,  jobject thiz, jint sessionId) {
67071f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("set_session_id(): %d", sessionId);
671619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
672619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    if (mp == NULL ) {
673619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent        jniThrowException(env, "java/lang/IllegalStateException", NULL);
674619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent        return;
675619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    }
676619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    process_media_player_call( env, thiz, mp->setAudioSessionId(sessionId), NULL, NULL );
677619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent}
678619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent
679619346f902241736d933657a4fe10f10c50a1ba8Eric Laurentstatic jint android_media_MediaPlayer_get_audio_session_id(JNIEnv *env,  jobject thiz) {
68071f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("get_session_id()");
681619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
682619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    if (mp == NULL ) {
683619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent        jniThrowException(env, "java/lang/IllegalStateException", NULL);
684619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent        return 0;
685619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    }
686619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent
687619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    return mp->getAudioSessionId();
688619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent}
689619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent
6907070b36549d511d6627538c73dfbab23fbae5b43Eric Laurentstatic void
6917070b36549d511d6627538c73dfbab23fbae5b43Eric Laurentandroid_media_MediaPlayer_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level)
6927070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent{
69371f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setAuxEffectSendLevel: level %f", level);
6947070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
6957070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    if (mp == NULL ) {
6967070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        jniThrowException(env, "java/lang/IllegalStateException", NULL);
6977070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        return;
6987070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    }
6997070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    process_media_player_call( env, thiz, mp->setAuxEffectSendLevel(level), NULL, NULL );
7007070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent}
7017070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent
7027070b36549d511d6627538c73dfbab23fbae5b43Eric Laurentstatic void android_media_MediaPlayer_attachAuxEffect(JNIEnv *env,  jobject thiz, jint effectId) {
70371f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("attachAuxEffect(): %d", effectId);
7047070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
7057070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    if (mp == NULL ) {
7067070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        jniThrowException(env, "java/lang/IllegalStateException", NULL);
7077070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        return;
7087070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    }
7097070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    process_media_player_call( env, thiz, mp->attachAuxEffect(effectId), NULL, NULL );
7107070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent}
7117070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent
712d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wangstatic jint
713d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wangandroid_media_MediaPlayer_pullBatteryData(JNIEnv *env, jobject thiz, jobject java_reply)
714d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang{
715d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang    sp<IBinder> binder = defaultServiceManager()->getService(String16("media.player"));
716d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang    sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
717d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang    if (service.get() == NULL) {
718d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang        jniThrowException(env, "java/lang/RuntimeException", "cannot get MediaPlayerService");
719d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang        return UNKNOWN_ERROR;
720d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang    }
721d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang
722d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang    Parcel *reply = parcelForJavaObject(env, java_reply);
723d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang
724d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang    return service->pullBatteryData(reply);
725d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang}
726d211f41f764fe81fe00b10a99b4b44cb84479cbeGloria Wang
727720aa282791ef9405d39a15f419a41ab24f11e30John Grossmanstatic jint
728720aa282791ef9405d39a15f419a41ab24f11e30John Grossmanandroid_media_MediaPlayer_setRetransmitEndpoint(JNIEnv *env, jobject thiz,
729720aa282791ef9405d39a15f419a41ab24f11e30John Grossman                                                jstring addrString, jint port) {
730720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
731720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    if (mp == NULL ) {
732720aa282791ef9405d39a15f419a41ab24f11e30John Grossman        jniThrowException(env, "java/lang/IllegalStateException", NULL);
733720aa282791ef9405d39a15f419a41ab24f11e30John Grossman        return INVALID_OPERATION;
734720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    }
735720aa282791ef9405d39a15f419a41ab24f11e30John Grossman
736720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    const char *cAddrString = NULL;
737720aa282791ef9405d39a15f419a41ab24f11e30John Grossman
738720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    if (NULL != addrString) {
739720aa282791ef9405d39a15f419a41ab24f11e30John Grossman        cAddrString = env->GetStringUTFChars(addrString, NULL);
740720aa282791ef9405d39a15f419a41ab24f11e30John Grossman        if (cAddrString == NULL) {  // Out of memory
741720aa282791ef9405d39a15f419a41ab24f11e30John Grossman            return NO_MEMORY;
742720aa282791ef9405d39a15f419a41ab24f11e30John Grossman        }
743720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    }
744720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    ALOGV("setRetransmitEndpoint: %s:%d",
745720aa282791ef9405d39a15f419a41ab24f11e30John Grossman            cAddrString ? cAddrString : "(null)", port);
746720aa282791ef9405d39a15f419a41ab24f11e30John Grossman
747720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    status_t ret;
748720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    if (cAddrString && (port > 0xFFFF)) {
749720aa282791ef9405d39a15f419a41ab24f11e30John Grossman        ret = BAD_VALUE;
750720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    } else {
751720aa282791ef9405d39a15f419a41ab24f11e30John Grossman        ret = mp->setRetransmitEndpoint(cAddrString,
752720aa282791ef9405d39a15f419a41ab24f11e30John Grossman                static_cast<uint16_t>(port));
753720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    }
754720aa282791ef9405d39a15f419a41ab24f11e30John Grossman
755720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    if (NULL != addrString) {
756720aa282791ef9405d39a15f419a41ab24f11e30John Grossman        env->ReleaseStringUTFChars(addrString, cAddrString);
757720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    }
758720aa282791ef9405d39a15f419a41ab24f11e30John Grossman
759720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    if (ret == INVALID_OPERATION ) {
760720aa282791ef9405d39a15f419a41ab24f11e30John Grossman        jniThrowException(env, "java/lang/IllegalStateException", NULL);
761720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    }
762720aa282791ef9405d39a15f419a41ab24f11e30John Grossman
763720aa282791ef9405d39a15f419a41ab24f11e30John Grossman    return ret;
764720aa282791ef9405d39a15f419a41ab24f11e30John Grossman}
765720aa282791ef9405d39a15f419a41ab24f11e30John Grossman
766d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wangstatic jboolean
767d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wangandroid_media_MediaPlayer_setParameter(JNIEnv *env, jobject thiz, jint key, jobject java_request)
768d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang{
76971f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setParameter: key %d", key);
770d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
771d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang    if (mp == NULL ) {
772d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang        jniThrowException(env, "java/lang/IllegalStateException", NULL);
773d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang        return false;
774d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang    }
775d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang
776d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang    Parcel *request = parcelForJavaObject(env, java_request);
777d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang    status_t err = mp->setParameter(key, *request);
778d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang    if (err == OK) {
779d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang        return true;
780d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang    } else {
781d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang        return false;
782d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang    }
783d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang}
784d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang
785d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wangstatic void
786d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wangandroid_media_MediaPlayer_getParameter(JNIEnv *env, jobject thiz, jint key, jobject java_reply)
787d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang{
78871f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("getParameter: key %d", key);
789d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
790d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang    if (mp == NULL ) {
791d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang        jniThrowException(env, "java/lang/IllegalStateException", NULL);
792d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang        return;
793d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang    }
794d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang
795d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang    Parcel *reply = parcelForJavaObject(env, java_reply);
796d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang    process_media_player_call(env, thiz, mp->getParameter(key, reply), NULL, NULL );
797d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang}
798d01ec6eab019e46398975202e9e4a198a603ad99Gloria Wang
79984b832054552e00257bb04997143ca33d6d100beMarco Nelissenstatic void
80084b832054552e00257bb04997143ca33d6d100beMarco Nelissenandroid_media_MediaPlayer_setNextMediaPlayer(JNIEnv *env, jobject thiz, jobject java_player)
80184b832054552e00257bb04997143ca33d6d100beMarco Nelissen{
80284b832054552e00257bb04997143ca33d6d100beMarco Nelissen    ALOGV("setNextMediaPlayer");
80384b832054552e00257bb04997143ca33d6d100beMarco Nelissen    sp<MediaPlayer> thisplayer = getMediaPlayer(env, thiz);
80484b832054552e00257bb04997143ca33d6d100beMarco Nelissen    if (thisplayer == NULL) {
80584b832054552e00257bb04997143ca33d6d100beMarco Nelissen        jniThrowException(env, "java/lang/IllegalStateException", "This player not initialized");
80684b832054552e00257bb04997143ca33d6d100beMarco Nelissen        return;
80784b832054552e00257bb04997143ca33d6d100beMarco Nelissen    }
80884b832054552e00257bb04997143ca33d6d100beMarco Nelissen    sp<MediaPlayer> nextplayer = (java_player == NULL) ? NULL : getMediaPlayer(env, java_player);
80984b832054552e00257bb04997143ca33d6d100beMarco Nelissen    if (nextplayer == NULL && java_player != NULL) {
81084b832054552e00257bb04997143ca33d6d100beMarco Nelissen        jniThrowException(env, "java/lang/IllegalStateException", "That player not initialized");
81184b832054552e00257bb04997143ca33d6d100beMarco Nelissen        return;
81284b832054552e00257bb04997143ca33d6d100beMarco Nelissen    }
81384b832054552e00257bb04997143ca33d6d100beMarco Nelissen
81484b832054552e00257bb04997143ca33d6d100beMarco Nelissen    if (nextplayer == thisplayer) {
81584b832054552e00257bb04997143ca33d6d100beMarco Nelissen        jniThrowException(env, "java/lang/IllegalArgumentException", "Next player can't be self");
81684b832054552e00257bb04997143ca33d6d100beMarco Nelissen        return;
81784b832054552e00257bb04997143ca33d6d100beMarco Nelissen    }
81884b832054552e00257bb04997143ca33d6d100beMarco Nelissen    // tie the two players together
81984b832054552e00257bb04997143ca33d6d100beMarco Nelissen    process_media_player_call(
82084b832054552e00257bb04997143ca33d6d100beMarco Nelissen            env, thiz, thisplayer->setNextMediaPlayer(nextplayer),
82184b832054552e00257bb04997143ca33d6d100beMarco Nelissen            "java/lang/IllegalArgumentException",
822