2020-04-23 18:46:44 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2023-05-25 18:31:22 +00:00
|
|
|
import Gio from 'gi://Gio';
|
|
|
|
import GLib from 'gi://GLib';
|
|
|
|
import Gst from 'gi://Gst?version=1.0';
|
|
|
|
import Gtk from 'gi://Gtk?version=4.0';
|
2020-04-23 18:46:44 +00:00
|
|
|
|
2023-05-25 18:31:22 +00:00
|
|
|
import {ServiceImplementation} from './dbusService.js';
|
2020-09-14 17:55:17 +00:00
|
|
|
|
2023-08-07 15:13:48 +00:00
|
|
|
import {loadInterfaceXML, loadSubInterfaceXML} from './misc/dbusUtils.js';
|
2023-07-10 09:53:00 +00:00
|
|
|
import * as Signals from './misc/signals.js';
|
2020-04-23 18:46:44 +00:00
|
|
|
|
|
|
|
const ScreencastIface = loadInterfaceXML('org.gnome.Shell.Screencast');
|
|
|
|
|
|
|
|
const IntrospectIface = loadInterfaceXML('org.gnome.Shell.Introspect');
|
|
|
|
const IntrospectProxy = Gio.DBusProxy.makeProxyWrapper(IntrospectIface);
|
|
|
|
|
|
|
|
const ScreenCastIface = loadSubInterfaceXML(
|
|
|
|
'org.gnome.Mutter.ScreenCast', 'org.gnome.Mutter.ScreenCast');
|
|
|
|
const ScreenCastSessionIface = loadSubInterfaceXML(
|
|
|
|
'org.gnome.Mutter.ScreenCast.Session', 'org.gnome.Mutter.ScreenCast');
|
|
|
|
const ScreenCastStreamIface = loadSubInterfaceXML(
|
|
|
|
'org.gnome.Mutter.ScreenCast.Stream', 'org.gnome.Mutter.ScreenCast');
|
|
|
|
const ScreenCastProxy = Gio.DBusProxy.makeProxyWrapper(ScreenCastIface);
|
|
|
|
const ScreenCastSessionProxy = Gio.DBusProxy.makeProxyWrapper(ScreenCastSessionIface);
|
|
|
|
const ScreenCastStreamProxy = Gio.DBusProxy.makeProxyWrapper(ScreenCastStreamIface);
|
|
|
|
|
|
|
|
const DEFAULT_FRAMERATE = 30;
|
|
|
|
const DEFAULT_DRAW_CURSOR = true;
|
|
|
|
|
2022-01-28 23:51:31 +00:00
|
|
|
const PIPELINES = [
|
2023-02-11 06:06:14 +00:00
|
|
|
{
|
|
|
|
pipelineString:
|
|
|
|
'capsfilter caps=video/x-raw(memory:DMABuf),max-framerate=%F/1 ! \
|
|
|
|
glupload ! glcolorconvert ! gldownload ! \
|
|
|
|
queue ! \
|
|
|
|
vp8enc cpu-used=16 max-quantizer=17 deadline=1 keyframe-mode=disabled threads=%T static-threshold=1000 buffer-size=20000 ! \
|
|
|
|
queue ! \
|
|
|
|
webmmux',
|
|
|
|
},
|
2022-01-28 23:51:31 +00:00
|
|
|
{
|
|
|
|
pipelineString:
|
|
|
|
'capsfilter caps=video/x-raw,max-framerate=%F/1 ! \
|
|
|
|
videoconvert chroma-mode=none dither=none matrix-mode=output-only n-threads=%T ! \
|
|
|
|
queue ! \
|
|
|
|
vp8enc cpu-used=16 max-quantizer=17 deadline=1 keyframe-mode=disabled threads=%T static-threshold=1000 buffer-size=20000 ! \
|
|
|
|
queue ! \
|
|
|
|
webmmux',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-04-23 18:46:44 +00:00
|
|
|
const PipelineState = {
|
2022-01-28 23:31:00 +00:00
|
|
|
INIT: 'INIT',
|
2022-01-28 23:51:31 +00:00
|
|
|
STARTING: 'STARTING',
|
2022-01-28 23:31:00 +00:00
|
|
|
PLAYING: 'PLAYING',
|
|
|
|
FLUSHING: 'FLUSHING',
|
|
|
|
STOPPED: 'STOPPED',
|
2022-01-28 23:31:15 +00:00
|
|
|
ERROR: 'ERROR',
|
2020-04-23 18:46:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const SessionState = {
|
2022-01-28 23:31:00 +00:00
|
|
|
INIT: 'INIT',
|
|
|
|
ACTIVE: 'ACTIVE',
|
|
|
|
STOPPED: 'STOPPED',
|
2020-04-23 18:46:44 +00:00
|
|
|
};
|
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
class Recorder extends Signals.EventEmitter {
|
2020-04-23 18:46:44 +00:00
|
|
|
constructor(sessionPath, x, y, width, height, filePath, options,
|
2023-04-26 10:29:57 +00:00
|
|
|
invocation) {
|
|
|
|
super();
|
|
|
|
|
2020-04-23 18:46:44 +00:00
|
|
|
this._startInvocation = invocation;
|
|
|
|
this._dbusConnection = invocation.get_connection();
|
|
|
|
this._stopInvocation = null;
|
|
|
|
|
|
|
|
this._x = x;
|
|
|
|
this._y = y;
|
|
|
|
this._width = width;
|
|
|
|
this._height = height;
|
|
|
|
this._filePath = filePath;
|
|
|
|
|
2021-12-17 07:17:35 +00:00
|
|
|
try {
|
|
|
|
const dir = Gio.File.new_for_path(filePath).get_parent();
|
|
|
|
dir.make_directory_with_parents(null);
|
|
|
|
} catch (e) {
|
|
|
|
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS))
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2023-05-30 18:23:14 +00:00
|
|
|
this._pipelineString = null;
|
2020-04-23 18:46:44 +00:00
|
|
|
this._framerate = DEFAULT_FRAMERATE;
|
|
|
|
this._drawCursor = DEFAULT_DRAW_CURSOR;
|
|
|
|
|
2022-01-28 22:50:35 +00:00
|
|
|
this._pipelineState = PipelineState.INIT;
|
|
|
|
this._pipeline = null;
|
|
|
|
|
2020-04-23 18:46:44 +00:00
|
|
|
this._applyOptions(options);
|
|
|
|
this._watchSender(invocation.get_sender());
|
|
|
|
|
2022-01-28 22:50:35 +00:00
|
|
|
this._sessionState = SessionState.INIT;
|
2020-04-23 18:46:44 +00:00
|
|
|
this._initSession(sessionPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
_applyOptions(options) {
|
|
|
|
for (const option in options)
|
2022-08-10 09:56:14 +00:00
|
|
|
options[option] = options[option].deepUnpack();
|
2020-04-23 18:46:44 +00:00
|
|
|
|
|
|
|
if (options['pipeline'] !== undefined)
|
|
|
|
this._pipelineString = options['pipeline'];
|
|
|
|
if (options['framerate'] !== undefined)
|
|
|
|
this._framerate = options['framerate'];
|
|
|
|
if ('draw-cursor' in options)
|
|
|
|
this._drawCursor = options['draw-cursor'];
|
|
|
|
}
|
|
|
|
|
2020-09-14 17:55:17 +00:00
|
|
|
_addRecentItem() {
|
|
|
|
const file = Gio.File.new_for_path(this._filePath);
|
|
|
|
Gtk.RecentManager.get_default().add_item(file.get_uri());
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:46:44 +00:00
|
|
|
_watchSender(sender) {
|
|
|
|
this._nameWatchId = this._dbusConnection.watch_name(
|
|
|
|
sender,
|
|
|
|
Gio.BusNameWatcherFlags.NONE,
|
|
|
|
null,
|
|
|
|
this._senderVanished.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
_unwatchSender() {
|
|
|
|
if (this._nameWatchId !== 0) {
|
|
|
|
this._dbusConnection.unwatch_name(this._nameWatchId);
|
|
|
|
this._nameWatchId = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:50:35 +00:00
|
|
|
_teardownPipeline() {
|
|
|
|
if (!this._pipeline)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (this._pipeline.set_state(Gst.State.NULL) !== Gst.StateChangeReturn.SUCCESS)
|
|
|
|
log('Failed to set pipeline state to NULL');
|
2020-04-23 18:46:44 +00:00
|
|
|
|
2022-01-28 22:50:35 +00:00
|
|
|
this._pipelineState = PipelineState.STOPPED;
|
|
|
|
this._pipeline = null;
|
2020-04-23 18:46:44 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 22:50:35 +00:00
|
|
|
_stopSession() {
|
|
|
|
if (this._sessionState === SessionState.ACTIVE) {
|
|
|
|
this._sessionState = SessionState.STOPPED;
|
|
|
|
this._sessionProxy.StopSync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_bailOutOnError(error) {
|
|
|
|
this._teardownPipeline();
|
2020-04-23 18:46:44 +00:00
|
|
|
this._unwatchSender();
|
2022-01-28 22:50:35 +00:00
|
|
|
this._stopSession();
|
|
|
|
|
|
|
|
log(`Recorder error: ${error.message}`);
|
|
|
|
|
|
|
|
if (this._startRequest) {
|
|
|
|
this._startRequest.reject(error);
|
|
|
|
delete this._startRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._stopRequest) {
|
|
|
|
this._stopRequest.reject(error);
|
|
|
|
delete this._stopRequest;
|
|
|
|
}
|
2023-04-26 10:29:57 +00:00
|
|
|
|
|
|
|
this.emit('error', error);
|
2022-01-28 22:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_handleFatalPipelineError(message) {
|
|
|
|
this._pipelineState = PipelineState.ERROR;
|
|
|
|
this._bailOutOnError(new Error(`Fatal pipeline error: ${message}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
_senderVanished() {
|
|
|
|
this._bailOutOnError(new Error('Sender has vanished'));
|
2020-04-23 18:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onSessionClosed() {
|
2022-01-28 22:50:35 +00:00
|
|
|
if (this._sessionState === SessionState.STOPPED)
|
|
|
|
return; // We closed the session ourselves
|
|
|
|
|
|
|
|
this._sessionState = SessionState.STOPPED;
|
|
|
|
this._bailOutOnError(new Error('Session closed unexpectedly'));
|
2020-04-23 18:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_initSession(sessionPath) {
|
|
|
|
this._sessionProxy = new ScreenCastSessionProxy(Gio.DBus.session,
|
|
|
|
'org.gnome.Mutter.ScreenCast',
|
|
|
|
sessionPath);
|
|
|
|
this._sessionProxy.connectSignal('Closed', this._onSessionClosed.bind(this));
|
|
|
|
}
|
|
|
|
|
2022-01-28 23:51:31 +00:00
|
|
|
_tryNextPipeline() {
|
|
|
|
const {done, value: pipelineConfig} = this._pipelineConfigs.next();
|
|
|
|
if (done) {
|
|
|
|
this._handleFatalPipelineError('All pipelines failed to start');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-26 17:13:27 +00:00
|
|
|
if (this._pipeline) {
|
|
|
|
if (this._pipeline.set_state(Gst.State.NULL) !== Gst.StateChangeReturn.SUCCESS)
|
|
|
|
log('Failed to set pipeline state to NULL');
|
|
|
|
|
|
|
|
this._pipeline = null;
|
|
|
|
}
|
|
|
|
|
2022-01-28 23:51:31 +00:00
|
|
|
try {
|
|
|
|
this._pipeline = this._createPipeline(this._nodeId, pipelineConfig,
|
|
|
|
this._framerate);
|
|
|
|
} catch (error) {
|
|
|
|
this._tryNextPipeline();
|
2021-06-09 14:42:52 +00:00
|
|
|
return;
|
2022-01-28 23:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._pipeline) {
|
|
|
|
this._tryNextPipeline();
|
|
|
|
return;
|
|
|
|
}
|
2020-04-23 18:46:44 +00:00
|
|
|
|
|
|
|
const bus = this._pipeline.get_bus();
|
|
|
|
bus.add_watch(bus, this._onBusMessage.bind(this));
|
|
|
|
|
2022-02-03 10:26:59 +00:00
|
|
|
const retval = this._pipeline.set_state(Gst.State.PLAYING);
|
2020-04-23 18:46:44 +00:00
|
|
|
|
2022-02-03 10:26:59 +00:00
|
|
|
if (retval === Gst.StateChangeReturn.SUCCESS ||
|
|
|
|
retval === Gst.StateChangeReturn.ASYNC) {
|
|
|
|
// We'll wait for the state change message to PLAYING on the bus
|
|
|
|
} else {
|
2022-01-28 23:51:31 +00:00
|
|
|
this._tryNextPipeline();
|
2022-02-03 10:26:59 +00:00
|
|
|
}
|
2020-04-23 18:46:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 18:23:14 +00:00
|
|
|
*_getPipelineConfigs() {
|
|
|
|
if (this._pipelineString) {
|
|
|
|
yield {
|
|
|
|
pipelineString:
|
|
|
|
`capsfilter caps=video/x-raw,max-framerate=%F/1 ! ${this._pipelineString}`,
|
|
|
|
};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fallbackSupported =
|
|
|
|
Gst.Registry.get().check_feature_version('pipewiresrc', 0, 3, 67);
|
|
|
|
if (fallbackSupported)
|
|
|
|
yield* PIPELINES;
|
|
|
|
else
|
|
|
|
yield PIPELINES.at(-1);
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:10:36 +00:00
|
|
|
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;
|
2022-01-28 23:51:31 +00:00
|
|
|
this._nodeId = nodeId;
|
|
|
|
|
|
|
|
this._pipelineState = PipelineState.STARTING;
|
2023-05-30 18:23:14 +00:00
|
|
|
this._pipelineConfigs = this._getPipelineConfigs();
|
2022-01-28 23:51:31 +00:00
|
|
|
this._tryNextPipeline();
|
2022-02-03 10:10:36 +00:00
|
|
|
});
|
|
|
|
this._sessionProxy.StartSync();
|
|
|
|
this._sessionState = SessionState.ACTIVE;
|
|
|
|
});
|
2020-04-23 18:46:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:10:36 +00:00
|
|
|
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());
|
|
|
|
});
|
2020-04-23 18:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onBusMessage(bus, message, _) {
|
|
|
|
switch (message.type) {
|
2022-02-03 10:26:59 +00:00
|
|
|
case Gst.MessageType.STATE_CHANGED: {
|
|
|
|
const [, newState] = message.parse_state_changed();
|
|
|
|
|
2022-01-28 23:51:31 +00:00
|
|
|
if (this._pipelineState === PipelineState.STARTING &&
|
2022-02-03 10:26:59 +00:00
|
|
|
message.src === this._pipeline &&
|
|
|
|
newState === Gst.State.PLAYING) {
|
|
|
|
this._pipelineState = PipelineState.PLAYING;
|
|
|
|
|
|
|
|
this._startRequest.resolve();
|
|
|
|
delete this._startRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:46:44 +00:00
|
|
|
case Gst.MessageType.EOS:
|
|
|
|
switch (this._pipelineState) {
|
2022-01-28 23:51:31 +00:00
|
|
|
case PipelineState.INIT:
|
2022-01-28 23:40:24 +00:00
|
|
|
case PipelineState.STOPPED:
|
|
|
|
case PipelineState.ERROR:
|
|
|
|
// In these cases there should be no pipeline, so should never happen
|
|
|
|
break;
|
|
|
|
|
2022-01-28 23:51:31 +00:00
|
|
|
case PipelineState.STARTING:
|
|
|
|
// This is something we can handle, try to switch to the next pipeline
|
|
|
|
this._tryNextPipeline();
|
|
|
|
break;
|
|
|
|
|
2022-01-28 23:40:24 +00:00
|
|
|
case PipelineState.PLAYING:
|
|
|
|
this._addRecentItem();
|
|
|
|
this._handleFatalPipelineError('Unexpected EOS message');
|
|
|
|
break;
|
|
|
|
|
2020-04-23 18:46:44 +00:00
|
|
|
case PipelineState.FLUSHING:
|
2022-01-28 22:50:35 +00:00
|
|
|
this._addRecentItem();
|
2020-04-23 18:46:44 +00:00
|
|
|
|
2022-01-28 23:40:24 +00:00
|
|
|
this._teardownPipeline();
|
2022-01-28 22:50:35 +00:00
|
|
|
this._unwatchSender();
|
2020-04-23 18:46:44 +00:00
|
|
|
this._stopSession();
|
2022-01-28 22:50:35 +00:00
|
|
|
|
|
|
|
this._stopRequest.resolve();
|
|
|
|
delete this._stopRequest;
|
2020-04-23 18:46:44 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2022-01-28 23:40:24 +00:00
|
|
|
|
|
|
|
case Gst.MessageType.ERROR:
|
|
|
|
switch (this._pipelineState) {
|
2022-01-28 23:51:31 +00:00
|
|
|
case PipelineState.INIT:
|
2022-01-28 23:40:24 +00:00
|
|
|
case PipelineState.STOPPED:
|
|
|
|
case PipelineState.ERROR:
|
|
|
|
// In these cases there should be no pipeline, so should never happen
|
|
|
|
break;
|
|
|
|
|
2022-01-28 23:51:31 +00:00
|
|
|
case PipelineState.STARTING:
|
|
|
|
// This is something we can handle, try to switch to the next pipeline
|
|
|
|
this._tryNextPipeline();
|
|
|
|
break;
|
|
|
|
|
2022-01-28 23:40:24 +00:00
|
|
|
case PipelineState.PLAYING:
|
|
|
|
case PipelineState.FLUSHING:
|
|
|
|
// Everything else we can't handle, so error out
|
|
|
|
this._handleFatalPipelineError(
|
|
|
|
`GStreamer error while in state ${this._pipelineState}: ${message.parse_error()[0].message}`);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2020-04-23 18:46:44 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-28 23:51:31 +00:00
|
|
|
_substituteVariables(pipelineDescr, framerate) {
|
2020-04-23 18:46:44 +00:00
|
|
|
const numProcessors = GLib.get_num_processors();
|
|
|
|
const numThreads = Math.min(Math.max(1, numProcessors), 64);
|
2022-01-28 23:51:31 +00:00
|
|
|
return pipelineDescr.replaceAll('%T', numThreads).replaceAll('%F', framerate);
|
2020-04-23 18:46:44 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 23:51:31 +00:00
|
|
|
_createPipeline(nodeId, pipelineConfig, framerate) {
|
|
|
|
const {pipelineString} = pipelineConfig;
|
|
|
|
const finalPipelineString = this._substituteVariables(pipelineString, framerate);
|
2020-04-23 18:46:44 +00:00
|
|
|
|
2022-01-28 23:51:31 +00:00
|
|
|
const fullPipeline = `
|
2020-04-23 18:46:44 +00:00
|
|
|
pipewiresrc path=${nodeId}
|
|
|
|
do-timestamp=true
|
|
|
|
keepalive-time=1000
|
|
|
|
resend-last=true !
|
2022-01-28 23:51:31 +00:00
|
|
|
${finalPipelineString} !
|
2020-08-10 23:34:03 +00:00
|
|
|
filesink location="${this._filePath}"`;
|
2020-04-23 18:46:44 +00:00
|
|
|
|
2022-01-28 23:51:31 +00:00
|
|
|
return Gst.parse_launch_full(fullPipeline, null,
|
|
|
|
Gst.ParseFlags.FATAL_ERRORS);
|
2020-04-23 18:46:44 +00:00
|
|
|
}
|
2023-07-10 09:53:00 +00:00
|
|
|
}
|
2020-04-23 18:46:44 +00:00
|
|
|
|
2023-05-25 18:31:22 +00:00
|
|
|
export const ScreencastService = class extends ServiceImplementation {
|
2022-10-19 10:43:29 +00:00
|
|
|
static canScreencast() {
|
2022-01-28 23:51:31 +00:00
|
|
|
if (!Gst.init_check(null))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
let elements = [
|
2022-10-24 12:01:03 +00:00
|
|
|
'pipewiresrc',
|
|
|
|
'filesink',
|
|
|
|
];
|
2022-01-28 23:51:31 +00:00
|
|
|
|
|
|
|
if (elements.some(e => Gst.ElementFactory.find(e) === null))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The fallback pipeline must be available, the other ones are not
|
|
|
|
// guaranteed to work because they depend on hw encoders.
|
|
|
|
const fallbackPipeline = PIPELINES.at(-1);
|
|
|
|
|
|
|
|
elements = fallbackPipeline.pipelineString.split('!').map(
|
|
|
|
e => e.trim().split(' ').at(0));
|
|
|
|
|
|
|
|
if (elements.every(e => Gst.ElementFactory.find(e) !== null))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2022-10-19 10:43:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 18:46:44 +00:00
|
|
|
constructor() {
|
|
|
|
super(ScreencastIface, '/org/gnome/Shell/Screencast');
|
|
|
|
|
2022-11-06 10:56:41 +00:00
|
|
|
this.hold(); // gstreamer initializing can take a bit
|
2022-10-19 10:43:29 +00:00
|
|
|
this._canScreencast = ScreencastService.canScreencast();
|
|
|
|
|
2020-04-23 18:46:44 +00:00
|
|
|
Gst.init(null);
|
2021-01-20 01:48:24 +00:00
|
|
|
Gtk.init();
|
2020-04-23 18:46:44 +00:00
|
|
|
|
2022-11-06 10:56:41 +00:00
|
|
|
this.release();
|
|
|
|
|
2020-04-23 18:46:44 +00:00
|
|
|
this._recorders = new Map();
|
|
|
|
this._senders = new Map();
|
|
|
|
|
|
|
|
this._lockdownSettings = new Gio.Settings({
|
|
|
|
schema_id: 'org.gnome.desktop.lockdown',
|
|
|
|
});
|
|
|
|
|
|
|
|
this._proxy = new ScreenCastProxy(Gio.DBus.session,
|
|
|
|
'org.gnome.Mutter.ScreenCast',
|
|
|
|
'/org/gnome/Mutter/ScreenCast');
|
|
|
|
|
|
|
|
this._introspectProxy = new IntrospectProxy(Gio.DBus.session,
|
|
|
|
'org.gnome.Shell.Introspect',
|
|
|
|
'/org/gnome/Shell/Introspect');
|
|
|
|
}
|
|
|
|
|
2022-10-19 10:43:29 +00:00
|
|
|
get ScreencastSupported() {
|
|
|
|
return this._canScreencast;
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:46:44 +00:00
|
|
|
_removeRecorder(sender) {
|
2022-01-28 22:50:35 +00:00
|
|
|
if (!this._recorders.delete(sender))
|
|
|
|
return;
|
|
|
|
|
2020-04-23 18:46:44 +00:00
|
|
|
if (this._recorders.size === 0)
|
|
|
|
this.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
_addRecorder(sender, recorder) {
|
|
|
|
this._recorders.set(sender, recorder);
|
|
|
|
if (this._recorders.size === 1)
|
|
|
|
this.hold();
|
|
|
|
}
|
|
|
|
|
|
|
|
_getAbsolutePath(filename) {
|
|
|
|
if (GLib.path_is_absolute(filename))
|
|
|
|
return filename;
|
|
|
|
|
2023-01-04 13:03:14 +00:00
|
|
|
const videoDir =
|
|
|
|
GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_VIDEOS) ||
|
|
|
|
GLib.get_home_dir();
|
|
|
|
|
2020-04-23 18:46:44 +00:00
|
|
|
return GLib.build_filenamev([videoDir, filename]);
|
|
|
|
}
|
|
|
|
|
|
|
|
_generateFilePath(template) {
|
|
|
|
let filename = '';
|
|
|
|
let escape = false;
|
|
|
|
|
|
|
|
[...template].forEach(c => {
|
|
|
|
if (escape) {
|
|
|
|
switch (c) {
|
|
|
|
case '%':
|
|
|
|
filename += '%';
|
|
|
|
break;
|
|
|
|
case 'd': {
|
|
|
|
const datetime = GLib.DateTime.new_now_local();
|
2022-05-20 16:40:35 +00:00
|
|
|
const datestr = datetime.format('%Y-%m-%d');
|
2020-04-23 18:46:44 +00:00
|
|
|
|
2022-05-20 16:40:35 +00:00
|
|
|
filename += datestr;
|
2020-04-23 18:46:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 't': {
|
|
|
|
const datetime = GLib.DateTime.new_now_local();
|
2022-05-20 16:40:35 +00:00
|
|
|
const datestr = datetime.format('%H-%M-%S');
|
2020-04-23 18:46:44 +00:00
|
|
|
|
2022-05-20 16:40:35 +00:00
|
|
|
filename += datestr;
|
2020-04-23 18:46:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
log(`Warning: Unknown escape ${c}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
escape = false;
|
|
|
|
} else if (c === '%') {
|
|
|
|
escape = true;
|
|
|
|
} else {
|
|
|
|
filename += c;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (escape)
|
|
|
|
filename += '%';
|
|
|
|
|
|
|
|
return this._getAbsolutePath(filename);
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:10:36 +00:00
|
|
|
async ScreencastAsync(params, invocation) {
|
2020-04-23 18:46:44 +00:00
|
|
|
let returnValue = [false, ''];
|
|
|
|
|
|
|
|
if (this._lockdownSettings.get_boolean('disable-save-to-disk')) {
|
|
|
|
invocation.return_value(GLib.Variant.new('(bs)', returnValue));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sender = invocation.get_sender();
|
|
|
|
|
|
|
|
if (this._recorders.get(sender)) {
|
|
|
|
invocation.return_value(GLib.Variant.new('(bs)', returnValue));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [sessionPath] = this._proxy.CreateSessionSync({});
|
|
|
|
|
|
|
|
const [fileTemplate, options] = params;
|
|
|
|
const [screenWidth, screenHeight] = this._introspectProxy.ScreenSize;
|
|
|
|
const filePath = this._generateFilePath(fileTemplate);
|
|
|
|
|
|
|
|
let recorder;
|
|
|
|
|
|
|
|
try {
|
|
|
|
recorder = new Recorder(
|
|
|
|
sessionPath,
|
|
|
|
0, 0,
|
|
|
|
screenWidth, screenHeight,
|
2020-09-02 14:26:34 +00:00
|
|
|
filePath,
|
2020-04-23 18:46:44 +00:00
|
|
|
options,
|
2023-04-26 10:29:57 +00:00
|
|
|
invocation);
|
2020-04-23 18:46:44 +00:00
|
|
|
} catch (error) {
|
|
|
|
log(`Failed to create recorder: ${error.message}`);
|
|
|
|
invocation.return_value(GLib.Variant.new('(bs)', returnValue));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._addRecorder(sender, recorder);
|
|
|
|
|
|
|
|
try {
|
2022-02-03 10:10:36 +00:00
|
|
|
await recorder.startRecording();
|
|
|
|
returnValue = [true, filePath];
|
2020-04-23 18:46:44 +00:00
|
|
|
} catch (error) {
|
|
|
|
log(`Failed to start recorder: ${error.message}`);
|
|
|
|
this._removeRecorder(sender);
|
2022-02-03 10:10:36 +00:00
|
|
|
} finally {
|
2020-04-23 18:46:44 +00:00
|
|
|
invocation.return_value(GLib.Variant.new('(bs)', returnValue));
|
|
|
|
}
|
2023-04-26 10:29:57 +00:00
|
|
|
|
|
|
|
recorder.connect('error', (r, error) => {
|
|
|
|
this._removeRecorder(sender);
|
|
|
|
this._dbusImpl.emit_signal('Error',
|
|
|
|
new GLib.Variant('(s)', [error.message]));
|
|
|
|
});
|
2020-04-23 18:46:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:10:36 +00:00
|
|
|
async ScreencastAreaAsync(params, invocation) {
|
2020-04-23 18:46:44 +00:00
|
|
|
let returnValue = [false, ''];
|
|
|
|
|
|
|
|
if (this._lockdownSettings.get_boolean('disable-save-to-disk')) {
|
|
|
|
invocation.return_value(GLib.Variant.new('(bs)', returnValue));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sender = invocation.get_sender();
|
|
|
|
|
|
|
|
if (this._recorders.get(sender)) {
|
|
|
|
invocation.return_value(GLib.Variant.new('(bs)', returnValue));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [sessionPath] = this._proxy.CreateSessionSync({});
|
|
|
|
|
|
|
|
const [x, y, width, height, fileTemplate, options] = params;
|
|
|
|
const filePath = this._generateFilePath(fileTemplate);
|
|
|
|
|
|
|
|
let recorder;
|
|
|
|
|
|
|
|
try {
|
|
|
|
recorder = new Recorder(
|
|
|
|
sessionPath,
|
|
|
|
x, y,
|
|
|
|
width, height,
|
|
|
|
filePath,
|
|
|
|
options,
|
2023-04-26 10:29:57 +00:00
|
|
|
invocation);
|
2020-04-23 18:46:44 +00:00
|
|
|
} catch (error) {
|
|
|
|
log(`Failed to create recorder: ${error.message}`);
|
|
|
|
invocation.return_value(GLib.Variant.new('(bs)', returnValue));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._addRecorder(sender, recorder);
|
|
|
|
|
|
|
|
try {
|
2022-02-03 10:10:36 +00:00
|
|
|
await recorder.startRecording();
|
|
|
|
returnValue = [true, filePath];
|
2020-04-23 18:46:44 +00:00
|
|
|
} catch (error) {
|
|
|
|
log(`Failed to start recorder: ${error.message}`);
|
|
|
|
this._removeRecorder(sender);
|
2022-02-03 10:10:36 +00:00
|
|
|
} finally {
|
2020-04-23 18:46:44 +00:00
|
|
|
invocation.return_value(GLib.Variant.new('(bs)', returnValue));
|
|
|
|
}
|
2023-04-26 10:29:57 +00:00
|
|
|
|
|
|
|
recorder.connect('error', (r, error) => {
|
|
|
|
this._removeRecorder(sender);
|
|
|
|
this._dbusImpl.emit_signal('Error',
|
|
|
|
new GLib.Variant('(s)', [error.message]));
|
|
|
|
});
|
2020-04-23 18:46:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:10:36 +00:00
|
|
|
async StopScreencastAsync(params, invocation) {
|
2020-04-23 18:46:44 +00:00
|
|
|
const sender = invocation.get_sender();
|
|
|
|
|
|
|
|
const recorder = this._recorders.get(sender);
|
|
|
|
if (!recorder) {
|
|
|
|
invocation.return_value(GLib.Variant.new('(b)', [false]));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:10:36 +00:00
|
|
|
try {
|
|
|
|
await recorder.stopRecording();
|
|
|
|
} catch (error) {
|
|
|
|
log(`${sender}: Error while stopping recorder: ${error.message}`);
|
|
|
|
} finally {
|
2020-04-23 18:46:44 +00:00
|
|
|
this._removeRecorder(sender);
|
|
|
|
invocation.return_value(GLib.Variant.new('(b)', [true]));
|
2022-02-03 10:10:36 +00:00
|
|
|
}
|
2020-04-23 18:46:44 +00:00
|
|
|
}
|
|
|
|
};
|