1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
|
#include "../include/pipewire.h"
#include "../include/egl.h"
#include <pipewire/pipewire.h>
#include <spa/param/video/format-utils.h>
#include <spa/debug/types.h>
#include <libdrm/drm_fourcc.h>
#include <fcntl.h>
#include <unistd.h>
/* This code is partially based on xr-video-player pipewire implementation which is based on obs-studio's pipewire implementation */
/* TODO: Make gsr_pipewire_init asynchronous */
/* TODO: Support 10-bit capture (hdr) when pipewire supports it */
/* TODO: Test all of the image formats */
#ifndef SPA_POD_PROP_FLAG_DONT_FIXATE
#define SPA_POD_PROP_FLAG_DONT_FIXATE (1 << 4)
#endif
#define CURSOR_META_SIZE(width, height) \
(sizeof(struct spa_meta_cursor) + sizeof(struct spa_meta_bitmap) + \
width * height * 4)
static bool parse_pw_version(gsr_pipewire_data_version *dst, const char *version) {
const int n_matches = sscanf(version, "%d.%d.%d", &dst->major, &dst->minor, &dst->micro);
return n_matches == 3;
}
static bool check_pw_version(const gsr_pipewire_data_version *pw_version, int major, int minor, int micro) {
if (pw_version->major != major)
return pw_version->major > major;
if (pw_version->minor != minor)
return pw_version->minor > minor;
return pw_version->micro >= micro;
}
static void update_pw_versions(gsr_pipewire *self, const char *version) {
fprintf(stderr, "gsr info: pipewire: server version: %s\n", version);
fprintf(stderr, "gsr info: pipewire: library version: %s\n", pw_get_library_version());
fprintf(stderr, "gsr info: pipewire: header version: %s\n", pw_get_headers_version());
if(!parse_pw_version(&self->server_version, version))
fprintf(stderr, "gsr error: pipewire: failed to parse server version\n");
}
static void on_core_info_cb(void *user_data, const struct pw_core_info *info) {
gsr_pipewire *self = user_data;
update_pw_versions(self, info->version);
}
static void on_core_error_cb(void *user_data, uint32_t id, int seq, int res, const char *message) {
gsr_pipewire *self = user_data;
fprintf(stderr, "gsr error: pipewire: error id:%u seq:%d res:%d: %s\n", id, seq, res, message);
pw_thread_loop_signal(self->thread_loop, false);
}
static void on_core_done_cb(void *user_data, uint32_t id, int seq) {
gsr_pipewire *self = user_data;
if (id == PW_ID_CORE && self->server_version_sync == seq)
pw_thread_loop_signal(self->thread_loop, false);
}
static bool is_cursor_format_supported(const enum spa_video_format format) {
switch(format) {
case SPA_VIDEO_FORMAT_RGBx: return true;
case SPA_VIDEO_FORMAT_BGRx: return true;
case SPA_VIDEO_FORMAT_xRGB: return true;
case SPA_VIDEO_FORMAT_xBGR: return true;
case SPA_VIDEO_FORMAT_RGBA: return true;
case SPA_VIDEO_FORMAT_BGRA: return true;
case SPA_VIDEO_FORMAT_ARGB: return true;
case SPA_VIDEO_FORMAT_ABGR: return true;
default: break;
}
return false;
}
static const struct pw_core_events core_events = {
PW_VERSION_CORE_EVENTS,
.info = on_core_info_cb,
.done = on_core_done_cb,
.error = on_core_error_cb,
};
static void on_process_cb(void *user_data) {
gsr_pipewire *self = user_data;
struct spa_meta_cursor *cursor = NULL;
/* Find the most recent buffer */
struct pw_buffer *pw_buf = NULL;
for(;;) {
struct pw_buffer *aux = pw_stream_dequeue_buffer(self->stream);
if(!aux)
break;
if(pw_buf)
pw_stream_queue_buffer(self->stream, pw_buf);
pw_buf = aux;
}
if(!pw_buf) {
fprintf(stderr, "gsr info: pipewire: out of buffers!\n");
return;
}
struct spa_buffer *buffer = pw_buf->buffer;
const bool has_buffer = buffer->datas[0].chunk->size != 0;
if(!has_buffer)
goto read_metadata;
pthread_mutex_lock(&self->mutex);
if(buffer->datas[0].type == SPA_DATA_DmaBuf) {
if(self->dmabuf_data.fd > 0) {
close(self->dmabuf_data.fd);
self->dmabuf_data.fd = -1;
}
if(buffer->n_datas > 0) {
self->dmabuf_data.fd = dup(buffer->datas[0].fd);
self->dmabuf_data.offset = buffer->datas[0].chunk->offset;
self->dmabuf_data.stride = buffer->datas[0].chunk->stride;
} else {
self->dmabuf_data.fd = -1;
}
} else {
// TODO:
}
struct spa_meta_region *region = spa_buffer_find_meta_data(buffer, SPA_META_VideoCrop, sizeof(*region));
if(region && spa_meta_region_is_valid(region)) {
// fprintf(stderr, "gsr info: pipewire: crop Region available (%dx%d+%d+%d)\n",
// region->region.position.x, region->region.position.y,
// region->region.size.width, region->region.size.height);
self->crop.x = region->region.position.x;
self->crop.y = region->region.position.y;
self->crop.width = region->region.size.width;
self->crop.height = region->region.size.height;
self->crop.valid = true;
} else {
self->crop.valid = false;
}
pthread_mutex_unlock(&self->mutex);
read_metadata:
cursor = spa_buffer_find_meta_data(buffer, SPA_META_Cursor, sizeof(*cursor));
self->cursor.valid = cursor && spa_meta_cursor_is_valid(cursor);
if (self->cursor.visible && self->cursor.valid) {
pthread_mutex_lock(&self->mutex);
struct spa_meta_bitmap *bitmap = NULL;
if (cursor->bitmap_offset)
bitmap = SPA_MEMBER(cursor, cursor->bitmap_offset, struct spa_meta_bitmap);
if (bitmap && bitmap->size.width > 0 && bitmap->size.height && is_cursor_format_supported(bitmap->format)) {
const uint8_t *bitmap_data = SPA_MEMBER(bitmap, bitmap->offset, uint8_t);
fprintf(stderr, "gsr info: pipewire: cursor bitmap update, size: %dx%d, format: %s\n",
(int)bitmap->size.width, (int)bitmap->size.height, spa_debug_type_find_name(spa_type_video_format, bitmap->format));
const size_t bitmap_size = bitmap->size.width * bitmap->size.height * 4;
uint8_t *new_bitmap_data = realloc(self->cursor.data, bitmap_size);
if(new_bitmap_data) {
self->cursor.data = new_bitmap_data;
/* TODO: Convert bgr and other image formats to rgb here */
memcpy(self->cursor.data, bitmap_data, bitmap_size);
}
self->cursor.hotspot_x = cursor->hotspot.x;
self->cursor.hotspot_y = cursor->hotspot.y;
self->cursor.width = bitmap->size.width;
self->cursor.height = bitmap->size.height;
}
self->cursor.x = cursor->position.x;
self->cursor.y = cursor->position.y;
pthread_mutex_unlock(&self->mutex);
//fprintf(stderr, "gsr info: pipewire: cursor: %d %d %d %d\n", cursor->hotspot.x, cursor->hotspot.y, cursor->position.x, cursor->position.y);
}
pw_stream_queue_buffer(self->stream, pw_buf);
}
static void on_param_changed_cb(void *user_data, uint32_t id, const struct spa_pod *param) {
gsr_pipewire *self = user_data;
if (!param || id != SPA_PARAM_Format)
return;
int result = spa_format_parse(param, &self->format.media_type, &self->format.media_subtype);
if (result < 0)
return;
if (self->format.media_type != SPA_MEDIA_TYPE_video || self->format.media_subtype != SPA_MEDIA_SUBTYPE_raw)
return;
pthread_mutex_lock(&self->mutex);
spa_format_video_raw_parse(param, &self->format.info.raw);
pthread_mutex_unlock(&self->mutex);
uint32_t buffer_types = 0;
const bool has_modifier = spa_pod_find_prop(param, NULL, SPA_FORMAT_VIDEO_modifier) != NULL;
if(has_modifier || check_pw_version(&self->server_version, 0, 3, 24))
buffer_types |= 1 << SPA_DATA_DmaBuf;
fprintf(stderr, "gsr info: pipewire: negotiated format:\n");
fprintf(stderr, "gsr info: pipewire: Format: %d (%s)\n",
self->format.info.raw.format,
spa_debug_type_find_name(spa_type_video_format, self->format.info.raw.format));
if(has_modifier) {
fprintf(stderr, "gsr info: pipewire: Modifier: %" PRIu64 "\n", self->format.info.raw.modifier);
}
fprintf(stderr, "gsr info: pipewire: Size: %dx%d\n", self->format.info.raw.size.width, self->format.info.raw.size.height);
fprintf(stderr, "gsr info: pipewire: Framerate: %d/%d\n", self->format.info.raw.framerate.num, self->format.info.raw.framerate.denom);
uint8_t params_buffer[1024];
struct spa_pod_builder pod_builder = SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
const struct spa_pod *params[3];
params[0] = spa_pod_builder_add_object(
&pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
SPA_PARAM_META_type, SPA_POD_Id(SPA_META_VideoCrop),
SPA_PARAM_META_size,
SPA_POD_Int(sizeof(struct spa_meta_region)));
params[1] = spa_pod_builder_add_object(
&pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Cursor),
SPA_PARAM_META_size,
SPA_POD_CHOICE_RANGE_Int(CURSOR_META_SIZE(64, 64),
CURSOR_META_SIZE(1, 1),
CURSOR_META_SIZE(1024, 1024)));
params[2] = spa_pod_builder_add_object(
&pod_builder, SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
SPA_PARAM_BUFFERS_dataType, SPA_POD_Int(buffer_types));
pw_stream_update_params(self->stream, params, 3);
self->negotiated = true;
}
static void on_state_changed_cb(void *user_data, enum pw_stream_state old, enum pw_stream_state state, const char *error) {
(void)old;
gsr_pipewire *self = user_data;
if(state == PW_STREAM_STATE_STREAMING)
self->started = true;
if(self->started && state == PW_STREAM_STATE_PAUSED) {
self->started = false;
self->stopped = true;
}
fprintf(stderr, "gsr info: pipewire: stream %p state: \"%s\" (error: %s)\n",
(void*)self->stream, pw_stream_state_as_string(state),
error ? error : "none");
}
static const struct pw_stream_events stream_events = {
PW_VERSION_STREAM_EVENTS,
.state_changed = on_state_changed_cb,
.param_changed = on_param_changed_cb,
.process = on_process_cb,
};
static inline struct spa_pod *build_format(struct spa_pod_builder *b,
const gsr_pipewire_video_info *ovi,
uint32_t format, const uint64_t *modifiers,
size_t modifier_count)
{
struct spa_pod_frame format_frame;
spa_pod_builder_push_object(b, &format_frame, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat);
spa_pod_builder_add(b, SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video), 0);
spa_pod_builder_add(b, SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw), 0);
spa_pod_builder_add(b, SPA_FORMAT_VIDEO_format, SPA_POD_Id(format), 0);
if (modifier_count > 0) {
struct spa_pod_frame modifier_frame;
spa_pod_builder_prop(b, SPA_FORMAT_VIDEO_modifier, SPA_POD_PROP_FLAG_MANDATORY | SPA_POD_PROP_FLAG_DONT_FIXATE);
spa_pod_builder_push_choice(b, &modifier_frame, SPA_CHOICE_Enum, 0);
/* The first element of choice pods is the preferred value. Here
* we arbitrarily pick the first modifier as the preferred one.
*/
// TODO:
spa_pod_builder_long(b, modifiers[0]);
for(uint32_t i = 0; i < modifier_count; i++)
spa_pod_builder_long(b, modifiers[i]);
spa_pod_builder_pop(b, &modifier_frame);
}
spa_pod_builder_add(b, SPA_FORMAT_VIDEO_size,
SPA_POD_CHOICE_RANGE_Rectangle(
&SPA_RECTANGLE(32, 32),
&SPA_RECTANGLE(1, 1),
&SPA_RECTANGLE(16384, 16384)),
SPA_FORMAT_VIDEO_framerate,
SPA_POD_CHOICE_RANGE_Fraction(
&SPA_FRACTION(ovi->fps_num, ovi->fps_den),
&SPA_FRACTION(0, 1), &SPA_FRACTION(500, 1)),
0);
return spa_pod_builder_pop(b, &format_frame);
}
#define NUM_VIDEO_FORMATS 10
/* https://gstreamer.freedesktop.org/documentation/additional/design/mediatype-video-raw.html?gi-language=c#formats */
/* For some reason gstreamer formats are in opposite order to drm formats */
static int64_t spa_video_format_to_drm_format(const enum spa_video_format format) {
switch(format) {
case SPA_VIDEO_FORMAT_RGBx: return DRM_FORMAT_XBGR8888;
case SPA_VIDEO_FORMAT_BGRx: return DRM_FORMAT_XRGB8888;
case SPA_VIDEO_FORMAT_xRGB: return DRM_FORMAT_BGRX8888;
case SPA_VIDEO_FORMAT_xBGR: return DRM_FORMAT_RGBX8888;
case SPA_VIDEO_FORMAT_RGBA: return DRM_FORMAT_ABGR8888;
case SPA_VIDEO_FORMAT_BGRA: return DRM_FORMAT_ARGB8888;
case SPA_VIDEO_FORMAT_ARGB: return DRM_FORMAT_BGRA8888;
case SPA_VIDEO_FORMAT_ABGR: return DRM_FORMAT_RGBA8888;
case SPA_VIDEO_FORMAT_RGB: return DRM_FORMAT_XBGR8888;
case SPA_VIDEO_FORMAT_BGR: return DRM_FORMAT_XRGB8888;
default: break;
}
return DRM_FORMAT_INVALID;
}
static const enum spa_video_format supported_video_formats[] = {
SPA_VIDEO_FORMAT_BGRx,
SPA_VIDEO_FORMAT_BGRA,
SPA_VIDEO_FORMAT_ABGR,
SPA_VIDEO_FORMAT_xBGR,
SPA_VIDEO_FORMAT_BGR,
SPA_VIDEO_FORMAT_RGBx,
SPA_VIDEO_FORMAT_xRGB,
SPA_VIDEO_FORMAT_RGBA,
SPA_VIDEO_FORMAT_ARGB,
SPA_VIDEO_FORMAT_RGB,
};
static void spa_video_format_get_modifiers(gsr_pipewire *self, const enum spa_video_format format, uint64_t *modifiers, int32_t max_modifiers, int32_t *num_modifiers) {
*num_modifiers = 0;
if(!self->egl->eglQueryDmaBufModifiersEXT) {
fprintf(stderr, "gsr error: spa_video_format_get_modifiers: failed to initialize modifiers because eglQueryDmaBufModifiersEXT is not available\n");
modifiers[0] = DRM_FORMAT_MOD_LINEAR;
modifiers[1] = DRM_FORMAT_MOD_INVALID;
*num_modifiers = 2;
return;
}
const int64_t drm_format = spa_video_format_to_drm_format(format);
if(!self->egl->eglQueryDmaBufModifiersEXT(self->egl->egl_display, drm_format, max_modifiers, modifiers, NULL, num_modifiers)) {
fprintf(stderr, "gsr error: spa_video_format_get_modifiers: eglQueryDmaBufModifiersEXT failed with drm format %" PRIi64 "\n", drm_format);
modifiers[0] = DRM_FORMAT_MOD_LINEAR;
modifiers[1] = DRM_FORMAT_MOD_INVALID;
*num_modifiers = 2;
return;
}
if(*num_modifiers + 2 <= max_modifiers) {
modifiers[*num_modifiers + 0] = DRM_FORMAT_MOD_LINEAR;
modifiers[*num_modifiers + 1] = DRM_FORMAT_MOD_INVALID;
*num_modifiers += 2;
}
}
static bool gsr_pipewire_build_format_params(gsr_pipewire *self, struct spa_pod_builder *pod_builder, struct spa_pod **params) {
if(!check_pw_version(&self->server_version, 0, 3, 33))
return false;
uint64_t modifiers[1024];
int32_t num_modifiers = 0;
for(size_t i = 0; i < NUM_VIDEO_FORMATS; i++) {
const enum spa_video_format format = supported_video_formats[i];
spa_video_format_get_modifiers(self, format, modifiers, sizeof(modifiers), &num_modifiers);
params[i] = build_format(pod_builder, &self->video_info, format, modifiers, num_modifiers);
}
return true;
}
static void renegotiate_format(void *data, uint64_t expirations) {
(void)expirations;
gsr_pipewire *self = (gsr_pipewire*)data;
pw_thread_loop_lock(self->thread_loop);
struct spa_pod *params[NUM_VIDEO_FORMATS];
uint8_t params_buffer[2048];
struct spa_pod_builder pod_builder = SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
if (!gsr_pipewire_build_format_params(self, &pod_builder, params)) {
pw_thread_loop_unlock(self->thread_loop);
return;
}
pw_stream_update_params(self->stream, (const struct spa_pod**)params, NUM_VIDEO_FORMATS);
pw_thread_loop_unlock(self->thread_loop);
}
static bool gsr_pipewire_setup_stream(gsr_pipewire *self) {
struct spa_pod *params[NUM_VIDEO_FORMATS];
uint8_t params_buffer[2048];
struct spa_pod_builder pod_builder = SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
self->thread_loop = pw_thread_loop_new("PipeWire thread loop", NULL);
if(!self->thread_loop) {
fprintf(stderr, "gsr error: gsr_pipewire_setup_stream: failed to create pipewire thread\n");
goto error;
}
self->context = pw_context_new(pw_thread_loop_get_loop(self->thread_loop), NULL, 0);
if(!self->context) {
fprintf(stderr, "gsr error: gsr_pipewire_setup_stream: failed to create pipewire context\n");
goto error;
}
if(pw_thread_loop_start(self->thread_loop) < 0) {
fprintf(stderr, "gsr error: gsr_pipewire_setup_stream: failed to start thread\n");
goto error;
}
pw_thread_loop_lock(self->thread_loop);
// TODO: Why pass 5 to fcntl?
self->core = pw_context_connect_fd(self->context, fcntl(self->fd, F_DUPFD_CLOEXEC, 5), NULL, 0);
if(!self->core) {
pw_thread_loop_unlock(self->thread_loop);
fprintf(stderr, "gsr error: gsr_pipewire_setup_stream: failed to connect to fd %d\n", self->fd);
goto error;
}
// TODO: Error check
pw_core_add_listener(self->core, &self->core_listener, &core_events, self);
// TODO: Cleanup?
self->reneg = pw_loop_add_event(pw_thread_loop_get_loop(self->thread_loop), renegotiate_format, self);
if(!self->reneg) {
pw_thread_loop_unlock(self->thread_loop);
fprintf(stderr, "gsr error: gsr_pipewire_setup_stream: pw_loop_add_event failed\n");
goto error;
}
self->server_version_sync = pw_core_sync(self->core, PW_ID_CORE, 0);
pw_thread_loop_wait(self->thread_loop);
self->stream = pw_stream_new(self->core, "com.dec05eba.gpu_screen_recorder",
pw_properties_new(PW_KEY_MEDIA_TYPE, "Video",
PW_KEY_MEDIA_CATEGORY, "Capture",
PW_KEY_MEDIA_ROLE, "Screen", NULL));
if(!self->stream) {
pw_thread_loop_unlock(self->thread_loop);
fprintf(stderr, "gsr error: gsr_pipewire_setup_stream: failed to create stream\n");
goto error;
}
pw_stream_add_listener(self->stream, &self->stream_listener, &stream_events, self);
if(!gsr_pipewire_build_format_params(self, &pod_builder, params)) {
pw_thread_loop_unlock(self->thread_loop);
fprintf(stderr, "gsr error: gsr_pipewire_setup_stream: failed to build format params\n");
goto error;
}
if(pw_stream_connect(
self->stream, PW_DIRECTION_INPUT, self->node,
PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS, (const struct spa_pod**)params,
NUM_VIDEO_FORMATS) < 0)
{
pw_thread_loop_unlock(self->thread_loop);
fprintf(stderr, "gsr error: gsr_pipewire_setup_stream: failed to connect stream\n");
goto error;
}
pw_thread_loop_unlock(self->thread_loop);
return true;
error:
if(self->thread_loop) {
//pw_thread_loop_wait(self->thread_loop);
pw_thread_loop_stop(self->thread_loop);
}
if(self->stream) {
pw_stream_disconnect(self->stream);
pw_stream_destroy(self->stream);
self->stream = NULL;
}
if(self->core) {
pw_core_disconnect(self->core);
self->core = NULL;
}
if(self->context) {
pw_context_destroy(self->context);
self->context = NULL;
}
if(self->thread_loop) {
pw_thread_loop_destroy(self->thread_loop);
self->thread_loop = NULL;
}
return false;
}
static int pw_init_counter = 0;
bool gsr_pipewire_init(gsr_pipewire *self, int pipewire_fd, uint32_t pipewire_node, int fps, bool capture_cursor, gsr_egl *egl) {
if(pw_init_counter == 0)
pw_init(NULL, NULL);
++pw_init_counter;
memset(self, 0, sizeof(*self));
self->egl = egl;
self->fd = pipewire_fd;
self->node = pipewire_node;
if(pthread_mutex_init(&self->mutex, NULL) != 0) {
fprintf(stderr, "gsr error: gsr_pipewire_init: failed to initialize mutex\n");
gsr_pipewire_deinit(self);
return false;
}
self->mutex_initialized = true;
self->video_info.fps_num = fps;
self->video_info.fps_den = 1;
self->cursor.visible = capture_cursor;
if(!gsr_pipewire_setup_stream(self)) {
gsr_pipewire_deinit(self);
return false;
}
return true;
}
void gsr_pipewire_deinit(gsr_pipewire *self) {
if(self->thread_loop) {
//pw_thread_loop_wait(self->thread_loop);
pw_thread_loop_stop(self->thread_loop);
}
if(self->stream) {
pw_stream_disconnect(self->stream);
pw_stream_destroy(self->stream);
self->stream = NULL;
}
if(self->core) {
pw_core_disconnect(self->core);
self->core = NULL;
}
if(self->context) {
pw_context_destroy(self->context);
self->context = NULL;
}
if(self->thread_loop) {
pw_thread_loop_destroy(self->thread_loop);
self->thread_loop = NULL;
}
if(self->fd > 0) {
close(self->fd);
self->fd = -1;
}
if(self->dmabuf_data.fd > 0) {
close(self->dmabuf_data.fd);
self->dmabuf_data.fd = -1;
}
self->negotiated = false;
if(self->mutex_initialized) {
pthread_mutex_destroy(&self->mutex);
self->mutex_initialized = false;
}
if(self->cursor.data) {
free(self->cursor.data);
self->cursor.data = NULL;
}
--pw_init_counter;
if(pw_init_counter == 0) {
#if PW_CHECK_VERSION(0, 3, 49)
pw_deinit();
#endif
}
}
bool gsr_pipewire_map_texture(gsr_pipewire *self, unsigned int texture_id, unsigned int cursor_texture_id, gsr_pipewire_region *region, gsr_pipewire_region *cursor_region, int *plane_fd) {
*plane_fd = -1;
pthread_mutex_lock(&self->mutex);
if(!self->negotiated || self->dmabuf_data.fd <= 0) {
pthread_mutex_unlock(&self->mutex);
return false;
}
*plane_fd = self->dmabuf_data.fd;
self->dmabuf_data.fd = -1;
/* TODO: Support multiple planes */
const intptr_t img_attr[] = {
EGL_LINUX_DRM_FOURCC_EXT, spa_video_format_to_drm_format(self->format.info.raw.format),
EGL_WIDTH, self->format.info.raw.size.width,
EGL_HEIGHT, self->format.info.raw.size.height,
EGL_DMA_BUF_PLANE0_FD_EXT, *plane_fd,
EGL_DMA_BUF_PLANE0_OFFSET_EXT, self->dmabuf_data.offset,
EGL_DMA_BUF_PLANE0_PITCH_EXT, self->dmabuf_data.stride,
EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT, self->format.info.raw.modifier & 0xFFFFFFFFULL,
EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, self->format.info.raw.modifier >> 32ULL,
EGL_NONE
};
EGLImage image = self->egl->eglCreateImage(self->egl->egl_display, 0, EGL_LINUX_DMA_BUF_EXT, NULL, img_attr);
self->egl->glBindTexture(GL_TEXTURE_2D, texture_id);
self->egl->glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
self->egl->eglDestroyImage(self->egl->egl_display, image);
self->egl->glBindTexture(GL_TEXTURE_2D, 0);
if(self->cursor.data) {
self->egl->glBindTexture(GL_TEXTURE_2D, cursor_texture_id);
// TODO: glTextureSubImage2D if same size
self->egl->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, self->cursor.width, self->cursor.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, self->cursor.data);
self->egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
self->egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
self->egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
self->egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
self->egl->glBindTexture(GL_TEXTURE_2D, 0);
free(self->cursor.data);
self->cursor.data = NULL;
}
region->x = 0;
region->y = 0;
region->width = self->format.info.raw.size.width;
region->height = self->format.info.raw.size.height;
if(self->crop.valid) {
region->x = self->crop.x;
region->y = self->crop.y;
region->width = self->crop.width;
region->height = self->crop.height;
}
/* TODO: Test if cursor hotspot is correct */
cursor_region->x = self->cursor.x - self->cursor.hotspot_x;
cursor_region->y = self->cursor.y - self->cursor.hotspot_y;
cursor_region->width = self->cursor.width;
cursor_region->height = self->cursor.height;
pthread_mutex_unlock(&self->mutex);
return true;
}
bool gsr_pipewire_recording_stopped(gsr_pipewire *self) {
return self->stopped;
}
|