diff --git a/spa/plugins/bluez5/a2dp-codec-aptx.c b/spa/plugins/bluez5/a2dp-codec-aptx.c index dbd60fad4..2682d9d1c 100644 --- a/spa/plugins/bluez5/a2dp-codec-aptx.c +++ b/spa/plugins/bluez5/a2dp-codec-aptx.c @@ -300,8 +300,17 @@ static void *codec_init(const struct media_codec *codec, uint32_t flags, void *config, size_t config_len, const struct spa_audio_info *info, void *props, size_t mtu) { - struct impl *this; + struct impl *this = NULL; + a2dp_aptx_t conf; int res; + int frequency; + + if (config_len < sizeof(conf)) { + res = -EINVAL; + goto error; + } + + memcpy(&conf, config, sizeof(conf)); if ((this = calloc(1, sizeof(struct impl))) == NULL) goto error_errno; @@ -322,10 +331,17 @@ static void *codec_init(const struct media_codec *codec, uint32_t flags, this->frame_length = this->hd ? 6 : 4; this->codesize = 4 * 3 * 2; + frequency = media_codec_get_config(aptx_frequencies, SPA_N_ELEMENTS(aptx_frequencies), conf.frequency); + if (frequency < 0) { + res = -EINVAL; + goto error; + } + if (this->hd) this->max_frames = (this->mtu - sizeof(struct rtp_header)) / this->frame_length; else if (codec_is_ll(codec)) - this->max_frames = SPA_MIN(256u, this->mtu) / this->frame_length; + /* try to make 7.5ms packets */ + this->max_frames = SPA_MIN((unsigned)frequency * 75u/10000u / 4u, this->mtu / this->frame_length); else this->max_frames = this->mtu / this->frame_length; diff --git a/spa/plugins/bluez5/media-codecs.c b/spa/plugins/bluez5/media-codecs.c index b8d0a2fc2..718e54ea2 100644 --- a/spa/plugins/bluez5/media-codecs.c +++ b/spa/plugins/bluez5/media-codecs.c @@ -61,6 +61,18 @@ int media_codec_select_config(const struct media_codec_config configs[], size_t return res; } +int media_codec_get_config(const struct media_codec_config configs[], size_t n, + uint32_t conf) +{ + size_t i; + + for (i = 0; i < n; ++i) + if (configs[i].config == conf) + return configs[i].value; + + return -EINVAL; +} + bool media_codec_check_caps(const struct media_codec *codec, unsigned int codec_id, const void *caps, size_t caps_size, const struct media_codec_audio_info *info, diff --git a/spa/plugins/bluez5/media-codecs.h b/spa/plugins/bluez5/media-codecs.h index 9b82589a1..7d7df5246 100644 --- a/spa/plugins/bluez5/media-codecs.h +++ b/spa/plugins/bluez5/media-codecs.h @@ -198,6 +198,9 @@ struct media_codec_config { int media_codec_select_config(const struct media_codec_config configs[], size_t n, uint32_t cap, int preferred_value); +int media_codec_get_config(const struct media_codec_config configs[], size_t n, + uint32_t conf); + bool media_codec_check_caps(const struct media_codec *codec, unsigned int codec_id, const void *caps, size_t caps_size, const struct media_codec_audio_info *info, const struct spa_dict *global_settings);