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

@ -0,0 +1,177 @@
/* Spa A2DP SBC codec
*
* Copyright © 2020 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <stddef.h>
#include <errno.h>
#include <arpa/inet.h>
#include <spa/param/audio/format.h>
#include "defs.h"
#include "rtp.h"
#include "a2dp-codecs.h"
struct impl {
struct rtp_header *header;
struct rtp_payload *payload;
int codesize;
int frame_length;
int min_bitpool;
int max_bitpool;
};
static int codec_fill_caps(uint32_t flags, uint8_t caps[A2DP_MAX_CAPS_SIZE])
{
const a2dp_aac_t a2dp_aac = {
.object_type =
/* NOTE: AAC Long Term Prediction and AAC Scalable are
* not supported by the FDK-AAC library. */
AAC_OBJECT_TYPE_MPEG2_AAC_LC |
AAC_OBJECT_TYPE_MPEG4_AAC_LC,
AAC_INIT_FREQUENCY(
AAC_SAMPLING_FREQ_8000 |
AAC_SAMPLING_FREQ_11025 |
AAC_SAMPLING_FREQ_12000 |
AAC_SAMPLING_FREQ_16000 |
AAC_SAMPLING_FREQ_22050 |
AAC_SAMPLING_FREQ_24000 |
AAC_SAMPLING_FREQ_32000 |
AAC_SAMPLING_FREQ_44100 |
AAC_SAMPLING_FREQ_48000 |
AAC_SAMPLING_FREQ_64000 |
AAC_SAMPLING_FREQ_88200 |
AAC_SAMPLING_FREQ_96000)
.channels =
AAC_CHANNELS_1 |
AAC_CHANNELS_2,
.vbr = 1,
AAC_INIT_BITRATE(0xFFFF)
};
memcpy(caps, &a2dp_aac, sizeof(a2dp_aac));
return sizeof(a2dp_aac);
}
static int codec_select_config(uint32_t flags, const void *caps, size_t caps_size,
const struct spa_audio_info *info, uint8_t config[A2DP_MAX_CAPS_SIZE])
{
a2dp_aac_t conf;
int freq;
if (caps_size < sizeof(conf))
return -EINVAL;
conf = *(a2dp_aac_t*)caps;
if (conf.object_type & AAC_OBJECT_TYPE_MPEG2_AAC_LC)
conf.object_type = AAC_OBJECT_TYPE_MPEG2_AAC_LC;
else if (conf.object_type & AAC_OBJECT_TYPE_MPEG4_AAC_LC)
conf.object_type = AAC_OBJECT_TYPE_MPEG4_AAC_LC;
else if (conf.object_type & AAC_OBJECT_TYPE_MPEG4_AAC_LTP)
conf.object_type = AAC_OBJECT_TYPE_MPEG4_AAC_LTP;
else if (conf.object_type & AAC_OBJECT_TYPE_MPEG4_AAC_SCA)
conf.object_type = AAC_OBJECT_TYPE_MPEG4_AAC_SCA;
else
return -ENOTSUP;
freq = AAC_GET_FREQUENCY(conf);
if (freq & AAC_SAMPLING_FREQ_48000)
freq = AAC_SAMPLING_FREQ_48000;
else if (freq & AAC_SAMPLING_FREQ_44100)
freq = AAC_SAMPLING_FREQ_44100;
else if (freq & AAC_SAMPLING_FREQ_64000)
freq = AAC_SAMPLING_FREQ_64000;
else if (freq & AAC_SAMPLING_FREQ_32000)
freq = AAC_SAMPLING_FREQ_32000;
else if (freq & AAC_SAMPLING_FREQ_88200)
freq = AAC_SAMPLING_FREQ_88200;
else if (freq & AAC_SAMPLING_FREQ_96000)
freq = AAC_SAMPLING_FREQ_96000;
else if (freq & AAC_SAMPLING_FREQ_24000)
freq = AAC_SAMPLING_FREQ_24000;
else if (freq & AAC_SAMPLING_FREQ_22050)
freq = AAC_SAMPLING_FREQ_22050;
else if (freq & AAC_SAMPLING_FREQ_16000)
freq = AAC_SAMPLING_FREQ_16000;
else if (freq & AAC_SAMPLING_FREQ_12000)
freq = AAC_SAMPLING_FREQ_12000;
else if (freq & AAC_SAMPLING_FREQ_11025)
freq = AAC_SAMPLING_FREQ_11025;
else if (freq & AAC_SAMPLING_FREQ_8000)
freq = AAC_SAMPLING_FREQ_8000;
else
return -ENOTSUP;
AAC_SET_FREQUENCY(conf, freq);
if (conf.channels & AAC_CHANNELS_2)
conf.channels = AAC_CHANNELS_2;
else if (conf.channels & AAC_CHANNELS_1)
conf.channels = AAC_CHANNELS_1;
else
return -ENOTSUP;
memcpy(config, &conf, sizeof(conf));
return sizeof(conf);
}
static void *codec_init(uint32_t flags, void *config, size_t config_len, struct spa_audio_info *info)
{
struct impl *this;
int res;
this = calloc(1, sizeof(struct impl));
if (this == NULL) {
res = -errno;
goto error;
}
spa_zero(*info);
info->media_type = SPA_MEDIA_TYPE_audio;
info->media_subtype = SPA_MEDIA_SUBTYPE_aac;
return this;
error:
errno = -res;
return NULL;
}
static void codec_deinit(void *data)
{
struct impl *this = data;
free(this);
}
struct a2dp_codec a2dp_codec_aac = {
.codec_id = A2DP_CODEC_MPEG24,
.name = "aac",
.description = "AAC",
.fill_caps = codec_fill_caps,
.select_config = codec_select_config,
.init = codec_init,
.deinit = codec_deinit,
};

