Commit 42978d29 authored by hewenhao's avatar hewenhao

feat(harmony): add native FFmpeg support

parent 02483457
...@@ -191,6 +191,22 @@ same source code but is built with different settings (Architectures, API Level, ...@@ -191,6 +191,22 @@ same source code but is built with different settings (Architectures, API Level,
[LTS Releases](https://github.com/arthenica/ffmpeg-kit/wiki/LTS-Releases) wiki page to see how they differ from each [LTS Releases](https://github.com/arthenica/ffmpeg-kit/wiki/LTS-Releases) wiki page to see how they differ from each
other. other.
#### 2.5 HarmonyOS (RNOH)
HarmonyOS uses the same `FFmpegKit`, `FFprobeKit`, `ReturnCode`, and session APIs.
Use `FFmpegKit.isSupported()` to check whether the host has linked the module.
Package `harmony/ffmpeg_kit` as a source HAR and link its ETS `FFmpegKitPackage`
and C++ `FFmpegKitPackage`. The Harmony CMake build uses the
host's OpenHarmony native SDK to build the arm64 FFmpeg and x264 libraries from
checksum-pinned sources. The first build needs `curl`, `tar`, `make`, and
`pkg-config`. Set `FFMPEG_KIT_HARMONY_CACHE_DIR` to reuse completed native builds
across CI jobs.
The bridge is based on [the RNOH FFmpegKit port](https://github.com/react-native-oh-library/ffmpeg-kit).
This build includes built-in FFmpeg codecs and libx264. It uses the GPL build
of FFmpeg, and the native build cache contains the dependency license files.
### 3. Using ### 3. Using
1. Execute FFmpeg commands. 1. Execute FFmpeg commands.
......
/**
* Use these variables when you tailor your ArkTS code. They must be of the const type.
*/
export const HAR_VERSION = '6.0.2-0.0.2-rc.0';
export const BUILD_MODE_NAME = 'debug';
export const DEBUG = true;
export const TARGET_NAME = 'default';
/**
* BuildProfile Class is used only for compatibility purposes.
*/
export default class BuildProfile {
static readonly HAR_VERSION = HAR_VERSION;
static readonly BUILD_MODE_NAME = BUILD_MODE_NAME;
static readonly DEBUG = DEBUG;
static readonly TARGET_NAME = TARGET_NAME;
}
{
"apiType": 'stageMode',
"targets": [
{
"name": "default",
"runtimeOS": "HarmonyOS"
}
]
}
\ No newline at end of file
// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently.
export { harTasks } from '@ohos/hvigor-ohos-plugin';
/**
* GUN License
*
* Copyright (C) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export * from "./ts"
{
"name": "@geek-front/ffmpeg-react-native",
"version": "6.0.6",
"main": "index.ets",
"dependencies": {
"@rnoh/react-native-openharmony": "file:../react_native_openharmony"
}
}
{
"name": "ffmpeg_kit",
"version": "1.0.0",
"private": true,
"files": [
"BuildProfile.ets",
"build-profile.json5",
"hvigorfile.ts",
"index.ets",
"oh-package.json5",
"src",
"ts.ts"
]
}
cmake_minimum_required(VERSION 3.16)
project(rnoh_ffmpeg_kit LANGUAGES CXX)
execute_process(
COMMAND bash "${CMAKE_CURRENT_SOURCE_DIR}/build-native.sh" "${CMAKE_SYSROOT}" "${OHOS_ARCH}"
OUTPUT_VARIABLE FFMPEG_BUILD
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE FFMPEG_BUILD_RESULT
)
if(NOT FFMPEG_BUILD_RESULT EQUAL 0)
message(FATAL_ERROR "Building the Harmony FFmpegKit libraries failed")
endif()
add_library(rnoh_ffmpeg_kit SHARED
RNFFmpegKitModule.cpp
RnohReactNativeHarmonyFFmpegKitPackage.cpp
)
target_compile_features(rnoh_ffmpeg_kit PRIVATE cxx_std_17)
target_compile_options(rnoh_ffmpeg_kit PRIVATE "-iquote${FFMPEG_BUILD}/include/ffmpegkit")
target_include_directories(rnoh_ffmpeg_kit
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}"
PRIVATE "${FFMPEG_BUILD}/include" "${FFMPEG_BUILD}/prefix/include"
)
target_link_options(rnoh_ffmpeg_kit PRIVATE "-Wl,--exclude-libs,ALL")
target_link_libraries(rnoh_ffmpeg_kit PRIVATE
-Wl,--start-group
"${FFMPEG_BUILD}/prefix/lib/libffmpegkit.a"
"${FFMPEG_BUILD}/prefix/lib/libavdevice.a"
"${FFMPEG_BUILD}/prefix/lib/libavformat.a"
"${FFMPEG_BUILD}/prefix/lib/libavfilter.a"
"${FFMPEG_BUILD}/prefix/lib/libavcodec.a"
"${FFMPEG_BUILD}/prefix/lib/libavutil.a"
"${FFMPEG_BUILD}/prefix/lib/libswscale.a"
"${FFMPEG_BUILD}/prefix/lib/libswresample.a"
"${FFMPEG_BUILD}/prefix/lib/libpostproc.a"
"${FFMPEG_BUILD}/prefix/lib/libx264.a"
-Wl,--end-group
rnoh
)
/**
* GUN License
*
* Copyright (C) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include "RnohReactNativeHarmonyFFmpegKitPackage.h"
namespace rnoh {
class FFmpegKitPackage : public RnohReactNativeHarmonyFFmpegKitPackage {
using Super = RnohReactNativeHarmonyFFmpegKitPackage;
using Super::Super;
};
} // namespace rnoh
This diff is collapsed.
/**
* GUN License
*
* Copyright (C) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include "RNOH/ArkTSTurboModule.h"
#include "FFmpegSession.h"
#include "Session.h"
#include "Statistics.h"
#include "rapidjson/document.h"
namespace rnoh {
class RNFFmpegKitModule : public ArkTSTurboModule,
public std::enable_shared_from_this<RNFFmpegKitModule> {
public:
bool logsEnabled = false;
bool statisticsEnabled = false;
Context getContext()
{
return m_ctx;
}
RNFFmpegKitModule(const ArkTSTurboModule::Context ctx, const std::string name);
facebook::jsi::Object statisticsToMap(facebook::jsi::Runtime &runtime, const std::shared_ptr<ffmpegkit::Statistics> &statistics);
facebook::jsi::Object logToMap(facebook::jsi::Runtime &runtime, const std::shared_ptr<ffmpegkit::Log> &log);
facebook::jsi::Object toMap(facebook::jsi::Runtime &runtime, std::shared_ptr<ffmpegkit::Session> session);
facebook::jsi::Value medInfoToMap(facebook::jsi::Runtime &rt, const rapidjson::Value &jsonValue);
void registerGlobalCallbacks(facebook::jsi::Runtime &rt);
void globalLogCallback(facebook::jsi::Runtime &runtime,std::shared_ptr<ffmpegkit::Session> &session);
void statisticsCallback(facebook::jsi::Runtime &runtime,std::shared_ptr<ffmpegkit::FFmpegSession> &session);
void enableLogs();
void disableLogs();
void enableStatistics();
void disableStatistics();
ffmpegkit::SessionState toSessionState(const int value);
};
} // namespace rnoh
/**
* GUN License
*
* Copyright (C) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "RnohReactNativeHarmonyFFmpegKitPackage.h"
#include "RNFFmpegKitModule.h"
using namespace rnoh;
class FFmpegKitTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate {
public:
SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override {
if (name == "NativeRNFFmpegKitModule") {
return std::make_shared<RNFFmpegKitModule>(ctx, name);
}
return nullptr;
};
};
std::unique_ptr<TurboModuleFactoryDelegate> RnohReactNativeHarmonyFFmpegKitPackage::createTurboModuleFactoryDelegate() {
return std::make_unique<FFmpegKitTurboModuleFactoryDelegate>();
}
/**
* GUN License
*
* Copyright (C) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include "RNOH/Package.h"
namespace rnoh {
class RnohReactNativeHarmonyFFmpegKitPackage : public Package {
public:
RnohReactNativeHarmonyFFmpegKitPackage(Package::Context ctx) : Package(ctx) {}
std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override;
};
} // namespace rnoh
#!/usr/bin/env bash
set -euo pipefail
sdk=$(cd "$1/.." && pwd)
abi=$2
case "$abi" in
arm64-v8a) cpu=aarch64; triple=aarch64-unknown-linux-ohos ;;
*) echo "FFmpegKit Harmony: unsupported ABI $abi" >&2; exit 1 ;;
esac
llvm="$sdk/llvm/bin"
cc="$llvm/$triple-clang"
cxx="$llvm/$triple-clang++"
jobs=${FFMPEG_KIT_BUILD_JOBS:-8}
cache=${FFMPEG_KIT_HARMONY_CACHE_DIR:-${XDG_CACHE_HOME:-$HOME/.cache}/geek-front/ffmpeg-harmony}
mkdir -p "$cache"
cache=$(cd "$cache" && pwd)
sha256() {
if command -v sha256sum >/dev/null; then sha256sum "$@"; else shasum -a 256 "$@"; fi
}
key=$( { sha256 "$0" "$sdk/oh-uni-package.json" | awk '{print $1}'; "$cc" --version; } | sha256 | cut -c1-20)
target="$cache/$abi-$key"
if [[ -f "$target/prefix/lib/libffmpegkit.a" ]]; then
printf '%s\n' "$target"
exit 0
fi
work=$(mktemp -d "$cache/.build-$abi.XXXXXX")
trap 'rm -rf "$work"' EXIT
cd "$work"
fetch() {
local url=$1 checksum=$2 archive=$3
curl -fLsS --retry 2 --connect-timeout 15 --max-time 180 "$url" -o "$archive"
printf '%s %s\n' "$checksum" "$archive" | sha256 -c - >&2
tar -xf "$archive"
}
fetch https://ffmpeg.org/releases/ffmpeg-6.1.2.tar.xz \
3b624649725ecdc565c903ca6643d41f33bd49239922e45c9b1442c63dca4e38 ffmpeg.tar.xz
fetch https://codeload.github.com/arthenica/ffmpeg-kit/tar.gz/refs/tags/react.native.v6.0.2 \
08b78ea473fa026dfc7bc5138325b3094b9fb55b69f0d26506b30621cfedb53c ffmpeg-kit.tar.gz
fetch https://codeload.github.com/Tencent/rapidjson/tar.gz/6089180ecb704cb2b136777798fa1be303618975 \
7b9ec36b549b076c9edc33b7b851d457cbf1683d44984166c788cc44370290f8 rapidjson.tar.gz
fetch https://codeload.github.com/mirror/x264/tar.gz/31e19f92f00c7003fa115047ce50978bc98c3a0d \
d053c9d86988d6bc78237ca5205865c5ddf99c98ef4cd9927eec8f6d388f6dd9 x264.tar.gz
prefix="$work/prefix"
ff="$work/ffmpeg-6.1.2"
kit="$work/ffmpeg-kit-react.native.v6.0.2/linux/src"
rapid="$work/rapidjson-6089180ecb704cb2b136777798fa1be303618975/include"
out="$work/kit-objects"
mkdir "$out"
echo "FFmpegKit Harmony: building x264 and FFmpeg for $abi" >&2
if ! (
cd x264-31e19f92f00c7003fa115047ce50978bc98c3a0d
CC="$cc" AS="$cc" AR="$llvm/llvm-ar" RANLIB="$llvm/llvm-ranlib" STRIP="$llvm/llvm-strip" \
./configure --prefix="$prefix" --host="$cpu-linux" --enable-static --enable-pic --disable-cli --disable-opencl
make -j"$jobs"
make install
) > x264-build.log 2>&1; then
tail -60 x264-build.log >&2
exit 1
fi
if ! (
cd "$ff"
PKG_CONFIG_LIBDIR="$prefix/lib/pkgconfig" ./configure \
--prefix="$prefix" --target-os=linux --arch="$cpu" --enable-cross-compile \
--sysroot="$sdk/sysroot" --cc="$cc" --ar="$llvm/llvm-ar" --ranlib="$llvm/llvm-ranlib" \
--nm="$llvm/llvm-nm" --strip="$llvm/llvm-strip" --pkg-config-flags=--static \
--disable-programs --disable-doc --disable-debug --disable-autodetect --disable-shared \
--enable-static --enable-pic --enable-gpl --enable-libx264
make -j"$jobs"
make install
) > ffmpeg-build.log 2>&1; then
tail -60 ffmpeg-build.log >&2
exit 1
fi
echo "FFmpegKit Harmony: building the session engine" >&2
export cc cxx ff kit rapid prefix out
# Keep FFmpeg's private headers after SDK headers, including on case-insensitive macOS.
if ! find "$kit" -maxdepth 1 \( -name '*.c' -o -name '*.cpp' \) -print0 |
xargs -0 -P "$jobs" -n 1 bash -c '
src=$1
case "$src" in *.cpp) tool=$cxx; standard=c++17;; *) tool=$cc; standard=gnu11;; esac
"$tool" -std="$standard" -O2 -fPIC -include stdint.h -DFFMPEG_KIT_BUILD_DATE=20240802 \
-I"$prefix/include" -I"$rapid" -iquote "$ff" -idirafter "$ff" -c "$src" -o "$out/${src##*/}.o"
' _ > kit-build.log 2>&1; then
tail -60 kit-build.log >&2
exit 1
fi
"$llvm/llvm-ar" rcs "$prefix/lib/libffmpegkit.a" "$out"/*.o
# Publish only a complete build. Keep public headers and license files with the libraries.
stage="$work/result"
mkdir -p "$stage/include/ffmpegkit" "$stage/include/rapidjson" "$stage/licenses"
cp "$kit"/*.h "$stage/include/ffmpegkit/"
cp -R "$rapid/rapidjson/." "$stage/include/rapidjson/"
cp "$ff/COPYING.GPLv2" "$stage/licenses/ffmpeg-GPLv2"
cp "$work/ffmpeg-kit-react.native.v6.0.2/LICENSE" "$stage/licenses/ffmpeg-kit"
cp "$work/x264-31e19f92f00c7003fa115047ce50978bc98c3a0d/COPYING" "$stage/licenses/x264"
cp "$work/rapidjson-6089180ecb704cb2b136777798fa1be303618975/license.txt" "$stage/licenses/rapidjson"
mv "$prefix" "$stage/prefix"
if [[ ! -e "$target" ]]; then
mv "$stage" "$target" || [[ -f "$target/prefix/lib/libffmpegkit.a" ]]
fi
printf '%s\n' "$target"
/**
* GUN License
*
* Copyright (C) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import type { RNPackageContext, UITurboModule, UITurboModuleContext } from '@rnoh/react-native-openharmony/ts';
import { RNPackage, UITurboModuleFactory } from '@rnoh/react-native-openharmony/ts';
import { RNFFmpegKitModule } from './RNFFmpegKitModule';
class FFmpegKitPackageTurboModulesFactory extends UITurboModuleFactory {
createTurboModule(name: string): UITurboModule | null {
if (name === 'NativeRNFFmpegKitModule') {
return new RNFFmpegKitModule(this.ctx);
}
return null;
}
hasTurboModule(name: string): boolean {
return name === 'NativeRNFFmpegKitModule';
}
}
export class FFmpegKitPackage extends RNPackage {
createTurboModulesFactory(ctx: UITurboModuleContext): UITurboModuleFactory {
return new FFmpegKitPackageTurboModulesFactory(ctx);
}
}
/**
* GUN License
*
* Copyright (C) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { TurboModule } from '@rnoh/react-native-openharmony/ts';
export class RNFFmpegKitModule extends TurboModule{
// 这里是arkts接口的具体实现文件,经过验证,纯jsi接口的项目,在arkts侧也需要构造一个对应的TurboModule。
addListener(eventName: string) {
// this.logger.warn("DevSettings::addListener is not supported");
}
removeListeners(count: number) {
// this.logger.warn("DevSettings::removeListeners is not supported");
}
FFmpegKitLogCallbackEvent(sessionId: number, level: number, message: string): void {
this.ctx.rnInstance.emitDeviceEvent("FFmpegKitLogCallbackEvent",
{ "sessionId": sessionId, "level": level, "message": message });
}
FFmpegKitStatisticsCallbackEvent(sessionId: number, videoFrameNumber: number, videoFps: number, videoQuality: number,
size: number, time: number, bitrate: number, speed: number): void {
this.ctx.rnInstance.emitDeviceEvent("FFmpegKitStatisticsCallbackEvent", {
"sessionId": sessionId,
"videoFrameNumber": videoFrameNumber,
"videoFps": videoFps,
"videoQuality": videoQuality,
"size": size,
"time": time,
"bitrate": bitrate,
"speed": speed
});
}
FFmpegKitCompleteCallbackEvent(sessionId: number): void {
this.ctx.rnInstance.emitDeviceEvent("FFmpegKitCompleteCallbackEvent",{"sessionId": sessionId});
}
}
{
"module": {
"name": "ffmpeg_kit",
"type": "har",
"deviceTypes": ["phone", "tablet", "2in1"]
}
}
/**
* GUN License
*
* Copyright (C) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export * from "./src/main/ets/FFmpegKitPackage"
export * from "./src/main/ets/RNFFmpegKitModule"
{ {
"name": "@geek-front/ffmpeg-react-native", "name": "@geek-front/ffmpeg-react-native",
"version": "6.0.6", "version": "6.0.7",
"description": "FFmpeg Kit for React Native", "description": "FFmpeg Kit for React Native",
"main": "src/index", "main": "src/index",
"types": "src/index.d.ts", "types": "src/index.d.ts",
"react-native": "src/index", "react-native": "src/index",
"source": "src/index", "source": "src/index",
"scripts": { "scripts": {
"build:har": "cd harmony/ffmpeg_kit && npm pack && mv ffmpeg_kit-1.0.0.tgz ../ffmpeg_kit.har",
"prepack": "npm run build:har",
"test": "jest", "test": "jest",
"lint": "eslint \"**/*.{js,ts,tsx}\"", "lint": "eslint \"**/*.{js,ts,tsx}\"",
"release": "release-it" "release": "release-it"
......
...@@ -69,6 +69,8 @@ export type FFprobeSessionCompleteCallback = (session: FFprobeSession) => void; ...@@ -69,6 +69,8 @@ export type FFprobeSessionCompleteCallback = (session: FFprobeSession) => void;
export type MediaInformationSessionCompleteCallback = (session: MediaInformationSession) => void; export type MediaInformationSessionCompleteCallback = (session: MediaInformationSession) => void;
export class FFmpegKit { export class FFmpegKit {
static isSupported(): boolean;
static execute(command: string): Promise<FFmpegSession>; static execute(command: string): Promise<FFmpegSession>;
......
import {NativeEventEmitter, NativeModules} from 'react-native'; import {NativeEventEmitter, NativeModules, Platform, TurboModuleRegistry} from 'react-native';
const {FFmpegKitReactNativeModule} = NativeModules; const FFmpegKitReactNativeModule = Platform.OS === 'harmony'
? TurboModuleRegistry.get('NativeRNFFmpegKitModule')
: NativeModules.FFmpegKitReactNativeModule;
const ffmpegSessionCompleteCallbackMap = new Map() const ffmpegSessionCompleteCallbackMap = new Map()
const ffprobeSessionCompleteCallbackMap = new Map() const ffprobeSessionCompleteCallbackMap = new Map()
...@@ -31,7 +33,7 @@ export const Signal = { ...@@ -31,7 +33,7 @@ export const Signal = {
class FFmpegKitReactNativeEventEmitter extends NativeEventEmitter { class FFmpegKitReactNativeEventEmitter extends NativeEventEmitter {
constructor() { constructor() {
super(FFmpegKitReactNativeModule); super(FFmpegKitReactNativeModule ?? {});
} }
addListener(eventType, listener, context) { addListener(eventType, listener, context) {
...@@ -698,6 +700,10 @@ export class ArchDetect { ...@@ -698,6 +700,10 @@ export class ArchDetect {
*/ */
export class FFmpegKit { export class FFmpegKit {
static isSupported() {
return FFmpegKitReactNativeModule != null;
}
/** /**
* <p>Synchronously executes FFmpeg command provided. Space character is used to split the command * <p>Synchronously executes FFmpeg command provided. Space character is used to split the command
* into arguments. You can use single or double quote characters to specify arguments inside your command. * into arguments. You can use single or double quote characters to specify arguments inside your command.
...@@ -1735,7 +1741,7 @@ class FFmpegKitFactory { ...@@ -1735,7 +1741,7 @@ class FFmpegKitFactory {
} }
class FFmpegKitInitializer { class FFmpegKitInitializer {
static #initialized = false; static #initialization;
static #eventEmitter = new FFmpegKitReactNativeEventEmitter(); static #eventEmitter = new FFmpegKitReactNativeEventEmitter();
static processLogCallbackEvent(event) { static processLogCallbackEvent(event) {
...@@ -1898,27 +1904,31 @@ class FFmpegKitInitializer { ...@@ -1898,27 +1904,31 @@ class FFmpegKitInitializer {
} }
static async initialize() { static async initialize() {
if (this.#initialized) { if (!FFmpegKit.isSupported()) throw new Error('FFmpegKit native module is unavailable');
return; if (!this.#initialization) {
} else { this.#initialization = this.setup().catch(error => {
this.#initialized = true; this.#initialization = undefined;
throw error;
});
} }
await this.#initialization;
}
console.log("Loading ffmpeg-kit-react-native."); static async setup() {
const module = FFmpegKitReactNativeModule;
this.#eventEmitter.addListener(eventLogCallbackEvent, FFmpegKitInitializer.processLogCallbackEvent); if (Platform.OS === 'harmony') await module.registerGlobalCallbacks();
this.#eventEmitter.addListener(eventStatisticsCallbackEvent, FFmpegKitInitializer.processStatisticsCallbackEvent);
this.#eventEmitter.addListener(eventCompleteCallbackEvent, FFmpegKitInitializer.processCompleteCallbackEvent);
FFmpegKitFactory.setLogLevel(await FFmpegKitReactNativeModule.getLogLevel()); FFmpegKitFactory.setLogLevel(await module.getLogLevel());
const version = FFmpegKitFactory.getVersion(); const platform = await module.getPlatform();
const platform = await FFmpegKitConfig.getPlatform(); const arch = await module.getArch();
const arch = await ArchDetect.getArch(); const packageName = await module.getPackageName();
const packageName = await Packages.getPackageName(); const isLTSPostfix = (await module.isLTSBuild()) ? '-lts' : '';
await FFmpegKitConfig.enableRedirection();
const isLTSPostfix = (await FFmpegKitConfig.isLTSBuild()) ? "-lts" : "";
console.log(`Loaded ffmpeg-kit-react-native-${platform}-${packageName}-${arch}-${version}${isLTSPostfix}.`); await module.enableRedirection();
this.#eventEmitter.addListener(eventLogCallbackEvent, this.processLogCallbackEvent);
this.#eventEmitter.addListener(eventStatisticsCallbackEvent, this.processStatisticsCallbackEvent);
this.#eventEmitter.addListener(eventCompleteCallbackEvent, this.processCompleteCallbackEvent);
console.log(`Loaded ffmpeg-kit-react-native-${platform}-${packageName}-${arch}-${FFmpegKitFactory.getVersion()}${isLTSPostfix}.`);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment