FreeRDP/channels/tsmf/client/gstreamer/tsmf_X11.c

317 lines
6.5 KiB
C
Raw Normal View History

2014-05-26 11:01:13 +04:00
/*
* FreeRDP: A Remote Desktop Protocol Implementation
* Video Redirection Virtual Channel - GStreamer Decoder X11 specifics
*
* (C) Copyright 2014 Thincast Technologies GmbH
* (C) Copyright 2014 Armin Novak <armin.novak@thincast.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2014-05-23 15:46:37 +04:00
#include <assert.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <fcntl.h>
#include <winpr/thread.h>
2014-05-25 00:22:33 +04:00
#include <gst/gst.h>
#if GST_VERSION_MAJOR > 0
2014-05-23 15:46:37 +04:00
#include <gst/video/videooverlay.h>
2014-05-25 00:22:33 +04:00
#else
#include <gst/interfaces/xoverlay.h>
#endif
2014-05-23 15:46:37 +04:00
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
#include <X11/extensions/shape.h>
#include <freerdp/channels/tsmf.h>
#include "tsmf_platform.h"
#include "tsmf_constants.h"
#include "tsmf_decoder.h"
#if !defined(WITH_XEXT)
#warning "Building TSMF without shape extension support"
#endif
struct X11Handle
{
int shmid;
int *xfwin;
#if defined(WITH_XEXT)
BOOL has_shape;
#endif
Display *disp;
Window subwin;
};
static const char *get_shm_id()
{
static char shm_id[64];
snprintf(shm_id, sizeof(shm_id), "com.freerdp.xfreerpd.tsmf_%016X", GetCurrentProcessId());
return shm_id;
}
const char *tsmf_platform_get_video_sink(void)
{
return "xvimagesink";
}
const char *tsmf_platform_get_audio_sink(void)
{
return "autoaudiosink";
}
int tsmf_platform_create(TSMFGstreamerDecoder *decoder)
{
struct X11Handle *hdl;
assert(decoder);
assert(!decoder->platform);
hdl = malloc(sizeof(struct X11Handle));
2014-05-25 00:22:33 +04:00
if (!hdl)
2014-05-23 15:46:37 +04:00
{
DEBUG_WARN("%s: Could not allocate handle.", __func__);
return -1;
}
2014-05-25 00:22:33 +04:00
2014-05-23 15:46:37 +04:00
memset(hdl, 0, sizeof(struct X11Handle));
decoder->platform = hdl;
hdl->shmid = shm_open(get_shm_id(), O_RDWR, PROT_READ | PROT_WRITE);;
2014-05-25 00:22:33 +04:00
if (hdl->shmid < 0)
2014-05-23 15:46:37 +04:00
{
DEBUG_WARN("%s: failed to get access to shared memory - shmget()",
__func__);
return -2;
}
else
hdl->xfwin = mmap(0, sizeof(void *), PROT_READ | PROT_WRITE, MAP_SHARED, hdl->shmid, 0);
2014-05-25 00:22:33 +04:00
if (hdl->xfwin == (int *)-1)
2014-05-23 15:46:37 +04:00
{
DEBUG_WARN("%s: shmat failed!", __func__);
return -3;
}
2014-05-25 00:22:33 +04:00
2014-05-23 15:46:37 +04:00
hdl->disp = XOpenDisplay(NULL);
2014-05-25 00:22:33 +04:00
if (!hdl->disp)
2014-05-23 15:46:37 +04:00
{
DEBUG_WARN("Failed to open display");
return -4;
}
2014-05-25 00:22:33 +04:00
2014-05-23 15:46:37 +04:00
return 0;
}
int tsmf_platform_set_format(TSMFGstreamerDecoder *decoder)
{
assert(decoder);
2014-05-25 00:22:33 +04:00
if (decoder->media_type == TSMF_MAJOR_TYPE_VIDEO)
2014-05-23 15:46:37 +04:00
{
}
2014-05-25 00:22:33 +04:00
2014-05-23 15:46:37 +04:00
return 0;
}
int tsmf_platform_register_handler(TSMFGstreamerDecoder *decoder)
{
assert(decoder);
assert(decoder->pipe);
GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(decoder->pipe));
2014-05-25 00:22:33 +04:00
if (!bus)
2014-05-23 15:46:37 +04:00
{
DEBUG_WARN("gst_pipeline_get_bus failed!");
return 1;
}
2014-05-25 00:22:33 +04:00
2014-05-23 15:46:37 +04:00
return 0;
}
int tsmf_platform_free(TSMFGstreamerDecoder *decoder)
{
struct X11Handle *hdl = decoder->platform;
2014-05-25 00:22:33 +04:00
if (!hdl)
2014-05-23 15:46:37 +04:00
return -1;
2014-05-25 00:22:33 +04:00
if (hdl->disp)
2014-05-23 15:46:37 +04:00
XCloseDisplay(hdl->disp);
2014-05-25 00:22:33 +04:00
if (hdl->xfwin)
2014-05-23 15:46:37 +04:00
munmap(0, sizeof(void *));
2014-05-25 00:22:33 +04:00
if (hdl->shmid >= 0)
2014-05-23 15:46:37 +04:00
close(hdl->shmid);
2014-05-25 00:22:33 +04:00
2014-05-23 15:46:37 +04:00
free(hdl);
decoder->platform = NULL;
return 0;
}
int tsmf_window_create(TSMFGstreamerDecoder *decoder)
{
2014-05-25 00:22:33 +04:00
if (decoder->media_type != TSMF_MAJOR_TYPE_VIDEO)
2014-05-23 15:46:37 +04:00
{
decoder->ready = TRUE;
return -3;
}
else
{
2014-05-25 00:22:33 +04:00
#if GST_VERSION_MAJOR > 0
2014-05-23 15:46:37 +04:00
GstVideoOverlay *overlay = GST_VIDEO_OVERLAY(decoder->outsink);
2014-05-25 01:21:05 +04:00
#else
GstXOverlay *overlay = GST_X_OVERLAY(decoder->outsink);
2014-05-25 00:22:33 +04:00
#endif
2014-05-23 15:46:37 +04:00
struct X11Handle *hdl = (struct X11Handle *)decoder->platform;
assert(decoder);
assert(hdl);
2014-05-25 00:22:33 +04:00
if (!hdl->subwin)
2014-05-23 15:46:37 +04:00
{
int event, error;
hdl->subwin = XCreateSimpleWindow(hdl->disp, *(int *)hdl->xfwin, 0, 0, 1, 1, 0, 0, 0);
2014-05-25 00:22:33 +04:00
if (!hdl->subwin)
2014-05-23 15:46:37 +04:00
{
DEBUG_WARN("Could not create subwindow!");
}
2014-05-25 00:22:33 +04:00
2014-05-23 15:46:37 +04:00
XMapWindow(hdl->disp, hdl->subwin);
XSync(hdl->disp, FALSE);
2014-05-25 00:22:33 +04:00
#if GST_VERSION_MAJOR > 0
2014-05-23 15:46:37 +04:00
gst_video_overlay_set_window_handle(overlay, hdl->subwin);
2014-05-25 00:22:33 +04:00
#else
2014-05-25 01:21:05 +04:00
gst_x_overlay_set_window_handle(overlay, hdl->subwin);
2014-05-25 00:22:33 +04:00
#endif
2014-05-23 15:46:37 +04:00
decoder->ready = TRUE;
#if defined(WITH_XEXT)
hdl->has_shape = XShapeQueryExtension(hdl->disp, &event, &error);
#endif
}
2014-05-25 00:22:33 +04:00
#if GST_VERSION_MAJOR > 0
2014-05-23 15:46:37 +04:00
gst_video_overlay_handle_events(overlay, TRUE);
2014-05-25 01:21:05 +04:00
#else
gst_x_overlay_handle_events(overlay, TRUE);
2014-05-25 00:22:33 +04:00
#endif
2014-05-23 15:46:37 +04:00
return 0;
}
}
int tsmf_window_resize(TSMFGstreamerDecoder *decoder, int x, int y, int width,
int height, int nr_rects, RDP_RECT *rects)
{
2014-05-25 00:22:33 +04:00
if (decoder->media_type != TSMF_MAJOR_TYPE_VIDEO)
2014-05-23 15:46:37 +04:00
return -3;
else
{
2014-05-25 00:22:33 +04:00
#if GST_VERSION_MAJOR > 0
2014-05-23 15:46:37 +04:00
GstVideoOverlay *overlay = GST_VIDEO_OVERLAY(decoder->outsink);
2014-05-25 01:21:05 +04:00
#else
GstXOverlay *overlay = GST_X_OVERLAY(decoder->outsink);
2014-05-25 00:22:33 +04:00
#endif
2014-05-23 15:46:37 +04:00
struct X11Handle *hdl = (struct X11Handle *)decoder->platform;
DEBUG_TSMF("resize: x=%d, y=%d, w=%d, h=%d", x, y, width, height);
assert(decoder);
assert(hdl);
2014-05-25 00:22:33 +04:00
#if GST_VERSION_MAJOR > 0
if (!gst_video_overlay_set_render_rectangle(overlay, 0, 0, width, height))
2014-05-23 15:46:37 +04:00
{
DEBUG_WARN("Could not resize overlay!");
}
2014-05-25 00:22:33 +04:00
2014-05-23 15:46:37 +04:00
gst_video_overlay_expose(overlay);
2014-05-25 00:22:33 +04:00
#else
2014-05-25 01:21:05 +04:00
if (!gst_x_overlay_set_render_rectangle(overlay, 0, 0, width, height))
{
DEBUG_WARN("Could not resize overlay!");
}
gst_x_overlay_expose(overlay);
2014-05-25 00:22:33 +04:00
#endif
if (hdl->subwin)
2014-05-23 15:46:37 +04:00
{
XMoveResizeWindow(hdl->disp, hdl->subwin, x, y, width, height);
#if defined(WITH_XEXT)
2014-05-25 00:22:33 +04:00
if (hdl->has_shape)
2014-05-23 15:46:37 +04:00
{
int i;
XRectangle *xrects = calloc(nr_rects, sizeof(XRectangle));
2014-05-25 00:22:33 +04:00
for (i=0; i<nr_rects; i++)
2014-05-23 15:46:37 +04:00
{
xrects[i].x = rects[i].x - x;
xrects[i].y = rects[i].y - y;
xrects[i].width = rects[i].width;
xrects[i].height = rects[i].height;
}
2014-05-25 00:22:33 +04:00
2014-05-23 15:46:37 +04:00
XShapeCombineRectangles(hdl->disp, hdl->subwin, ShapeBounding, x, y, xrects, nr_rects, ShapeSet, 0);
free(xrects);
}
2014-05-25 00:22:33 +04:00
2014-05-23 15:46:37 +04:00
#endif
XSync(hdl->disp, FALSE);
}
2014-05-25 00:22:33 +04:00
2014-05-23 15:46:37 +04:00
return 0;
}
}
int tsmf_window_pause(TSMFGstreamerDecoder *decoder)
{
assert(decoder);
return 0;
}
int tsmf_window_resume(TSMFGstreamerDecoder *decoder)
{
assert(decoder);
return 0;
}
int tsmf_window_destroy(TSMFGstreamerDecoder *decoder)
{
struct X11Handle *hdl = (struct X11Handle *)decoder->platform;
decoder->ready = FALSE;
2014-05-25 00:22:33 +04:00
if (decoder->media_type != TSMF_MAJOR_TYPE_VIDEO)
2014-05-23 15:46:37 +04:00
return -3;
2014-05-25 00:22:33 +04:00
2014-05-23 15:46:37 +04:00
assert(decoder);
assert(hdl);
2014-05-25 00:22:33 +04:00
if (hdl->subwin)
2014-05-23 15:46:37 +04:00
{
XDestroyWindow(hdl->disp, hdl->subwin);
XSync(hdl->disp, FALSE);
}
2014-05-25 00:22:33 +04:00
2014-05-23 15:46:37 +04:00
hdl->subwin = 0;
return 0;
}