From 12e1855ebc0bc1d62b5ed4488698d5198dbaa239 Mon Sep 17 00:00:00 2001 From: fryshorts Date: Sun, 9 Feb 2014 13:50:15 +0100 Subject: [PATCH 01/15] added linux test module with first implementation of a screengrabber using the xshm extension for x11 --- test/CMakeLists.txt | 4 + test/linux/CMakeLists.txt | 29 +++++++ test/linux/linux.c | 25 ++++++ test/linux/linux.h | 15 ++++ test/linux/xshm-input.c | 173 ++++++++++++++++++++++++++++++++++++++ test/linux/xshm-input.h | 27 ++++++ 6 files changed, 273 insertions(+) create mode 100644 test/linux/CMakeLists.txt create mode 100644 test/linux/linux.c create mode 100644 test/linux/linux.h create mode 100644 test/linux/xshm-input.c create mode 100644 test/linux/xshm-input.h diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index afcbf5ec0..6fa01ee6c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -8,3 +8,7 @@ endif() if(APPLE AND UNIX) add_subdirectory(osx) endif() + +if("${CMAKE_SYSTEM_NAME}" MATCHES "Linux") + add_subdirectory(linux) +endif() \ No newline at end of file diff --git a/test/linux/CMakeLists.txt b/test/linux/CMakeLists.txt new file mode 100644 index 000000000..bb2522b7c --- /dev/null +++ b/test/linux/CMakeLists.txt @@ -0,0 +1,29 @@ +project(linux) + +find_package(X11 REQUIRED) + +include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/libobs") +#include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/libobs-opengl") + +set(linux_SOURCES + linux.c + xshm-input.c +) +set(linux_HEADERS + linux.h + xshm-input.h +) + +add_library(linux MODULE + ${linux_SOURCES} + ${linux_HEADERS} +) +target_link_libraries(linux + libobs + ${X11_LIBRARIES} + ${X11_XShm_LIB} +) + +install_obs_plugin(linux) + +#obs_fixup_install_target(xshm-input PATH ${Libx264_LIBRARIES}) diff --git a/test/linux/linux.c b/test/linux/linux.c new file mode 100644 index 000000000..78c11c366 --- /dev/null +++ b/test/linux/linux.c @@ -0,0 +1,25 @@ +#include +#include "linux.h" + +const char *inputs[] = { + "xshm_input" +}; + +uint32_t module_version(uint32_t in_version) +{ + return LIBOBS_API_VER; +} + +bool enum_inputs(size_t idx, const char **name) +{ + if (idx >= (sizeof(inputs)/sizeof(const char*))) + return false; + + *name = inputs[idx]; + return true; +} + +bool enum_filters(size_t idx, const char **name) +{ + return false; +} \ No newline at end of file diff --git a/test/linux/linux.h b/test/linux/linux.h new file mode 100644 index 000000000..b64a7f5c0 --- /dev/null +++ b/test/linux/linux.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +EXPORT uint32_t module_version(uint32_t in_version); +EXPORT bool enum_inputs(size_t idx, const char **name); +EXPORT bool enum_filters(size_t idx, const char **name); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/test/linux/xshm-input.c b/test/linux/xshm-input.c new file mode 100644 index 000000000..5a3ea988a --- /dev/null +++ b/test/linux/xshm-input.c @@ -0,0 +1,173 @@ +#include +#include +#include +#include +#include +#include +#include "xshm-input.h" + +struct xshm_data { + Display *dpy; + Screen *screen; + Window root_window; + Visual *visual; + int depth; + uint32_t width, height; + int shm_attached; + XShmSegmentInfo shm_info; + XImage *image; + texture_t texture; +}; + +const char* xshm_input_getname(const char* locale) +{ + return "X11 Shared Memory Screen Input"; +} + +struct xshm_data *xshm_input_create(const char *settings, obs_source_t source) +{ + // create data structure + struct xshm_data *data = bmalloc(sizeof(struct xshm_data)); + memset(data, 0, sizeof(struct xshm_data)); + + // try to open display and all the good stuff + data->dpy = XOpenDisplay(NULL); + if (!data->dpy) + goto fail; + + data->screen = XDefaultScreenOfDisplay(data->dpy); + data->width = WidthOfScreen(data->screen); + data->height = HeightOfScreen(data->screen); + data->root_window = XRootWindowOfScreen(data->screen); + data->visual = DefaultVisualOfScreen(data->screen); + data->depth = DefaultDepthOfScreen(data->screen); + + // query for shm extension + if (!XShmQueryExtension(data->dpy)) + goto fail; + + // create xshm image + data->image = XShmCreateImage(data->dpy, data->visual, data->depth, + ZPixmap, NULL, &data->shm_info, + data->width, data->height); + if (!data->image) + goto fail; + + printf("Image size: %dx%d\n", data->image->width, data->image->height); + + // create shared memory + data->shm_info.shmid = shmget(IPC_PRIVATE, data->image->bytes_per_line * + data->image->height, IPC_CREAT | 0700); + if (data->shm_info.shmid < 0) { + printf("Failed to create shared memory !\n"); + goto fail; + } + + // attach shared memory + data->shm_info.shmaddr = data->image->data + = (char *) shmat(data->shm_info.shmid, 0, 0); + if (data->shm_info.shmaddr == (char *) -1) { + printf("Failed to attach shared memory !\n"); + goto fail; + } + // set shared memory as read only + data->shm_info.readOnly = False; + + // attach shm + if (!XShmAttach(data->dpy, &data->shm_info)) { + printf("Failed to attach to XShm !\n"); + goto fail; + } + data->shm_attached = 1; + + // get image + if (!XShmGetImage(data->dpy, data->root_window, data->image, + 0, 0, AllPlanes)) { + printf("Failed to get image data !\n"); + goto fail; + } + + // create obs texture + gs_entercontext(obs_graphics()); + data->texture = gs_create_texture(data->width, data->height, GS_BGRA, 1, + (const void**) &data->image->data, + GS_DYNAMIC); + gs_leavecontext(); + + if (!data->texture) + goto fail; + + return data; + +fail: + // clean up and return null + xshm_input_destroy(data); + return NULL; +} + +void xshm_input_destroy(struct xshm_data *data) +{ + if (data) { + gs_entercontext(obs_graphics()); + + texture_destroy(data->texture); + + gs_leavecontext(); + + // detach xshm + if (data->shm_attached) + XShmDetach(data->dpy, &data->shm_info); + + // detach shared memory + if (data->shm_info.shmaddr != (char *) -1) { + shmdt(data->shm_info.shmaddr); + data->shm_info.shmaddr = (char *) -1; + } + + // remove shared memory + if (data->shm_info.shmid != -1) + shmctl(data->shm_info.shmid, IPC_RMID, NULL); + + // destroy image + if (data->image) + XDestroyImage(data->image); + + // close display + if (data->dpy) + XCloseDisplay(data->dpy); + + bfree(data); + } +} + +uint32_t xshm_input_get_output_flags(struct xshm_data *data) +{ + return SOURCE_VIDEO | SOURCE_DEFAULT_EFFECT; +} + +void xshm_input_video_render(struct xshm_data *data, obs_source_t filter_target) +{ + // update texture + if (!XShmGetImage(data->dpy, data->root_window, data->image, + 0, 0, AllPlanes)) { + printf("Failed to get image !\n"); + } + texture_setimage(data->texture, (void *) data->image->data, + data->width * 4, False); + + //effect_t effect = gs_geteffect(); + //eparam_t diffuse = effect_getparambyname(effect, "diffuse"); + + //effect_settexture(effect, diffuse, data->texture); + gs_draw_sprite(data->texture, 0, 0, 0); +} + +uint32_t xshm_input_getwidth(struct xshm_data *data) +{ + return texture_getwidth(data->texture); +} + +uint32_t xshm_input_getheight(struct xshm_data *data) +{ + return texture_getheight(data->texture); +} diff --git a/test/linux/xshm-input.h b/test/linux/xshm-input.h new file mode 100644 index 000000000..9bf19d579 --- /dev/null +++ b/test/linux/xshm-input.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// forward declare struct, so we don't have to include X11 headers here +struct xshm_data; + +EXPORT const char *xshm_input_getname(const char *locale); + +EXPORT struct xshm_data *xshm_input_create(const char *settings, + obs_source_t source); +EXPORT void xshm_input_destroy(struct xshm_data *data); +EXPORT uint32_t xshm_input_get_output_flags(struct xshm_data *data); + +EXPORT void xshm_input_video_render(struct xshm_data *data, + obs_source_t filter_target); + +EXPORT uint32_t xshm_input_getwidth(struct xshm_data *data); +EXPORT uint32_t xshm_input_getheight(struct xshm_data *data); + +#ifdef __cplusplus +} +#endif \ No newline at end of file From 820de55ae2135e8d94a8aaa5bbee3cef6ed8305c Mon Sep 17 00:00:00 2001 From: fryshorts Date: Sun, 9 Feb 2014 20:36:52 +0100 Subject: [PATCH 02/15] Added initial Mouse pointer lib Added library to get the mouse pointer with the XFixes extension and draw it via a sprite. --- test/linux/CMakeLists.txt | 6 +-- test/linux/xcursor.c | 94 +++++++++++++++++++++++++++++++++++++++ test/linux/xcursor.h | 36 +++++++++++++++ test/linux/xshm-input.c | 36 +++++++-------- 4 files changed, 148 insertions(+), 24 deletions(-) create mode 100644 test/linux/xcursor.c create mode 100644 test/linux/xcursor.h diff --git a/test/linux/CMakeLists.txt b/test/linux/CMakeLists.txt index bb2522b7c..20f8be146 100644 --- a/test/linux/CMakeLists.txt +++ b/test/linux/CMakeLists.txt @@ -3,14 +3,15 @@ project(linux) find_package(X11 REQUIRED) include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/libobs") -#include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/libobs-opengl") set(linux_SOURCES linux.c + xcursor.c xshm-input.c ) set(linux_HEADERS linux.h + xcursor.h xshm-input.h ) @@ -22,8 +23,7 @@ target_link_libraries(linux libobs ${X11_LIBRARIES} ${X11_XShm_LIB} + ${X11_Xfixes_LIB} ) install_obs_plugin(linux) - -#obs_fixup_install_target(xshm-input PATH ${Libx264_LIBRARIES}) diff --git a/test/linux/xcursor.c b/test/linux/xcursor.c new file mode 100644 index 000000000..b3f012854 --- /dev/null +++ b/test/linux/xcursor.c @@ -0,0 +1,94 @@ +#include +#include +#include +#include "xcursor.h" + +uint32_t *xcursor_pixels(XFixesCursorImage *xc) { + int size = xc->width * xc->height; + uint32_t *pixels = bmalloc(size * 4); + + // pixel data from XFixes is defined as unsigned long ... + // TODO: check why everybody is making a fuss about this + for (int i = 0; i < size; ++i) + pixels[i] = (uint32_t) xc->pixels[i]; + + return pixels; +} + +void xcursor_create(xcursor_t *data, XFixesCursorImage *xc) { + // get cursor pixel data + uint32_t *pixels = xcursor_pixels(xc); + + // if the cursor has the same size as the last one we can simply update + if (data->tex + && data->last_height == xc->width + && data->last_width == xc->height) { + texture_setimage(data->tex, (void **) pixels, xc->width * 4, False); + } + else { + if (data->tex) + texture_destroy(data->tex); + + data->tex = gs_create_texture( + xc->width, xc->height, + GS_RGBA, 1, + (const void **) &pixels, + GS_DYNAMIC + ); + } + bfree(pixels); + + // set some data + data->last_serial = xc->cursor_serial; + data->last_width = xc->width; + data->last_height = xc->height; +} + +xcursor_t *xcursor_init(Display *dpy) { + xcursor_t *data = bmalloc(sizeof(xcursor_t)); + memset(data, 0, sizeof(xcursor_t)); + + data->dpy = dpy; + + return data; +} + +void xcursor_destroy(xcursor_t *data) { + if (data->tex) + texture_destroy(data->tex); + bfree(data); +} + +void xcursor_render(xcursor_t *data) { + // get cursor data + XFixesCursorImage *xc = XFixesGetCursorImage(data->dpy); + + // update cursor if necessary + if (!data->tex || data->last_serial != xc->cursor_serial) + xcursor_create(data, xc); + + // TODO: why do i need effects ? + effect_t effect = gs_geteffect(); + eparam_t diffuse = effect_getparambyname(effect, "diffuse"); + + effect_settexture(effect, diffuse, data->tex); + + gs_matrix_push(); + + // move cursor to the right position + gs_matrix_translate3f( + -1.0 * (xc->x - xc->xhot), + -1.0 * (xc->y - xc->yhot), + 0 + ); + + // blend cursor + gs_enable_blending(True); + gs_blendfunction(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA); + gs_draw_sprite(data->tex, 0, 0, 0); + gs_enable_blending(False); + + gs_matrix_pop(); + + XFree(xc); +} \ No newline at end of file diff --git a/test/linux/xcursor.h b/test/linux/xcursor.h new file mode 100644 index 000000000..110067362 --- /dev/null +++ b/test/linux/xcursor.h @@ -0,0 +1,36 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + Display *dpy; + unsigned long last_serial; + short unsigned int last_width; + short unsigned int last_height; + texture_t tex; +} xcursor_t; + +/** + * Initializes the xcursor object + */ +xcursor_t *xcursor_init(Display *dpy); + +/** + * Destroys the xcursor object + */ +void xcursor_destroy(xcursor_t *data); + +/** + * Draw the cursor + * + * This needs to be executed within a valid render context + */ +void xcursor_render(xcursor_t *data); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/test/linux/xshm-input.c b/test/linux/xshm-input.c index 5a3ea988a..ee7e12f27 100644 --- a/test/linux/xshm-input.c +++ b/test/linux/xshm-input.c @@ -4,6 +4,7 @@ #include #include #include +#include "xcursor.h" #include "xshm-input.h" struct xshm_data { @@ -17,6 +18,7 @@ struct xshm_data { XShmSegmentInfo shm_info; XImage *image; texture_t texture; + xcursor_t *cursor; }; const char* xshm_input_getname(const char* locale) @@ -53,45 +55,36 @@ struct xshm_data *xshm_input_create(const char *settings, obs_source_t source) if (!data->image) goto fail; - printf("Image size: %dx%d\n", data->image->width, data->image->height); - // create shared memory data->shm_info.shmid = shmget(IPC_PRIVATE, data->image->bytes_per_line * data->image->height, IPC_CREAT | 0700); - if (data->shm_info.shmid < 0) { - printf("Failed to create shared memory !\n"); + if (data->shm_info.shmid < 0) goto fail; - } // attach shared memory data->shm_info.shmaddr = data->image->data = (char *) shmat(data->shm_info.shmid, 0, 0); - if (data->shm_info.shmaddr == (char *) -1) { - printf("Failed to attach shared memory !\n"); + if (data->shm_info.shmaddr == (char *) -1) goto fail; - } // set shared memory as read only data->shm_info.readOnly = False; // attach shm - if (!XShmAttach(data->dpy, &data->shm_info)) { - printf("Failed to attach to XShm !\n"); + if (!XShmAttach(data->dpy, &data->shm_info)) goto fail; - } data->shm_attached = 1; // get image if (!XShmGetImage(data->dpy, data->root_window, data->image, - 0, 0, AllPlanes)) { - printf("Failed to get image data !\n"); + 0, 0, AllPlanes)) goto fail; - } // create obs texture gs_entercontext(obs_graphics()); data->texture = gs_create_texture(data->width, data->height, GS_BGRA, 1, (const void**) &data->image->data, GS_DYNAMIC); + data->cursor = xcursor_init(data->dpy); gs_leavecontext(); if (!data->texture) @@ -111,6 +104,7 @@ void xshm_input_destroy(struct xshm_data *data) gs_entercontext(obs_graphics()); texture_destroy(data->texture); + xcursor_destroy(data->cursor); gs_leavecontext(); @@ -148,18 +142,18 @@ uint32_t xshm_input_get_output_flags(struct xshm_data *data) void xshm_input_video_render(struct xshm_data *data, obs_source_t filter_target) { // update texture - if (!XShmGetImage(data->dpy, data->root_window, data->image, - 0, 0, AllPlanes)) { - printf("Failed to get image !\n"); - } + XShmGetImage(data->dpy, data->root_window, data->image, 0, 0, AllPlanes); texture_setimage(data->texture, (void *) data->image->data, data->width * 4, False); - //effect_t effect = gs_geteffect(); - //eparam_t diffuse = effect_getparambyname(effect, "diffuse"); + effect_t effect = gs_geteffect(); + eparam_t diffuse = effect_getparambyname(effect, "diffuse"); - //effect_settexture(effect, diffuse, data->texture); + effect_settexture(effect, diffuse, data->texture); gs_draw_sprite(data->texture, 0, 0, 0); + + // render the cursor + xcursor_render(data->cursor); } uint32_t xshm_input_getwidth(struct xshm_data *data) From c00d9e3b4e08640d23f18b9b2e82f4deb10719d0 Mon Sep 17 00:00:00 2001 From: fryshorts Date: Mon, 10 Feb 2014 19:09:44 +0100 Subject: [PATCH 03/15] Moved screen grabber code to _tick function Moved the screen grabbing and texture generation to the _tick function in order to keep _render fast. Migrated xshm video source to the new plugin api. --- test/linux/CMakeLists.txt | 2 - test/linux/linux.c | 26 ++----- test/linux/linux.h | 15 ---- test/linux/xcursor.c | 19 +++-- test/linux/xcursor.h | 15 +++- test/linux/xshm-input.c | 146 ++++++++++++++++++++++---------------- test/linux/xshm-input.h | 27 ------- 7 files changed, 119 insertions(+), 131 deletions(-) delete mode 100644 test/linux/linux.h delete mode 100644 test/linux/xshm-input.h diff --git a/test/linux/CMakeLists.txt b/test/linux/CMakeLists.txt index 20f8be146..42656540b 100644 --- a/test/linux/CMakeLists.txt +++ b/test/linux/CMakeLists.txt @@ -10,9 +10,7 @@ set(linux_SOURCES xshm-input.c ) set(linux_HEADERS - linux.h xcursor.h - xshm-input.h ) add_library(linux MODULE diff --git a/test/linux/linux.c b/test/linux/linux.c index 78c11c366..94ce14fb7 100644 --- a/test/linux/linux.c +++ b/test/linux/linux.c @@ -1,25 +1,11 @@ -#include -#include "linux.h" +#include -const char *inputs[] = { - "xshm_input" -}; +OBS_DECLARE_MODULE() -uint32_t module_version(uint32_t in_version) +extern struct obs_source_info xshm_input; + +bool obs_module_load(uint32_t obs_version) { - return LIBOBS_API_VER; -} - -bool enum_inputs(size_t idx, const char **name) -{ - if (idx >= (sizeof(inputs)/sizeof(const char*))) - return false; - - *name = inputs[idx]; + obs_register_source(&xshm_input); return true; } - -bool enum_filters(size_t idx, const char **name) -{ - return false; -} \ No newline at end of file diff --git a/test/linux/linux.h b/test/linux/linux.h deleted file mode 100644 index b64a7f5c0..000000000 --- a/test/linux/linux.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -EXPORT uint32_t module_version(uint32_t in_version); -EXPORT bool enum_inputs(size_t idx, const char **name); -EXPORT bool enum_filters(size_t idx, const char **name); - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/test/linux/xcursor.c b/test/linux/xcursor.c index b3f012854..4e04588cf 100644 --- a/test/linux/xcursor.c +++ b/test/linux/xcursor.c @@ -50,6 +50,9 @@ xcursor_t *xcursor_init(Display *dpy) { data->dpy = dpy; + // initialize texture so we don't crash + xcursor_tick(data); + return data; } @@ -59,7 +62,7 @@ void xcursor_destroy(xcursor_t *data) { bfree(data); } -void xcursor_render(xcursor_t *data) { +void xcursor_tick(xcursor_t *data) { // get cursor data XFixesCursorImage *xc = XFixesGetCursorImage(data->dpy); @@ -67,6 +70,14 @@ void xcursor_render(xcursor_t *data) { if (!data->tex || data->last_serial != xc->cursor_serial) xcursor_create(data, xc); + // update cursor position + data->pos_x = -1.0 * (xc->x - xc->xhot); + data->pos_y = -1.0 * (xc->y - xc->yhot); + + XFree(xc); +} + +void xcursor_render(xcursor_t *data) { // TODO: why do i need effects ? effect_t effect = gs_geteffect(); eparam_t diffuse = effect_getparambyname(effect, "diffuse"); @@ -77,8 +88,8 @@ void xcursor_render(xcursor_t *data) { // move cursor to the right position gs_matrix_translate3f( - -1.0 * (xc->x - xc->xhot), - -1.0 * (xc->y - xc->yhot), + data->pos_x, + data->pos_y, 0 ); @@ -89,6 +100,4 @@ void xcursor_render(xcursor_t *data) { gs_enable_blending(False); gs_matrix_pop(); - - XFree(xc); } \ No newline at end of file diff --git a/test/linux/xcursor.h b/test/linux/xcursor.h index 110067362..b7850b551 100644 --- a/test/linux/xcursor.h +++ b/test/linux/xcursor.h @@ -8,14 +8,18 @@ extern "C" { typedef struct { Display *dpy; + float pos_x; + float pos_y; unsigned long last_serial; - short unsigned int last_width; - short unsigned int last_height; + unsigned short int last_width; + unsigned short int last_height; texture_t tex; } xcursor_t; /** * Initializes the xcursor object + * + * This needs to be executed within a valid render context */ xcursor_t *xcursor_init(Display *dpy); @@ -24,6 +28,13 @@ xcursor_t *xcursor_init(Display *dpy); */ void xcursor_destroy(xcursor_t *data); +/** + * Update the cursor texture + * + * This needs to be executed within a valid render context + */ +void xcursor_tick(xcursor_t *data); + /** * Draw the cursor * diff --git a/test/linux/xshm-input.c b/test/linux/xshm-input.c index ee7e12f27..f8dca2869 100644 --- a/test/linux/xshm-input.c +++ b/test/linux/xshm-input.c @@ -4,15 +4,14 @@ #include #include #include +#include #include "xcursor.h" -#include "xshm-input.h" + +#define XSHM_DATA(voidptr) struct xshm_data *data = voidptr; struct xshm_data { Display *dpy; - Screen *screen; Window root_window; - Visual *visual; - int depth; uint32_t width, height; int shm_attached; XShmSegmentInfo shm_info; @@ -21,12 +20,50 @@ struct xshm_data { xcursor_t *cursor; }; -const char* xshm_input_getname(const char* locale) +static const char* xshm_input_getname(const char* locale) { return "X11 Shared Memory Screen Input"; } -struct xshm_data *xshm_input_create(const char *settings, obs_source_t source) +static void xshm_input_destroy(void *vptr) +{ + XSHM_DATA(vptr); + + if (data) { + gs_entercontext(obs_graphics()); + + texture_destroy(data->texture); + xcursor_destroy(data->cursor); + + gs_leavecontext(); + + // detach xshm + if (data->shm_attached) + XShmDetach(data->dpy, &data->shm_info); + + // detach shared memory + if (data->shm_info.shmaddr != (char *) -1) { + shmdt(data->shm_info.shmaddr); + data->shm_info.shmaddr = (char *) -1; + } + + // remove shared memory + if (data->shm_info.shmid != -1) + shmctl(data->shm_info.shmid, IPC_RMID, NULL); + + // destroy image + if (data->image) + XDestroyImage(data->image); + + // close display + if (data->dpy) + XCloseDisplay(data->dpy); + + bfree(data); + } +} + +static void *xshm_input_create(obs_data_t settings, obs_source_t source) { // create data structure struct xshm_data *data = bmalloc(sizeof(struct xshm_data)); @@ -37,19 +74,19 @@ struct xshm_data *xshm_input_create(const char *settings, obs_source_t source) if (!data->dpy) goto fail; - data->screen = XDefaultScreenOfDisplay(data->dpy); - data->width = WidthOfScreen(data->screen); - data->height = HeightOfScreen(data->screen); - data->root_window = XRootWindowOfScreen(data->screen); - data->visual = DefaultVisualOfScreen(data->screen); - data->depth = DefaultDepthOfScreen(data->screen); + Screen *screen = XDefaultScreenOfDisplay(data->dpy); + data->width = WidthOfScreen(screen); + data->height = HeightOfScreen(screen); + data->root_window = XRootWindowOfScreen(screen); + Visual *visual = DefaultVisualOfScreen(screen); + int depth = DefaultDepthOfScreen(screen); // query for shm extension if (!XShmQueryExtension(data->dpy)) goto fail; // create xshm image - data->image = XShmCreateImage(data->dpy, data->visual, data->depth, + data->image = XShmCreateImage(data->dpy, visual, depth, ZPixmap, NULL, &data->shm_info, data->width, data->height); if (!data->image) @@ -98,70 +135,59 @@ fail: return NULL; } -void xshm_input_destroy(struct xshm_data *data) +static void xshm_input_video_tick(void *vptr, float seconds) { - if (data) { - gs_entercontext(obs_graphics()); - - texture_destroy(data->texture); - xcursor_destroy(data->cursor); - - gs_leavecontext(); - - // detach xshm - if (data->shm_attached) - XShmDetach(data->dpy, &data->shm_info); - - // detach shared memory - if (data->shm_info.shmaddr != (char *) -1) { - shmdt(data->shm_info.shmaddr); - data->shm_info.shmaddr = (char *) -1; - } - - // remove shared memory - if (data->shm_info.shmid != -1) - shmctl(data->shm_info.shmid, IPC_RMID, NULL); - - // destroy image - if (data->image) - XDestroyImage(data->image); - - // close display - if (data->dpy) - XCloseDisplay(data->dpy); - - bfree(data); - } -} - -uint32_t xshm_input_get_output_flags(struct xshm_data *data) -{ - return SOURCE_VIDEO | SOURCE_DEFAULT_EFFECT; -} - -void xshm_input_video_render(struct xshm_data *data, obs_source_t filter_target) -{ - // update texture + XSHM_DATA(vptr); + + gs_entercontext(obs_graphics()); + + // update screen texture XShmGetImage(data->dpy, data->root_window, data->image, 0, 0, AllPlanes); texture_setimage(data->texture, (void *) data->image->data, data->width * 4, False); - effect_t effect = gs_geteffect(); - eparam_t diffuse = effect_getparambyname(effect, "diffuse"); + // update mouse cursor + xcursor_tick(data->cursor); + + gs_leavecontext(); +} +static void xshm_input_video_render(void *vptr, effect_t effect) +{ + XSHM_DATA(vptr); + + eparam_t diffuse = effect_getparambyname(effect, "diffuse"); effect_settexture(effect, diffuse, data->texture); + gs_draw_sprite(data->texture, 0, 0, 0); // render the cursor xcursor_render(data->cursor); } -uint32_t xshm_input_getwidth(struct xshm_data *data) +static uint32_t xshm_input_getwidth(void *vptr) { + XSHM_DATA(vptr); + return texture_getwidth(data->texture); } -uint32_t xshm_input_getheight(struct xshm_data *data) +static uint32_t xshm_input_getheight(void *vptr) { + XSHM_DATA(vptr); + return texture_getheight(data->texture); } + +struct obs_source_info xshm_input = { + .id = "xshm_input", + .type = OBS_SOURCE_TYPE_INPUT, + .output_flags = OBS_SOURCE_VIDEO, + .getname = xshm_input_getname, + .create = xshm_input_create, + .destroy = xshm_input_destroy, + .video_tick = xshm_input_video_tick, + .video_render = xshm_input_video_render, + .getwidth = xshm_input_getwidth, + .getheight = xshm_input_getheight +}; diff --git a/test/linux/xshm-input.h b/test/linux/xshm-input.h deleted file mode 100644 index 9bf19d579..000000000 --- a/test/linux/xshm-input.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -// forward declare struct, so we don't have to include X11 headers here -struct xshm_data; - -EXPORT const char *xshm_input_getname(const char *locale); - -EXPORT struct xshm_data *xshm_input_create(const char *settings, - obs_source_t source); -EXPORT void xshm_input_destroy(struct xshm_data *data); -EXPORT uint32_t xshm_input_get_output_flags(struct xshm_data *data); - -EXPORT void xshm_input_video_render(struct xshm_data *data, - obs_source_t filter_target); - -EXPORT uint32_t xshm_input_getwidth(struct xshm_data *data); -EXPORT uint32_t xshm_input_getheight(struct xshm_data *data); - -#ifdef __cplusplus -} -#endif \ No newline at end of file From 42bc5eddbb36371c4bf7b531d3fdc4304f39a690 Mon Sep 17 00:00:00 2001 From: fryshorts Date: Thu, 13 Feb 2014 22:05:23 +0100 Subject: [PATCH 04/15] added initial pulse audio input plugin - first try at implementing a pulse audio input plugin, it is currently completely untested. - added license headers to source files --- test/linux/CMakeLists.txt | 3 + test/linux/linux.c | 18 +++ test/linux/pulse-input.c | 298 ++++++++++++++++++++++++++++++++++++++ test/linux/xcursor.c | 16 ++ test/linux/xcursor.h | 16 ++ test/linux/xshm-input.c | 18 +++ 6 files changed, 369 insertions(+) create mode 100644 test/linux/pulse-input.c diff --git a/test/linux/CMakeLists.txt b/test/linux/CMakeLists.txt index 42656540b..b63ed9aea 100644 --- a/test/linux/CMakeLists.txt +++ b/test/linux/CMakeLists.txt @@ -1,6 +1,7 @@ project(linux) find_package(X11 REQUIRED) +find_package(PulseAudio REQUIRED) include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/libobs") @@ -8,6 +9,7 @@ set(linux_SOURCES linux.c xcursor.c xshm-input.c + pulse-input.c ) set(linux_HEADERS xcursor.h @@ -22,6 +24,7 @@ target_link_libraries(linux ${X11_LIBRARIES} ${X11_XShm_LIB} ${X11_Xfixes_LIB} + ${PULSEAUDIO_LIBRARY} ) install_obs_plugin(linux) diff --git a/test/linux/linux.c b/test/linux/linux.c index 94ce14fb7..b2fb82d25 100644 --- a/test/linux/linux.c +++ b/test/linux/linux.c @@ -1,11 +1,29 @@ +/* +Copyright (C) 2014 by Leonhard Oelke + +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, see . +*/ #include OBS_DECLARE_MODULE() extern struct obs_source_info xshm_input; +extern struct obs_source_info pulse_input; bool obs_module_load(uint32_t obs_version) { obs_register_source(&xshm_input); + obs_register_source(&pulse_input); return true; } diff --git a/test/linux/pulse-input.c b/test/linux/pulse-input.c new file mode 100644 index 000000000..d03afccee --- /dev/null +++ b/test/linux/pulse-input.c @@ -0,0 +1,298 @@ +/* +Copyright (C) 2014 by Leonhard Oelke + +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, see . +*/ +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#define PULSE_DATA(voidptr) struct pulse_data *data = voidptr; + +/* + * delay in nanos before starting to record, this eliminates problems with + * pulse audio sending weird data/timestamps when the stream is connected + * + * for more information see: + * github.com/MaartenBaert/ssr/blob/master/src/AV/Input/PulseAudioInput.cpp + */ +const uint64_t pulse_start_delay = 100000000; + +struct pulse_data { + pthread_t thread; + event_t event; + obs_source_t source; + + uint32_t frames; + uint32_t samples_per_sec; + uint32_t channels; + enum speaker_layout speakers; + + pa_mainloop *mainloop; + pa_context *context; + pa_stream *stream; +}; + +static void pulse_iterate(struct pulse_data *data) +{ + if (pa_mainloop_prepare(data->mainloop, 1000) < 0) { + blog(LOG_ERROR, "Unable to prepare main loop"); + return; + } + if (pa_mainloop_poll(data->mainloop) < 0) { + blog(LOG_ERROR, "Unable to poll main loop"); + return; + } + if (pa_mainloop_dispatch(data->mainloop) < 0) + blog(LOG_ERROR, "Unable to dispatch main loop"); +} + +static int pulse_connect(struct pulse_data *data) +{ + data->mainloop = pa_mainloop_new(); + if (!data->mainloop) { + blog(LOG_ERROR, "Unable to create main loop"); + return 0; + } + + data->context = pa_context_new( + pa_mainloop_get_api(data->mainloop), "OBS Studio"); + if (!data->context) { + blog(LOG_ERROR, "Unable to create context"); + return 0; + } + + int status = pa_context_connect( + data->context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL); + if (status < 0) { + blog(LOG_ERROR, "Unable to connect! Status: %d", status); + return 0; + } + + // wait until connected + for (;;) { + pulse_iterate(data); + pa_context_state_t state = pa_context_get_state(data->context); + if (state == PA_CONTEXT_READY) { + blog(LOG_DEBUG, "Context ready"); + break; + } + if (!PA_CONTEXT_IS_GOOD(state)) { + blog(LOG_ERROR, "Connection attempt failed"); + return 0; + } + } + + return 1; +} + +static void pulse_disconnect(struct pulse_data *data) +{ + if (data->context) { + pa_context_disconnect(data->context); + pa_context_unref(data->context); + } + + if (data->mainloop) + pa_mainloop_free(data->mainloop); +} + +static int pulse_connect_stream(struct pulse_data *data) +{ + pa_sample_spec spec; + spec.format = PA_SAMPLE_U8; + spec.rate = data->samples_per_sec; + spec.channels = data->channels; + + pa_buffer_attr attr; + attr.fragsize = data->frames * spec.channels; + attr.maxlength = (uint32_t) -1; + attr.minreq = (uint32_t) -1; + attr.prebuf = (uint32_t) -1; + attr.tlength = (uint32_t) -1; + + data->stream = pa_stream_new( + data->context, "OBS Audio Input", &spec, NULL); + if (!data->stream) { + blog(LOG_ERROR, "Unable to create stream"); + return 0; + } + pa_stream_flags_t flags = + PA_STREAM_INTERPOLATE_TIMING + | PA_STREAM_AUTO_TIMING_UPDATE + | PA_STREAM_ADJUST_LATENCY; + if (pa_stream_connect_record(data->stream, NULL, &attr, flags) < 0) { + blog(LOG_ERROR, "Unable to connect to stream"); + return 0; + } + + for (;;) { + pulse_iterate(data); + pa_stream_state_t state = pa_stream_get_state(data->stream); + if (state == PA_STREAM_READY) { + blog(LOG_DEBUG, "Stream ready"); + break; + } + if (!PA_STREAM_IS_GOOD(state)) { + blog(LOG_ERROR, "Stream connection failed"); + return 0; + } + } + + return 1; +} + +static void pulse_diconnect_stream(struct pulse_data *data) +{ + if (data->stream) { + pa_stream_disconnect(data->stream); + pa_stream_unref(data->stream); + } +} + +static void *pulse_thread(void *vptr) +{ + PULSE_DATA(vptr); + + if (!pulse_connect(data)) + return NULL; + if (!pulse_connect_stream(data)) + return NULL; + + uint64_t skip = 1; + uint8_t *out_buffer = bmalloc(data->frames * data->channels); + + while (event_try(&data->event) == EAGAIN) { + uint64_t cur_time = os_gettime_ns(); + + pulse_iterate(data); + + const void *in_buffer; + size_t bytes; + pa_stream_peek(data->stream, &in_buffer, &bytes); + + // check if we got data + if (!bytes) { + pa_stream_drop(data->stream); + continue; + } + + // skip the first frames received + if (skip) { + // start delay when we receive the first bytes + if (skip == 1 && bytes) + skip = os_gettime_ns(); + if (skip + pulse_start_delay < os_gettime_ns()) + skip = 0; + pa_stream_drop(data->stream); + continue; + } + + // get stream latency + pa_usec_t l_abs; + int l_sign; + pa_stream_get_latency(data->stream, &l_abs, &l_sign); + int64_t latency = (l_sign) ? -(int64_t) l_abs : (int64_t) l_abs; + + // copy data to local buffer + memcpy(out_buffer, in_buffer, bytes); + + pa_stream_drop(data->stream); + + // TODO: deinterleave data ? + + // push structure + struct source_audio out; + out.data[0] = out_buffer; + out.data[1] = out_buffer + (bytes / data->channels); + out.frames = bytes / data->channels; + out.speakers = data->speakers; + out.samples_per_sec = data->samples_per_sec; + out.timestamp = cur_time - (latency * 1000); + out.format = AUDIO_FORMAT_U8BIT; + + // send data to obs + obs_source_output_audio(data->source, &out); + } + + bfree(out_buffer); + + pulse_diconnect_stream(data); + pulse_disconnect(data); + + return NULL; +} + +static const char *pulse_getname(const char *locale) +{ + return "Pulse Audio Input"; +} + +static void pulse_destroy(void *vptr) +{ + PULSE_DATA(vptr); + + if (!data) + return; + + if (data->thread) { + void *ret; + event_signal(&data->event); + pthread_join(data->thread, &ret); + } + + event_destroy(&data->event); + + bfree(data); +} + +static void *pulse_create(obs_data_t settings, obs_source_t source) +{ + struct pulse_data *data = bmalloc(sizeof(struct pulse_data)); + memset(data, 0, sizeof(struct pulse_data)); + + data->source = source; + data->frames = 480; + data->samples_per_sec = 48000; + data->speakers = SPEAKERS_STEREO; + data->channels = (data->speakers == SPEAKERS_STEREO) ? 2 : 1; + + if (event_init(&data->event, EVENT_TYPE_MANUAL) != 0) + goto fail; + if (pthread_create(&data->thread, NULL, pulse_thread, data) != 0) + goto fail; + + return data; + +fail: + pulse_destroy(data); + return NULL; +} + +struct obs_source_info pulse_input = { + .id = "pulse_input", + .type = OBS_SOURCE_TYPE_INPUT, + .output_flags = OBS_SOURCE_AUDIO, + .getname = pulse_getname, + .create = pulse_create, + .destroy = pulse_destroy +}; diff --git a/test/linux/xcursor.c b/test/linux/xcursor.c index 4e04588cf..cde3ad83c 100644 --- a/test/linux/xcursor.c +++ b/test/linux/xcursor.c @@ -1,3 +1,19 @@ +/* +Copyright (C) 2014 by Leonhard Oelke + +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, see . +*/ #include #include #include diff --git a/test/linux/xcursor.h b/test/linux/xcursor.h index b7850b551..72436f2fb 100644 --- a/test/linux/xcursor.h +++ b/test/linux/xcursor.h @@ -1,3 +1,19 @@ +/* +Copyright (C) 2014 by Leonhard Oelke + +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, see . +*/ #pragma once #include diff --git a/test/linux/xshm-input.c b/test/linux/xshm-input.c index f8dca2869..aae7d3f32 100644 --- a/test/linux/xshm-input.c +++ b/test/linux/xshm-input.c @@ -1,9 +1,27 @@ +/* +Copyright (C) 2014 by Leonhard Oelke + +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, see . +*/ #include #include + #include #include #include #include + #include #include "xcursor.h" From e67d7607e44cb6206c94a407740b41fd3e50a047 Mon Sep 17 00:00:00 2001 From: Ziemas Date: Sat, 15 Feb 2014 00:12:06 +0100 Subject: [PATCH 05/15] In the API update the diffuse effect got renamed to image. --- test/linux/xcursor.c | 6 +++--- test/linux/xshm-input.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/linux/xcursor.c b/test/linux/xcursor.c index cde3ad83c..4ac187fbf 100644 --- a/test/linux/xcursor.c +++ b/test/linux/xcursor.c @@ -96,9 +96,9 @@ void xcursor_tick(xcursor_t *data) { void xcursor_render(xcursor_t *data) { // TODO: why do i need effects ? effect_t effect = gs_geteffect(); - eparam_t diffuse = effect_getparambyname(effect, "diffuse"); + eparam_t image = effect_getparambyname(effect, "image"); - effect_settexture(effect, diffuse, data->tex); + effect_settexture(effect, image, data->tex); gs_matrix_push(); @@ -116,4 +116,4 @@ void xcursor_render(xcursor_t *data) { gs_enable_blending(False); gs_matrix_pop(); -} \ No newline at end of file +} diff --git a/test/linux/xshm-input.c b/test/linux/xshm-input.c index aae7d3f32..38b991028 100644 --- a/test/linux/xshm-input.c +++ b/test/linux/xshm-input.c @@ -174,8 +174,8 @@ static void xshm_input_video_render(void *vptr, effect_t effect) { XSHM_DATA(vptr); - eparam_t diffuse = effect_getparambyname(effect, "diffuse"); - effect_settexture(effect, diffuse, data->texture); + eparam_t image = effect_getparambyname(effect, "image"); + effect_settexture(effect, image, data->texture); gs_draw_sprite(data->texture, 0, 0, 0); From c558a7b6fa3a38dd6c72548df7225fee9e24ce05 Mon Sep 17 00:00:00 2001 From: fryshorts Date: Sat, 15 Feb 2014 11:21:48 +0100 Subject: [PATCH 06/15] Removed unnecessary buffer Instead of copying the data from pulse to a local buffer and then push it to obs, the pointer from pulse is now used and pa_stream_drop() called afterwards. --- test/linux/pulse-input.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/test/linux/pulse-input.c b/test/linux/pulse-input.c index d03afccee..c4465355c 100644 --- a/test/linux/pulse-input.c +++ b/test/linux/pulse-input.c @@ -179,16 +179,15 @@ static void *pulse_thread(void *vptr) return NULL; uint64_t skip = 1; - uint8_t *out_buffer = bmalloc(data->frames * data->channels); while (event_try(&data->event) == EAGAIN) { uint64_t cur_time = os_gettime_ns(); pulse_iterate(data); - const void *in_buffer; + const void *frames; size_t bytes; - pa_stream_peek(data->stream, &in_buffer, &bytes); + pa_stream_peek(data->stream, &frames, &bytes); // check if we got data if (!bytes) { @@ -213,17 +212,9 @@ static void *pulse_thread(void *vptr) pa_stream_get_latency(data->stream, &l_abs, &l_sign); int64_t latency = (l_sign) ? -(int64_t) l_abs : (int64_t) l_abs; - // copy data to local buffer - memcpy(out_buffer, in_buffer, bytes); - - pa_stream_drop(data->stream); - - // TODO: deinterleave data ? - // push structure struct source_audio out; - out.data[0] = out_buffer; - out.data[1] = out_buffer + (bytes / data->channels); + out.data[0] = (uint8_t *) frames; out.frames = bytes / data->channels; out.speakers = data->speakers; out.samples_per_sec = data->samples_per_sec; @@ -232,9 +223,10 @@ static void *pulse_thread(void *vptr) // send data to obs obs_source_output_audio(data->source, &out); + + // clear pulse audio buffer + pa_stream_drop(data->stream); } - - bfree(out_buffer); pulse_diconnect_stream(data); pulse_disconnect(data); From 09a75c53940cf0d626b9ba9e9d2b71491e9f696f Mon Sep 17 00:00:00 2001 From: fryshorts Date: Sat, 15 Feb 2014 19:34:37 +0100 Subject: [PATCH 07/15] fixed warnings --- test/linux/linux.c | 1 + test/linux/pulse-input.c | 3 +++ test/linux/xshm-input.c | 5 +++++ 3 files changed, 9 insertions(+) diff --git a/test/linux/linux.c b/test/linux/linux.c index b2fb82d25..41c7258d4 100644 --- a/test/linux/linux.c +++ b/test/linux/linux.c @@ -23,6 +23,7 @@ extern struct obs_source_info pulse_input; bool obs_module_load(uint32_t obs_version) { + UNUSED_PARAMETER(obs_version); obs_register_source(&xshm_input); obs_register_source(&pulse_input); return true; diff --git a/test/linux/pulse-input.c b/test/linux/pulse-input.c index c4465355c..537396617 100644 --- a/test/linux/pulse-input.c +++ b/test/linux/pulse-input.c @@ -236,6 +236,7 @@ static void *pulse_thread(void *vptr) static const char *pulse_getname(const char *locale) { + UNUSED_PARAMETER(locale); return "Pulse Audio Input"; } @@ -259,6 +260,8 @@ static void pulse_destroy(void *vptr) static void *pulse_create(obs_data_t settings, obs_source_t source) { + UNUSED_PARAMETER(settings); + struct pulse_data *data = bmalloc(sizeof(struct pulse_data)); memset(data, 0, sizeof(struct pulse_data)); diff --git a/test/linux/xshm-input.c b/test/linux/xshm-input.c index 38b991028..8e997a354 100644 --- a/test/linux/xshm-input.c +++ b/test/linux/xshm-input.c @@ -40,6 +40,7 @@ struct xshm_data { static const char* xshm_input_getname(const char* locale) { + UNUSED_PARAMETER(locale); return "X11 Shared Memory Screen Input"; } @@ -83,6 +84,9 @@ static void xshm_input_destroy(void *vptr) static void *xshm_input_create(obs_data_t settings, obs_source_t source) { + UNUSED_PARAMETER(settings); + UNUSED_PARAMETER(source); + // create data structure struct xshm_data *data = bmalloc(sizeof(struct xshm_data)); memset(data, 0, sizeof(struct xshm_data)); @@ -155,6 +159,7 @@ fail: static void xshm_input_video_tick(void *vptr, float seconds) { + UNUSED_PARAMETER(seconds); XSHM_DATA(vptr); gs_entercontext(obs_graphics()); From 9095ad0a001ee2303a1fa71d8e19febc8956a193 Mon Sep 17 00:00:00 2001 From: fryshorts Date: Sun, 16 Feb 2014 13:45:46 +0100 Subject: [PATCH 08/15] reformatted source to kernel style --- test/linux/pulse-input.c | 455 +++++++++++++++++++++------------------ 1 file changed, 243 insertions(+), 212 deletions(-) diff --git a/test/linux/pulse-input.c b/test/linux/pulse-input.c index 537396617..c5831935c 100644 --- a/test/linux/pulse-input.c +++ b/test/linux/pulse-input.c @@ -14,6 +14,7 @@ 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, see . */ + #include #include #include @@ -38,256 +39,286 @@ along with this program. If not, see . const uint64_t pulse_start_delay = 100000000; struct pulse_data { - pthread_t thread; - event_t event; - obs_source_t source; - - uint32_t frames; - uint32_t samples_per_sec; - uint32_t channels; - enum speaker_layout speakers; - - pa_mainloop *mainloop; - pa_context *context; - pa_stream *stream; + pthread_t thread; + event_t event; + obs_source_t source; + + uint32_t frames; + uint32_t samples_per_sec; + uint32_t channels; + enum speaker_layout speakers; + + pa_mainloop *mainloop; + pa_context *context; + pa_stream *stream; }; static void pulse_iterate(struct pulse_data *data) { - if (pa_mainloop_prepare(data->mainloop, 1000) < 0) { - blog(LOG_ERROR, "Unable to prepare main loop"); - return; - } - if (pa_mainloop_poll(data->mainloop) < 0) { - blog(LOG_ERROR, "Unable to poll main loop"); - return; - } - if (pa_mainloop_dispatch(data->mainloop) < 0) - blog(LOG_ERROR, "Unable to dispatch main loop"); + if (pa_mainloop_prepare(data->mainloop, 1000) < 0) { + blog(LOG_ERROR, "Unable to prepare main loop"); + return; + } + if (pa_mainloop_poll(data->mainloop) < 0) { + blog(LOG_ERROR, "Unable to poll main loop"); + return; + } + if (pa_mainloop_dispatch(data->mainloop) < 0) + blog(LOG_ERROR, "Unable to dispatch main loop"); } +/* + * Create a new pulse audio main loop and connect to the server + * + * Returns a negative value on error + */ static int pulse_connect(struct pulse_data *data) { - data->mainloop = pa_mainloop_new(); - if (!data->mainloop) { - blog(LOG_ERROR, "Unable to create main loop"); - return 0; - } - - data->context = pa_context_new( - pa_mainloop_get_api(data->mainloop), "OBS Studio"); - if (!data->context) { - blog(LOG_ERROR, "Unable to create context"); - return 0; - } - - int status = pa_context_connect( - data->context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL); - if (status < 0) { - blog(LOG_ERROR, "Unable to connect! Status: %d", status); - return 0; - } - - // wait until connected - for (;;) { - pulse_iterate(data); - pa_context_state_t state = pa_context_get_state(data->context); - if (state == PA_CONTEXT_READY) { - blog(LOG_DEBUG, "Context ready"); - break; - } - if (!PA_CONTEXT_IS_GOOD(state)) { - blog(LOG_ERROR, "Connection attempt failed"); - return 0; - } - } - - return 1; + data->mainloop = pa_mainloop_new(); + if (!data->mainloop) { + blog(LOG_ERROR, "Unable to create main loop"); + return -1; + } + + data->context = pa_context_new( + pa_mainloop_get_api(data->mainloop), "OBS Studio"); + if (!data->context) { + blog(LOG_ERROR, "Unable to create context"); + return -1; + } + + int status = pa_context_connect( + data->context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL); + if (status < 0) { + blog(LOG_ERROR, "Unable to connect! Status: %d", status); + return -1; + } + + // wait until connected + for (;;) { + pulse_iterate(data); + pa_context_state_t state = pa_context_get_state(data->context); + if (state == PA_CONTEXT_READY) { + blog(LOG_DEBUG, "Context ready"); + break; + } + if (!PA_CONTEXT_IS_GOOD(state)) { + blog(LOG_ERROR, "Connection attempt failed"); + return -1; + } + } + + return 0; } +/* + * Disconnect from the pulse audio server and destroy the main loop + */ static void pulse_disconnect(struct pulse_data *data) { - if (data->context) { - pa_context_disconnect(data->context); - pa_context_unref(data->context); - } - - if (data->mainloop) - pa_mainloop_free(data->mainloop); + if (data->context) { + pa_context_disconnect(data->context); + pa_context_unref(data->context); + } + + if (data->mainloop) + pa_mainloop_free(data->mainloop); } +/* + * Create a new pulse audio stream and connect to it + * + * Return a negative value on error + */ static int pulse_connect_stream(struct pulse_data *data) { - pa_sample_spec spec; - spec.format = PA_SAMPLE_U8; - spec.rate = data->samples_per_sec; - spec.channels = data->channels; - - pa_buffer_attr attr; - attr.fragsize = data->frames * spec.channels; - attr.maxlength = (uint32_t) -1; - attr.minreq = (uint32_t) -1; - attr.prebuf = (uint32_t) -1; - attr.tlength = (uint32_t) -1; - - data->stream = pa_stream_new( - data->context, "OBS Audio Input", &spec, NULL); - if (!data->stream) { - blog(LOG_ERROR, "Unable to create stream"); - return 0; - } - pa_stream_flags_t flags = - PA_STREAM_INTERPOLATE_TIMING - | PA_STREAM_AUTO_TIMING_UPDATE - | PA_STREAM_ADJUST_LATENCY; - if (pa_stream_connect_record(data->stream, NULL, &attr, flags) < 0) { - blog(LOG_ERROR, "Unable to connect to stream"); - return 0; - } - - for (;;) { - pulse_iterate(data); - pa_stream_state_t state = pa_stream_get_state(data->stream); - if (state == PA_STREAM_READY) { - blog(LOG_DEBUG, "Stream ready"); - break; - } - if (!PA_STREAM_IS_GOOD(state)) { - blog(LOG_ERROR, "Stream connection failed"); - return 0; - } - } - - return 1; + pa_sample_spec spec; + spec.format = PA_SAMPLE_U8; + spec.rate = data->samples_per_sec; + spec.channels = data->channels; + + pa_buffer_attr attr; + attr.fragsize = data->frames * spec.channels; + attr.maxlength = (uint32_t) -1; + attr.minreq = (uint32_t) -1; + attr.prebuf = (uint32_t) -1; + attr.tlength = (uint32_t) -1; + + data->stream = pa_stream_new( + data->context, "OBS Audio Input", &spec, NULL); + if (!data->stream) { + blog(LOG_ERROR, "Unable to create stream"); + return -1; + } + pa_stream_flags_t flags = + PA_STREAM_INTERPOLATE_TIMING + | PA_STREAM_AUTO_TIMING_UPDATE + | PA_STREAM_ADJUST_LATENCY; + if (pa_stream_connect_record(data->stream, NULL, &attr, flags) < 0) { + blog(LOG_ERROR, "Unable to connect to stream"); + return -1; + } + + for (;;) { + pulse_iterate(data); + pa_stream_state_t state = pa_stream_get_state(data->stream); + if (state == PA_STREAM_READY) { + blog(LOG_DEBUG, "Stream ready"); + break; + } + if (!PA_STREAM_IS_GOOD(state)) { + blog(LOG_ERROR, "Stream connection failed"); + return -1; + } + } + + return 0; } +/* + * Disconnect from the pulse audio stream + */ static void pulse_diconnect_stream(struct pulse_data *data) { - if (data->stream) { - pa_stream_disconnect(data->stream); - pa_stream_unref(data->stream); - } + if (data->stream) { + pa_stream_disconnect(data->stream); + pa_stream_unref(data->stream); + } } +/* + * Worker thread to get audio data + * + * Will run until signaled + */ static void *pulse_thread(void *vptr) { - PULSE_DATA(vptr); - - if (!pulse_connect(data)) - return NULL; - if (!pulse_connect_stream(data)) - return NULL; - - uint64_t skip = 1; - - while (event_try(&data->event) == EAGAIN) { - uint64_t cur_time = os_gettime_ns(); - - pulse_iterate(data); - - const void *frames; - size_t bytes; - pa_stream_peek(data->stream, &frames, &bytes); - - // check if we got data - if (!bytes) { - pa_stream_drop(data->stream); - continue; - } - - // skip the first frames received - if (skip) { - // start delay when we receive the first bytes - if (skip == 1 && bytes) - skip = os_gettime_ns(); - if (skip + pulse_start_delay < os_gettime_ns()) - skip = 0; - pa_stream_drop(data->stream); - continue; - } - - // get stream latency - pa_usec_t l_abs; - int l_sign; - pa_stream_get_latency(data->stream, &l_abs, &l_sign); - int64_t latency = (l_sign) ? -(int64_t) l_abs : (int64_t) l_abs; - - // push structure - struct source_audio out; - out.data[0] = (uint8_t *) frames; - out.frames = bytes / data->channels; - out.speakers = data->speakers; - out.samples_per_sec = data->samples_per_sec; - out.timestamp = cur_time - (latency * 1000); - out.format = AUDIO_FORMAT_U8BIT; - - // send data to obs - obs_source_output_audio(data->source, &out); - - // clear pulse audio buffer - pa_stream_drop(data->stream); - } - - pulse_diconnect_stream(data); - pulse_disconnect(data); - - return NULL; + PULSE_DATA(vptr); + + if (pulse_connect(data) < 0) + return NULL; + if (pulse_connect_stream(data) < 0) + return NULL; + + uint64_t skip = 1; + + while (event_try(&data->event) == EAGAIN) { + uint64_t cur_time = os_gettime_ns(); + + pulse_iterate(data); + + const void *frames; + size_t bytes; + pa_stream_peek(data->stream, &frames, &bytes); + + // check if we got data + if (!bytes) { + pa_stream_drop(data->stream); + continue; + } + + // skip the first frames received + if (skip) { + // start delay when we receive the first bytes + if (skip == 1 && bytes) + skip = os_gettime_ns(); + if (skip + pulse_start_delay < os_gettime_ns()) + skip = 0; + pa_stream_drop(data->stream); + continue; + } + + // get stream latency + pa_usec_t l_abs; + int l_sign; + pa_stream_get_latency(data->stream, &l_abs, &l_sign); + int64_t latency = (l_sign) ? -(int64_t) l_abs : (int64_t) l_abs; + + // push structure + struct source_audio out; + out.data[0] = (uint8_t *) frames; + out.frames = bytes / data->channels; + out.speakers = data->speakers; + out.samples_per_sec = data->samples_per_sec; + out.timestamp = cur_time - (latency * 1000); + out.format = AUDIO_FORMAT_U8BIT; + + // send data to obs + obs_source_output_audio(data->source, &out); + + // clear pulse audio buffer + pa_stream_drop(data->stream); + } + + pulse_diconnect_stream(data); + pulse_disconnect(data); + + return NULL; } +/* + * Returns the name of the plugin + */ static const char *pulse_getname(const char *locale) { - UNUSED_PARAMETER(locale); - return "Pulse Audio Input"; + UNUSED_PARAMETER(locale); + return "Pulse Audio Input"; } +/* + * Destroy the plugin object and free all memory + */ static void pulse_destroy(void *vptr) { - PULSE_DATA(vptr); - - if (!data) - return; - - if (data->thread) { - void *ret; - event_signal(&data->event); - pthread_join(data->thread, &ret); - } - - event_destroy(&data->event); - - bfree(data); + PULSE_DATA(vptr); + + if (!data) + return; + + if (data->thread) { + void *ret; + event_signal(&data->event); + pthread_join(data->thread, &ret); + } + + event_destroy(&data->event); + + bfree(data); } +/* + * Create the plugin object + */ static void *pulse_create(obs_data_t settings, obs_source_t source) { - UNUSED_PARAMETER(settings); - - struct pulse_data *data = bmalloc(sizeof(struct pulse_data)); - memset(data, 0, sizeof(struct pulse_data)); - - data->source = source; - data->frames = 480; - data->samples_per_sec = 48000; - data->speakers = SPEAKERS_STEREO; - data->channels = (data->speakers == SPEAKERS_STEREO) ? 2 : 1; - - if (event_init(&data->event, EVENT_TYPE_MANUAL) != 0) - goto fail; - if (pthread_create(&data->thread, NULL, pulse_thread, data) != 0) - goto fail; - - return data; - + UNUSED_PARAMETER(settings); + + struct pulse_data *data = bmalloc(sizeof(struct pulse_data)); + memset(data, 0, sizeof(struct pulse_data)); + + data->source = source; + data->frames = 480; + data->samples_per_sec = 48000; + data->speakers = SPEAKERS_STEREO; + data->channels = (data->speakers == SPEAKERS_STEREO) ? 2 : 1; + + if (event_init(&data->event, EVENT_TYPE_MANUAL) != 0) + goto fail; + if (pthread_create(&data->thread, NULL, pulse_thread, data) != 0) + goto fail; + + return data; + fail: - pulse_destroy(data); - return NULL; + pulse_destroy(data); + return NULL; } struct obs_source_info pulse_input = { - .id = "pulse_input", - .type = OBS_SOURCE_TYPE_INPUT, - .output_flags = OBS_SOURCE_AUDIO, - .getname = pulse_getname, - .create = pulse_create, - .destroy = pulse_destroy + .id = "pulse_input", + .type = OBS_SOURCE_TYPE_INPUT, + .output_flags = OBS_SOURCE_AUDIO, + .getname = pulse_getname, + .create = pulse_create, + .destroy = pulse_destroy }; From 47a6f54910ffbb914a2aadf9daa9f8df3d5dcfb0 Mon Sep 17 00:00:00 2001 From: fryshorts Date: Sun, 16 Feb 2014 13:49:28 +0100 Subject: [PATCH 09/15] use pa_mainloop_iterate instead of own implementation pulse audio provides it's own convenience function to iterate the mainloop, no need to use a custom one. pulseaudio provides timing information for the stream, we use that instead of system time. --- test/linux/pulse-input.c | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/test/linux/pulse-input.c b/test/linux/pulse-input.c index c5831935c..5284cf765 100644 --- a/test/linux/pulse-input.c +++ b/test/linux/pulse-input.c @@ -17,7 +17,6 @@ along with this program. If not, see . #include #include -#include #include #include @@ -30,13 +29,13 @@ along with this program. If not, see . #define PULSE_DATA(voidptr) struct pulse_data *data = voidptr; /* - * delay in nanos before starting to record, this eliminates problems with + * delay in usecs before starting to record, this eliminates problems with * pulse audio sending weird data/timestamps when the stream is connected * * for more information see: * github.com/MaartenBaert/ssr/blob/master/src/AV/Input/PulseAudioInput.cpp */ -const uint64_t pulse_start_delay = 100000000; +const uint64_t pulse_start_delay = 100000; struct pulse_data { pthread_t thread; @@ -53,20 +52,6 @@ struct pulse_data { pa_stream *stream; }; -static void pulse_iterate(struct pulse_data *data) -{ - if (pa_mainloop_prepare(data->mainloop, 1000) < 0) { - blog(LOG_ERROR, "Unable to prepare main loop"); - return; - } - if (pa_mainloop_poll(data->mainloop) < 0) { - blog(LOG_ERROR, "Unable to poll main loop"); - return; - } - if (pa_mainloop_dispatch(data->mainloop) < 0) - blog(LOG_ERROR, "Unable to dispatch main loop"); -} - /* * Create a new pulse audio main loop and connect to the server * @@ -96,7 +81,7 @@ static int pulse_connect(struct pulse_data *data) // wait until connected for (;;) { - pulse_iterate(data); + pa_mainloop_iterate(data->mainloop, 0, NULL); pa_context_state_t state = pa_context_get_state(data->context); if (state == PA_CONTEXT_READY) { blog(LOG_DEBUG, "Context ready"); @@ -160,7 +145,7 @@ static int pulse_connect_stream(struct pulse_data *data) } for (;;) { - pulse_iterate(data); + pa_mainloop_iterate(data->mainloop, 0, NULL); pa_stream_state_t state = pa_stream_get_state(data->stream); if (state == PA_STREAM_READY) { blog(LOG_DEBUG, "Stream ready"); @@ -203,9 +188,7 @@ static void *pulse_thread(void *vptr) uint64_t skip = 1; while (event_try(&data->event) == EAGAIN) { - uint64_t cur_time = os_gettime_ns(); - - pulse_iterate(data); + pa_mainloop_iterate(data->mainloop, 0, NULL); const void *frames; size_t bytes; @@ -217,12 +200,16 @@ static void *pulse_thread(void *vptr) continue; } + uint64_t pa_time; + if (pa_stream_get_time(data->stream, &pa_time) < 0) + continue; + // skip the first frames received if (skip) { // start delay when we receive the first bytes if (skip == 1 && bytes) - skip = os_gettime_ns(); - if (skip + pulse_start_delay < os_gettime_ns()) + skip = pa_time; + if (skip + pulse_start_delay < pa_time) skip = 0; pa_stream_drop(data->stream); continue; @@ -240,7 +227,7 @@ static void *pulse_thread(void *vptr) out.frames = bytes / data->channels; out.speakers = data->speakers; out.samples_per_sec = data->samples_per_sec; - out.timestamp = cur_time - (latency * 1000); + out.timestamp = (pa_time - latency) * 1000; out.format = AUDIO_FORMAT_U8BIT; // send data to obs From f13ae77e00bf207d054cca50ae5b5b48dec0f32c Mon Sep 17 00:00:00 2001 From: fryshorts Date: Tue, 18 Feb 2014 21:29:15 +0100 Subject: [PATCH 10/15] disable blending for desktop capture --- test/linux/xshm-input.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/linux/xshm-input.c b/test/linux/xshm-input.c index 8e997a354..fb4d59f35 100644 --- a/test/linux/xshm-input.c +++ b/test/linux/xshm-input.c @@ -182,6 +182,8 @@ static void xshm_input_video_render(void *vptr, effect_t effect) eparam_t image = effect_getparambyname(effect, "image"); effect_settexture(effect, image, data->texture); + gs_enable_blending(False); + gs_draw_sprite(data->texture, 0, 0, 0); // render the cursor From 63d441e1827890c25d4ce4464f18ea79159b9206 Mon Sep 17 00:00:00 2001 From: fryshorts Date: Tue, 18 Feb 2014 21:46:39 +0100 Subject: [PATCH 11/15] added functions to translate obs settings to pulse --- test/linux/pulse-input.c | 238 ++++++++++++++++++++++++++++++--------- 1 file changed, 184 insertions(+), 54 deletions(-) diff --git a/test/linux/pulse-input.c b/test/linux/pulse-input.c index 5284cf765..324cedc44 100644 --- a/test/linux/pulse-input.c +++ b/test/linux/pulse-input.c @@ -17,6 +17,7 @@ along with this program. If not, see . #include #include +#include #include #include @@ -42,16 +43,100 @@ struct pulse_data { event_t event; obs_source_t source; - uint32_t frames; uint32_t samples_per_sec; - uint32_t channels; enum speaker_layout speakers; + pa_sample_format_t format; pa_mainloop *mainloop; pa_context *context; pa_stream *stream; + pa_proplist *props; }; +/* + * get obs from pulse audio format + */ +static enum audio_format pulse_to_obs_audio_format( + pa_sample_format_t format) +{ + switch (format) { + case PA_SAMPLE_U8: + return AUDIO_FORMAT_U8BIT; + case PA_SAMPLE_S16LE: + return AUDIO_FORMAT_16BIT; + case PA_SAMPLE_S24_32LE: + return AUDIO_FORMAT_32BIT; + case PA_SAMPLE_FLOAT32LE: + return AUDIO_FORMAT_FLOAT; + default: + return AUDIO_FORMAT_UNKNOWN; + } + + return AUDIO_FORMAT_UNKNOWN; +} + +/* + * get the number of frames from bytes and current format + */ +static uint32_t get_frames_from_bytes(struct pulse_data *data, size_t bytes) +{ + uint32_t ret = bytes; + ret /= get_audio_bytes_per_channel( + pulse_to_obs_audio_format(data->format)); + ret /= get_audio_channels(data->speakers); + + return ret; +} + +/* + * get the buffer size needed for length msec with current settings + */ +static uint32_t get_buffer_size(struct pulse_data *data, uint32_t length) +{ + uint32_t ret = length; + ret *= data->samples_per_sec; + ret *= get_audio_bytes_per_channel( + pulse_to_obs_audio_format(data->format)); + ret *= get_audio_channels(data->speakers); + ret /= 1000; + + return ret; +} + +/* + * Get latency for a pulse audio stream + */ +static int pulse_get_stream_latency(pa_stream *stream, int64_t *latency) +{ + int ret; + int sign; + pa_usec_t abs; + + ret = pa_stream_get_latency(stream, &abs, &sign); + *latency = (sign) ? -(int64_t) abs : (int64_t) abs; + return ret; +} + +/* + * Iterate the mainloop + * + * The custom implementation gives better performance than the function + * provided by pulse audio, maybe due to the timeout set in prepare ? + */ +static void pulse_iterate(struct pulse_data *data) +{ + if (pa_mainloop_prepare(data->mainloop, 1000) < 0) { + blog(LOG_ERROR, "Unable to prepare main loop"); + return; + } + if (pa_mainloop_poll(data->mainloop) < 0) { + blog(LOG_ERROR, "Unable to poll main loop"); + return; + } + if (pa_mainloop_dispatch(data->mainloop) < 0) + blog(LOG_ERROR, "Unable to dispatch main loop"); +} + /* * Create a new pulse audio main loop and connect to the server * @@ -61,34 +146,35 @@ static int pulse_connect(struct pulse_data *data) { data->mainloop = pa_mainloop_new(); if (!data->mainloop) { - blog(LOG_ERROR, "Unable to create main loop"); + blog(LOG_ERROR, "pulse-input: Unable to create main loop"); return -1; } - data->context = pa_context_new( - pa_mainloop_get_api(data->mainloop), "OBS Studio"); + data->context = pa_context_new_with_proplist( + pa_mainloop_get_api(data->mainloop), "OBS Studio", data->props); if (!data->context) { - blog(LOG_ERROR, "Unable to create context"); + blog(LOG_ERROR, "pulse-input: Unable to create context"); return -1; } int status = pa_context_connect( data->context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL); if (status < 0) { - blog(LOG_ERROR, "Unable to connect! Status: %d", status); + blog(LOG_ERROR, "pulse-input: Unable to connect! Status: %d", + status); return -1; } // wait until connected for (;;) { - pa_mainloop_iterate(data->mainloop, 0, NULL); + pulse_iterate(data); pa_context_state_t state = pa_context_get_state(data->context); if (state == PA_CONTEXT_READY) { - blog(LOG_DEBUG, "Context ready"); + blog(LOG_DEBUG, "pulse-input: Context ready"); break; } if (!PA_CONTEXT_IS_GOOD(state)) { - blog(LOG_ERROR, "Connection attempt failed"); + blog(LOG_ERROR, "pulse-input: Context connect failed"); return -1; } } @@ -118,21 +204,26 @@ static void pulse_disconnect(struct pulse_data *data) static int pulse_connect_stream(struct pulse_data *data) { pa_sample_spec spec; - spec.format = PA_SAMPLE_U8; + spec.format = data->format; spec.rate = data->samples_per_sec; - spec.channels = data->channels; + spec.channels = get_audio_channels(data->speakers); + + if (!pa_sample_spec_valid(&spec)) { + blog(LOG_ERROR, "pulse-input: Sample spec is not valid"); + return -1; + } pa_buffer_attr attr; - attr.fragsize = data->frames * spec.channels; + attr.fragsize = get_buffer_size(data, 250); attr.maxlength = (uint32_t) -1; attr.minreq = (uint32_t) -1; attr.prebuf = (uint32_t) -1; attr.tlength = (uint32_t) -1; - data->stream = pa_stream_new( - data->context, "OBS Audio Input", &spec, NULL); + data->stream = pa_stream_new_with_proplist(data->context, + obs_source_getname(data->source), &spec, NULL, data->props); if (!data->stream) { - blog(LOG_ERROR, "Unable to create stream"); + blog(LOG_ERROR, "pulse-input: Unable to create stream"); return -1; } pa_stream_flags_t flags = @@ -140,19 +231,19 @@ static int pulse_connect_stream(struct pulse_data *data) | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_ADJUST_LATENCY; if (pa_stream_connect_record(data->stream, NULL, &attr, flags) < 0) { - blog(LOG_ERROR, "Unable to connect to stream"); + blog(LOG_ERROR, "pulse-input: Unable to connect to stream"); return -1; } for (;;) { - pa_mainloop_iterate(data->mainloop, 0, NULL); + pulse_iterate(data); pa_stream_state_t state = pa_stream_get_state(data->stream); if (state == PA_STREAM_READY) { - blog(LOG_DEBUG, "Stream ready"); + blog(LOG_DEBUG, "pulse-input: Stream ready"); break; } if (!PA_STREAM_IS_GOOD(state)) { - blog(LOG_ERROR, "Stream connection failed"); + blog(LOG_ERROR, "pulse-input: Stream connect failed"); return -1; } } @@ -171,6 +262,38 @@ static void pulse_diconnect_stream(struct pulse_data *data) } } +/* + * Loop to skip the first few samples of a stream + */ +static int pulse_skip(struct pulse_data *data) +{ + uint64_t skip = 1; + const void *frames; + size_t bytes; + uint64_t pa_time; + + while (event_try(&data->event) == EAGAIN) { + pulse_iterate(data); + pa_stream_peek(data->stream, &frames, &bytes); + + if (!bytes) + continue; + if (!frames || pa_stream_get_time(data->stream, &pa_time) < 0) { + pa_stream_drop(data->stream); + continue; + } + + if (skip == 1 && pa_time) + skip = pa_time; + if (skip + pulse_start_delay < pa_time) + return 0; + + pa_stream_drop(data->stream); + } + + return -1; +} + /* * Worker thread to get audio data * @@ -185,55 +308,51 @@ static void *pulse_thread(void *vptr) if (pulse_connect_stream(data) < 0) return NULL; - uint64_t skip = 1; + if (pulse_skip(data) < 0) + return NULL; + + blog(LOG_DEBUG, "pulse-input: Start recording"); + + const void *frames; + size_t bytes; + uint64_t pa_time; + int64_t pa_latency; + + struct source_audio out; + out.speakers = data->speakers; + out.samples_per_sec = data->samples_per_sec; + out.format = pulse_to_obs_audio_format(data->format); while (event_try(&data->event) == EAGAIN) { - pa_mainloop_iterate(data->mainloop, 0, NULL); + pulse_iterate(data); - const void *frames; - size_t bytes; pa_stream_peek(data->stream, &frames, &bytes); // check if we got data - if (!bytes) { + if (!bytes) + continue; + if (!frames) { + blog(LOG_DEBUG, + "pulse-input: Got audio hole of %u bytes", + (unsigned int) bytes); pa_stream_drop(data->stream); continue; } - uint64_t pa_time; - if (pa_stream_get_time(data->stream, &pa_time) < 0) - continue; - - // skip the first frames received - if (skip) { - // start delay when we receive the first bytes - if (skip == 1 && bytes) - skip = pa_time; - if (skip + pulse_start_delay < pa_time) - skip = 0; + if (pa_stream_get_time(data->stream, &pa_time) < 0) { + blog(LOG_ERROR, + "pulse-input: Failed to get timing info !"); pa_stream_drop(data->stream); continue; } - // get stream latency - pa_usec_t l_abs; - int l_sign; - pa_stream_get_latency(data->stream, &l_abs, &l_sign); - int64_t latency = (l_sign) ? -(int64_t) l_abs : (int64_t) l_abs; + pulse_get_stream_latency(data->stream, &pa_latency); - // push structure - struct source_audio out; out.data[0] = (uint8_t *) frames; - out.frames = bytes / data->channels; - out.speakers = data->speakers; - out.samples_per_sec = data->samples_per_sec; - out.timestamp = (pa_time - latency) * 1000; - out.format = AUDIO_FORMAT_U8BIT; - - // send data to obs + out.frames = get_frames_from_bytes(data, bytes); + out.timestamp = (pa_time - pa_latency) * 1000; obs_source_output_audio(data->source, &out); - // clear pulse audio buffer pa_stream_drop(data->stream); } @@ -270,6 +389,8 @@ static void pulse_destroy(void *vptr) event_destroy(&data->event); + pa_proplist_free(data->props); + bfree(data); } @@ -284,10 +405,19 @@ static void *pulse_create(obs_data_t settings, obs_source_t source) memset(data, 0, sizeof(struct pulse_data)); data->source = source; - data->frames = 480; - data->samples_per_sec = 48000; + data->samples_per_sec = 44100; data->speakers = SPEAKERS_STEREO; - data->channels = (data->speakers == SPEAKERS_STEREO) ? 2 : 1; + data->format = PA_SAMPLE_S16LE; + + /* TODO: use obs-studio icon */ + data->props = pa_proplist_new(); + pa_proplist_sets(data->props, PA_PROP_APPLICATION_NAME, + "OBS Studio"); + pa_proplist_sets(data->props, PA_PROP_APPLICATION_ICON_NAME, + "application-exit"); + pa_proplist_sets(data->props, PA_PROP_MEDIA_ROLE, + "production"); + if (event_init(&data->event, EVENT_TYPE_MANUAL) != 0) goto fail; From ce9db656951f3b5e4cfb3995fbe754b8bf52f882 Mon Sep 17 00:00:00 2001 From: fryshorts Date: Thu, 20 Feb 2014 19:41:04 +0100 Subject: [PATCH 12/15] changed to kernel formatting and added some more documentation --- test/linux/linux.c | 8 +- test/linux/xcursor.c | 163 ++++++++++----------- test/linux/xcursor.h | 17 +-- test/linux/xshm-input.c | 306 +++++++++++++++++++--------------------- 4 files changed, 238 insertions(+), 256 deletions(-) diff --git a/test/linux/linux.c b/test/linux/linux.c index 41c7258d4..294e21d47 100644 --- a/test/linux/linux.c +++ b/test/linux/linux.c @@ -23,8 +23,8 @@ extern struct obs_source_info pulse_input; bool obs_module_load(uint32_t obs_version) { - UNUSED_PARAMETER(obs_version); - obs_register_source(&xshm_input); - obs_register_source(&pulse_input); - return true; + UNUSED_PARAMETER(obs_version); + obs_register_source(&xshm_input); + obs_register_source(&pulse_input); + return true; } diff --git a/test/linux/xcursor.c b/test/linux/xcursor.c index 4ac187fbf..cc7c7aa78 100644 --- a/test/linux/xcursor.c +++ b/test/linux/xcursor.c @@ -14,106 +14,99 @@ 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, see . */ -#include -#include + +#include #include + +#include #include "xcursor.h" -uint32_t *xcursor_pixels(XFixesCursorImage *xc) { - int size = xc->width * xc->height; - uint32_t *pixels = bmalloc(size * 4); - - // pixel data from XFixes is defined as unsigned long ... - // TODO: check why everybody is making a fuss about this - for (int i = 0; i < size; ++i) - pixels[i] = (uint32_t) xc->pixels[i]; - - return pixels; +/* + * Get pixel data for the cursor + * + * XFixes has the data defined as unsigned long, so we can not use memcpy. + * Theres a lot of talk about this in other implementation and they tend to + * be really complicated, but this naive approach seems to work fine ... + */ +static uint32_t *xcursor_pixels(XFixesCursorImage *xc) { + uint_fast32_t size = xc->width * xc->height; + uint32_t *pixels = bmalloc(size * sizeof(uint32_t)); + + for (uint_fast32_t i = 0; i < size; ++i) + pixels[i] = (uint32_t) xc->pixels[i]; + + return pixels; } -void xcursor_create(xcursor_t *data, XFixesCursorImage *xc) { - // get cursor pixel data - uint32_t *pixels = xcursor_pixels(xc); - - // if the cursor has the same size as the last one we can simply update - if (data->tex - && data->last_height == xc->width - && data->last_width == xc->height) { - texture_setimage(data->tex, (void **) pixels, xc->width * 4, False); - } - else { - if (data->tex) - texture_destroy(data->tex); - - data->tex = gs_create_texture( - xc->width, xc->height, - GS_RGBA, 1, - (const void **) &pixels, - GS_DYNAMIC - ); - } - bfree(pixels); - - // set some data - data->last_serial = xc->cursor_serial; - data->last_width = xc->width; - data->last_height = xc->height; +/* + * Create the cursor texture, either by updating if the new cursor has the same + * size or by creating a new texture if the size is different + */ +static void xcursor_create(xcursor_t *data, XFixesCursorImage *xc) { + uint32_t *pixels = xcursor_pixels(xc); + + if (data->tex + && data->last_height == xc->width + && data->last_width == xc->height) { + texture_setimage(data->tex, (void **) pixels, + xc->width * sizeof(uint32_t), False); + } else { + if (data->tex) + texture_destroy(data->tex); + + data->tex = gs_create_texture(xc->width, xc->height, + GS_RGBA, 1, (const void **) &pixels, GS_DYNAMIC); + } + + bfree(pixels); + + data->last_serial = xc->cursor_serial; + data->last_width = xc->width; + data->last_height = xc->height; } xcursor_t *xcursor_init(Display *dpy) { - xcursor_t *data = bmalloc(sizeof(xcursor_t)); - memset(data, 0, sizeof(xcursor_t)); - - data->dpy = dpy; - - // initialize texture so we don't crash - xcursor_tick(data); - - return data; + xcursor_t *data = bmalloc(sizeof(xcursor_t)); + memset(data, 0, sizeof(xcursor_t)); + + data->dpy = dpy; + xcursor_tick(data); + + return data; } void xcursor_destroy(xcursor_t *data) { - if (data->tex) - texture_destroy(data->tex); - bfree(data); + if (data->tex) + texture_destroy(data->tex); + bfree(data); } void xcursor_tick(xcursor_t *data) { - // get cursor data - XFixesCursorImage *xc = XFixesGetCursorImage(data->dpy); - - // update cursor if necessary - if (!data->tex || data->last_serial != xc->cursor_serial) - xcursor_create(data, xc); - - // update cursor position - data->pos_x = -1.0 * (xc->x - xc->xhot); - data->pos_y = -1.0 * (xc->y - xc->yhot); - - XFree(xc); + XFixesCursorImage *xc = XFixesGetCursorImage(data->dpy); + + if (!data->tex || data->last_serial != xc->cursor_serial) + xcursor_create(data, xc); + data->pos_x = -1.0 * (xc->x - xc->xhot); + data->pos_y = -1.0 * (xc->y - xc->yhot); + + XFree(xc); } void xcursor_render(xcursor_t *data) { - // TODO: why do i need effects ? - effect_t effect = gs_geteffect(); - eparam_t image = effect_getparambyname(effect, "image"); - - effect_settexture(effect, image, data->tex); - - gs_matrix_push(); - - // move cursor to the right position - gs_matrix_translate3f( - data->pos_x, - data->pos_y, - 0 - ); - - // blend cursor - gs_enable_blending(True); - gs_blendfunction(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA); - gs_draw_sprite(data->tex, 0, 0, 0); - gs_enable_blending(False); - - gs_matrix_pop(); + /* TODO: why do i need effects ? */ + effect_t effect = gs_geteffect(); + eparam_t image = effect_getparambyname(effect, "image"); + + effect_settexture(effect, image, data->tex); + + gs_matrix_push(); + + gs_matrix_translate3f(data->pos_x, data->pos_y, 0); + + gs_enable_blending(True); + gs_blendfunction(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA); + gs_draw_sprite(data->tex, 0, 0, 0); + gs_enable_blending(False); + + gs_matrix_pop(); } diff --git a/test/linux/xcursor.h b/test/linux/xcursor.h index 72436f2fb..69dfe4297 100644 --- a/test/linux/xcursor.h +++ b/test/linux/xcursor.h @@ -14,6 +14,7 @@ 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, see . */ + #pragma once #include @@ -23,13 +24,13 @@ extern "C" { #endif typedef struct { - Display *dpy; - float pos_x; - float pos_y; - unsigned long last_serial; - unsigned short int last_width; - unsigned short int last_height; - texture_t tex; + Display *dpy; + float pos_x; + float pos_y; + unsigned long last_serial; + unsigned short int last_width; + unsigned short int last_height; + texture_t tex; } xcursor_t; /** @@ -60,4 +61,4 @@ void xcursor_render(xcursor_t *data); #ifdef __cplusplus } -#endif \ No newline at end of file +#endif diff --git a/test/linux/xshm-input.c b/test/linux/xshm-input.c index fb4d59f35..51914c406 100644 --- a/test/linux/xshm-input.c +++ b/test/linux/xshm-input.c @@ -28,191 +28,179 @@ along with this program. If not, see . #define XSHM_DATA(voidptr) struct xshm_data *data = voidptr; struct xshm_data { - Display *dpy; - Window root_window; - uint32_t width, height; - int shm_attached; - XShmSegmentInfo shm_info; - XImage *image; - texture_t texture; - xcursor_t *cursor; + Display *dpy; + Window root_window; + uint32_t width, height; + int shm_attached; + XShmSegmentInfo shm_info; + XImage *image; + texture_t texture; + xcursor_t *cursor; }; -static const char* xshm_input_getname(const char* locale) +static const char* xshm_getname(const char* locale) { - UNUSED_PARAMETER(locale); - return "X11 Shared Memory Screen Input"; + UNUSED_PARAMETER(locale); + return "X11 Shared Memory Screen Input"; } -static void xshm_input_destroy(void *vptr) +static void xshm_destroy(void *vptr) { - XSHM_DATA(vptr); - - if (data) { - gs_entercontext(obs_graphics()); - - texture_destroy(data->texture); - xcursor_destroy(data->cursor); - - gs_leavecontext(); - - // detach xshm - if (data->shm_attached) - XShmDetach(data->dpy, &data->shm_info); - - // detach shared memory - if (data->shm_info.shmaddr != (char *) -1) { - shmdt(data->shm_info.shmaddr); - data->shm_info.shmaddr = (char *) -1; - } - - // remove shared memory - if (data->shm_info.shmid != -1) - shmctl(data->shm_info.shmid, IPC_RMID, NULL); - - // destroy image - if (data->image) - XDestroyImage(data->image); - - // close display - if (data->dpy) - XCloseDisplay(data->dpy); - - bfree(data); - } + XSHM_DATA(vptr); + + if (!data) + return; + + gs_entercontext(obs_graphics()); + + texture_destroy(data->texture); + xcursor_destroy(data->cursor); + + gs_leavecontext(); + + if (data->shm_attached) + XShmDetach(data->dpy, &data->shm_info); + + if (data->shm_info.shmaddr != (char *) -1) { + shmdt(data->shm_info.shmaddr); + data->shm_info.shmaddr = (char *) -1; + } + + if (data->shm_info.shmid != -1) + shmctl(data->shm_info.shmid, IPC_RMID, NULL); + + if (data->image) + XDestroyImage(data->image); + + if (data->dpy) + XCloseDisplay(data->dpy); + + bfree(data); } -static void *xshm_input_create(obs_data_t settings, obs_source_t source) +static void *xshm_create(obs_data_t settings, obs_source_t source) { - UNUSED_PARAMETER(settings); - UNUSED_PARAMETER(source); - - // create data structure - struct xshm_data *data = bmalloc(sizeof(struct xshm_data)); - memset(data, 0, sizeof(struct xshm_data)); - - // try to open display and all the good stuff - data->dpy = XOpenDisplay(NULL); - if (!data->dpy) - goto fail; - - Screen *screen = XDefaultScreenOfDisplay(data->dpy); - data->width = WidthOfScreen(screen); - data->height = HeightOfScreen(screen); - data->root_window = XRootWindowOfScreen(screen); - Visual *visual = DefaultVisualOfScreen(screen); - int depth = DefaultDepthOfScreen(screen); - - // query for shm extension - if (!XShmQueryExtension(data->dpy)) - goto fail; - - // create xshm image - data->image = XShmCreateImage(data->dpy, visual, depth, - ZPixmap, NULL, &data->shm_info, - data->width, data->height); - if (!data->image) - goto fail; - - // create shared memory - data->shm_info.shmid = shmget(IPC_PRIVATE, data->image->bytes_per_line * - data->image->height, IPC_CREAT | 0700); - if (data->shm_info.shmid < 0) - goto fail; - - // attach shared memory - data->shm_info.shmaddr = data->image->data - = (char *) shmat(data->shm_info.shmid, 0, 0); - if (data->shm_info.shmaddr == (char *) -1) - goto fail; - // set shared memory as read only - data->shm_info.readOnly = False; - - // attach shm - if (!XShmAttach(data->dpy, &data->shm_info)) - goto fail; - data->shm_attached = 1; - - // get image - if (!XShmGetImage(data->dpy, data->root_window, data->image, - 0, 0, AllPlanes)) - goto fail; - - // create obs texture - gs_entercontext(obs_graphics()); - data->texture = gs_create_texture(data->width, data->height, GS_BGRA, 1, - (const void**) &data->image->data, - GS_DYNAMIC); - data->cursor = xcursor_init(data->dpy); - gs_leavecontext(); - - if (!data->texture) - goto fail; - - return data; - + UNUSED_PARAMETER(settings); + UNUSED_PARAMETER(source); + + + struct xshm_data *data = bmalloc(sizeof(struct xshm_data)); + memset(data, 0, sizeof(struct xshm_data)); + + data->dpy = XOpenDisplay(NULL); + if (!data->dpy) + goto fail; + + Screen *screen = XDefaultScreenOfDisplay(data->dpy); + data->width = WidthOfScreen(screen); + data->height = HeightOfScreen(screen); + data->root_window = XRootWindowOfScreen(screen); + Visual *visual = DefaultVisualOfScreen(screen); + int depth = DefaultDepthOfScreen(screen); + + if (!XShmQueryExtension(data->dpy)) + goto fail; + + data->image = XShmCreateImage(data->dpy, visual, depth, + ZPixmap, NULL, &data->shm_info, data->width, data->height); + if (!data->image) + goto fail; + + data->shm_info.shmid = shmget(IPC_PRIVATE, + data->image->bytes_per_line * data->image->height, + IPC_CREAT | 0700); + if (data->shm_info.shmid < 0) + goto fail; + + data->shm_info.shmaddr + = data->image->data + = (char *) shmat(data->shm_info.shmid, 0, 0); + if (data->shm_info.shmaddr == (char *) -1) + goto fail; + data->shm_info.readOnly = False; + + + if (!XShmAttach(data->dpy, &data->shm_info)) + goto fail; + data->shm_attached = 1; + + if (!XShmGetImage(data->dpy, data->root_window, data->image, + 0, 0, AllPlanes)) { + goto fail; + } + + + gs_entercontext(obs_graphics()); + data->texture = gs_create_texture(data->width, data->height, + GS_BGRA, 1, (const void**) &data->image->data, GS_DYNAMIC); + data->cursor = xcursor_init(data->dpy); + gs_leavecontext(); + + if (!data->texture) + goto fail; + + return data; + fail: - // clean up and return null - xshm_input_destroy(data); - return NULL; + xshm_destroy(data); + return NULL; } -static void xshm_input_video_tick(void *vptr, float seconds) +static void xshm_video_tick(void *vptr, float seconds) { - UNUSED_PARAMETER(seconds); - XSHM_DATA(vptr); - - gs_entercontext(obs_graphics()); - - // update screen texture - XShmGetImage(data->dpy, data->root_window, data->image, 0, 0, AllPlanes); - texture_setimage(data->texture, (void *) data->image->data, - data->width * 4, False); - - // update mouse cursor - xcursor_tick(data->cursor); - - gs_leavecontext(); + UNUSED_PARAMETER(seconds); + XSHM_DATA(vptr); + + gs_entercontext(obs_graphics()); + + + XShmGetImage(data->dpy, data->root_window, data->image, + 0, 0, AllPlanes); + texture_setimage(data->texture, (void *) data->image->data, + data->width * 4, False); + + xcursor_tick(data->cursor); + + gs_leavecontext(); } -static void xshm_input_video_render(void *vptr, effect_t effect) +static void xshm_video_render(void *vptr, effect_t effect) { - XSHM_DATA(vptr); - - eparam_t image = effect_getparambyname(effect, "image"); - effect_settexture(effect, image, data->texture); - - gs_enable_blending(False); - - gs_draw_sprite(data->texture, 0, 0, 0); - - // render the cursor - xcursor_render(data->cursor); + XSHM_DATA(vptr); + + eparam_t image = effect_getparambyname(effect, "image"); + effect_settexture(effect, image, data->texture); + + gs_enable_blending(False); + + gs_draw_sprite(data->texture, 0, 0, 0); + + xcursor_render(data->cursor); } -static uint32_t xshm_input_getwidth(void *vptr) +static uint32_t xshm_getwidth(void *vptr) { - XSHM_DATA(vptr); - - return texture_getwidth(data->texture); + XSHM_DATA(vptr); + + return texture_getwidth(data->texture); } -static uint32_t xshm_input_getheight(void *vptr) +static uint32_t xshm_getheight(void *vptr) { - XSHM_DATA(vptr); - - return texture_getheight(data->texture); + XSHM_DATA(vptr); + + return texture_getheight(data->texture); } struct obs_source_info xshm_input = { .id = "xshm_input", .type = OBS_SOURCE_TYPE_INPUT, .output_flags = OBS_SOURCE_VIDEO, - .getname = xshm_input_getname, - .create = xshm_input_create, - .destroy = xshm_input_destroy, - .video_tick = xshm_input_video_tick, - .video_render = xshm_input_video_render, - .getwidth = xshm_input_getwidth, - .getheight = xshm_input_getheight + .getname = xshm_getname, + .create = xshm_create, + .destroy = xshm_destroy, + .video_tick = xshm_video_tick, + .video_render = xshm_video_render, + .getwidth = xshm_getwidth, + .getheight = xshm_getheight }; From f28912101ef9a6bfcafb0c083b87cc63a6917414 Mon Sep 17 00:00:00 2001 From: fryshorts Date: Sat, 22 Feb 2014 14:48:11 +0100 Subject: [PATCH 13/15] Use default pulse audio frame format for recording We use the default format to avoid any resampling by pulse audio, since obs will probably resample anyways if necessary. --- test/linux/linux.c | 6 +- test/linux/pulse-input.c | 305 +++++++++++++++++++++++++++++++++++---- 2 files changed, 277 insertions(+), 34 deletions(-) diff --git a/test/linux/linux.c b/test/linux/linux.c index 294e21d47..247363a23 100644 --- a/test/linux/linux.c +++ b/test/linux/linux.c @@ -19,12 +19,14 @@ along with this program. If not, see . OBS_DECLARE_MODULE() extern struct obs_source_info xshm_input; -extern struct obs_source_info pulse_input; +extern struct obs_source_info pulse_input_capture; +extern struct obs_source_info pulse_output_capture; bool obs_module_load(uint32_t obs_version) { UNUSED_PARAMETER(obs_version); obs_register_source(&xshm_input); - obs_register_source(&pulse_input); + obs_register_source(&pulse_input_capture); + obs_register_source(&pulse_output_capture); return true; } diff --git a/test/linux/pulse-input.c b/test/linux/pulse-input.c index 324cedc44..d5598ca85 100644 --- a/test/linux/pulse-input.c +++ b/test/linux/pulse-input.c @@ -19,6 +19,7 @@ along with this program. If not, see . #include #include +#include #include #include #include @@ -43,9 +44,12 @@ struct pulse_data { event_t event; obs_source_t source; - uint32_t samples_per_sec; enum speaker_layout speakers; pa_sample_format_t format; + uint_fast32_t samples_per_sec; + uint_fast8_t channels; + + uint_fast32_t bytes_per_frame; pa_mainloop *mainloop; pa_context *context; @@ -53,6 +57,17 @@ struct pulse_data { pa_proplist *props; }; +struct pulse_context_change { + pa_threaded_mainloop *mainloop; + pa_context_state_t state; +}; + +struct pulse_enumerate { + pa_threaded_mainloop *mainloop; + obs_property_t devices; + bool input; +}; + /* * get obs from pulse audio format */ @@ -78,29 +93,18 @@ static enum audio_format pulse_to_obs_audio_format( /* * get the number of frames from bytes and current format */ -static uint32_t get_frames_from_bytes(struct pulse_data *data, size_t bytes) +static uint_fast32_t frames_to_bytes(struct pulse_data *data, size_t bytes) { - uint32_t ret = bytes; - ret /= get_audio_bytes_per_channel( - pulse_to_obs_audio_format(data->format)); - ret /= get_audio_channels(data->speakers); - - return ret; + return (bytes / data->bytes_per_frame); } /* * get the buffer size needed for length msec with current settings */ -static uint32_t get_buffer_size(struct pulse_data *data, uint32_t length) +static uint_fast32_t get_buffer_size(struct pulse_data *data, + uint_fast32_t length) { - uint32_t ret = length; - ret *= data->samples_per_sec; - ret *= get_audio_bytes_per_channel( - pulse_to_obs_audio_format(data->format)); - ret *= get_audio_channels(data->speakers); - ret /= 1000; - - return ret; + return (length * data->samples_per_sec * data->bytes_per_frame) / 1000; } /* @@ -137,6 +141,47 @@ static void pulse_iterate(struct pulse_data *data) blog(LOG_ERROR, "Unable to dispatch main loop"); } +/* + * Server info callback, this is called from pa_mainloop_dispatch + * TODO: how to free the server info struct ? + */ +static void pulse_get_server_info_cb(pa_context *c, const pa_server_info *i, + void *userdata) +{ + UNUSED_PARAMETER(c); + PULSE_DATA(userdata); + + const pa_sample_spec *spec = &i->sample_spec; + data->format = spec->format; + data->samples_per_sec = spec->rate; + + blog(LOG_DEBUG, "pulse-input: Default format: %s, %u Hz, %u channels", + pa_sample_format_to_string(spec->format), + spec->rate, + spec->channels); +} + +/* + * Request pulse audio server info + * TODO: handle failures ? + */ +static int pulse_get_server_info(struct pulse_data *data) +{ + pa_server_info_cb_t cb = pulse_get_server_info_cb; + pa_operation *op = pa_context_get_server_info(data->context, cb, data); + + for(;;) { + pulse_iterate(data); + pa_operation_state_t state = pa_operation_get_state(op); + if (state == PA_OPERATION_DONE) { + pa_operation_unref(op); + break; + } + } + + return 0; +} + /* * Create a new pulse audio main loop and connect to the server * @@ -213,6 +258,10 @@ static int pulse_connect_stream(struct pulse_data *data) return -1; } + data->bytes_per_frame = pa_frame_size(&spec); + blog(LOG_DEBUG, "pulse-input: %u bytes per frame", + (unsigned int) data->bytes_per_frame); + pa_buffer_attr attr; attr.fragsize = get_buffer_size(data, 250); attr.maxlength = (uint32_t) -1; @@ -272,7 +321,7 @@ static int pulse_skip(struct pulse_data *data) size_t bytes; uint64_t pa_time; - while (event_try(&data->event) == EAGAIN) { + while (event_try(data->event) == EAGAIN) { pulse_iterate(data); pa_stream_peek(data->stream, &frames, &bytes); @@ -305,6 +354,8 @@ static void *pulse_thread(void *vptr) if (pulse_connect(data) < 0) return NULL; + if (pulse_get_server_info(data) < 0) + return NULL; if (pulse_connect_stream(data) < 0) return NULL; @@ -323,7 +374,7 @@ static void *pulse_thread(void *vptr) out.samples_per_sec = data->samples_per_sec; out.format = pulse_to_obs_audio_format(data->format); - while (event_try(&data->event) == EAGAIN) { + while (event_try(data->event) == EAGAIN) { pulse_iterate(data); pa_stream_peek(data->stream, &frames, &bytes); @@ -349,7 +400,7 @@ static void *pulse_thread(void *vptr) pulse_get_stream_latency(data->stream, &pa_latency); out.data[0] = (uint8_t *) frames; - out.frames = get_frames_from_bytes(data, bytes); + out.frames = frames_to_bytes(data, bytes); out.timestamp = (pa_time - pa_latency) * 1000; obs_source_output_audio(data->source, &out); @@ -363,12 +414,187 @@ static void *pulse_thread(void *vptr) } /* - * Returns the name of the plugin + * Create a new pulseaudio context */ -static const char *pulse_getname(const char *locale) +static pa_context *pulse_context_create(pa_threaded_mainloop *m) +{ + pa_context *c; + pa_proplist *p; + + p = pa_proplist_new(); + pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, "OBS Studio"); + pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, "application-exit"); + pa_proplist_sets(p, PA_PROP_MEDIA_ROLE, "production"); + + pa_threaded_mainloop_lock(m); + c = pa_context_new_with_proplist(pa_threaded_mainloop_get_api(m), + "OBS Studio", p); + pa_threaded_mainloop_unlock(m); + + pa_proplist_free(p); + + return c; +} + +/** + * Context state callback + */ +static void pulse_context_state_changed(pa_context *c, void *userdata) +{ + struct pulse_context_change *ctx = + (struct pulse_context_change *) userdata; + ctx->state = pa_context_get_state(c); + + pa_threaded_mainloop_signal(ctx->mainloop, 0); +} + +/* + * Connect context + */ +static int pulse_context_connect(pa_threaded_mainloop *m, pa_context *c) +{ + int status = 0; + struct pulse_context_change ctx; + ctx.mainloop = m; + ctx.state = PA_CONTEXT_UNCONNECTED; + + pa_threaded_mainloop_lock(m); + pa_context_set_state_callback(c, pulse_context_state_changed, + (void *) &ctx); + + status = pa_context_connect(c, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL); + if (status < 0) { + blog(LOG_ERROR, "pulse-input: Unable to connect! Status: %d", + status); + } + else { + for (;;) { + if (ctx.state == PA_CONTEXT_READY) { + blog(LOG_DEBUG, "pulse-input: Context Ready"); + break; + } + if (!PA_CONTEXT_IS_GOOD(ctx.state)) { + blog(LOG_ERROR, + "pulse-input: Context connect failed !"); + status = -1; + break; + } + pa_threaded_mainloop_wait(m); + } + } + + pa_threaded_mainloop_unlock(m); + return status; +} + +/* + * Source properties callback + */ +static void pulse_source_info(pa_context *c, const pa_source_info *i, int eol, + void *userdata) +{ + UNUSED_PARAMETER(c); + + if (eol != 0) + return; + + struct pulse_enumerate *e = (struct pulse_enumerate *) userdata; + + if ((e->input) ^ (i->monitor_of_sink == PA_INVALID_INDEX)) + return; + + blog(LOG_DEBUG, "pulse-input: Got source #%u '%s'", + i->index, i->description); + + obs_property_list_add_item(e->devices, i->description, i->name); + + pa_threaded_mainloop_signal(e->mainloop, 0); +} + +/* + * enumerate input/output devices + */ +static void pulse_enumerate_devices(obs_properties_t props, bool input) +{ + pa_context *c; + pa_operation *op; + pa_threaded_mainloop *m = pa_threaded_mainloop_new(); + struct pulse_enumerate e; + + e.mainloop = m; + e.devices = obs_properties_add_list(props, "device_id", "Device", + OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING); + e.input = input; + + pa_threaded_mainloop_start(m); + c = pulse_context_create(m); + + if (pulse_context_connect(m, c) < 0) + goto fail; + + pa_threaded_mainloop_lock(m); + + op = pa_context_get_source_info_list(c, pulse_source_info, (void *) &e); + while (pa_operation_get_state(op) == PA_OPERATION_RUNNING) + pa_threaded_mainloop_wait(m); + pa_operation_unref(op); + + pa_threaded_mainloop_unlock(m); + + pa_context_disconnect(c); +fail: + pa_context_unref(c); + pa_threaded_mainloop_stop(m); + pa_threaded_mainloop_free(m); +} + +/* + * get plugin properties + */ +static obs_properties_t pulse_properties(const char *locale, bool input) { UNUSED_PARAMETER(locale); - return "Pulse Audio Input"; + + blog(LOG_DEBUG, "pulse-input: properties requested !"); + + obs_properties_t props = obs_properties_create(); + + pulse_enumerate_devices(props, input); + + return props; +} + +static obs_properties_t pulse_input_properties(const char *locale) +{ + return pulse_properties(locale, true); +} + +static obs_properties_t pulse_output_properties(const char *locale) +{ + return pulse_properties(locale, false); +} + +/* + * get plugin defaults + */ +static void pulse_defaults(obs_data_t settings) +{ + obs_data_set_default_string(settings, "device_id", "default"); +} + +/* + * Returns the name of the plugin + */ +static const char *pulse_input_getname(const char *locale) +{ + UNUSED_PARAMETER(locale); + return "Pulse Audio Input Capture"; +} + +static const char *pulse_output_getname(const char *locale) +{ + UNUSED_PARAMETER(locale); + return "Pulse Audio Output Capture"; } /* @@ -383,14 +609,16 @@ static void pulse_destroy(void *vptr) if (data->thread) { void *ret; - event_signal(&data->event); + event_signal(data->event); pthread_join(data->thread, &ret); } - event_destroy(&data->event); + event_destroy(data->event); pa_proplist_free(data->props); + blog(LOG_DEBUG, "pulse-input: Input destroyed"); + bfree(data); } @@ -405,9 +633,10 @@ static void *pulse_create(obs_data_t settings, obs_source_t source) memset(data, 0, sizeof(struct pulse_data)); data->source = source; - data->samples_per_sec = 44100; data->speakers = SPEAKERS_STEREO; - data->format = PA_SAMPLE_S16LE; + + blog(LOG_DEBUG, "pulse-input: obs wants '%s'", + obs_data_getstring(settings, "device_id")); /* TODO: use obs-studio icon */ data->props = pa_proplist_new(); @@ -418,7 +647,6 @@ static void *pulse_create(obs_data_t settings, obs_source_t source) pa_proplist_sets(data->props, PA_PROP_MEDIA_ROLE, "production"); - if (event_init(&data->event, EVENT_TYPE_MANUAL) != 0) goto fail; if (pthread_create(&data->thread, NULL, pulse_thread, data) != 0) @@ -431,11 +659,24 @@ fail: return NULL; } -struct obs_source_info pulse_input = { - .id = "pulse_input", +struct obs_source_info pulse_input_capture = { + .id = "pulse_input_capture", .type = OBS_SOURCE_TYPE_INPUT, .output_flags = OBS_SOURCE_AUDIO, - .getname = pulse_getname, + .getname = pulse_input_getname, .create = pulse_create, - .destroy = pulse_destroy + .destroy = pulse_destroy, + .defaults = pulse_defaults, + .properties = pulse_input_properties +}; + +struct obs_source_info pulse_output_capture = { + .id = "pulse_output_capture", + .type = OBS_SOURCE_TYPE_INPUT, + .output_flags = OBS_SOURCE_AUDIO, + .getname = pulse_output_getname, + .create = pulse_create, + .destroy = pulse_destroy, + .defaults = pulse_defaults, + .properties = pulse_output_properties }; From c0ab8fadda476ae009a0b998f50cb7d6e54f55de Mon Sep 17 00:00:00 2001 From: fryshorts Date: Mon, 10 Mar 2014 23:02:37 +0100 Subject: [PATCH 14/15] moved and split up the linux xshm and pulseaudio capture plugins --- obs/obs-app.cpp | 4 +-- obs/window-basic-main.cpp | 3 ++ plugins/CMakeLists.txt | 3 ++ plugins/linux-pulseaudio/CMakeLists.txt | 18 +++++++++++ .../linux-pulseaudio/linux-pulseaudio.c | 2 -- .../linux-pulseaudio}/pulse-input.c | 0 plugins/linux-xshm/CMakeLists.txt | 25 ++++++++++++++++ plugins/linux-xshm/linux-xshm.c | 28 +++++++++++++++++ {test/linux => plugins/linux-xshm}/xcursor.c | 0 {test/linux => plugins/linux-xshm}/xcursor.h | 0 .../linux => plugins/linux-xshm}/xshm-input.c | 0 test/CMakeLists.txt | 4 --- test/linux/CMakeLists.txt | 30 ------------------- 13 files changed, 79 insertions(+), 38 deletions(-) create mode 100644 plugins/linux-pulseaudio/CMakeLists.txt rename test/linux/linux.c => plugins/linux-pulseaudio/linux-pulseaudio.c (92%) rename {test/linux => plugins/linux-pulseaudio}/pulse-input.c (100%) create mode 100644 plugins/linux-xshm/CMakeLists.txt create mode 100644 plugins/linux-xshm/linux-xshm.c rename {test/linux => plugins/linux-xshm}/xcursor.c (100%) rename {test/linux => plugins/linux-xshm}/xcursor.h (100%) rename {test/linux => plugins/linux-xshm}/xshm-input.c (100%) delete mode 100644 test/linux/CMakeLists.txt diff --git a/obs/obs-app.cpp b/obs/obs-app.cpp index 8e6e40f19..ae1b6e48f 100644 --- a/obs/obs-app.cpp +++ b/obs/obs-app.cpp @@ -188,8 +188,8 @@ void OBSApp::OBSInit() #define INPUT_AUDIO_SOURCE "wasapi_input_capture" #define OUTPUT_AUDIO_SOURCE "wasapi_output_capture" #else -#define INPUT_AUDIO_SOURCE "" -#define OUTPUT_AUDIO_SOURCE "" +#define INPUT_AUDIO_SOURCE "pulse_input_capture" +#define OUTPUT_AUDIO_SOURCE "pulse_output_capture" #endif const char *OBSApp::InputAudioSource() const diff --git a/obs/window-basic-main.cpp b/obs/window-basic-main.cpp index 787e25d86..5241d8f51 100644 --- a/obs/window-basic-main.cpp +++ b/obs/window-basic-main.cpp @@ -172,6 +172,9 @@ void OBSBasic::OBSInit() #elif _WIN32 obs_load_module("win-wasapi"); obs_load_module("win-capture"); +#else + obs_load_module("linux-xshm"); + obs_load_module("linux-pulseaudio"); #endif ResetAudioDevices(); diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 101b5953a..241e4ebfa 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -6,6 +6,9 @@ if(WIN32) add_subdirectory(win-capture) elseif(APPLE) add_subdirectory(mac-capture) +elseif("${CMAKE_SYSTEM_NAME}" MATCHES "Linux") + add_subdirectory(linux-xshm) + add_subdirectory(linux-pulseaudio) endif() add_subdirectory(obs-ffmpeg) diff --git a/plugins/linux-pulseaudio/CMakeLists.txt b/plugins/linux-pulseaudio/CMakeLists.txt new file mode 100644 index 000000000..d5258a94e --- /dev/null +++ b/plugins/linux-pulseaudio/CMakeLists.txt @@ -0,0 +1,18 @@ +find_package(PulseAudio REQUIRED) + +include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/libobs") + +set(linux-pulseaudio_SOURCES + linux-pulseaudio.c + pulse-input.c +) + +add_library(linux-pulseaudio MODULE + ${linux-pulseaudio_SOURCES} +) +target_link_libraries(linux-pulseaudio + libobs + ${PULSEAUDIO_LIBRARY} +) + +install_obs_plugin(linux-pulseaudio) diff --git a/test/linux/linux.c b/plugins/linux-pulseaudio/linux-pulseaudio.c similarity index 92% rename from test/linux/linux.c rename to plugins/linux-pulseaudio/linux-pulseaudio.c index 247363a23..1993dddce 100644 --- a/test/linux/linux.c +++ b/plugins/linux-pulseaudio/linux-pulseaudio.c @@ -18,14 +18,12 @@ along with this program. If not, see . OBS_DECLARE_MODULE() -extern struct obs_source_info xshm_input; extern struct obs_source_info pulse_input_capture; extern struct obs_source_info pulse_output_capture; bool obs_module_load(uint32_t obs_version) { UNUSED_PARAMETER(obs_version); - obs_register_source(&xshm_input); obs_register_source(&pulse_input_capture); obs_register_source(&pulse_output_capture); return true; diff --git a/test/linux/pulse-input.c b/plugins/linux-pulseaudio/pulse-input.c similarity index 100% rename from test/linux/pulse-input.c rename to plugins/linux-pulseaudio/pulse-input.c diff --git a/plugins/linux-xshm/CMakeLists.txt b/plugins/linux-xshm/CMakeLists.txt new file mode 100644 index 000000000..347aed6af --- /dev/null +++ b/plugins/linux-xshm/CMakeLists.txt @@ -0,0 +1,25 @@ +find_package(X11 REQUIRED) + +include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/libobs") + +set(linux-xshm_SOURCES + linux-xshm.c + xcursor.c + xshm-input.c +) +set(linux-xshm_HEADERS + xcursor.h +) + +add_library(linux-xshm MODULE + ${linux-xshm_SOURCES} + ${linux-xshm_HEADERS} +) +target_link_libraries(linux-xshm + libobs + ${X11_LIBRARIES} + ${X11_XShm_LIB} + ${X11_Xfixes_LIB} +) + +install_obs_plugin(linux-xshm) diff --git a/plugins/linux-xshm/linux-xshm.c b/plugins/linux-xshm/linux-xshm.c new file mode 100644 index 000000000..41aebec61 --- /dev/null +++ b/plugins/linux-xshm/linux-xshm.c @@ -0,0 +1,28 @@ +/* +Copyright (C) 2014 by Leonhard Oelke + +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, see . +*/ +#include + +OBS_DECLARE_MODULE() + +extern struct obs_source_info xshm_input; + +bool obs_module_load(uint32_t obs_version) +{ + UNUSED_PARAMETER(obs_version); + obs_register_source(&xshm_input); + return true; +} diff --git a/test/linux/xcursor.c b/plugins/linux-xshm/xcursor.c similarity index 100% rename from test/linux/xcursor.c rename to plugins/linux-xshm/xcursor.c diff --git a/test/linux/xcursor.h b/plugins/linux-xshm/xcursor.h similarity index 100% rename from test/linux/xcursor.h rename to plugins/linux-xshm/xcursor.h diff --git a/test/linux/xshm-input.c b/plugins/linux-xshm/xshm-input.c similarity index 100% rename from test/linux/xshm-input.c rename to plugins/linux-xshm/xshm-input.c diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6fa01ee6c..afcbf5ec0 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -8,7 +8,3 @@ endif() if(APPLE AND UNIX) add_subdirectory(osx) endif() - -if("${CMAKE_SYSTEM_NAME}" MATCHES "Linux") - add_subdirectory(linux) -endif() \ No newline at end of file diff --git a/test/linux/CMakeLists.txt b/test/linux/CMakeLists.txt deleted file mode 100644 index b63ed9aea..000000000 --- a/test/linux/CMakeLists.txt +++ /dev/null @@ -1,30 +0,0 @@ -project(linux) - -find_package(X11 REQUIRED) -find_package(PulseAudio REQUIRED) - -include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/libobs") - -set(linux_SOURCES - linux.c - xcursor.c - xshm-input.c - pulse-input.c -) -set(linux_HEADERS - xcursor.h -) - -add_library(linux MODULE - ${linux_SOURCES} - ${linux_HEADERS} -) -target_link_libraries(linux - libobs - ${X11_LIBRARIES} - ${X11_XShm_LIB} - ${X11_Xfixes_LIB} - ${PULSEAUDIO_LIBRARY} -) - -install_obs_plugin(linux) From 32c8cd00c54653c0d4e3fa243c0a2968623daa38 Mon Sep 17 00:00:00 2001 From: fryshorts Date: Tue, 11 Mar 2014 14:16:03 +0100 Subject: [PATCH 15/15] Fixed usage of event functions The event functions got renamed in obs. --- plugins/linux-pulseaudio/pulse-input.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/linux-pulseaudio/pulse-input.c b/plugins/linux-pulseaudio/pulse-input.c index d5598ca85..4dca35da7 100644 --- a/plugins/linux-pulseaudio/pulse-input.c +++ b/plugins/linux-pulseaudio/pulse-input.c @@ -41,7 +41,7 @@ const uint64_t pulse_start_delay = 100000; struct pulse_data { pthread_t thread; - event_t event; + os_event_t event; obs_source_t source; enum speaker_layout speakers; @@ -321,7 +321,7 @@ static int pulse_skip(struct pulse_data *data) size_t bytes; uint64_t pa_time; - while (event_try(data->event) == EAGAIN) { + while (os_event_try(data->event) == EAGAIN) { pulse_iterate(data); pa_stream_peek(data->stream, &frames, &bytes); @@ -374,7 +374,7 @@ static void *pulse_thread(void *vptr) out.samples_per_sec = data->samples_per_sec; out.format = pulse_to_obs_audio_format(data->format); - while (event_try(data->event) == EAGAIN) { + while (os_event_try(data->event) == EAGAIN) { pulse_iterate(data); pa_stream_peek(data->stream, &frames, &bytes); @@ -609,11 +609,11 @@ static void pulse_destroy(void *vptr) if (data->thread) { void *ret; - event_signal(data->event); + os_event_signal(data->event); pthread_join(data->thread, &ret); } - event_destroy(data->event); + os_event_destroy(data->event); pa_proplist_free(data->props); @@ -647,7 +647,7 @@ static void *pulse_create(obs_data_t settings, obs_source_t source) pa_proplist_sets(data->props, PA_PROP_MEDIA_ROLE, "production"); - if (event_init(&data->event, EVENT_TYPE_MANUAL) != 0) + if (os_event_init(&data->event, OS_EVENT_TYPE_MANUAL) != 0) goto fail; if (pthread_create(&data->thread, NULL, pulse_thread, data) != 0) goto fail;