From 9eea48536a6af497b90d815edc3d3141c5764813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Dre=C3=9Fler?= Date: Thu, 3 Feb 2022 11:10:36 +0100 Subject: [PATCH] screencastService: Promisify Recorder start/stopRecording functions This will allow for cleaner error handling in the following commits. A benefit we get from it right now is having one less callback when calling startRecording/stopRecording(). Part-of: --- .../screencast/screencastService.js | 112 +++++++++--------- 1 file changed, 55 insertions(+), 57 deletions(-) diff --git a/js/dbusServices/screencast/screencastService.js b/js/dbusServices/screencast/screencastService.js index eb3dc88e6..7c0810567 100644 --- a/js/dbusServices/screencast/screencastService.js +++ b/js/dbusServices/screencast/screencastService.js @@ -114,12 +114,12 @@ var Recorder = class { _notifyStopped() { this._unwatchSender(); - if (this._onStartedCallback) - this._onStartedCallback(this, false); - else if (this._onStoppedCallback) - this._onStoppedCallback(this); + if (this._startRequest) + this._startRequest.reject(new Error()); + else if (this._stopRequest) + this._stopRequest.resolve(); else - this._onErrorCallback(this); + this._onErrorCallback(); } _onSessionClosed() { @@ -151,38 +151,46 @@ var Recorder = class { this._pipeline.set_state(Gst.State.PLAYING); this._pipelineState = PipelineState.PLAYING; - this._onStartedCallback(this, true); - this._onStartedCallback = null; + this._startRequest.resolve(); + delete this._startRequest; } - startRecording(onStartedCallback) { - this._onStartedCallback = onStartedCallback; + startRecording() { + return new Promise((resolve, reject) => { + this._startRequest = {resolve, reject}; - const [streamPath] = this._sessionProxy.RecordAreaSync( - this._x, this._y, - this._width, this._height, - { - 'is-recording': GLib.Variant.new('b', true), - 'cursor-mode': GLib.Variant.new('u', this._drawCursor ? 1 : 0), - }); + const [streamPath] = this._sessionProxy.RecordAreaSync( + this._x, this._y, + this._width, this._height, + { + 'is-recording': GLib.Variant.new('b', true), + 'cursor-mode': GLib.Variant.new('u', this._drawCursor ? 1 : 0), + }); - this._streamProxy = new ScreenCastStreamProxy(Gio.DBus.session, - 'org.gnome.ScreenCast.Stream', - streamPath); + this._streamProxy = new ScreenCastStreamProxy(Gio.DBus.session, + 'org.gnome.ScreenCast.Stream', + streamPath); - this._streamProxy.connectSignal('PipeWireStreamAdded', - (proxy, sender, params) => { - const [nodeId] = params; - this._startPipeline(nodeId); - }); - this._sessionProxy.StartSync(); - this._sessionState = SessionState.ACTIVE; + this._streamProxy.connectSignal('PipeWireStreamAdded', + (_proxy, _sender, params) => { + const [nodeId] = params; + this._startPipeline(nodeId); + }); + this._sessionProxy.StartSync(); + this._sessionState = SessionState.ACTIVE; + }); } - stopRecording(onStoppedCallback) { - this._pipelineState = PipelineState.FLUSHING; - this._onStoppedCallback = onStoppedCallback; - this._pipeline.send_event(Gst.Event.new_eos()); + stopRecording() { + if (this._startRequest) + return Promise.reject(new Error('Unable to stop recorder while still starting')); + + return new Promise((resolve, reject) => { + this._stopRequest = {resolve, reject}; + + this._pipelineState = PipelineState.FLUSHING; + this._pipeline.send_event(Gst.Event.new_eos()); + }); } _stopSession() { @@ -366,7 +374,7 @@ var ScreencastService = class extends ServiceImplementation { return this._getAbsolutePath(filename); } - ScreencastAsync(params, invocation) { + async ScreencastAsync(params, invocation) { let returnValue = [false, '']; if (this._lockdownSettings.get_boolean('disable-save-to-disk')) { @@ -397,7 +405,7 @@ var ScreencastService = class extends ServiceImplementation { filePath, options, invocation, - _recorder => this._removeRecorder(sender)); + () => this._removeRecorder(sender)); } catch (error) { log(`Failed to create recorder: ${error.message}`); invocation.return_value(GLib.Variant.new('(bs)', returnValue)); @@ -407,24 +415,17 @@ var ScreencastService = class extends ServiceImplementation { this._addRecorder(sender, recorder); try { - recorder.startRecording( - (_, result) => { - if (result) { - returnValue = [true, filePath]; - invocation.return_value(GLib.Variant.new('(bs)', returnValue)); - } else { - this._removeRecorder(sender); - invocation.return_value(GLib.Variant.new('(bs)', returnValue)); - } - }); + await recorder.startRecording(); + returnValue = [true, filePath]; } catch (error) { log(`Failed to start recorder: ${error.message}`); this._removeRecorder(sender); + } finally { invocation.return_value(GLib.Variant.new('(bs)', returnValue)); } } - ScreencastAreaAsync(params, invocation) { + async ScreencastAreaAsync(params, invocation) { let returnValue = [false, '']; if (this._lockdownSettings.get_boolean('disable-save-to-disk')) { @@ -454,7 +455,7 @@ var ScreencastService = class extends ServiceImplementation { filePath, options, invocation, - _recorder => this._removeRecorder(sender)); + () => this._removeRecorder(sender)); } catch (error) { log(`Failed to create recorder: ${error.message}`); invocation.return_value(GLib.Variant.new('(bs)', returnValue)); @@ -464,24 +465,17 @@ var ScreencastService = class extends ServiceImplementation { this._addRecorder(sender, recorder); try { - recorder.startRecording( - (_, result) => { - if (result) { - returnValue = [true, filePath]; - invocation.return_value(GLib.Variant.new('(bs)', returnValue)); - } else { - this._removeRecorder(sender); - invocation.return_value(GLib.Variant.new('(bs)', returnValue)); - } - }); + await recorder.startRecording(); + returnValue = [true, filePath]; } catch (error) { log(`Failed to start recorder: ${error.message}`); this._removeRecorder(sender); + } finally { invocation.return_value(GLib.Variant.new('(bs)', returnValue)); } } - StopScreencastAsync(params, invocation) { + async StopScreencastAsync(params, invocation) { const sender = invocation.get_sender(); const recorder = this._recorders.get(sender); @@ -490,9 +484,13 @@ var ScreencastService = class extends ServiceImplementation { return; } - recorder.stopRecording(() => { + try { + await recorder.stopRecording(); + } catch (error) { + log(`${sender}: Error while stopping recorder: ${error.message}`); + } finally { this._removeRecorder(sender); invocation.return_value(GLib.Variant.new('(b)', [true])); - }); + } } };