Gstreamer Examples in Android Studio

Gstreamer examples in Android Studio

I've made all tutorials from the Gstreamer's site work but it was very hard. To help another people I documented it. Here's the tutorial for linux (ubuntu) but might work on windows too, if you have any question regarding it, just ask :)... I will also link a project from gitlab ready to run in Android Studio:
(tutorial 5)
https://gitlab.com/eduardoprado/gstreamer-tutorial5.git
(tutorial 2) https://gitlab.com/eduardoprado/gstreamer-tutorial2.git

Using the library at Android Studio

The best way to understand and integrate GStreamer into the project is doing the tutorials in this site:
Sorry, you have to find the link by yourself, i don't have reputation to post the direct link
However these tutorials are old and were made to work in Eclipse, in order to work with Android Studio is necessary to:

1. Import the tutorials using Android Studio’s Import option;

2. Set the Android Studio to use NDK;

3. Set a variable inside the “Android.mk” file;

4. Set Gradle compile only some architecture;

5. Copy the Gstreamer class to the correct path.

6. Disable "Instante Run" from Android Studio. (edit made 10/27/2017)

7. Fix "linker gold argument" (edit made 11/28/2017 WINDOWS users only)

Each of these instructions will be explained in the following sections:

1. Import the tutorials using Android Studio’s Import option

Android Studio have an option to import project from another IDEs. To import the Gstreamer’s project made to Eclipse to the following steps:

1. Download all tutorials from git: ;

2. In Android Studio go to File -> New Import Project;

3. In the new browser for file screen, go to "gst-docs", select the desired project (in my case tutorial 5) "android-tutorial-5" and hit "OK".

Selecting Project
2. Set the Android Studio to use NDK

Now that the project has been imported, it is needed to inform Android Studio that the project uses the NDK framework. Thus the Gradle will be able to import and handle all dependencies.
To do this we need to link the file Android.mk (already in the tutorial folder):

1. Right click at “app” in the left navigation tab

2. Click at "Link C++ Project with Gradle"

3. In the new window:

  • Change "Build System" from "CMake" to "ndk-build"
  • Click at "..." and browse to the Android.mk’s location "/AndroidStudioProjects/android-studio-5/app/src/jni/Android.mk"
  • Hit OK

Now External Build Files will appear in the project’s left tab. Some erros will also appear, it is normal and will be corrected in the next sections.

3. Set a variable inside the “Android.mk” file

The last section error happens because “Android.mk” won’t find the path to where GStreamer was extracted. So let’s set the GSTREAMER_ROOT_ANDROID variable to the path where GStreamer was extracted.
To achieve this:

1. Browse the left tab and expand "External Build FIles";

2. Double click at Android.mk to open the file;

3. At line 10, under "include $(BUILD_SHARED_LIBRARY)", type:

- "GSTREAMER_ROOT_ANDROID := path"

- NO quotation marks

- path is the path (obviously) where GStreamer was extracted.

After it Gradle will synchronize and an error "***Target arch ABI not supported: mips. Stop" will be showed.

Setting GSTREAMER_ROOT_ANDROID

4. Set Gradle compile only some architecture

The last section error occurs because Grade tries to build for different architectures but it doesn’t find the file to build in MIPS. To solve this problem we will specify the architectures to build excluding MIPS:

1. In the left browser tab, expand Gradle Scripts;

2. Double click at "build.gradle (Module: app)"

3. In the line under "moduleName" type :
(to run tutorial 5)

- abiFilters 'x86','armeabi', 'armeabi-v7a', 'arm64-v8a'

(to run tutorial 2)

- abiFilters 'x86', 'armeabi-v7a', 'arm64-v8a'

