Sunday, July 27, 2014

Building The Native Android WebRTC Source (C++/Java JNI)


                              pictured: I could swear I was clean-shaven when I started this. Note the little screen where I'm holding the phone (that's where the main image comes from)

TL;DR

1. Get to https://apprtc.appspot.com
2. Get the APK that was built on my machine, open it on your Android device and input the room number from https://apprtc.appspot.com

So, you want to build a WebRTC enabled native Android app?

At the end of this process, if all goes well, you should have everything you need to start writing native android WebRTC apps: a sample APK, an Android project (Java) with a ready made JNI (C++), and the native (C++) WebRTC implementation (video engine, peer connection, etc.).

It might be a bit more complicated than it would first appear. Google's WebRTC is using the same Android toolchain as Chromium, which is known to be pretty complex. Obtaining and Installing the toolchain is a pain, and if you are an android developer with little Linux and C++ experience, you would have to get familiarized with some tools like ninja for building and stuff like that.

If you are a Java Android developer that's new to this WebRTC business, it might be a good idea to be aware of the following this before you start:

1. WebRTC spec is pretty mature for a young standard and it’s being used in production by some companies already - but not the native mobile versions - and anyway, the spec and Google’s implementation still changes. Sometimes changes are small, sometimes big. Currently there is also talk about a next-gen platform that's called ORTC - kind of a more accessible WebRTC with an easier API (it's said to be using WebRTC "under the hood" so WebRTC JavaScript code probably won't break).

2. Google developers like to re-use things. This is why the implementation in Chromium is re-used for the Android version, which is good software engineering, but not so nice for you as an Android developer caring only about using WebRTC in your native app.

3. Upcoming version of Android (perhaps even L) might have built in Java Support for WebRTC, so you would not need to do crazy things like compiling the entire implementation into your app.

When compiling WebRTC for Android, I used Simon Guest's great blog post and WebRTC.org's getting started tutorial. Using a combination of those two resources I was able to build the demo app after a good few hours, and hopefully this blog post can help you in case you get stuck and these two other resources are not enough.

If you are still reading, you brave soul, let's do this!

Before we begin, make sure you are using a strong 64 bit machine with a fast and reliable internet connection (a slow connection would generate timeouts that can really mess up the build process, so be warned!).

1. You can only compile WebRTC for Android on Linux (no, Mac OSX won't work). Internally, Google uses a customized version of Ubuntu, so you want to use something similar to that (otherwise things might break).

My configuration: MacBook pro, using VMWare Fusion to run Ubuntu 14.04 LTS 64 bit.

2. Make sure you have git and SVN installed:
$ sudo apt-get install git git-svn subversion

3. Install Chromium Depot-Tools and other dependences:
* Fetch depot_tools:
$ git clone  https://chromium.googlesource.com/chromium/tools/depot_tools.git
* Add depot_tools to your PATH:
$ export PATH=`pwd`/depot_tools:"$PATH"
* Install some dependencies required to build Chromium by running this script.
* Install 32-bit compatiblity lib (you are building a 32 bit app from a 64 bit machine):
$ sudo apt-get install package-name:i386

4. Installing Java JDK
You can build WebRTC with Open JDK 1.6 or 1.7:
$ sudo apt-get install openjdk-7-jdk

Make also sure that OpenJDK is selected as default:
$ sudo update-alternatives --config javac
$ sudo update-alternatives --config java
$ sudo update-alternatives --config javaws
$ sudo update-alternatives --config javap
$ sudo update-alternatives --config jar
$ sudo update-alternatives --config jarsigner

5. Now let’s configure gclient. Create a working directory (e.g. WebRTC), enter it, and run:
the location of the JDK on Ubuntu is usually /usr/lib/jvm/java-6-openjdk/
$ export JAVA_HOME=<location of Java SE 6 - JDK>
$ echo "target_os = ['android', 'unix']" >> .gclient
$ gclient sync --nohooks
$ cd trunk
$ source ./build/android/envsetup.sh
$ export GYP_DEFINES="$GYP_DEFINES OS=android"
$ gclient runhooks

6. If you reach this step, all that’s left is to order ninja to build the AppRTCDemo target (that’s the Android app containing WebRTC and the AppRTC demo app), which will generate an APK from the Android project:

$ ninja -C out/Debug AppRTCDemo

If all is well, you should see the “AppRTCDemp-debug.apk” file under the out/Debug directory.

The actual Android code is at trunk/webrtc/examples/android/media_demo/






No comments:

Post a Comment