Getting Started with WebRTC on Android and Linux

July 31, 2013

Logos of WebRTC, Android and Linux

What is WebRTC?

WebRTC is skype/google hangouts/msn-webcam in the browser - plugin free.

It is also a general way for client applications to talk to each other over the web.

Try it out now with the AppRTC demo.

As well as working in the browser, there are clients for desktop applications, Java apps, iOS and Android.

This article covers how to set up your own version of the AppRTC demo as well as how to compile your own AppRTC Android application. It is meant as a starting point for more poking around with the code.

For more information on WebRTC and how to use WebRTC in the browser see Sam Dutton's article on HTML5 Rocks and watch the Google IO talk.

These instrucations worked on a x86_64 Arch Linux installation.

Your own AppRTC app

Install gclient and Google AppEngine

# On Arch Linux
yaourt -S \
    depot_tools-svn \
    google-appengine-python

On Arch Linux Python 3 is the default Python interpretter, whilst gclient assumes Python 2 is. Virtual environment comes to save the day. If your default interpretter is Python 2 you can choose to ignore this part (try running python and it will print out its version number).

# On Arch Linux
pacman -S \
    python2 \
    python2-virtualenv

Sync the webrtc repo

mkdir webrtc/
cd webrtc/

# On systems where python 2 is not the default interpretter create a virtual
# environment, activate it and install dependencies
virtualenv2 env
source env/bin/activate
pip install colorama

gclient config http://webrtc.googlecode.com/svn/trunk
gclient sync --force

Launch the AppRTC demo

cd trunk/samples/js/apprtc
# Setting host to 0.0.0.0 allows both localhost access and hostname access
# otherwise how are your so called friends going to connect to you?
dev_appserver.py --host 0.0.0.0 .

Open up Chrome or Firefox to (http://localhost:8080)[http://localhost:8080] and then get a friend to point themselves at http://<YOUR_HOSTNAME>:8080 with the same room number (the ?r=XXXXXXXX part of the URL the site gives you).

WebRTC on Android

In the browser, the easy way

Install Chrome Beta on your Android device, the got to (apprtc.appspot.com)[apprtc.appspot.com], or your own local/deployed version.

Native Android app, the less easy way

You'll need a device with api 13 or higher, I tried creating an AVD (with api 17 and an SD card) but I couldn't get the application to load, may have been a problem with my laptop not being able to emulate OpenGL ES 2.0 - who knows. I caved in a bought a new Nexus 4 (currently awaiting my first scratch).

All based off from the WebRTC documentation.

Parts that fetch dependencies are Arch Linux only and noted as such, also on Arch Linux the packages are found in the AUR and are thus installed conveniently using yaourt.

Get the Android SDK and Android NDK

# On Arch Linux
yaourt -S \
    android-sdk \
    android-ndk

Make sure you have Oracle JDK6 installed. This is a requirement of the build process, I tried with OpenJDK and it ran into problems with the .apk that was spat out.

# On Arch Linux
yaourt -S \
    jdk6

Time to get ready to rock the house

# Note: this is a separate repo from the one we ran AppRTC from, i.e., don't
# create this inside the webrtc repo.

mkdir libjingle/
cd libjingle/

# On systems where python 2 is not the default interpretter create a virtual
# environment, activate it and install dependencies
virtualenv2 env
source env/bin/activate
pip install colorama

gclient config http://libjingle.googlecode.com/svn/trunk
echo "target_os = ['android', 'unix']" >> .gclient


# Set cross compile flags
export CC=/opt/android-ndk/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc
export CXX=/opt/android-ndk/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++
export AR=/opt/android-ndk/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ar
export RANDLIB=/opt/android-ndk/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ranlib
export CC_host=gcc
export CXX_host=g++



# Get ready to wait a while
gclient sync --force

cd trunk/

# Env vars need to be set up to target Android;
# easiest way to do this is to run
. ./build/android/envsetup.sh

# Set up webrtc-related GYP variables
export GYP_DEFINES="build_with_libjingle=1 build_with_chromium=0 libjingle_java=1 enable_tracing=1 $GYP_DEFINES"

# Set cross compile flags
export CC=/opt/android-ndk/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc
export CXX=/opt/android-ndk/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++
export AR=/opt/android-ndk/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ar
export RANDLIB=/opt/android-ndk/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ranlib
export CC_host=gcc
export CXX_host=g++

# Generate Android-targeting .ninja files
gclient runhooks

Rock the house by building the app

ninja -C out/Debug AppRTCDemo

Then install it

adb install -r out/Debug/AppRTCDemo-debug.apk

Open the application on an Android device and the AppRTC demo in a browser and set the URL in the Android to the same URL as in the browser.