media-kit icon indicating copy to clipboard operation
media-kit copied to clipboard

Missing libmpv.so.2 when packaging Flutter app with media-kit on Linux using Flatpak

Open AoEiuV020 opened this issue 1 year ago • 6 comments

Description:

Hello,

I am trying to package a Flutter application that uses the media-kit library on Linux using Flatpak. However, I am encountering an issue where the application fails to find libmpv.so.2 at runtime.

I am using Arch Linux, and the app generated by flutter build linux runs fine on my machine. However, I want to use a more compatible packaging solution.

The main issue is that I am not very familiar with Flatpak, and the script was generated by AI. However, without media-kit, the project works fine with external libraries like WebRTC. media-kit uses system dynamic libraries, so how should I package this on Linux for better compatibility? Alternatively, is there a way to modify media-kit dependencies to easily package them into the app?

flatpak build script,

#!/bin/sh
. "$(dirname $0)/env.sh"
example_path="$ROOT"/example

echo 检查依赖
check_dependency() {
    if ! command -v "$1" >/dev/null 2>&1; then
        echo "错误: 未找到 $1,请先安装。"
        echo "在 Arch Linux 上运行: sudo pacman -S $2"
        echo "然后重新运行此脚本"
        exit 1
    fi
}

check_flatpak_runtime() {
    if ! flatpak info org.freedesktop.Platform//23.08 >/dev/null 2>&1; then
        echo "错误: 未找到 Flatpak 运行时。"
        echo "请运行以下命令安装必要的运行时:"
        echo "sudo flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo"
        echo "sudo flatpak install flathub org.freedesktop.Platform//23.08 org.freedesktop.Sdk//23.08"
        echo "然后重新运行此脚本"
        exit 1
    fi
}

echo 检查必要的依赖
check_dependency "flatpak" "flatpak"
check_dependency "flatpak-builder" "flatpak-builder"
check_dependency "eu-strip" "elfutils"

echo 检查 Flatpak 运行时
check_flatpak_runtime

echo 设置变量
APP_NAME="Meeting"
OUTPUT_DIR="$example_path/build/output"
APPDIR="$example_path/build/linux/x64/release/bundle"
APP_ICON="$example_path/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_256.png"
FLATPAK_BUILD_DIR="$example_path/build/flatpak"

echo "删除构建目录"
rm -rf "$FLATPAK_BUILD_DIR"

echo 创建构建目录
FLATPAK_BUNDLE_DIR="$FLATPAK_BUILD_DIR/bundle"
mkdir -p "$FLATPAK_BUNDLE_DIR/app"
mkdir -p "$FLATPAK_BUNDLE_DIR/share/applications"
mkdir -p "$FLATPAK_BUNDLE_DIR/share/icons/hicolor/256x256/apps"

