-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
pipewire.c
1490 lines (1234 loc) · 42.3 KB
/
pipewire.c
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* pipewire.c
*
* Copyright 2020 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "pipewire.h"
#include "formats.h"
#include <util/darray.h>
#include <gio/gio.h>
#include <gio/gunixfdlist.h>
#include <fcntl.h>
#include <glad/glad.h>
#include <libdrm/drm_fourcc.h>
#include <pipewire/pipewire.h>
#include <spa/param/video/format-utils.h>
#include <spa/buffer/meta.h>
#include <spa/debug/format.h>
#include <spa/debug/types.h>
#include <spa/param/video/type-info.h>
#include <spa/utils/result.h>
//#define DEBUG_PIPEWIRE
#if !PW_CHECK_VERSION(0, 3, 62)
enum spa_meta_videotransform_value {
SPA_META_TRANSFORMATION_None = 0, /**< no transform */
SPA_META_TRANSFORMATION_90, /**< 90 degree counter-clockwise */
SPA_META_TRANSFORMATION_180, /**< 180 degree counter-clockwise */
SPA_META_TRANSFORMATION_270, /**< 270 degree counter-clockwise */
SPA_META_TRANSFORMATION_Flipped, /**< 180 degree flipped around the vertical axis. Equivalent
* to a reflexion through the vertical line splitting the
* bufffer in two equal sized parts */
SPA_META_TRANSFORMATION_Flipped90, /**< flip then rotate around 90 degree counter-clockwise */
SPA_META_TRANSFORMATION_Flipped180, /**< flip then rotate around 180 degree counter-clockwise */
SPA_META_TRANSFORMATION_Flipped270, /**< flip then rotate around 270 degree counter-clockwise */
};
#define SPA_META_VideoTransform 8
struct spa_meta_videotransform {
uint32_t transform; /**< orientation transformation that was applied to the buffer */
};
#endif
#define CURSOR_META_SIZE(width, height) \
(sizeof(struct spa_meta_cursor) + sizeof(struct spa_meta_bitmap) + \
width * height * 4)
struct obs_pw_version {
int major;
int minor;
int micro;
};
struct format_info {
uint32_t spa_format;
uint32_t drm_format;
DARRAY(uint64_t) modifiers;
};
struct _obs_pipewire {
int pipewire_fd;
struct pw_thread_loop *thread_loop;
struct pw_context *context;
struct pw_core *core;
struct spa_hook core_listener;
int sync_id;
struct obs_pw_version server_version;
struct pw_registry *registry;
struct spa_hook registry_listener;
GPtrArray *streams;
};
struct _obs_pipewire_stream {
obs_pipewire *obs_pw;
obs_source_t *source;
gs_texture_t *texture;
struct pw_stream *stream;
struct spa_hook stream_listener;
struct spa_source *reneg;
struct spa_video_info format;
enum spa_meta_videotransform_value transform;
struct {
bool valid;
int x, y;
uint32_t width, height;
} crop;
struct {
bool visible;
bool valid;
int x, y;
int hotspot_x, hotspot_y;
int width, height;
gs_texture_t *texture;
} cursor;
struct obs_video_info video_info;
bool negotiated;
DARRAY(struct format_info) format_info;
struct {
struct spa_rectangle rect;
bool set;
} resolution;
struct {
struct spa_fraction fraction;
bool set;
} framerate;
};
/* auxiliary methods */
static bool parse_pw_version(struct obs_pw_version *dst, const char *version)
{
int n_matches = sscanf(version, "%d.%d.%d", &dst->major, &dst->minor,
&dst->micro);
return n_matches == 3;
}
static bool check_pw_version(const struct obs_pw_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(obs_pipewire *obs_pw, const char *version)
{
blog(LOG_INFO, "[pipewire] Server version: %s", version);
blog(LOG_INFO, "[pipewire] Library version: %s",
pw_get_library_version());
blog(LOG_INFO, "[pipewire] Header version: %s",
pw_get_headers_version());
if (!parse_pw_version(&obs_pw->server_version, version))
blog(LOG_WARNING, "[pipewire] failed to parse server version");
}
static void teardown_pipewire(obs_pipewire *obs_pw)
{
if (obs_pw->thread_loop) {
pw_thread_loop_wait(obs_pw->thread_loop);
pw_thread_loop_stop(obs_pw->thread_loop);
}
g_clear_pointer(&obs_pw->context, pw_context_destroy);
g_clear_pointer(&obs_pw->thread_loop, pw_thread_loop_destroy);
if (obs_pw->pipewire_fd > 0) {
close(obs_pw->pipewire_fd);
obs_pw->pipewire_fd = 0;
}
}
static inline bool has_effective_crop(obs_pipewire_stream *obs_pw_stream)
{
return obs_pw_stream->crop.valid &&
(obs_pw_stream->crop.x != 0 || obs_pw_stream->crop.y != 0 ||
obs_pw_stream->crop.width <
obs_pw_stream->format.info.raw.size.width ||
obs_pw_stream->crop.height <
obs_pw_stream->format.info.raw.size.height);
}
static int get_buffer_flip(obs_pipewire_stream *obs_pw_stream)
{
int flip = 0;
switch (obs_pw_stream->transform) {
case SPA_META_TRANSFORMATION_Flipped:
case SPA_META_TRANSFORMATION_Flipped180:
flip = GS_FLIP_U;
break;
case SPA_META_TRANSFORMATION_Flipped90:
case SPA_META_TRANSFORMATION_Flipped270:
flip = GS_FLIP_V;
break;
case SPA_META_TRANSFORMATION_None:
case SPA_META_TRANSFORMATION_90:
case SPA_META_TRANSFORMATION_180:
case SPA_META_TRANSFORMATION_270:
break;
}
return flip;
}
static bool push_rotation(obs_pipewire_stream *obs_pw_stream)
{
double offset_x = 0;
double offset_y = 0;
double rotation = 0;
bool has_crop;
has_crop = has_effective_crop(obs_pw_stream);
switch (obs_pw_stream->transform) {
case SPA_META_TRANSFORMATION_Flipped:
case SPA_META_TRANSFORMATION_None:
rotation = 0;
break;
case SPA_META_TRANSFORMATION_Flipped90:
case SPA_META_TRANSFORMATION_90:
rotation = 90;
offset_x = 0;
offset_y = has_crop
? obs_pw_stream->crop.height
: obs_pw_stream->format.info.raw.size.height;
break;
case SPA_META_TRANSFORMATION_Flipped180:
case SPA_META_TRANSFORMATION_180:
rotation = 180;
offset_x = has_crop ? obs_pw_stream->crop.width
: obs_pw_stream->format.info.raw.size.width;
offset_y = has_crop
? obs_pw_stream->crop.height
: obs_pw_stream->format.info.raw.size.height;
break;
case SPA_META_TRANSFORMATION_Flipped270:
case SPA_META_TRANSFORMATION_270:
rotation = 270;
offset_x = has_crop ? obs_pw_stream->crop.width
: obs_pw_stream->format.info.raw.size.width;
offset_y = 0;
break;
}
if (rotation != 0) {
gs_matrix_push();
gs_matrix_rotaa4f(0.0f, 0.0f, 1.0f, RAD(rotation));
gs_matrix_translate3f(-offset_x, -offset_y, 0.0f);
}
return rotation != 0;
}
static const uint32_t supported_formats_async[] = {
SPA_VIDEO_FORMAT_RGBA,
SPA_VIDEO_FORMAT_YUY2,
};
#define N_SUPPORTED_FORMATS_ASYNC \
(sizeof(supported_formats_async) / sizeof(supported_formats_async[0]))
static const uint32_t supported_formats_sync[] = {
SPA_VIDEO_FORMAT_BGRA, SPA_VIDEO_FORMAT_RGBA,
SPA_VIDEO_FORMAT_BGRx, SPA_VIDEO_FORMAT_RGBx,
#if PW_CHECK_VERSION(0, 3, 41)
SPA_VIDEO_FORMAT_ABGR_210LE, SPA_VIDEO_FORMAT_xBGR_210LE,
#endif
};
#define N_SUPPORTED_FORMATS_SYNC \
(sizeof(supported_formats_sync) / sizeof(supported_formats_sync[0]))
static void swap_texture_red_blue(gs_texture_t *texture)
{
GLuint gl_texure = *(GLuint *)gs_texture_get_obj(texture);
glBindTexture(GL_TEXTURE_2D, gl_texure);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
glBindTexture(GL_TEXTURE_2D, 0);
}
static inline struct spa_pod *build_format(obs_pipewire_stream *obs_pw_stream,
struct spa_pod_builder *b,
uint32_t format, uint64_t *modifiers,
size_t modifier_count)
{
struct spa_rectangle max_resolution = SPA_RECTANGLE(8192, 4320);
struct spa_rectangle min_resolution = SPA_RECTANGLE(1, 1);
struct spa_rectangle resolution;
struct spa_pod_frame format_frame;
struct spa_fraction max_framerate;
struct spa_fraction min_framerate;
struct spa_fraction framerate;
if (obs_pw_stream->framerate.set) {
framerate = obs_pw_stream->framerate.fraction;
min_framerate = obs_pw_stream->framerate.fraction;
max_framerate = obs_pw_stream->framerate.fraction;
} else {
framerate = SPA_FRACTION(obs_pw_stream->video_info.fps_num,
obs_pw_stream->video_info.fps_den);
min_framerate = SPA_FRACTION(0, 1);
max_framerate = SPA_FRACTION(360, 1);
}
if (obs_pw_stream->resolution.set) {
resolution = obs_pw_stream->resolution.rect;
min_resolution = obs_pw_stream->resolution.rect;
max_resolution = obs_pw_stream->resolution.rect;
} else {
resolution =
SPA_RECTANGLE(obs_pw_stream->video_info.output_width,
obs_pw_stream->video_info.output_height);
min_resolution = SPA_RECTANGLE(1, 1);
max_resolution = SPA_RECTANGLE(8192, 4320);
}
/* Make an object of type SPA_TYPE_OBJECT_Format and id SPA_PARAM_EnumFormat.
* The object type is important because it defines the properties that are
* acceptable. The id gives more context about what the object is meant to
* contain. In this case we enumerate supported formats. */
spa_pod_builder_push_object(b, &format_frame, SPA_TYPE_OBJECT_Format,
SPA_PARAM_EnumFormat);
/* add media type and media subtype properties */
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);
/* formats */
spa_pod_builder_add(b, SPA_FORMAT_VIDEO_format, SPA_POD_Id(format), 0);
/* modifier */
if (modifier_count > 0) {
struct spa_pod_frame modifier_frame;
/* build an enumeration of modifiers */
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.
*/
spa_pod_builder_long(b, modifiers[0]);
/* modifiers from an array */
for (uint32_t i = 0; i < modifier_count; i++)
spa_pod_builder_long(b, modifiers[i]);
spa_pod_builder_pop(b, &modifier_frame);
}
/* add size and framerate ranges */
spa_pod_builder_add(
b, SPA_FORMAT_VIDEO_size,
SPA_POD_CHOICE_RANGE_Rectangle(&resolution, &min_resolution,
&max_resolution),
SPA_FORMAT_VIDEO_framerate,
SPA_POD_CHOICE_RANGE_Fraction(&framerate, &min_framerate,
&max_framerate),
0);
return spa_pod_builder_pop(b, &format_frame);
}
static bool build_format_params(obs_pipewire_stream *obs_pw_stream,
struct spa_pod_builder *pod_builder,
const struct spa_pod ***param_list,
uint32_t *n_params)
{
obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
uint32_t params_count = 0;
const struct spa_pod **params;
params = bzalloc(2 * obs_pw_stream->format_info.num *
sizeof(struct spa_pod *));
if (!params) {
blog(LOG_ERROR,
"[pipewire] Failed to allocate memory for param pointers");
return false;
}
if (!check_pw_version(&obs_pw->server_version, 0, 3, 33))
goto build_shm;
for (size_t i = 0; i < obs_pw_stream->format_info.num; i++) {
if (obs_pw_stream->format_info.array[i].modifiers.num == 0) {
continue;
}
params[params_count++] = build_format(
obs_pw_stream, pod_builder,
obs_pw_stream->format_info.array[i].spa_format,
obs_pw_stream->format_info.array[i].modifiers.array,
obs_pw_stream->format_info.array[i].modifiers.num);
}
build_shm:
for (size_t i = 0; i < obs_pw_stream->format_info.num; i++) {
params[params_count++] = build_format(
obs_pw_stream, pod_builder,
obs_pw_stream->format_info.array[i].spa_format, NULL,
0);
}
*param_list = params;
*n_params = params_count;
return true;
}
static bool drm_format_available(uint32_t drm_format, uint32_t *drm_formats,
size_t n_drm_formats)
{
for (size_t j = 0; j < n_drm_formats; j++) {
if (drm_format == drm_formats[j]) {
return true;
}
}
return false;
}
static void init_format_info_async(obs_pipewire_stream *obs_pw_stream)
{
da_init(obs_pw_stream->format_info);
for (size_t i = 0; i < N_SUPPORTED_FORMATS_ASYNC; i++) {
struct obs_pw_video_format obs_pw_video_format;
struct format_info *info;
if (!obs_pw_video_format_from_spa_format(
supported_formats_async[i], &obs_pw_video_format))
continue;
info = da_push_back_new(obs_pw_stream->format_info);
da_init(info->modifiers);
info->spa_format = obs_pw_video_format.spa_format;
info->drm_format = obs_pw_video_format.drm_format;
}
}
static void init_format_info_sync(obs_pipewire_stream *obs_pw_stream)
{
da_init(obs_pw_stream->format_info);
obs_enter_graphics();
enum gs_dmabuf_flags dmabuf_flags;
uint32_t *drm_formats = NULL;
size_t n_drm_formats = 0;
bool capabilities_queried = gs_query_dmabuf_capabilities(
&dmabuf_flags, &drm_formats, &n_drm_formats);
for (size_t i = 0; i < N_SUPPORTED_FORMATS_SYNC; i++) {
struct obs_pw_video_format obs_pw_video_format;
struct format_info *info;
if (!obs_pw_video_format_from_spa_format(
supported_formats_sync[i], &obs_pw_video_format) ||
obs_pw_video_format.gs_format == GS_UNKNOWN)
continue;
info = da_push_back_new(obs_pw_stream->format_info);
da_init(info->modifiers);
info->spa_format = obs_pw_video_format.spa_format;
info->drm_format = obs_pw_video_format.drm_format;
if (!capabilities_queried ||
!drm_format_available(obs_pw_video_format.drm_format,
drm_formats, n_drm_formats))
continue;
size_t n_modifiers;
uint64_t *modifiers = NULL;
if (gs_query_dmabuf_modifiers_for_format(
obs_pw_video_format.drm_format, &modifiers,
&n_modifiers)) {
da_push_back_array(info->modifiers, modifiers,
n_modifiers);
}
bfree(modifiers);
if (dmabuf_flags &
GS_DMABUF_FLAG_IMPLICIT_MODIFIERS_SUPPORTED) {
uint64_t modifier_implicit = DRM_FORMAT_MOD_INVALID;
da_push_back(info->modifiers, &modifier_implicit);
}
}
obs_leave_graphics();
bfree(drm_formats);
}
static void init_format_info(obs_pipewire_stream *obs_pw_stream)
{
uint32_t output_flags;
output_flags = obs_source_get_output_flags(obs_pw_stream->source);
if (output_flags & OBS_SOURCE_VIDEO) {
if (output_flags & OBS_SOURCE_ASYNC)
init_format_info_async(obs_pw_stream);
else
init_format_info_sync(obs_pw_stream);
}
}
static void clear_format_info(obs_pipewire_stream *obs_pw_stream)
{
for (size_t i = 0; i < obs_pw_stream->format_info.num; i++) {
da_free(obs_pw_stream->format_info.array[i].modifiers);
}
da_free(obs_pw_stream->format_info);
}
static void remove_modifier_from_format(obs_pipewire_stream *obs_pw_stream,
uint32_t spa_format, uint64_t modifier)
{
obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
for (size_t i = 0; i < obs_pw_stream->format_info.num; i++) {
if (obs_pw_stream->format_info.array[i].spa_format !=
spa_format)
continue;
if (!check_pw_version(&obs_pw->server_version, 0, 3, 40)) {
da_erase_range(
obs_pw_stream->format_info.array[i].modifiers,
0,
obs_pw_stream->format_info.array[i]
.modifiers.num -
1);
continue;
}
int idx = da_find(obs_pw_stream->format_info.array[i].modifiers,
&modifier, 0);
while (idx != -1) {
da_erase(obs_pw_stream->format_info.array[i].modifiers,
idx);
idx = da_find(
obs_pw_stream->format_info.array[i].modifiers,
&modifier, 0);
}
}
}
static void renegotiate_format(void *data, uint64_t expirations)
{
UNUSED_PARAMETER(expirations);
obs_pipewire_stream *obs_pw_stream = (obs_pipewire_stream *)data;
obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
const struct spa_pod **params = NULL;
blog(LOG_INFO, "[pipewire] Renegotiating stream");
pw_thread_loop_lock(obs_pw->thread_loop);
uint8_t params_buffer[4096];
struct spa_pod_builder pod_builder =
SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
uint32_t n_params;
if (!build_format_params(obs_pw_stream, &pod_builder, ¶ms,
&n_params)) {
teardown_pipewire(obs_pw);
pw_thread_loop_unlock(obs_pw->thread_loop);
return;
}
pw_stream_update_params(obs_pw_stream->stream, params, n_params);
pw_thread_loop_unlock(obs_pw->thread_loop);
bfree(params);
}
/* ------------------------------------------------- */
static inline struct pw_buffer *find_latest_buffer(struct pw_stream *stream)
{
struct pw_buffer *b;
/* Find the most recent buffer */
b = NULL;
while (true) {
struct pw_buffer *aux = pw_stream_dequeue_buffer(stream);
if (!aux)
break;
if (b)
pw_stream_queue_buffer(stream, b);
b = aux;
}
return b;
}
static enum video_colorspace
video_colorspace_from_spa_color_matrix(enum spa_video_color_matrix matrix)
{
switch (matrix) {
case SPA_VIDEO_COLOR_MATRIX_RGB:
return VIDEO_CS_DEFAULT;
case SPA_VIDEO_COLOR_MATRIX_BT601:
return VIDEO_CS_601;
case SPA_VIDEO_COLOR_MATRIX_BT709:
return VIDEO_CS_709;
default:
return VIDEO_CS_DEFAULT;
}
}
static enum video_range_type
video_color_range_from_spa_color_range(enum spa_video_color_range colorrange)
{
switch (colorrange) {
case SPA_VIDEO_COLOR_RANGE_0_255:
return VIDEO_RANGE_FULL;
case SPA_VIDEO_COLOR_RANGE_16_235:
return VIDEO_RANGE_PARTIAL;
default:
return VIDEO_RANGE_DEFAULT;
}
}
static bool prepare_obs_frame(obs_pipewire_stream *obs_pw_stream,
struct obs_source_frame *frame)
{
struct obs_pw_video_format obs_pw_video_format;
frame->width = obs_pw_stream->format.info.raw.size.width;
frame->height = obs_pw_stream->format.info.raw.size.height;
video_format_get_parameters(
video_colorspace_from_spa_color_matrix(
obs_pw_stream->format.info.raw.color_matrix),
video_color_range_from_spa_color_range(
obs_pw_stream->format.info.raw.color_range),
frame->color_matrix, frame->color_range_min,
frame->color_range_max);
if (!obs_pw_video_format_from_spa_format(
obs_pw_stream->format.info.raw.format,
&obs_pw_video_format) ||
obs_pw_video_format.video_format == VIDEO_FORMAT_NONE)
return false;
frame->format = obs_pw_video_format.video_format;
frame->linesize[0] =
SPA_ROUND_UP_N(frame->width * obs_pw_video_format.bpp, 4);
return true;
}
static void process_video_async(obs_pipewire_stream *obs_pw_stream)
{
struct spa_buffer *buffer;
struct pw_buffer *b;
bool has_buffer;
b = find_latest_buffer(obs_pw_stream->stream);
if (!b) {
blog(LOG_DEBUG, "[pipewire] Out of buffers!");
return;
}
buffer = b->buffer;
has_buffer = buffer->datas[0].chunk->size != 0;
if (!has_buffer)
goto done;
#ifdef DEBUG_PIPEWIRE
blog(LOG_DEBUG, "[pipewire] Buffer has memory texture");
#endif
struct obs_source_frame out = {0};
if (!prepare_obs_frame(obs_pw_stream, &out)) {
blog(LOG_ERROR, "[pipewire] Couldn't prepare frame");
goto done;
}
for (uint32_t i = 0; i < buffer->n_datas && i < MAX_AV_PLANES; i++) {
out.data[i] = buffer->datas[i].data;
if (out.data[i] == NULL) {
blog(LOG_ERROR, "[pipewire] Failed to access data");
goto done;
}
}
#ifdef DEBUG_PIPEWIRE
blog(LOG_DEBUG, "[pipewire] Frame info: Format: %s, Planes: %u",
get_video_format_name(out.format), buffer->n_datas);
for (uint32_t i = 0; i < buffer->n_datas && i < MAX_AV_PLANES; i++) {
blog(LOG_DEBUG, "[pipewire] Plane %u: Dataptr:%p, Linesize:%d",
i, out.data[i], out.linesize[i]);
}
#endif
obs_source_output_video(obs_pw_stream->source, &out);
done:
pw_stream_queue_buffer(obs_pw_stream->stream, b);
}
static void process_video_sync(obs_pipewire_stream *obs_pw_stream)
{
obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
struct spa_meta_cursor *cursor;
struct spa_meta_header *header;
struct spa_meta_region *region;
struct spa_meta_videotransform *video_transform;
struct obs_pw_video_format obs_pw_video_format;
struct spa_buffer *buffer;
struct pw_buffer *b;
bool has_buffer = true;
b = find_latest_buffer(obs_pw_stream->stream);
if (!b) {
blog(LOG_DEBUG, "[pipewire] Out of buffers!");
return;
}
buffer = b->buffer;
header = spa_buffer_find_meta_data(buffer, SPA_META_Header,
sizeof(*header));
if (header && (header->flags & SPA_META_HEADER_FLAG_CORRUPTED) > 0) {
blog(LOG_ERROR, "[pipewire] buffer is corrupt");
pw_stream_queue_buffer(obs_pw_stream->stream, b);
return;
}
obs_enter_graphics();
// Workaround for kwin behaviour pre 5.27.5
// Workaround for mutter behaviour pre GNOME 43
// Only check this if !SPA_META_Header, once supported platforms update.
has_buffer = buffer->datas[0].chunk->size != 0;
if (!has_buffer)
goto read_metadata;
if (buffer->datas[0].type == SPA_DATA_DmaBuf) {
uint32_t planes = buffer->n_datas;
uint32_t *offsets = alloca(sizeof(uint32_t) * planes);
uint32_t *strides = alloca(sizeof(uint32_t) * planes);
uint64_t *modifiers = alloca(sizeof(uint64_t) * planes);
int *fds = alloca(sizeof(int) * planes);
bool use_modifiers;
bool corrupt = false;
#ifdef DEBUG_PIPEWIRE
blog(LOG_DEBUG,
"[pipewire] DMA-BUF info: fd:%ld, stride:%d, offset:%u, size:%dx%d",
buffer->datas[0].fd, buffer->datas[0].chunk->stride,
buffer->datas[0].chunk->offset,
obs_pw_stream->format.info.raw.size.width,
obs_pw_stream->format.info.raw.size.height);
#endif
if (!obs_pw_video_format_from_spa_format(
obs_pw_stream->format.info.raw.format,
&obs_pw_video_format) ||
obs_pw_video_format.gs_format == GS_UNKNOWN) {
blog(LOG_ERROR,
"[pipewire] unsupported DMA buffer format: %d",
obs_pw_stream->format.info.raw.format);
goto read_metadata;
}
for (uint32_t plane = 0; plane < planes; plane++) {
fds[plane] = buffer->datas[plane].fd;
offsets[plane] = buffer->datas[plane].chunk->offset;
strides[plane] = buffer->datas[plane].chunk->stride;
modifiers[plane] =
obs_pw_stream->format.info.raw.modifier;
corrupt |= (buffer->datas[plane].chunk->flags &
SPA_CHUNK_FLAG_CORRUPTED) > 0;
}
if (corrupt) {
blog(LOG_DEBUG,
"[pipewire] buffer contains corrupted data");
goto read_metadata;
}
g_clear_pointer(&obs_pw_stream->texture, gs_texture_destroy);
use_modifiers = obs_pw_stream->format.info.raw.modifier !=
DRM_FORMAT_MOD_INVALID;
obs_pw_stream->texture = gs_texture_create_from_dmabuf(
obs_pw_stream->format.info.raw.size.width,
obs_pw_stream->format.info.raw.size.height,
obs_pw_video_format.drm_format, GS_BGRX, planes, fds,
strides, offsets, use_modifiers ? modifiers : NULL);
if (obs_pw_stream->texture == NULL) {
remove_modifier_from_format(
obs_pw_stream,
obs_pw_stream->format.info.raw.format,
obs_pw_stream->format.info.raw.modifier);
pw_loop_signal_event(
pw_thread_loop_get_loop(obs_pw->thread_loop),
obs_pw_stream->reneg);
goto read_metadata;
}
} else {
blog(LOG_DEBUG, "[pipewire] Buffer has memory texture");
if (!obs_pw_video_format_from_spa_format(
obs_pw_stream->format.info.raw.format,
&obs_pw_video_format) ||
obs_pw_video_format.gs_format == GS_UNKNOWN) {
blog(LOG_ERROR,
"[pipewire] unsupported buffer format: %d",
obs_pw_stream->format.info.raw.format);
goto read_metadata;
}
if ((buffer->datas[0].chunk->flags & SPA_CHUNK_FLAG_CORRUPTED) >
0) {
blog(LOG_DEBUG,
"[pipewire] buffer contains corrupted data");
goto read_metadata;
}
if (buffer->datas[0].chunk->size == 0) {
blog(LOG_DEBUG,
"[pipewire] buffer contains empty data");
goto read_metadata;
}
g_clear_pointer(&obs_pw_stream->texture, gs_texture_destroy);
obs_pw_stream->texture = gs_texture_create(
obs_pw_stream->format.info.raw.size.width,
obs_pw_stream->format.info.raw.size.height,
obs_pw_video_format.gs_format, 1,
(const uint8_t **)&buffer->datas[0].data, GS_DYNAMIC);
}
if (obs_pw_video_format.swap_red_blue)
swap_texture_red_blue(obs_pw_stream->texture);
/* Video Crop */
region = spa_buffer_find_meta_data(buffer, SPA_META_VideoCrop,
sizeof(*region));
if (region && spa_meta_region_is_valid(region)) {
#ifdef DEBUG_PIPEWIRE
blog(LOG_DEBUG,
"[pipewire] Crop Region available (%dx%d+%d+%d)",
region->region.position.x, region->region.position.y,
region->region.size.width, region->region.size.height);
#endif
obs_pw_stream->crop.x = region->region.position.x;
obs_pw_stream->crop.y = region->region.position.y;
obs_pw_stream->crop.width = region->region.size.width;
obs_pw_stream->crop.height = region->region.size.height;
obs_pw_stream->crop.valid = true;
} else {
obs_pw_stream->crop.valid = false;
}
/* Video Transform */
video_transform = spa_buffer_find_meta_data(
buffer, SPA_META_VideoTransform, sizeof(*video_transform));
if (video_transform)
obs_pw_stream->transform = video_transform->transform;
else
obs_pw_stream->transform = SPA_META_TRANSFORMATION_None;
read_metadata:
/* Cursor */
cursor = spa_buffer_find_meta_data(buffer, SPA_META_Cursor,
sizeof(*cursor));
obs_pw_stream->cursor.valid = cursor &&
spa_meta_cursor_is_valid(cursor);
if (obs_pw_stream->cursor.visible && obs_pw_stream->cursor.valid) {
struct spa_meta_bitmap *bitmap = NULL;
if (cursor->bitmap_offset)
bitmap = SPA_MEMBER(cursor, cursor->bitmap_offset,
struct spa_meta_bitmap);
if (bitmap)
g_clear_pointer(&obs_pw_stream->cursor.texture,
gs_texture_destroy);
if (bitmap && bitmap->size.width > 0 &&
bitmap->size.height > 0 &&
obs_pw_video_format_from_spa_format(bitmap->format,
&obs_pw_video_format) &&
obs_pw_video_format.gs_format != GS_UNKNOWN) {
const uint8_t *bitmap_data;
bitmap_data =
SPA_MEMBER(bitmap, bitmap->offset, uint8_t);
obs_pw_stream->cursor.hotspot_x = cursor->hotspot.x;
obs_pw_stream->cursor.hotspot_y = cursor->hotspot.y;
obs_pw_stream->cursor.width = bitmap->size.width;
obs_pw_stream->cursor.height = bitmap->size.height;
assert(obs_pw_stream->cursor.texture == NULL);
obs_pw_stream->cursor.texture =
gs_texture_create(obs_pw_stream->cursor.width,
obs_pw_stream->cursor.height,
obs_pw_video_format.gs_format,
1, &bitmap_data, GS_DYNAMIC);
if (obs_pw_video_format.swap_red_blue)
swap_texture_red_blue(
obs_pw_stream->cursor.texture);
}
obs_pw_stream->cursor.x = cursor->position.x;
obs_pw_stream->cursor.y = cursor->position.y;
}
pw_stream_queue_buffer(obs_pw_stream->stream, b);
obs_leave_graphics();
}
static void on_process_cb(void *user_data)
{
obs_pipewire_stream *obs_pw_stream = user_data;
uint32_t output_flags;
output_flags = obs_source_get_output_flags(obs_pw_stream->source);
if (output_flags & OBS_SOURCE_VIDEO) {
if (output_flags & OBS_SOURCE_ASYNC)
process_video_async(obs_pw_stream);
else
process_video_sync(obs_pw_stream);
}
}
static void on_param_changed_cb(void *user_data, uint32_t id,
const struct spa_pod *param)
{
obs_pipewire_stream *obs_pw_stream = user_data;
obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
struct spa_pod_builder pod_builder;
const struct spa_pod *params[5];
const char *format_name;
uint32_t n_params = 0;
uint32_t buffer_types;
uint32_t output_flags;
uint8_t params_buffer[1024];
int result;
if (!param || id != SPA_PARAM_Format)
return;
result = spa_format_parse(param, &obs_pw_stream->format.media_type,
&obs_pw_stream->format.media_subtype);
if (result < 0)
return;
if (obs_pw_stream->format.media_type != SPA_MEDIA_TYPE_video ||
obs_pw_stream->format.media_subtype != SPA_MEDIA_SUBTYPE_raw)
return;
spa_format_video_raw_parse(param, &obs_pw_stream->format.info.raw);
output_flags = obs_source_get_output_flags(obs_pw_stream->source);
buffer_types = 1 << SPA_DATA_MemPtr;
bool has_modifier =
spa_pod_find_prop(param, NULL, SPA_FORMAT_VIDEO_modifier) !=
NULL;
if ((has_modifier ||
check_pw_version(&obs_pw->server_version, 0, 3, 24)) &&
(output_flags & OBS_SOURCE_ASYNC_VIDEO) != OBS_SOURCE_ASYNC_VIDEO)
buffer_types |= 1 << SPA_DATA_DmaBuf;
blog(LOG_INFO, "[pipewire] Negotiated format:");
format_name = spa_debug_type_find_name(
spa_type_video_format, obs_pw_stream->format.info.raw.format);
blog(LOG_INFO, "[pipewire] Format: %d (%s)",
obs_pw_stream->format.info.raw.format,
format_name ? format_name : "unknown format");
if (has_modifier) {