Compare commits
6 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
ee5faf01e2 | |
|
|
caa465a3eb | |
|
|
88a35b2316 | |
|
|
36e702bfbb | |
|
|
2cd9b4870f | |
|
|
8fe11a0a07 |
|
|
@ -0,0 +1,3 @@
|
||||||
|
Language: Cpp
|
||||||
|
ColumnLimit: 180
|
||||||
|
IndentPPDirectives: AfterHash
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
CompileFlags:
|
||||||
|
Add: ['-I/usr/include/gstreamer-1.0', '-I/usr/lib/glib-2.0/include', '-I/usr/include/glib-2.0', '-I/media/user/luks/Work/Piklema/imx-weston']
|
||||||
|
|
@ -0,0 +1,570 @@
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <linux/input-event-codes.h>
|
||||||
|
#include <linux/uinput.h>
|
||||||
|
#include <msgpack/object.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <wayland-client-protocol.h>
|
||||||
|
#include <wayland-util.h>
|
||||||
|
#include <msgpack.h>
|
||||||
|
#include <cjson/cJSON.h>
|
||||||
|
|
||||||
|
#include "glib-object.h"
|
||||||
|
#include "glib.h"
|
||||||
|
#include "gst/gstbuffer.h"
|
||||||
|
#include "gst/gstinfo.h"
|
||||||
|
#include "gst/gstobject.h"
|
||||||
|
#include "gst/gstparamspecs.h"
|
||||||
|
#include "gst/gstvalue.h"
|
||||||
|
#include "gst/video/video-format.h"
|
||||||
|
#include "gstwestonimagesrc.h"
|
||||||
|
|
||||||
|
// Gtreamer plugin related defines and declarations
|
||||||
|
GST_DEBUG_CATEGORY_STATIC(gst_westonimagesrc_debug);
|
||||||
|
#define GST_CAT_DEFAULT gst_westonimagesrc_debug
|
||||||
|
|
||||||
|
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE("src", GST_PAD_SRC, GST_PAD_ALWAYS,
|
||||||
|
GST_STATIC_CAPS("video/x-raw, "
|
||||||
|
"format = (string) { BGRA, bgra }, "
|
||||||
|
"width = (int) [ 1, 1280 ], "
|
||||||
|
"height = (int) [ 1, 800 ], "
|
||||||
|
"framerate = (fraction) [ 1/1, 30/1 ]"));
|
||||||
|
|
||||||
|
enum {
|
||||||
|
PROP_0,
|
||||||
|
N_PROPERTIES,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Gstreamer related
|
||||||
|
static gboolean gst_westonimagesrc_start(GstBaseSrc *basesrc);
|
||||||
|
static gboolean gst_westonimagesrc_stop(GstBaseSrc *basesrc);
|
||||||
|
static GstFlowReturn gst_westonimagesrc_create(GstBaseSrc *basesrc, guint64 offset, guint size, GstBuffer **buf);
|
||||||
|
static gboolean gst_westonimagesrc_unlock(GstBaseSrc *basesrc);
|
||||||
|
static gboolean gst_westonimagesrc_unlock_stop(GstBaseSrc *basesrc);
|
||||||
|
|
||||||
|
static void gst_westonimagesrc_class_init(GstWestonImageSrcClass *klass);
|
||||||
|
static void gst_westonimagesrc_init(GstWestonImageSrc *westonimagesrc);
|
||||||
|
static void gst_westonimagesrc_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
|
||||||
|
static void gst_westonimagesrc_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
|
||||||
|
static gboolean gst_westonimagesrc_set_caps(GstBaseSrc *src, GstCaps *caps);
|
||||||
|
|
||||||
|
G_DEFINE_TYPE(GstWestonImageSrc, gst_westonimagesrc, GST_TYPE_BASE_SRC);
|
||||||
|
|
||||||
|
// Weston related
|
||||||
|
static void weston_display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags, int width, int height, int refresh);
|
||||||
|
static void weston_display_handle_geometry(void *data, struct wl_output *wl_output, int x, int y, int physical_width, int physical_height, int subpixel, const char *make,
|
||||||
|
const char *model, int transform);
|
||||||
|
static void weston_screenshot_done(void *data, struct weston_screenshooter *screenshooter);
|
||||||
|
static void weston_handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version);
|
||||||
|
static void weston_handle_global_remove(void *data, struct wl_registry *registry, uint32_t name);
|
||||||
|
|
||||||
|
static void weston_handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) { /* XXX: unimplemented */ }
|
||||||
|
static struct wl_buffer *weston_screenshot_create_shm_buffer(int width, int height, void **data_out, struct wl_shm *shm);
|
||||||
|
static int weston_screenshot_set_buffer_size(struct buffer_size *buff_size, struct wl_list *output_list);
|
||||||
|
|
||||||
|
static const struct wl_output_listener output_listener = {weston_display_handle_geometry, weston_display_handle_mode};
|
||||||
|
static const struct weston_screenshooter_listener screenshooter_listener = {weston_screenshot_done};
|
||||||
|
static const struct wl_registry_listener registry_listener = {weston_handle_global, weston_handle_global_remove};
|
||||||
|
|
||||||
|
// Remote control
|
||||||
|
static void rcontrol_init(GstWestonImageSrc *westonimagesrc);
|
||||||
|
static void rcontrol_init_uinput(GstWestonImageSrc *westonimagesrc);
|
||||||
|
static void rcontrol_init_server(GstWestonImageSrc *westonimagesrc);
|
||||||
|
static gpointer rcontrol_listener_thread(gpointer data);
|
||||||
|
static void rcontrol_destroy(GstWestonImageSrc *westonimagesrc);
|
||||||
|
static void rcontrol_emit(GstWestonImageSrc *westonimagesrc, int fd, int type, int code, int value);
|
||||||
|
|
||||||
|
static void rcontrol_emit(GstWestonImageSrc *westonimagesrc, int fd, int type, int code, int value) {
|
||||||
|
struct input_event ev;
|
||||||
|
memset(&ev, 0, sizeof(ev));
|
||||||
|
gettimeofday(&ev.time, NULL);
|
||||||
|
ev.type = type;
|
||||||
|
ev.code = code;
|
||||||
|
ev.value = value;
|
||||||
|
|
||||||
|
if (write(fd, &ev, sizeof(ev)) < 0) {
|
||||||
|
GST_WARNING_OBJECT(westonimagesrc, "write() input_event %i:%i:%i:%i failed", fd, type, code, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rcontrol_init(GstWestonImageSrc *westonimagesrc) {
|
||||||
|
rcontrol_init_uinput(westonimagesrc);
|
||||||
|
rcontrol_init_server(westonimagesrc);
|
||||||
|
}
|
||||||
|
|
||||||
|
static gpointer rcontrol_listener_thread(gpointer data) {
|
||||||
|
const uint16_t port = 7755u;
|
||||||
|
GstWestonImageSrc *westonimagesrc = data;
|
||||||
|
g_mutex_lock(&westonimagesrc->lock);
|
||||||
|
|
||||||
|
westonimagesrc->udp_socket_fd = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||||
|
if (westonimagesrc->udp_socket_fd < 0) {
|
||||||
|
GST_ERROR_OBJECT(westonimagesrc, "socket() UDP socket creation failed: %s", strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ВАЖНО: разрешаем dual-stack (IPv4 + IPv6) */
|
||||||
|
int off = 0;
|
||||||
|
if (setsockopt(westonimagesrc->udp_socket_fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off)) < 0) {
|
||||||
|
GST_WARNING_OBJECT(westonimagesrc, "setsockopt() IPV6_V6ONLY failed: %s", strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sockaddr_in6 addr;
|
||||||
|
memset(&addr, 0, sizeof(addr));
|
||||||
|
addr.sin6_family = AF_INET6;
|
||||||
|
addr.sin6_port = htons(port);
|
||||||
|
addr.sin6_addr = in6addr_any; /* :: */
|
||||||
|
|
||||||
|
if (bind(westonimagesrc->udp_socket_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||||
|
GST_ERROR_OBJECT(westonimagesrc, "bind() UDP socket binding failed: %s", strerror(errno));
|
||||||
|
close(westonimagesrc->udp_socket_fd);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_mutex_unlock(&westonimagesrc->lock);
|
||||||
|
GST_INFO_OBJECT(westonimagesrc, "UDP listener started on port %d\n", port);
|
||||||
|
|
||||||
|
char msgpack_buf[1024u], msg_buf[1024u];
|
||||||
|
char addrstr[INET6_ADDRSTRLEN];
|
||||||
|
int src_port = 0;
|
||||||
|
|
||||||
|
msgpack_zone mempool;
|
||||||
|
msgpack_zone_init(&mempool, sizeof(msgpack_buf));
|
||||||
|
|
||||||
|
while (TRUE) {
|
||||||
|
struct sockaddr_in src;
|
||||||
|
socklen_t srclen = sizeof(src);
|
||||||
|
ssize_t len = recvfrom(westonimagesrc->udp_socket_fd, msgpack_buf, sizeof(msgpack_buf) - 1, 0, (struct sockaddr *)&src, &srclen);
|
||||||
|
|
||||||
|
if (len < 0) {
|
||||||
|
GST_WARNING_OBJECT(westonimagesrc, "recvfrom() failed");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (src.sin_family == AF_INET6) {
|
||||||
|
|
||||||
|
struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&src;
|
||||||
|
inet_ntop(AF_INET6, &s6->sin6_addr, addrstr, sizeof(addrstr));
|
||||||
|
src_port = ntohs(s6->sin6_port);
|
||||||
|
} else if (src.sin_family == AF_INET) {
|
||||||
|
|
||||||
|
struct sockaddr_in *s4 = (struct sockaddr_in *)&src;
|
||||||
|
inet_ntop(AF_INET, &s4->sin_addr, addrstr, sizeof(addrstr));
|
||||||
|
src_port = ntohs(s4->sin_port);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
strcpy(addrstr, "unknown");
|
||||||
|
}
|
||||||
|
|
||||||
|
msgpack_object deserialized;
|
||||||
|
msgpack_unpack(msgpack_buf, sizeof(msgpack_buf), NULL, &mempool, &deserialized);
|
||||||
|
msgpack_object_print_buffer(msg_buf, sizeof(msg_buf), deserialized);
|
||||||
|
|
||||||
|
GST_INFO_OBJECT(westonimagesrc, "[UDP %s:%d]: %s", addrstr, src_port, msg_buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(westonimagesrc->udp_socket_fd);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rcontrol_init_server(GstWestonImageSrc *westonimagesrc) {
|
||||||
|
g_mutex_lock(&westonimagesrc->lock);
|
||||||
|
westonimagesrc->listener_thread = g_thread_new("UDP listener thread", rcontrol_listener_thread, westonimagesrc);
|
||||||
|
g_mutex_unlock(&westonimagesrc->lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rcontrol_init_uinput(GstWestonImageSrc *westonimagesrc) {
|
||||||
|
g_mutex_lock(&westonimagesrc->lock);
|
||||||
|
westonimagesrc->uinput_fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
|
||||||
|
if (westonimagesrc->uinput_fd < 0) {
|
||||||
|
GST_ERROR_OBJECT(westonimagesrc, "open() /dev/uinput failed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init keyboard
|
||||||
|
{
|
||||||
|
ioctl(westonimagesrc->uinput_fd, UI_SET_EVBIT, EV_KEY);
|
||||||
|
|
||||||
|
for (int key = KEY_ESC; key <= KEY_MICMUTE; key++) {
|
||||||
|
ioctl(westonimagesrc->uinput_fd, UI_SET_KEYBIT, key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init mouse
|
||||||
|
{
|
||||||
|
// Mouse events
|
||||||
|
ioctl(westonimagesrc->uinput_fd, UI_SET_EVBIT, EV_REL);
|
||||||
|
|
||||||
|
// Mouse buttons
|
||||||
|
ioctl(westonimagesrc->uinput_fd, UI_SET_KEYBIT, BTN_LEFT);
|
||||||
|
ioctl(westonimagesrc->uinput_fd, UI_SET_KEYBIT, BTN_RIGHT);
|
||||||
|
ioctl(westonimagesrc->uinput_fd, UI_SET_KEYBIT, BTN_MIDDLE);
|
||||||
|
|
||||||
|
// Mouse rels
|
||||||
|
ioctl(westonimagesrc->uinput_fd, UI_SET_RELBIT, REL_X);
|
||||||
|
ioctl(westonimagesrc->uinput_fd, UI_SET_RELBIT, REL_Y);
|
||||||
|
ioctl(westonimagesrc->uinput_fd, UI_SET_RELBIT, REL_WHEEL);
|
||||||
|
ioctl(westonimagesrc->uinput_fd, UI_SET_RELBIT, REL_HWHEEL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init device
|
||||||
|
struct uinput_setup usetup;
|
||||||
|
memset(&usetup, 0, sizeof(usetup));
|
||||||
|
|
||||||
|
snprintf(usetup.name, UINPUT_MAX_NAME_SIZE, "Virtual Keyboard + Mouse");
|
||||||
|
usetup.id.bustype = BUS_USB;
|
||||||
|
usetup.id.vendor = 0x7777;
|
||||||
|
usetup.id.product = 0x8888;
|
||||||
|
usetup.id.version = 1;
|
||||||
|
|
||||||
|
ioctl(westonimagesrc->uinput_fd, UI_SET_EVBIT, EV_SYN);
|
||||||
|
if (ioctl(westonimagesrc->uinput_fd, UI_DEV_SETUP, &usetup) < 0) {
|
||||||
|
GST_ERROR_OBJECT(westonimagesrc, "ioctl() UI_DEV_SETUP failed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ioctl(westonimagesrc->uinput_fd, UI_DEV_CREATE) < 0) {
|
||||||
|
GST_ERROR_OBJECT(westonimagesrc, "ioctl() UI_DEV_CREATE failed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_mutex_unlock(&westonimagesrc->lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rcontrol_destroy(GstWestonImageSrc *westonimagesrc) {
|
||||||
|
ioctl(westonimagesrc->uinput_fd, UI_DEV_DESTROY);
|
||||||
|
close(westonimagesrc->uinput_fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int weston_screenshot_set_buffer_size(struct buffer_size *buff_size, struct wl_list *output_list) {
|
||||||
|
struct screenshooter_output *output;
|
||||||
|
buff_size->min_x = buff_size->min_y = INT_MAX;
|
||||||
|
buff_size->max_x = buff_size->max_y = INT_MIN;
|
||||||
|
int position = 0;
|
||||||
|
|
||||||
|
wl_list_for_each_reverse(output, output_list, link) {
|
||||||
|
output->offset_x = position;
|
||||||
|
position += output->width;
|
||||||
|
}
|
||||||
|
|
||||||
|
wl_list_for_each(output, output_list, link) {
|
||||||
|
buff_size->min_x = MIN(buff_size->min_x, output->offset_x);
|
||||||
|
buff_size->min_y = MIN(buff_size->min_y, output->offset_y);
|
||||||
|
buff_size->max_x = MAX(buff_size->max_x, output->offset_x + output->width);
|
||||||
|
buff_size->max_y = MAX(buff_size->max_y, output->offset_y + output->height);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buff_size->max_x <= buff_size->min_x || buff_size->max_y <= buff_size->min_y)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
buff_size->width = buff_size->max_x - buff_size->min_x;
|
||||||
|
buff_size->height = buff_size->max_y - buff_size->min_y;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct wl_buffer *weston_screenshot_create_shm_buffer(int width, int height, void **data_out, struct wl_shm *shm) {
|
||||||
|
struct wl_shm_pool *pool;
|
||||||
|
struct wl_buffer *buffer;
|
||||||
|
int fd, size, stride;
|
||||||
|
void *data;
|
||||||
|
|
||||||
|
stride = width * 4;
|
||||||
|
size = stride * height;
|
||||||
|
|
||||||
|
fd = os_create_anonymous_file(size);
|
||||||
|
if (fd < 0) {
|
||||||
|
fprintf(stderr, "creating a buffer file for %d B failed: %s\n", size, strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||||
|
if (data == MAP_FAILED) {
|
||||||
|
fprintf(stderr, "mmap failed: %s\n", strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
pool = wl_shm_create_pool(shm, fd, size);
|
||||||
|
close(fd);
|
||||||
|
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_XRGB8888);
|
||||||
|
wl_shm_pool_destroy(pool);
|
||||||
|
|
||||||
|
*data_out = data;
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void weston_handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) {
|
||||||
|
static struct screenshooter_output *output;
|
||||||
|
struct screenshooter_data *sh_data = data;
|
||||||
|
|
||||||
|
if (strcmp(interface, "wl_output") == 0) {
|
||||||
|
|
||||||
|
output = xmalloc(sizeof *output);
|
||||||
|
output->output = wl_registry_bind(registry, name, &wl_output_interface, 1);
|
||||||
|
wl_list_insert(&sh_data->output_list, &output->link);
|
||||||
|
wl_output_add_listener(output->output, &output_listener, output);
|
||||||
|
} else if (strcmp(interface, "wl_shm") == 0) {
|
||||||
|
|
||||||
|
sh_data->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
|
||||||
|
} else if (strcmp(interface, "weston_screenshooter") == 0) {
|
||||||
|
|
||||||
|
sh_data->screenshooter = wl_registry_bind(registry, name, &weston_screenshooter_interface, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void weston_screenshot_done(void *data, struct weston_screenshooter *screenshooter) {
|
||||||
|
struct screenshooter_data *sh_data = data;
|
||||||
|
sh_data->buffer_copy_done = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void weston_display_handle_geometry(void *data, struct wl_output *wl_output, int x, int y, int physical_width, int physical_height, int subpixel, const char *make,
|
||||||
|
const char *model, int transform) {
|
||||||
|
struct screenshooter_output *output;
|
||||||
|
output = wl_output_get_user_data(wl_output);
|
||||||
|
|
||||||
|
if (wl_output == output->output) {
|
||||||
|
output->offset_x = x;
|
||||||
|
output->offset_y = y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void weston_display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags, int width, int height, int refresh) {
|
||||||
|
struct screenshooter_output *output;
|
||||||
|
output = wl_output_get_user_data(wl_output);
|
||||||
|
|
||||||
|
if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
|
||||||
|
output->width = width;
|
||||||
|
output->height = height;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gst_westonimagesrc_class_init(GstWestonImageSrcClass *klass) {
|
||||||
|
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
|
||||||
|
GstElementClass *gstelement_class = GST_ELEMENT_CLASS(klass);
|
||||||
|
GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS(klass);
|
||||||
|
|
||||||
|
gobject_class->set_property = gst_westonimagesrc_set_property;
|
||||||
|
gobject_class->get_property = gst_westonimagesrc_get_property;
|
||||||
|
|
||||||
|
gstbasesrc_class->start = GST_DEBUG_FUNCPTR(gst_westonimagesrc_start);
|
||||||
|
gstbasesrc_class->stop = GST_DEBUG_FUNCPTR(gst_westonimagesrc_stop);
|
||||||
|
gstbasesrc_class->create = GST_DEBUG_FUNCPTR(gst_westonimagesrc_create);
|
||||||
|
|
||||||
|
gstbasesrc_class->set_caps = GST_DEBUG_FUNCPTR(gst_westonimagesrc_set_caps);
|
||||||
|
gstbasesrc_class->get_caps = NULL;
|
||||||
|
gstbasesrc_class->negotiate = NULL;
|
||||||
|
|
||||||
|
gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR(gst_westonimagesrc_unlock);
|
||||||
|
gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR(gst_westonimagesrc_unlock_stop);
|
||||||
|
|
||||||
|
gst_element_class_set_static_metadata(gstelement_class, "Weston Image Source", "Source/Video", "Generates XRGB screenshots",
|
||||||
|
"OlegShishlyannikov oleg.shishlyannikov@piklema.com");
|
||||||
|
gst_element_class_add_static_pad_template(gstelement_class, &src_template);
|
||||||
|
GST_DEBUG_CATEGORY_INIT(gst_westonimagesrc_debug, "westonimagesrc", 0, "Weston Image Source Plugin");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gst_westonimagesrc_init(GstWestonImageSrc *westonimagesrc) {
|
||||||
|
gst_base_src_set_live(GST_BASE_SRC(westonimagesrc), TRUE);
|
||||||
|
gst_base_src_set_format(GST_BASE_SRC(westonimagesrc), GST_FORMAT_TIME);
|
||||||
|
gst_base_src_set_blocksize(GST_BASE_SRC(westonimagesrc), 0);
|
||||||
|
|
||||||
|
westonimagesrc->running = FALSE;
|
||||||
|
westonimagesrc->timeout_id = 0;
|
||||||
|
westonimagesrc->offset = 0;
|
||||||
|
westonimagesrc->framerate_n = 30;
|
||||||
|
westonimagesrc->framerate_d = 1;
|
||||||
|
|
||||||
|
g_mutex_init(&westonimagesrc->lock);
|
||||||
|
g_cond_init(&westonimagesrc->cond);
|
||||||
|
|
||||||
|
// Get current display
|
||||||
|
display = wl_display_connect(NULL);
|
||||||
|
if (display == NULL) {
|
||||||
|
fprintf(stderr, "failed to create display: %s\n", strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
wl_list_init(&sh_data.output_list);
|
||||||
|
registry = wl_display_get_registry(display);
|
||||||
|
wl_registry_add_listener(registry, ®istry_listener, &sh_data);
|
||||||
|
wl_display_dispatch(display);
|
||||||
|
wl_display_roundtrip(display);
|
||||||
|
|
||||||
|
if (sh_data.screenshooter == NULL) {
|
||||||
|
fprintf(stderr, "display doesn't support screenshooter\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
weston_screenshooter_add_listener(sh_data.screenshooter, &screenshooter_listener, &sh_data);
|
||||||
|
weston_screenshot_set_buffer_size(&buff_size, &sh_data.output_list);
|
||||||
|
|
||||||
|
wl_list_for_each(output, &sh_data.output_list, link) {
|
||||||
|
GST_INFO_OBJECT(westonimagesrc, "Creating shared mem buffer ...");
|
||||||
|
output->buffer = weston_screenshot_create_shm_buffer(output->width, output->height, &output->data, sh_data.shm);
|
||||||
|
}
|
||||||
|
|
||||||
|
rcontrol_init(westonimagesrc);
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean gst_westonimagesrc_set_caps(GstBaseSrc *basesrc, GstCaps *caps) {
|
||||||
|
GstWestonImageSrc *westonimagesrc = GST_WESTONIMAGESRC(basesrc);
|
||||||
|
GstStructure *s;
|
||||||
|
const gchar *fmt;
|
||||||
|
|
||||||
|
GST_INFO_OBJECT(westonimagesrc, "set_caps called: %" GST_PTR_FORMAT, caps);
|
||||||
|
s = gst_caps_get_structure(caps, 0);
|
||||||
|
|
||||||
|
gst_structure_get_int(s, "width", &westonimagesrc->width);
|
||||||
|
gst_structure_get_int(s, "height", &westonimagesrc->height);
|
||||||
|
gst_structure_get_fraction(s, "framerate", &westonimagesrc->framerate_n, &westonimagesrc->framerate_d);
|
||||||
|
|
||||||
|
fmt = gst_structure_get_string(s, "format");
|
||||||
|
if (!fmt)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
westonimagesrc->format = gst_video_format_from_string(fmt);
|
||||||
|
GST_INFO_OBJECT(westonimagesrc, "Caps accepted: %s %dx%d @ %d/%d", fmt, westonimagesrc->width, westonimagesrc->height, westonimagesrc->framerate_n, westonimagesrc->framerate_d);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gst_westonimagesrc_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) {
|
||||||
|
GstWestonImageSrc *westonimagesrc = GST_WESTONIMAGESRC(object);
|
||||||
|
GST_OBJECT_LOCK(westonimagesrc);
|
||||||
|
|
||||||
|
switch (prop_id) {
|
||||||
|
default: {
|
||||||
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
|
||||||
|
|
||||||
|
} break;
|
||||||
|
}
|
||||||
|
|
||||||
|
GST_OBJECT_UNLOCK(westonimagesrc);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gst_westonimagesrc_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
|
||||||
|
GstWestonImageSrc *westonimagesrc = GST_WESTONIMAGESRC(object);
|
||||||
|
GST_OBJECT_LOCK(westonimagesrc);
|
||||||
|
|
||||||
|
switch (prop_id) {
|
||||||
|
default:
|
||||||
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
GST_OBJECT_UNLOCK(westonimagesrc);
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean gst_westonimagesrc_start(GstBaseSrc *basesrc) {
|
||||||
|
GstWestonImageSrc *westonimagesrc = GST_WESTONIMAGESRC(basesrc);
|
||||||
|
|
||||||
|
g_mutex_lock(&westonimagesrc->lock);
|
||||||
|
westonimagesrc->running = TRUE;
|
||||||
|
westonimagesrc->offset = 0;
|
||||||
|
g_mutex_unlock(&westonimagesrc->lock);
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT(westonimagesrc, "started");
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean gst_westonimagesrc_stop(GstBaseSrc *basesrc) {
|
||||||
|
GstWestonImageSrc *westonimagesrc = GST_WESTONIMAGESRC(basesrc);
|
||||||
|
|
||||||
|
g_mutex_lock(&westonimagesrc->lock);
|
||||||
|
westonimagesrc->running = FALSE;
|
||||||
|
g_cond_signal(&westonimagesrc->cond);
|
||||||
|
g_mutex_unlock(&westonimagesrc->lock);
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT(westonimagesrc, "stopped");
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static GstFlowReturn gst_westonimagesrc_create(GstBaseSrc *basesrc, guint64 offset, guint size, GstBuffer **buf) {
|
||||||
|
GstWestonImageSrc *src = GST_WESTONIMAGESRC(basesrc);
|
||||||
|
GstBuffer *buffer;
|
||||||
|
GstMapInfo map;
|
||||||
|
GstClockTime dur;
|
||||||
|
|
||||||
|
g_mutex_lock(&src->lock);
|
||||||
|
if (!src->running) {
|
||||||
|
g_mutex_unlock(&src->lock);
|
||||||
|
return GST_FLOW_FLUSHING;
|
||||||
|
}
|
||||||
|
g_mutex_unlock(&src->lock);
|
||||||
|
|
||||||
|
// Захват кадра
|
||||||
|
wl_list_for_each(output, &sh_data.output_list, link) {
|
||||||
|
weston_screenshooter_shoot(sh_data.screenshooter, output->output, output->buffer);
|
||||||
|
sh_data.buffer_copy_done = 0;
|
||||||
|
|
||||||
|
while (!sh_data.buffer_copy_done)
|
||||||
|
wl_display_roundtrip(display);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Размер кадра
|
||||||
|
size_t frame_size = buff_size.width * buff_size.height * 4; // XRGB
|
||||||
|
/* size_t frame_size = buff_size.width * buff_size.height * 3 / 2; */
|
||||||
|
buffer = gst_buffer_new_allocate(NULL, frame_size, NULL);
|
||||||
|
if (!buffer)
|
||||||
|
return GST_FLOW_ERROR;
|
||||||
|
|
||||||
|
if (!gst_buffer_map(buffer, &map, GST_MAP_WRITE)) {
|
||||||
|
gst_buffer_unref(buffer);
|
||||||
|
return GST_FLOW_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct screenshooter_output *output = wl_container_of(sh_data.output_list.next, output, link);
|
||||||
|
memcpy(map.data, output->data, frame_size);
|
||||||
|
gst_buffer_unmap(buffer, &map);
|
||||||
|
|
||||||
|
// Добавляем мета
|
||||||
|
gst_buffer_add_video_meta(buffer, GST_VIDEO_FRAME_FLAG_NONE, GST_VIDEO_FORMAT_BGRA, buff_size.width, buff_size.height);
|
||||||
|
|
||||||
|
// PTS и Duration
|
||||||
|
dur = gst_util_uint64_scale(GST_SECOND, src->framerate_d, src->framerate_n);
|
||||||
|
GST_BUFFER_PTS(buffer) = src->offset * dur;
|
||||||
|
GST_BUFFER_DURATION(buffer) = dur;
|
||||||
|
src->offset++;
|
||||||
|
|
||||||
|
*buf = buffer;
|
||||||
|
return GST_FLOW_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean gst_westonimagesrc_unlock(GstBaseSrc *basesrc) {
|
||||||
|
GstWestonImageSrc *westonimagesrc = GST_WESTONIMAGESRC(basesrc);
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT(westonimagesrc, "unlock");
|
||||||
|
g_mutex_lock(&westonimagesrc->lock);
|
||||||
|
westonimagesrc->running = FALSE;
|
||||||
|
g_cond_signal(&westonimagesrc->cond);
|
||||||
|
g_mutex_unlock(&westonimagesrc->lock);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean gst_westonimagesrc_unlock_stop(GstBaseSrc *basesrc) {
|
||||||
|
GstWestonImageSrc *westonimagesrc = GST_WESTONIMAGESRC(basesrc);
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT(westonimagesrc, "unlock stop");
|
||||||
|
g_mutex_lock(&westonimagesrc->lock);
|
||||||
|
westonimagesrc->running = TRUE;
|
||||||
|
g_mutex_unlock(&westonimagesrc->lock);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean plugin_init(GstPlugin *plugin) { return gst_element_register(plugin, "westonimagesrc", GST_RANK_NONE, GST_TYPE_WESTONIMAGESRC); }
|
||||||
|
|
||||||
|
#ifndef PACKAGE
|
||||||
|
# define PACKAGE "westonimagesrc"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
GST_PLUGIN_DEFINE(GST_VERSION_MAJOR, GST_VERSION_MINOR, westonimagesrc, "WestonImageSrc World source plugin", plugin_init, "1.0", "LGPL", "GStreamer", "http://gstreamer.net/")
|
||||||
|
|
@ -0,0 +1,110 @@
|
||||||
|
#ifndef __GST_WESTONIMAGESRC_H__
|
||||||
|
#define __GST_WESTONIMAGESRC_H__
|
||||||
|
|
||||||
|
#include <gst/base/gstbasesrc.h>
|
||||||
|
#include <gst/gst.h>
|
||||||
|
#include <gst/video/video.h>
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <wayland-client.h>
|
||||||
|
#include <wayland-util.h>
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
#include "glib.h"
|
||||||
|
#include "weston-screenshooter-client-protocol.h"
|
||||||
|
#include "shared/os-compatibility.h"
|
||||||
|
#include "shared/xalloc.h"
|
||||||
|
#include "shared/file-util.h"
|
||||||
|
|
||||||
|
G_BEGIN_DECLS
|
||||||
|
|
||||||
|
#define GST_TYPE_WESTONIMAGESRC (gst_westonimagesrc_get_type())
|
||||||
|
#define GST_WESTONIMAGESRC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_WESTONIMAGESRC, GstWestonImageSrc))
|
||||||
|
#define GST_WESTONIMAGESRC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_WESTONIMAGESRC, GstWestonImageSrcClass))
|
||||||
|
#define GST_IS_WESTONIMAGESRC(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_WESTONIMAGESRC))
|
||||||
|
#define GST_IS_WESTONIMAGESRC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_WESTONIMAGESRC))
|
||||||
|
|
||||||
|
typedef struct _GstWestonImageSrc GstWestonImageSrc;
|
||||||
|
typedef struct _GstWestonImageSrcClass GstWestonImageSrcClass;
|
||||||
|
|
||||||
|
struct _GstWestonImageSrc {
|
||||||
|
GstBaseSrc element;
|
||||||
|
|
||||||
|
// Video
|
||||||
|
GstVideoInfo video_info;
|
||||||
|
gint width;
|
||||||
|
gint height;
|
||||||
|
GstVideoFormat format;
|
||||||
|
|
||||||
|
// Properties
|
||||||
|
gint framerate_n, framerate_d;
|
||||||
|
GstClockTime duration;
|
||||||
|
|
||||||
|
// Image data
|
||||||
|
gpointer image_data;
|
||||||
|
gsize image_size;
|
||||||
|
|
||||||
|
// Sync
|
||||||
|
GMutex lock;
|
||||||
|
GCond cond;
|
||||||
|
gboolean running;
|
||||||
|
guint timeout_id;
|
||||||
|
guint64 offset;
|
||||||
|
guint64 frame_number;
|
||||||
|
gboolean do_timestamp;
|
||||||
|
|
||||||
|
// Remote control
|
||||||
|
gint uinput_fd, udp_socket_fd;
|
||||||
|
GThread *listener_thread;
|
||||||
|
|
||||||
|
// Stat
|
||||||
|
GstClockTime last_timestamp;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct _GstWestonImageSrcClass {
|
||||||
|
GstBaseSrcClass parent_class;
|
||||||
|
};
|
||||||
|
|
||||||
|
GType gst_westonimagesrc_get_type(void);
|
||||||
|
|
||||||
|
G_END_DECLS
|
||||||
|
|
||||||
|
// Screenshoter logic related declarations and defines
|
||||||
|
struct screenshooter_output {
|
||||||
|
struct wl_output *output;
|
||||||
|
struct wl_buffer *buffer;
|
||||||
|
int width, height, offset_x, offset_y;
|
||||||
|
void *data;
|
||||||
|
struct wl_list link;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct buffer_size {
|
||||||
|
int width, height;
|
||||||
|
int min_x, min_y;
|
||||||
|
int max_x, max_y;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct screenshooter_data {
|
||||||
|
struct wl_shm *shm;
|
||||||
|
struct wl_list output_list;
|
||||||
|
struct weston_screenshooter *screenshooter;
|
||||||
|
int buffer_copy_done;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Weston related structures
|
||||||
|
struct wl_display *display;
|
||||||
|
struct wl_registry *registry;
|
||||||
|
struct screenshooter_output *output;
|
||||||
|
struct buffer_size buff_size = {};
|
||||||
|
struct screenshooter_data sh_data = {};
|
||||||
|
|
||||||
|
#endif /* __GST_WESTONIMAGESRC_H__ */
|
||||||
|
|
@ -343,16 +343,60 @@ if get_option('shell-desktop')
|
||||||
env_modmap += 'weston-screenshooter=@0@;'.format(exe_shooter.full_path())
|
env_modmap += 'weston-screenshooter=@0@;'.format(exe_shooter.full_path())
|
||||||
|
|
||||||
exe_shell_desktop = executable(
|
exe_shell_desktop = executable(
|
||||||
'weston-desktop-shell',
|
'weston-desktop-shell',
|
||||||
'desktop-shell.c',
|
'desktop-shell.c',
|
||||||
weston_desktop_shell_client_protocol_h,
|
weston_desktop_shell_client_protocol_h,
|
||||||
weston_desktop_shell_protocol_c,
|
weston_desktop_shell_protocol_c,
|
||||||
include_directories: common_inc,
|
include_directories: common_inc,
|
||||||
dependencies: dep_toytoolkit,
|
dependencies: dep_toytoolkit,
|
||||||
install_dir: get_option('libexecdir'),
|
install_dir: get_option('libexecdir'),
|
||||||
install: true
|
install: true
|
||||||
)
|
)
|
||||||
env_modmap += 'weston-desktop-shell=@0@;'.format(exe_shell_desktop.full_path())
|
env_modmap += 'weston-desktop-shell=@0@;'.format(exe_shell_desktop.full_path())
|
||||||
|
|
||||||
|
plugins_install_dir = join_paths(get_option('libdir'), 'gstreamer-1.0')
|
||||||
|
cc = meson.get_compiler('c')
|
||||||
|
gst_version = meson.project_version()
|
||||||
|
api_version = '0.1'
|
||||||
|
|
||||||
|
gst_dep = dependency('gstreamer-1.0', version : '>=1.18',
|
||||||
|
fallback : ['gstreamer', 'gst_dep'])
|
||||||
|
|
||||||
|
gstbase_dep = dependency('gstreamer-base-1.0', version : '>=1.18',
|
||||||
|
fallback : ['gstreamer', 'gstbase_dep'])
|
||||||
|
|
||||||
|
gstvideo_dep = dependency('gstreamer-video-1.0', version : '>=1.18',
|
||||||
|
fallback : ['gstreamer', 'gstvideo_dep'])
|
||||||
|
|
||||||
|
msgpack_dep = dependency('msgpack-c')
|
||||||
|
cjson_dep = dependency('cjson')
|
||||||
|
|
||||||
|
gst_westonimagesrc_plugin_c_args = ['-DHAVE_CONFIG_H']
|
||||||
|
|
||||||
|
gst_westonimagesrc_cdata = configuration_data()
|
||||||
|
gst_westonimagesrc_cdata.set_quoted('PACKAGE_VERSION', gst_version)
|
||||||
|
gst_westonimagesrc_cdata.set_quoted('PACKAGE', 'gst-template-plugin')
|
||||||
|
gst_westonimagesrc_cdata.set_quoted('GST_LICENSE', 'LGPL')
|
||||||
|
gst_westonimagesrc_cdata.set_quoted('GST_API_VERSION', api_version)
|
||||||
|
gst_westonimagesrc_cdata.set_quoted('GST_PACKAGE_NAME', 'GStreamer template Plug-ins')
|
||||||
|
gst_westonimagesrc_cdata.set_quoted('GST_PACKAGE_ORIGIN', 'https://gstreamer.freedesktop.org')
|
||||||
|
configure_file(output : 'config.h', configuration : gst_westonimagesrc_cdata)
|
||||||
|
|
||||||
|
# The westonimagesrc Plugin
|
||||||
|
gstwestonimagesrc_sources = [
|
||||||
|
'gstwestonimagesrc.c',
|
||||||
|
]
|
||||||
|
|
||||||
|
gstwestonimagesrcexample = library('gstwestonimagesrc',
|
||||||
|
gstwestonimagesrc_sources,
|
||||||
|
weston_screenshooter_client_protocol_h,
|
||||||
|
weston_screenshooter_protocol_c,
|
||||||
|
include_directories: common_inc,
|
||||||
|
c_args: gst_westonimagesrc_plugin_c_args,
|
||||||
|
dependencies : [gst_dep, gstbase_dep, gstvideo_dep, dep_toytoolkit, msgpack_dep, cjson_dep],
|
||||||
|
install : true,
|
||||||
|
install_dir : plugins_install_dir,
|
||||||
|
)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@
|
||||||
#include <cairo.h>
|
#include <cairo.h>
|
||||||
|
|
||||||
#include <wayland-client.h>
|
#include <wayland-client.h>
|
||||||
|
#include <wayland-util.h>
|
||||||
#include "weston-screenshooter-client-protocol.h"
|
#include "weston-screenshooter-client-protocol.h"
|
||||||
#include "shared/os-compatibility.h"
|
#include "shared/os-compatibility.h"
|
||||||
#include "shared/xalloc.h"
|
#include "shared/xalloc.h"
|
||||||
|
|
@ -69,7 +70,6 @@ struct screenshooter_data {
|
||||||
int buffer_copy_done;
|
int buffer_copy_done;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
display_handle_geometry(void *data,
|
display_handle_geometry(void *data,
|
||||||
struct wl_output *wl_output,
|
struct wl_output *wl_output,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue