QtMultimedia on Android: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Created page with "muu")
 
No edit summary
Line 1: Line 1:
muu
In this page, we are going to show the main components of QtMultimedia on Android.
 
FFmpeg was in introduced on QtMultimedia for Android starting from 6.7. It is imperatively recommended to be used because the MediaCodec-based backend is deprecated as Qt 6.8 and will be removed in Qt 7.0.
 
Let's start by showing how to build QtMultimedia with FFmpeg.
 
== Build FFmpeg for Android from source ==
 
* We need to clone the official FFmpeg repository
<syntaxhighlight>
git clone git@source.ffmpeg.org:ffmpeg
</syntaxhighlight>
 
* Copy the script below into a file (for example '''build-ffmpeg-android.sh'''). We will be using it to build FFmpeg for Android
<syntaxhighlight lang="sh">
#!/bin/bash
 
helpFunction() {
    echo "Usage: ./build-ffmpeg-android.sh --arch=ARCH --install-path=PATH"
    echo -e "\t--arch            Select architecture [aarch64, armv7, x86, x86_64]"
    echo -e "\t--install-path    Install in PATH used in make install, full path please"
    exit 1
}
 
# Parse command-line options
for opt in "$@"; do
    case "$opt" in
        --arch=*)
            ARCH="${opt#*=}"
        ;;
        --install-path=*)
            INSTALL_PATH="${opt#*=}"
        ;;
        *)
            helpFunction
        ;;
    esac
done
 
# Check for empty parameters
if [ -z "$ARCH" ] || [ -z "$INSTALL_PATH" ]; then
    echo "Some or all of the parameters are empty"
    helpFunction
fi
 
# Check for required environment variables
if [ -z "$ANDROID_SDK_ROOT" ] || [ -z "$ANDROID_NDK_ROOT" ] || [ -z "$HOST_ARCH" ] || [ -z "$FFMPEG_LIBRARY_PATH" ]; then
    echo "Please set ANDROID_SDK_ROOT, ANDROID_NDK_ROOT, HOST_ARCH, and FFMPEG_LIBRARY_PATH."
    exit 1
fi
 
# Setup architecture-specific variables
case "$ARCH" in
    aarch64)
        TOOLCHAIN_ARCH="aarch64-linux-android"
        CPU="armv8-a"
    ;;
    armv7)
        TOOLCHAIN_ARCH="armv7a-linux-androideabi"
        CPU="armv7-a"
    ;;
    x86)
        TOOLCHAIN_ARCH="i686-linux-android"
        CPU="i686"
    ;;
    x86_64)
        TOOLCHAIN_ARCH="x86_64-linux-android"
        CPU="x86_64"
    ;;
    *)
        echo "Invalid architecture: $ARCH"
        helpFunction
    ;;
esac
 
ANDROID_OPTIONS="
--target-os=android
--arch=${ARCH}
--cpu=${CPU}
--enable-cross-compile
--enable-jni
--enable-mediacodec
--enable-pthreads
--disable-indev=android_camera
--sysroot=${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_ARCH}/sysroot
--sysinclude=${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_ARCH}/sysroot/usr/include/
--cc=${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_ARCH}/bin/${TOOLCHAIN_ARCH}24-clang
--cxx=${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_ARCH}/bin/${TOOLCHAIN_ARCH}24-clang++
--disable-autodetect
--enable-neon
--disable-asm
"
 
export LDFLAGS="-L${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_ARCH}/sysroot/usr/lib/${TOOLCHAIN_ARCH} -L${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_ARCH}/sysroot/usr/lib/${TOOLCHAIN_ARCH}/24 -lm -ldl -llog"
export CFLAGS="-O3 -Wl,exclude-libs,libgcc.a,libunwind.a"
 
COMMON_OPTIONS="
--prefix=${INSTALL_PATH}
--enable-static
--disable-shared
--host-os=${HOST_ARCH}
--install-name-dir=${INSTALL_PATH}
--disable-logging
--disable-debug
--disable-programs
--enable-pic
"
 
pushd ${FFMPEG_LIBRARY_PATH}
make clean
make distclean
 
echo "./configure ${COMMON_OPTIONS} ${ANDROID_OPTIONS}"
./configure ${COMMON_OPTIONS} ${ANDROID_OPTIONS}
 
sed -i "s/-fPIE/-fPIC/" ffbuild/config.mak
sed -i "s/-pie/-pic/" ffbuild/config.mak
 
make -j8 install
popd
 
</syntaxhighlight>
 
* Set the environment variables as they are needed to build ffmpeg
<syntaxhighlight lang="shell">
export ANDROID_SDK_ROOT=android_sdk_location (i.e ~/Library/Android/sdk)
export ANDROID_NDK_ROOT=android_ndk_location (i.e ~/Library/Android/sdk/ndk/25.1.8937393)
export HOST_ARCH="darwin-x86_64"  // or "linux-x86_64"
export FFMPEG_LIBRARY_PATH=cloned_ffmpeg_location (i.e ~/Workspace/Ffmpeg/ffmpeg-android/ffmpeg)
</syntaxhighlight>
 
* Run '''build-ffmpeg-android.sh'''
<syntaxhighlight lang="shell">
sh build-ffmpeg-android.sh --arch=aarch64 --install-path=desired_install_path (i.e ../build/arm64-v8a)
</syntaxhighlight>After the lib files are generated.  Then configure Qt with the flags below<syntaxhighlight lang="shell">
-- -D OPENSSL_INCLUDE_DIR=openssl_include_folder (i.e /opt/homebrew/opt/openssl/include) -D FFMPEG_DIR:PATH=desired_install_path (i.e ~/Workspace/Ffmpeg/ffmpeg-android/build/arm64-v8a)
 
