Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rtmpts / rtmps / crypto protocol on ffmpegframegrabber / ffmpegframerecorder support with libspeex #2

Closed
ratmole opened this issue May 6, 2014 · 15 comments

Comments

@ratmole
Copy link

ratmole commented May 6, 2014

label:encancement

to build javacpp-presets with rtmpts / rtmps / crypto protocol support with libspeex enabled please replace the following files

cppbuild.sh :

#!/bin/bash
# Scripts to build and install native C++ libraries

KERNEL=(`uname -s | tr [A-Z] [a-z]`)
ARCH=(`uname -m | tr [A-Z] [a-z]`)
case $KERNEL in
    darwin)
        OS=macosx
        ;;
    mingw*)
        OS=windows
        KERNEL=windows
        if [[ $TARGET_CPU == "x64" ]]; then
            ARCH=x86_64
        fi
        ;;
    *)
        OS=$KERNEL
        ;;
esac
case $ARCH in
    arm*)
        ARCH=arm
        ;;
    i386|i486|i586|i686)
        ARCH=x86
        ;;
    amd64|x86-64)
        ARCH=x86_64
        ;;
esac
PLATFORM=$OS-$ARCH
echo "Detected platform \"$PLATFORM\""

while [[ $# > 0 ]]; do
    case "$1" in
        -platform)
            shift
            PLATFORM="$1"
            ;;
        install)
            OPERATION=install
            ;;
        clean)
            OPERATION=clean
            ;;
        *)
            PROJECTS+=("$1")
            ;;
    esac
    shift
done
echo "Targeting platform \"$PLATFORM\""

if [[ -z "$OPERATION" ]]; then
    echo "Usage: ANDROID_NDK=/path/to/android-ndk/ bash cppbuild.sh [-platform <name>] [<install | clean>] [projects]"
    echo "where platform includes: android-arm, android-x86, linux-x86, linux-x86_64, macosx-x86_64, windows-x86, windows-x86_64, etc."
    exit 1
fi

if [[ -z "$ANDROID_NDK" ]]; then
    ANDROID_NDK=~/projects/android/android-ndk/
fi
if [[ -z "$NDK_TOOLCHAIN" ]]; then
    NDK_TOOLCHAIN=arm-linux-androideabi-4.6
fi

export NDK_TOOLCHAIN
export ANDROID_NDK
export ANDROID_CPP="$ANDROID_NDK/sources/cxx-stl/gnu-libstdc++/4.6/"
case $PLATFORM in
    android-x86)
        export ANDROID_BIN="$ANDROID_NDK/toolchains/x86-4.6/prebuilt/$KERNEL-$ARCH/bin/i686-linux-android"
        export ANDROID_ROOT="$ANDROID_NDK/platforms/android-9/arch-x86/"
        ;;
    *)
        export ANDROID_BIN="$ANDROID_NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/$KERNEL-$ARCH/bin/arm-linux-androideabi"
        export ANDROID_ROOT="$ANDROID_NDK/platforms/android-9/arch-arm/"
        ;;
esac

function download {
    COMMAND="curl -C - -L $1 -o $2"
    echo "$COMMAND"
    $COMMAND
}

