summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Dreßler <verdre@v0yd.nl>2022-02-03 11:10:36 +0100
committerJonas Dreßler <verdre@v0yd.nl>2023-04-25 17:17:41 +0200
commit9eea48536a6af497b90d815edc3d3141c5764813 (patch)
tree05944b6d5494a8b47ee5b8c0a77b7f29f648069a
parent9711a918e73a2959be7f7966db300615035468c9 (diff)
downloadgnome-shell-9eea48536a6af497b90d815edc3d3141c5764813.tar.gz
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: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2197>
-rw-r--r--js/dbusServices/screencast/screencastService.js118
1 files changed, 58 insertions, 60 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;
-
- 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.connectSignal('PipeWireStreamAdded',
- (proxy, sender, params) => {
- const [nodeId] = params;
- this._startPipeline(nodeId);
- });
- this._sessionProxy.StartSync();
- this._sessionState = SessionState.ACTIVE;
+ 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),
+ });
+
+ 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;
+ });
}
- 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]));
- });
+ }
}
};