View file

@ -0,0 +1,398 @@
/* Spa A2DP SBC codec
*
* Copyright © 2020 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <stddef.h>
#include <errno.h>
#include <arpa/inet.h>
#include <spa/param/audio/format.h>
#include <sbc/sbc.h>
#include "defs.h"
#include "rtp.h"
#include "a2dp-codecs.h"
#define MAX_FRAME_COUNT 16
struct impl {
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_fill_caps(uint32_t flags, uint8_t caps[A2DP_MAX_CAPS_SIZE])
{
const a2dp_sbc_t a2dp_sbc = {
.frequency =
SBC_SAMPLING_FREQ_16000 |
SBC_SAMPLING_FREQ_32000 |
SBC_SAMPLING_FREQ_44100 |
SBC_SAMPLING_FREQ_48000,
.channel_mode =
SBC_CHANNEL_MODE_MONO |
SBC_CHANNEL_MODE_DUAL_CHANNEL |
SBC_CHANNEL_MODE_STEREO |
SBC_CHANNEL_MODE_JOINT_STEREO,
.block_length =
SBC_BLOCK_LENGTH_4 |
SBC_BLOCK_LENGTH_8 |
SBC_BLOCK_LENGTH_12 |
SBC_BLOCK_LENGTH_16,
.subbands =
SBC_SUBBANDS_4 |
SBC_SUBBANDS_8,
.allocation_method =
SBC_ALLOCATION_SNR |
SBC_ALLOCATION_LOUDNESS,
.min_bitpool = SBC_MIN_BITPOOL,
.max_bitpool = SBC_MAX_BITPOOL,
};
memcpy(caps, &a2dp_sbc, sizeof(a2dp_sbc));
return sizeof(a2dp_sbc);
}
static uint8_t default_bitpool(uint8_t freq, uint8_t mode)
{
/* These bitpool values were chosen based on the A2DP spec recommendation */
switch (freq) {
case SBC_SAMPLING_FREQ_16000:
case SBC_SAMPLING_FREQ_32000:
return 53;
case SBC_SAMPLING_FREQ_44100:
switch (mode) {
case SBC_CHANNEL_MODE_MONO:
case SBC_CHANNEL_MODE_DUAL_CHANNEL:
return 31;
case SBC_CHANNEL_MODE_STEREO:
case SBC_CHANNEL_MODE_JOINT_STEREO:
return 53;
}
return 53;
case SBC_SAMPLING_FREQ_48000:
switch (mode) {
case SBC_CHANNEL_MODE_MONO:
case SBC_CHANNEL_MODE_DUAL_CHANNEL:
return 29;
case SBC_CHANNEL_MODE_STEREO:
case SBC_CHANNEL_MODE_JOINT_STEREO:
return 51;
}
return 51;
}
return 53;
}
static int codec_select_config(uint32_t flags, const void *caps, size_t caps_size,
const struct spa_audio_info *info, uint8_t config[A2DP_MAX_CAPS_SIZE])
{
a2dp_sbc_t conf;
int bitpool;
if (caps_size < sizeof(conf))
return -EINVAL;
memcpy(&conf, caps, sizeof(conf));
if (conf.frequency & SBC_SAMPLING_FREQ_48000)
conf.frequency = SBC_SAMPLING_FREQ_48000;
else if (conf.frequency & SBC_SAMPLING_FREQ_44100)
conf.frequency = SBC_SAMPLING_FREQ_44100;
else if (conf.frequency & SBC_SAMPLING_FREQ_32000)
conf.frequency = SBC_SAMPLING_FREQ_32000;
else if (conf.frequency & SBC_SAMPLING_FREQ_16000)
conf.frequency = SBC_SAMPLING_FREQ_16000;
else
return -ENOTSUP;
if (conf.channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
conf.channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
else if (conf.channel_mode & SBC_CHANNEL_MODE_STEREO)
conf.channel_mode = SBC_CHANNEL_MODE_STEREO;
else if (conf.channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
conf.channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
else if (conf.channel_mode & SBC_CHANNEL_MODE_MONO)
conf.channel_mode = SBC_CHANNEL_MODE_MONO;
else
return -ENOTSUP;
if (conf.block_length & SBC_BLOCK_LENGTH_16)
conf.block_length = SBC_BLOCK_LENGTH_16;
else if (conf.block_length & SBC_BLOCK_LENGTH_12)
conf.block_length = SBC_BLOCK_LENGTH_12;
else if (conf.block_length & SBC_BLOCK_LENGTH_8)
conf.block_length = SBC_BLOCK_LENGTH_8;
else if (conf.block_length & SBC_BLOCK_LENGTH_4)
conf.block_length = SBC_BLOCK_LENGTH_4;
else
return -ENOTSUP;
if (conf.subbands & SBC_SUBBANDS_8)
conf.subbands = SBC_SUBBANDS_8;
else if (conf.subbands & SBC_SUBBANDS_4)
conf.subbands = SBC_SUBBANDS_4;
else
return -ENOTSUP;
if (conf.allocation_method & SBC_ALLOCATION_LOUDNESS)
conf.allocation_method = SBC_ALLOCATION_LOUDNESS;
else if (conf.allocation_method & SBC_ALLOCATION_SNR)
conf.allocation_method = SBC_ALLOCATION_SNR;
else
return -ENOTSUP;
bitpool = default_bitpool(conf.frequency, conf.channel_mode);
conf.min_bitpool = SPA_MAX(SBC_MIN_BITPOOL, conf.min_bitpool);
conf.max_bitpool = SPA_MIN(bitpool, conf.max_bitpool);
memcpy(config, &conf, sizeof(conf));
return sizeof(conf);
}
static int codec_set_bitpool(struct impl *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 this->sbc.bitpool;
}
static int codec_reduce_bitpool(void *data)
{
struct impl *this = data;
return codec_set_bitpool(this, this->sbc.bitpool - 2);
}
static int codec_increase_bitpool(void *data)
{
struct impl *this = data;
return codec_set_bitpool(this, this->sbc.bitpool + 1);
}
static int codec_get_num_blocks(void *data, size_t mtu)
{
struct impl *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_get_block_size(void *data)
{
struct impl *this = data;
return this->codesize;
}
static void *codec_init(uint32_t flags, void *config, size_t config_len, struct spa_audio_info *info)
{
struct impl *this;
a2dp_sbc_t *conf = config;
int res;
this = calloc(1, sizeof(struct impl));
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_set_bitpool(this, conf->max_bitpool);
return this;
error:
errno = -res;
return NULL;
}
static void codec_deinit(void *data)
{
struct impl *this = data;
sbc_finish(&this->sbc);
free(this);
}
static int codec_start_encode (void *data,
void *dst, size_t dst_size, uint16_t seqnum, uint32_t timestamp)
{
struct impl *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_encode(void *data,
const void *src, size_t src_size,
void *dst, size_t dst_size,
size_t *encoded)
{
struct impl *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 a2dp_codec a2dp_codec_sbc = {
.codec_id = A2DP_CODEC_SBC,
.name = "sbc",
.description = "SBC",
.fill_caps = codec_fill_caps,
.select_config = codec_select_config,
.init = codec_init,
.deinit = codec_deinit,
.get_block_size = codec_get_block_size,
.get_num_blocks = codec_get_num_blocks,
.start_encode = codec_start_encode,
.encode = codec_encode,
.reduce_bitpool = codec_reduce_bitpool,
.increase_bitpool = codec_increase_bitpool,
};

View file

@ -10,32 +10,6 @@
#include "a2dp-codecs.h"
const a2dp_sbc_t bluez_a2dp_sbc = {
.frequency =
SBC_SAMPLING_FREQ_16000 |
SBC_SAMPLING_FREQ_32000 |
SBC_SAMPLING_FREQ_44100 |
SBC_SAMPLING_FREQ_48000,
.channel_mode =
SBC_CHANNEL_MODE_MONO |
SBC_CHANNEL_MODE_DUAL_CHANNEL |
SBC_CHANNEL_MODE_STEREO |
SBC_CHANNEL_MODE_JOINT_STEREO,
.block_length =
SBC_BLOCK_LENGTH_4 |
SBC_BLOCK_LENGTH_8 |
SBC_BLOCK_LENGTH_12 |
SBC_BLOCK_LENGTH_16,
.subbands =
SBC_SUBBANDS_4 |
SBC_SUBBANDS_8,
.allocation_method =
SBC_ALLOCATION_SNR |
SBC_ALLOCATION_LOUDNESS,
.min_bitpool = MIN_BITPOOL,
.max_bitpool = MAX_BITPOOL,
};
#if ENABLE_MP3
const a2dp_mpeg_t bluez_a2dp_mpeg = {
.layer =

View file

@ -26,6 +26,9 @@
#define BLUEALSA_A2DPCODECS_H_
#include <stdint.h>
#include <stddef.h>
#include <spa/param/audio/format.h>
#define A2DP_CODEC_SBC 0x00
#define A2DP_CODEC_MPEG12 0x01
@ -33,6 +36,8 @@
#define A2DP_CODEC_ATRAC 0x03
#define A2DP_CODEC_VENDOR 0xFF
#define A2DP_MAX_CAPS_SIZE 254
/* customized 16-bit vendor extension */
#define A2DP_CODEC_VENDOR_APTX 0x4FFF
#define A2DP_CODEC_VENDOR_LDAC 0x2DFF
@ -58,8 +63,8 @@
#define SBC_ALLOCATION_LOUDNESS (1 << 0)
#define SBC_ALLOCATION_SNR (1 << 1)
#define MIN_BITPOOL 2
#define MAX_BITPOOL 64
#define SBC_MIN_BITPOOL 2
#define SBC_MAX_BITPOOL 64
#define MPEG_CHANNEL_MODE_JOINT_STEREO (1 << 0)
#define MPEG_CHANNEL_MODE_STEREO (1 << 1)
@ -155,8 +160,6 @@
#define LDAC_VENDOR_ID 0x0000012d
#define LDAC_CODEC_ID 0x00aa
#define A2DP_MAX_CONFIG_SIZE 254
typedef struct {
uint32_t vendor_id;
uint16_t codec_id;
@ -286,13 +289,48 @@ static inline int a2dp_sbc_get_frequency(a2dp_sbc_t *config)
}
}
extern const a2dp_sbc_t bluez_a2dp_sbc;
struct a2dp_codec {
uint32_t flags;
uint8_t codec_id;
uint32_t vendor_id;
uint16_t vendor_codec_id;
const char *name;
const char *description;
int (*fill_caps) (uint32_t flags, uint8_t caps[A2DP_MAX_CAPS_SIZE]);
int (*select_config) (uint32_t flags, const void *caps, size_t caps_size,
const struct spa_audio_info *info, uint8_t config[A2DP_MAX_CAPS_SIZE]);
void *(*init) (uint32_t flags, void *config, size_t config_size, struct spa_audio_info *info);
void (*deinit) (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);
int (*decode) (void *data,
const void *src, size_t src_size,
void *dst, size_t dst_size,
size_t *dst_out);
int (*reduce_bitpool) (void *data);
int (*increase_bitpool) (void *data);
};
extern struct a2dp_codec a2dp_codec_sbc;
extern struct a2dp_codec a2dp_codec_aac;
#if ENABLE_MP3
extern const a2dp_mpeg_t bluez_a2dp_mpeg;
#endif
#if ENABLE_AAC
extern const a2dp_aac_t bluez_a2dp_aac;
#endif
#if ENABLE_APTX
extern const a2dp_aptx_t bluez_a2dp_aptx;
#endif

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);

View file

@ -84,205 +84,16 @@ static inline void add_dict(struct spa_pod_builder *builder, const char *key, co
spa_pod_builder_string(builder, val);
}
static uint8_t a2dp_default_bitpool(struct spa_bt_monitor *monitor, uint8_t freq, uint8_t mode) {
/* These bitpool values were chosen based on the A2DP spec recommendation */
switch (freq) {
case SBC_SAMPLING_FREQ_16000:
case SBC_SAMPLING_FREQ_32000:
return 53;
case SBC_SAMPLING_FREQ_44100:
switch (mode) {
case SBC_CHANNEL_MODE_MONO:
case SBC_CHANNEL_MODE_DUAL_CHANNEL:
return 31;
case SBC_CHANNEL_MODE_STEREO:
case SBC_CHANNEL_MODE_JOINT_STEREO:
return 53;
}
spa_log_warn(monitor->log, "Invalid channel mode %u", mode);
return 53;
case SBC_SAMPLING_FREQ_48000:
switch (mode) {
case SBC_CHANNEL_MODE_MONO:
case SBC_CHANNEL_MODE_DUAL_CHANNEL:
return 29;
case SBC_CHANNEL_MODE_STEREO:
case SBC_CHANNEL_MODE_JOINT_STEREO:
return 51;
}
spa_log_warn(monitor->log, "Invalid channel mode %u", mode);
return 51;
}
spa_log_warn(monitor->log, "Invalid sampling freq %u", freq);
return 53;
}
static int select_configuration_sbc(struct spa_bt_monitor *monitor, void *capabilities, size_t size, void *config)
{
a2dp_sbc_t *cap, conf;
int bitpool;
if (size < sizeof(conf)) {
spa_log_error(monitor->log, "Capabilities array has invalid size");
return -ENOSPC;
}
cap = capabilities;
conf = *cap;
if (conf.frequency & SBC_SAMPLING_FREQ_48000)
conf.frequency = SBC_SAMPLING_FREQ_48000;
else if (conf.frequency & SBC_SAMPLING_FREQ_44100)
conf.frequency = SBC_SAMPLING_FREQ_44100;
else if (conf.frequency & SBC_SAMPLING_FREQ_32000)
conf.frequency = SBC_SAMPLING_FREQ_32000;
else if (conf.frequency & SBC_SAMPLING_FREQ_16000)
conf.frequency = SBC_SAMPLING_FREQ_16000;
else {
spa_log_error(monitor->log, "No supported sampling frequencies: 0x%x", conf.frequency);
return -ENOTSUP;
}
if (conf.channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
conf.channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
else if (conf.channel_mode & SBC_CHANNEL_MODE_STEREO)
conf.channel_mode = SBC_CHANNEL_MODE_STEREO;
else if (conf.channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
conf.channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
else if (conf.channel_mode & SBC_CHANNEL_MODE_MONO)
conf.channel_mode = SBC_CHANNEL_MODE_MONO;
else {
spa_log_error(monitor->log, "No supported channel modes: 0x%x", conf.channel_mode);
return -ENOTSUP;
}
if (conf.block_length & SBC_BLOCK_LENGTH_16)
conf.block_length = SBC_BLOCK_LENGTH_16;
else if (conf.block_length & SBC_BLOCK_LENGTH_12)
conf.block_length = SBC_BLOCK_LENGTH_12;
else if (conf.block_length & SBC_BLOCK_LENGTH_8)
conf.block_length = SBC_BLOCK_LENGTH_8;
else if (conf.block_length & SBC_BLOCK_LENGTH_4)
conf.block_length = SBC_BLOCK_LENGTH_4;
else {
spa_log_error(monitor->log, "No supported block lengths: 0x%x", conf.block_length);
return -ENOTSUP;
}
if (conf.subbands & SBC_SUBBANDS_8)
conf.subbands = SBC_SUBBANDS_8;
else if (conf.subbands & SBC_SUBBANDS_4)
conf.subbands = SBC_SUBBANDS_4;
else {
spa_log_error(monitor->log, "No supported subbands: 0x%x", conf.subbands);
return -ENOTSUP;
}
if (conf.allocation_method & SBC_ALLOCATION_LOUDNESS)
conf.allocation_method = SBC_ALLOCATION_LOUDNESS;
else if (conf.allocation_method & SBC_ALLOCATION_SNR)
conf.allocation_method = SBC_ALLOCATION_SNR;
else {
spa_log_error(monitor->log, "No supported allocation: 0x%x", conf.allocation_method);
return -ENOTSUP;
}
bitpool = a2dp_default_bitpool(monitor, conf.frequency, conf.channel_mode);
conf.min_bitpool = SPA_MAX(MIN_BITPOOL, conf.min_bitpool);
conf.max_bitpool = SPA_MIN(bitpool, conf.max_bitpool);
memcpy(config, &conf, size);
spa_log_debug(monitor->log, "SelectConfiguration(): %d %d %d %d ",
conf.frequency, conf.channel_mode, conf.min_bitpool, conf.max_bitpool);
return 0;
}
static int select_configuration_aac(struct spa_bt_monitor *monitor, void *capabilities, size_t size, void *config)
{
a2dp_aac_t *cap, conf;
int freq;
if (size < sizeof(conf)) {
spa_log_error(monitor->log, "Capabilities array has invalid size");
return -ENOSPC;
}
cap = capabilities;
conf = *cap;
if (conf.object_type & AAC_OBJECT_TYPE_MPEG2_AAC_LC)
conf.object_type = AAC_OBJECT_TYPE_MPEG2_AAC_LC;
else if (conf.object_type & AAC_OBJECT_TYPE_MPEG4_AAC_LC)
conf.object_type = AAC_OBJECT_TYPE_MPEG4_AAC_LC;
else if (conf.object_type & AAC_OBJECT_TYPE_MPEG4_AAC_LTP)
conf.object_type = AAC_OBJECT_TYPE_MPEG4_AAC_LTP;
else if (conf.object_type & AAC_OBJECT_TYPE_MPEG4_AAC_SCA)
conf.object_type = AAC_OBJECT_TYPE_MPEG4_AAC_SCA;
else {
spa_log_error(monitor->log, "No supported object type: 0x%x", conf.object_type);
return -ENOTSUP;
}
freq = AAC_GET_FREQUENCY(conf);
if (freq & AAC_SAMPLING_FREQ_48000)
freq = AAC_SAMPLING_FREQ_48000;
else if (freq & AAC_SAMPLING_FREQ_44100)
freq = AAC_SAMPLING_FREQ_44100;
else if (freq & AAC_SAMPLING_FREQ_64000)
freq = AAC_SAMPLING_FREQ_64000;
else if (freq & AAC_SAMPLING_FREQ_32000)
freq = AAC_SAMPLING_FREQ_32000;
else if (freq & AAC_SAMPLING_FREQ_88200)
freq = AAC_SAMPLING_FREQ_88200;
else if (freq & AAC_SAMPLING_FREQ_96000)
freq = AAC_SAMPLING_FREQ_96000;
else if (freq & AAC_SAMPLING_FREQ_24000)
freq = AAC_SAMPLING_FREQ_24000;
else if (freq & AAC_SAMPLING_FREQ_22050)
freq = AAC_SAMPLING_FREQ_22050;
else if (freq & AAC_SAMPLING_FREQ_16000)
freq = AAC_SAMPLING_FREQ_16000;
else if (freq & AAC_SAMPLING_FREQ_12000)
freq = AAC_SAMPLING_FREQ_12000;
else if (freq & AAC_SAMPLING_FREQ_11025)
freq = AAC_SAMPLING_FREQ_11025;
else if (freq & AAC_SAMPLING_FREQ_8000)
freq = AAC_SAMPLING_FREQ_8000;
else {
spa_log_error(monitor->log, "No supported sampling frequency: 0x%0x", freq);
return -ENOTSUP;
}
AAC_SET_FREQUENCY(conf, freq);
if (conf.channels & AAC_CHANNELS_2)
conf.channels = AAC_CHANNELS_2;
else if (conf.channels & AAC_CHANNELS_1)
conf.channels = AAC_CHANNELS_1;
else {
spa_log_error(monitor->log, "No supported channels: 0x%0x", conf.channels);
return -ENOTSUP;
}
memcpy(config, &conf, size);
spa_log_debug(monitor->log, "SelectConfiguration() %d %d %d", conf.object_type, freq, conf.channels);
return 0;
}
static DBusHandlerResult endpoint_select_configuration(DBusConnection *conn, DBusMessage *m, void *userdata)
{
struct spa_bt_monitor *monitor = userdata;
const char *path;
uint8_t *cap, config[16];
uint8_t *cap, config[A2DP_MAX_CAPS_SIZE];
uint8_t *pconf = (uint8_t *) config;
DBusMessage *r;
DBusError err;
int size, res;
struct a2dp_codec *codec;
dbus_error_init(&err);
@ -294,20 +105,25 @@ static DBusHandlerResult endpoint_select_configuration(DBusConnection *conn, DBu
dbus_error_free(&err);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
spa_log_info(monitor->log, "%p: select conf %d", monitor, size);
if (strstr(path, "/A2DP/SBC/") == path) {
res = select_configuration_sbc(monitor, cap, size, config);
codec = &a2dp_codec_sbc;
} else if (strstr(path, "/A2DP/MPEG24/") == path) {
res = select_configuration_aac(monitor, cap, size, config);
codec = &a2dp_codec_aac;
} else
codec = NULL;
if (codec == NULL)
res = -ENOTSUP;
res = codec->select_config(0, cap, size, NULL, config);
if (res < 0) {
if ((r = dbus_message_new_error(m, "org.bluez.Error.InvalidArguments",
"Unable to select configuration")) == NULL)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
goto exit_send;
}
size = res;
if ((r = dbus_message_new_method_return(m)) == NULL)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
@ -1309,7 +1125,7 @@ static int register_a2dp_endpoint(struct spa_bt_monitor *monitor,
if (object_path == NULL)
return -errno;
spa_log_debug(monitor->log, "Registering endpoint: %s", object_path);
spa_log_info(monitor->log, "Registering endpoint: %s", object_path);
if (!dbus_connection_register_object_path(monitor->conn,
object_path,
@ -1367,6 +1183,9 @@ static int register_a2dp_endpoint(struct spa_bt_monitor *monitor,
static int adapter_register_endpoints(struct spa_bt_adapter *a)
{
struct spa_bt_monitor *monitor = a->monitor;
struct a2dp_codec *codec;
uint8_t caps[A2DP_MAX_CAPS_SIZE];
int caps_size;
/* We don't support MPEG24 for now */
/*
@ -1382,16 +1201,21 @@ static int adapter_register_endpoints(struct spa_bt_adapter *a)
&bluez_a2dp_aac, sizeof(bluez_a2dp_aac));
*/
codec = &a2dp_codec_sbc;
caps_size = codec->fill_caps(0, caps);
spa_log_info(monitor->log, "register %s config_size:%d",
codec->name, caps_size);
register_a2dp_endpoint(monitor, a->path,
SPA_BT_UUID_A2DP_SOURCE,
SPA_BT_PROFILE_A2DP_SOURCE,
A2DP_CODEC_SBC,
&bluez_a2dp_sbc, sizeof(bluez_a2dp_sbc));
codec->codec_id,
caps, caps_size);
register_a2dp_endpoint(monitor, a->path,
SPA_BT_UUID_A2DP_SINK,
SPA_BT_PROFILE_A2DP_SINK,
A2DP_CODEC_SBC,
&bluez_a2dp_sbc, sizeof(bluez_a2dp_sbc));
codec->codec_id,
caps, caps_size);
return 0;
}

View file

@ -29,6 +29,8 @@
extern "C" {
#endif
#include <spa/support/plugin.h>
#include <spa/support/loop.h>
#include <spa/utils/hook.h>
#include "config.h"

View file

@ -1,6 +1,8 @@
bluez5_sources = ['plugin.c',
'a2dp-codecs.c',
'a2dp-codec-aac.c',
'a2dp-codec-sbc.c',
'a2dp-sink.c',
'a2dp-source.c',
'sco-sink.c',