From e915be92088d1d9b265ea30350bc3a8a2def2ed8 Mon Sep 17 00:00:00 2001 From: Arun Raghavan Date: Sun, 11 Nov 2012 11:06:09 +0530 Subject: [PATCH] First pass at adding transcode support This does a hard-coded audio encode to vorbis-in-ogg. In the long term, this should use encodebin and real encoding profiles. --- jni/Android.mk | 22 ++++++ jni/README.gst-sdk | 8 ++ jni/transcode.c | 91 +++++++++++++++++++++++ src/org/wikimedia/commons/Transcoder.java | 10 +++ 4 files changed, 131 insertions(+) create mode 100644 jni/Android.mk create mode 100644 jni/README.gst-sdk create mode 100644 jni/transcode.c create mode 100644 src/org/wikimedia/commons/Transcoder.java diff --git a/jni/Android.mk b/jni/Android.mk new file mode 100644 index 000000000..fdd2a2851 --- /dev/null +++ b/jni/Android.mk @@ -0,0 +1,22 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := libtranscode +LOCAL_SRC_FILES := transcode.c +LOCAL_SHARED_LIBRARIES := gstreamer_android +LOCAL_LDLIBS := -landroid + +include $(BUILD_SHARED_LIBRARY) + +GSTREAMER_SDK_ROOT := $(GSTREAMER_SDK_ROOT_ANDROID) +GSTREAMER_NDK_BUILD_PATH := $(GSTREAMER_SDK_ROOT)/share/gst-android/ndk-build/ +include $(GSTREAMER_NDK_BUILD_PATH)/plugins.mk +GSTREAMER_PLUGINS := \ + $(GSTREAMER_PLUGINS_CORE) \ + $(GSTREAMER_PLUGINS_PLAYBACK) \ + audioparsers id3demux isomp4 ogg vorbis \ + amrnb amrwbdec faad mad mpegaudioparse \ + amc + +include $(GSTREAMER_NDK_BUILD_PATH)/gstreamer.mk diff --git a/jni/README.gst-sdk b/jni/README.gst-sdk new file mode 100644 index 000000000..c14029f7a --- /dev/null +++ b/jni/README.gst-sdk @@ -0,0 +1,8 @@ +* Get the GStreamer SDK for Android + - http://docs.gstreamer.com/display/GstSDK/Installing+for+Android+development + - user: test, pass: Jo6eem7e (these will go away once the SDK gets out of the + beta stage) +* Get the NDK +* export GSTREAMER_SDK_ROOT_ANDROID=/path/to/unpacked/gstreamer/sdk +* cd jni; /ndk-build +* cd ..; Continue building as usual diff --git a/jni/transcode.c b/jni/transcode.c new file mode 100644 index 000000000..783e496dd --- /dev/null +++ b/jni/transcode.c @@ -0,0 +1,91 @@ +#include + +#include + +static int init(void) +{ + /* XXX: ZERO thread-safety guarantees here */ + static gboolean inited = 0; + + if (inited) + return 0; + + gst_init(NULL, NULL); + return 0; +} + +static int transcode(const char *infile, const char *outfile, + const char *profile) +{ + GstElement *pipeline; + GstBus *bus; + GstMessage *msg; + gchar pipeline_str[1024]; + + init(); + + snprintf(pipeline_str, 1024, + "filesrc location=%s ! decodebin2 ! audioconvert ! " + "vorbisenc ! oggmux ! filesink location=%s", + infile, outfile); + + pipeline = gst_parse_launch(pipeline_str, NULL); + + gst_element_set_state (pipeline, GST_STATE_PLAYING); + + bus = gst_element_get_bus(pipeline); + msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, + GST_MESSAGE_ERROR | GST_MESSAGE_EOS); + + if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_ERROR) { + GError *err = NULL; + gchar *debug_info = NULL; + + gst_message_parse_error(msg, &err, &debug_info); + + GST_ERROR_OBJECT(pipeline, "%s -- %s", err->message, + debug_info ? debug_info : "no debug info"); + + g_error_free(err); + g_free(debug_info); + } + + if (msg != NULL) + gst_message_unref (msg); + + gst_object_unref (bus); + gst_element_set_state (pipeline, GST_STATE_NULL); + gst_object_unref (pipeline); +} + +jint Java_org_wikimedia_commons_Transcoder_transcode(JNIEnv* env, + jclass *klass, jstring infile, jstring outfile, jstring profile) +{ + const char *in; + const char *out; + const char *prof = NULL; + + if (!infile || !outfile) + return -1; + + in = (*env)->GetStringUTFChars(env, infile, 0); + out = (*env)->GetStringUTFChars(env, outfile, 0); + + if (profile) + prof = (*env)->GetStringUTFChars(env, profile, 0); + + return transcode(in, out, prof); +} + + +#ifdef TEST +int main(int argc, char **argv) +{ + if (argc != 3) + return -1; + + transcode(argv[1], argv[2], NULL); + + return 0; +} +#endif diff --git a/src/org/wikimedia/commons/Transcoder.java b/src/org/wikimedia/commons/Transcoder.java new file mode 100644 index 000000000..b25bb0fbd --- /dev/null +++ b/src/org/wikimedia/commons/Transcoder.java @@ -0,0 +1,10 @@ +package org.wikimedia.commons; + +public class Transcoder { + public static native int transcode(String infile, String outfile, + String profile); + + static { + System.loadLibrary("transcode"); + } +}