(edit 11/21/2017: Recently I had problems with arm64-v8a... devices with 64 bits won't show live streaming, i solved "removing arm64-v8a" too )

(edit 3/29/2018: I realised that may be needed to remove more architecture depending of your Gstreamer and NDK version, if it's not working try different removal combinations)

Sample Image

5. Copy the Gstreamer class to the correct path.

The last needed configuration regards a package importation in the Main class.

Class import error

The error showed above occurs because there’s no GStreamer class to get the “.init” function. To solve this we need to copy the class GStreamer from the file where it was extracted and paste at the specified path:

  1. Browse, using the file manager from your system, to the file where you extracted GStreamer.
  2. From the extracted file, browse to the location of GStreamer class. It is located at /arm/share/gst-android/ndk-build/GStreamer.java
    Copy GStreamer.java
  3. Go back to Android Studio and create the import path that the Main class is trying to access starting from /app/java at the left browser tab. So, to the image above, we have to create the path org.freedesktop.gstreamer and paste the file GStreamer.java in it:
  4. Right click at java Folder
  5. New-> Package
  6. Type org.freedesktop.gstreamer
  7. Right click at org.freedesktop.gstreamer and Paste
    5.The importation error is now solved, but the new class has errors. To solve them all you have to do is erase from the copied class the “@” and all that’s located between the “@”.

6. Disable "Instante Run" from Android Studio. (edit made 27/10/2017)
This feature from Android Studio could cause problems when trying to run the project, so it's a good idea disable it.

Disabling "Instante Run":

1. At Android Studio go to "File" -> "Settings..." (Ctrl+Alt+S)

2. "Build, Execution, Deployment" -> "Instant Run"

3. Uncheck "Enable Instant Run to hot swap code/resource changes on deploy (default enabled)"

4. Hit "Apply"

7. Fix "linker gold argument" (WINDOWS users only)

When trying to Windows you will get the error:

Error:error: invalid linker name in argument '-fuse-ld=gold'

To fix it:

1. Go to Android NDK Linker (GStreamer) - invalid linker name -fuse-ld=gold

2. Follow the Antoine Zambelli's answer instructions.

GStreamer tutorial for Android Studio

these tutorials are really not made for Android Studio but I was able to make them work and I already answer one question on how to make it work at link: Gstreamer examples in Android Studio
You could also try to set the variable GSTREAMER_ROOT_ANDROID at Android.mk file... Take a look at step 3 from the link typed above.

Answering 1: Maybe it's a similar problem I had at step 5 from the Question from the link above. Take a look there, perhaps it will help.

Answering 2: I'm not sure, but I think you don't need to compile .so...

In the link I typed above there's also a project from the Tutorial 5 already set to work if Android Studio (but it was made in Linux)... Maybe it could also help you.

Using Gstreamer for Android with JNA & gst1-java-core?

So it was my mistake not knowing enough about Android and the sub systems to know that even though Android is primarily developed in Java it does not support running Java "natively". This means that JNI is not relevant as according to Wikipedia JNI is:

In software design, the Java Native Interface is a foreign function interface programming framework that enables Java code running in a Java virtual machine to call and be called by native applications and libraries written in other languages such as C, C++ and assembly

So in saying that - because Android does not run a JVM but rather it runs Native C on a Linux Kernel this means that trying to integrate a "java wrapper" through the JNI is not currently possible on Android.

Because of this - the common solution and following Gstreamers official tutorials appears to currently be the only way to use Gstreamer in Android.

I only have myself to blame for thinking that everyone else had just "missed" JNI and gst1-java-core and that I somehow knew better than even the Gstreamer official website itself.

I felt like answering this question though because if anyone else goes down the wrong track I can now officially suggest to following the official tutorials and create an Android App with Native support so that you can compile and integrate the C code directly from Gstreamer and then make your app work like that.

I can currently say that following the above paragraph's advice resulted in me creating a working Android application with a Gstreamer live stream currently up and running using a custom pipeline. I'm not going to say it was easy; because it isn't; especially if you are not great at C programming. But it is definitely doable and the tutorials are good.

GStreamer Unable to build pipline: no element tcppclientsrc

I managed to figure it out. For anyone with this particular issue in the future, you must check your Android.mk file, make sure GSTREAMER_PLUGINS has all the required plugins for what you require.

Plugins can be found here: https://gstreamer.freedesktop.org/documentation/installing/for-android-development.html?gi-language=c

Unable to build GStreamer for Android Tutorials

I posted the above question on the GStreamer Android mailing list and got the following response:

Hi,
Unfortunately we forgot to include pkg-config in this release for Mac OS X.
You can download it from here:
http://macpkg.sourceforge.net/

I used homebrew to install pkg-config and was able to build all of the Android Tutorials via the command line using ndk-build.

I still have a number of issues in Eclipse:

  1. * GSTREAMER_SDK_ROOT_ANDROID is not defined! - I can not get Eclipse to pick up this variable
  2. If I hard code the variable into the make file I then get /bin/sh: pkg-config: command not found - pkg-config is installed and working via the CLI

I believe some of the issues are related to Android Issue 33788 and I have attempted to resolve them using CDT 8.0.2 instead of CDT 8.10 but it did not solve either of the above issues.

I have worked around the problem for now by doing the following:

  1. Disabled CDT building for my project
  2. Building the NDK code using the CLI ndk-build
  3. Building and deploying the application to my device via Eclipse as normal


Related Topics



Leave a reply



Submit