address comments
This commit is contained in:
+13
-3
@@ -84,6 +84,7 @@ export class BrowserManager {
|
|||||||
private screencastActive: boolean = false;
|
private screencastActive: boolean = false;
|
||||||
private screencastSessionId: number = 0;
|
private screencastSessionId: number = 0;
|
||||||
private frameCallback: ((frame: ScreencastFrame) => void) | null = null;
|
private frameCallback: ((frame: ScreencastFrame) => void) | null = null;
|
||||||
|
private screencastFrameHandler: ((params: any) => void) | null = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if browser is launched
|
* Check if browser is launched
|
||||||
@@ -952,8 +953,8 @@ export class BrowserManager {
|
|||||||
this.frameCallback = callback;
|
this.frameCallback = callback;
|
||||||
this.screencastActive = true;
|
this.screencastActive = true;
|
||||||
|
|
||||||
// Listen for screencast frames
|
// Create and store the frame handler so we can remove it later
|
||||||
cdp.on('Page.screencastFrame', async (params: any) => {
|
this.screencastFrameHandler = async (params: any) => {
|
||||||
const frame: ScreencastFrame = {
|
const frame: ScreencastFrame = {
|
||||||
data: params.data,
|
data: params.data,
|
||||||
metadata: params.metadata,
|
metadata: params.metadata,
|
||||||
@@ -967,7 +968,10 @@ export class BrowserManager {
|
|||||||
if (this.frameCallback) {
|
if (this.frameCallback) {
|
||||||
this.frameCallback(frame);
|
this.frameCallback(frame);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
// Listen for screencast frames
|
||||||
|
cdp.on('Page.screencastFrame', this.screencastFrameHandler);
|
||||||
|
|
||||||
// Start the screencast
|
// Start the screencast
|
||||||
await cdp.send('Page.startScreencast', {
|
await cdp.send('Page.startScreencast', {
|
||||||
@@ -990,12 +994,18 @@ export class BrowserManager {
|
|||||||
try {
|
try {
|
||||||
const cdp = await this.getCDPSession();
|
const cdp = await this.getCDPSession();
|
||||||
await cdp.send('Page.stopScreencast');
|
await cdp.send('Page.stopScreencast');
|
||||||
|
|
||||||
|
// Remove the event listener to prevent accumulation
|
||||||
|
if (this.screencastFrameHandler) {
|
||||||
|
cdp.off('Page.screencastFrame', this.screencastFrameHandler);
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Ignore errors when stopping
|
// Ignore errors when stopping
|
||||||
}
|
}
|
||||||
|
|
||||||
this.screencastActive = false;
|
this.screencastActive = false;
|
||||||
this.frameCallback = null;
|
this.frameCallback = null;
|
||||||
|
this.screencastFrameHandler = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+23
-17
@@ -304,26 +304,32 @@ export class StreamServer {
|
|||||||
* Start screencasting
|
* Start screencasting
|
||||||
*/
|
*/
|
||||||
private async startScreencast(): Promise<void> {
|
private async startScreencast(): Promise<void> {
|
||||||
|
// Set flag immediately to prevent race conditions with concurrent calls
|
||||||
if (this.isScreencasting) return;
|
if (this.isScreencasting) return;
|
||||||
|
|
||||||
// Check if browser is launched
|
|
||||||
if (!this.browser.isLaunched()) {
|
|
||||||
throw new Error('Browser not launched');
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.browser.startScreencast((frame) => this.broadcastFrame(frame), {
|
|
||||||
format: 'jpeg',
|
|
||||||
quality: 80,
|
|
||||||
maxWidth: 1280,
|
|
||||||
maxHeight: 720,
|
|
||||||
everyNthFrame: 1,
|
|
||||||
});
|
|
||||||
|
|
||||||
this.isScreencasting = true;
|
this.isScreencasting = true;
|
||||||
|
|
||||||
// Notify all clients
|
try {
|
||||||
for (const client of this.clients) {
|
// Check if browser is launched
|
||||||
this.sendStatus(client);
|
if (!this.browser.isLaunched()) {
|
||||||
|
throw new Error('Browser not launched');
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.browser.startScreencast((frame) => this.broadcastFrame(frame), {
|
||||||
|
format: 'jpeg',
|
||||||
|
quality: 80,
|
||||||
|
maxWidth: 1280,
|
||||||
|
maxHeight: 720,
|
||||||
|
everyNthFrame: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Notify all clients
|
||||||
|
for (const client of this.clients) {
|
||||||
|
this.sendStatus(client);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// Reset flag on failure so caller can retry
|
||||||
|
this.isScreencasting = false;
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user