gstreamer/sys/androidmedia/gstamc.c
Sebastian Dröge 8f232ea90b Initial commit
2012-10-15 16:28:36 +02:00

150 lines
3.7 KiB
C

/*
* Copyright (C) 2012, Collabora Ltd.
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstamc.h"
#include <gst/gst.h>
#include <string.h>
#include <jni.h>
GST_DEBUG_CATEGORY (gst_amc_debug);
#define GST_CAT_DEFAULT gst_amc_debug
static GModule *java_module;
static jint (*get_created_java_vms) (JavaVM ** vmBuf, jsize bufLen,
jsize * nVMs);
static jint (*create_java_vm) (JavaVM ** p_vm, JNIEnv ** p_env, void *vm_args);
static JavaVM *java_vm;
JNIEnv *
gst_amc_attach_current_thread (void)
{
JNIEnv *env;
JavaVMAttachArgs args;
args.version = JNI_VERSION_1_6;
args.name = NULL;
args.group = NULL;
if ((*java_vm)->AttachCurrentThread (java_vm, (void **) &env, &args) < 0) {
GST_ERROR ("Failed to attach current thread");
return NULL;
}
return env;
}
void
gst_amc_detach_current_thread (void)
{
(*java_vm)->DetachCurrentThread (java_vm);
}
static gboolean
initialize_java_vm (void)
{
jsize n_vms;
java_module = g_module_open ("libdvm", G_MODULE_BIND_LOCAL);
if (!java_module)
goto load_failed;
if (!g_module_symbol (java_module, "JNI_CreateJavaVM",
(gpointer *) & create_java_vm))
goto symbol_error;
if (!g_module_symbol (java_module, "JNI_GetCreatedJavaVMs",
(gpointer *) & get_created_java_vms))
goto symbol_error;
n_vms = 0;
if (get_created_java_vms (&java_vm, 1, &n_vms) < 0)
goto get_created_failed;
if (n_vms > 0) {
GST_DEBUG ("Successfully got existing Java VM %p", java_vm);
} else {
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options[1];
/* FIXME: Do we need any options here? Like exit()
* handler, or classpaths? */
vm_args.version = JNI_VERSION_1_6;
vm_args.options = options;
vm_args.nOptions = 0;
vm_args.ignoreUnrecognized = JNI_TRUE;
if (create_java_vm (&java_vm, &env, &vm_args) < 0)
goto create_failed;
GST_DEBUG ("Successfully created Java VM %p", java_vm);
}
return java_vm != NULL;
load_failed:
{
GST_ERROR ("Failed to load libdvm: %s", g_module_error ());
return FALSE;
}
symbol_error:
{
GST_ERROR ("Failed to locate required JNI symbols in libdvm: %s",
g_module_error ());
g_module_close (java_module);
java_module = NULL;
return FALSE;
}
get_created_failed:
{
GST_ERROR ("Failed to get already created VMs");
g_module_close (java_module);
java_module = NULL;
return FALSE;
}
create_failed:
{
GST_ERROR ("Failed to create a Java VM");
g_module_close (java_module);
java_module = NULL;
return FALSE;
}
}
static gboolean
plugin_init (GstPlugin * plugin)
{
GST_DEBUG_CATEGORY_INIT (gst_amc_debug, "amc", 0, "android-media-codec");
if (!initialize_java_vm ())
return FALSE;
return TRUE;
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
"androidmediacodec",
"GStreamer Android MediaCodec Plug-ins",
plugin_init,
PACKAGE_VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)