Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stream to ffserver from android

I need to stream from an android camera/ file to a remote ffserver which will broadcast my video. I can do this on the desktop in ubuntu by issuing a command like:

ffmpeg -f video4linux2 -s 640x480 -r 25 -i /dev/video0 http://192.168.0.20:8090/cam1.ffm

or stream a file like this:

ffmpeg -i /home/kev/share/movie.mp4 http://192.168.0.20:8090/cam1.ffm

So basically i want to be able to do the above from android. After several searches this is what i've done so far - i came across this link http://bambuser.com/opensource from which i downloaded the ffmpeg source and built it. The build outputs several things: 1. shared libs [libavcodec, libavcore, libavdevice, libavfilter,libavformat,libavutil,libswscale] 2. executables [ffmpeg,ffprobe]

Not sure how to plug my functionality with these resources this is what i've tried so far: 1. loaded the libs in my Activity using System.loadLibrary() then copied the ffmpeg executable to the assets folder which at runtime i copied to my application's "files" directory i then set permissions for the executable using Runtime.getRuntime().exec(). then the last step was to execute it in java with the following statement:

Runtime.getRuntime().exec("ffmpeg -i file:///android_asset/movie.mp4http://<server>:8090/cam1.ffm");

2. copied ffmpeg.c,the shared libraries and the "include" folder that was generated by the build to my jni folder and added a jni function that wraps around the main() function in ffmpeg.c. With this approach i've found myself having to copy several header files from the ffmpeg source for the ndk-build to succeed and i highly doubt if this is the way to go.

The above two approaches havnt worked for me, i'm not sure where i'm going wrong, so any help on how to do a simple ffmpeg streaming like an mp4 file from android would be highly appreciated.

like image 614
kev Avatar asked Feb 07 '13 12:02

kev


2 Answers

I got it working by using apporach 2, this is what i did. 1. copied ffmpeg.c,the "include" folder and the shared libraries to my project's jni folder.

  1. modified ffmpeg.c with reference to this blog post http://demo860.blogspot.com/2010/07/android-ffmpeg-dynamic-module-jni.html

  2. there were several errors while building with ndk so i just added the missing dependencies until finally the build succeeded.

At first the app would start and then immediately exit, this was due to a couple of things i forgot to do so make sure you've done the following to save yourself some hours and hair loss: - set internet permission on manifest(if media file is in sdcard, set write external storage permission and make sure the sdcard is mounted) - make sure the remote ffserver is running and configured correctly. you can confirm by streaming from a desktop - make sure you have the correct params passed

Now i can stream from an mp4 file in my sdcard to a remote ffserver, havnt tried streaming from the device camera yet.

like image 50
kev Avatar answered Oct 21 '22 01:10

kev


It seems to be a bit late to answer this question, but if you need a solution, here's one...

Well, I had devised a workaround to the same problem but through the first approach that is using a compiled FFmpeg Binary rather than JNI...

First, as far as it seems to me, the builds provided by Bambuser are way too old and FFmpeg has a vicious development cycle... So I'd rather suggest to custom build your own binary from the latest FFmpeg Source...

Here's a script that could be used to generate one :

#!/bin/bash

echo ""
echo " ********** FFmpeg Android Build ********** "
echo ""

NDK=$HOME/android-ndk-r8d
PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86
PLATFORM=$NDK/platforms/android-14/arch-arm
PREFIX=$HOME/FFmpeg.Binaries.Android
FFMPEG_BASE=$HOME/FFmpeg.Build

if [ -d "$FFMPEG_BASE" ]; then
    rm -v -r -f $FFMPEG_BASE
fi
if [ -d "$PREFIX" ]; then
    rm -v -r -f $PREFIX
fi

mkdir $FFMPEG_BASE
mkdir $PREFIX

# x264 Installation
echo ""
echo " ********** libx264 Installation ********** "
echo ""

cd $FFMPEG_BASE
git clone --depth 1 git://git.videolan.org/x264
cd $FFMPEG_BASE/x264

./configure --prefix=$PREFIX \
--enable-static \
--enable-pic \
--disable-asm \
--disable-cli \
--host=arm-linux \
--cross-prefix=$PREBUILT/bin/arm-linux-androideabi- \
--sysroot=$PLATFORM

make
sudo make install
sudo ldconfig

#FFmpeg Installation
echo ""
echo " ********** FFmpeg (Android) Installation ********** "
echo ""
cd $FFMPEG_BASE
# git clone --depth 1 git://source.ffmpeg.org/ffmpeg
cd $FFMPEG_BASE/ffmpeg

./configure --target-os=linux --prefix=$PREFIX \
--enable-cross-compile \
--enable-runtime-cpudetect \
--disable-asm \
--arch=arm \
--cc=$PREBUILT/bin/arm-linux-androideabi-gcc \
--cross-prefix=$PREBUILT/bin/arm-linux-androideabi- \
--disable-stripping \
--nm=$PREBUILT/bin/arm-linux-androideabi-nm \
--sysroot=$PLATFORM \
--enable-nonfree \
--enable-version3 \
--enable-gpl \
--disable-doc \
--enable-avresample \
--enable-demuxer=rtsp \
--enable-muxer=rtsp \
--disable-ffserver \
--disable-ffprobe \
--enable-ffmpeg \
--enable-ffplay \
--enable-libx264 \
--enable-encoder=libx264 \
--enable-decoder=h264 \
--enable-protocol=rtp \
--enable-hwaccels \
--enable-zlib \
--extra-cflags="-I$PREFIX/include -fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=armv7-a" \
--extra-ldflags="-L$PREFIX/lib"

make -j4 install

$PREBUILT/bin/arm-linux-androideabi-ar d libavcodec/libavcodec.a inverse.o
$PREBUILT/bin/arm-linux-androideabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -L$PREFIX/lib  -soname libffmpeg.so -shared -nostdlib  -z,noexecstack -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavfilter/libavfilter.a libavresample/libavresample.a libavformat/libavformat.a libavutil/libavutil.a libswscale/libswscale.a -lc -lm -lz -ldl -llog -lx264 --warn-once --dynamic-linker=/system/bin/linker $PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/libgcc.a

# rm -v -r -f $FFMPEG_BASE
clear
echo ""
echo "FFmpeg Android Build Successful..."
echo ""

ls -l -R $PREFIX

exit    

For the above script to work, Android NDK is required and could be downloaded from here. Download the NDK and extract to your /home/<username> directory or else customize the script as per your needs...

And also avoid using the file:// protocol in the command line, just specify the absolute path of the input file. And try to log the output from the FFmpeg process by gettin' instances of its stdout and stderr streams...

like image 24
Stryker33 Avatar answered Oct 21 '22 02:10

Stryker33