FreeRDP
Loading...
Searching...
No Matches
tsmf_X11.c
1/*
2 * FreeRDP: A Remote Desktop Protocol Implementation
3 * Video Redirection Virtual Channel - GStreamer Decoder X11 specifics
4 *
5 * (C) Copyright 2014 Thincast Technologies GmbH
6 * (C) Copyright 2014 Armin Novak <armin.novak@thincast.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#include <sys/types.h>
22#include <sys/mman.h>
23#include <sys/stat.h>
24#ifndef __CYGWIN__
25#include <sys/syscall.h>
26#endif
27
28#include <unistd.h>
29#include <fcntl.h>
30#include <string.h>
31#include <err.h>
32#include <errno.h>
33#include <winpr/thread.h>
34#include <winpr/string.h>
35#include <winpr/platform.h>
36
37#include <gst/gst.h>
38
39#if GST_VERSION_MAJOR > 0
40#include <gst/video/videooverlay.h>
41#else
42#include <gst/interfaces/xoverlay.h>
43#endif
44
45#include <X11/Xlib.h>
46#include <X11/extensions/Xrandr.h>
47#include <X11/extensions/shape.h>
48
49#include <freerdp/channels/tsmf.h>
50
51#include "tsmf_platform.h"
52#include "tsmf_constants.h"
53#include "tsmf_decoder.h"
54
55#if !defined(WITH_XEXT)
56#warning "Building TSMF without shape extension support"
57#endif
58
59struct X11Handle
60{
61 int shmid;
62 int* xfwin;
63#if defined(WITH_XEXT)
64 BOOL has_shape;
65#endif
66 Display* disp;
67 Window subwin;
68 BOOL subwinMapped;
69#if GST_VERSION_MAJOR > 0
70 GstVideoOverlay* overlay;
71#else
72 GstXOverlay* overlay;
73#endif
74 int subwinWidth;
75 int subwinHeight;
76 int subwinX;
77 int subwinY;
78};
79
80static const char* get_shm_id()
81{
82 static char shm_id[128];
83 sprintf_s(shm_id, sizeof(shm_id), "/com.freerdp.xfreerdp.tsmf_%016X", GetCurrentProcessId());
84 return shm_id;
85}
86
87static GstBusSyncReply tsmf_platform_bus_sync_handler(GstBus* bus, GstMessage* message,
88 gpointer user_data)
89{
90 struct X11Handle* hdl;
91
92 TSMFGstreamerDecoder* decoder = user_data;
93
94 if (GST_MESSAGE_TYPE(message) != GST_MESSAGE_ELEMENT)
95 return GST_BUS_PASS;
96
97#if GST_VERSION_MAJOR > 0
98 if (!gst_is_video_overlay_prepare_window_handle_message(message))
99 return GST_BUS_PASS;
100#else
101 if (!gst_structure_has_name(message->structure, "prepare-xwindow-id"))
102 return GST_BUS_PASS;
103#endif
104
105 hdl = (struct X11Handle*)decoder->platform;
106
107 if (hdl->subwin)
108 {
109#if GST_VERSION_MAJOR > 0
110 hdl->overlay = GST_VIDEO_OVERLAY(GST_MESSAGE_SRC(message));
111 gst_video_overlay_set_window_handle(hdl->overlay, hdl->subwin);
112 gst_video_overlay_handle_events(hdl->overlay, FALSE);
113#else
114 hdl->overlay = GST_X_OVERLAY(GST_MESSAGE_SRC(message));
115#if GST_CHECK_VERSION(0, 10, 31)
116 gst_x_overlay_set_window_handle(hdl->overlay, hdl->subwin);
117#else
118 gst_x_overlay_set_xwindow_id(hdl->overlay, hdl->subwin);
119#endif
120 gst_x_overlay_handle_events(hdl->overlay, TRUE);
121#endif
122
123 if (hdl->subwinWidth != -1 && hdl->subwinHeight != -1 && hdl->subwinX != -1 &&
124 hdl->subwinY != -1)
125 {
126#if GST_VERSION_MAJOR > 0
127 if (!gst_video_overlay_set_render_rectangle(hdl->overlay, 0, 0, hdl->subwinWidth,
128 hdl->subwinHeight))
129 {
130 WLog_ERR(TAG, "Could not resize overlay!");
131 }
132
133 gst_video_overlay_expose(hdl->overlay);
134#else
135 if (!gst_x_overlay_set_render_rectangle(hdl->overlay, 0, 0, hdl->subwinWidth,
136 hdl->subwinHeight))
137 {
138 WLog_ERR(TAG, "Could not resize overlay!");
139 }
140
141 gst_x_overlay_expose(hdl->overlay);
142#endif
143 XLockDisplay(hdl->disp);
144 XMoveResizeWindow(hdl->disp, hdl->subwin, hdl->subwinX, hdl->subwinY, hdl->subwinWidth,
145 hdl->subwinHeight);
146 XSync(hdl->disp, FALSE);
147 XUnlockDisplay(hdl->disp);
148 }
149 }
150 else
151 {
152 g_warning("Window was not available before retrieving the overlay!");
153 }
154
155 gst_message_unref(message);
156
157 return GST_BUS_DROP;
158}
159
160const char* tsmf_platform_get_video_sink(void)
161{
162 return "autovideosink";
163}
164
165const char* tsmf_platform_get_audio_sink(void)
166{
167 return "autoaudiosink";
168}
169
170int tsmf_platform_create(TSMFGstreamerDecoder* decoder)
171{
172 struct X11Handle* hdl;
173
174 if (!decoder)
175 return -1;
176
177 if (decoder->platform)
178 return -1;
179
180 hdl = calloc(1, sizeof(struct X11Handle));
181 if (!hdl)
182 {
183 WLog_ERR(TAG, "Could not allocate handle.");
184 return -1;
185 }
186
187 decoder->platform = hdl;
188 hdl->shmid = shm_open(get_shm_id(), (O_RDWR | O_CREAT), (PROT_READ | PROT_WRITE));
189 if (hdl->shmid == -1)
190 {
191 char ebuffer[256] = WINPR_C_ARRAY_INIT;
192 WLog_ERR(TAG, "failed to get access to shared memory - shmget(%s): %i - %s", get_shm_id(),
193 errno, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
194 return -2;
195 }
196
197 hdl->xfwin = mmap(0, sizeof(void*), PROT_READ | PROT_WRITE, MAP_SHARED, hdl->shmid, 0);
198 if (hdl->xfwin == MAP_FAILED)
199 {
200 WLog_ERR(TAG, "shmat failed!");
201 return -3;
202 }
203
204 hdl->disp = XOpenDisplay(nullptr);
205 if (!hdl->disp)
206 {
207 WLog_ERR(TAG, "Failed to open display");
208 return -4;
209 }
210
211 hdl->subwinMapped = FALSE;
212 hdl->subwinX = -1;
213 hdl->subwinY = -1;
214 hdl->subwinWidth = -1;
215 hdl->subwinHeight = -1;
216
217 return 0;
218}
219
220int tsmf_platform_set_format(TSMFGstreamerDecoder* decoder)
221{
222 if (!decoder)
223 return -1;
224
225 if (decoder->media_type == TSMF_MAJOR_TYPE_VIDEO)
226 {
227 }
228
229 return 0;
230}
231
232int tsmf_platform_register_handler(TSMFGstreamerDecoder* decoder)
233{
234 GstBus* bus;
235
236 if (!decoder)
237 return -1;
238
239 if (!decoder->pipe)
240 return -1;
241
242 bus = gst_pipeline_get_bus(GST_PIPELINE(decoder->pipe));
243
244#if GST_VERSION_MAJOR > 0
245 gst_bus_set_sync_handler(bus, (GstBusSyncHandler)tsmf_platform_bus_sync_handler, decoder,
246 nullptr);
247#else
248 gst_bus_set_sync_handler(bus, (GstBusSyncHandler)tsmf_platform_bus_sync_handler, decoder);
249#endif
250
251 if (!bus)
252 {
253 WLog_ERR(TAG, "gst_pipeline_get_bus failed!");
254 return 1;
255 }
256
257 gst_object_unref(bus);
258
259 return 0;
260}
261
262int tsmf_platform_free(TSMFGstreamerDecoder* decoder)
263{
264 struct X11Handle* hdl = decoder->platform;
265
266 if (!hdl)
267 return -1;
268
269 if (hdl->disp)
270 XCloseDisplay(hdl->disp);
271
272 if (hdl->xfwin)
273 munmap(0, sizeof(void*));
274
275 if (hdl->shmid >= 0)
276 close(hdl->shmid);
277
278 free(hdl);
279 decoder->platform = nullptr;
280
281 return 0;
282}
283
284int tsmf_window_create(TSMFGstreamerDecoder* decoder)
285{
286 struct X11Handle* hdl;
287
288 if (decoder->media_type != TSMF_MAJOR_TYPE_VIDEO)
289 {
290 decoder->ready = TRUE;
291 return -3;
292 }
293 else
294 {
295 if (!decoder)
296 return -1;
297
298 if (!decoder->platform)
299 return -1;
300
301 hdl = (struct X11Handle*)decoder->platform;
302
303 if (!hdl->subwin)
304 {
305 XLockDisplay(hdl->disp);
306 hdl->subwin = XCreateSimpleWindow(hdl->disp, *(int*)hdl->xfwin, 0, 0, 1, 1, 0, 0, 0);
307 XUnlockDisplay(hdl->disp);
308
309 if (!hdl->subwin)
310 {
311 WLog_ERR(TAG, "Could not create subwindow!");
312 }
313 }
314
315 tsmf_window_map(decoder);
316
317 decoder->ready = TRUE;
318#if defined(WITH_XEXT)
319 int event, error;
320 XLockDisplay(hdl->disp);
321 hdl->has_shape = XShapeQueryExtension(hdl->disp, &event, &error);
322 XUnlockDisplay(hdl->disp);
323#endif
324 }
325
326 return 0;
327}
328
329int tsmf_window_resize(TSMFGstreamerDecoder* decoder, int x, int y, int width, int height,
330 int nr_rects, RDP_RECT* rects)
331{
332 struct X11Handle* hdl;
333
334 if (!decoder)
335 return -1;
336
337 if (decoder->media_type != TSMF_MAJOR_TYPE_VIDEO)
338 {
339 return -3;
340 }
341
342 if (!decoder->platform)
343 return -1;
344
345 hdl = (struct X11Handle*)decoder->platform;
346 DEBUG_TSMF("resize: x=%d, y=%d, w=%d, h=%d", x, y, width, height);
347
348 if (hdl->overlay)
349 {
350#if GST_VERSION_MAJOR > 0
351
352 if (!gst_video_overlay_set_render_rectangle(hdl->overlay, 0, 0, width, height))
353 {
354 WLog_ERR(TAG, "Could not resize overlay!");
355 }
356
357 gst_video_overlay_expose(hdl->overlay);
358#else
359 if (!gst_x_overlay_set_render_rectangle(hdl->overlay, 0, 0, width, height))
360 {
361 WLog_ERR(TAG, "Could not resize overlay!");
362 }
363
364 gst_x_overlay_expose(hdl->overlay);
365#endif
366 }
367
368 if (hdl->subwin)
369 {
370 hdl->subwinX = x;
371 hdl->subwinY = y;
372 hdl->subwinWidth = width;
373 hdl->subwinHeight = height;
374
375 XLockDisplay(hdl->disp);
376 XMoveResizeWindow(hdl->disp, hdl->subwin, hdl->subwinX, hdl->subwinY, hdl->subwinWidth,
377 hdl->subwinHeight);
378
379 /* Unmap the window if there are no visibility rects */
380 if (nr_rects == 0)
381 tsmf_window_unmap(decoder);
382 else
383 tsmf_window_map(decoder);
384
385#if defined(WITH_XEXT)
386 if (hdl->has_shape)
387 {
388 XRectangle* xrects = nullptr;
389
390 if (nr_rects == 0)
391 {
392 xrects = calloc(1, sizeof(XRectangle));
393 xrects->x = x;
394 xrects->y = y;
395 xrects->width = width;
396 xrects->height = height;
397 }
398 else
399 {
400 xrects = calloc(nr_rects, sizeof(XRectangle));
401 }
402
403 if (xrects)
404 {
405 for (int i = 0; i < nr_rects; i++)
406 {
407 xrects[i].x = rects[i].x - x;
408 xrects[i].y = rects[i].y - y;
409 xrects[i].width = rects[i].width;
410 xrects[i].height = rects[i].height;
411 }
412
413 XShapeCombineRectangles(hdl->disp, hdl->subwin, ShapeBounding, x, y, xrects,
414 nr_rects, ShapeSet, 0);
415 free(xrects);
416 }
417 }
418#endif
419 XSync(hdl->disp, FALSE);
420 XUnlockDisplay(hdl->disp);
421 }
422
423 return 0;
424}
425
426int tsmf_window_map(TSMFGstreamerDecoder* decoder)
427{
428 struct X11Handle* hdl;
429 if (!decoder)
430 return -1;
431
432 hdl = (struct X11Handle*)decoder->platform;
433
434 /* Only need to map the window if it is not currently mapped */
435 if ((hdl->subwin) && (!hdl->subwinMapped))
436 {
437 XLockDisplay(hdl->disp);
438 XMapWindow(hdl->disp, hdl->subwin);
439 hdl->subwinMapped = TRUE;
440 XSync(hdl->disp, FALSE);
441 XUnlockDisplay(hdl->disp);
442 }
443
444 return 0;
445}
446
447int tsmf_window_unmap(TSMFGstreamerDecoder* decoder)
448{
449 struct X11Handle* hdl;
450 if (!decoder)
451 return -1;
452
453 hdl = (struct X11Handle*)decoder->platform;
454
455 /* only need to unmap window if it is currently mapped */
456 if ((hdl->subwin) && (hdl->subwinMapped))
457 {
458 XLockDisplay(hdl->disp);
459 XUnmapWindow(hdl->disp, hdl->subwin);
460 hdl->subwinMapped = FALSE;
461 XSync(hdl->disp, FALSE);
462 XUnlockDisplay(hdl->disp);
463 }
464
465 return 0;
466}
467
468int tsmf_window_destroy(TSMFGstreamerDecoder* decoder)
469{
470 struct X11Handle* hdl;
471
472 if (!decoder)
473 return -1;
474
475 decoder->ready = FALSE;
476
477 if (decoder->media_type != TSMF_MAJOR_TYPE_VIDEO)
478 return -3;
479
480 if (!decoder->platform)
481 return -1;
482
483 hdl = (struct X11Handle*)decoder->platform;
484
485 if (hdl->subwin)
486 {
487 XLockDisplay(hdl->disp);
488 XDestroyWindow(hdl->disp, hdl->subwin);
489 XSync(hdl->disp, FALSE);
490 XUnlockDisplay(hdl->disp);
491 }
492
493 hdl->overlay = nullptr;
494 hdl->subwin = 0;
495 hdl->subwinMapped = FALSE;
496 hdl->subwinX = -1;
497 hdl->subwinY = -1;
498 hdl->subwinWidth = -1;
499 hdl->subwinHeight = -1;
500 return 0;
501}