diff options
author | 2024-08-23 15:15:35 +0000 | |
---|---|---|
committer | 2024-08-23 17:15:35 +0200 | |
commit | 8e5a72ac5c37f26b49f5fdd02726ea13e3aac6b0 (patch) | |
tree | 041139f1883541411c9bb4b6738dee64b51b197a /internal/media/ffmpeg/ffmpeg.go | |
parent | [feature] Use `local_only` field, deprecate `federated` field (#3222) (diff) | |
download | gotosocial-8e5a72ac5c37f26b49f5fdd02726ea13e3aac6b0.tar.xz |
[performance] ffmpeg ffprobe wrapper improvements (#3225)
* use a single instance of wazero runtime and compiled modules
* remove test output :facepalm:
* undo process-{media,emoji} changes
* update test runner to include wazero compilation cache
* sign drone.yml
---------
Co-authored-by: tobi <tobi.smethurst@protonmail.com>
Diffstat (limited to 'internal/media/ffmpeg/ffmpeg.go')
-rw-r--r-- | internal/media/ffmpeg/ffmpeg.go | 67 |
1 files changed, 12 insertions, 55 deletions
diff --git a/internal/media/ffmpeg/ffmpeg.go b/internal/media/ffmpeg/ffmpeg.go index 253323989..d33fef34e 100644 --- a/internal/media/ffmpeg/ffmpeg.go +++ b/internal/media/ffmpeg/ffmpeg.go @@ -19,65 +19,22 @@ package ffmpeg import ( "context" - - ffmpeglib "codeberg.org/gruf/go-ffmpreg/embed/ffmpeg" - "codeberg.org/gruf/go-ffmpreg/wasm" - - "github.com/tetratelabs/wazero" - "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" ) -// InitFfmpeg initializes the ffmpeg WebAssembly instance pool, -// with given maximum limiting the number of concurrent instances. +// ffmpegRunner limits the number of +// ffmpeg WebAssembly instances that +// may be concurrently running, in +// order to reduce memory usage. +var ffmpegRunner runner + +// InitFfmpeg precompiles the ffmpeg WebAssembly source into memory and +// prepares the runner to only allow max given concurrent running instances. func InitFfmpeg(ctx context.Context, max int) error { - initCache() // ensure compilation cache initialized - return ffmpegPool.Init(ctx, max) + ffmpegRunner.Init(max) + return compileFfmpeg(ctx) } // Ffmpeg runs the given arguments with an instance of ffmpeg. -func Ffmpeg(ctx context.Context, args wasm.Args) (uint32, error) { - return ffmpegPool.Run(ctx, args) -} - -var ffmpegPool = wasmInstancePool{ - inst: wasm.Instantiator{ - - // WASM module name. - Module: "ffmpeg", - - // Per-instance WebAssembly runtime (with shared cache). - Runtime: func(ctx context.Context) wazero.Runtime { - - // Prepare config with cache. - cfg := wazero.NewRuntimeConfig() - cfg = cfg.WithCoreFeatures(ffmpeglib.CoreFeatures) - cfg = cfg.WithCompilationCache(cache) - - // Instantiate runtime with our config. - rt := wazero.NewRuntimeWithConfig(ctx, cfg) - - // Prepare default "env" host module. - env := rt.NewHostModuleBuilder("env") - - // Instantiate "env" module in our runtime. - _, err := env.Instantiate(context.Background()) - if err != nil { - panic(err) - } - - // Instantiate the wasi snapshot preview 1 in runtime. - _, err = wasi_snapshot_preview1.Instantiate(ctx, rt) - if err != nil { - panic(err) - } - - return rt - }, - - // Per-run module configuration. - Config: wazero.NewModuleConfig, - - // Embedded WASM. - Source: ffmpeglib.B, - }, +func Ffmpeg(ctx context.Context, args Args) (uint32, error) { + return ffmpegRunner.Run(ctx, ffmpeg, args) } |