livekit-client 1.12.0 → 1.12.1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +14 -0
- package/dist/livekit-client.e2ee.worker.js +1 -1
- package/dist/livekit-client.e2ee.worker.js.map +1 -1
- package/dist/livekit-client.e2ee.worker.mjs +59 -39
- package/dist/livekit-client.e2ee.worker.mjs.map +1 -1
- package/dist/livekit-client.esm.mjs +44 -17
- package/dist/livekit-client.esm.mjs.map +1 -1
- package/dist/livekit-client.umd.js +1 -1
- package/dist/livekit-client.umd.js.map +1 -1
- package/dist/src/e2ee/worker/FrameCryptor.d.ts +0 -1
- package/dist/src/e2ee/worker/FrameCryptor.d.ts.map +1 -1
- package/dist/src/e2ee/worker/ParticipantKeyHandler.d.ts +8 -0
- package/dist/src/e2ee/worker/ParticipantKeyHandler.d.ts.map +1 -1
- package/dist/src/room/Room.d.ts.map +1 -1
- package/dist/src/room/participant/RemoteParticipant.d.ts +6 -4
- package/dist/src/room/participant/RemoteParticipant.d.ts.map +1 -1
- package/dist/src/room/track/create.d.ts.map +1 -1
- package/dist/src/utils/browserParser.d.ts +2 -0
- package/dist/src/utils/browserParser.d.ts.map +1 -1
- package/dist/ts4.2/src/e2ee/worker/FrameCryptor.d.ts +0 -1
- package/dist/ts4.2/src/e2ee/worker/ParticipantKeyHandler.d.ts +8 -0
- package/dist/ts4.2/src/room/participant/RemoteParticipant.d.ts +6 -4
- package/dist/ts4.2/src/utils/browserParser.d.ts +2 -0
- package/package.json +1 -1
- package/src/e2ee/worker/FrameCryptor.ts +12 -14
- package/src/e2ee/worker/ParticipantKeyHandler.ts +15 -0
- package/src/e2ee/worker/e2ee.worker.ts +12 -6
- package/src/room/Room.ts +17 -7
- package/src/room/participant/RemoteParticipant.ts +19 -15
- package/src/room/track/create.ts +9 -0
- package/src/utils/browserParser.ts +5 -0
package/README.md
CHANGED
@@ -30,6 +30,18 @@ yarn add livekit-client
|
|
30
30
|
npm install livekit-client --save
|
31
31
|
```
|
32
32
|
|
33
|
+
### Minified JS
|
34
|
+
|
35
|
+
To use the SDK without a package manager, you can include it with a script tag:
|
36
|
+
|
37
|
+
```html
|
38
|
+
<script src="https://cdn.jsdelivr.net/npm/livekit-client/dist/livekit-client.umd.min.js"></script>
|
39
|
+
```
|
40
|
+
|
41
|
+
The module will be exported under `LivekitClient` in the global namespace. When
|
42
|
+
accessing symbols from the class, you'd need to prefix them with `LivekitClient.`.
|
43
|
+
For example, `Room` becomes `LivekitClient.Room`.
|
44
|
+
|
33
45
|
## Usage
|
34
46
|
|
35
47
|
Examples below are in TypeScript, if using JS/CommonJS imports replace import with:
|
@@ -297,7 +309,9 @@ If you are targeting legacy browsers, but still want adaptiveStream functionalit
|
|
297
309
|
Also when targeting legacy browsers, older than the ones specified in our browserslist target, make sure to transpile the library code to your desired target and include required polyfills with babel and/or corejs.
|
298
310
|
|
299
311
|
<!--BEGIN_REPO_NAV-->
|
312
|
+
|
300
313
|
<br/><table>
|
314
|
+
|
301
315
|
<thead><tr><th colspan="2">LiveKit Ecosystem</th></tr></thead>
|
302
316
|
<tbody>
|
303
317
|
<tr><td>Client SDKs</td><td><a href="https://github.com/livekit/components-js">Components</a> · <b>JavaScript</b> · <a href="https://github.com/livekit/client-sdk-rust">Rust</a> · <a href="https://github.com/livekit/client-sdk-swift">iOS/macOS</a> · <a href="https://github.com/livekit/client-sdk-android">Android</a> · <a href="https://github.com/livekit/client-sdk-flutter">Flutter</a> · <a href="https://github.com/livekit/client-sdk-unity-web">Unity (web)</a> · <a href="https://github.com/livekit/client-sdk-python">Python</a> · <a href="https://github.com/livekit/client-sdk-react-native">React Native (beta)</a></td></tr><tr></tr>
|
@@ -1,2 +1,2 @@
|
|
1
|
-
!function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function t(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var n,r,i,o={exports:{}};r=e,i=function(){var e=function(){},t="undefined",n=typeof window!==t&&typeof window.navigator!==t&&/Trident\/|MSIE /.test(window.navigator.userAgent),r=["trace","debug","info","warn","error"];function i(e,t){var n=e[t];if("function"==typeof n.bind)return n.bind(e);try{return Function.prototype.bind.call(n,e)}catch(t){return function(){return Function.prototype.apply.apply(n,[e,arguments])}}}function o(){console.log&&(console.log.apply?console.log.apply(console,arguments):Function.prototype.apply.apply(console.log,[console,arguments])),console.trace&&console.trace()}function a(t,n){for(var i=0;i<r.length;i++){var o=r[i];this[o]=i<t?e:this.methodFactory(o,t,n)}this.log=this.debug}function s(e,n,r){return function(){typeof console!==t&&(a.call(this,n,r),this[e].apply(this,arguments))}}function c(r,a,c){return function(r){return"debug"===r&&(r="log"),typeof console!==t&&("trace"===r&&n?o:void 0!==console[r]?i(console,r):void 0!==console.log?i(console,"log"):e)}(r)||s.apply(this,arguments)}function d(e,n,i){var o,s=this;n=null==n?"WARN":n;var d="loglevel";function l(){var e;if(typeof window!==t&&d){try{e=window.localStorage[d]}catch(e){}if(typeof e===t)try{var n=window.document.cookie,r=n.indexOf(encodeURIComponent(d)+"=");-1!==r&&(e=/^([^;]+)/.exec(n.slice(r))[1])}catch(e){}return void 0===s.levels[e]&&(e=void 0),e}}"string"==typeof e?d+=":"+e:"symbol"==typeof e&&(d=void 0),s.name=e,s.levels={TRACE:0,DEBUG:1,INFO:2,WARN:3,ERROR:4,SILENT:5},s.methodFactory=i||c,s.getLevel=function(){return o},s.setLevel=function(n,i){if("string"==typeof n&&void 0!==s.levels[n.toUpperCase()]&&(n=s.levels[n.toUpperCase()]),!("number"==typeof n&&n>=0&&n<=s.levels.SILENT))throw"log.setLevel() called with invalid level: "+n;if(o=n,!1!==i&&function(e){var n=(r[e]||"silent").toUpperCase();if(typeof window!==t&&d){try{return void(window.localStorage[d]=n)}catch(e){}try{window.document.cookie=encodeURIComponent(d)+"="+n+";"}catch(e){}}}(n),a.call(s,n,e),typeof console===t&&n<s.levels.SILENT)return"No console available for logging"},s.setDefaultLevel=function(e){n=e,l()||s.setLevel(e,!1)},s.resetLevel=function(){s.setLevel(n,!1),function(){if(typeof window!==t&&d){try{return void window.localStorage.removeItem(d)}catch(e){}try{window.document.cookie=encodeURIComponent(d)+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC"}catch(e){}}}()},s.enableAll=function(e){s.setLevel(s.levels.TRACE,e)},s.disableAll=function(e){s.setLevel(s.levels.SILENT,e)};var u=l();null==u&&(u=n),s.setLevel(u,!1)}var l=new d,u={};l.getLogger=function(e){if("symbol"!=typeof e&&"string"!=typeof e||""===e)throw new TypeError("You must supply a name when creating a logger.");var t=u[e];return t||(t=u[e]=new d(e,l.getLevel(),l.methodFactory)),t};var y=typeof window!==t?window.log:void 0;return l.noConflict=function(){return typeof window!==t&&window.log===l&&(window.log=y),l},l.getLoggers=function(){return u},l.default=l,l},(n=o).exports?n.exports=i():r.log=i();var a,s=o.exports;!function(e){e[e.trace=0]="trace",e[e.debug=1]="debug",e[e.info=2]="info",e[e.warn=3]="warn",e[e.error=4]="error",e[e.silent=5]="silent"}(a||(a={}));s.getLogger("livekit").setDefaultLevel(a.info);const c=s.getLogger("lk-e2ee"),d="AES-GCM",l={key:10,delta:3,audio:1,empty:0},u={sharedKey:!1,ratchetSalt:"LKFrameEncryptionKey",ratchetWindowSize:8};class y extends Error{constructor(e,t){super(t||"an error has occured"),this.code=e}}var h,p;!function(e){e.PermissionDenied="PermissionDenied",e.NotFound="NotFound",e.DeviceInUse="DeviceInUse",e.Other="Other"}(h||(h={})),function(e){e.getFailure=function(t){if(t&&"name"in t)return"NotFoundError"===t.name||"DevicesNotFoundError"===t.name?e.NotFound:"NotAllowedError"===t.name||"PermissionDeniedError"===t.name?e.PermissionDenied:"NotReadableError"===t.name||"TrackStartError"===t.name?e.DeviceInUse:e.Other}}(h||(h={})),function(e){e[e.InvalidKey=0]="InvalidKey",e[e.MissingKey=1]="MissingKey",e[e.InternalError=2]="InternalError"}(p||(p={}));class f extends y{constructor(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:p.InternalError;super(40,e),this.reason=t}}function v(e,t,n,r){return new(n||(n=Promise))((function(i,o){function a(e){try{c(r.next(e))}catch(e){o(e)}}function s(e){try{c(r.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,s)}c((r=r.apply(e,t||[])).next())}))}var g={exports:{}};!function(e){var t=Object.prototype.hasOwnProperty,n="~";function r(){}function i(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function o(e,t,r,o,a){if("function"!=typeof r)throw new TypeError("The listener must be a function");var s=new i(r,o||e,a),c=n?n+t:t;return e._events[c]?e._events[c].fn?e._events[c]=[e._events[c],s]:e._events[c].push(s):(e._events[c]=s,e._eventsCount++),e}function a(e,t){0==--e._eventsCount?e._events=new r:delete e._events[t]}function s(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),s.prototype.eventNames=function(){var e,r,i=[];if(0===this._eventsCount)return i;for(r in e=this._events)t.call(e,r)&&i.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?i.concat(Object.getOwnPropertySymbols(e)):i},s.prototype.listeners=function(e){var t=n?n+e:e,r=this._events[t];if(!r)return[];if(r.fn)return[r.fn];for(var i=0,o=r.length,a=new Array(o);i<o;i++)a[i]=r[i].fn;return a},s.prototype.listenerCount=function(e){var t=n?n+e:e,r=this._events[t];return r?r.fn?1:r.length:0},s.prototype.emit=function(e,t,r,i,o,a){var s=n?n+e:e;if(!this._events[s])return!1;var c,d,l=this._events[s],u=arguments.length;if(l.fn){switch(l.once&&this.removeListener(e,l.fn,void 0,!0),u){case 1:return l.fn.call(l.context),!0;case 2:return l.fn.call(l.context,t),!0;case 3:return l.fn.call(l.context,t,r),!0;case 4:return l.fn.call(l.context,t,r,i),!0;case 5:return l.fn.call(l.context,t,r,i,o),!0;case 6:return l.fn.call(l.context,t,r,i,o,a),!0}for(d=1,c=new Array(u-1);d<u;d++)c[d-1]=arguments[d];l.fn.apply(l.context,c)}else{var y,h=l.length;for(d=0;d<h;d++)switch(l[d].once&&this.removeListener(e,l[d].fn,void 0,!0),u){case 1:l[d].fn.call(l[d].context);break;case 2:l[d].fn.call(l[d].context,t);break;case 3:l[d].fn.call(l[d].context,t,r);break;case 4:l[d].fn.call(l[d].context,t,r,i);break;default:if(!c)for(y=1,c=new Array(u-1);y<u;y++)c[y-1]=arguments[y];l[d].fn.apply(l[d].context,c)}}return!0},s.prototype.on=function(e,t,n){return o(this,e,t,n,!1)},s.prototype.once=function(e,t,n){return o(this,e,t,n,!0)},s.prototype.removeListener=function(e,t,r,i){var o=n?n+e:e;if(!this._events[o])return this;if(!t)return a(this,o),this;var s=this._events[o];if(s.fn)s.fn!==t||i&&!s.once||r&&s.context!==r||a(this,o);else{for(var c=0,d=[],l=s.length;c<l;c++)(s[c].fn!==t||i&&!s[c].once||r&&s[c].context!==r)&&d.push(s[c]);d.length?this._events[o]=1===d.length?d[0]:d:a(this,o)}return this},s.prototype.removeAllListeners=function(e){var t;return e?(t=n?n+e:e,this._events[t]&&a(this,t)):(this._events=new r,this._eventsCount=0),this},s.prototype.off=s.prototype.removeListener,s.prototype.addListener=s.prototype.on,s.prefixed=n,s.EventEmitter=s,e.exports=s}(g);var w=t(g.exports);const m="cryptorError";var I;function b(e,t){const n=(new TextEncoder).encode(t);switch(e){case"HKDF":return{name:"HKDF",salt:n,hash:"SHA-256",info:new ArrayBuffer(128)};case"PBKDF2":return{name:"PBKDF2",salt:n,hash:"SHA-256",iterations:1e5};default:throw new Error("algorithm ".concat(e," is currently unsupported"))}}function k(e,t){return v(this,void 0,void 0,(function*(){const n=b(e.algorithm.name,t),r=yield crypto.subtle.deriveKey(n,e,{name:d,length:128},!1,["encrypt","decrypt"]);return{material:e,encryptionKey:r}}))}!function(e){e.telephone={maxBitrate:12e3},e.speech={maxBitrate:2e4},e.music={maxBitrate:32e3},e.musicStereo={maxBitrate:48e3},e.musicHighQuality={maxBitrate:64e3},e.musicHighQualityStereo={maxBitrate:96e3}}(I||(I={}));class E extends w{encodeFunction(e,t){throw Error("not implemented for subclass")}decodeFunction(e,t){throw Error("not implemented for subclass")}}class L extends E{constructor(e){var t;super(),this.isKeyInvalid=!1,this.sendCounts=new Map,this.keys=e.keys,this.participantId=e.participantId,this.rtpMap=new Map,this.keyProviderOptions=e.keyProviderOptions,this.unencryptedFrameByteTrailer=null!==(t=e.unencryptedFrameBytes)&&void 0!==t?t:(new TextEncoder).encode("LKROCKS")}setParticipant(e,t){this.participantId=e,this.keys=t}unsetParticipant(){this.participantId=void 0}getParticipantId(){return this.participantId}getTrackId(){return this.trackId}setVideoCodec(e){this.videoCodec=e}setRtpMap(e){this.rtpMap=e}setupTransform(e,t,n,r,i){i&&(console.info("setting codec on cryptor to",i),this.videoCodec=i);const o="encode"===e?this.encodeFunction:this.decodeFunction,a=new TransformStream({transform:o.bind(this)});t.pipeThrough(a).pipeTo(n).catch((e=>{console.error(e),this.emit("cryptorError",e instanceof f?e:new f(e.message))})),this.trackId=r}encodeFunction(e,t){var n;return v(this,void 0,void 0,(function*(){if(!this.keys.isEnabled()||0===e.data.byteLength)return t.enqueue(e);const{encryptionKey:r}=this.keys.getKeySet(),i=this.keys.getCurrentKeyIndex();if(r){const o=this.makeIV(null!==(n=e.getMetadata().synchronizationSource)&&void 0!==n?n:-1,e.timestamp),a=new Uint8Array(e.data,0,this.getUnencryptedBytes(e)),s=new Uint8Array(2);s[0]=12,s[1]=i;try{const n=yield crypto.subtle.encrypt({name:d,iv:o,additionalData:new Uint8Array(e.data,0,a.byteLength)},r,new Uint8Array(e.data,this.getUnencryptedBytes(e))),i=new ArrayBuffer(a.byteLength+n.byteLength+o.byteLength+s.byteLength),c=new Uint8Array(i);return c.set(a),c.set(new Uint8Array(n),a.byteLength),c.set(new Uint8Array(o),a.byteLength+n.byteLength),c.set(s,a.byteLength+n.byteLength+o.byteLength),e.data=i,t.enqueue(e)}catch(e){c.error(e)}}else this.emit(m,new f("encryption key missing for encoding",p.MissingKey))}))}decodeFunction(e,t){return v(this,void 0,void 0,(function*(){if(!this.keys.isEnabled()||0===e.data.byteLength||function(e,t){const n=new Uint8Array(e.slice(e.byteLength-t.byteLength));return t.every(((e,t)=>e===n[t]))}(e.data,this.unencryptedFrameByteTrailer))return t.enqueue(e);const n=new Uint8Array(e.data)[e.data.byteLength-1];if(this.keys.getKeySet(n))try{const r=yield this.decryptFrame(e,n);if(r)return t.enqueue(r);this.isKeyInvalid=!1}catch(e){e instanceof f&&e.reason===p.InvalidKey?this.isKeyInvalid||(c.warn("invalid key"),this.emit(m,new f("invalid key for participant ".concat(this.participantId),p.InvalidKey)),this.isKeyInvalid=!0):c.warn("decoding frame failed",{error:e})}else this.emit(m,new f("key missing for participant ".concat(this.participantId),p.MissingKey));return t.enqueue(e)}))}decryptFrame(e,t){let n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:void 0,r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{ratchetCount:0};var i;return v(this,void 0,void 0,(function*(){const o=this.keys.getKeySet(t);try{const t=new Uint8Array(e.data,0,this.getUnencryptedBytes(e)),n=new Uint8Array(e.data,e.data.byteLength-2,2),a=n[0],s=new Uint8Array(e.data,e.data.byteLength-a-n.byteLength,a),c=t.byteLength,l=e.data.byteLength-(t.byteLength+a+n.byteLength),u=yield crypto.subtle.decrypt({name:d,iv:s,additionalData:new Uint8Array(e.data,0,t.byteLength)},null!==(i=r.encryptionKey)&&void 0!==i?i:o.encryptionKey,new Uint8Array(e.data,c,l)),y=new ArrayBuffer(t.byteLength+u.byteLength),h=new Uint8Array(y);return h.set(new Uint8Array(e.data,0,t.byteLength)),h.set(new Uint8Array(u),t.byteLength),e.data=y,e}catch(i){if(!(this.keyProviderOptions.ratchetWindowSize>0))throw new f("Decryption failed, most likely because of an invalid key",p.InvalidKey);if(r.ratchetCount<this.keyProviderOptions.ratchetWindowSize){let i;if(c.debug("ratcheting key attempt ".concat(r.ratchetCount," of ").concat(this.keyProviderOptions.ratchetWindowSize,", for kind ").concat(e instanceof RTCEncodedAudioFrame?"audio":"video")),o===this.keys.getKeySet(t)){const e=yield this.keys.ratchetKey(t,!1);i=yield k(e,this.keyProviderOptions.ratchetSalt)}const a=yield this.decryptFrame(e,t,n||o,{ratchetCount:r.ratchetCount+1,encryptionKey:null==i?void 0:i.encryptionKey});return a&&i&&(this.keys.setKeySet(i,t,!0),this.keys.setCurrentKeyIndex(t)),a}n&&(c.debug("resetting to initial material"),this.keys.setKeyFromMaterial(n.material,t)),c.warn("maximum ratchet attempts exceeded, resetting key")}}))}makeIV(e,t){var n;const r=new ArrayBuffer(12),i=new DataView(r);this.sendCounts.has(e)||this.sendCounts.set(e,Math.floor(65535*Math.random()));const o=null!==(n=this.sendCounts.get(e))&&void 0!==n?n:0;return i.setUint32(0,e),i.setUint32(4,t),i.setUint32(8,t-o%65535),this.sendCounts.set(e,o+1),r}getUnencryptedBytes(e){var t;if(function(e){return"type"in e}(e)){let n=null!==(t=this.getVideoCodec(e))&&void 0!==t?t:this.videoCodec;if("av1"===n||"vp9"===n)throw new Error("".concat(n," is not yet supported for end to end encryption"));if("vp8"===n)return l[e.type];const r=new Uint8Array(e.data);try{const e=function(e){const t=[];let n=0,r=0,i=e.length-2;for(;r<i;){for(;r<i&&(0!==e[r]||0!==e[r+1]||1!==e[r+2]);)r++;r>=i&&(r=e.length);let o=r;for(;o>n&&0===e[o-1];)o--;if(0===n){if(o!==n)throw TypeError("byte stream contains leading data")}else t.push(n);n=r+=3}return t}(r);if("h264"===n||e.some((e=>[K.SLICE_IDR,K.SLICE_NON_IDR].includes(S(r[e]))))){for(const t of e){switch(S(r[t])){case K.SLICE_IDR:case K.SLICE_NON_IDR:return t+2}}throw new TypeError("Could not find NALU")}}catch(e){}return l[e.type]}return l.audio}getVideoCodec(e){if(0===this.rtpMap.size)return;const t=e.getMetadata().payloadType;return t?this.rtpMap.get(t):void 0}}function S(e){return e&_}const _=31;var K;!function(e){e[e.SLICE_NON_IDR=1]="SLICE_NON_IDR",e[e.SLICE_PARTITION_A=2]="SLICE_PARTITION_A",e[e.SLICE_PARTITION_B=3]="SLICE_PARTITION_B",e[e.SLICE_PARTITION_C=4]="SLICE_PARTITION_C",e[e.SLICE_IDR=5]="SLICE_IDR",e[e.SEI=6]="SEI",e[e.SPS=7]="SPS",e[e.PPS=8]="PPS",e[e.AUD=9]="AUD",e[e.END_SEQ=10]="END_SEQ",e[e.END_STREAM=11]="END_STREAM",e[e.FILLER_DATA=12]="FILLER_DATA",e[e.SPS_EXT=13]="SPS_EXT",e[e.PREFIX_NALU=14]="PREFIX_NALU",e[e.SUBSET_SPS=15]="SUBSET_SPS",e[e.DPS=16]="DPS",e[e.SLICE_AUX=19]="SLICE_AUX",e[e.SLICE_EXT=20]="SLICE_EXT",e[e.SLICE_LAYER_EXT=21]="SLICE_LAYER_EXT"}(K||(K={}));class C extends w{constructor(e,t,n){super(),this.currentKeyIndex=0,this.cryptoKeyRing=new Array(16),this.enabled=t,this.keyProviderOptions=n,this.ratchetPromiseMap=new Map,this.participantId=e}setEnabled(e){this.enabled=e}ratchetKey(e){let t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];const n=null!=e?e:e=this.getCurrentKeyIndex(),r=this.ratchetPromiseMap.get(n);if(void 0!==r)return r;const i=new Promise(((r,i)=>v(this,void 0,void 0,(function*(){try{const i=this.getKeySet(n).material,o=yield function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{name:d},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"encrypt";return v(this,void 0,void 0,(function*(){return crypto.subtle.importKey("raw",e,t,!1,"derive"===n?["deriveBits","deriveKey"]:["encrypt","decrypt"])}))}(yield function(e,t){return v(this,void 0,void 0,(function*(){const n=b(e.algorithm.name,t);return crypto.subtle.deriveBits(n,e,256)}))}(i,this.keyProviderOptions.ratchetSalt),i.algorithm.name,"derive");t&&this.setKeyFromMaterial(o,n,!0),this.emit("keyRatcheted",o,e,this.participantId),r(o)}catch(e){i(e)}finally{this.ratchetPromiseMap.delete(n)}}))));return this.ratchetPromiseMap.set(n,i),i}setKeyFromMaterial(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return v(this,void 0,void 0,(function*(){c.debug("setting new key"),t>=0&&(this.currentKeyIndex=t%this.cryptoKeyRing.length);const r=yield k(e,this.keyProviderOptions.ratchetSalt);this.setKeySet(r,this.currentKeyIndex,n)}))}setKeySet(e,t){let n=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return v(this,void 0,void 0,(function*(){this.cryptoKeyRing[t%this.cryptoKeyRing.length]=e,n&&this.emit("keyRatcheted",e.material,t,this.participantId)}))}setCurrentKeyIndex(e){return v(this,void 0,void 0,(function*(){this.currentKeyIndex=e%this.cryptoKeyRing.length}))}isEnabled(){return this.enabled}getCurrentKeyIndex(){return this.currentKeyIndex}getKeySet(e){return this.cryptoKeyRing[null!=e?e:this.currentKeyIndex]}}const T=[],A=new Map;let x,P,R=[],U=!1,F=!1,M=u;function O(e,t){let n=T.find((e=>e.getTrackId()===t));if(n)e!==n.getParticipantId()&&n.setParticipant(e,D(e));else{if(c.info("creating new cryptor for",{participantId:e}),!M)throw Error("Missing keyProvider options");n=new L({participantId:e,keys:D(e),keyProviderOptions:M}),B(n),T.push(n)}return n}function D(e){if(!e)return x;let t=A.get(e);return t||(t=new C(e,!0,M),P&&t.setKeyFromMaterial(P),A.set(e,t)),t}function N(e){let t=R.find((t=>t.getTrackId()===e));if(!t){if(!M)throw new TypeError("Missing keyProvider options");t=new L({keys:x,participantId:"publisher",keyProviderOptions:M}),B(t),R.push(t)}return t}function B(e){e.on("cryptorError",(e=>{const t={kind:"error",data:{error:new Error("".concat(p[e.reason],": ").concat(e.message))}};postMessage(t)}))}function X(e,t){postMessage({kind:"ratchetKey",data:{keyIndex:t,material:e}})}c.setDefaultLevel("info"),onmessage=e=>{const{kind:t,data:n}=e.data;switch(t){case"init":c.info("worker initialized"),M=n.keyProviderOptions,F=!!n.keyProviderOptions.sharedKey;const s={kind:"enable",data:{enabled:U}};x=new C(void 0,U,M),x.on("keyRatcheted",X),postMessage(s);break;case"enable":o=n.enabled,(a=n.participantId)?D(a).setEnabled(o):(U=o,x.setEnabled(o)),c.info("updated e2ee enabled status"),postMessage(e.data);break;case"decode":O(n.participantId,n.trackId).setupTransform(t,n.readableStream,n.writableStream,n.trackId,n.codec);break;case"encode":N(n.trackId).setupTransform(t,n.readableStream,n.writableStream,n.trackId,n.codec);break;case"setKey":F?(c.debug("set shared key"),function(e,t){c.debug("setting shared key"),P=e,null==x||x.setKeyFromMaterial(e,t);for(const[,n]of A)n.setKeyFromMaterial(e,t)}(n.key,n.keyIndex)):n.participantId?D(n.participantId).setKeyFromMaterial(n.key,n.keyIndex):c.error("no participant Id was provided and shared key usage is disabled");break;case"removeTransform":r=n.trackId,null===(i=T.find((e=>e.getTrackId()===r)))||void 0===i||i.unsetParticipant();break;case"updateCodec":O(n.participantId,n.trackId).setVideoCodec(n.codec);break;case"setRTPMap":R.forEach((e=>{e.setRtpMap(n.map)}));break;case"ratchetRequest":D(n.participantId).ratchetKey(n.keyIndex)}var r,i,o,a},self.RTCTransformEvent&&(c.debug("setup transform event"),self.onrtctransform=e=>{const t=e.transformer;c.debug("transformer",t),t.handled=!0;const{kind:n,participantId:r,trackId:i,codec:o}=t.options,a="encode"===n?N(i):O(r,i);c.debug("transform",{codec:o}),a.setupTransform(n,t.readable,t.writable,i,o)})}));
|
1
|
+
!function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";function e(e,t,n,r){return new(n||(n=Promise))((function(i,o){function a(e){try{c(r.next(e))}catch(e){o(e)}}function s(e){try{c(r.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,s)}c((r=r.apply(e,t||[])).next())}))}var t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function n(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var r,i,o,a={exports:{}};i=t,o=function(){var e=function(){},t="undefined",n=typeof window!==t&&typeof window.navigator!==t&&/Trident\/|MSIE /.test(window.navigator.userAgent),r=["trace","debug","info","warn","error"];function i(e,t){var n=e[t];if("function"==typeof n.bind)return n.bind(e);try{return Function.prototype.bind.call(n,e)}catch(t){return function(){return Function.prototype.apply.apply(n,[e,arguments])}}}function o(){console.log&&(console.log.apply?console.log.apply(console,arguments):Function.prototype.apply.apply(console.log,[console,arguments])),console.trace&&console.trace()}function a(t,n){for(var i=0;i<r.length;i++){var o=r[i];this[o]=i<t?e:this.methodFactory(o,t,n)}this.log=this.debug}function s(e,n,r){return function(){typeof console!==t&&(a.call(this,n,r),this[e].apply(this,arguments))}}function c(r,a,c){return function(r){return"debug"===r&&(r="log"),typeof console!==t&&("trace"===r&&n?o:void 0!==console[r]?i(console,r):void 0!==console.log?i(console,"log"):e)}(r)||s.apply(this,arguments)}function d(e,n,i){var o,s=this;n=null==n?"WARN":n;var d="loglevel";function l(){var e;if(typeof window!==t&&d){try{e=window.localStorage[d]}catch(e){}if(typeof e===t)try{var n=window.document.cookie,r=n.indexOf(encodeURIComponent(d)+"=");-1!==r&&(e=/^([^;]+)/.exec(n.slice(r))[1])}catch(e){}return void 0===s.levels[e]&&(e=void 0),e}}"string"==typeof e?d+=":"+e:"symbol"==typeof e&&(d=void 0),s.name=e,s.levels={TRACE:0,DEBUG:1,INFO:2,WARN:3,ERROR:4,SILENT:5},s.methodFactory=i||c,s.getLevel=function(){return o},s.setLevel=function(n,i){if("string"==typeof n&&void 0!==s.levels[n.toUpperCase()]&&(n=s.levels[n.toUpperCase()]),!("number"==typeof n&&n>=0&&n<=s.levels.SILENT))throw"log.setLevel() called with invalid level: "+n;if(o=n,!1!==i&&function(e){var n=(r[e]||"silent").toUpperCase();if(typeof window!==t&&d){try{return void(window.localStorage[d]=n)}catch(e){}try{window.document.cookie=encodeURIComponent(d)+"="+n+";"}catch(e){}}}(n),a.call(s,n,e),typeof console===t&&n<s.levels.SILENT)return"No console available for logging"},s.setDefaultLevel=function(e){n=e,l()||s.setLevel(e,!1)},s.resetLevel=function(){s.setLevel(n,!1),function(){if(typeof window!==t&&d){try{return void window.localStorage.removeItem(d)}catch(e){}try{window.document.cookie=encodeURIComponent(d)+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC"}catch(e){}}}()},s.enableAll=function(e){s.setLevel(s.levels.TRACE,e)},s.disableAll=function(e){s.setLevel(s.levels.SILENT,e)};var y=l();null==y&&(y=n),s.setLevel(y,!1)}var l=new d,y={};l.getLogger=function(e){if("symbol"!=typeof e&&"string"!=typeof e||""===e)throw new TypeError("You must supply a name when creating a logger.");var t=y[e];return t||(t=y[e]=new d(e,l.getLevel(),l.methodFactory)),t};var u=typeof window!==t?window.log:void 0;return l.noConflict=function(){return typeof window!==t&&window.log===l&&(window.log=u),l},l.getLoggers=function(){return y},l.default=l,l},(r=a).exports?r.exports=o():i.log=o();var s,c=a.exports;!function(e){e[e.trace=0]="trace",e[e.debug=1]="debug",e[e.info=2]="info",e[e.warn=3]="warn",e[e.error=4]="error",e[e.silent=5]="silent"}(s||(s={}));c.getLogger("livekit").setDefaultLevel(s.info);const d=c.getLogger("lk-e2ee"),l="AES-GCM",y={key:10,delta:3,audio:1,empty:0},u={sharedKey:!1,ratchetSalt:"LKFrameEncryptionKey",ratchetWindowSize:8};class h extends Error{constructor(e,t){super(t||"an error has occured"),this.code=e}}var p,f;!function(e){e.PermissionDenied="PermissionDenied",e.NotFound="NotFound",e.DeviceInUse="DeviceInUse",e.Other="Other"}(p||(p={})),function(e){e.getFailure=function(t){if(t&&"name"in t)return"NotFoundError"===t.name||"DevicesNotFoundError"===t.name?e.NotFound:"NotAllowedError"===t.name||"PermissionDeniedError"===t.name?e.PermissionDenied:"NotReadableError"===t.name||"TrackStartError"===t.name?e.DeviceInUse:e.Other}}(p||(p={})),function(e){e[e.InvalidKey=0]="InvalidKey",e[e.MissingKey=1]="MissingKey",e[e.InternalError=2]="InternalError"}(f||(f={}));class v extends h{constructor(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:f.InternalError;super(40,e),this.reason=t}}var g={exports:{}};!function(e){var t=Object.prototype.hasOwnProperty,n="~";function r(){}function i(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function o(e,t,r,o,a){if("function"!=typeof r)throw new TypeError("The listener must be a function");var s=new i(r,o||e,a),c=n?n+t:t;return e._events[c]?e._events[c].fn?e._events[c]=[e._events[c],s]:e._events[c].push(s):(e._events[c]=s,e._eventsCount++),e}function a(e,t){0==--e._eventsCount?e._events=new r:delete e._events[t]}function s(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),s.prototype.eventNames=function(){var e,r,i=[];if(0===this._eventsCount)return i;for(r in e=this._events)t.call(e,r)&&i.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?i.concat(Object.getOwnPropertySymbols(e)):i},s.prototype.listeners=function(e){var t=n?n+e:e,r=this._events[t];if(!r)return[];if(r.fn)return[r.fn];for(var i=0,o=r.length,a=new Array(o);i<o;i++)a[i]=r[i].fn;return a},s.prototype.listenerCount=function(e){var t=n?n+e:e,r=this._events[t];return r?r.fn?1:r.length:0},s.prototype.emit=function(e,t,r,i,o,a){var s=n?n+e:e;if(!this._events[s])return!1;var c,d,l=this._events[s],y=arguments.length;if(l.fn){switch(l.once&&this.removeListener(e,l.fn,void 0,!0),y){case 1:return l.fn.call(l.context),!0;case 2:return l.fn.call(l.context,t),!0;case 3:return l.fn.call(l.context,t,r),!0;case 4:return l.fn.call(l.context,t,r,i),!0;case 5:return l.fn.call(l.context,t,r,i,o),!0;case 6:return l.fn.call(l.context,t,r,i,o,a),!0}for(d=1,c=new Array(y-1);d<y;d++)c[d-1]=arguments[d];l.fn.apply(l.context,c)}else{var u,h=l.length;for(d=0;d<h;d++)switch(l[d].once&&this.removeListener(e,l[d].fn,void 0,!0),y){case 1:l[d].fn.call(l[d].context);break;case 2:l[d].fn.call(l[d].context,t);break;case 3:l[d].fn.call(l[d].context,t,r);break;case 4:l[d].fn.call(l[d].context,t,r,i);break;default:if(!c)for(u=1,c=new Array(y-1);u<y;u++)c[u-1]=arguments[u];l[d].fn.apply(l[d].context,c)}}return!0},s.prototype.on=function(e,t,n){return o(this,e,t,n,!1)},s.prototype.once=function(e,t,n){return o(this,e,t,n,!0)},s.prototype.removeListener=function(e,t,r,i){var o=n?n+e:e;if(!this._events[o])return this;if(!t)return a(this,o),this;var s=this._events[o];if(s.fn)s.fn!==t||i&&!s.once||r&&s.context!==r||a(this,o);else{for(var c=0,d=[],l=s.length;c<l;c++)(s[c].fn!==t||i&&!s[c].once||r&&s[c].context!==r)&&d.push(s[c]);d.length?this._events[o]=1===d.length?d[0]:d:a(this,o)}return this},s.prototype.removeAllListeners=function(e){var t;return e?(t=n?n+e:e,this._events[t]&&a(this,t)):(this._events=new r,this._eventsCount=0),this},s.prototype.off=s.prototype.removeListener,s.prototype.addListener=s.prototype.on,s.prefixed=n,s.EventEmitter=s,e.exports=s}(g);var w=n(g.exports);const m="cryptorError";var I;function b(e,t){const n=(new TextEncoder).encode(t);switch(e){case"HKDF":return{name:"HKDF",salt:n,hash:"SHA-256",info:new ArrayBuffer(128)};case"PBKDF2":return{name:"PBKDF2",salt:n,hash:"SHA-256",iterations:1e5};default:throw new Error("algorithm ".concat(e," is currently unsupported"))}}function k(t,n){return e(this,void 0,void 0,(function*(){const e=b(t.algorithm.name,n),r=yield crypto.subtle.deriveKey(e,t,{name:l,length:128},!1,["encrypt","decrypt"]);return{material:t,encryptionKey:r}}))}!function(e){e.telephone={maxBitrate:12e3},e.speech={maxBitrate:2e4},e.music={maxBitrate:32e3},e.musicStereo={maxBitrate:48e3},e.musicHighQuality={maxBitrate:64e3},e.musicHighQualityStereo={maxBitrate:96e3}}(I||(I={}));class E extends w{encodeFunction(e,t){throw Error("not implemented for subclass")}decodeFunction(e,t){throw Error("not implemented for subclass")}}class L extends E{constructor(e){var t;super(),this.sendCounts=new Map,this.keys=e.keys,this.participantId=e.participantId,this.rtpMap=new Map,this.keyProviderOptions=e.keyProviderOptions,this.unencryptedFrameByteTrailer=null!==(t=e.unencryptedFrameBytes)&&void 0!==t?t:(new TextEncoder).encode("LKROCKS")}setParticipant(e,t){this.participantId=e,this.keys=t}unsetParticipant(){this.participantId=void 0}getParticipantId(){return this.participantId}getTrackId(){return this.trackId}setVideoCodec(e){this.videoCodec=e}setRtpMap(e){this.rtpMap=e}setupTransform(e,t,n,r,i){i&&(console.info("setting codec on cryptor to",i),this.videoCodec=i);const o="encode"===e?this.encodeFunction:this.decodeFunction,a=new TransformStream({transform:o.bind(this)});t.pipeThrough(a).pipeTo(n).catch((e=>{console.error(e),this.emit("cryptorError",e instanceof v?e:new v(e.message))})),this.trackId=r}encodeFunction(t,n){var r;return e(this,void 0,void 0,(function*(){if(!this.keys.isEnabled()||0===t.data.byteLength)return n.enqueue(t);const{encryptionKey:e}=this.keys.getKeySet(),i=this.keys.getCurrentKeyIndex();if(e){const o=this.makeIV(null!==(r=t.getMetadata().synchronizationSource)&&void 0!==r?r:-1,t.timestamp),a=new Uint8Array(t.data,0,this.getUnencryptedBytes(t)),s=new Uint8Array(2);s[0]=12,s[1]=i;try{const r=yield crypto.subtle.encrypt({name:l,iv:o,additionalData:new Uint8Array(t.data,0,a.byteLength)},e,new Uint8Array(t.data,this.getUnencryptedBytes(t))),i=new ArrayBuffer(a.byteLength+r.byteLength+o.byteLength+s.byteLength),c=new Uint8Array(i);return c.set(a),c.set(new Uint8Array(r),a.byteLength),c.set(new Uint8Array(o),a.byteLength+r.byteLength),c.set(s,a.byteLength+r.byteLength+o.byteLength),t.data=i,n.enqueue(t)}catch(e){d.error(e)}}else this.emit(m,new v("encryption key missing for encoding",f.MissingKey))}))}decodeFunction(t,n){return e(this,void 0,void 0,(function*(){if(!this.keys.isEnabled()||0===t.data.byteLength||function(e,t){const n=new Uint8Array(e.slice(e.byteLength-t.byteLength));return t.every(((e,t)=>e===n[t]))}(t.data,this.unencryptedFrameByteTrailer))return n.enqueue(t);const e=new Uint8Array(t.data)[t.data.byteLength-1];if(this.keys.getKeySet(e)&&this.keys.hasValidKey)try{const r=yield this.decryptFrame(t,e);if(r)return n.enqueue(r)}catch(e){e instanceof v&&e.reason===f.InvalidKey?this.keys.hasValidKey&&(d.warn("invalid key"),this.emit(m,new v("invalid key for participant ".concat(this.participantId),f.InvalidKey)),this.keys.hasValidKey=!1):d.warn("decoding frame failed",{error:e})}return n.enqueue(t)}))}decryptFrame(t,n){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:void 0,i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{ratchetCount:0};var o;return e(this,void 0,void 0,(function*(){const e=this.keys.getKeySet(n);try{const n=new Uint8Array(t.data,0,this.getUnencryptedBytes(t)),r=new Uint8Array(t.data,t.data.byteLength-2,2),a=r[0],s=new Uint8Array(t.data,t.data.byteLength-a-r.byteLength,a),c=n.byteLength,d=t.data.byteLength-(n.byteLength+a+r.byteLength),y=yield crypto.subtle.decrypt({name:l,iv:s,additionalData:new Uint8Array(t.data,0,n.byteLength)},null!==(o=i.encryptionKey)&&void 0!==o?o:e.encryptionKey,new Uint8Array(t.data,c,d)),u=new ArrayBuffer(n.byteLength+y.byteLength),h=new Uint8Array(u);return h.set(new Uint8Array(t.data,0,n.byteLength)),h.set(new Uint8Array(y),n.byteLength),t.data=u,t}catch(o){if(!(this.keyProviderOptions.ratchetWindowSize>0))throw new v("Decryption failed, most likely because of an invalid key",f.InvalidKey);if(i.ratchetCount<this.keyProviderOptions.ratchetWindowSize){let o;if(d.debug("ratcheting key attempt ".concat(i.ratchetCount," of ").concat(this.keyProviderOptions.ratchetWindowSize,", for kind ").concat(t instanceof RTCEncodedAudioFrame?"audio":"video")),e===this.keys.getKeySet(n)){const e=yield this.keys.ratchetKey(n,!1);o=yield k(e,this.keyProviderOptions.ratchetSalt)}const a=yield this.decryptFrame(t,n,r||e,{ratchetCount:i.ratchetCount+1,encryptionKey:null==o?void 0:o.encryptionKey});return a&&o&&(this.keys.setKeySet(o,n,!0),this.keys.setCurrentKeyIndex(n)),a}r&&(d.debug("resetting to initial material"),this.keys.setKeyFromMaterial(r.material,n)),this.keys.hasValidKey=!1,d.warn("maximum ratchet attempts exceeded, resetting key"),this.emit(m,new v("valid key missing for participant ".concat(this.participantId),f.MissingKey))}}))}makeIV(e,t){var n;const r=new ArrayBuffer(12),i=new DataView(r);this.sendCounts.has(e)||this.sendCounts.set(e,Math.floor(65535*Math.random()));const o=null!==(n=this.sendCounts.get(e))&&void 0!==n?n:0;return i.setUint32(0,e),i.setUint32(4,t),i.setUint32(8,t-o%65535),this.sendCounts.set(e,o+1),r}getUnencryptedBytes(e){var t;if(function(e){return"type"in e}(e)){let n=null!==(t=this.getVideoCodec(e))&&void 0!==t?t:this.videoCodec;if("av1"===n||"vp9"===n)throw new Error("".concat(n," is not yet supported for end to end encryption"));if("vp8"===n)return y[e.type];const r=new Uint8Array(e.data);try{const e=function(e){const t=[];let n=0,r=0,i=e.length-2;for(;r<i;){for(;r<i&&(0!==e[r]||0!==e[r+1]||1!==e[r+2]);)r++;r>=i&&(r=e.length);let o=r;for(;o>n&&0===e[o-1];)o--;if(0===n){if(o!==n)throw TypeError("byte stream contains leading data")}else t.push(n);n=r+=3}return t}(r);if("h264"===n||e.some((e=>[_.SLICE_IDR,_.SLICE_NON_IDR].includes(S(r[e]))))){for(const t of e){switch(S(r[t])){case _.SLICE_IDR:case _.SLICE_NON_IDR:return t+2}}throw new TypeError("Could not find NALU")}}catch(e){}return y[e.type]}return y.audio}getVideoCodec(e){if(0===this.rtpMap.size)return;const t=e.getMetadata().payloadType;return t?this.rtpMap.get(t):void 0}}function S(e){return e&K}const K=31;var _;!function(e){e[e.SLICE_NON_IDR=1]="SLICE_NON_IDR",e[e.SLICE_PARTITION_A=2]="SLICE_PARTITION_A",e[e.SLICE_PARTITION_B=3]="SLICE_PARTITION_B",e[e.SLICE_PARTITION_C=4]="SLICE_PARTITION_C",e[e.SLICE_IDR=5]="SLICE_IDR",e[e.SEI=6]="SEI",e[e.SPS=7]="SPS",e[e.PPS=8]="PPS",e[e.AUD=9]="AUD",e[e.END_SEQ=10]="END_SEQ",e[e.END_STREAM=11]="END_STREAM",e[e.FILLER_DATA=12]="FILLER_DATA",e[e.SPS_EXT=13]="SPS_EXT",e[e.PREFIX_NALU=14]="PREFIX_NALU",e[e.SUBSET_SPS=15]="SUBSET_SPS",e[e.DPS=16]="DPS",e[e.SLICE_AUX=19]="SLICE_AUX",e[e.SLICE_EXT=20]="SLICE_EXT",e[e.SLICE_LAYER_EXT=21]="SLICE_LAYER_EXT"}(_||(_={}));class C extends w{constructor(e,t,n){super(),this.currentKeyIndex=0,this.cryptoKeyRing=new Array(16),this.enabled=t,this.keyProviderOptions=n,this.ratchetPromiseMap=new Map,this.participantId=e,this.hasValidKey=!1}setEnabled(e){this.enabled=e}ratchetKey(t){let n=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];const r=null!=t?t:t=this.getCurrentKeyIndex(),i=this.ratchetPromiseMap.get(r);if(void 0!==i)return i;const o=new Promise(((i,o)=>e(this,void 0,void 0,(function*(){try{const o=this.getKeySet(r).material,a=yield function(t){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{name:l},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"encrypt";return e(this,void 0,void 0,(function*(){return crypto.subtle.importKey("raw",t,n,!1,"derive"===r?["deriveBits","deriveKey"]:["encrypt","decrypt"])}))}(yield function(t,n){return e(this,void 0,void 0,(function*(){const e=b(t.algorithm.name,n);return crypto.subtle.deriveBits(e,t,256)}))}(o,this.keyProviderOptions.ratchetSalt),o.algorithm.name,"derive");n&&this.setKeyFromMaterial(a,r,!0),this.emit("keyRatcheted",a,t,this.participantId),i(a)}catch(e){o(e)}finally{this.ratchetPromiseMap.delete(r)}}))));return this.ratchetPromiseMap.set(r,o),o}setKey(t){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return e(this,void 0,void 0,(function*(){yield this.setKeyFromMaterial(t,n),this.hasValidKey=!0}))}setKeyFromMaterial(t){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,r=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return e(this,void 0,void 0,(function*(){d.debug("setting new key"),n>=0&&(this.currentKeyIndex=n%this.cryptoKeyRing.length);const e=yield k(t,this.keyProviderOptions.ratchetSalt);this.setKeySet(e,this.currentKeyIndex,r)}))}setKeySet(t,n){let r=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return e(this,void 0,void 0,(function*(){this.cryptoKeyRing[n%this.cryptoKeyRing.length]=t,r&&this.emit("keyRatcheted",t.material,n,this.participantId)}))}setCurrentKeyIndex(t){return e(this,void 0,void 0,(function*(){this.currentKeyIndex=t%this.cryptoKeyRing.length,this.hasValidKey=!0}))}isEnabled(){return this.enabled}getCurrentKeyIndex(){return this.currentKeyIndex}getKeySet(e){return this.cryptoKeyRing[null!=e?e:this.currentKeyIndex]}}const T=[],A=new Map;let x,P,R=[],U=!1,O=!1,F=u;function D(e,t){let n=T.find((e=>e.getTrackId()===t));if(n)e!==n.getParticipantId()&&n.setParticipant(e,M(e));else{if(d.info("creating new cryptor for",{participantId:e}),!F)throw Error("Missing keyProvider options");n=new L({participantId:e,keys:M(e),keyProviderOptions:F}),B(n),T.push(n)}return n}function M(e){if(!e)return x;let t=A.get(e);return t||(t=new C(e,!0,F),P&&t.setKey(P),A.set(e,t)),t}function N(e){let t=R.find((t=>t.getTrackId()===e));if(!t){if(!F)throw new TypeError("Missing keyProvider options");t=new L({keys:x,participantId:"publisher",keyProviderOptions:F}),B(t),R.push(t)}return t}function B(e){e.on("cryptorError",(e=>{const t={kind:"error",data:{error:new Error("".concat(f[e.reason],": ").concat(e.message))}};postMessage(t)}))}function V(e,t){postMessage({kind:"ratchetKey",data:{keyIndex:t,material:e}})}d.setDefaultLevel("info"),onmessage=t=>{const{kind:n,data:r}=t.data;switch(n){case"init":d.info("worker initialized"),F=r.keyProviderOptions,O=!!r.keyProviderOptions.sharedKey;const c={kind:"enable",data:{enabled:U}};x=new C(void 0,U,F),x.on("keyRatcheted",V),postMessage(c);break;case"enable":a=r.enabled,(s=r.participantId)?M(s).setEnabled(a):(U=a,x.setEnabled(a)),d.info("updated e2ee enabled status"),postMessage(t.data);break;case"decode":D(r.participantId,r.trackId).setupTransform(n,r.readableStream,r.writableStream,r.trackId,r.codec);break;case"encode":N(r.trackId).setupTransform(n,r.readableStream,r.writableStream,r.trackId,r.codec);break;case"setKey":O?(d.debug("set shared key"),function(e,t){d.debug("setting shared key"),P=e,null==x||x.setKey(e,t);for(const[,n]of A)n.setKey(e,t)}(r.key,r.keyIndex)):r.participantId?M(r.participantId).setKey(r.key,r.keyIndex):d.error("no participant Id was provided and shared key usage is disabled");break;case"removeTransform":i=r.trackId,null===(o=T.find((e=>e.getTrackId()===i)))||void 0===o||o.unsetParticipant();break;case"updateCodec":D(r.participantId,r.trackId).setVideoCodec(r.codec);break;case"setRTPMap":R.forEach((e=>{e.setRtpMap(r.map)}));break;case"ratchetRequest":!function(t){e(this,void 0,void 0,(function*(){const e=M(t.participantId);yield e.ratchetKey(t.keyIndex),e.hasValidKey=!0}))}(r)}var i,o,a,s},self.RTCTransformEvent&&(d.debug("setup transform event"),self.onrtctransform=e=>{const t=e.transformer;d.debug("transformer",t),t.handled=!0;const{kind:n,participantId:r,trackId:i,codec:o}=t.options,a="encode"===n?N(i):D(r,i);d.debug("transform",{codec:o}),a.setupTransform(n,t.readable,t.writable,i,o)})}));
|
2
2
|
//# sourceMappingURL=livekit-client.e2ee.worker.js.map
|