Skip to content
Snippets Groups Projects
Commit e8e6c1d8 authored by George Kiagiadakis's avatar George Kiagiadakis
Browse files

algorithms: allow specifying a preferred number of channels in the format choosing algorithm

parent 9e2aa612
No related branches found
No related tags found
No related merge requests found
...@@ -284,7 +284,7 @@ select_rate (WpSpaPod *value) ...@@ -284,7 +284,7 @@ select_rate (WpSpaPod *value)
} }
static gint static gint
select_channels (WpSpaPod *value) select_channels (WpSpaPod *value, gint preference)
{ {
gint ret = 0; gint ret = 0;
...@@ -307,10 +307,13 @@ select_channels (WpSpaPod *value) ...@@ -307,10 +307,13 @@ select_channels (WpSpaPod *value)
/* choose the most channels */ /* choose the most channels */
g_autoptr (WpIterator) it = wp_spa_pod_iterator_new (value); g_autoptr (WpIterator) it = wp_spa_pod_iterator_new (value);
GValue next = G_VALUE_INIT; GValue next = G_VALUE_INIT;
gint diff = SPA_AUDIO_MAX_CHANNELS;
while (wp_iterator_next (it, &next)) { while (wp_iterator_next (it, &next)) {
gint *channel = (gint *) g_value_get_pointer (&next); gint *channel = (gint *) g_value_get_pointer (&next);
if (*channel > ret) if (abs (*channel - preference) < diff) {
diff = abs (*channel - preference);
ret = *channel; ret = *channel;
}
g_value_unset (&next); g_value_unset (&next);
} }
} }
...@@ -329,8 +332,8 @@ select_channels (WpSpaPod *value) ...@@ -329,8 +332,8 @@ select_channels (WpSpaPod *value)
g_value_unset (&next); g_value_unset (&next);
i++; i++;
} }
ret = SPA_MAX (vals[1], vals[2]); ret = SPA_MAX (vals[1], preference);
ret = SPA_MIN (ret, SPA_AUDIO_MAX_CHANNELS); ret = SPA_MIN (ret, vals[2]);
} }
return ret; return ret;
...@@ -338,7 +341,7 @@ select_channels (WpSpaPod *value) ...@@ -338,7 +341,7 @@ select_channels (WpSpaPod *value)
gboolean gboolean
choose_sensible_raw_audio_format (GPtrArray *formats, choose_sensible_raw_audio_format (GPtrArray *formats,
struct spa_audio_info_raw *result) gint channels_preference, struct spa_audio_info_raw *result)
{ {
guint i, most_channels = 0; guint i, most_channels = 0;
struct spa_audio_info_raw *raw; struct spa_audio_info_raw *raw;
...@@ -392,7 +395,7 @@ choose_sensible_raw_audio_format (GPtrArray *formats, ...@@ -392,7 +395,7 @@ choose_sensible_raw_audio_format (GPtrArray *formats,
/* channels */ /* channels */
else if (g_strcmp0 (key, "channels") == 0) { else if (g_strcmp0 (key, "channels") == 0) {
raw[i].channels = select_channels (value); raw[i].channels = select_channels (value, channels_preference);
} }
/* position */ /* position */
......
...@@ -12,4 +12,4 @@ gboolean multiport_link_create (GVariant * src_data, GVariant * sink_data, ...@@ -12,4 +12,4 @@ gboolean multiport_link_create (GVariant * src_data, GVariant * sink_data,
struct spa_audio_info_raw; struct spa_audio_info_raw;
gboolean choose_sensible_raw_audio_format (GPtrArray *formats, gboolean choose_sensible_raw_audio_format (GPtrArray *formats,
struct spa_audio_info_raw *result); gint channels_preference, struct spa_audio_info_raw *result);
...@@ -87,7 +87,7 @@ on_proxy_enum_format_done (WpProxy *proxy, GAsyncResult *res, ...@@ -87,7 +87,7 @@ on_proxy_enum_format_done (WpProxy *proxy, GAsyncResult *res,
return; return;
} }
if (!choose_sensible_raw_audio_format (formats, &self->format)) { if (!choose_sensible_raw_audio_format (formats, 34, &self->format)) {
uint32_t media_type, media_subtype; uint32_t media_type, media_subtype;
g_warning ("WpAudioAdapter:%p failed to choose a sensible audio format", g_warning ("WpAudioAdapter:%p failed to choose a sensible audio format",
......
...@@ -207,7 +207,7 @@ on_node_enum_format_done (WpProxy *proxy, GAsyncResult *res, ...@@ -207,7 +207,7 @@ on_node_enum_format_done (WpProxy *proxy, GAsyncResult *res,
return; return;
} }
if (!choose_sensible_raw_audio_format (formats, &self->format)) { if (!choose_sensible_raw_audio_format (formats, 34, &self->format)) {
wp_warning_object (self, "failed to choose a sensible audio format"); wp_warning_object (self, "failed to choose a sensible audio format");
wp_transition_return_error (transition, wp_transition_return_error (transition,
g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED, g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
......
...@@ -28,68 +28,92 @@ test_choose_sensible_raw_audio_format (void) ...@@ -28,68 +28,92 @@ test_choose_sensible_raw_audio_format (void)
g_autoptr (GPtrArray) formats = g_autoptr (GPtrArray) formats =
g_ptr_array_new_with_free_func ((GDestroyNotify) wp_spa_pod_unref); g_ptr_array_new_with_free_func ((GDestroyNotify) wp_spa_pod_unref);
/* test 1 */ {
g_ptr_array_remove_range (formats, 0, formats->len); g_ptr_array_remove_range (formats, 0, formats->len);
g_autoptr (WpSpaPod) param1 = wp_spa_pod_new_object ( g_autoptr (WpSpaPod) param1 = wp_spa_pod_new_object (
"Format", "Format", "Format", "Format",
"mediaType", SPA_POD_Id(SPA_MEDIA_TYPE_audio), "mediaType", SPA_POD_Id(SPA_MEDIA_TYPE_audio),
"mediaSubtype", SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw), "mediaSubtype", SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
"format", SPA_POD_CHOICE_ENUM_Id(3, "format", SPA_POD_CHOICE_ENUM_Id(3,
SPA_AUDIO_FORMAT_F32_OE, SPA_AUDIO_FORMAT_F32_OE,
SPA_AUDIO_FORMAT_S16, SPA_AUDIO_FORMAT_S16,
SPA_AUDIO_FORMAT_S20), SPA_AUDIO_FORMAT_S20),
"rate", SPA_POD_CHOICE_RANGE_Int(22000, 44100, 8000), "rate", SPA_POD_CHOICE_RANGE_Int(22000, 44100, 8000),
"channels", SPA_POD_CHOICE_RANGE_Int(2, 1, 8), "channels", SPA_POD_CHOICE_RANGE_Int(2, 1, 8),
NULL); NULL);
g_assert_nonnull (param1); g_assert_nonnull (param1);
g_ptr_array_add (formats, g_steal_pointer (&param1)); g_ptr_array_add (formats, g_steal_pointer (&param1));
g_assert_true (choose_sensible_raw_audio_format (formats, &info)); g_assert_true (choose_sensible_raw_audio_format (formats, 34, &info));
g_assert_cmpint (info.format, ==, SPA_AUDIO_FORMAT_S16); g_assert_cmpint (info.format, ==, SPA_AUDIO_FORMAT_S16);
g_assert_cmpint (info.rate, ==, 44100); g_assert_cmpint (info.rate, ==, 44100);
g_assert_cmpint (info.channels, ==, 8); g_assert_cmpint (info.channels, ==, 8);
g_assert_cmpint (info.flags, ==, SPA_AUDIO_FLAG_UNPOSITIONED); g_assert_cmpint (info.flags, ==, SPA_AUDIO_FLAG_UNPOSITIONED);
}
/* test 2 */ {
g_ptr_array_remove_range (formats, 0, formats->len); g_ptr_array_remove_range (formats, 0, formats->len);
g_autoptr (WpSpaPod) param2 = wp_spa_pod_new_object ( g_autoptr (WpSpaPod) param1 = wp_spa_pod_new_object (
"Format", "Format", "Format", "Format",
"mediaType", SPA_POD_Id(SPA_MEDIA_TYPE_audio), "mediaType", SPA_POD_Id(SPA_MEDIA_TYPE_audio),
"mediaSubtype", SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw), "mediaSubtype", SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
"format", SPA_POD_CHOICE_ENUM_Id(3, "format", SPA_POD_CHOICE_ENUM_Id(3,
SPA_AUDIO_FORMAT_S32, SPA_AUDIO_FORMAT_F32_OE,
SPA_AUDIO_FORMAT_U8, SPA_AUDIO_FORMAT_S16,
SPA_AUDIO_FORMAT_F32), SPA_AUDIO_FORMAT_S20),
"rate", SPA_POD_CHOICE_RANGE_Int(56000, 44100, 96000), "rate", SPA_POD_CHOICE_RANGE_Int(22000, 44100, 8000),
"channels", SPA_POD_Int(2), "channels", SPA_POD_CHOICE_RANGE_Int(2, 1, 8),
"position", SPA_POD_Array(sizeof(uint32_t), SPA_TYPE_Id, 2, layout), NULL);
NULL); g_assert_nonnull (param1);
g_assert_nonnull (param2); g_ptr_array_add (formats, g_steal_pointer (&param1));
g_ptr_array_add (formats, g_steal_pointer (&param2)); g_assert_true (choose_sensible_raw_audio_format (formats, 2, &info));
g_autoptr (WpSpaPod) param3 = wp_spa_pod_new_object ( g_assert_cmpint (info.format, ==, SPA_AUDIO_FORMAT_S16);
"Format", "Format", g_assert_cmpint (info.rate, ==, 44100);
"mediaType", SPA_POD_Id(SPA_MEDIA_TYPE_audio), g_assert_cmpint (info.channels, ==, 2);
"mediaSubtype", SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw), g_assert_cmpint (info.flags, ==, SPA_AUDIO_FLAG_UNPOSITIONED);
"format", SPA_POD_CHOICE_ENUM_Id(3, }
SPA_AUDIO_FORMAT_S32,
SPA_AUDIO_FORMAT_U8, {
SPA_AUDIO_FORMAT_F32), g_ptr_array_remove_range (formats, 0, formats->len);
"rate", SPA_POD_CHOICE_RANGE_Int(56000, 44100, 96000), g_autoptr (WpSpaPod) param2 = wp_spa_pod_new_object (
"channels", SPA_POD_Int(5), "Format", "Format",
"position", SPA_POD_Array(sizeof(uint32_t), SPA_TYPE_Id, 5, layout), "mediaType", SPA_POD_Id(SPA_MEDIA_TYPE_audio),
NULL); "mediaSubtype", SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
g_assert_nonnull (param3); "format", SPA_POD_CHOICE_ENUM_Id(3,
g_ptr_array_add (formats, g_steal_pointer (&param3)); SPA_AUDIO_FORMAT_S32,
g_assert_true (choose_sensible_raw_audio_format (formats, &info)); SPA_AUDIO_FORMAT_U8,
g_assert_cmpint (info.format, ==, SPA_AUDIO_FORMAT_F32); SPA_AUDIO_FORMAT_F32),
g_assert_cmpint (info.rate, ==, 48000); "rate", SPA_POD_CHOICE_RANGE_Int(56000, 44100, 96000),
g_assert_cmpint (info.channels, ==, 5); "channels", SPA_POD_Int(2),
g_assert_cmpint (info.flags, ==, SPA_AUDIO_FLAG_NONE); "position", SPA_POD_Array(sizeof(uint32_t), SPA_TYPE_Id, 2, layout),
g_assert_cmpint (info.position[0], ==, layout[0]); NULL);
g_assert_cmpint (info.position[1], ==, layout[1]); g_assert_nonnull (param2);
g_assert_cmpint (info.position[2], ==, layout[2]); g_ptr_array_add (formats, g_steal_pointer (&param2));
g_assert_cmpint (info.position[3], ==, layout[3]); g_autoptr (WpSpaPod) param3 = wp_spa_pod_new_object (
g_assert_cmpint (info.position[4], ==, layout[4]); "Format", "Format",
g_assert_cmpint (info.position[5], ==, 0); "mediaType", SPA_POD_Id(SPA_MEDIA_TYPE_audio),
"mediaSubtype", SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
"format", SPA_POD_CHOICE_ENUM_Id(3,
SPA_AUDIO_FORMAT_S32,
SPA_AUDIO_FORMAT_U8,
SPA_AUDIO_FORMAT_F32),
"rate", SPA_POD_CHOICE_RANGE_Int(56000, 44100, 96000),
"channels", SPA_POD_Int(5),
"position", SPA_POD_Array(sizeof(uint32_t), SPA_TYPE_Id, 5, layout),
NULL);
g_assert_nonnull (param3);
g_ptr_array_add (formats, g_steal_pointer (&param3));
g_assert_true (choose_sensible_raw_audio_format (formats, 34, &info));
g_assert_cmpint (info.format, ==, SPA_AUDIO_FORMAT_F32);
g_assert_cmpint (info.rate, ==, 48000);
g_assert_cmpint (info.channels, ==, 5);
g_assert_cmpint (info.flags, ==, SPA_AUDIO_FLAG_NONE);
g_assert_cmpint (info.position[0], ==, layout[0]);
g_assert_cmpint (info.position[1], ==, layout[1]);
g_assert_cmpint (info.position[2], ==, layout[2]);
g_assert_cmpint (info.position[3], ==, layout[3]);
g_assert_cmpint (info.position[4], ==, layout[4]);
g_assert_cmpint (info.position[5], ==, 0);
}
wp_spa_type_deinit (); wp_spa_type_deinit ();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment