(function (global) {
'use strict';
if (global.tvpipeCapture) return;
function lib() {
var L = {};
L.KIND_AUDIO = 2; L.KIND_H264 = 3; L.KIND_CONFIG = 4;
L.FLAG_KEY = 1; L.FLAG_ANNEXB = 2;
var LEVELS = [
[0x1e, 1620, 40500], [0x1f, 3600, 108000], [0x20, 5120, 216000],
[0x28, 8192, 245760], [0x2a, 8704, 522240], [0x32, 22080, 589824],
[0x33, 36864, 983040], [0x34, 36864, 2073600]
];
L.clock = function () { return Math.round((performance.timeOrigin + performance.now()) * 1000); };
L.hex2 = function (n) { return (n < 16 ? '0' : '') + n.toString(16).toUpperCase(); };
L.avcLevel = function (w, h, fps) {
var fs = Math.ceil(w / 16) * Math.ceil(h / 16), mbps = fs * fps;
for (var i = 0; i < LEVELS.length; i++) {
if (fs <= LEVELS[i][1] && mbps <= LEVELS[i][2]) return LEVELS[i][0];
}
return 0x34;
};
L.place = function (sw, sh, dw, dh) {
var even = function (v) { return Math.max(2, v - (v % 2)); };
sw = Math.max(1, sw); sh = Math.max(1, sh); dw = Math.max(2, dw); dh = Math.max(2, dh);
var w, h;
if (sw * dh >= sh * dw) {
w = dw;
h = Math.floor((sh * dw + Math.floor(sw / 2)) / sw);
} else {
w = Math.floor((sw * dh + Math.floor(sh / 2)) / sh);
h = dh;
}
w = even(Math.min(w, dw));
h = even(Math.min(h, dh));
var x = Math.floor((dw - w) / 2), y = Math.floor((dh - h) / 2);
return { x: x - (x % 2), y: y - (y % 2), width: w, height: h };
};
L.record = function (kind, pts, parts) {
var len = 0, i;
for (i = 0; i < parts.length; i++) len += parts[i].byteLength;
var buf = new Uint8Array(13 + len);
var dv = new DataView(buf.buffer);
dv.setUint8(0, kind);
pts = Math.max(0, Math.round(pts));
dv.setUint32(1, pts % 4294967296, true);
dv.setUint32(5, Math.floor(pts / 4294967296), true);
dv.setUint32(9, len, true);
var off = 13;
for (i = 0; i < parts.length; i++) {
buf.set(parts[i], off);
off += parts[i].byteLength;
}
return buf.buffer;
};
L.bytesOf = function (src) {
if (src instanceof ArrayBuffer) return new Uint8Array(src.slice(0));
return new Uint8Array(src.buffer.slice(src.byteOffset, src.byteOffset + src.byteLength));
};
L.sameBytes = function (a, b) {
if (!a || !b || a.length !== b.length) return false;
for (var i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
return true;
};
return L;
}
function engine(L, post, every) {
var MAX_BUFFERED = 4 << 20;
var IDLE_REPEAT_MS = 250;
var STATS_MS = 2000;
var WARMUP_LATE_MS = 150;
var WARMUP_MAX_MS = 3000;
var DEFAULT_PROFILES = ['42E0', '4D40', '6400'];
var plan = null, capture = null, enc = null, frameUs = 33333;
var lastFrame = null, lastEncodeAt = -1e15, needKey = true, sinceKey = 0;
var lastDesc = null, lastChunkTs = -1, sentKey = false, firstChunkAt = null;
var buffered = 0, canvas = null, g2d = null, sizeSent = false, stopped = false;
var timers = [], reader = null, track = null, keyAskedAt = null, warnedOrder = false;
var codec = '';
var stats = fresh();
function fresh() {
return {
since: L.clock(), source: 0, encoded: 0, repeated: 0, dropped: 0, chunks: 0, keys: 0,
bytes: 0, latencySum: 0, keyRequests: 0, warmup: 0, keyLatencyMs: null
};
}
function log(level, message) { post({ type: 'log', level: level, message: String(message) }); }
function fail(message) { post({ type: 'fail', message: String(message) }); stop(); }
function frame(f) {
if (stopped) { f.close(); return; }
if (!sizeSent) {
sizeSent = true;
post({ type: 'size', width: f.displayWidth, height: f.displayHeight });
}
if (lastFrame) lastFrame.close();
lastFrame = f;
if (!enc) return;
stats.source++;
var t = L.clock();
if (t - lastEncodeAt < frameUs - 2000 && !needKey) return;
encode(f, t);
}
function readTrack(tr) {
track = tr;
var r = new MediaStreamTrackProcessor({ track: tr }).readable.getReader();
reader = r;
var pump = function () {
r.read().then(function (x) {
if (x.done || stopped) {
if (x.value) x.value.close();
if (x.done && !stopped) post({ type: 'ended' });
return;
}
frame(x.value);
pump();
}, function () { if (!stopped) post({ type: 'ended' }); });
};
pump();
}
function pickCodec(p, profiles) {
var lvl = L.hex2(L.avcLevel(p.width, p.height, p.fps));
var base = {
width: p.width, height: p.height, bitrate: p.bitrate, framerate: p.fps,
latencyMode: 'realtime', avc: { format: 'avc' }
};
var i = 0;
var next = function () {
if (i >= profiles.length) {
return Promise.reject(new Error('this browser cannot encode H.264 at ' + p.width + 'x' + p.height));
}
var cfg = Object.assign({}, base, { codec: 'avc1.' + profiles[i++] + lvl });
return VideoEncoder.isConfigSupported(cfg).then(function (r) {
return r && r.supported ? cfg : next();
}, function () { return next(); });
};
return next();
}
function start(m) {
plan = m.plan;
capture = m.capture;
frameUs = 1e6 / (plan.fps || 30);
pickCodec(plan, m.profiles || DEFAULT_PROFILES).then(function (cfg) {
if (stopped) return;
codec = cfg.codec;
enc = new VideoEncoder({
output: chunk,
error: function (e) { fail('video encoder: ' + (e && e.message || e)); }
});
enc.configure(cfg);
post({ type: 'codec', codec: codec });
log('info', 'encoding ' + codec + ' ' + plan.width + 'x' + plan.height + '@' + plan.fps + ' ' +
Math.round(plan.bitrate / 1000) + ' kbit/s');
timers.push(every(IDLE_REPEAT_MS / 2, idle));
timers.push(every(STATS_MS, report));
if (lastFrame) encode(lastFrame, L.clock());
}, function (e) { fail(e && e.message || e); });
}
function idle() {
var t = L.clock();
if (lastFrame && t - lastEncodeAt >= IDLE_REPEAT_MS * 1000) {
stats.repeated++;
encode(lastFrame, t);
}
}
function keyframe() {
needKey = true;
stats.keyRequests++;
if (keyAskedAt === null) keyAskedAt = L.clock();
var t = L.clock();
if (lastFrame && t - lastEncodeAt >= frameUs / 2) encode(lastFrame, t);
}
function rect(w, h) {
if (plan.content && capture && w === capture.width && h === capture.height) return plan.content;
return L.place(w, h, plan.width, plan.height);
}
function drawn(src, r, t) {
var W = plan.width, H = plan.height;
if (!canvas) {
canvas = new OffscreenCanvas(W, H);
g2d = canvas.getContext('2d', { alpha: false, desynchronized: true });
g2d.imageSmoothingEnabled = true;
g2d.imageSmoothingQuality = 'high';
}
if (r.x > 0 || r.y > 0 || r.width < W || r.height < H) {
g2d.fillStyle = '#000';
g2d.fillRect(0, 0, W, H);
}
g2d.drawImage(src, r.x, r.y, r.width, r.height);
return new VideoFrame(canvas, { timestamp: t, alpha: 'discard' });
}
function encode(src, t) {
if (!enc || enc.state !== 'configured') return;
if (buffered > MAX_BUFFERED || enc.encodeQueueSize > 2) {
stats.dropped++;
return;
}
var f;
try {
var w = src.displayWidth, h = src.displayHeight, r = rect(w, h);
f = (r.x === 0 && r.y === 0 && w === plan.width && h === plan.height &&
r.width === w && r.height === h)
? new VideoFrame(src, { timestamp: t })
: drawn(src, r, t);
} catch (e) {
stats.dropped++;
return;
}
var key = needKey || sinceKey + 1 >= (plan.keyInterval || 30);
try {
enc.encode(f, { keyFrame: key });
} finally {
f.close();
}
if (key) { needKey = false; sinceKey = 0; } else { sinceKey++; }
lastEncodeAt = t;
stats.encoded++;
}
function chunk(c, md) {
if (stopped) return;
var cfg = md && md.decoderConfig;
if (cfg && cfg.description) {
var desc = L.bytesOf(cfg.description);
if (!L.sameBytes(desc, lastDesc)) {
lastDesc = desc;
var rec = L.record(L.KIND_CONFIG, c.timestamp, [new Uint8Array([0]), desc]);
post({ type: 'record', data: rec }, [rec]);
}
}
var now = L.clock(), late = (now - c.timestamp) / 1000;
if (!sentKey) {
if (firstChunkAt === null) firstChunkAt = now;
var patient = now - firstChunkAt < WARMUP_MAX_MS * 1000;
if (c.type !== 'key' || (late > WARMUP_LATE_MS && patient)) {
if (c.type === 'key') needKey = true;
stats.warmup++;
return;
}
}
var data = new Uint8Array(c.byteLength);
c.copyTo(data);
var flags = c.type === 'key' ? L.FLAG_KEY : 0;
if (!lastDesc && data.length > 4 && data[0] === 0 && data[1] === 0 &&
(data[2] === 1 || (data[2] === 0 && data[3] === 1))) flags |= L.FLAG_ANNEXB;
if (c.timestamp <= lastChunkTs && !warnedOrder) {
warnedOrder = true;
log('warn', 'the encoder reorders frames (B-frames?)');
}
lastChunkTs = c.timestamp;
var buf = L.record(L.KIND_H264, c.timestamp, [new Uint8Array([flags]), data]);
post({ type: 'record', data: buf }, [buf]);
if (flags & L.FLAG_KEY) {
stats.keys++;
if (keyAskedAt !== null) {
stats.keyLatencyMs = (now - keyAskedAt) / 1000;
keyAskedAt = null;
}
if (!sentKey) {
sentKey = true;
post({ type: 'keySent' });
}
}
stats.bytes += data.length;
stats.chunks++;
stats.latencySum += late;
}
function report() {
var s = stats, secs = (L.clock() - s.since) / 1e6;
if (secs <= 0) return;
post({
type: 'stats',
fps: +(s.chunks / secs).toFixed(1),
sourceFps: +(s.source / secs).toFixed(1),
kbps: Math.round(s.bytes * 8 / secs / 1000),
encodeMs: s.chunks ? +(s.latencySum / s.chunks).toFixed(1) : null,
dropped: s.dropped, repeated: s.repeated, keyRequests: s.keyRequests, keys: s.keys,
keyLatencyMs: s.keyLatencyMs, warmup: s.warmup,
queue: enc ? enc.encodeQueueSize : 0, buffered: buffered, codec: codec,
width: plan ? plan.width : 0, height: plan ? plan.height : 0
});
stats = fresh();
}
function stop() {
if (stopped) return;
stopped = true;
timers.forEach(function (cancel) { cancel(); });
timers = [];
if (reader) reader.cancel().catch(function () {});
if (track) track.stop();
if (enc && enc.state !== 'closed') { try { enc.close(); } catch (e) { } }
if (lastFrame) { lastFrame.close(); lastFrame = null; }
}
return function (m) {
switch (m.type) {
case 'start': start(m); break;
case 'track': readTrack(m.track); break;
case 'frame': frame(m.frame); break;
case 'keyframe': keyframe(); break;
case 'buffered': buffered = m.bytes; break;
case 'stop': stop(); break;
}
};
}
var L = lib();
var VERSION = 1;
var STATS_MS = 2000;
var CAPTURE_WAIT_MS = 1500;
var MAX_BUFFERED = 4 << 20;
var WORKER_SOURCE =
'var L = (' + lib.toString() + ')();\n' +
'var handle = (' + engine.toString() + ')(L, function (m, t) { postMessage(m, t || []); },\n' +
' function (ms, fn) { var id = setInterval(fn, ms); return function () { clearInterval(id); }; });\n' +
'postMessage({ type: "hello", ok: typeof MediaStreamTrackProcessor === "function" &&\n' +
' typeof VideoEncoder === "function" && typeof OffscreenCanvas === "function" });\n' +
'onmessage = function (e) { handle(e.data); };\n';
function family() {
var ua = navigator.userAgent;
if (/Firefox\//.test(ua)) return 'firefox';
if (/Chrome\/|Chromium\/|Edg\//.test(ua)) return 'chromium';
if (/Version\/[\d.]+.*Safari\//.test(ua)) return 'safari';
return 'other';
}
function features() {
var fam = family();
return {
family: fam,
secure: !!global.isSecureContext,
displayMedia: !!(navigator.mediaDevices && navigator.mediaDevices.getDisplayMedia),
webcodecs: typeof global.VideoEncoder === 'function',
trackProcessor: typeof global.MediaStreamTrackProcessor === 'function',
audioWorklet: typeof global.AudioWorkletNode === 'function',
tabs: fam === 'chromium',
audio: fam === 'chromium'
};
}
var TICKER = 'var t=null;onmessage=function(e){clearInterval(t);if(e.data>0)t=setInterval(function(){postMessage(0)},e.data)}';
function every(ms, fn) {
var w = null, id = null;
var fallback = function () {
if (w) { w.terminate(); w = null; }
if (id === null) id = setInterval(fn, ms);
};
try {
var url = URL.createObjectURL(new Blob([TICKER], { type: 'text/javascript' }));
w = new Worker(url);
URL.revokeObjectURL(url);
w.onmessage = fn;
w.onerror = fallback;
w.postMessage(ms);
} catch (e) {
fallback();
}
return function () {
if (w) w.terminate();
if (id !== null) clearInterval(id);
w = null;
id = null;
};
}
var WORKLET = [
'class TvpipePcm extends AudioWorkletProcessor {',
' constructor(o) {',
' super();',
' var p = o.processorOptions;',
' this.ch = p.channels; this.chunk = p.chunk;',
' this.step = sampleRate / p.rate; this.t = 0;',
' this.prev = new Float32Array(this.ch);',
' this.out = new Int16Array(this.chunk * this.ch); this.n = 0;',
' this.on = true;',
' this.port.onmessage = (e) => { if (e.data === "stop") this.on = false; };',
' }',
' process(inputs) {',
' if (!this.on) return false;',
' var inp = inputs[0] || [];',
' var frames = inp.length ? inp[0].length : 128;',
' var ch = this.ch, prev = this.prev;',
' var s = function (c, k) {',
' if (!inp.length) return 0;',
' return k < 0 ? prev[c] : inp[Math.min(c, inp.length - 1)][k];',
' };',
' while (this.t < frames - 1) {',
' var i = Math.floor(this.t), f = this.t - i;',
' for (var c = 0; c < ch; c++) {',
' var v = s(c, i) + (s(c, i + 1) - s(c, i)) * f;',
' v = v < -1 ? -1 : v > 1 ? 1 : v;',
' this.out[this.n * ch + c] = Math.round(v * 32767);',
' }',
' if (++this.n === this.chunk) {',
' this.port.postMessage(this.out.buffer, [this.out.buffer]);',
' this.out = new Int16Array(this.chunk * ch); this.n = 0;',
' }',
' this.t += this.step;',
' }',
' this.t -= frames;',
' for (var c2 = 0; c2 < ch; c2++) prev[c2] = s(c2, frames - 1);',
' return true;',
' }',
'}',
'registerProcessor("tvpipe-pcm", TvpipePcm);'
].join('\n');
function Capture(url, opts) {
this.url = url;
this.opts = opts || {};
this.handlers = {};
this.config = null;
this.plan = null;
this.stream = null;
this.engine = null;
this.worker = null;
this.mode = '';
this.audioCtx = null;
this.audioNode = null;
this.sentKey = false;
this.timers = [];
this.stopped = false;
this.codec = '';
this.audioBytes = 0;
this.audioPeak = 0;
this.audioDropped = 0;
this.audioSince = L.clock();
this.ws = new WebSocket(url);
this.ws.binaryType = 'arraybuffer';
var self = this;
this.ws.onopen = function () {
self._send({
type: 'hello', version: VERSION, clock: L.clock(), agent: navigator.userAgent,
features: features()
});
self._emit('open');
};
this.ws.onmessage = function (e) {
if (typeof e.data !== 'string') return;
var m;
try { m = JSON.parse(e.data); } catch (err) { return; }
self._message(m);
};
this.ws.onclose = function (e) {
self._teardown();
self._emit('close', { code: e.code, reason: e.reason });
};
}
Capture.prototype.on = function (ev, fn) {
(this.handlers[ev] = this.handlers[ev] || []).push(fn);
return this;
};
Capture.prototype._emit = function (ev, data) {
var hs = this.handlers[ev] || [];
for (var i = 0; i < hs.length; i++) {
try { hs[i](data); } catch (e) { }
}
};
Capture.prototype._send = function (obj) {
if (this.ws.readyState === 1) this.ws.send(JSON.stringify(obj));
};
Capture.prototype._log = function (level, message) {
this._send({ type: 'log', level: level, message: String(message) });
this._emit('log', { level: level, message: String(message) });
};
Capture.prototype._fail = function (message) {
this._log('error', message);
this._emit('error', String(message));
this.stop(String(message));
};
Capture.prototype._toEngine = function (m, transfer) {
if (this.worker) this.worker.postMessage(m, transfer || []);
else if (this.engine) this.engine(m);
};
Capture.prototype._message = function (m) {
switch (m.type) {
case 'config':
this.config = m;
this._emit('config', m);
break;
case 'status':
this._emit('status', m);
break;
case 'plan':
this.plan = m;
this._emit('plan', m);
this._start(m);
break;
case 'keyframe':
this._toEngine({ type: 'keyframe' });
break;
case 'busy':
this._emit('error', 'another page is already sharing to tvpipe');
this._teardown();
break;
case 'end':
this._teardown();
this._emit('end', m.reason || 'tvpipe stopped');
break;
}
};
Capture.prototype._prepareAudio = function () {
if (this.audioCtx || !this.config || !this.config.audio) return;
var Ctx = global.AudioContext || global.webkitAudioContext;
if (!Ctx) return;
try {
this.audioCtx = new Ctx({ sampleRate: this.config.sampleRate, latencyHint: 'interactive' });
} catch (e) {
try { this.audioCtx = new Ctx(); } catch (e2) { this.audioCtx = null; }
}
if (this.audioCtx && this.audioCtx.state !== 'running') this.audioCtx.resume().catch(function () {});
};
Capture.prototype.captureDisplay = function (extra) {
if (!navigator.mediaDevices || !navigator.mediaDevices.getDisplayMedia) {
return Promise.reject(new Error('this browser cannot capture the screen (getDisplayMedia)'));
}
var cfg = this.config || { fps: 30, audio: true };
var o = this.opts, f = features();
if (o.preferCurrentTab && f.family === 'firefox') return this._captureFirefoxTab(cfg);
var video = { frameRate: { ideal: cfg.fps, max: cfg.fps } };
if (!f.tabs) video.displaySurface = 'window';
if (o.surface) video.displaySurface = o.surface;
var audio = cfg.audio && f.audio ? {
echoCancellation: false, noiseSuppression: false, autoGainControl: false,
channelCount: { ideal: 2 },
suppressLocalAudioPlayback: !o.localAudio
} : false;
var c = { video: video, audio: audio };
if (f.family === 'chromium') {
c.selfBrowserSurface = o.selfBrowserSurface || 'exclude';
c.surfaceSwitching = 'include';
c.systemAudio = 'include';
if (o.windowAudio) c.windowAudio = o.windowAudio;
if (o.preferCurrentTab) c.preferCurrentTab = true;
}
for (var k in (extra || {})) c[k] = extra[k];
if (audio) this._prepareAudio();
var self = this;
return navigator.mediaDevices.getDisplayMedia(c).then(function (stream) { return self.share(stream); });
};
Capture.prototype._captureFirefoxTab = function (cfg) {
var c = {
video: { mediaSource: 'browser', frameRate: { ideal: cfg.fps, max: cfg.fps } },
audio: cfg.audio ? { mediaSource: 'audioCapture' } : false
};
var self = this;
return navigator.mediaDevices.getUserMedia(c).then(function (stream) {
self.surface = 'browser';
return self.share(stream);
});
};
Capture.prototype.share = function (stream) {
if (this.stream) return Promise.reject(new Error('already sharing'));
if (typeof global.VideoEncoder !== 'function') {
stream.getTracks().forEach(function (t) { t.stop(); });
return Promise.reject(new Error('this browser has no WebCodecs VideoEncoder'));
}
var vt = stream.getVideoTracks()[0];
if (!vt) return Promise.reject(new Error('the stream has no video'));
this.stream = stream;
var self = this;
var at = stream.getAudioTracks()[0];
if (at) at.addEventListener('ended', function () { self._log('info', 'the shared audio ended'); });
vt.addEventListener('ended', function () { self._stoppedInBrowser(); });
var s = vt.getSettings ? vt.getSettings() : {};
this.captureInfo = {
type: 'capture',
width: s.width || 0, height: s.height || 0, fps: s.frameRate || null,
audio: !!at, surface: s.displaySurface || this.surface || null, label: vt.label || null
};
this.timers.push(every(CAPTURE_WAIT_MS, function () { self._sendCapture(null); }));
this.timers.push(every(100, function () {
self._toEngine({ type: 'buffered', bytes: self.ws.bufferedAmount });
}));
return this._startEngine(vt);
};
Capture.prototype._startEngine = function (vt) {
var self = this;
var onEngine = function (m) { self._fromEngine(m); };
var inPage = function () {
self.mode = 'page';
self.engine = engine(L, onEngine, every);
self._pageSource(vt);
};
if (this.opts.worker === false || typeof global.Worker !== 'function') {
inPage();
return Promise.resolve();
}
return new Promise(function (resolve) {
var w, url;
try {
url = URL.createObjectURL(new Blob([WORKER_SOURCE], { type: 'text/javascript' }));
w = new Worker(url);
URL.revokeObjectURL(url);
} catch (e) {
inPage();
resolve();
return;
}
var settled = false;
var settle = function (ok) {
if (settled) return;
settled = true;
if (!ok) { w.terminate(); inPage(); }
resolve();
};
setTimeout(function () { settle(false); }, 2000);
w.onerror = function () { settle(false); };
w.onmessage = function (e) {
var m = e.data;
if (m.type !== 'hello') { onEngine(m); return; }
if (!m.ok) { settle(false); return; }
try {
w.postMessage({ type: 'track', track: vt }, [vt]);
} catch (err) {
settle(false);
return;
}
self.worker = w;
self.mode = 'worker';
settle(true);
};
});
};
Capture.prototype._pageSource = function (track) {
var self = this;
var feed = function (f) { self._toEngine({ type: 'frame', frame: f }); };
if (typeof global.MediaStreamTrackProcessor === 'function') {
var reader = new global.MediaStreamTrackProcessor({ track: track }).readable.getReader();
this.reader = reader;
var pump = function () {
reader.read().then(function (r) {
if (r.done || self.stopped) { if (r.value) r.value.close(); return; }
feed(r.value);
pump();
}, function () {});
};
pump();
return;
}
var v = document.createElement('video');
v.muted = true;
v.playsInline = true;
v.autoplay = true;
v.srcObject = new MediaStream([track]);
v.style.cssText = 'position:fixed;left:-10px;top:-10px;width:2px;height:2px;opacity:0;pointer-events:none';
(document.body || document.documentElement).appendChild(v);
this.videoEl = v;
var grab = function () {
if (self.stopped || v.readyState < 2 || !v.videoWidth) return;
var f;
try { f = new VideoFrame(v, { timestamp: L.clock() }); } catch (e) { return; }
feed(f);
};
v.play().catch(function () {});
var fps = (this.config && this.config.fps) || 30;
var interval = Math.max(5, Math.round(1000 / fps)), lastShown = -1e9;
if (typeof v.requestVideoFrameCallback === 'function') {
var cb = function () {
lastShown = performance.now();
grab();
if (!self.stopped) v.requestVideoFrameCallback(cb);
};
v.requestVideoFrameCallback(cb);
}
this.timers.push(every(interval, function () {
if (performance.now() - lastShown > 2 * interval) grab();
}));
};
Capture.prototype._fromEngine = function (m) {
switch (m.type) {
case 'size': this._sendCapture(m); break;
case 'record':
if (!this.stopped && this.ws.readyState === 1) this.ws.send(m.data);
break;
case 'keySent': this.sentKey = true; break;
case 'codec': this.codec = m.codec; break;
case 'log': this._log(m.level, m.message); break;
case 'fail': this._fail(m.message); break;
case 'ended': this._stoppedInBrowser(); break;
case 'stats': this._stats(m); break;
}
};
Capture.prototype._sendCapture = function (size) {
if (this.captureSent || this.stopped || !this.captureInfo) return;
var c = this.captureInfo;
if (size) {
c.width = size.width;
c.height = size.height;
}
if (!c.width || !c.height) return;
this.captureSent = true;
this._send(c);
this._emit('capture', {
settings: { width: c.width, height: c.height, frameRate: c.fps, displaySurface: c.surface },
audio: c.audio, mode: this.mode
});
};
Capture.prototype._start = function (plan) {
if (!this.stream) { this._fail('plan before a capture'); return; }
var c = this.captureInfo;
this._toEngine({
type: 'start', plan: plan, capture: { width: c.width, height: c.height },
profiles: this.opts.profiles
});
var self = this;
this._startAudio(plan).catch(function (e) {
self._log('warn', 'no audio: ' + (e && e.message || e));
});
};
Capture.prototype._startAudio = function (plan) {
var track = this.stream.getAudioTracks()[0];
if (!track || !plan.audio) return Promise.resolve();
if (typeof global.AudioWorkletNode !== 'function') return Promise.reject(new Error('no AudioWorklet'));
if (!this.audioCtx) this._prepareAudio();
if (!this.audioCtx) return Promise.reject(new Error('no AudioContext'));
var self = this, rate = plan.audio.sampleRate, ch = plan.audio.channels;
var url = URL.createObjectURL(new Blob([WORKLET], { type: 'text/javascript' }));
var connect = function () {
var ctx = self.audioCtx;
var src = ctx.createMediaStreamSource(new MediaStream([track]));
var node = new AudioWorkletNode(ctx, 'tvpipe-pcm', {
numberOfInputs: 1, numberOfOutputs: 1, outputChannelCount: [1],
processorOptions: { rate: rate, channels: ch, chunk: Math.round(rate / 100) }
});
node.port.onmessage = function (e) {
if (self.stopped || self.ws.readyState !== 1 || !self.sentKey) return;
var pcm = new Uint8Array(e.data);
var dur = pcm.length / (2 * ch) / rate * 1e6;
var smp = new Int16Array(e.data), peak = self.audioPeak;
for (var i = 0; i < smp.length; i++) {
var a = smp[i] < 0 ? -smp[i] : smp[i];
if (a > peak) peak = a;
}
self.audioPeak = peak;
if (self.ws.bufferedAmount > MAX_BUFFERED) { self.audioDropped++; return; }
self.ws.send(L.record(L.KIND_AUDIO, L.clock() - dur, [pcm]));
self.audioBytes += pcm.length;
};
src.connect(node);
node.connect(family() === 'firefox' ? ctx.createMediaStreamDestination() : ctx.destination);
self.audioNode = node;
self.audioSrc = src;
if (ctx.state !== 'running') ctx.resume().catch(function () {});
self._log('info', 'audio: ' + ctx.sampleRate + ' Hz context, sent as ' + rate + ' Hz ' + ch + ' ch');
};
var load = function () {
var w = self.audioCtx.audioWorklet;
return w.addModule(url).catch(function () {
return w.addModule('data:text/javascript;base64,' + btoa(WORKLET));
});
};
return load().then(function () {
try {
connect();
} catch (e) {
self.audioCtx.close().catch(function () {});
self.audioCtx = new (global.AudioContext || global.webkitAudioContext)();
return load().then(connect);
}
}).then(function () { URL.revokeObjectURL(url); });
};
Capture.prototype._stats = function (v) {
var secs = (L.clock() - this.audioSince) / 1e6;
v.audioKbps = secs > 0 ? Math.round(this.audioBytes * 8 / secs / 1000) : 0;
v.audioDropped = this.audioDropped;
v.audioPeak = Math.round((this.audioPeak || 0) / 32.767) / 1000;
v.mode = this.mode;
this.audioPeak = 0;
this.audioBytes = 0;
this.audioDropped = 0;
this.audioSince = L.clock();
this._send(v);
this._emit('stats', v);
};
Capture.prototype._stoppedInBrowser = function () {
if (!this.stopped) this.stoppedInBrowser = true;
this.stop('the share was stopped in the browser');
};
Capture.prototype.stop = function (reason) {
if (this.stopped) return;
this._send({ type: 'ended', reason: reason || 'the page stopped sharing' });
this._teardown();
var ws = this.ws;
setTimeout(function () { try { ws.close(1000, 'ended'); } catch (e) { } }, 100);
this._emit('end', reason || 'stopped');
};
Capture.prototype._teardown = function () {
if (this.stopped) return;
this.stopped = true;
this.timers.forEach(function (cancel) { cancel(); });
this.timers = [];
this._toEngine({ type: 'stop' });
var w = this.worker;
if (w) setTimeout(function () { w.terminate(); }, 200);
if (this.reader) this.reader.cancel().catch(function () {});
if (this.stream) {
this.stream.getTracks().forEach(function (t) { try { t.stop(); } catch (e) { } });
}
if (this.audioNode) { this.audioNode.port.postMessage('stop'); this.audioNode.disconnect(); }
if (this.audioSrc) this.audioSrc.disconnect();
if (this.audioCtx) this.audioCtx.close().catch(function () {});
if (this.videoEl) this.videoEl.remove();
};
global.tvpipeCapture = {
version: VERSION,
features: features,
place: L.place,
connect: function (url, opts) { return new Capture(url, opts); }
};
})(typeof globalThis !== 'undefined' ? globalThis : window);