UNPKG

media-encoder-host-broker

Version:
157 lines (153 loc) 6.51 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@babel/runtime/helpers/asyncToGenerator'), require('@babel/runtime/regenerator'), require('broker-factory'), require('fast-unique-numbers')) : typeof define === 'function' && define.amd ? define(['exports', '@babel/runtime/helpers/asyncToGenerator', '@babel/runtime/regenerator', 'broker-factory', 'fast-unique-numbers'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.mediaEncoderHostBroker = {}, global._asyncToGenerator, global._regeneratorRuntime, global.brokerFactory, global.fastUniqueNumbers)); })(this, (function (exports, _asyncToGenerator, _regeneratorRuntime, brokerFactory, fastUniqueNumbers) { 'use strict'; var encoderIds = new Set(); var encoderInstanceIds = new Set(); var messagePorts = new WeakMap(); var wrap = brokerFactory.createBroker({ deregister: function deregister(_ref) { var call = _ref.call; return /*#__PURE__*/function () { var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(port) { var encoderId, result; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: encoderId = messagePorts.get(port); if (!(encoderId === undefined)) { _context.next = 3; break; } throw new Error('There is no encoder registered with the given port.'); case 3: _context.next = 5; return call('deregister', { encoderId: encoderId }); case 5: result = _context.sent; encoderIds["delete"](encoderId); messagePorts["delete"](port); return _context.abrupt("return", result); case 9: case "end": return _context.stop(); } }, _callee); })); return function (_x) { return _ref2.apply(this, arguments); }; }(); }, encode: function encode(_ref3) { var call = _ref3.call; return /*#__PURE__*/function () { var _ref4 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(encoderInstanceId, timeslice) { var arrayBuffers; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return call('encode', { encoderInstanceId: encoderInstanceId, timeslice: timeslice }); case 2: arrayBuffers = _context2.sent; encoderInstanceIds["delete"](encoderInstanceId); return _context2.abrupt("return", arrayBuffers); case 5: case "end": return _context2.stop(); } }, _callee2); })); return function (_x2, _x3) { return _ref4.apply(this, arguments); }; }(); }, instantiate: function instantiate(_ref5) { var call = _ref5.call; return /*#__PURE__*/function () { var _ref6 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(mimeType, sampleRate) { var encoderInstanceId, port; return _regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: encoderInstanceId = fastUniqueNumbers.addUniqueNumber(encoderInstanceIds); _context3.next = 3; return call('instantiate', { encoderInstanceId: encoderInstanceId, mimeType: mimeType, sampleRate: sampleRate }); case 3: port = _context3.sent; return _context3.abrupt("return", { encoderInstanceId: encoderInstanceId, port: port }); case 5: case "end": return _context3.stop(); } }, _callee3); })); return function (_x4, _x5) { return _ref6.apply(this, arguments); }; }(); }, register: function register(_ref7) { var call = _ref7.call; return /*#__PURE__*/function () { var _ref8 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee4(port) { var encoderId; return _regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: if (!messagePorts.has(port)) { _context4.next = 2; break; } throw new Error(''); case 2: encoderId = fastUniqueNumbers.addUniqueNumber(encoderIds); messagePorts.set(port, encoderId); _context4.prev = 4; _context4.next = 7; return call('register', { encoderId: encoderId, port: port }, [port]); case 7: return _context4.abrupt("return", _context4.sent); case 10: _context4.prev = 10; _context4.t0 = _context4["catch"](4); encoderIds["delete"](encoderId); messagePorts["delete"](port); throw _context4.t0; case 15: case "end": return _context4.stop(); } }, _callee4, null, [[4, 10]]); })); return function (_x6) { return _ref8.apply(this, arguments); }; }(); } }); var load = function load(url) { var worker = new Worker(url); return wrap(worker); }; exports.load = load; exports.wrap = wrap; }));