218 lines
14 KiB
JavaScript
218 lines
14 KiB
JavaScript
// Capture only this test's own window; no desktop or unrelated application image.
|
|
import assert from "node:assert/strict";
|
|
import { spawn } from "node:child_process";
|
|
import { once } from "node:events";
|
|
import { randomUUID } from "node:crypto";
|
|
import { writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
|
|
export async function nativePreviews({ room, browser, action, view, evaluate, until, playing, progressing, hook, output, directory }) {
|
|
assert.equal(process.platform, "win32", "This fixture needs an interactive Windows desktop");
|
|
const title = `Piik preview fixture ${randomUUID()}`;
|
|
const script = join(directory, "preview-window.ps1");
|
|
await writeFile(script, `param([string]$Title)
|
|
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
|
|
$form = New-Object System.Windows.Forms.Form
|
|
$form.Text = $Title
|
|
$form.ClientSize = New-Object System.Drawing.Size(640,360)
|
|
$form.StartPosition = 'Manual'
|
|
$form.Location = New-Object System.Drawing.Point(80,80)
|
|
$form.ShowInTaskbar = $false
|
|
$font = New-Object System.Drawing.Font('Segoe UI',28)
|
|
$form.add_Paint({ param($sender,$paint)
|
|
$paint.Graphics.DrawString('Piik native preview',$font,[System.Drawing.Brushes]::White,45,130)
|
|
})
|
|
$replacement = New-Object System.Windows.Forms.Form
|
|
$replacement.Text = $Title + ' replacement'
|
|
$replacement.ClientSize = New-Object System.Drawing.Size(640,360)
|
|
$replacement.StartPosition = 'Manual'
|
|
$replacement.Location = New-Object System.Drawing.Point(780,80)
|
|
$replacement.ShowInTaskbar = $false
|
|
$replacement.add_Paint({ param($sender,$paint)
|
|
$paint.Graphics.DrawString('Piik replacement source',$font,[System.Drawing.Brushes]::White,35,130)
|
|
})
|
|
$timer = New-Object System.Windows.Forms.Timer
|
|
$timer.Interval = 100
|
|
$timer.add_Tick({
|
|
$tick = [DateTime]::UtcNow.Millisecond
|
|
$form.BackColor = [System.Drawing.Color]::FromArgb(30,80 + [int]($tick / 10),120)
|
|
$replacement.BackColor = [System.Drawing.Color]::FromArgb(100,40,80 + [int]($tick / 10))
|
|
})
|
|
try { $replacement.Show(); $timer.Start(); [System.Windows.Forms.Application]::Run($form) }
|
|
finally { $timer.Dispose(); $font.Dispose(); $replacement.Dispose(); $form.Dispose() }
|
|
`);
|
|
const fixture = spawn("powershell.exe", ["-NoProfile", "-STA", "-ExecutionPolicy", "Bypass", "-File", script, title],
|
|
{ windowsHide: true, stdio: ["ignore", "ignore", "pipe"] });
|
|
let errors = "";
|
|
fixture.stderr.on("data", chunk => { errors += chunk; });
|
|
try {
|
|
const target = await until("owned preview window is selectable", async () => {
|
|
if (fixture.exitCode !== null) throw new Error(errors);
|
|
const sources = await action(1, "nativeSources()");
|
|
return sources.sources.find(source => source.title === title);
|
|
}, Boolean);
|
|
const source = JSON.stringify(target);
|
|
const codecSample = publication => evaluate(0, `(async () => {
|
|
const stream = ${hook}.snapshot().streams.get(${JSON.stringify(publication)});
|
|
const video = stream?.getVideoTracks().find(track => track.readyState === 'live');
|
|
const pc = window.__channelProbe.peers.find(pc => pc.connectionState === 'connected'
|
|
&& pc.getReceivers().some(receiver => receiver.track === video));
|
|
const receiver = pc?.getReceivers().find(receiver => receiver.track === video);
|
|
if (!receiver) return null;
|
|
const stats = await receiver.getStats();
|
|
if (${hook}.snapshot().streams.get(${JSON.stringify(publication)}) !== stream) return null;
|
|
return { trackId: video.id, reports: [...stats.values()].filter(report => report.type === 'inbound-rtp'
|
|
&& report.kind === 'video' && report.isRemote !== true).map(report => ({ id: report.id,
|
|
mimeType: stats.get(report.codecId)?.mimeType, frames: report.framesDecoded, bytes: report.bytesReceived,
|
|
width: report.frameWidth, height: report.frameHeight })) };
|
|
})()`);
|
|
const codecProgressing = async (publication, codec, size) => {
|
|
let before = null;
|
|
return until(`current Native ${codec} receiver decodes advancing frames`, () => codecSample(publication), current => {
|
|
const advancing = current && before?.trackId === current.trackId && current.reports.some(report => {
|
|
const previous = before.reports.find(previous => previous.id === report.id);
|
|
return report.mimeType?.toLowerCase() === `video/${codec}` && previous
|
|
&& report.frames > previous.frames && report.bytes > previous.bytes
|
|
&& (!size || report.width <= size.width && report.height <= size.height);
|
|
});
|
|
before = current;
|
|
return advancing;
|
|
});
|
|
};
|
|
await evaluate(1, `(() => {
|
|
const app = ${hook}.session.app, prepare = app.prepareCapture;
|
|
window.__previewCodecs = { acknowledgements: [], restore: () => { app.prepareCapture = prepare; } };
|
|
app.prepareCapture = async (...args) => {
|
|
const prepared = await prepare.apply(app, args);
|
|
window.__previewCodecs.acknowledgements.push({ mode: args[5], codec: prepared.codec });
|
|
return prepared;
|
|
};
|
|
return true;
|
|
})()`);
|
|
const first = await evaluate(1, `(async () => {
|
|
const started = performance.now();
|
|
const data = await ${hook}.session.nativePreview(${source});
|
|
if (!data) throw new Error('Owned window produced no preview');
|
|
const image = new Image(); image.src = data; await image.decode();
|
|
return {data,width:image.naturalWidth,height:image.naturalHeight,elapsed:performance.now()-started};
|
|
})()`);
|
|
assert(first.width > 0 && first.width <= 320 && first.height > 0 && first.height <= 180);
|
|
assert(first.data.length <= 87407);
|
|
await writeFile(join(output, "native-preview.jpg"), Buffer.from(first.data.split(",")[1], "base64"));
|
|
|
|
await action(1, `startNativeScreen(${JSON.stringify(room)}, ${source}, false)`);
|
|
const shared = await until("Native source is published", () => view(0), value => value.state.publications.some(publication => publication.roomId === room));
|
|
const publication = shared.state.publications.find(publication => publication.roomId === room).id;
|
|
await action(0, `browse(${JSON.stringify(room)})`);
|
|
await action(0, `watch(${JSON.stringify(publication)})`);
|
|
await playing(0, publication);
|
|
const automatic = await evaluate(1, 'window.__previewCodecs.acknowledgements.at(-1)');
|
|
assert.equal(automatic.mode, 'auto');
|
|
const autoRtp = await codecProgressing(publication, automatic.codec);
|
|
|
|
const during = await evaluate(1, `(async () => {
|
|
const before = ${hook}.snapshot().state.publications.map(source => source.id);
|
|
const abort = new AbortController();
|
|
const cancelled = ${hook}.session.nativePreview(${source}, abort.signal);
|
|
await new Promise(resolve => setTimeout(resolve,25)); abort.abort();
|
|
const cancelledImage = await cancelled;
|
|
const data = await ${hook}.session.nativePreview(${source});
|
|
const invalid = await ${hook}.session.nativePreview({...${source},sourceId:'0'});
|
|
return {before,after:${hook}.snapshot().state.publications.map(source => source.id),cancelled:cancelledImage===null,resumed:!!data,invalid};
|
|
})()`);
|
|
assert.deepEqual(during.after, during.before, "previews do not replace or retire publications");
|
|
assert(during.cancelled && during.resumed && during.invalid === null);
|
|
await progressing(0, publication);
|
|
|
|
// Keep this real picker acceptance confined to the fixture's own window.
|
|
await evaluate(1, `(() => {
|
|
const session = ${hook}.session, sources = session.nativeSources, preview = session.nativePreview;
|
|
window.__previewPicker = {requests:[],restore:()=>{session.nativeSources=sources;session.nativePreview=preview;session.clearError();}};
|
|
session.nativeSources = async () => ({...await sources(),sources:[${source}]});
|
|
session.nativePreview = (target,signal) => {
|
|
const request = {signal}; window.__previewPicker.requests.push(request);
|
|
request.done = preview(target,signal); return request.done;
|
|
};
|
|
session.clearError(); return true;
|
|
})()`);
|
|
await browser(1, "click", '.c3-room-link[aria-label="Preview room"]');
|
|
const pickerCancelled = await evaluate(1, `(async () => {
|
|
document.querySelector('.c3-share-control button').click();
|
|
const deadline = performance.now()+5000;
|
|
while (!window.__previewPicker.requests.length && performance.now()<deadline)
|
|
await new Promise(resolve=>requestAnimationFrame(resolve));
|
|
const request = window.__previewPicker.requests[0];
|
|
if (!request) throw new Error('Picker did not request its visible source');
|
|
document.querySelector('.c3-share-picker button[aria-label="Cancel"]').click();
|
|
const image = await request.done;
|
|
return request.signal.aborted && image===null;
|
|
})()`);
|
|
assert(pickerCancelled, "closing the normal picker cancels its in-flight thumbnail");
|
|
await browser(1, "click", ".c3-share-control button");
|
|
await until("normal picker displays its Native thumbnail", () => evaluate(1,
|
|
"!!document.querySelector('.c3-share-picker [data-native-source] img')?.naturalWidth"), Boolean);
|
|
await browser(1, "screenshot", join(output,"native-preview-picker.png"));
|
|
await browser(1, "press", "Escape");
|
|
await evaluate(1, "(() => { window.__previewPicker.restore(); delete window.__previewPicker; return true; })()");
|
|
await progressing(0, publication);
|
|
await action(1, `stopScreen(${JSON.stringify(publication)})`);
|
|
await until("explicit stop retires the Native publication", () => view(0), value => !value.state.publications.some(source => source.id === publication));
|
|
|
|
await action(1, "setVideoCodecMode('vp8')");
|
|
await action(1, `startNativeScreen(${JSON.stringify(room)}, ${source}, false)`);
|
|
const manual = await until("manual Native source is published", () => view(0), value => value.state.publications.some(source => source.roomId === room));
|
|
const manualId = manual.state.publications.find(source => source.roomId === room).id;
|
|
await action(0, `watch(${JSON.stringify(manualId)})`);
|
|
const vp8Rtp = await codecProgressing(manualId, 'vp8');
|
|
await evaluate(1, `(async () => {
|
|
const session = ${hook}.session;
|
|
await session.setScreenQuality(${JSON.stringify(manualId)}, {
|
|
...session.screenQuality(${JSON.stringify(manualId)}).applied,
|
|
resolution: {width:320,height:180}, maxFramerate:15, degradationPreference:'maintain-framerate' });
|
|
return true;
|
|
})()`);
|
|
const qualityRtp = await codecProgressing(manualId, 'vp8', {width:320,height:180});
|
|
const sourceSettings = () => view(0).then(value => value.state.publications.find(source => source.id === manualId)?.videoSettings);
|
|
await until("Native quality reaches publication metadata", sourceSettings,
|
|
settings => settings?.degradationPreference === 'maintain-framerate' && settings.ceiling.maxFramerate === 15);
|
|
await evaluate(1, `(async () => {
|
|
const session = ${hook}.session;
|
|
await session.setScreenQuality(${JSON.stringify(manualId)}, {
|
|
...session.screenQuality(${JSON.stringify(manualId)}).applied, degradationPreference:'maintain-resolution' });
|
|
return true;
|
|
})()`);
|
|
const preferenceRtp = await codecProgressing(manualId, 'vp8', {width:320,height:180});
|
|
assert.equal(preferenceRtp.trackId, qualityRtp.trackId, 'preference replacement retains the receiver track');
|
|
await until("preference-only change retains the source ceiling", sourceSettings,
|
|
settings => settings?.degradationPreference === 'maintain-resolution' && settings.ceiling.maxFramerate === 15);
|
|
const replacement = (await action(1, 'nativeSources()')).sources.find(source => source.title === `${title} replacement`);
|
|
assert(replacement, 'the replacement is another window owned by this fixture');
|
|
await action(1, `startNativeScreen(${JSON.stringify(room)}, ${JSON.stringify(replacement)}, false)`);
|
|
assert.equal(await action(1, `screenCodec(${JSON.stringify(manualId)}).mode`), 'vp8');
|
|
const replacementRtp = await codecProgressing(manualId, 'vp8', {width:320,height:180});
|
|
const acknowledgements = await evaluate(1, 'window.__previewCodecs.acknowledgements');
|
|
assert.deepEqual(acknowledgements.slice(-2), [{mode:'vp8',codec:'vp8'},{mode:'vp8',codec:'vp8'}]);
|
|
assert((await view(0)).state.publications.some(source => source.id === manualId), 'reselection keeps the publication');
|
|
await action(1, `stopScreen(${JSON.stringify(manualId)})`);
|
|
await until("explicit stop retires the manual Native publication", () => view(0), value => !value.state.publications.some(source => source.id === manualId));
|
|
await evaluate(1, '(() => { window.__previewCodecs.restore(); delete window.__previewCodecs; return true; })()');
|
|
await action(1, "setVideoCodecMode('auto')");
|
|
await evaluate(1, `(async () => {
|
|
const pending = ${hook}.session.nativePreview(${source});
|
|
await new Promise(resolve => setTimeout(resolve,25)); ${hook}.session.close();
|
|
await pending; return true;
|
|
})()`);
|
|
await browser(1, "reload");
|
|
await until("page reopens the same App after preview cancellation", () => evaluate(1, `${hook}?.snapshot().appConnected ?? false`), Boolean);
|
|
assert(await action(1, `nativePreview(${source})`));
|
|
await writeFile(join(output, "native-preview-result.json"), JSON.stringify({ passed:true,
|
|
width:first.width,height:first.height,dataUrlBytes:first.data.length,firstFrameMs:Math.round(first.elapsed),
|
|
cancelled:true,pickerCancelled,invalidSource:true,activePublicationRetained:true,pageCloseAndReopen:true,
|
|
codec: { acknowledgements, autoRtp, vp8Rtp, qualityRtp, preferenceRtp, replacementRtp } },null,2));
|
|
} finally {
|
|
if (fixture.exitCode === null && fixture.signalCode === null) {
|
|
const exited = once(fixture,"exit"); fixture.kill(); await exited;
|
|
}
|
|
}
|
|
}
|