audio: make audio format an uri type

This commit is contained in:
Wim Taymans 2017-03-21 16:50:44 +01:00
parent ff62c1b9ce
commit 03292fd80f
24 changed files with 608 additions and 364 deletions

View file

@ -326,9 +326,9 @@ next:
case 0:
spa_pod_builder_format (&b, &f[0],
this->uri.media_types.audio, this->uri.media_subtypes.raw,
PROP_U_EN (&f[1], this->uri.prop_audio.format, SPA_POD_TYPE_INT, 3, SPA_AUDIO_FORMAT_S16,
SPA_AUDIO_FORMAT_S16,
SPA_AUDIO_FORMAT_S32),
PROP_U_EN (&f[1], this->uri.prop_audio.format, SPA_POD_TYPE_URI, 3, this->uri.audio_formats.S16,
this->uri.audio_formats.S16,
this->uri.audio_formats.S32),
PROP_U_MM (&f[1], this->uri.prop_audio.rate, SPA_POD_TYPE_INT, 44100, 1, INT32_MAX),
PROP_U_MM (&f[1], this->uri.prop_audio.channels, SPA_POD_TYPE_INT, 2, 1, INT32_MAX));
break;
@ -799,6 +799,7 @@ alsa_sink_init (const SpaHandleFactory *factory,
spa_media_subtypes_map (this->map, &this->uri.media_subtypes);
spa_media_subtypes_audio_map (this->map, &this->uri.media_subtypes_audio);
spa_prop_audio_map (this->map, &this->uri.prop_audio);
spa_audio_formats_map (this->map, &this->uri.audio_formats);
this->node = alsasink_node;
this->stream = SND_PCM_STREAM_PLAYBACK;

View file

@ -360,9 +360,9 @@ next:
case 0:
spa_pod_builder_format (&b, &f[0],
this->uri.media_types.audio, this->uri.media_subtypes.raw,
PROP_U_EN (&f[1], this->uri.prop_audio.format, SPA_POD_TYPE_INT, 3, SPA_AUDIO_FORMAT_S16,
SPA_AUDIO_FORMAT_S16,
SPA_AUDIO_FORMAT_S32),
PROP_U_EN (&f[1], this->uri.prop_audio.format, SPA_POD_TYPE_URI, 3, this->uri.audio_formats.S16,
this->uri.audio_formats.S16,
this->uri.audio_formats.S32),
PROP_U_MM (&f[1], this->uri.prop_audio.rate, SPA_POD_TYPE_INT, 44100, 1, INT32_MAX),
PROP_U_MM (&f[1], this->uri.prop_audio.channels, SPA_POD_TYPE_INT, 2, 1, INT32_MAX));
break;
@ -865,6 +865,7 @@ alsa_source_init (const SpaHandleFactory *factory,
spa_media_subtypes_map (this->map, &this->uri.media_subtypes);
spa_media_subtypes_audio_map (this->map, &this->uri.media_subtypes_audio);
spa_prop_audio_map (this->map, &this->uri.prop_audio);
spa_audio_formats_map (this->map, &this->uri.audio_formats);
this->node = alsasource_node;
this->clock = alsasource_clock;

View file

@ -56,54 +56,52 @@ spa_alsa_close (SpaALSAState *state)
return err;
}
static snd_pcm_format_t
spa_alsa_format_to_alsa (SpaAudioFormat format)
{
switch (format) {
case SPA_AUDIO_FORMAT_S8:
return SND_PCM_FORMAT_S8;
case SPA_AUDIO_FORMAT_U8:
return SND_PCM_FORMAT_U8;
/* 16 bit */
case SPA_AUDIO_FORMAT_S16LE:
return SND_PCM_FORMAT_S16_LE;
case SPA_AUDIO_FORMAT_S16BE:
return SND_PCM_FORMAT_S16_BE;
case SPA_AUDIO_FORMAT_U16LE:
return SND_PCM_FORMAT_U16_LE;
case SPA_AUDIO_FORMAT_U16BE:
return SND_PCM_FORMAT_U16_BE;
/* 24 bit in low 3 bytes of 32 bits */
case SPA_AUDIO_FORMAT_S24_32LE:
return SND_PCM_FORMAT_S24_LE;
case SPA_AUDIO_FORMAT_S24_32BE:
return SND_PCM_FORMAT_S24_BE;
case SPA_AUDIO_FORMAT_U24_32LE:
return SND_PCM_FORMAT_U24_LE;
case SPA_AUDIO_FORMAT_U24_32BE:
return SND_PCM_FORMAT_U24_BE;
/* 24 bit in 3 bytes */
case SPA_AUDIO_FORMAT_S24LE:
return SND_PCM_FORMAT_S24_3LE;
case SPA_AUDIO_FORMAT_S24BE:
return SND_PCM_FORMAT_S24_3BE;
case SPA_AUDIO_FORMAT_U24LE:
return SND_PCM_FORMAT_U24_3LE;
case SPA_AUDIO_FORMAT_U24BE:
return SND_PCM_FORMAT_U24_3BE;
/* 32 bit */
case SPA_AUDIO_FORMAT_S32LE:
return SND_PCM_FORMAT_S32_LE;
case SPA_AUDIO_FORMAT_S32BE:
return SND_PCM_FORMAT_S32_BE;
case SPA_AUDIO_FORMAT_U32LE:
return SND_PCM_FORMAT_U32_LE;
case SPA_AUDIO_FORMAT_U32BE:
return SND_PCM_FORMAT_U32_BE;
default:
break;
}
typedef struct {
off_t format_offset;
snd_pcm_format_t format;
} FormatInfo;
#if __BYTE_ORDER == __BIG_ENDIAN
#define _FORMAT_LE(fmt) offsetof(URI, audio_formats. fmt ## _OE)
#define _FORMAT_BE(fmt) offsetof(URI, audio_formats. fmt)
#elif __BYTE_ORDER == __LITTLE_ENDIAN
#define _FORMAT_LE(fmt) offsetof(URI, audio_formats. fmt)
#define _FORMAT_BE(fmt) offsetof(URI, audio_formats. fmt ## _OE)
#endif
static const FormatInfo format_info[] =
{
{ offsetof(URI, audio_formats.UNKNOWN), SND_PCM_FORMAT_UNKNOWN },
{ offsetof(URI, audio_formats.S8), SND_PCM_FORMAT_S8 },
{ offsetof(URI, audio_formats.U8), SND_PCM_FORMAT_U8 },
{ _FORMAT_LE (S16), SND_PCM_FORMAT_S16_LE },
{ _FORMAT_BE (S16), SND_PCM_FORMAT_S16_BE },
{ _FORMAT_LE (U16), SND_PCM_FORMAT_U16_LE },
{ _FORMAT_BE (U16), SND_PCM_FORMAT_U16_BE },
{ _FORMAT_LE (S24_32), SND_PCM_FORMAT_S24_LE },
{ _FORMAT_BE (S24_32), SND_PCM_FORMAT_S24_BE },
{ _FORMAT_LE (U24_32), SND_PCM_FORMAT_U24_LE },
{ _FORMAT_BE (U24_32), SND_PCM_FORMAT_U24_BE },
{ _FORMAT_LE (S24), SND_PCM_FORMAT_S24_3LE },
{ _FORMAT_BE (S24), SND_PCM_FORMAT_S24_3BE },
{ _FORMAT_LE (U24), SND_PCM_FORMAT_U24_3LE },
{ _FORMAT_BE (U24), SND_PCM_FORMAT_U24_3BE },
{ _FORMAT_LE (S32), SND_PCM_FORMAT_S32_LE },
{ _FORMAT_BE (S32), SND_PCM_FORMAT_S32_BE },
{ _FORMAT_LE (U32), SND_PCM_FORMAT_U32_LE },
{ _FORMAT_BE (U32), SND_PCM_FORMAT_U32_BE },
};
static snd_pcm_format_t
spa_alsa_format_to_alsa (URI *uri, uint32_t format)
{
int i;
for (i = 0; i < SPA_N_ELEMENTS (format_info); i++) {
uint32_t f = *SPA_MEMBER (uri, format_info[i].format_offset, uint32_t);
if (f == format)
return format_info[i].format;
}
return SND_PCM_FORMAT_UNKNOWN;
}
@ -134,7 +132,7 @@ spa_alsa_set_format (SpaALSAState *state, SpaAudioInfo *fmt, SpaPortFormatFlags
CHECK (snd_pcm_hw_params_set_access(hndl, params, SND_PCM_ACCESS_MMAP_INTERLEAVED), "set_access");
/* set the sample format */
format = spa_alsa_format_to_alsa (info->format);
format = spa_alsa_format_to_alsa (&state->uri, info->format);
spa_log_info (state->log, "Stream parameters are %iHz, %s, %i channels", info->rate, snd_pcm_format_name(format), info->channels);
CHECK (snd_pcm_hw_params_set_format (hndl, params, format), "set_format");

View file

@ -34,7 +34,7 @@ extern "C" {
#include <spa/node.h>
#include <spa/loop.h>
#include <spa/ringbuffer.h>
#include <spa/audio/format.h>
#include <spa/audio/format-utils.h>
#include <spa/format-builder.h>
typedef struct _SpaALSAState SpaALSAState;
@ -66,6 +66,7 @@ typedef struct {
SpaMediaSubtypes media_subtypes;
SpaMediaSubtypesAudio media_subtypes_audio;
SpaPropAudio prop_audio;
SpaAudioFormats audio_formats;
} URI;
struct _SpaALSAState {