</syntaxhighlight>Check how to build [https://doc-snapshots.qt.io/qt6-dev/android-building.html#building Qt from source for Android].
 
For more details check [https://intranet.qt.io/pages/viewpage.action?spaceKey=QTRD&title=FFmpeg+lib+Qt+for+Android+Multimedia FFmpeg for Android].

Revision as of 11:37, 21 May 2024

In this page, we are going to show the main components of QtMultimedia on Android.

FFmpeg was in introduced on QtMultimedia for Android starting from 6.7. It is imperatively recommended to be used because the MediaCodec-based backend is deprecated as Qt 6.8 and will be removed in Qt 7.0.

Let's start by showing how to build QtMultimedia with FFmpeg.

Build FFmpeg for Android from source

  • We need to clone the official FFmpeg repository
git clone git@source.ffmpeg.org:ffmpeg
  • Copy the script below into a file (for example build-ffmpeg-android.sh). We will be using it to build FFmpeg for Android
#!/bin/bash

helpFunction() {
    echo "Usage: ./build-ffmpeg-android.sh --arch=ARCH --install-path=PATH"
    echo -e "\t--arch            Select architecture [aarch64, armv7, x86, x86_64]"
    echo -e "\t--install-path    Install in PATH used in make install, full path please"
    exit 1
}

# Parse command-line options
for opt in "$@"; do
    case "$opt" in
        --arch=*)
            ARCH="${opt#*=}"
        ;;
        --install-path=*)
            INSTALL_PATH="${opt#*=}"
        ;;
        *)
            helpFunction
        ;;
    esac
done

# Check for empty parameters
if [ -z "$ARCH" ] || [ -z "$INSTALL_PATH" ]; then
    echo "Some or all of the parameters are empty"
    helpFunction
fi

# Check for required environment variables
if [ -z "$ANDROID_SDK_ROOT" ] || [ -z "$ANDROID_NDK_ROOT" ] || [ -z "$HOST_ARCH" ] || [ -z "$FFMPEG_LIBRARY_PATH" ]; then
    echo "Please set ANDROID_SDK_ROOT, ANDROID_NDK_ROOT, HOST_ARCH, and FFMPEG_LIBRARY_PATH."
    exit 1
fi

# Setup architecture-specific variables
case "$ARCH" in
    aarch64)
        TOOLCHAIN_ARCH="aarch64-linux-android"
        CPU="armv8-a"
    ;;
    armv7)
        TOOLCHAIN_ARCH="armv7a-linux-androideabi"
        CPU="armv7-a"
    ;;
    x86)
        TOOLCHAIN_ARCH="i686-linux-android"
        CPU="i686"
    ;;
    x86_64)
        TOOLCHAIN_ARCH="x86_64-linux-android"
        CPU="x86_64"
    ;;
    *)
        echo "Invalid architecture: $ARCH"
        helpFunction
    ;;
esac

ANDROID_OPTIONS="
--target-os=android
--arch=${ARCH}
--cpu=${CPU}
--enable-cross-compile
--enable-jni
--enable-mediacodec
--enable-pthreads
--disable-indev=android_camera
--sysroot=${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_ARCH}/sysroot
--sysinclude=${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_ARCH}/sysroot/usr/include/
--cc=${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_ARCH}/bin/${TOOLCHAIN_ARCH}24-clang
--cxx=${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_ARCH}/bin/${TOOLCHAIN_ARCH}24-clang++
--disable-autodetect
--enable-neon
--disable-asm
"

export LDFLAGS="-L${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_ARCH}/sysroot/usr/lib/${TOOLCHAIN_ARCH} -L${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_ARCH}/sysroot/usr/lib/${TOOLCHAIN_ARCH}/24 -lm -ldl -llog"
export CFLAGS="-O3 -Wl,exclude-libs,libgcc.a,libunwind.a"

COMMON_OPTIONS="
--prefix=${INSTALL_PATH}
--enable-static
--disable-shared
--host-os=${HOST_ARCH}
--install-name-dir=${INSTALL_PATH}
--disable-logging
--disable-debug
--disable-programs
--enable-pic
"

pushd ${FFMPEG_LIBRARY_PATH}
make clean
make distclean

echo "./configure ${COMMON_OPTIONS} ${ANDROID_OPTIONS}"
./configure ${COMMON_OPTIONS} ${ANDROID_OPTIONS}

sed -i "s/-fPIE/-fPIC/" ffbuild/config.mak
sed -i "s/-pie/-pic/" ffbuild/config.mak

make -j8 install
popd
  • Set the environment variables as they are needed to build ffmpeg
export ANDROID_SDK_ROOT=android_sdk_location (i.e ~/Library/Android/sdk)
export ANDROID_NDK_ROOT=android_ndk_location (i.e ~/Library/Android/sdk/ndk/25.1.8937393)
export HOST_ARCH="darwin-x86_64"   // or "linux-x86_64"
export FFMPEG_LIBRARY_PATH=cloned_ffmpeg_location (i.e ~/Workspace/Ffmpeg/ffmpeg-android/ffmpeg)
  • Run build-ffmpeg-android.sh
sh build-ffmpeg-android.sh --arch=aarch64 --install-path=desired_install_path (i.e ../build/arm64-v8a)

After the lib files are generated. Then configure Qt with the flags below

-- -D OPENSSL_INCLUDE_DIR=openssl_include_folder (i.e /opt/homebrew/opt/openssl/include) -D FFMPEG_DIR:PATH=desired_install_path (i.e ~/Workspace/Ffmpeg/ffmpeg-android/build/arm64-v8a)

Check how to build Qt from source for Android.

For more details check FFmpeg for Android.