Skip to content

Commit

Permalink
add threads support using wasi-sdk-20
Browse files Browse the repository at this point in the history
Signed-off-by: yanghaku <1961882079@qq.com>
  • Loading branch information
yanghaku committed Apr 3, 2023
1 parent 1bf38ea commit 7ed3085
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Introduction
# Use ffmpeg library in C/C++ and Rust and compile to Webassembly.

Use ffmpeg library in Webassembly.
## Introduction

There are 2 patches:

Expand All @@ -10,8 +10,8 @@ There are 2 patches:

The binary programs:

* [ ] ffmpeg
* [ ] ffplay
* [x] ffmpeg (wasm32-wasi-threads target only)
* [ ] ffplay (no sdl support now)
* [x] ffprobe

The static libraries:
Expand All @@ -24,13 +24,13 @@ The static libraries:
* [x] libavformat.a (with patch, cannot use dup)
* [x] libswresample.a

## build ffmpeg from source
### build ffmpeg from source

Requirement: wasi-sdk

The script [build_ffmpeg.sh](./build_ffmpeg.sh) can compile ffmpeg source to wasm32-wasi.

# C Examples
## C Examples

Requirements:
* wasi-sdk
Expand Down Expand Up @@ -61,9 +61,9 @@ The ffmpeg C examples which can be built:
* [x] transcode_aac


# Rust Examples
## Rust Examples

## Requirements:
### Requirements:

* wasi-sysroot
* libclang_rt.builtins-wasm32.a (because [rust compiler-rt missing some f128 functions](https://github.com/rust-lang/compiler-builtins#unimplemented-functions)
Expand All @@ -72,7 +72,7 @@ The ffmpeg C examples which can be built:

wasi-sysroot and libclang_rt.builtins-wasm32.a is bundled in wasi-sdk, or download these separately from [wasi-sdk release page](https://github.com/WebAssembly/wasi-sdk/releases)

## Build:
### Build:

1. Config the wasi-sysroot and ffmpeg path in ```rust-examples/.cargo/config.toml```

Expand Down Expand Up @@ -103,7 +103,13 @@ cd rust-examples
cargo build --release
```

# Test Data
## Test Data

download from: https://github.com/ffmpegwasm/testdata.git

## Reference

* https://github.com/yamt/FFmpeg-WASI

> When I searched how to use the new feature of wasi-sdk-20, I found this repo: https://github.com/yamt/FFmpeg-WASI, which has already compiled the ffmpeg to wasm32-wasi and support thread.
19 changes: 16 additions & 3 deletions build_ffmpeg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ FFMPEG_VERSION="release/6.0"


# options for ffmpeg
DISABLES="--disable-runtime-cpudetect --disable-autodetect --disable-doc --disable-pthreads --disable-network --disable-w32threads --disable-os2threads --disable-alsa --disable-appkit --disable-avfoundation --disable-bzlib --disable-coreimage --disable-iconv --disable-metal --disable-sndio --disable-schannel --disable-sdl2 --disable-securetransport --disable-vulkan --disable-xlib --disable-zlib --disable-amf --disable-audiotoolbox --disable-cuda-llvm --disable-cuvid --disable-d3d11va --disable-dxva2 --disable-ffnvcodec --disable-nvdec --disable-nvenc --disable-v4l2-m2m --disable-vaapi --disable-vdpau --disable-videotoolbox --disable-asm --disable-altivec --disable-vsx --disable-power8 --disable-amd3dnow --disable-amd3dnowext --disable-xop --disable-fma3 --disable-fma4 --disable-aesni --disable-armv5te --disable-armv6 --disable-armv6t2 --disable-vfp --disable-neon --disable-inline-asm --disable-x86asm --disable-mipsdsp --disable-mipsdspr2 --disable-msa --disable-mipsfpu --disable-mmi --disable-lsx --disable-lasx --disable-rvv --disable-debug"
DISABLES="--disable-runtime-cpudetect --disable-autodetect --disable-doc --disable-network --disable-w32threads --disable-os2threads --disable-alsa --disable-appkit --disable-avfoundation --disable-bzlib --disable-coreimage --disable-iconv --disable-metal --disable-sndio --disable-schannel --disable-sdl2 --disable-securetransport --disable-vulkan --disable-xlib --disable-zlib --disable-amf --disable-audiotoolbox --disable-cuda-llvm --disable-cuvid --disable-d3d11va --disable-dxva2 --disable-ffnvcodec --disable-nvdec --disable-nvenc --disable-v4l2-m2m --disable-vaapi --disable-vdpau --disable-videotoolbox --disable-asm --disable-altivec --disable-vsx --disable-power8 --disable-amd3dnow --disable-amd3dnowext --disable-xop --disable-fma3 --disable-fma4 --disable-aesni --disable-armv5te --disable-armv6 --disable-armv6t2 --disable-vfp --disable-neon --disable-inline-asm --disable-x86asm --disable-mipsdsp --disable-mipsdspr2 --disable-msa --disable-mipsfpu --disable-mmi --disable-lsx --disable-lasx --disable-rvv --disable-debug"

OS=android # can not use native os
ARCH=wasm32
Expand All @@ -25,8 +25,21 @@ STRIP="${WASI_SDK}/bin/llvm-strip"
CC="${WASI_SDK}/bin/clang"
CXX="${WASI_SDK}/bin/clang++"
LD="${WASI_SDK}/bin/clang"
EXTRA_C_FLAGS='-DWASM32_WASI -D_WASI_EMULATED_PROCESS_CLOCKS' # used for patch
EXTRA_LD_FLAGS='-lwasi-emulated-process-clocks'

# detect pthread support
VERSION=$(${CC} --version | head -n 1 | grep -oE "[0-9][0-9].")
# pthread since wasi-sdk-20 (llvm-16.0)
if [[ "${VERSION}" == "16." ]]; then
echo "Build with pthread support!"
EXTRA_C_FLAGS='-DWASM32_WASI -D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL --target=wasm32-wasi-threads'
EXTRA_LD_FLAGS='-lwasi-emulated-process-clocks -lwasi-emulated-signal --target=wasm32-wasi-threads -Wl,--import-memory,--export-memory,--max-memory=2147483648'
DISABLES="${DISABLES} --enable-pthreads"
else
EXTRA_C_FLAGS='-DWASM32_WASI -D_WASI_EMULATED_PROCESS_CLOCKS'
EXTRA_LD_FLAGS='-lwasi-emulated-process-clocks'
DISABLES="${DISABLES} --disable-pthreads"
fi


TOOLCHAIN="--enable-cross-compile --arch=${ARCH} --target-os=${OS} --nm=${NM} --ar=${AR} --as=${AS} --strip=${STRIP} --cc=${CC} --cxx=${CXX} --objcc=${CC} --dep-cc=${CC} --ld=${LD} --ranlib=${RANLIB} --enable-pic --enable-lto"

Expand Down

0 comments on commit 7ed3085

Please sign in to comment.