if [[ ${#PROJECTS[@]} -eq 0 ]]; then
    PROJECTS=(opencv ffmpeg flycapture libdc1394 libfreenect videoinput artoolkitplus)
fi

for PROJECT in ${PROJECTS[@]}; do
    case $OPERATION in
        install)
            echo "Installing \"$PROJECT\""
            mkdir $PROJECT/cppbuild
            cd $PROJECT/cppbuild
            source ../cppbuild.sh
            cd ../..
            ;;
        clean)
            echo "Cleaning \"$PROJECT\""
            rm -Rf $PROJECT/cppbuild
            ;;
    esac
done

ffmpeg/cppbuild.sh:

if [[ -z "$PLATFORM" ]]; then
    echo "This file is meant to be included by the parent cppbuild.sh script"
    exit 1
fi

if [[ $PLATFORM == windows* ]]; then
    FFMPEG_VERSION=2.2.1
    download http://ffmpeg.zeranoe.com/builds/win32/dev/ffmpeg-$FFMPEG_VERSION-win32-dev.7z ffmpeg-$FFMPEG_VERSION-win32-dev.7z
    download http://ffmpeg.zeranoe.com/builds/win64/dev/ffmpeg-$FFMPEG_VERSION-win64-dev.7z ffmpeg-$FFMPEG_VERSION-win64-dev.7z
    download http://ffmpeg.zeranoe.com/builds/win32/shared/ffmpeg-$FFMPEG_VERSION-win32-shared.7z ffmpeg-$FFMPEG_VERSION-win32-shared.7z
    download http://ffmpeg.zeranoe.com/builds/win64/shared/ffmpeg-$FFMPEG_VERSION-win64-shared.7z ffmpeg-$FFMPEG_VERSION-win64-shared.7z
    download http://msinttypes.googlecode.com/files/msinttypes-r26.zip msinttypes-r26.zip

    INSTALL_DIR=/C/MinGW/local
    mkdir -p $INSTALL_DIR/include
    unzip -o msinttypes-r26.zip -d $INSTALL_DIR/include
else
    FFMPEG_VERSION=2.2.1
    download http://ffmpeg.org/releases/ffmpeg-$FFMPEG_VERSION.tar.bz2 ffmpeg-$FFMPEG_VERSION.tar.bz2
    download ftp://ftp.videolan.org/pub/videolan/x264/snapshots/last_stable_x264.tar.bz2 last_stable_x264.tar.bz2
    download https://github.com/guardianproject/openssl-android/archive/master.zip openssl-android-latest.zip
    download https://github.com/yayanyang/speex-android/archive/master.zip speex-android-latest.zip


    tar -xjvf ffmpeg-$FFMPEG_VERSION.tar.bz2
    mv ffmpeg-$FFMPEG_VERSION ffmpeg-$FFMPEG_VERSION-$PLATFORM
    cd ffmpeg-$FFMPEG_VERSION-$PLATFORM
    tar -xjvf ../last_stable_x264.tar.bz2
    X264=`echo x264-snapshot-*`
fi

case $PLATFORM in
    android-arm)
        cd $X264
        ./configure --enable-static --enable-pic --disable-cli --cross-prefix="$ANDROID_BIN-" --sysroot="$ANDROID_ROOT" --host=arm-linux --extra-cflags="-DANDROID -fPIC -ffunction-sections -funwind-tables -fstack-protector -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fomit-frame-pointer -fstrict-aliasing -funswitch-loops -finline-limit=300" --extra-ldflags="-nostdlib -Wl,--fix-cortex-a8 -lgcc -ldl -lz -lm -lc"
        make -j4
        cd ..

        unzip -o ../speex-android-latest.zip
        SPEEX=`echo speex-android-master`
        cd $SPEEX/speexjni
        $ANDROID_NDK/ndk-build -j4
        cd ../../

        unzip -o ../openssl-android-latest.zip
        OPENSSL=`echo openssl-android-master`
        cd $OPENSSL
        $ANDROID_NDK/ndk-build -j4
        cd ../

        patch -Np1 < ../../ffmpeg-$FFMPEG_VERSION-android.patch
        ./configure --prefix="$ANDROID_NDK/../local/" --enable-shared --enable-gpl --enable-nonfree --enable-version3 --enable-runtime-cpudetect --disable-outdev=sdl --enable-libx264 --extra-cflags="-I$SPEEX/speexjni/jni/include -I$OPENSSL/include -I$X264" --extra-ldflags="-L$SPEEX/speexjni/libs/armeabi -L$OPENSSL/libs/armeabi -L$X264" --enable-cross-compile --cross-prefix="$ANDROID_BIN-" --sysroot="$ANDROID_ROOT" --target-os=linux --arch=arm --extra-cflags="-DANDROID -fPIC -ffunction-sections -funwind-tables -fstack-protector -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fomit-frame-pointer -fstrict-aliasing -funswitch-loops -finline-limit=300" --extra-ldflags="-nostdlib -Wl,--fix-cortex-a8" --extra-libs="-lgcc -ldl -lz -lm -lc -lssl -lcrypto -lspeex" --disable-symver --disable-programs --enable-protocol=tls --enable-openssl --enable-libspeex --libdir="$ANDROID_NDK/../local/lib/armeabi/" --shlibdir="$ANDROID_NDK/../local/lib/armeabi/"
        make -j4
        make install
        ;;
     android-x86)
        cd $X264
        ./configure --enable-static --enable-pic --disable-cli --cross-prefix="$ANDROID_BIN-" --sysroot="$ANDROID_ROOT" --host=i686-linux --extra-cflags="-DANDROID -fPIC -ffunction-sections -funwind-tables -mtune=atom -mssse3 -mfpmath=sse -fomit-frame-pointer -fstrict-aliasing -funswitch-loops -finline-limit=300" --extra-ldflags="-nostdlib -lgcc -ldl -lz -lm -lc"
        make -j4
        cd ..
        patch -Np1 < ../../ffmpeg-$FFMPEG_VERSION-android.patch
        ./configure --prefix="$ANDROID_NDK/../local/" --enable-shared --enable-gpl --enable-version3 --enable-runtime-cpudetect --disable-outdev=sdl --enable-libx264 --extra-cflags="-I$X264" --extra-ldflags="-L$X264" --enable-cross-compile --cross-prefix="$ANDROID_BIN-" --sysroot="$ANDROID_ROOT" --target-os=linux --arch=atom --extra-cflags="-DANDROID -fPIC -ffunction-sections -funwind-tables -mssse3 -mfpmath=sse -fomit-frame-pointer -fstrict-aliasing -funswitch-loops -finline-limit=300" --extra-ldflags="-nostdlib" --extra-libs="-lgcc -ldl -lz -lm -lc" --disable-symver --disable-programs --libdir="$ANDROID_NDK/../local/lib/x86/" --shlibdir="$ANDROID_NDK/../local/lib/x86/"
        make -j4
        make install
        ;;
    linux-x86)
        cd $X264
        ./configure --enable-static --enable-pic --host=i686-linux
        make -j4
        cd ..
        ./configure --enable-shared --enable-gpl --enable-version3 --enable-runtime-cpudetect --disable-outdev=sdl --enable-libx264 --extra-cflags="-I$X264" --extra-ldflags="-L$X264" --cc="gcc -m32" --extra-ldflags="-ldl" --libdir=/usr/local/lib32/ --shlibdir=/usr/local/lib32/
        make -j4
        sudo make install
        ;;
    linux-x86_64)
        cd $X264
        ./configure --enable-static --enable-pic
        make -j4
        cd ..
        ./configure --enable-shared --enable-gpl --enable-version3 --enable-runtime-cpudetect --disable-outdev=sdl --enable-libx264 --extra-cflags="-I$X264" --extra-ldflags="-L$X264" --extra-ldflags="-ldl" --libdir=/usr/local/lib64/ --shlibdir=/usr/local/lib64/
        make -j4
        sudo make install
        ;;
    macosx-x86_64)
        cd $X264
        ./configure --enable-static --enable-pic
        make -j4
        cd ..
        ./configure --enable-shared --enable-gpl --enable-version3 --enable-runtime-cpudetect --disable-outdev=sdl --enable-libx264 --extra-cflags="-I$X264" --extra-ldflags="-L$X264" --extra-ldflags="-Wl,-headerpad_max_install_names -ldl"
        make -j4
        sudo make install
        BADPATH=/usr/local/lib
        LIBS="libavcodec.55.dylib libavdevice.55.dylib libavfilter.4.dylib libavformat.55.dylib libavutil.52.dylib libpostproc.52.dylib libswresample.0.dylib libswscale.2.dylib"
        for f in $LIBS; do sudo install_name_tool $BADPATH/$f -id @rpath/$f \
            -add_rpath /usr/local/lib/ -add_rpath /opt/local/lib/ -add_rpath @loader_path/. \
            -change $BADPATH/libavcodec.55.dylib @rpath/libavcodec.55.dylib \
            -change $BADPATH/libavdevice.55.dylib @rpath/libavdevice.55.dylib \
            -change $BADPATH/libavfilter.4.dylib @rpath/libavfilter.4.dylib \
            -change $BADPATH/libavformat.55.dylib @rpath/libavformat.55.dylib \
            -change $BADPATH/libavutil.52.dylib @rpath/libavutil.52.dylib \
            -change $BADPATH/libpostproc.52.dylib @rpath/libpostproc.52.dylib \
            -change $BADPATH/libswresample.0.dylib @rpath/libswresample.0.dylib \
            -change $BADPATH/libswscale.2.dylib @rpath/libswscale.2.dylib; done
        ;;
    windows-x86)
        7za x -y ffmpeg-$FFMPEG_VERSION-win32-dev.7z
        7za x -y ffmpeg-$FFMPEG_VERSION-win32-shared.7z
        patch -Np1 -d ffmpeg-$FFMPEG_VERSION-win32-dev/ < ../ffmpeg-$FFMPEG_VERSION-win32-dev.patch
        # http://ffmpeg.org/platform.html#Linking-to-FFmpeg-with-Microsoft-Visual-C_002b_002b
        LIBS=(avcodec-55 avdevice-55 avfilter-4 avformat-55 avutil-52 postproc-52 swresample-0 swscale-2)
        for LIB in ${LIBS[@]}; do
            lib /def:ffmpeg-$FFMPEG_VERSION-win32-dev/lib/$LIB.def /out:ffmpeg-$FFMPEG_VERSION-win32-dev/lib/$LIB.lib /machine:x86
        done
        rm -Rf ffmpeg-$FFMPEG_VERSION-win32-dev/lib32
        rm -Rf ffmpeg-$FFMPEG_VERSION-win32-shared/bin32
        mv ffmpeg-$FFMPEG_VERSION-win32-dev/lib ffmpeg-$FFMPEG_VERSION-win32-dev/lib32
        mv ffmpeg-$FFMPEG_VERSION-win32-shared/bin ffmpeg-$FFMPEG_VERSION-win32-shared/bin32
        cp -a ffmpeg-$FFMPEG_VERSION-win32-dev/* $INSTALL_DIR
        cp -a ffmpeg-$FFMPEG_VERSION-win32-shared/* $INSTALL_DIR
        ;;
    windows-x86_64)
        7za x -y ffmpeg-$FFMPEG_VERSION-win64-dev.7z
        7za x -y ffmpeg-$FFMPEG_VERSION-win64-shared.7z
        patch -Np1 -d ffmpeg-$FFMPEG_VERSION-win64-dev/ < ../ffmpeg-$FFMPEG_VERSION-win64-dev.patch
        # http://ffmpeg.org/platform.html#Linking-to-FFmpeg-with-Microsoft-Visual-C_002b_002b
        LIBS=(avcodec-55 avdevice-55 avfilter-4 avformat-55 avutil-52 postproc-52 swresample-0 swscale-2)
        for LIB in ${LIBS[@]}; do
            lib /def:ffmpeg-$FFMPEG_VERSION-win64-dev/lib/$LIB.def /out:ffmpeg-$FFMPEG_VERSION-win64-dev/lib/$LIB.lib /machine:x64
        done
        rm -Rf ffmpeg-$FFMPEG_VERSION-win64-dev/lib64
        rm -Rf ffmpeg-$FFMPEG_VERSION-win64-shared/bin64
        mv ffmpeg-$FFMPEG_VERSION-win64-dev/lib ffmpeg-$FFMPEG_VERSION-win64-dev/lib64
        mv ffmpeg-$FFMPEG_VERSION-win64-shared/bin ffmpeg-$FFMPEG_VERSION-win64-shared/bin64
        cp -a ffmpeg-$FFMPEG_VERSION-win64-dev/* $INSTALL_DIR
        cp -a ffmpeg-$FFMPEG_VERSION-win64-shared/* $INSTALL_DIR
        ;;
    *)
        echo "Error: Platform \"$PLATFORM\" is not supported"
        ;;
esac

if [[ $PLATFORM != windows* ]]; then
    cd ..
fi

and run

NDK_TOOLCHAIN=arm-linux-androideabi-4.6 ANDROID_NDK=/home/android-ndk-r9b/ bash cppbuild.sh -platform android-arm install ffmpeg

@saudet
Copy link
Member

saudet commented May 7, 2014

Looks great! Do you think you could do the same for the rest of the non-Windows platforms and send that over via a pull request? (Along with a one-liner in the CHANGELOG.md file with your name, if desired.) Thanks!

@ratmole
Copy link
Author

ratmole commented May 8, 2014

I will do that when i'm done finishing the implementation.
i still need a little help from you.

I'm trying to find a way to include the libspeex.so, libcrypto.so and libssl.so in the generated ffmpeg-android-arm.jar but i cannot find how.

can you provide a hint?

@saudet
Copy link
Member

saudet commented May 8, 2014

The easiest thing would be to compile them as static libraries, but if you really want to use shared libraries, we might as well make a completely separate artifact out of those. Which wouldn't be a bad thing when you think about it. Is this something you'd like to try? I have to write a tutorial about that anyway.

Or if not, are there any reasons to not use use static libraries?

@ratmole
Copy link
Author

ratmole commented May 8, 2014

trying to compile as static right now... i will let you know if everything went ok. thanks

@ratmole
Copy link
Author

ratmole commented May 9, 2014

Well i have some issues right know.
still building them as shared libraries, and i get

dlopen("/data/app-lib/xxxx/libjniavformat.so") failed: dlopen failed: cannot locate symbol "avio_find_protocol_name" referenced by "libjniavformat.so"...
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load gnustl_static from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/xxxx.apk"],nativeLibraryDirectories=[/data/app-lib/xxxx, /vendor/lib, /system/lib]]]: findLibrary returned null

edit: i get the same error also when building static libraries.

@saudet
Copy link
Member

saudet commented May 9, 2014

Are you saying that you get that error without your additions? Or does this only happen with your additions? If this happens only with your additions, could you please provide the modifications you made to the files? Thanks

@ratmole
Copy link
Author

ratmole commented May 10, 2014

after some failed attempts to get my code to work(without luck), i downloaded
JavaCV 0.8 binary package javacv-0.8-bin.zip from
http://search.maven.org/remotecontent?filepath=org/bytedeco/javacv/0.8/javacv-0.8-bin.zip

replaced all the .so files im my armeabi folder,
replaced artoolkitplus.jar, ffmpeg.jar, javacpp.jar, javacv.jar, opencv.jar from the downloaded file
and i get the same error:

dlopen("/data/app-lib/xxxx/libjniavformat.so") failed: dlopen failed: cannot locate symbol "avio_find_protocol_name" referenced by "libjniavformat.so"...

@saudet
Copy link
Member

saudet commented May 11, 2014

So, you are saying that you have issues with the binaries of release 0.8?
If so, could you provide the problematic sample code so I can reproduce the
problem here? Thanks!

@ratmole
Copy link
Author

ratmole commented May 12, 2014

Finally, it works. There is nothing wrong on your side, i had to System.loadLibrary my extra libraries.
on the 0.7 they are loading fine without the System.loadLibrary.
Weird.

So sorry for the trouble.
Right know my enhancement works, but you have to cp libcrypto.so,libssl.so and libspeex.so by hand in the armeabi-v7a folder and do:

System.loadLibrary("crypto");
System.loadLibrary("ssl");
System.loadLibrary("speex");
rec = new FFmpegFrameRecorder(rtmps_link,width,height,1);
...
rec.setFormat("flv");
rec.setVideoCodec(avcodec.AV_CODEC_ID_H264);
rec.setAudioCodec(avcodec.AV_CODEC_ID_SPEEX);
...

@ratmole ratmole closed this as completed May 12, 2014
@ratmole ratmole reopened this May 12, 2014
@saudet
Copy link
Member

saudet commented May 12, 2014

So, are you saying those cannot be linked statically?

@ratmole
Copy link
Author

ratmole commented May 12, 2014

Samuel, right know i cannot test it due time issues,have to finish some tests at work :)
I will try to build them as static libraries, and when succeed i will let you know.
Thanks again.

@saudet
Copy link
Member

saudet commented Jun 14, 2014

Be sure to let me know how that goes, thanks!

@lu-zero
Copy link
Contributor

lu-zero commented Jul 24, 2014

I just added speex support on my branch, probably even that can be ported back.

@saudet
Copy link
Member

saudet commented Jul 24, 2014

Great! Clean it up and send a PR, thanks!

saudet added a commit that referenced this issue Nov 16, 2014
…ommon RTMPS streams, among other things (issue #2)
@saudet
Copy link
Member

saudet commented Dec 27, 2014

@ratmole @lu-zero I've added those in version 0.10, thanks for helping out with this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants