Skip to content

Commit 15066cd

Browse files
committed
feat(🎬): react-native-webcodecs, WebCodecs on VideoToolbox and MediaCodec
WebCodecs (VideoEncoder, VideoDecoder, AudioEncoder, AudioDecoder, VideoFrame, AudioData, the chunks) for React Native on VideoToolbox and AudioToolbox (iOS, Swift) and MediaCodec (Android, C++), as a Nitro module written to sit under Mediabunny and next to react-native-webgpu. On the web, the package exports the browser's own classes. - H.264 and AAC on both platforms, HEVC on iOS, AV1 encoding on Android. - HDR export (HLG in BT.2020) from half-float surfaces: HEVC Main 10 on iOS, AV1 Main 10 on Android. - example/: a react-native-test-app app that exports a 1080x1920 Lottie card drawn by Skia through WebGPU, with sound, in SDR or HDR, on iOS, Android, an Apple silicon Mac, and as a one-page demo on the web. It also compresses a picked video (scaled on the GPU, re-encoded, sound copied), with its own small picker so it needs no picker library. - src/__tests__: 18 e2e tests (a WebSocket server the example app connects to), passing on the iOS Simulator and an Android emulator (API 35), and unit tests for the WritableStream and Mediabunny writing through it. - react-native-webgpu and react-native-skia are patched (.yarn/patches) with changes pending upstream.
1 parent 3d4cc57 commit 15066cd

222 files changed

Lines changed: 40524 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.editorconfig‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
[*]
8+
9+
indent_style = space
10+
indent_size = 2
11+
12+
end_of_line = lf
13+
charset = utf-8
14+
trim_trailing_whitespace = true
15+
insert_final_newline = true

‎.gitattributes‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.pbxproj -text
2+
# specific for windows script files
3+
*.bat text eol=crlf
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Setup
2+
description: Setup Node.js and install dependencies
3+
4+
inputs:
5+
github_token:
6+
description: 'GitHub Token'
7+
required: true
8+
9+
runs:
10+
using: composite
11+
steps:
12+
- name: Setup Node.js
13+
uses: actions/setup-node@v4
14+
with:
15+
node-version-file: .nvmrc
16+
17+
- name: Install dependencies
18+
run: yarn install --immutable
19+
shell: bash
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Cancel PR workflows
2+
3+
# When a pull request is closed or merged, cancel every workflow run that is
4+
# still queued or running for its head branch, so runners are freed instead of
5+
# finishing work nobody will look at. A new push already does this through the
6+
# concurrency groups of the individual workflows; this covers close and merge.
7+
#
8+
# pull_request_target is used instead of pull_request so the job also has a
9+
# token that can cancel runs when the pull request comes from a fork. The job
10+
# never checks out or runs code from the pull request, so this is safe.
11+
on:
12+
pull_request_target:
13+
types:
14+
- closed
15+
16+
permissions:
17+
actions: write
18+
19+
jobs:
20+
cancel:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- name: Cancel queued and running workflow runs for the pull request
24+
env:
25+
GH_TOKEN: ${{ github.token }}
26+
REPO: ${{ github.repository }}
27+
PR_NUMBER: ${{ github.event.pull_request.number }}
28+
HEAD_REF: ${{ github.event.pull_request.head.ref }}
29+
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
30+
HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
31+
OWN_RUN_ID: ${{ github.run_id }}
32+
run: |
33+
set -euo pipefail
34+
echo "Pull request #$PR_NUMBER ($HEAD_REPO@$HEAD_REF, $HEAD_SHA)"
35+
runs=$(mktemp)
36+
for status in queued in_progress waiting pending requested; do
37+
gh api -X GET "repos/$REPO/actions/runs" \
38+
-f branch="$HEAD_REF" -f status="$status" -F per_page=100 \
39+
| jq -r \
40+
--arg head_repo "$HEAD_REPO" \
41+
--arg head_sha "$HEAD_SHA" \
42+
--argjson own "$OWN_RUN_ID" \
43+
'.workflow_runs[]
44+
| select(.id != $own)
45+
| select((.head_repository.full_name // "") == $head_repo
46+
or .head_sha == $head_sha)
47+
| "\(.id) \(.name)"' >> "$runs"
48+
done
49+
if [ ! -s "$runs" ]; then
50+
echo "No active runs to cancel."
51+
exit 0
52+
fi
53+
sort -u "$runs" | while read -r id name; do
54+
echo "Cancelling run $id ($name)"
55+
gh api -X POST "repos/$REPO/actions/runs/$id/cancel" >/dev/null \
56+
|| echo "::warning::Could not cancel run $id ($name)"
57+
done

‎.github/workflows/ci.yml‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
workflow_dispatch:
10+
11+
concurrency:
12+
group: ci-${{ github.event.pull_request.number || github.ref }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
check:
17+
runs-on: ubuntu-latest
18+
timeout-minutes: 15
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v4
22+
23+
- name: Setup
24+
uses: ./.github/actions/setup
25+
with:
26+
github_token: ${{ secrets.GITHUB_TOKEN }}
27+
28+
- name: Lint files
29+
run: yarn lint
30+
31+
- name: Typecheck files
32+
run: yarn tsc
33+
34+
- name: Unit tests
35+
run: yarn test:unit
36+
37+
- name: Build the package
38+
run: yarn build
39+
40+
# The generated Nitro sources are committed: fail when they are stale.
41+
- name: Check Nitro specs
42+
run: yarn specs && git diff --exit-code -- nitrogen
43+
44+
# Validate the semantic-release config without publishing, so a broken
45+
# .releaserc is caught on PRs rather than at release time.
46+
- name: Release dry run
47+
run: yarn semantic-release --dry-run
48+
env:
49+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50+
YARN_NPM_AUTH_TOKEN: ''
51+
NPM_TOKEN: ''
52+
53+
# Builds the example, runs it on a simulator and runs the e2e tests against
54+
# it (src/__tests__): exports checked with ffprobe and
55+
# ffmpeg on the host.
56+
ios:
57+
runs-on: macos-latest
58+
timeout-minutes: 60
59+
env:
60+
METRO_PORT: 8081
61+
E2E_PORT: 4242
62+
IOS_SIMULATOR: webcodecs-ci
63+
steps:
64+
- name: Checkout
65+
uses: actions/checkout@v4
66+
67+
- name: Setup
68+
uses: ./.github/actions/setup
69+
with:
70+
github_token: ${{ secrets.GITHUB_TOKEN }}
71+
72+
- name: Install ffmpeg
73+
run: command -v ffprobe >/dev/null || brew install ffmpeg
74+
75+
# xcpretty is a gem, not part of the macOS runner image.
76+
- name: Install xcpretty
77+
run: command -v xcpretty >/dev/null || sudo gem install xcpretty --no-document
78+
79+
- name: Install CocoaPods
80+
working-directory: example/ios
81+
run: pod install
82+
83+
- name: Start Package Manager
84+
working-directory: example
85+
run: CI=true yarn start --port $METRO_PORT --reset-cache &
86+
87+
- name: Build example for iOS
88+
working-directory: example/ios
89+
run: |
90+
set -o pipefail && xcodebuild \
91+
-workspace Example.xcworkspace \
92+
-scheme Example \
93+
-configuration Debug \
94+
-sdk iphonesimulator \
95+
-destination "generic/platform=iOS Simulator" \
96+
-derivedDataPath build \
97+
COMPILER_INDEX_STORE_ENABLE=NO \
98+
build | xcpretty
99+
100+
- name: Prepare iOS simulator
101+
run: |
102+
RUNTIME=$(xcrun simctl list runtimes available --json | jq -r '
103+
.runtimes | map(select(.platform == "iOS"))
104+
| sort_by(.version | split(".") | map(tonumber)) | last | .identifier')
105+
DEVICE_TYPE=$(xcrun simctl list devicetypes --json | jq -r '
106+
.devicetypes | map(select(.name | test("^iPhone [0-9]+ Pro$")))
107+
| sort_by(.name | capture("^iPhone (?<n>[0-9]+) Pro$").n | tonumber) | last | .identifier')
108+
UDID=$(xcrun simctl create "$IOS_SIMULATOR" "$DEVICE_TYPE" "$RUNTIME")
109+
echo "IOS_SIMULATOR_UDID=$UDID" >> $GITHUB_ENV
110+
echo "SIMULATOR_UDID=$UDID" >> $GITHUB_ENV
111+
xcrun simctl boot "$UDID"
112+
xcrun simctl bootstatus "$UDID" -b
113+
114+
- name: Install and launch app on Simulator
115+
run: |
116+
xcrun simctl install "$IOS_SIMULATOR_UDID" example/ios/build/Build/Products/Debug-iphonesimulator/ReactTestApp.app
117+
xcrun simctl launch "$IOS_SIMULATOR_UDID" com.reactnativewebcodecs.example \
118+
-RCT_jsLocation "localhost:$METRO_PORT"
119+
120+
- name: Run e2e tests
121+
run: yarn test
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Deploy the web demo
2+
on:
3+
push:
4+
branches:
5+
- main
6+
workflow_dispatch:
7+
8+
# One deployment at a time; a newer push replaces a queued one.
9+
concurrency:
10+
group: pages
11+
cancel-in-progress: false
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
timeout-minutes: 15
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
21+
- name: Setup
22+
uses: ./.github/actions/setup
23+
with:
24+
github_token: ${{ secrets.GITHUB_TOKEN }}
25+
26+
# The example app's web page as static files (example/scripts/build-web.js).
27+
- name: Build the demo
28+
run: yarn example build:web
29+
30+
- name: Upload
31+
uses: actions/upload-pages-artifact@v3
32+
with:
33+
path: example/web-build
34+
35+
deploy:
36+
needs: build
37+
runs-on: ubuntu-latest
38+
permissions:
39+
pages: write
40+
id-token: write
41+
environment:
42+
name: github-pages
43+
url: ${{ steps.deployment.outputs.page_url }}
44+
steps:
45+
- name: Deploy to GitHub Pages
46+
id: deployment
47+
uses: actions/deploy-pages@v4
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Create a release
2+
on: workflow_dispatch
3+
4+
concurrency:
5+
group: release-${{ github.ref }}
6+
cancel-in-progress: false
7+
8+
jobs:
9+
release:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write # to push tags and the CHANGELOG commit, and create the GitHub release
13+
issues: write # to comment on released issues
14+
pull-requests: write # to comment on released pull requests
15+
id-token: write # to mint the OIDC token for npm provenance / trusted publishing
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0 # semantic-release needs full history and tags
21+
22+
- name: Setup
23+
uses: ./.github/actions/setup
24+
with:
25+
github_token: ${{ secrets.GITHUB_TOKEN }}
26+
27+
- name: Use Node 24 (npm 12 needs it)
28+
uses: actions/setup-node@v4
29+
with:
30+
node-version: "24"
31+
registry-url: "https://registry.npmjs.org"
32+
33+
- name: Build the package
34+
run: yarn build
35+
36+
# Releases are staged: the workflow's trusted publisher may only run
37+
# `npm stage publish` (npm 12), and each version waits on npmjs.com
38+
# until a maintainer approves it with 2FA (`npm stage approve`).
39+
- name: Release
40+
run: yarn release
41+
env:
42+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
# Trusted publishing authenticates via the OIDC id-token, so no npm
44+
# token is needed. These are set empty so semantic-release-yarn's
45+
# verifyConditions step does not trip on a missing token.
46+
YARN_NPM_AUTH_TOKEN: ''
47+
NPM_TOKEN: ''
48+
NPM_CONFIG_PROVENANCE: true

‎.gitignore‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Yarn
2+
.yarn/*
3+
!.yarn/patches
4+
!.yarn/plugins
5+
!.yarn/releases
6+
!.yarn/sdks
7+
!.yarn/versions
8+
9+
10+
# OSX
11+
#
12+
.DS_Store
13+
14+
# node.js
15+
#
16+
node_modules/
17+
npm-debug.log
18+
yarn-error.log
19+
.eslintcache
20+
21+
# Xcode
22+
#
23+
build
24+
*.pbxuser
25+
!default.pbxuser
26+
*.mode1v3
27+
!default.mode1v3
28+
*.mode2v3
29+
!default.mode2v3
30+
*.perspectivev3
31+
!default.perspectivev3
32+
xcuserdata
33+
*.xccheckout
34+
*.moved-aside
35+
DerivedData
36+
*.hmap
37+
*.ipa
38+
*.xcuserstate
39+
project.xcworkspace
40+
41+
# Android/IntelliJ
42+
build
43+
.idea
44+
.gradle
45+
local.properties
46+
*.iml
47+
.cxx
48+
# Legacy Eclipse Settings
49+
.settings/
50+
51+
# BUCK
52+
buck-out/
53+
\.buckd/
54+
*.keystore
55+
56+
# Build
57+
dist
58+
.vscode
59+
*.tgz
60+
61+
# Cocoapods
62+
#
63+
example/**/Pods
64+
# Ruby
65+
vendor/
66+
67+
# Swift Package Manager
68+
.swiftpm/
69+
Package.resolved
70+
71+
.xcode.env.local
72+
73+
# Expo
74+
.expo/
75+
76+
# generated by bob
77+
/lib/
78+
79+
# Dawn xcframework copied in from react-native-webgpu-dawn at pod install
80+
libs/
81+
web-build/

0 commit comments

Comments
 (0)