1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/*
* Copyright (C) 2015-2017 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
*/
#include "config.h"
#include "backends/meta-screen-cast-session.h"
#include "backends/meta-backend-private.h"
#include "backends/meta-dbus-session-watcher.h"
#include "backends/meta-screen-cast-monitor-stream.h"
#include "backends/meta-screen-cast-stream.h"
#define META_SCREEN_CAST_SESSION_DBUS_PATH "/org/gnome/Mutter/ScreenCast/Session"
struct _MetaScreenCastSession
{
MetaDBusScreenCastSessionSkeleton parent;
char *peer_name;
MetaScreenCastSessionType session_type;
char *object_path;
GList *streams;
};
static void
meta_screen_cast_session_init_iface (MetaDBusScreenCastSessionIface *iface);
static void
meta_dbus_session_init_iface (MetaDbusSessionInterface *iface);
G_DEFINE_TYPE_WITH_CODE (MetaScreenCastSession,
meta_screen_cast_session,
META_DBUS_TYPE_SCREEN_CAST_SESSION_SKELETON,
G_IMPLEMENT_INTERFACE (META_DBUS_TYPE_SCREEN_CAST_SESSION,
meta_screen_cast_session_init_iface)
G_IMPLEMENT_INTERFACE (META_TYPE_DBUS_SESSION,
meta_dbus_session_init_iface))
gboolean
meta_screen_cast_session_start (MetaScreenCastSession *session,
GError **error)
{
GList *l;
for (l = session->streams; l; l = l->next)
{
MetaScreenCastStream *stream = l->data;
if (!meta_screen_cast_stream_start (stream, error))
return FALSE;
}
return TRUE;
}
void
meta_screen_cast_session_close (MetaScreenCastSession *session)
{
MetaDBusScreenCastSession *skeleton = META_DBUS_SCREEN_CAST_SESSION (session);
g_list_free_full (session->streams, g_object_unref);
meta_dbus_session_notify_closed (META_DBUS_SESSION (session));
switch (session->session_type)
{
case META_SCREEN_CAST_SESSION_TYPE_NORMAL:
meta_dbus_screen_cast_session_emit_closed (skeleton);
break;
case META_SCREEN_CAST_SESSION_TYPE_REMOTE_DESKTOP:
break;
}
g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (session));
g_object_unref (session);
}
MetaScreenCastStream *
meta_screen_cast_session_get_stream (MetaScreenCastSession *session,
const char *path)
{
GList *l;
for (l = session->streams; l; l = l->next)
{
MetaScreenCastStream *stream = l->data;
if (g_strcmp0 (meta_screen_cast_stream_get_object_path (stream),
path) == 0)
return stream;
}
return NULL;
}
char *
meta_screen_cast_session_get_object_path (MetaScreenCastSession *session)
{
return session->object_path;
}
static gboolean
check_permission (MetaScreenCastSession *session,
GDBusMethodInvocation *invocation)
{
return g_strcmp0 (session->peer_name,
g_dbus_method_invocation_get_sender (invocation)) == 0;
}
static gboolean
handle_start (MetaDBusScreenCastSession *skeleton,
GDBusMethodInvocation *invocation)
{
MetaScreenCastSession *session = META_SCREEN_CAST_SESSION (skeleton);
GError *error = NULL;
if (!check_permission (session, invocation))
{
g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR,
G_DBUS_ERROR_ACCESS_DENIED,
"Permission denied");
return TRUE;
}
switch (session->session_type)
{
case META_SCREEN_CAST_SESSION_TYPE_NORMAL:
break;
case META_SCREEN_CAST_SESSION_TYPE_REMOTE_DESKTOP:
g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR,
G_DBUS_ERROR_FAILED,
"Must be started from remote desktop session");
return TRUE;
}
if (!meta_screen_cast_session_start (session, &error))
{
g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR,
G_DBUS_ERROR_FAILED,
"Failed to start screen cast: %s",
error->message);
g_error_free (error);
return TRUE;
}
meta_dbus_screen_cast_session_complete_start (skeleton, invocation);
return TRUE;
}
static gboolean
handle_stop (MetaDBusScreenCastSession *skeleton,
GDBusMethodInvocation *invocation)
{
MetaScreenCastSession *session = META_SCREEN_CAST_SESSION (skeleton);
if (!check_permission (session, invocation))
{
g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR,
G_DBUS_ERROR_ACCESS_DENIED,
"Permission denied");
return TRUE;
}
switch (session->session_type)
{
case META_SCREEN_CAST_SESSION_TYPE_NORMAL:
break;
case META_SCREEN_CAST_SESSION_TYPE_REMOTE_DESKTOP:
g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR,
G_DBUS_ERROR_FAILED,
"Must be stopped from remote desktop session");
return TRUE;
}
meta_screen_cast_session_close (session);
meta_dbus_screen_cast_session_complete_stop (skeleton, invocation);
return TRUE;
}
static void
on_stream_closed (MetaScreenCastStream *stream,
MetaScreenCastSession *session)
{
meta_screen_cast_session_close (session);
}
static gboolean
handle_record_monitor (MetaDBusScreenCastSession *skeleton,
GDBusMethodInvocation *invocation,
const char *connector,
GVariant *properties_variant)
{
MetaScreenCastSession *session = META_SCREEN_CAST_SESSION (skeleton);
GDBusInterfaceSkeleton *interface_skeleton;
GDBusConnection *connection;
MetaBackend *backend = meta_get_backend ();
MetaMonitorManager *monitor_manager =
meta_backend_get_monitor_manager (backend);
MetaMonitor *monitor;
ClutterStage *stage;
GError *error = NULL;
MetaScreenCastMonitorStream *monitor_stream;
MetaScreenCastStream *stream;
char *stream_path;
if (!check_permission (session, invocation))
{
g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR,
G_DBUS_ERROR_ACCESS_DENIED,
"Permission denied");
return TRUE;
}
interface_skeleton = G_DBUS_INTERFACE_SKELETON (skeleton);
connection = g_dbus_interface_skeleton_get_connection (interface_skeleton);
if (g_str_equal (connector, ""))
monitor = meta_monitor_manager_get_primary_monitor (monitor_manager);
else
monitor = meta_monitor_manager_get_monitor_from_connector (monitor_manager,
connector);
if (!monitor)
{
g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR,
G_DBUS_ERROR_FAILED,
"Unknown monitor");
return TRUE;
}
stage = CLUTTER_STAGE (meta_backend_get_stage (backend));
monitor_stream = meta_screen_cast_monitor_stream_new (connection,
monitor_manager,
monitor,
stage,
&error);
if (!monitor_stream)
{
g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR,
G_DBUS_ERROR_FAILED,
"Failed to record monitor: %s",
error->message);
g_error_free (error);
return TRUE;
}
stream = META_SCREEN_CAST_STREAM (monitor_stream);
stream_path = meta_screen_cast_stream_get_object_path (stream);
session->streams = g_list_append (session->streams, stream);
g_signal_connect (stream, "closed", G_CALLBACK (on_stream_closed), session);
meta_dbus_screen_cast_session_complete_record_monitor (skeleton,
invocation,
stream_path);
return TRUE;
}
static gboolean
handle_record_window (MetaDBusScreenCastSession *skeleton,
GDBusMethodInvocation *invocation,
GVariant *properties_variant)
{
MetaScreenCastSession *session = META_SCREEN_CAST_SESSION (skeleton);
if (!check_permission (session, invocation))
{
g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR,
G_DBUS_ERROR_ACCESS_DENIED,
"Permission denied");
return TRUE;
}
g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR,
G_DBUS_ERROR_FAILED,
"Recording a window not yet supported");
return TRUE;
}
static void
meta_screen_cast_session_init_iface (MetaDBusScreenCastSessionIface *iface)
{
iface->handle_start = handle_start;
iface->handle_stop = handle_stop;
iface->handle_record_monitor = handle_record_monitor;
iface->handle_record_window = handle_record_window;
}
static void
meta_screen_cast_session_client_vanished (MetaDbusSession *dbus_session)
{
meta_screen_cast_session_close (META_SCREEN_CAST_SESSION (dbus_session));
}
static void
meta_dbus_session_init_iface (MetaDbusSessionInterface *iface)
{
iface->client_vanished = meta_screen_cast_session_client_vanished;
}
MetaScreenCastSession *
meta_screen_cast_session_new (MetaScreenCast *screen_cast,
MetaScreenCastSessionType session_type,
const char *peer_name,
GError **error)
{
GDBusInterfaceSkeleton *interface_skeleton;
MetaScreenCastSession *session;
GDBusConnection *connection;
static unsigned int global_session_number = 0;
session = g_object_new (META_TYPE_SCREEN_CAST_SESSION, NULL);
session->session_type = session_type;
session->peer_name = g_strdup (peer_name);
session->object_path =
g_strdup_printf (META_SCREEN_CAST_SESSION_DBUS_PATH "/u%u",
++global_session_number);
interface_skeleton = G_DBUS_INTERFACE_SKELETON (session);
connection = meta_screen_cast_get_connection (screen_cast);
if (!g_dbus_interface_skeleton_export (interface_skeleton,
connection,
session->object_path,
error))
return NULL;
return session;
}
static void
meta_screen_cast_session_finalize (GObject *object)
{
MetaScreenCastSession *session = META_SCREEN_CAST_SESSION (object);
g_free (session->peer_name);
g_free (session->object_path);
G_OBJECT_CLASS (meta_screen_cast_session_parent_class)->finalize (object);
}
static void
meta_screen_cast_session_init (MetaScreenCastSession *session)
{
}
static void
meta_screen_cast_session_class_init (MetaScreenCastSessionClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = meta_screen_cast_session_finalize;
}
|