Gstreamer examples in Android Studio
Asked Answered
H

2

8

I have been trying to get Gstreamer working in Android studio, following their tutorials, see for example here:

https://gstreamer.freedesktop.org/documentation/tutorials/android/link-against-gstreamer.html

But in the latest Android studio there is no jni/Android.mk. Where do I put the code at the end of that web page?

Should it go in the CMakeLists.txt? Or should something different go in there?

Or do I just make an Android.mk file, and if so, where (as there is no jni folder, only a cpp folder)? And how do I tell Android studio where to find my Android.mk file?

Thanks!

Hagerty answered 11/7, 2017 at 20:47 Comment(1)
If my answer helped, please accept it. Thank youAkmolinsk
A
30

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)

enter image description here

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.

Akmolinsk answered 14/9, 2017 at 15:48 Comment(8)
This is a great reference for building the gstreamer tutorials with Android Studio! Wish it was part of the official docs. For Android Studio on windows, I had to apply [this fix][1] for invalid linker name in argument '-fuse-ld=gold'. I also had to remove the armeabi target due to a linker relocation overflow error. [1]: #44650970Knobby
Thank you for the positive feedback, it's always good to know that the step-by-step is working and another people are have success with it. I edited the tutorial adding the information that the tutorial are made for linux (maybe the error that you got was because you made it on windows) and with the information to remove armeabi from filters in case of trying tutorials 2 and 3. If another people will have the same problem you had on windows I will add another step including the link you used to solve your problem.Akmolinsk
@EduardoFernando pretty sure this is a Windows problem in general - see "Windows linkage problems" in GStreamer but the solution they provide on that page didn't work for me (hence my answer to the linked question).Outmaneuver
@AntoineZambelli Thanks for the feedback. Recently a friend of mine tried to use it on Windows and he had the same problem, so I will add +1 step on tutorial specifically for Windows usersAkmolinsk
Do you know how I'd do this with cmake? The rest of my project is using cmake and I'd prefer not shifting to ndk-build.Atp
@CrearoRotar The short answer is: I don't know. If I understood correctly from #39589927 , when you choose ndk-build you are using the file "Android.mk" to link and build the project. So I think you should look for how to "translate" de "Android.mk" to a CMake file. But I also don't know if after the "translate" all the steps from the tutorial will keep working.Akmolinsk
Yes, I tried to find the exact same thing! I couldn't how to transform an Android.mk to cmakelist so, I decided it'd just be easier for me to shift everything to Android.mk because I am more comfortable there.Atp
I'm having this invalid linker name in argument '-fuse-ld=gold' in MacOS, so the fix doesn't workThicken
K
2

After a few days with struggling, I found that the gstreamer tutorial docs have been updated to use the gradle build system. You can see a full working example in https://cgit.freedesktop.org/gstreamer/gst-docs/, see commit from 14 June 2018, 'Android tutorials: add gradle build system'

Kenzie answered 19/6, 2018 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.