370 likes | 632 Views
GStreamer SDK Android Workshop. Xavi Artigas xartigas@fluendo.com. Workshop Objective. Learn how to make a media player on Android using GStreamer. What is GStreamer?. Open-source library for constructing graphs of media-handling elements Multiplatform
E N D
GStreamer SDKAndroid Workshop Xavi Artigas xartigas@fluendo.com
Workshop Objective Learn how to make a media player on Android using GStreamer
What is GStreamer? • Open-source library for constructing graphs of media-handling elements • Multiplatform • Each element is a plugin, for added extensibility
What is GStreamer? • General enough to allow media playback, streaming, non-linear video editing… • Has been around for more than 10 years • Used by several open-source and commercial applications
What is the GStreamer SDK? • Aims at simplifying the adoption of GStreamer • Pre-packaged, ready-to-use, for different platforms • Sticks to one particular version of GStreamer • Documentation! http://docs.gstreamer.com
The GStreamer SDK Documentation • Installation instructions • Tutorials • Basic tutorials • Playback tutorials • Android tutorials • iOS tutorials • Deployment instructions YOU ARE HERE
Workshop outline • Install the GStreamer SDK • Import Tutorial 4 into eclipse. Build. Run. • Build your own Media Player, step by step • Create a new Android project and copy selected bits of code from the tutorials
Install the GStreamer SDK Go to docs.gstreamer.com» Download » Android Download the zip or tarball, unpack Set GSTREAMER_SDK_ROOT_ANDROID: At the system level: System environment variables (Windows) .bashrc or .profile files (Linux and Mac) Or in Eclipse: Window » Preferences » C/C++ » Build » Build Variables
Import tutorial 4 into Eclipse File » New » Project… »Android Project from Existing Code Select tutorial 4 folder <GST_SDK_HOME>/share/gst-sdk/tutorials Right-click » Android Tools » Add native support Build & Run
Build your own Media Player Step 1: Link against GStreamer
Media Player 1: Link against GStreamer We are going to: Create a new Android application Add JNI support Add GStreamer to the mix Display the GStreamer version in the UI 13
Media Player 1: Link against GStreamer File » New » Project… Android Application Project Name it, choose any target API level > 9 Create a blank activity for it Right-click » Android Tools » Add native support Rename the JNI .cpp file to .c JNI file: Copy contents of Tutorial 1 Pay attention to your Activity class name!
Media Player 1: Link against GStreamer Java file: Import com.gstreamer.GStreamer Add a static class initializer that calls: System.loadlibrary(“gstreamer_android”); System.loadlibrary(“your_jni_class_name”); Call GStreamer.init(this) in onCreate Declare nativeGetGStreamerInfo method private native String nativeGetGStreamerInfo() Call it and assign the returned string to the TextView in the UI 15
Android.mk Copy contents of tutorial 1 This will remain almost the same for all tutorials Change these lines to match your JNI class name: LOCAL_MODULE := tutorial-1 LOCAL_SRC_FILES := tutorial-1.c Build & Run Media Player 1: Link against GStreamer 16
Build your own Media Player Step 2: A running pipeline
Media Player 2: A running pipeline We are going to: Create a thread from JNI to handle GStreamer Store all data needed by JNI code in a JNI struct Keep the pointer in the Java class Call from Java to JNI and vice versa 18
Media Player 2: A running pipeline JNI file: Replace with contents of Tutorial 2Search & replace the string “tutorial”Interesting points: CustomData JNI_OnLoad() gst_native_init() app_function() check_initialization_complete() gst_native_play() and gst_native_pause() error_cb() and state_changed_cb() 19
Media Player 2: A running pipeline A quick note on the used GStreamer pipeline: gst_parse_launch("audiotestsrc! audioconvert ! audioresample ! autoaudiosink", &error); The above line creates the following pipeline: audiotestsrc audioconvert audioresample autoaudiosink 20
Add two Play and Pause buttons to the UI Set them to be initially disabled Media Player 2: A running pipeline 21
Media Player 2: A running pipeline Java file: Copy interesting bits from Tutorial 2 Native method declarations & Static class initializer Call nativePlay and nativePause when buttons are pressed Call nativeInit at the end of onCreate Call nativeFinalize at the beginning of onDestroy Create a method with this signature: private void onGStreamerInitialized() In it, enable the UI buttons from the UI thread Create a method with this signature: private void setMessage(final String msg) In it, set the TextView message from the UI thread 22
Media Player 2: A running pipeline Android.mk Simply replace: GSTREAMER_PLUGINS := coreelements by: include $(GSTREAMER_NDK_BUILD_PATH)/plugins.mk GSTREAMER_PLUGINS := $(GSTREAMER_PLUGINS_CORE) $(GSTREAMER_PLUGINS_SYS) Build & Run 23
Build your own Media Player Step 3: Video!
Media Player 3: Video! We are going to: Add a video surface to the UI Tell GStreamer to use it Use a pipeline that produces video 25
Media Player 3: Video! JNI file: Copy interesting bits from Tutorial 3 List of #include’s Add video_sink and native_window to CustomData check_initialization_complete() app_function() Call to gst_parse_launch() Call to gst_bin_get_by_interface() gst_native_surface_init() gst_native_surface_finalize() Add new methods to the native_methods array 26
Media Player 3: Video! A quick note on the used GStreamer pipeline: gst_parse_launch("videotestsrc! warptv ! ffmpegcolorspace! autovideosink", &error); The above line creates the following pipeline: videotestsrc warptv ffmpegcolorspace autovideosink 27
Add a SurfaceViewto the UI Media Player 3: Video! 28
Java file: Copy interesting bits from Tutorial 3 Implement SurfaceHolder.Callback interface Add new native methods nativeSurfaceInit and nativeSurfaceFinalize Add this to onCreate: SurfaceViewsv = (SurfaceView)findViewById (R.id.video); SurfaceHoldersh = sv.getHolder(); sh.addCallback (this); Copy the SurfaceHolder callbacks: surfaceChanged() surfaceCreated() surfaceDestroyed() Media Player 3: Video! 29
Android.mk Add a new plugin: $(GSTREAMER_PLUGINS_EFFECTS) A new library (after the shared libraries line): LOCAL_LDLIBS := -llog -landroid And some GStreamer extra libraries (after the plugins line): GSTREAMER_EXTRA_DEPS := gstreamer-interfaces-0.10 gstreamer-video-0.10 Build & Run Media Player 3: Video! 30
Build your own Media Player Step 4: A basic player
Media Player 4: A basic player We are going to: Play any media from an arbitrary URI 32
Media Player 4: A basic player JNI file: Copy interesting bits from Tutorial 4 gst_native_set_uri() Add it to the native_methodsarray Replace the pipeline description in app_funcsimply with “playbin2” 33
Java file: Copy interesting bits from Tutorial 4 Add new native method: private native void nativeSetUri (String uri); Call it in onGStreamerInitialized() Media Player 4: A basic player 34
Android.mk: Add new plugins: $(GSTREAMER_PLUGINS_PLAYBACK) $(GSTREAMER_PLUGINS_CODECS) $(GSTREAMER_PLUGINS_NET) Additional plugins (use with caution): $(GSTREAMER_PLUGINS_CODECS_RESTRICTED) Media Player 4: A basic player 35
AndroidManifest.xml: Request INTERNET access permission android.permission.INTERNET Media Player 4: A basic player 36