Wednesday, March 25, 2015

How did I build an Android App with Gstreamer Support

Well, it is just a personal project of mind. I'm making an Android App that can play video sent from my Raspberry Pi. Since "VideoView" component that comes with Android doesn't work for me. I have to integrate GStreamer library to my App. I found it extremely difficult. First of all there were lots of changes on Android, Gstreamer, and the development tool since the existence of these stuff. So there are lots of Google result that do not work today. And those official documentation are already out of date. There are many small pieces you'll need to add by yourself. That's why I do my own howto document here. Cause I know I'll forget the detail in the future.

Get the Tools

First problem is the Android development tool. The Gstreamer-Android library is a NDK pre-built library. However as of today the official development tool for Android is Android Studio, which have no support on NDK. I know some Android developers had done great job to make NDK works on Android Studio, but I found that is out of my hand. So I choose the old tool "ADT for Eclipse". I just grabbed the latest version of Eclipse for Java Development (Luna release 2). And installed ADT plugin as instructed on here. This is just the beginning.

Second tool you'll need is the NDK. The latest version today is NDK-r10d. You just need to download it and extract it somewhere in you hard drive. And, once you started a new work space with Eclipse, remember you need to set the path of NDK in the Eclipse preference (Perference > Android > NDK).

Also you'll need the Gstreamer-Android library. You should get it from here. Note that the library is a pre-built native library so you need to choose the CPU architecture. The site do provide ARM, ARMv7 and x86 for you to choose from. To start with, get the ARM debug version first.

Get the Tutorial Source

There are two main stream versions of Gstreamer: 0.10 and 1.0. Actually the latest version as of today is 1.4.5. The old version was better documented, but you can't make 1.0 version works for you by just reading those old document. The development team had published 5 Gstreamer-Android tutorial programs. With detail document that provide section-by-section explanation. However, in the website for 1.0 you cannot find any tutorial code. Finally I found this, which is great work done by slomo who ported the 0.10 tutorial code to work under 1.0 API.  Just use git clone to copy the tutorial code to your own hard disk.

Build the Tutorial Projects

You can easily find the 5 Android Tutorial projects in the tutorial source. Use "New > Project" command. Then choose "New Android Project from existing code". Browse to one of the 5 folders for the tutorials. And I recommend to have Eclipse copy the files into your work space.

Here come all the tricky parts. There are still many small changes you need to make before you can make it work.

1. Define Environment String

Actually you only need to define GSTREAMER_ROOT_ANDROID. Define it in the project's preference. Under "C / C++ Build > Environment". It should points to the root of your Gstreamer-Android API library.

2. Add Some Magic in Android.mk

At this point if you try to build the project. Eclipse will hang at certain point. And you don't get any error or hint. Google bought me the solution. Do two things:
  1. Add the line "ifdef BUILD_PROJECT" at the beginning of Android.mk under the jni folder in your project. And add the line "endif" at the bottom.
  2. Open the project's preference. Under "C / C++ Build", un-check "Use default build command". And add "BUILD_PROJECT=1" to the build command

3. Define Path and Symbol

In the project's preference, go to "C/C++ General > Paths and Symbols". Here you need to add all directories that contain all the header files that your C code need. They would be come from the NDK (<ndk root>/platforms/android-xx/arch-arm/usr/include) and GStreamer (<gstreamer-android api root>/include/<library name>). Without this even you can build your project successfully, the project will not run in the simulator as Eclipse still find "error" in the C editor.

4. Define NULL Symbol

Even with all above, you will still find one type of fault in the C editor. The editor cannot resolve the symbol "NULL". I don't know why it is not in any included header files. I think maybe the reason is I'm doing this on Windows. So I added it by putting these 3 lines under the include statements of the C code:


#ifndef NULL
#define NULL   ((void *) 0)
#endif

After all these. Try to run your project as an Android Application. If everything is fine, you'll get a working tutorial app. 

It is really a pain to do so many Google and troubleshoot all the problems one by one. I'm still wondering why I can't find a single instruction page or forum thread that give me all solution. Hope this can help you.