Skip to content

Commit

Permalink
refactor: rework fetching of source screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
grantjbutler committed Mar 13, 2022
1 parent e977068 commit 5c30216
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions packages/main/src/obs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,46 @@ export default class OBSSocket {
}

_screenshotSource(sourceName: string, quality: ImageQuality = ImageQuality.Best): Promise<string> {
const options: ScreenshotOptions = { sourceName };

switch (quality) {
case ImageQuality.Worst:
options.embedPictureFormat = 'jpeg';
options.compressionQuality = 1;
break;
case ImageQuality.Best:
options.embedPictureFormat = 'png';
break;
}
return this._screenshotSources([sourceName], quality)
.then(results => results[sourceName]);
}

return this._socket.send('TakeSourceScreenshot', options)
.then(screenshot => screenshot.img);
_screenshotSources(sourceNames: string[], quality: ImageQuality = ImageQuality.Best): Promise<{ [index: string]: string }> {
const requests = sourceNames.map(sourceName => {
const options: ScreenshotOptions = { sourceName };

switch (quality) {
case ImageQuality.Worst:
options.embedPictureFormat = 'jpeg';
options.compressionQuality = 1;
break;
case ImageQuality.Best:
options.embedPictureFormat = 'png';
break;
}

return {
...options,
'request-type': 'TakeSourceScreenshot',
'message-id': `${sourceName}-screenshot`,
};
});

return this._socket.send('ExecuteBatch', { requests, abortOnFail: false })
.then(({ results }) => {
return results.reduce((images, result) => {
let sourceName: string;
if (result['message-id'] && result['message-id'].endsWith('-screenshot')) {
sourceName = result['message-id'].substring(0, result['message-id'].length - '-screenshot'.length);
} else {
return images;
}

images[sourceName] = result.img;

return images;
}, {} as { [index: string]: string });
});
}

_fetchCanvasSize(): Promise<void> {
Expand Down

0 comments on commit 5c30216

Please sign in to comment.