Commit 106d5973 authored by Wim Taymans's avatar Wim Taymans
Browse files

bluez5: add aptX and aptX HD codecs

They need the libopenaptx libraries from
https://github.com/pali/libopenaptx
parent af8bb557
Pipeline #238721 passed with stages
in 1 minute and 10 seconds
......@@ -29,6 +29,7 @@ if get_option('spa-plugins')
bluez_dep = dependency('bluez', version : '>= 4.101')
sbc_dep = dependency('sbc')
ldac_dep = dependency('ldacBT-enc', required : false)
aptx_dep = dependency('libopenaptx', required : false)
endif
if get_option('ffmpeg')
avcodec_dep = dependency('libavcodec')
......
......@@ -42,7 +42,8 @@ struct impl {
int frame_length;
};
static int codec_fill_caps(uint32_t flags, uint8_t caps[A2DP_MAX_CAPS_SIZE])
static int codec_fill_caps(const struct a2dp_codec *codec, uint32_t flags,
uint8_t caps[A2DP_MAX_CAPS_SIZE])
{
const a2dp_aac_t a2dp_aac = {
.object_type =
......@@ -73,8 +74,9 @@ static int codec_fill_caps(uint32_t flags, uint8_t caps[A2DP_MAX_CAPS_SIZE])
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])
static int codec_select_config(const struct a2dp_codec *codec, 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;
......@@ -137,7 +139,8 @@ static int codec_select_config(uint32_t flags, const void *caps, size_t caps_siz
return sizeof(conf);
}
static void *codec_init(uint32_t flags, void *config, size_t config_len, struct spa_audio_info *info, size_t mtu)
static void *codec_init(const struct a2dp_codec *codec, uint32_t flags,
void *config, size_t config_len, struct spa_audio_info *info, size_t mtu)
{
struct impl *this;
int res;
......@@ -165,8 +168,8 @@ static void codec_deinit(void *data)
free(this);
}
struct a2dp_codec a2dp_codec_aac = {
.id = {.codec_id = A2DP_CODEC_MPEG24},
const struct a2dp_codec a2dp_codec_aac = {
.codec_id = A2DP_CODEC_MPEG24,
.name = "aac",
.description = "AAC",
.fill_caps = codec_fill_caps,
......
/* 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 <openaptx.h>
#include "defs.h"
#include "rtp.h"
#include "a2dp-codecs.h"
#define MAX_FRAME_COUNT 16
struct impl {
struct aptx_context *aptx;
struct rtp_header *header;
struct rtp_payload *payload;
size_t mtu;
int codesize;
int frame_length;
};
static int codec_fill_caps(const struct a2dp_codec *codec, uint32_t flags,
uint8_t caps[A2DP_MAX_CAPS_SIZE])
{
const a2dp_aptx_t a2dp_aptx = {
.info = codec->vendor,
.frequency =
APTX_SAMPLING_FREQ_16000 |
APTX_SAMPLING_FREQ_32000 |
APTX_SAMPLING_FREQ_44100 |
APTX_SAMPLING_FREQ_48000,
.channel_mode =
APTX_CHANNEL_MODE_STEREO,
};
memcpy(caps, &a2dp_aptx, sizeof(a2dp_aptx));
return sizeof(a2dp_aptx);
}
static int codec_select_config(const struct a2dp_codec *codec, uint32_t flags,
const void *caps, size_t caps_size,
const struct spa_audio_info *info, uint8_t config[A2DP_MAX_CAPS_SIZE])
{
a2dp_aptx_t conf;
if (caps_size < sizeof(conf))
return -EINVAL;
memcpy(&conf, caps, sizeof(conf));
if (conf.frequency & APTX_SAMPLING_FREQ_48000)
conf.frequency = APTX_SAMPLING_FREQ_48000;
else if (conf.frequency & APTX_SAMPLING_FREQ_44100)
conf.frequency = APTX_SAMPLING_FREQ_44100;
else if (conf.frequency & APTX_SAMPLING_FREQ_32000)
conf.frequency = APTX_SAMPLING_FREQ_32000;
else if (conf.frequency & APTX_SAMPLING_FREQ_16000)
conf.frequency = APTX_SAMPLING_FREQ_16000;
else
return -ENOTSUP;
if (conf.channel_mode & APTX_CHANNEL_MODE_STEREO)
conf.channel_mode = APTX_CHANNEL_MODE_STEREO;
else
return -ENOTSUP;
return sizeof(conf);
}
static int codec_enum_config(const struct a2dp_codec *codec,
const void *caps, size_t caps_size, uint32_t id, uint32_t idx,
struct spa_pod_builder *b, struct spa_pod **param)
{
a2dp_aptx_t conf;
struct spa_pod_frame f[2];
struct spa_pod_choice *choice;
uint32_t i = 0;
if (caps_size < sizeof(conf))
return -EINVAL;
memcpy(&conf, caps, sizeof(conf));
if (idx > 0)
return 0;
spa_pod_builder_push_object(b, &f[0], SPA_TYPE_OBJECT_Format, id);
spa_pod_builder_add(b,
SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_audio),
SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
SPA_FORMAT_AUDIO_format, SPA_POD_Id(SPA_AUDIO_FORMAT_S24),
0);
spa_pod_builder_prop(b, SPA_FORMAT_AUDIO_rate, 0);
spa_pod_builder_push_choice(b, &f[1], SPA_CHOICE_None, 0);
choice = (struct spa_pod_choice*)spa_pod_builder_frame(b, &f[1]);
i = 0;
if (conf.frequency & APTX_SAMPLING_FREQ_48000) {
if (i++ == 0)
spa_pod_builder_int(b, 48000);
spa_pod_builder_int(b, 48000);
}
if (conf.frequency & APTX_SAMPLING_FREQ_44100) {
if (i++ == 0)
spa_pod_builder_int(b, 44100);
spa_pod_builder_int(b, 44100);
}
if (conf.frequency & APTX_SAMPLING_FREQ_32000) {
if (i++ == 0)
spa_pod_builder_int(b, 32000);
spa_pod_builder_int(b, 32000);
}
if (conf.frequency & APTX_SAMPLING_FREQ_16000) {
if (i++ == 0)
spa_pod_builder_int(b, 16000);
spa_pod_builder_int(b, 16000);
}
if (i == 0)
return -EINVAL;
if (i > 1)
choice->body.type = SPA_CHOICE_Enum;
spa_pod_builder_pop(b, &f[1]);
if (SPA_FLAG_IS_SET(conf.channel_mode, APTX_CHANNEL_MODE_MONO | APTX_CHANNEL_MODE_STEREO)) {
spa_pod_builder_add(b,
SPA_FORMAT_AUDIO_channels, SPA_POD_CHOICE_RANGE_Int(2, 1, 2),
0);
} else if (conf.channel_mode & APTX_CHANNEL_MODE_MONO) {
spa_pod_builder_add(b,
SPA_FORMAT_AUDIO_channels, SPA_POD_Int(1),
0);
} else if (conf.channel_mode & APTX_CHANNEL_MODE_STEREO) {
spa_pod_builder_add(b,
SPA_FORMAT_AUDIO_channels, SPA_POD_Int(2),
0);
} else
return -EINVAL;
*param = spa_pod_builder_pop(b, &f[0]);
return 1;
}
static int codec_reduce_bitpool(void *data)
{
return -ENOTSUP;
}
static int codec_increase_bitpool(void *data)
{
return -ENOTSUP;
}
static int codec_get_num_blocks(void *data)
{
struct impl *this = data;
size_t frame_count = this->mtu / this->frame_length;
return frame_count;
}
static int codec_get_block_size(void *data)
{
struct impl *this = data;
return this->codesize;
}
static void *codec_init(const struct a2dp_codec *codec, uint32_t flags,
void *config, size_t config_len, struct spa_audio_info *info, size_t mtu)
{
struct impl *this;
a2dp_aptx_t *conf = config;
int res, hd;
if ((this = calloc(1, sizeof(struct impl))) == NULL)
goto error_errno;
hd = codec->vendor.vendor_id == APTX_HD_CODEC_ID;
if ((this->aptx = aptx_init(hd)) == NULL)
goto error_errno;
this->mtu = mtu;
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 APTX_SAMPLING_FREQ_16000:
info->info.raw.rate = 16000;
break;
case APTX_SAMPLING_FREQ_32000:
info->info.raw.rate = 32000;
break;
case APTX_SAMPLING_FREQ_44100:
info->info.raw.rate = 44100;
break;
case APTX_SAMPLING_FREQ_48000:
info->info.raw.rate = 48000;
break;
default:
res = -EINVAL;
goto error;
}
switch (conf->channel_mode) {
case APTX_CHANNEL_MODE_MONO:
info->info.raw.channels = 1;
break;
case APTX_CHANNEL_MODE_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;
default:
res = -EINVAL;
goto error;
}
this->frame_length = hd ? 6 : 4;
this->codesize = 4 * 3 * 2;
return this;
error_errno:
res = -errno;
goto error;
error:
free(this);
errno = -res;
return NULL;
}
static void codec_deinit(void *data)
{
struct impl *this = data;
aptx_finish(this->aptx);
free(this);
}
static int codec_start_encode (void *data,
void *dst, size_t dst_size, uint16_t seqnum, uint32_t timestamp)
{
return 0;
}
static int codec_encode(void *data,
const void *src, size_t src_size,
void *dst, size_t dst_size,
size_t *dst_out)
{
struct impl *this = data;
int res;
res = aptx_encode(this->aptx, src, src_size,
dst, dst_size, dst_out);
return res;
}
static int codec_start_decode (void *data,
const void *src, size_t src_size, uint16_t *seqnum, uint32_t *timestamp)
{
return 0;
}
static int codec_decode(void *data,
const void *src, size_t src_size,
void *dst, size_t dst_size,
size_t *dst_out)
{
struct impl *this = data;
int res;
res = aptx_decode(this->aptx, src, src_size,
dst, dst_size, dst_out);
return res;
}
const struct a2dp_codec a2dp_codec_aptx = {
.codec_id = A2DP_CODEC_VENDOR,
.vendor = { .vendor_id = APTX_VENDOR_ID,
.codec_id = APTX_CODEC_ID },
.name = "aptx",
.description = "aptX",
.fill_caps = codec_fill_caps,
.select_config = codec_select_config,
.enum_config = codec_enum_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,
.start_decode = codec_start_decode,
.decode = codec_decode,
.reduce_bitpool = codec_reduce_bitpool,
.increase_bitpool = codec_increase_bitpool,
};
const struct a2dp_codec a2dp_codec_aptx_hd = {
.codec_id = A2DP_CODEC_VENDOR,
.vendor = { .vendor_id = APTX_HD_VENDOR_ID,
.codec_id = APTX_HD_CODEC_ID },
.name = "aptx_hd",
.description = "aptX HD",
.fill_caps = codec_fill_caps,
.select_config = codec_select_config,
.enum_config = codec_enum_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,
.start_decode = codec_start_decode,
.decode = codec_decode,
.reduce_bitpool = codec_reduce_bitpool,
.increase_bitpool = codec_increase_bitpool,
};
......@@ -53,7 +53,7 @@ struct impl {
int frame_length;
};
static int codec_fill_caps(uint32_t flags, uint8_t caps[A2DP_MAX_CAPS_SIZE])
static int codec_fill_caps(const struct a2dp_codec *codec, uint32_t flags, uint8_t caps[A2DP_MAX_CAPS_SIZE])
{
const a2dp_ldac_t a2dp_ldac = {
.info.vendor_id = LDAC_VENDOR_ID,
......@@ -72,8 +72,9 @@ static int codec_fill_caps(uint32_t flags, uint8_t caps[A2DP_MAX_CAPS_SIZE])
return sizeof(a2dp_ldac);
}
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])
static int codec_select_config(const struct a2dp_codec *codec, uint32_t flags,
const void *caps, size_t caps_size,
const struct spa_audio_info *info, uint8_t config[A2DP_MAX_CAPS_SIZE])
{
a2dp_ldac_t conf;
......@@ -113,8 +114,9 @@ static int codec_select_config(uint32_t flags, const void *caps, size_t caps_siz
return sizeof(conf);
}
static int codec_enum_config(const void *caps, size_t caps_size, uint32_t id, uint32_t idx,
struct spa_pod_builder *b, struct spa_pod **param)
static int codec_enum_config(const struct a2dp_codec *codec,
const void *caps, size_t caps_size, uint32_t id, uint32_t idx,
struct spa_pod_builder *b, struct spa_pod **param)
{
a2dp_sbc_t conf;
struct spa_pod_frame f[2];
......@@ -223,8 +225,8 @@ static int codec_get_block_size(void *data)
return this->codesize;
}
static void *codec_init(uint32_t flags, void *config, size_t config_len, struct spa_audio_info *info,
size_t mtu)
static void *codec_init(const struct a2dp_codec *codec, uint32_t flags,
void *config, size_t config_len, struct spa_audio_info *info, size_t mtu)
{
struct impl *this;
a2dp_ldac_t *conf = config;
......@@ -380,10 +382,10 @@ static int codec_encode(void *data,
return src_used;
}
struct a2dp_codec a2dp_codec_ldac = {
.id = {.codec_id = A2DP_CODEC_VENDOR,
.vendor_id = LDAC_VENDOR_ID,
.vendor_codec_id = LDAC_CODEC_ID },
const struct a2dp_codec a2dp_codec_ldac = {
.codec_id = A2DP_CODEC_VENDOR,
.vendor = { .vendor_id = LDAC_VENDOR_ID,
.codec_id = LDAC_CODEC_ID },
.name = "ldac",
.description = "LDAC",
.fill_caps = codec_fill_caps,
......
......@@ -51,7 +51,8 @@ struct impl {
int max_bitpool;
};
static int codec_fill_caps(uint32_t flags, uint8_t caps[A2DP_MAX_CAPS_SIZE])
static int codec_fill_caps(const struct a2dp_codec *codec, uint32_t flags,
uint8_t caps[A2DP_MAX_CAPS_SIZE])
{
const a2dp_sbc_t a2dp_sbc = {
.frequency =
......@@ -116,8 +117,9 @@ static uint8_t default_bitpool(uint8_t freq, uint8_t mode)
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])
static int codec_select_config(const struct a2dp_codec *codec, 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;
......@@ -191,8 +193,9 @@ static int codec_set_bitpool(struct impl *this, int bitpool)
return this->sbc.bitpool;
}
static int codec_enum_config(const void *caps, size_t caps_size, uint32_t id, uint32_t idx,
struct spa_pod_builder *b, struct spa_pod **param)
static int codec_enum_config(const struct a2dp_codec *codec,
const void *caps, size_t caps_size, uint32_t id, uint32_t idx,
struct spa_pod_builder *b, struct spa_pod **param)
{
a2dp_sbc_t conf;
struct spa_pod_frame f[2];
......@@ -290,7 +293,8 @@ static int codec_get_block_size(void *data)
return this->codesize;
}
static void *codec_init(uint32_t flags, void *config, size_t config_len, struct spa_audio_info *info, size_t mtu)
static void *codec_init(const struct a2dp_codec *codec, uint32_t flags,
void *config, size_t config_len, struct spa_audio_info *info, size_t mtu)
{
struct impl *this;
a2dp_sbc_t *conf = config;
......@@ -482,8 +486,8 @@ static int codec_decode(void *data,
return res;
}
struct a2dp_codec a2dp_codec_sbc = {
.id = {.codec_id = A2DP_CODEC_SBC},
const struct a2dp_codec a2dp_codec_sbc = {
.codec_id = A2DP_CODEC_SBC,
.name = "sbc",
.description = "SBC",
.fill_caps = codec_fill_caps,
......
......@@ -50,24 +50,6 @@ const a2dp_mpeg_t bluez_a2dp_mpeg = {
};
#endif
#if ENABLE_APTX
const a2dp_aptx_t bluez_a2dp_aptx = {
.info.vendor_id = APTX_VENDOR_ID,
.info.codec_id = APTX_CODEC_ID,
.channel_mode =
/* NOTE: Used apt-X library does not support
* single channel (mono) mode. */
APTX_CHANNEL_MODE_DUAL_CHANNEL |
APTX_CHANNEL_MODE_STEREO |
APTX_CHANNEL_MODE_JOINT_STEREO,
.frequency =
APTX_SAMPLING_FREQ_16000 |
APTX_SAMPLING_FREQ_32000 |
APTX_SAMPLING_FREQ_44100 |
APTX_SAMPLING_FREQ_48000,
};
#endif
extern struct a2dp_codec a2dp_codec_sbc;
#if ENABLE_LDAC
extern struct a2dp_codec a2dp_codec_ldac;
......@@ -80,6 +62,7 @@ extern struct a2dp_codec a2dp_codec_mpeg;
#endif
#if ENABLE_APTX
extern struct a2dp_codec a2dp_codec_aptx;
extern struct a2dp_codec a2dp_codec_aptx_hd;
#endif
const struct a2dp_codec *a2dp_codec_list[] = {
......@@ -95,6 +78,7 @@ const struct a2dp_codec *a2dp_codec_list[] = {
#endif
#if ENABLE_APTX
&a2dp_codec_aptx,
&a2dp_codec_aptx_hd,
#endif
NULL,
};
......
......@@ -149,16 +149,25 @@
#define APTX_VENDOR_ID 0x0000004f
#define APTX_CODEC_ID 0x0001
#define APTX_CHANNEL_MODE_MONO 0x08
#define APTX_CHANNEL_MODE_DUAL_CHANNEL 0x04
#define APTX_CHANNEL_MODE_MONO 0x01
#define APTX_CHANNEL_MODE_STEREO 0x02
#define APTX_CHANNEL_MODE_JOINT_STEREO 0x01
#define APTX_SAMPLING_FREQ_16000 0x08
#define APTX_SAMPLING_FREQ_32000 0x04
#define APTX_SAMPLING_FREQ_44100 0x02
#define APTX_SAMPLING_FREQ_48000 0x01
#define APTX_HD_VENDOR_ID 0x000000D7
#define APTX_HD_CODEC_ID 0x0024
#define APTX_HD_CHANNEL_MODE_MONO 0x1
#define APTX_HD_CHANNEL_MODE_STEREO 0x2
#define APTX_HD_SAMPLING_FREQ_16000 0x8
#define APTX_HD_SAMPLING_FREQ_32000 0x4
#define APTX_HD_SAMPLING_FREQ_44100 0x2
#define APTX_HD_SAMPLING_FREQ_48000 0x1
#define LDAC_VENDOR_ID 0x0000012d
#define LDAC_CODEC_ID 0x00aa
......@@ -306,27 +315,27 @@ static inline int a2dp_sbc_get_frequency(a2dp_sbc_t *config)
}
}
struct a2dp_codec_id {
uint8_t codec_id;
uint32_t vendor_id;
uint16_t vendor_codec_id;
};
struct a2dp_codec_handle;
struct a2dp_codec {
uint32_t flags;
struct a2dp_codec_id id;
uint8_t codec_id;
a2dp_vendor_codec_t vendor;