format: make helper function to parse media type

This commit is contained in:
Wim Taymans 2018-08-29 14:50:58 +02:00
parent 5976beb149
commit 80de83f38a
24 changed files with 127 additions and 100 deletions

View file

@ -342,14 +342,7 @@ impl_node_port_enum_params(struct spa_node *node,
if (*index > 0)
return 0;
param = spa_pod_builder_object(&b,
SPA_TYPE_OBJECT_Format, id,
"I", SPA_MEDIA_TYPE_audio,
"I", SPA_MEDIA_SUBTYPE_raw,
":", SPA_FORMAT_AUDIO_format, "I", this->current_format.info.raw.format,
":", SPA_FORMAT_AUDIO_layout, "I", this->current_format.info.raw.layout,
":", SPA_FORMAT_AUDIO_rate, "i", this->current_format.info.raw.rate,
":", SPA_FORMAT_AUDIO_channels, "i", this->current_format.info.raw.channels);
param = spa_format_audio_raw_build(&b, id, &this->current_format.info.raw);
break;
case SPA_PARAM_Buffers:
@ -450,9 +443,7 @@ static int port_set_format(struct spa_node *node,
} else {
struct spa_audio_info info = { 0 };
if ((err = spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype)) < 0)
if ((err = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return err;
if (info.media_type != SPA_MEDIA_TYPE_audio ||

View file

@ -305,31 +305,6 @@ static void recycle_buffer(struct state *this, uint32_t buffer_id)
}
}
static int port_get_format(struct spa_node *node,
enum spa_direction direction, uint32_t port_id,
uint32_t *index,
struct spa_pod **param,
struct spa_pod_builder *builder)
{
struct state *this = SPA_CONTAINER_OF(node, struct state, node);
if (!this->have_format)
return -EIO;
if (*index > 0)
return 0;
*param = spa_pod_builder_object(builder,
SPA_TYPE_OBJECT_Format, SPA_PARAM_Format,
"I", SPA_MEDIA_TYPE_audio,
"I", SPA_MEDIA_SUBTYPE_raw,
":", SPA_FORMAT_AUDIO_format, "I", this->current_format.info.raw.format,
":", SPA_FORMAT_AUDIO_layout, "I", this->current_format.info.raw.layout,
":", SPA_FORMAT_AUDIO_rate, "i", this->current_format.info.raw.rate,
":", SPA_FORMAT_AUDIO_channels, "i", this->current_format.info.raw.channels);
return 1;
}
static int
impl_node_port_enum_params(struct spa_node *node,
enum spa_direction direction, uint32_t port_id,
@ -342,7 +317,6 @@ impl_node_port_enum_params(struct spa_node *node,
struct spa_pod *param;
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(index != NULL, -EINVAL);
@ -374,8 +348,12 @@ impl_node_port_enum_params(struct spa_node *node,
return spa_alsa_enum_format(this, index, filter, result, builder);
case SPA_PARAM_Format:
if ((res = port_get_format(node, direction, port_id, index, &param, &b)) <= 0)
return res;
if (!this->have_format)
return -EIO;
if (*index > 0)
return 0;
param = spa_format_audio_raw_build(&b, id, &this->current_format.info.raw);
break;
case SPA_PARAM_Buffers:
@ -469,9 +447,8 @@ static int port_set_format(struct spa_node *node,
} else {
struct spa_audio_info info = { 0 };
spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype);
if ((err = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return err;
if (info.media_type != SPA_MEDIA_TYPE_audio ||
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)

View file

@ -522,9 +522,8 @@ static int port_set_format(struct spa_node *node,
} else {
struct spa_audio_info info = { 0 };
spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype);
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if (info.media_type != SPA_MEDIA_TYPE_audio ||
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)

View file

@ -676,9 +676,8 @@ static int port_set_format(struct spa_node *node,
} else {
struct spa_audio_info info = { 0 };
spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype);
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if (info.media_type != SPA_MEDIA_TYPE_audio ||
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)

View file

@ -587,6 +587,7 @@ static int port_set_format(struct spa_node *node,
{
struct impl *this = SPA_CONTAINER_OF(node, struct impl, node);
struct port *port;
int res;
port = GET_PORT(this, direction, port_id);
@ -602,9 +603,8 @@ static int port_set_format(struct spa_node *node,
} else {
struct spa_audio_info info = { 0 };
spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype);
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if (info.media_type != SPA_MEDIA_TYPE_audio ||
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)

View file

@ -493,9 +493,8 @@ static int port_set_format(struct spa_node *node,
} else {
struct spa_audio_info info = { 0 };
spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype);
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if (info.media_type != SPA_MEDIA_TYPE_audio ||
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)

View file

@ -589,6 +589,7 @@ static int port_set_format(struct spa_node *node,
{
struct impl *this = SPA_CONTAINER_OF(node, struct impl, node);
struct port *port;
int res;
port = GET_PORT(this, direction, port_id);
@ -604,9 +605,8 @@ static int port_set_format(struct spa_node *node,
} else {
struct spa_audio_info info = { 0 };
spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype);
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if (info.media_type != SPA_MEDIA_TYPE_audio ||
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)

View file

@ -515,6 +515,7 @@ static int port_set_format(struct spa_node *node,
{
struct impl *this = SPA_CONTAINER_OF(node, struct impl, node);
struct port *port;
int res;
port = GET_PORT(this, direction, port_id);
@ -528,9 +529,8 @@ static int port_set_format(struct spa_node *node,
} else {
struct spa_audio_info info = { 0 };
spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype);
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if (info.media_type != SPA_MEDIA_TYPE_audio ||
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)

View file

@ -705,6 +705,8 @@ port_set_format(struct impl *this,
uint32_t flags,
const struct spa_pod *format)
{
int res;
if (format == NULL) {
this->have_format = false;
clear_buffers(this);
@ -713,9 +715,8 @@ port_set_format(struct impl *this,
int idx;
int sizes[4] = { 2, 4, 4, 8 };
spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype);
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if (info.media_type != SPA_MEDIA_TYPE_audio ||
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)
@ -724,16 +725,22 @@ port_set_format(struct impl *this,
if (spa_format_audio_raw_parse(format, &info.info.raw) < 0)
return -EINVAL;
if (info.info.raw.format == SPA_AUDIO_FORMAT_S16)
switch (info.info.raw.format) {
case SPA_AUDIO_FORMAT_S16:
idx = 0;
else if (info.info.raw.format == SPA_AUDIO_FORMAT_S32)
break;
case SPA_AUDIO_FORMAT_S32:
idx = 1;
else if (info.info.raw.format == SPA_AUDIO_FORMAT_F32)
break;
case SPA_AUDIO_FORMAT_F32:
idx = 2;
else if (info.info.raw.format == SPA_AUDIO_FORMAT_F64)
break;
case SPA_AUDIO_FORMAT_F64:
idx = 3;
else
break;
default:
return -EINVAL;
}
this->bpf = sizes[idx] * info.info.raw.channels;
this->current_format = info;

View file

@ -1066,9 +1066,7 @@ static int port_set_format(struct spa_node *node,
} else {
struct spa_audio_info info = { 0 };
if ((err = spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype)) < 0)
if ((err = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return err;
if (info.media_type != SPA_MEDIA_TYPE_audio ||

View file

@ -310,6 +310,7 @@ static int port_set_format(struct spa_node *node,
{
struct impl *this;
struct port *port;
int res;
if (node == NULL || format == NULL)
return -EINVAL;
@ -327,9 +328,8 @@ static int port_set_format(struct spa_node *node,
} else {
struct spa_video_info info = { 0 };
spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype);
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if (info.media_type != SPA_MEDIA_TYPE_video &&
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)

View file

@ -298,6 +298,7 @@ static int port_set_format(struct spa_node *node,
{
struct impl *this = SPA_CONTAINER_OF(node, struct impl, node);
struct port *port;
int res;
port = GET_PORT(this, direction, port_id);
@ -307,9 +308,8 @@ static int port_set_format(struct spa_node *node,
} else {
struct spa_video_info info = { 0 };
spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype);
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if (info.media_type != SPA_MEDIA_TYPE_video &&
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)

View file

@ -552,6 +552,7 @@ static int port_set_format(struct spa_node *node,
struct impl *this = SPA_CONTAINER_OF(node, struct impl, node);
struct spa_video_info info;
struct port *port = GET_PORT(this, direction, port_id);
int res;
if (format == NULL) {
spa_v4l2_stream_off(this);
@ -560,9 +561,8 @@ static int port_set_format(struct spa_node *node,
spa_v4l2_close(this);
return 0;
} else {
spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype);
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if (info.media_type != SPA_MEDIA_TYPE_video) {
spa_log_error(this->log, "media type must be video");

View file

@ -544,9 +544,8 @@ spa_v4l2_enum_format(struct impl *this,
}
if (filter) {
spa_pod_object_parse(filter,
"I", &filter_media_type,
"I", &filter_media_subtype);
if ((res = spa_format_parse(filter, &filter_media_type, &filter_media_subtype)) < 0)
return res;
}
if (false) {

View file

@ -622,6 +622,7 @@ static int port_set_format(struct spa_node *node,
const struct spa_pod *format)
{
struct impl *this = SPA_CONTAINER_OF(node, struct impl, node);
int res;
if (format == NULL) {
this->have_format = false;
@ -629,9 +630,8 @@ static int port_set_format(struct spa_node *node,
} else {
struct spa_video_info info = { 0 };
spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype);
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if (info.media_type != SPA_MEDIA_TYPE_video &&
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)

View file

@ -499,6 +499,7 @@ static int port_set_format(struct spa_node *node,
{
struct impl *this = SPA_CONTAINER_OF(node, struct impl, node);
struct port *port;
int res;
port = GET_PORT(this, direction, port_id);
@ -508,9 +509,8 @@ static int port_set_format(struct spa_node *node,
} else {
struct spa_audio_info info = { 0 };
spa_pod_object_parse(format,
"I", &info.media_type,
"I", &info.media_subtype);
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
return res;
if (info.media_type != SPA_MEDIA_TYPE_audio ||
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)