Files
shaka-player/transmuxer_worker.uncompiled.js
T
Ivan 921206dc1d feat: transmux in a worker (#9914)
This PR introduces a Web Worker for transmuxing resolving
https://github.com/shaka-project/shaka-player/issues/1735

- The worker bundle is compiled separately
- The build output is embedded as a string constant and then wrapped in
a Blob to create an inline Worker URL (HLS.js does this very similarly)
- `TransmuxerProxy` is created wrapping a real transmuxer, but no worker
is started yet - on the first `transmux()` call, it checks if the device
supports worker transmuxing
- For each transmux() call: the buffer is copied, then zero-copy
transferred to the worker. A PublicPromise is stored under a reqId with
a timeout timer, and the main thread awaits it.
- The worker transmuxes and posts back transmuxed (or error). The shared
message listener routes the response to the right proxy instance by id,
which resolves the promise and cancels the timer.
- When the last proxy instance is destroyed, the worker is terminated
and the blob URL is revoked.
  loaded inside the worker.
- Some low-end devices have been excluded since their Worker support is
questionable

There most likely is a better way to do this - please let me know
2026-05-27 21:51:03 +02:00

67 lines
2.5 KiB
JavaScript

/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Entry point for the transmuxer Web Worker in uncompiled
* (development) mode. Loads Closure Library via importScripts, pulls in the
* dependency graph, then goog.require's the transmuxer plugins and the worker
* entry point.
*
* In compiled builds this file is not used — the compiled bundle is loaded
* directly as the Worker script.
*/
// Closure's base.js uses document.currentScript (or document.write) to resolve
// relative paths. Neither exists inside a Worker, so we provide a
// CLOSURE_IMPORT_SCRIPT hook that uses importScripts instead, and set
// CLOSURE_BASE_PATH so Closure can find its own modules.
/* eslint-disable no-restricted-syntax */
// @ts-ignore
self.CLOSURE_BASE_PATH =
'node_modules/google-closure-library/closure/goog/';
// Tell Closure to use importScripts for loading dependencies.
self.CLOSURE_IMPORT_SCRIPT = (src) => {
importScripts(src);
return true;
};
// Load Closure Library base and the generated dependency map.
importScripts(
'node_modules/google-closure-library/closure/goog/base.js',
'dist/deps.js');
// Pull in all device plugins so DeviceFactory.getDevice() returns the correct
// device inside the worker (needed by Ac3Transmuxer and MimeUtils codec
// conversion). Without these, getDevice() always returns DefaultBrowser even
// on Tizen/WebOS/Xbox/etc.
goog.require('shaka.device.AppleBrowser');
goog.require('shaka.device.Chromecast');
goog.require('shaka.device.DefaultBrowser');
goog.require('shaka.device.Hisense');
goog.require('shaka.device.PlayStation');
goog.require('shaka.device.TitanOS');
goog.require('shaka.device.Tizen');
goog.require('shaka.device.Vizio');
goog.require('shaka.device.WebKitSTB');
goog.require('shaka.device.WebOS');
goog.require('shaka.device.Xbox');
// Pull in the transmuxer plugins so they self-register with
// TransmuxerEngine, then load the worker entry point.
goog.require('shaka.transmuxer.AacTransmuxer');
goog.require('shaka.transmuxer.Ac3Transmuxer');
goog.require('shaka.transmuxer.Ec3Transmuxer');
goog.require('shaka.transmuxer.LocTransmuxer');
goog.require('shaka.transmuxer.Mp3Transmuxer');
goog.require('shaka.transmuxer.MpegTsTransmuxer');
goog.require('shaka.transmuxer.TsTransmuxer');
goog.require('shaka.transmuxer.TransmuxerWorker');
// The auto-boot at the bottom of transmuxer_worker.js will have already
// called TransmuxerWorker.boot() when it was loaded by goog.require above.