2016-02-23 23:16:51 +00:00
|
|
|
#include <obs-module.h>
|
|
|
|
#include <util/darray.h>
|
|
|
|
#include <util/threading.h>
|
|
|
|
#include <util/platform.h>
|
|
|
|
|
|
|
|
#import <CoreGraphics/CGWindow.h>
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
#include "window-utils.h"
|
|
|
|
|
|
|
|
struct window_capture {
|
|
|
|
obs_source_t *source;
|
|
|
|
|
|
|
|
struct cocoa_window window;
|
|
|
|
|
|
|
|
//CGRect bounds;
|
|
|
|
//CGWindowListOption window_option;
|
|
|
|
CGWindowImageOption image_option;
|
|
|
|
|
|
|
|
CGColorSpaceRef color_space;
|
|
|
|
|
|
|
|
DARRAY(uint8_t) buffer;
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
pthread_t capture_thread;
|
2016-02-23 23:16:51 +00:00
|
|
|
os_event_t *capture_event;
|
|
|
|
os_event_t *stop_event;
|
|
|
|
};
|
|
|
|
|
|
|
|
static CGImageRef get_image(struct window_capture *wc)
|
|
|
|
{
|
2019-09-22 21:19:10 +00:00
|
|
|
NSArray *arr = (NSArray *)CGWindowListCreate(
|
|
|
|
kCGWindowListOptionIncludingWindow, wc->window.window_id);
|
2016-02-23 23:16:51 +00:00
|
|
|
[arr autorelease];
|
|
|
|
|
|
|
|
if (arr.count)
|
2019-09-22 21:19:10 +00:00
|
|
|
return CGWindowListCreateImage(
|
|
|
|
CGRectNull, kCGWindowListOptionIncludingWindow,
|
|
|
|
wc->window.window_id, wc->image_option);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
if (!find_window(&wc->window, NULL, false))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return CGWindowListCreateImage(CGRectNull,
|
2019-09-22 21:19:10 +00:00
|
|
|
kCGWindowListOptionIncludingWindow,
|
|
|
|
wc->window.window_id, wc->image_option);
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void capture_frame(struct window_capture *wc)
|
|
|
|
{
|
2019-09-22 21:19:10 +00:00
|
|
|
uint64_t ts = os_gettime_ns();
|
2016-02-23 23:16:51 +00:00
|
|
|
CGImageRef img = get_image(wc);
|
|
|
|
if (!img)
|
|
|
|
return;
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
size_t width = CGImageGetWidth(img);
|
2016-02-23 23:16:51 +00:00
|
|
|
size_t height = CGImageGetHeight(img);
|
|
|
|
|
|
|
|
CGRect rect = {{0, 0}, {width, height}};
|
|
|
|
da_reserve(wc->buffer, width * height * 4);
|
|
|
|
uint8_t *data = wc->buffer.array;
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
CGContextRef cg_context = CGBitmapContextCreate(
|
|
|
|
data, width, height, 8, width * 4, wc->color_space,
|
|
|
|
kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst);
|
2016-02-23 23:16:51 +00:00
|
|
|
CGContextSetBlendMode(cg_context, kCGBlendModeCopy);
|
|
|
|
CGContextDrawImage(cg_context, rect, img);
|
|
|
|
CGContextRelease(cg_context);
|
|
|
|
CGImageRelease(img);
|
|
|
|
|
|
|
|
struct obs_source_frame frame = {
|
2019-09-22 21:19:10 +00:00
|
|
|
.format = VIDEO_FORMAT_BGRA,
|
|
|
|
.width = width,
|
|
|
|
.height = height,
|
|
|
|
.data[0] = data,
|
2016-02-23 23:16:51 +00:00
|
|
|
.linesize[0] = width * 4,
|
2019-09-22 21:19:10 +00:00
|
|
|
.timestamp = ts,
|
2016-02-23 23:16:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
obs_source_output_video(wc->source, &frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *capture_thread(void *data)
|
|
|
|
{
|
|
|
|
struct window_capture *wc = data;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
os_event_wait(wc->capture_event);
|
|
|
|
if (os_event_try(wc->stop_event) != EAGAIN)
|
|
|
|
break;
|
|
|
|
|
|
|
|
@autoreleasepool {
|
|
|
|
capture_frame(wc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *window_capture_create_internal(obs_data_t *settings,
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_source_t *source)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
|
|
|
struct window_capture *wc = bzalloc(sizeof(struct window_capture));
|
|
|
|
|
|
|
|
wc->source = source;
|
|
|
|
|
|
|
|
wc->color_space = CGColorSpaceCreateDeviceRGB();
|
|
|
|
|
|
|
|
da_init(wc->buffer);
|
|
|
|
|
|
|
|
init_window(&wc->window, settings);
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
wc->image_option = obs_data_get_bool(settings, "show_shadow")
|
|
|
|
? kCGWindowImageDefault
|
|
|
|
: kCGWindowImageBoundsIgnoreFraming;
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
os_event_init(&wc->capture_event, OS_EVENT_TYPE_AUTO);
|
|
|
|
os_event_init(&wc->stop_event, OS_EVENT_TYPE_MANUAL);
|
|
|
|
|
|
|
|
pthread_create(&wc->capture_thread, NULL, capture_thread, wc);
|
|
|
|
|
|
|
|
return wc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *window_capture_create(obs_data_t *settings, obs_source_t *source)
|
|
|
|
{
|
|
|
|
@autoreleasepool {
|
|
|
|
return window_capture_create_internal(settings, source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void window_capture_destroy(void *data)
|
|
|
|
{
|
|
|
|
struct window_capture *cap = data;
|
|
|
|
|
|
|
|
os_event_signal(cap->stop_event);
|
|
|
|
os_event_signal(cap->capture_event);
|
2019-09-22 21:19:10 +00:00
|
|
|
|
2016-02-23 23:16:51 +00:00
|
|
|
pthread_join(cap->capture_thread, NULL);
|
|
|
|
|
|
|
|
CGColorSpaceRelease(cap->color_space);
|
|
|
|
|
|
|
|
da_free(cap->buffer);
|
|
|
|
|
|
|
|
os_event_destroy(cap->capture_event);
|
|
|
|
os_event_destroy(cap->stop_event);
|
|
|
|
|
|
|
|
destroy_window(&cap->window);
|
|
|
|
|
|
|
|
bfree(cap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void window_capture_defaults(obs_data_t *settings)
|
|
|
|
{
|
|
|
|
obs_data_set_default_bool(settings, "show_shadow", false);
|
|
|
|
window_defaults(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
static obs_properties_t *window_capture_properties(void *unused)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(unused);
|
|
|
|
|
|
|
|
obs_properties_t *props = obs_properties_create();
|
|
|
|
|
|
|
|
add_window_properties(props);
|
|
|
|
|
|
|
|
obs_properties_add_bool(props, "show_shadow",
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_module_text("WindowCapture.ShowShadow"));
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void window_capture_update_internal(struct window_capture *wc,
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_data_t *settings)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2019-09-22 21:19:10 +00:00
|
|
|
wc->image_option = obs_data_get_bool(settings, "show_shadow")
|
|
|
|
? kCGWindowImageDefault
|
|
|
|
: kCGWindowImageBoundsIgnoreFraming;
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
update_window(&wc->window, settings);
|
2018-02-19 19:54:37 +00:00
|
|
|
|
|
|
|
if (wc->window.window_name.length) {
|
2019-09-22 21:19:10 +00:00
|
|
|
blog(LOG_INFO,
|
|
|
|
"[window-capture: '%s'] update settings:\n"
|
|
|
|
"\twindow: %s\n"
|
|
|
|
"\towner: %s",
|
|
|
|
obs_source_get_name(wc->source),
|
|
|
|
[wc->window.window_name UTF8String],
|
|
|
|
[wc->window.owner_name UTF8String]);
|
2018-02-19 19:54:37 +00:00
|
|
|
}
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void window_capture_update(void *data, obs_data_t *settings)
|
|
|
|
{
|
|
|
|
@autoreleasepool {
|
|
|
|
return window_capture_update_internal(data, settings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *window_capture_getname(void *unused)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(unused);
|
|
|
|
return obs_module_text("WindowCapture");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void window_capture_tick_internal(struct window_capture *wc,
|
2019-09-22 21:19:10 +00:00
|
|
|
float seconds)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(seconds);
|
|
|
|
os_event_signal(wc->capture_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void window_capture_tick(void *data, float seconds)
|
|
|
|
{
|
|
|
|
struct window_capture *wc = data;
|
|
|
|
|
|
|
|
if (!obs_source_showing(wc->source))
|
|
|
|
return;
|
|
|
|
|
|
|
|
@autoreleasepool {
|
|
|
|
return window_capture_tick_internal(data, seconds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct obs_source_info window_capture_info = {
|
2019-09-22 21:19:10 +00:00
|
|
|
.id = "window_capture",
|
|
|
|
.type = OBS_SOURCE_TYPE_INPUT,
|
|
|
|
.get_name = window_capture_getname,
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
.create = window_capture_create,
|
|
|
|
.destroy = window_capture_destroy,
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
.output_flags = OBS_SOURCE_ASYNC_VIDEO,
|
|
|
|
.video_tick = window_capture_tick,
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
.get_defaults = window_capture_defaults,
|
2016-02-23 23:16:51 +00:00
|
|
|
.get_properties = window_capture_properties,
|
2019-09-22 21:19:10 +00:00
|
|
|
.update = window_capture_update,
|
2016-02-23 23:16:51 +00:00
|
|
|
};
|