## Problem and outcome / 问题与结果 Sharing could stall at Browser encoder handoff or retain a pause after a video binding was replaced. Source/audio changes could also overlap offer/answer negotiation, while Native recovery could request redundant keyframes. This phase repairs those existing owners, drains capture output during bounded shutdown, and normalizes configured Site origins consistently with Browser serialization. ## Release notes ### 中文 这是一次稳定性更新,重点改善分享中的画面连续性,以及暂停、换源和恢复播放。 - 修复浏览器编码复用中的重复帧率限制和交接等待,减少由这些问题造成的卡顿。 - 修复换源和音频变更时的协商重叠,并防止已关闭连接的异步操作干扰恢复。 - 修复房主暂停期间重连后无法恢复播放,以及编码复用回退后仍停在暂停状态的问题;正常取消播放不再被当作媒体故障。 - 减少原生采集的重复关键帧请求,并修复停止采集时输出未排空导致的退出等待问题。 - 修复部分国际化域名和 IPv6 站点地址的识别,并改善窄屏下分享设置项的文字排版。 升级 Piik App 或自部署 Server 后刷新页面即可,无需修改配置。部分设备和网络环境下报告的开播失败、偶发断流仍需现场日志确认,本次更新不代表所有此类反馈均已解决。 ### English This stability update improves picture continuity and recovery when pausing, changing sources, or resuming playback. - Fix duplicate frame-rate limiting and unnecessary handoff waits in Browser encoding reuse, reducing stalls caused by these issues. - Serialize negotiation across source and audio changes, and prevent asynchronous work on closed connections from interfering with recovery. - Fix playback remaining paused after reconnecting while the Host is paused, or after falling back from shared encoding. Normal playback cancellation is no longer treated as a media failure. - Avoid redundant Native capture keyframe requests and drain pending capture output during bounded shutdown. - Fix recognition of some internationalized domain names and IPv6 Site addresses, and improve sharing-setting labels on narrow screens. Update Piik App or your self-hosted Server, then refresh the page. No configuration changes are required. Some device- and network-specific reports of sharing startup failure or intermittent stream loss still need matching diagnostics; this update does not claim to resolve every reported case. ## Verification / 验证 - `npm run check`: 76 test files / 1,152 tests, TypeScript and production Web build passed. - Real Chrome VP8/H264 checks with two viewers passed live/paused profile changes, quiet-source recovery, source replacement and ordinary-sender fallback after producer failure. - New pause-owner regressions fail against the previous implementation; actual Viewer effects preserve same-binding local pause and resume replacement bindings. - Independent cumulative Browser and Native/config review found no confirmed release blocker. - `npm run check:go`: formatting, Go tests/vet, Windows/Linux builds and Windows native capture compile/probes passed. Darwin packaging and Go race detection run on the native CI runners. - Windows App H264 and VP8 Host gates passed decoded playback, live/paused quality changes, quiet/background-source recovery, source replacement, failure cleanup and subsequent sharing; experiment-owned processes, ports and profiles were cleaned up. - Documentation, repository hygiene and release-note extraction passed. ## Impact / 影响 Patch release v1.6.7. Browser/server v23, Native control v9, capture v7 and SQLite schema 2 remain unchanged. Existing WebRTC adaptation and route policy remain unchanged; no quality floor is introduced. Interaction and Windows x86 prototypes are excluded. Managed Server restart interrupts active media and preserves room authority. Physical macOS/Linux capture, broad public-network endurance and unmatched reporter environments remain outside local acceptance.
369 lines
19 KiB
TypeScript
369 lines
19 KiB
TypeScript
import { createOpaqueId } from "../lib/opaque-id";
|
|
import { browserDebugEnabled, debugError, debugEvent } from "../lib/debug";
|
|
import { EMPTY_METRICS, type ConnectionMetrics } from "../types";
|
|
import { captureMetrics, collectConnectionMetricsFromReport, createStatsAccumulator } from "../webrtc/stats";
|
|
import { BrowserEncodingProducer } from "./browser-encoding-producer";
|
|
import { BROWSER_CARRIER_SIZE, type BrowserEncodingOutput } from "./browser-encoding-output";
|
|
import { codecIdentity, fits, nativeVideoBudget, negotiatedCodec, noRegression, observeVideo,
|
|
projectEncodingMetrics, videoOutbound, type Observation } from "./browser-encoding-stats";
|
|
import { QUALITY_RESOLUTIONS, videoQualitySettingsEqual, type QualityProfile } from "./quality";
|
|
|
|
export interface BrowserPooledSender {
|
|
updateProfile(profile: QualityProfile): Promise<void>;
|
|
carrierScale(): number | undefined;
|
|
setPaused(paused: boolean): void;
|
|
metrics(transport: ConnectionMetrics): ConnectionMetrics;
|
|
dispose(): void;
|
|
}
|
|
|
|
type Output = ReturnType<BrowserEncodingOutput["snapshot"]>;
|
|
type Group = {
|
|
producer: BrowserEncodingProducer; source: MediaStreamTrack; profile: QualityProfile;
|
|
codecKey: string; budget: number; ready: boolean; failed: boolean;
|
|
report?: RTCStatsReport; previous?: RTCOutboundRtpStreamStats; output?: Observation;
|
|
updating?: Promise<void>;
|
|
};
|
|
type Member = {
|
|
id: string; source: MediaStreamTrack; sender: RTCRtpSender; connection: RTCPeerConnection;
|
|
profile: QualityProfile; encoded: BrowserEncodingOutput;
|
|
configureCarrier: () => Promise<boolean>;
|
|
requestKey: () => Promise<void>; onFatal: () => void;
|
|
paused: boolean; disabled: boolean; disposed: boolean; mixed: boolean; carrier: boolean;
|
|
codec?: RTCRtpCodec; codecKey?: string; budget?: number; raw?: RTCOutboundRtpStreamStats;
|
|
encoderStats: ReturnType<typeof createStatsAccumulator>;
|
|
current?: Group; pending?: { group: Group; selecting?: boolean };
|
|
output?: Output; previousOutput?: Output; replacement?: Promise<void>;
|
|
};
|
|
|
|
// M152 exposes BWE through getStats, not a bandwidth callback. The paired
|
|
// weak-entry probe bounds observation lag at this cadence; it is not a retry.
|
|
const BUDGET_SAMPLE_MS = 500;
|
|
|
|
/** One owner per captured source tree; connections retain their own RTP and transport. */
|
|
export class BrowserEncodingPool {
|
|
private readonly members = new Set<Member>();
|
|
private readonly groups = new Set<Group>();
|
|
private readonly failedSources = new WeakSet<MediaStreamTrack>();
|
|
private timer: ReturnType<typeof setInterval> | undefined;
|
|
private polling = false;
|
|
private disposed = false;
|
|
|
|
create(source: MediaStreamTrack, sender: RTCRtpSender, connection: RTCPeerConnection,
|
|
profile: QualityProfile, encoded: BrowserEncodingOutput, configureCarrier: Member["configureCarrier"], requestKey: Member["requestKey"],
|
|
onFatal: () => void): BrowserPooledSender | null {
|
|
if (this.disposed || this.failedSources.has(source) || source.kind !== "video" || source.readyState !== "live") return null;
|
|
const member: Member = { id: createOpaqueId(), source, sender, connection, profile: { ...profile }, encoded, configureCarrier, requestKey, onFatal,
|
|
paused: !source.enabled, disabled: false, disposed: false, mixed: false, carrier: encoded.snapshot().frames === 0,
|
|
encoderStats: createStatsAccumulator() };
|
|
this.members.add(member);
|
|
// A fresh connection has no useful raw output to preserve. Start its
|
|
// tiny clock immediately, rather than warming two full encoders first.
|
|
if (!member.carrier) encoded.passthrough(requestKey);
|
|
if (!this.timer) this.timer = setInterval(() => void this.poll(), BUDGET_SAMPLE_MS);
|
|
return {
|
|
carrierScale: () => {
|
|
if (!member.carrier) return;
|
|
const width = member.sender.track?.getSettings().width ?? QUALITY_RESOLUTIONS[member.profile.resolution].width;
|
|
return Math.max(1, width / BROWSER_CARRIER_SIZE);
|
|
},
|
|
updateProfile: async (next) => {
|
|
if (member.disposed || videoQualitySettingsEqual(member.profile, next)) return;
|
|
member.profile = { ...next };
|
|
member.encoderStats = createStatsAccumulator(); member.previousOutput = undefined;
|
|
// HostPeer awaits this inside its sender queue. Handoffs run outside it.
|
|
},
|
|
setPaused: (paused) => {
|
|
if (member.disposed || member.paused === paused) return;
|
|
member.paused = paused;
|
|
member.previousOutput = undefined;
|
|
this.updatePauses();
|
|
},
|
|
metrics: (transport) => this.metrics(member, transport),
|
|
dispose: () => this.remove(member),
|
|
};
|
|
}
|
|
|
|
dispose(): void {
|
|
this.disposed = true;
|
|
for (const member of [...this.members]) this.remove(member);
|
|
this.prune();
|
|
}
|
|
|
|
private compatible(member: Member, group: Group): boolean {
|
|
return member.source === group.source && member.codecKey === group.codecKey &&
|
|
videoQualitySettingsEqual(member.profile, group.profile) && !group.failed;
|
|
}
|
|
|
|
private references(group: Group): Member[] {
|
|
return [...this.members].filter((member) => member.current === group || member.pending?.group === group);
|
|
}
|
|
|
|
private async poll(): Promise<void> {
|
|
if (this.polling || this.disposed) return;
|
|
this.polling = true;
|
|
try {
|
|
await Promise.allSettled([
|
|
...[...this.members].map(async (member) => {
|
|
if (member.disposed || member.disabled || member.connection.connectionState !== "connected") { member.budget = undefined; return; }
|
|
const report = await member.connection.getStats().catch(() => null);
|
|
if (member.disposed) return;
|
|
if (!report) { member.budget = undefined; return; }
|
|
const codec = negotiatedCodec(report);
|
|
if (!codec) {
|
|
if (member.current || member.pending) this.failSource(member.source);
|
|
else {
|
|
const outbound = videoOutbound(report);
|
|
if (outbound?.codecId && report.get(outbound.codecId)) this.failSource(member.source);
|
|
}
|
|
return;
|
|
}
|
|
const key = codecIdentity(codec);
|
|
if (member.codecKey && member.codecKey !== key && (member.current || member.pending)) {
|
|
this.failSource(member.source); return;
|
|
}
|
|
member.codec = codec; member.codecKey = key;
|
|
member.raw = videoOutbound(report);
|
|
member.budget = nativeVideoBudget(member.raw, member.profile.maxBitrate);
|
|
}),
|
|
...[...this.groups].map(async (group) => {
|
|
if (!group.ready || group.failed) { group.output = undefined; return; }
|
|
const report = await group.producer.report().catch(() => null);
|
|
if (!this.groups.has(group)) return;
|
|
if (!report) { group.output = undefined; return; }
|
|
group.report = report;
|
|
const video = videoOutbound(report);
|
|
group.output = observeVideo(video, group.previous);
|
|
group.previous = video;
|
|
}),
|
|
]);
|
|
if (this.disposed) return;
|
|
this.reconcile();
|
|
for (const member of this.members) {
|
|
member.output = member.encoded.snapshot();
|
|
if (!member.pending && !member.replacement && member.output.frames > 0 &&
|
|
member.output.lastProducerId === (member.current?.producer.id ?? null)) member.mixed = false;
|
|
}
|
|
if (browserDebugEnabled) debugEvent("encoding-pool", "sample", {
|
|
members: [...this.members].map((member) => ({ memberId: member.id, trackId: member.source.id,
|
|
rtpStatsId: member.raw?.id, ssrc: member.raw?.ssrc,
|
|
producerId: member.current?.producer.id, pendingProducerId: member.pending?.group.producer.id,
|
|
budget: member.budget, carrier: member.carrier, paused: member.paused, disabled: member.disabled, output: member.output })),
|
|
groups: [...this.groups].map((group) => ({ producerId: group.producer.id, budget: group.budget,
|
|
ready: group.ready, failed: group.failed, output: group.output })),
|
|
});
|
|
} finally { this.polling = false; }
|
|
}
|
|
|
|
private reconcile(): void {
|
|
const changing = new Set<Group>();
|
|
for (const group of this.groups) {
|
|
const references = this.references(group);
|
|
const profile = references[0]?.profile;
|
|
if (!profile || group.failed || videoQualitySettingsEqual(profile, group.profile) ||
|
|
!references.every((member) => member.source === group.source && member.codecKey === group.codecKey &&
|
|
videoQualitySettingsEqual(member.profile, profile))) continue;
|
|
changing.add(group);
|
|
if (!group.ready || group.updating) continue;
|
|
for (const member of references) if (member.current === group && member.pending && !member.pending.selecting) member.pending = undefined;
|
|
const next = { ...profile };
|
|
const budget = Math.min(next.maxBitrate, Math.max(...references.map((member) => member.budget ?? group.budget)));
|
|
group.budget = budget;
|
|
group.updating = group.producer.update(next, budget).then(() => {
|
|
if (!this.groups.has(group)) return;
|
|
group.profile = next;
|
|
for (const member of this.members) if (member.current === group || member.pending?.group === group) {
|
|
member.encoderStats = createStatsAccumulator(); member.previousOutput = undefined;
|
|
}
|
|
}).catch(() => {
|
|
if (this.groups.has(group)) this.failSource(group.source);
|
|
}).finally(() => { group.updating = undefined; });
|
|
}
|
|
for (const member of this.members) {
|
|
if (member.disposed || member.disabled || member.paused || member.replacement || !member.codec || member.budget === undefined) continue;
|
|
if ([member.current, member.pending?.group].some((group) => group && (group.updating || changing.has(group)))) continue;
|
|
if (member.pending) {
|
|
const pending = member.pending;
|
|
// A shared producer can outgrow a waiting child's native allocation.
|
|
// Replan that membership; never wait forever for its budget to catch up.
|
|
if (!pending.selecting && pending.group.budget > member.budget &&
|
|
!fits(pending.group.output, member.budget) && this.references(pending.group).some((other) => other !== member)) {
|
|
member.pending = undefined;
|
|
continue;
|
|
}
|
|
if (!pending.selecting && member.current && this.compatible(member, member.current) &&
|
|
fits(member.current.output, member.budget) && pending.group.budget < member.current.budget) {
|
|
member.pending = undefined;
|
|
continue;
|
|
}
|
|
if (!pending.selecting && !this.compatible(member, pending.group)) {
|
|
member.pending = undefined;
|
|
continue;
|
|
}
|
|
const ownedBudget = pending.group.budget <= member.budget;
|
|
// The output already waits for a real recovery frame. A rate-owned
|
|
// downgrade need not wait another statistics window while the old
|
|
// high-rate output keeps congesting the same path.
|
|
if (!pending.selecting && pending.group.ready &&
|
|
(fits(pending.group.output, member.budget) || ((member.current || member.carrier) && ownedBudget))) {
|
|
const output = pending.group.output;
|
|
// A fresh carrier has delivered no picture. Its tiny encode cannot
|
|
// establish a quality baseline for the first real output.
|
|
if (member.current || member.carrier || (output && noRegression(output, member.raw?.frameWidth ?? null,
|
|
member.raw?.frameHeight ?? null, member.raw?.framesPerSecond ?? null, member.profile))) {
|
|
pending.selecting = true;
|
|
member.mixed = true;
|
|
member.encoded.select(pending.group.producer.id, () => pending.group.producer.requestKey(), () => {
|
|
if (member.disposed || member.pending !== pending) return;
|
|
member.current = pending.group;
|
|
pending.group.producer.beginOutput();
|
|
member.pending = undefined;
|
|
member.encoderStats = createStatsAccumulator(); member.previousOutput = undefined;
|
|
member.carrier = true;
|
|
member.replacement = member.configureCarrier().then((applied) => {
|
|
if (!member.disposed && !applied) this.failSource(member.source);
|
|
}).catch(() => this.failSource(member.source)).finally(() => { member.replacement = undefined; });
|
|
this.prune();
|
|
});
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
const current = member.current;
|
|
if (current && this.compatible(member, current) &&
|
|
(!current.output || current.output.bitrate === 0 || current.output.fps === 0)) continue;
|
|
if (current?.output && this.compatible(member, current) && fits(current.output, member.budget)) {
|
|
// Equal outputs converge to the earlier owner. A strictly better healthy
|
|
// output is also reusable when this encoder has not recovered itself.
|
|
let earlier = true;
|
|
for (const group of this.groups) {
|
|
if (group === current) { earlier = false; continue; }
|
|
const improves = group.output && current.output && (group.output.width > current.output.width ||
|
|
group.output.height > current.output.height || Math.round(group.output.fps) > Math.round(current.output.fps));
|
|
if ((earlier || improves) && this.compatible(member, group) && fits(group.output, member.budget) && group.output?.reason === "none" &&
|
|
noRegression(group.output!, current.output.width, current.output.height, current.output.fps, member.profile)) {
|
|
member.pending = { group }; break;
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
// A short output-rate spike is not new demand. Keep the rate owner and
|
|
// let its native encoder adapt unless another child needs a higher rate.
|
|
if (current && this.compatible(member, current) && !this.references(current).some((other) =>
|
|
this.compatible(other, current) && other.budget !== undefined && other.budget > member.budget!)) continue;
|
|
const candidates = [...this.groups].filter((group) => this.compatible(member, group));
|
|
let target = candidates.find((group) => group !== current && fits(group.output, member.budget) &&
|
|
(!current || group.budget === member.budget));
|
|
target ??= candidates.find((group) => group !== current && group.budget === member.budget);
|
|
if (!target) {
|
|
target = this.startGroup(member, member.budget);
|
|
}
|
|
member.pending = { group: target };
|
|
}
|
|
for (const group of this.groups) {
|
|
const references = this.references(group);
|
|
const budgets = references.flatMap((member) => this.compatible(member, group) && member.budget !== undefined ? [member.budget] : []);
|
|
if (group.ready && budgets.length) {
|
|
const budget = Math.max(...budgets);
|
|
if (budget !== group.budget && !group.updating) {
|
|
group.budget = budget;
|
|
group.updating = group.producer.update(group.profile, budget).catch(() => {
|
|
if (this.groups.has(group)) this.failSource(group.source);
|
|
})
|
|
.finally(() => {
|
|
group.updating = undefined;
|
|
// A ready downgrade can use this applied budget now. Waiting for
|
|
// another sample compares the previous limit to falling demand again.
|
|
if (!this.disposed && this.groups.has(group)) this.reconcile();
|
|
});
|
|
}
|
|
}
|
|
}
|
|
this.updatePauses();
|
|
this.prune();
|
|
}
|
|
|
|
private startGroup(member: Member, budget: number): Group {
|
|
const profile = { ...member.profile };
|
|
const producer = new BrowserEncodingProducer(member.source, profile, member.codec!, (frame) => {
|
|
for (const output of this.members) if (output.current === group || output.pending?.group === group) {
|
|
output.encoded.push(producer.id, frame);
|
|
}
|
|
},
|
|
() => { group.failed = true; this.failSource(member.source); });
|
|
const group: Group = { producer, source: member.source, profile, codecKey: member.codecKey!, budget, ready: false, failed: false };
|
|
this.groups.add(group);
|
|
debugEvent("encoding-pool", "producer-preparing", { producerId: producer.id, trackId: member.source.id, codec: member.codecKey, budget, profile });
|
|
void producer.start(budget).then(() => {
|
|
if (!this.groups.has(group)) return;
|
|
group.ready = true;
|
|
this.reconcile();
|
|
}, (error) => {
|
|
debugError("encoding-pool", "producer-start-failed", error, { producerId: producer.id });
|
|
if (this.groups.has(group)) { group.failed = true; this.failSource(group.source); }
|
|
});
|
|
return group;
|
|
}
|
|
|
|
private restore(member: Member): void {
|
|
if (member.disposed) return;
|
|
member.mixed = true;
|
|
member.pending = undefined;
|
|
member.carrier = false;
|
|
member.replacement = member.configureCarrier().then((applied) => {
|
|
if (member.disposed) return;
|
|
if (!applied) { member.onFatal(); return; }
|
|
member.encoded.passthrough(member.requestKey);
|
|
member.current = undefined;
|
|
member.encoderStats = createStatsAccumulator(); member.previousOutput = undefined;
|
|
}).catch(() => { if (!member.disposed) member.onFatal(); })
|
|
.finally(() => { member.replacement = undefined; this.prune(); });
|
|
}
|
|
|
|
private failSource(source: MediaStreamTrack): void {
|
|
if (this.failedSources.has(source)) return;
|
|
this.failedSources.add(source);
|
|
for (const member of this.members) if (member.source === source) {
|
|
member.disabled = true;
|
|
if (member.replacement) void member.replacement.then(() => this.restore(member));
|
|
else this.restore(member);
|
|
}
|
|
}
|
|
|
|
private updatePauses(): void {
|
|
for (const group of this.groups) group.producer.setPaused(!this.references(group).some((member) => !member.paused));
|
|
}
|
|
|
|
private metrics(member: Member, transport: ConnectionMetrics): ConnectionMetrics {
|
|
if (member.disposed || (!member.current && !member.mixed)) return transport;
|
|
const group = member.current, input = group?.producer.track, sample = member.output, previous = member.previousOutput;
|
|
member.previousOutput = sample;
|
|
const measured = !member.mixed && !member.paused && input && group?.report && !group.failed && sample?.lastProducerId === group.producer.id &&
|
|
previous?.lastProducerId === group.producer.id && sample.timestamp > previous.timestamp;
|
|
const encoder = measured ? { ...collectConnectionMetricsFromReport(group.report!, "send", member.encoderStats),
|
|
...captureMetrics(input) } : EMPTY_METRICS;
|
|
const qualityEligible = !!measured && !member.pending && !group.updating && this.compatible(member, group) &&
|
|
(encoder.nativeEdgeQualityState === "degraded" || fits(group.output, member.budget));
|
|
return projectEncodingMetrics(transport, encoder, measured ? sample : undefined, measured ? previous : undefined, qualityEligible);
|
|
}
|
|
|
|
private remove(member: Member): void {
|
|
if (member.disposed) return;
|
|
member.disposed = true;
|
|
this.members.delete(member);
|
|
member.encoded.passthrough(member.requestKey);
|
|
member.carrier = false;
|
|
member.current = undefined; member.pending = undefined;
|
|
this.prune();
|
|
}
|
|
|
|
private prune(): void {
|
|
for (const group of this.groups) if (!this.references(group).length) {
|
|
this.groups.delete(group); group.producer.dispose();
|
|
}
|
|
if (!this.members.size) {
|
|
if (this.timer) clearInterval(this.timer);
|
|
this.timer = undefined;
|
|
}
|
|
}
|
|
}
|