stream: Simplify passing of formats in extended API

Passing a NULL-terminated array of pa_format_info pointers is a bit
unwieldy for clients. Instead of this, let's pass in an array of
pointers and the number of elements in the array.
This commit is contained in:
Arun Raghavan 2011-06-18 15:36:18 -07:00
parent 573b9d6ca8
commit 257bdcafe3
3 changed files with 14 additions and 11 deletions

View file

@ -86,14 +86,16 @@ static pa_stream *pa_stream_new_with_proplist_internal(
const pa_sample_spec *ss, const pa_sample_spec *ss,
const pa_channel_map *map, const pa_channel_map *map,
pa_format_info * const *formats, pa_format_info * const *formats,
unsigned int n_formats,
pa_proplist *p) { pa_proplist *p) {
pa_stream *s; pa_stream *s;
int i; unsigned int i;
pa_assert(c); pa_assert(c);
pa_assert(PA_REFCNT_VALUE(c) >= 1); pa_assert(PA_REFCNT_VALUE(c) >= 1);
pa_assert((ss == NULL && map == NULL) || formats == NULL); pa_assert((ss == NULL && map == NULL) || (formats == NULL && n_formats == 0));
pa_assert(n_formats < PA_MAX_FORMATS);
PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED); PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
PA_CHECK_VALIDITY_RETURN_NULL(c, name || (p && pa_proplist_contains(p, PA_PROP_MEDIA_NAME)), PA_ERR_INVALID); PA_CHECK_VALIDITY_RETURN_NULL(c, name || (p && pa_proplist_contains(p, PA_PROP_MEDIA_NAME)), PA_ERR_INVALID);
@ -119,13 +121,10 @@ static pa_stream *pa_stream_new_with_proplist_internal(
s->n_formats = 0; s->n_formats = 0;
if (formats) { if (formats) {
for (i = 0; formats[i] && i < PA_MAX_FORMATS; i++) { s->n_formats = n_formats;
s->n_formats++; for (i = 0; i < n_formats; i++)
s->req_formats[i] = pa_format_info_copy(formats[i]); s->req_formats[i] = pa_format_info_copy(formats[i]);
} }
/* Make sure the input array was NULL-terminated */
pa_assert(formats[i] == NULL);
}
/* We'll get the final negotiated format after connecting */ /* We'll get the final negotiated format after connecting */
s->format = NULL; s->format = NULL;
@ -221,18 +220,19 @@ pa_stream *pa_stream_new_with_proplist(
if (!map) if (!map)
PA_CHECK_VALIDITY_RETURN_NULL(c, map = pa_channel_map_init_auto(&tmap, ss->channels, PA_CHANNEL_MAP_DEFAULT), PA_ERR_INVALID); PA_CHECK_VALIDITY_RETURN_NULL(c, map = pa_channel_map_init_auto(&tmap, ss->channels, PA_CHANNEL_MAP_DEFAULT), PA_ERR_INVALID);
return pa_stream_new_with_proplist_internal(c, name, ss, map, NULL, p); return pa_stream_new_with_proplist_internal(c, name, ss, map, NULL, 0, p);
} }
pa_stream *pa_stream_new_extended( pa_stream *pa_stream_new_extended(
pa_context *c, pa_context *c,
const char *name, const char *name,
pa_format_info * const *formats, pa_format_info * const *formats,
unsigned int n_formats,
pa_proplist *p) { pa_proplist *p) {
PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 21, PA_ERR_NOTSUPPORTED); PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 21, PA_ERR_NOTSUPPORTED);
return pa_stream_new_with_proplist_internal(c, name, NULL, NULL, formats, p); return pa_stream_new_with_proplist_internal(c, name, NULL, NULL, formats, n_formats, p);
} }
static void stream_unlink(pa_stream *s) { static void stream_unlink(pa_stream *s) {

View file

@ -365,6 +365,7 @@ pa_stream *pa_stream_new_extended(
pa_context *c /**< The context to create this stream in */, pa_context *c /**< The context to create this stream in */,
const char *name /**< A name for this stream */, const char *name /**< A name for this stream */,
pa_format_info * const * formats /**< The list of formats that can be provided */, pa_format_info * const * formats /**< The list of formats that can be provided */,
unsigned int n_formats /**< The number of formats being passed in */,
pa_proplist *p /**< The initial property list */); pa_proplist *p /**< The initial property list */);
/** Decrease the reference counter by one */ /** Decrease the reference counter by one */

View file

@ -122,7 +122,7 @@ static void context_state_callback(pa_context *c, void *userdata) {
for (i = 0; i < NSTREAMS; i++) { for (i = 0; i < NSTREAMS; i++) {
char name[64]; char name[64];
pa_format_info *formats[2] = { NULL }; pa_format_info *formats[1];
formats[0] = pa_format_info_new(); formats[0] = pa_format_info_new();
formats[0]->encoding = PA_ENCODING_PCM; formats[0]->encoding = PA_ENCODING_PCM;
@ -134,10 +134,12 @@ static void context_state_callback(pa_context *c, void *userdata) {
snprintf(name, sizeof(name), "stream #%i", i); snprintf(name, sizeof(name), "stream #%i", i);
streams[i] = pa_stream_new_extended(c, name, formats, NULL); streams[i] = pa_stream_new_extended(c, name, formats, 1, NULL);
assert(streams[i]); assert(streams[i]);
pa_stream_set_state_callback(streams[i], stream_state_callback, (void*) (long) i); pa_stream_set_state_callback(streams[i], stream_state_callback, (void*) (long) i);
pa_stream_connect_playback(streams[i], NULL, &buffer_attr, PA_STREAM_START_CORKED, NULL, i == 0 ? NULL : streams[0]); pa_stream_connect_playback(streams[i], NULL, &buffer_attr, PA_STREAM_START_CORKED, NULL, i == 0 ? NULL : streams[0]);
pa_format_info_free(formats[0]);
} }
break; break;