echo "复制App"
cp -r "$APPDIR"/* "$FLATPAK_BUNDLE_DIR/app/"

echo 创建 desktop 文件
cat > "$FLATPAK_BUNDLE_DIR/share/applications/com.chat.weichat.Meeting.desktop" << EOF
[Desktop Entry]
Name=$APP_NAME
Exec=meeting
Icon=com.chat.weichat.Meeting
Type=Application
Categories=Utility;
EOF

echo 复制图标
cp "$APP_ICON" "$FLATPAK_BUNDLE_DIR/share/icons/hicolor/256x256/apps/com.chat.weichat.Meeting.png"

echo 创建 manifest 文件
cat > "$FLATPAK_BUILD_DIR/com.chat.weichat.Meeting.yml" << EOF
app-id: com.chat.weichat.Meeting
runtime: org.freedesktop.Platform
runtime-version: '23.08'
sdk: org.freedesktop.Sdk
command: meeting
tags:
  - proprietary
finish-args:
  - --device=all
  - --share=ipc
  - --share=network
  - --socket=cups
  - --socket=pcsc # FIDO2
  - --socket=pulseaudio
  - --socket=x11
  - --socket=wayland
modules:
  - name: meeting
    buildsystem: simple
    build-commands:
      - mkdir -p /app/bin
      - cp -r app /app/app
      - ln -s /app/app/meeting_flutter_example /app/bin/meeting
      - cp -r share /app/
    sources:
      - type: dir
        path: bundle
EOF

# 构建 Flatpak
cd "$FLATPAK_BUILD_DIR"
flatpak-builder --force-clean --repo=repo build-dir com.chat.weichat.Meeting.yml
flatpak build-bundle repo meeting.flatpak com.chat.weichat.Meeting

mv meeting.flatpak "$OUTPUT_DIR/meeting.flatpak"
echo "Flatpak 构建完成!" 

echo "安装: flatpak install --user $OUTPUT_DIR/meeting.flatpak"
echo "运行: flatpak run com.chat.weichat.Meeting"

output:

meeting: error while loading shared libraries: libmpv.so.2: cannot open shared object file: No such file or directory

AoEiuV020 avatar Dec 06 '24 02:12 AoEiuV020

for my any device with the Android API 35 caused the issue, you will need to run your app on something below android API 35. Hopefully this can get fixed

Judimax avatar Dec 07 '24 00:12 Judimax

this might be helpful https://github.com/flet-dev/flet/discussions/2696

EricApostal avatar Dec 07 '24 06:12 EricApostal

@AoEiuV020

You should add the libmpv module to your flatpak.

 modules:
  - name: libmpv
    buildsystem: meson
    config-opts:
      - -Dlibmpv=true
      - -Dlua=disabled
      - -Ddebug=false
      - -Dbuild-date=false
      - -Dcplayer=false
      - -Dmanpage-build=disabled
      - -Dsdl2=enabled
      - -Dvulkan=enabled
    sources:
      - type: git
        url: https://github.com/mpv-player/mpv.git
        tag: v0.39.0
        commit: a0fba7be57f3822d967b04f0f6b6d6341e7516e7
        x-checker-data:
          type: git
          tag-pattern: ^v([\d.]+)$

This is just an example. In reality, you also need to add the dependencies required to compile libmpv, such as ffmpeg.

On the Linux platform, media-kit depends on the external libmpv, we need do that.

If you are interested in packaging libmpv into the build artifacts, you can check out my current media-kit branch. I recently completed this work, but it lacks sufficient testing. I am prepared to attempt submitting a PR after the tests pass without issues.

Predidit avatar Dec 07 '24 11:12 Predidit

Hello @Predidit @EricApostal - I am interested in this

Honestly it feels very bulky to have to package an entire media player into my app and all its dependencies. But setting that aside, any advice to package it properly?

I tried the above but I am getting:

C linker for the host machine: cc ld.bfd 2.44
Host machine cpu family: x86_64
Host machine cpu: x86_64
Program python3 found: YES (/usr/bin/python3)
Found pkg-config: YES (/usr/bin/pkg-config) 2.4.3
Run-time dependency libavcodec found: YES 61.3.100
Run-time dependency libavfilter found: YES 10.1.100
Run-time dependency libavformat found: YES 61.1.100
Run-time dependency libavutil found: YES 59.8.100
Run-time dependency libswresample found: YES 5.1.100
Run-time dependency libswscale found: YES 8.1.100
Found CMake: /usr/bin/cmake (3.31.8)
Run-time dependency libplacebo found: NO (tried pkgconfig and cmake)

../meson.build:29:13: ERROR: Dependency "libplacebo" not found, tried pkgconfig and cmake

I guess I'll have plenty of tinkering ahead!

saif-ellafi avatar Aug 01 '25 13:08 saif-ellafi

@saif-ellafi

As I mentioned earlier, resolving all dependencies for libmpv, including ffmpeg and libplacebo, is extremely complicated, and the resulting Flatpak manifest would be very lengthy. Therefore, in my branch, I've implemented an alternative solution: I store the binary libmpv in the application directory and dynamically link to it. Since this approach no longer depends on the system's libmpv, we don't need to worry about various Flatpak packaging complexities. If you're interested in this solution, you can use my branch directly. Regarding the previously mentioned approach of writing a lengthy Flatpak manifest, I haven't completed that method either.

Predidit avatar Aug 01 '25 13:08 Predidit

@Predidit

Interesting! However, I managed and seems to work like a charm (ok I only tried m4a codec playbacks!). Works without errors at least!! Phew, I was close to giving up!

  - name: glslang
    buildsystem: cmake-ninja
    config-opts:
      - -DCMAKE_BUILD_TYPE=Release
      - -DBUILD_EXTERNAL=OFF
      - -DENABLE_CTEST=OFF
      - -DENABLE_OPT=OFF
    sources:
      - type: git
        url: https://github.com/KhronosGroup/glslang.git
        tag: 15.4.0
        commit: 8a85691a0740d390761a1008b4696f57facd02c4
  - name: libplacebo
    buildsystem: meson
    config-opts:
      - -Ddemos=false
      - -Dtests=false
    sources:
      - type: git
        url: https://github.com/haasn/libplacebo.git
        tag: v7.351.0
        commit: 3188549fba13bbdf3a5a98de2a38c2e71f04e21e
  - name: fribidi
    buildsystem: meson
    config-opts:
      - -Ddocs=false
    sources:
      - type: git
        url: https://github.com/fribidi/fribidi.git
        tag: v1.0.16
        commit: 68162babff4f39c4e2dc164a5e825af93bda9983
  - name: libass
    buildsystem: autotools
    config-opts:
      - --disable-static
      - --disable-fontconfig
      - --disable-require-system-font-provider
    sources:
      - type: git
        url: https://github.com/libass/libass.git
        tag: 0.17.4
        commit: bbb3c7f1570a4a021e52683f3fbdf74fe492ae84
  - name: libmpv
    buildsystem: meson
    config-opts:
      - -Dlibmpv=true
      - -Dlua=disabled
      - -Ddebug=false
      - -Dbuild-date=false
      - -Dcplayer=false
      - -Dmanpage-build=disabled
      - -Dsdl2=enabled
      - -Dvulkan=enabled
    sources:
      - type: git
        url: https://github.com/mpv-player/mpv.git
        tag: v0.40.0
        commit: e48ac7ce08462f5e33af6ef9deeac6fa87eef01e
        x-checker-data:
          type: git
          tag-pattern: ^v([\d.]+)$

saif-ellafi avatar Aug 01 '25 14:08 saif-ellafi