a2dp: move codecs to separate files

This commit is contained in:
Wim Taymans 2020-10-19 12:12:21 +02:00
parent 5c5b9f61bb
commit e18c4d76dc
8 changed files with 649 additions and 479 deletions

View file

@ -127,7 +127,7 @@ struct impl {
uint64_t next_time;
uint64_t last_error;
struct codec *codec;
struct a2dp_codec *codec;
void *codec_data;
struct spa_audio_info codec_format;
@ -143,250 +143,6 @@ struct impl {
int tmp_buffer_used;
};
struct codec {
void *(*fill_caps) (uint32_t flags, uint8_t caps[A2DP_MAX_CONFIG_SIZE]);
void *(*select_config) (uint32_t flags, const void *caps, size_t caps_size,
const struct spa_audio_info *info, uint8_t config[A2DP_MAX_CONFIG_SIZE]);
void *(*init) (uint32_t flags, void *config, size_t config_size, struct spa_audio_info *info);
void (*deinit) (void *data);
int (*reduce_bitpool) (void *data);
int (*increase_bitpool) (void *data);
int (*get_block_size) (void *data);
int (*get_num_blocks) (void *data, size_t mtu);
int (*start_encode) (void *data,
void *dst, size_t dst_size, uint16_t seqnum, uint32_t timestamp);
int (*encode) (void *data,
const void *src, size_t src_size,
void *dst, size_t dst_size,
size_t *dst_out);
};
struct impl_sbc {
sbc_t sbc;
struct rtp_header *header;
struct rtp_payload *payload;
int codesize;
int frame_length;
int min_bitpool;
int max_bitpool;
};
static int codec_sbc_set_bitpool(struct impl_sbc *this, int bitpool)
{
this->sbc.bitpool = SPA_CLAMP(bitpool, this->min_bitpool, this->max_bitpool);
this->codesize = sbc_get_codesize(&this->sbc);
this->frame_length = sbc_get_frame_length(&this->sbc);
return 0;
}
static int codec_sbc_reduce_bitpool(void *data)
{
struct impl_sbc *this = data;
return codec_sbc_set_bitpool(this, this->sbc.bitpool - 2);
}
static int codec_sbc_increase_bitpool(void *data)
{
struct impl_sbc *this = data;
return codec_sbc_set_bitpool(this, this->sbc.bitpool + 1);
}
static int codec_sbc_get_num_blocks(void *data, size_t mtu)
{
struct impl_sbc *this = data;
size_t rtp_size = sizeof(struct rtp_header) + sizeof(struct rtp_payload);
size_t frame_count = (mtu - rtp_size) / this->frame_length;
/* frame_count is only 4 bit number */
if (frame_count > 15)
frame_count = 15;
return frame_count;
}
static int codec_sbc_get_block_size(void *data)
{
struct impl_sbc *this = data;
return this->codesize;
}
static void *codec_sbc_init(uint32_t flags, void *config, size_t config_len, struct spa_audio_info *info)
{
struct impl_sbc *this;
a2dp_sbc_t *conf = config;
int res;
this = calloc(1, sizeof(struct impl_sbc));
if (this == NULL) {
res = -errno;
goto error;
}
sbc_init(&this->sbc, 0);
this->sbc.endian = SBC_LE;
spa_zero(*info);
info->media_type = SPA_MEDIA_TYPE_audio;
info->media_subtype = SPA_MEDIA_SUBTYPE_raw;
info->info.raw.format = SPA_AUDIO_FORMAT_S16;
switch (conf->frequency) {
case SBC_SAMPLING_FREQ_16000:
this->sbc.frequency = SBC_FREQ_16000;
info->info.raw.rate = 16000;
break;
case SBC_SAMPLING_FREQ_32000:
this->sbc.frequency = SBC_FREQ_32000;
info->info.raw.rate = 32000;
break;
case SBC_SAMPLING_FREQ_44100:
this->sbc.frequency = SBC_FREQ_44100;
info->info.raw.rate = 44100;
break;
case SBC_SAMPLING_FREQ_48000:
this->sbc.frequency = SBC_FREQ_48000;
info->info.raw.rate = 48000;
break;
default:
res = -EINVAL;
goto error;
}
switch (conf->channel_mode) {
case SBC_CHANNEL_MODE_MONO:
this->sbc.mode = SBC_MODE_MONO;
info->info.raw.channels = 1;
break;
case SBC_CHANNEL_MODE_DUAL_CHANNEL:
this->sbc.mode = SBC_MODE_DUAL_CHANNEL;
info->info.raw.channels = 2;
break;
case SBC_CHANNEL_MODE_STEREO:
this->sbc.mode = SBC_MODE_STEREO;
info->info.raw.channels = 2;
break;
case SBC_CHANNEL_MODE_JOINT_STEREO:
this->sbc.mode = SBC_MODE_JOINT_STEREO;
info->info.raw.channels = 2;
break;
default:
res = -EINVAL;
goto error;
}
switch (info->info.raw.channels) {
case 1:
info->info.raw.position[0] = SPA_AUDIO_CHANNEL_MONO;
break;
case 2:
info->info.raw.position[0] = SPA_AUDIO_CHANNEL_FL;
info->info.raw.position[1] = SPA_AUDIO_CHANNEL_FR;
break;
}
switch (conf->subbands) {
case SBC_SUBBANDS_4:
this->sbc.subbands = SBC_SB_4;
break;
case SBC_SUBBANDS_8:
this->sbc.subbands = SBC_SB_8;
break;
default:
res = -EINVAL;
goto error;
}
if (conf->allocation_method & SBC_ALLOCATION_LOUDNESS)
this->sbc.allocation = SBC_AM_LOUDNESS;
else
this->sbc.allocation = SBC_AM_SNR;
switch (conf->block_length) {
case SBC_BLOCK_LENGTH_4:
this->sbc.blocks = SBC_BLK_4;
break;
case SBC_BLOCK_LENGTH_8:
this->sbc.blocks = SBC_BLK_8;
break;
case SBC_BLOCK_LENGTH_12:
this->sbc.blocks = SBC_BLK_12;
break;
case SBC_BLOCK_LENGTH_16:
this->sbc.blocks = SBC_BLK_16;
break;
default:
res = -EINVAL;
goto error;
}
this->min_bitpool = SPA_MAX(conf->min_bitpool, 12);
this->max_bitpool = conf->max_bitpool;
codec_sbc_set_bitpool(this, conf->max_bitpool);
return this;
error:
errno = -res;
return NULL;
}
static void codec_sbc_deinit(void *data)
{
struct impl_sbc *this = data;
sbc_finish(&this->sbc);
free(this);
}
static int codec_sbc_start_encode (void *data,
void *dst, size_t dst_size, uint16_t seqnum, uint32_t timestamp)
{
struct impl_sbc *this = data;
this->header = (struct rtp_header *)dst;
this->payload = SPA_MEMBER(dst, sizeof(struct rtp_header), struct rtp_payload);
memset(this->header, 0, sizeof(struct rtp_header)+sizeof(struct rtp_payload));
this->payload->frame_count = 0;
this->header->v = 2;
this->header->pt = 1;
this->header->sequence_number = htons(seqnum);
this->header->timestamp = htonl(timestamp);
this->header->ssrc = htonl(1);
return sizeof(struct rtp_header) + sizeof(struct rtp_payload);
}
static int codec_sbc_encode(void *data,
const void *src, size_t src_size,
void *dst, size_t dst_size,
size_t *encoded)
{
struct impl_sbc *this = data;
int res;
res = sbc_encode(&this->sbc, src, src_size,
dst, dst_size, encoded);
if (res >= this->codesize)
this->payload->frame_count += res / this->codesize;
return res;
}
struct codec codec_sbc = {
.init = codec_sbc_init,
.deinit = codec_sbc_deinit,
.reduce_bitpool = codec_sbc_reduce_bitpool,
.increase_bitpool = codec_sbc_increase_bitpool,
.get_block_size = codec_sbc_get_block_size,
.get_num_blocks = codec_sbc_get_num_blocks,
.start_encode = codec_sbc_start_encode,
.encode = codec_sbc_encode,
};
#define NAME "a2dp-sink"
#define CHECK_PORT(this,d,p) ((d) == SPA_DIRECTION_INPUT && (p) == 0)
@ -1501,8 +1257,7 @@ impl_init(const struct spa_handle_factory *factory,
this->timerfd = spa_system_timerfd_create(this->data_system,
CLOCK_MONOTONIC, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
this->codec = &codec_sbc;
this->codec = &a2dp_codec_sbc;
this->codec_data = this->codec->init(0, this->transport->configuration,
this->transport->configuration_len,
&this->codec_format);