Skip to content

Commit 4fbc093

Browse files
committed
linux-pipewire: camera-portal: Remove duplicate sizes, frame rates
Remove duplicate resolutions and frame rates after sorting. There was already some logic in place to filter duplicate resolutions but that only considered the previous item. Also rename `sort_resolutions()` to `compare_resolutions()` since that describes its purpose better.
1 parent 79f6c1c commit 4fbc093

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

plugins/linux-pipewire/camera-portal.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,28 @@ static bool device_selected(void *data, obs_properties_t *props, obs_property_t
499499
return true;
500500
}
501501

502-
static int sort_resolutions(gconstpointer a, gconstpointer b)
502+
static void remove_duplicates_from_sorted(GArray *array, GCompareFunc cmp)
503+
{
504+
if (array->len < 2)
505+
return;
506+
507+
guint element_size = g_array_get_element_size(array);
508+
const gchar *const end = array->data + element_size * array->len;
509+
gchar *last = array->data;
510+
size_t new_size = 1;
511+
512+
for (gchar *i = array->data + element_size; i < end; i += element_size) {
513+
if (cmp(last, i) != 0) {
514+
last += element_size;
515+
memcpy(last, i, element_size);
516+
new_size += 1;
517+
}
518+
}
519+
520+
g_array_set_size(array, new_size);
521+
}
522+
523+
static int compare_resolutions(gconstpointer a, gconstpointer b)
503524
{
504525
const struct spa_rectangle *resolution_a = a;
505526
const struct spa_rectangle *resolution_b = b;
@@ -516,7 +537,6 @@ static int sort_resolutions(gconstpointer a, gconstpointer b)
516537

517538
static void resolution_list(struct camera_device *dev, uint32_t pixelformat, obs_property_t *prop)
518539
{
519-
struct spa_rectangle last_resolution = SPA_RECTANGLE(0, 0);
520540
g_autoptr(GArray) resolutions = NULL;
521541
struct param *p;
522542
obs_data_t *data;
@@ -554,14 +574,11 @@ static void resolution_list(struct camera_device *dev, uint32_t pixelformat, obs
554574
SPA_POD_OPT_Rectangle(&resolution)) < 0)
555575
continue;
556576

557-
if (resolution.width == last_resolution.width && resolution.height == last_resolution.height)
558-
continue;
559-
560-
last_resolution = resolution;
561577
g_array_append_val(resolutions, resolution);
562578
}
563579

564-
g_array_sort(resolutions, sort_resolutions);
580+
g_array_sort(resolutions, compare_resolutions);
581+
remove_duplicates_from_sorted(resolutions, compare_resolutions);
565582

566583
obs_property_list_clear(prop);
567584

@@ -699,6 +716,7 @@ static void framerate_list(struct camera_device *dev, uint32_t pixelformat, cons
699716
}
700717

701718
g_array_sort(framerates, compare_framerates);
719+
remove_duplicates_from_sorted(framerates, compare_framerates);
702720

703721
obs_property_list_clear(prop);
704722

0 commit comments

Comments
 (0)