screencastService: Streamline teardown and error handling

We're tracking three different state-machines in the Recorder object:
The state of the gstreamer pipeline, the state of the screencast
session, and the sender of our dbus invocation that might vanish.

Properly handling errors that might appear in any of those three "black
boxes" is not easy, especially tearing down the other two when one of
them breaks.

So refactor the error handling here: Add a single error path for each of
those three states we're tracking, and make them all subsequently call
the _bailOutOnError() method. From there we tear down the other states and
call the error callbacks.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2197>
This commit is contained in:
Jonas Dreßler 2022-01-28 23:50:35 +01:00
parent e6196b5b9f
commit cbfbdc4be5

View File

@ -69,9 +69,13 @@ var Recorder = class {
this._framerate = DEFAULT_FRAMERATE; this._framerate = DEFAULT_FRAMERATE;
this._drawCursor = DEFAULT_DRAW_CURSOR; this._drawCursor = DEFAULT_DRAW_CURSOR;
this._pipelineState = PipelineState.INIT;
this._pipeline = null;
this._applyOptions(options); this._applyOptions(options);
this._watchSender(invocation.get_sender()); this._watchSender(invocation.get_sender());
this._sessionState = SessionState.INIT;
this._initSession(sessionPath); this._initSession(sessionPath);
} }
@ -107,32 +111,62 @@ var Recorder = class {
} }
} }
_senderVanished() { _teardownPipeline() {
this._unwatchSender(); if (!this._pipeline)
return;
this.stopRecording(null); if (this._pipeline.set_state(Gst.State.NULL) !== Gst.StateChangeReturn.SUCCESS)
log('Failed to set pipeline state to NULL');
this._pipelineState = PipelineState.STOPPED;
this._pipeline = null;
} }
_notifyStopped() { _stopSession() {
if (this._sessionState === SessionState.ACTIVE) {
this._sessionState = SessionState.STOPPED;
this._sessionProxy.StopSync();
}
}
_bailOutOnError(error) {
this._teardownPipeline();
this._unwatchSender(); this._unwatchSender();
if (this._startRequest) this._stopSession();
this._startRequest.reject(new Error());
else if (this._stopRequest) log(`Recorder error: ${error.message}`);
this._stopRequest.resolve();
else if (this._onErrorCallback) {
this._onErrorCallback(); this._onErrorCallback();
delete this._onErrorCallback;
}
if (this._startRequest) {
this._startRequest.reject(error);
delete this._startRequest;
}
if (this._stopRequest) {
this._stopRequest.reject(error);
delete this._stopRequest;
}
}
_handleFatalPipelineError(message) {
this._pipelineState = PipelineState.ERROR;
this._bailOutOnError(new Error(`Fatal pipeline error: ${message}`));
}
_senderVanished() {
this._bailOutOnError(new Error('Sender has vanished'));
} }
_onSessionClosed() { _onSessionClosed() {
switch (this._pipelineState) { if (this._sessionState === SessionState.STOPPED)
case PipelineState.STOPPED: return; // We closed the session ourselves
break;
default: this._sessionState = SessionState.STOPPED;
this._pipeline.set_state(Gst.State.NULL); this._bailOutOnError(new Error('Session closed unexpectedly'));
log(`Unexpected pipeline state: ${this._pipelineState}`);
break;
}
this._notifyStopped();
} }
_initSession(sessionPath) { _initSession(sessionPath) {
@ -194,31 +228,20 @@ var Recorder = class {
}); });
} }
_stopSession() {
this._sessionProxy.StopSync();
this._sessionState = SessionState.STOPPED;
}
_onBusMessage(bus, message, _) { _onBusMessage(bus, message, _) {
switch (message.type) { switch (message.type) {
case Gst.MessageType.EOS: case Gst.MessageType.EOS:
this._pipeline.set_state(Gst.State.NULL); this._teardownPipeline();
this._addRecentItem();
switch (this._pipelineState) { switch (this._pipelineState) {
case PipelineState.FLUSHING: case PipelineState.FLUSHING:
this._pipelineState = PipelineState.STOPPED; this._addRecentItem();
break;
default:
break;
}
switch (this._sessionState) { this._unwatchSender();
case SessionState.ACTIVE:
this._stopSession(); this._stopSession();
break;
case SessionState.STOPPED: this._stopRequest.resolve();
this._notifyStopped(); delete this._stopRequest;
break; break;
default: default:
break; break;
@ -258,9 +281,8 @@ var Recorder = class {
this._pipeline = Gst.parse_launch_full(fullPipeline, this._pipeline = Gst.parse_launch_full(fullPipeline,
null, null,
Gst.ParseFlags.FATAL_ERRORS); Gst.ParseFlags.FATAL_ERRORS);
} catch (e) { } catch (e) {
log(`Failed to create pipeline: ${e}`); this._handleFatalPipelineError(`Failed to create pipeline: ${e.message}`);
this._notifyStopped();
} }
return !!this._pipeline; return !!this._pipeline;
} }
@ -309,7 +331,9 @@ var ScreencastService = class extends ServiceImplementation {
} }
_removeRecorder(sender) { _removeRecorder(sender) {
this._recorders.delete(sender); if (!this._recorders.delete(sender))
return;
if (this._recorders.size === 0) if (this._recorders.size === 0)
this.release(); this.release();
} }