Skip to content

Commit 691e0f5

Browse files
committed
Update after merge with master
1 parent 54492bc commit 691e0f5

File tree

2 files changed

+53
-48
lines changed

2 files changed

+53
-48
lines changed

src/a2dp-lhdc.c

+40-48
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
#include <sys/ioctl.h>
2222
#include <unistd.h>
2323

24-
#include <glib.h>
25-
2624
#include <lhdcBT.h>
2725
#include <lhdcBT_dec.h>
2826

@@ -44,7 +42,7 @@ static const enum ba_transport_pcm_channel a2dp_lhdc_channel_map_stereo[] = {
4442
BA_TRANSPORT_PCM_CHANNEL_FL, BA_TRANSPORT_PCM_CHANNEL_FR,
4543
};
4644

47-
static const struct a2dp_bit_mapping a2dp_lhdc_samplings[] = {
45+
static const struct a2dp_bit_mapping a2dp_lhdc_rates[] = {
4846
{ LHDC_SAMPLING_FREQ_44100, { 44100 } },
4947
{ LHDC_SAMPLING_FREQ_48000, { 48000 } },
5048
{ LHDC_SAMPLING_FREQ_96000, { 96000 } },
@@ -70,14 +68,14 @@ static int a2dp_lhdc_caps_foreach_channel_mode(
7068
return -1;
7169
}
7270

73-
static int a2dp_lhdc_caps_foreach_sampling_freq(
71+
static int a2dp_lhdc_caps_foreach_sample_rate(
7472
const void *capabilities,
7573
enum a2dp_stream stream,
7674
a2dp_bit_mapping_foreach_func func,
7775
void *userdata) {
7876
const a2dp_lhdc_v3_t *caps = capabilities;
7977
if (stream == A2DP_MAIN)
80-
return a2dp_bit_mapping_foreach(a2dp_lhdc_samplings, caps->sampling_freq, func, userdata);
78+
return a2dp_bit_mapping_foreach(a2dp_lhdc_rates, caps->sampling_freq, func, userdata);
8179
return -1;
8280
}
8381

@@ -90,23 +88,23 @@ static void a2dp_lhdc_caps_select_channel_mode(
9088
(void)channels;
9189
}
9290

93-
static void a2dp_lhdc_caps_select_sampling_freq(
91+
static void a2dp_lhdc_caps_select_sample_rate(
9492
void *capabilities,
9593
enum a2dp_stream stream,
96-
unsigned int frequency) {
94+
unsigned int rate) {
9795
a2dp_lhdc_v3_t *caps = capabilities;
9896
if (stream == A2DP_MAIN)
99-
caps->sampling_freq = a2dp_bit_mapping_lookup_value(a2dp_lhdc_samplings,
100-
caps->sampling_freq, frequency);
97+
caps->sampling_freq = a2dp_bit_mapping_lookup_value(a2dp_lhdc_rates,
98+
caps->sampling_freq, rate);
10199
}
102100

103101
static struct a2dp_caps_helpers a2dp_lhdc_caps_helpers = {
104102
.intersect = a2dp_lhdc_caps_intersect,
105103
.has_stream = a2dp_caps_has_main_stream_only,
106104
.foreach_channel_mode = a2dp_lhdc_caps_foreach_channel_mode,
107-
.foreach_sampling_freq = a2dp_lhdc_caps_foreach_sampling_freq,
105+
.foreach_sample_rate = a2dp_lhdc_caps_foreach_sample_rate,
108106
.select_channel_mode = a2dp_lhdc_caps_select_channel_mode,
109-
.select_sampling_freq = a2dp_lhdc_caps_select_sampling_freq,
107+
.select_sample_rate = a2dp_lhdc_caps_select_sample_rate,
110108
};
111109

112110
static LHDC_VERSION_SETUP get_version(const a2dp_lhdc_v3_t *configuration) {
@@ -157,8 +155,9 @@ void *a2dp_lhdc_enc_thread(struct ba_transport_pcm *t_pcm) {
157155
struct io_poll io = { .timeout = -1 };
158156

159157
const a2dp_lhdc_v3_t *configuration = &t->a2dp.configuration.lhdc_v3;
158+
const unsigned int bit_depth = get_bit_depth(configuration);
160159
const unsigned int channels = t_pcm->channels;
161-
const unsigned int samplerate = t_pcm->sampling;
160+
const unsigned int rate = t_pcm->rate;
162161

163162
HANDLE_LHDC_BT handle;
164163
if ((handle = lhdcBT_get_handle(get_version(configuration))) == NULL) {
@@ -168,22 +167,14 @@ void *a2dp_lhdc_enc_thread(struct ba_transport_pcm *t_pcm) {
168167

169168
pthread_cleanup_push(PTHREAD_CLEANUP(lhdcBT_free_handle), handle);
170169

171-
const unsigned int bitdepth = get_bit_depth(configuration);
172170

173171
lhdcBT_set_hasMinBitrateLimit(handle, configuration->min_bitrate);
174172
lhdcBT_set_max_bitrate(handle, get_max_bitrate(configuration));
175173

176-
struct {
177-
uint8_t seq_num;
178-
uint8_t latency:2;
179-
uint8_t frames:6;
180-
} *lhdc_media_header;
181-
182-
rtp_header_t *rtp_header;
183-
184-
if (lhdcBT_init_encoder(handle, samplerate, bitdepth, config.lhdc_eqmid,
185-
configuration->ch_split_mode > LHDC_CH_SPLIT_MODE_NONE, 0, t->mtu_write - sizeof(*lhdc_media_header) - RTP_HEADER_LEN,
186-
get_interval(configuration)) == -1) {
174+
if (lhdcBT_init_encoder(handle, rate, bit_depth, config.lhdc_eqmid,
175+
configuration->ch_split_mode > LHDC_CH_SPLIT_MODE_NONE, 0,
176+
t->mtu_write - RTP_HEADER_LEN - sizeof(rtp_lhdc_media_header_t),
177+
get_interval(configuration)) == -1) {
187178
error("Couldn't initialize LHDC encoder");
188179
goto fail_init;
189180
}
@@ -208,13 +199,15 @@ void *a2dp_lhdc_enc_thread(struct ba_transport_pcm *t_pcm) {
208199
goto fail_ffb;
209200
}
210201

202+
rtp_header_t *rtp_header;
203+
rtp_lhdc_media_header_t *rtp_lhdc_media_header;
211204
/* initialize RTP headers and get anchor for payload */
212205
uint8_t *rtp_payload = rtp_a2dp_init(bt.data, &rtp_header,
213-
(void **)&lhdc_media_header, sizeof(*lhdc_media_header));
206+
(void **)&rtp_lhdc_media_header, sizeof(*rtp_lhdc_media_header));
214207

215208
struct rtp_state rtp = { .synced = false };
216209
/* RTP clock frequency equal to audio sample rate */
217-
rtp_state_init(&rtp, samplerate, samplerate);
210+
rtp_state_init(&rtp, rate, rate);
218211

219212
uint8_t seq_num = 0;
220213

@@ -263,9 +256,9 @@ void *a2dp_lhdc_enc_thread(struct ba_transport_pcm *t_pcm) {
263256

264257
rtp_state_new_frame(&rtp, rtp_header);
265258

266-
lhdc_media_header->seq_num = seq_num++;
267-
lhdc_media_header->latency = 0;
268-
lhdc_media_header->frames = frames;
259+
rtp_lhdc_media_header->seq_number = seq_num++;
260+
rtp_lhdc_media_header->latency = 0;
261+
rtp_lhdc_media_header->frame_count = frames;
269262

270263
/* Try to get the number of bytes queued in the
271264
* socket output buffer. */
@@ -337,12 +330,12 @@ void *a2dp_lhdc_dec_thread(struct ba_transport_pcm *t_pcm) {
337330
const a2dp_lhdc_v3_t *configuration = &t->a2dp.configuration.lhdc_v3;
338331
const size_t sample_size = BA_TRANSPORT_PCM_FORMAT_BYTES(t_pcm->format);
339332
const unsigned int channels = t_pcm->channels;
340-
const unsigned int samplerate = t_pcm->sampling;
333+
const unsigned int rate = t_pcm->rate;
341334
const unsigned int bit_depth = get_bit_depth(configuration);
342335

343336
tLHDCV3_DEC_CONFIG dec_config = {
344337
.version = get_decoder_version(configuration),
345-
.sample_rate = samplerate,
338+
.sample_rate = rate,
346339
.bits_depth = bit_depth,
347340
};
348341

@@ -366,7 +359,7 @@ void *a2dp_lhdc_dec_thread(struct ba_transport_pcm *t_pcm) {
366359

367360
struct rtp_state rtp = { .synced = false };
368361
/* RTP clock frequency equal to audio sample rate */
369-
rtp_state_init(&rtp, samplerate, samplerate);
362+
rtp_state_init(&rtp, rate, rate);
370363

371364
debug_transport_pcm_thread_loop(t_pcm, "START");
372365
for (ba_transport_pcm_state_set_running(t_pcm);;) {
@@ -380,8 +373,8 @@ void *a2dp_lhdc_dec_thread(struct ba_transport_pcm *t_pcm) {
380373
}
381374

382375
const rtp_header_t *rtp_header = bt.data;
383-
const void *lhdc_media_header;
384-
if ((lhdc_media_header = rtp_a2dp_get_payload(rtp_header)) == NULL)
376+
const rtp_lhdc_media_header_t *rtp_lhdc_media_header;
377+
if ((rtp_lhdc_media_header = rtp_a2dp_get_payload(rtp_header)) == NULL)
385378
continue;
386379

387380
int missing_rtp_frames = 0;
@@ -392,11 +385,10 @@ void *a2dp_lhdc_dec_thread(struct ba_transport_pcm *t_pcm) {
392385
continue;
393386
}
394387

395-
const uint8_t *rtp_payload = (uint8_t *) lhdc_media_header;
388+
const uint8_t *rtp_payload = (uint8_t *)rtp_lhdc_media_header;
396389
size_t rtp_payload_len = len - (rtp_payload - (uint8_t *)bt.data);
397390

398-
uint32_t decoded = 16 * 256 * sizeof(int32_t) * channels;
399-
391+
uint32_t decoded = ffb_blen_in(&pcm);
400392
lhdcBT_dec_decode(rtp_payload, rtp_payload_len, pcm.data, &decoded, 24);
401393

402394
const size_t samples = decoded / sample_size;
@@ -442,11 +434,11 @@ static int a2dp_lhdc_configuration_select(
442434
}
443435

444436
unsigned int sampling_freq = 0;
445-
if (a2dp_lhdc_caps_foreach_sampling_freq(caps, A2DP_MAIN,
446-
a2dp_bit_mapping_foreach_get_best_sampling_freq, &sampling_freq) != -1)
437+
if (a2dp_lhdc_caps_foreach_sample_rate(caps, A2DP_MAIN,
438+
a2dp_bit_mapping_foreach_get_best_sample_rate, &sampling_freq) != -1)
447439
caps->sampling_freq = sampling_freq;
448440
else {
449-
error("LHDC: No supported sampling frequencies: %#x", saved.sampling_freq);
441+
error("LHDC: No supported sample rates: %#x", saved.sampling_freq);
450442
return errno = ENOTSUP, -1;
451443
}
452444

@@ -463,18 +455,18 @@ static int a2dp_lhdc_configuration_check(
463455
/* Validate configuration against BlueALSA capabilities. */
464456
a2dp_lhdc_caps_intersect(&conf_v, &sep->config.capabilities);
465457

466-
if (a2dp_bit_mapping_lookup(a2dp_lhdc_samplings, conf_v.sampling_freq) == -1) {
467-
debug("LHDC: Invalid sampling frequency: %#x", conf->sampling_freq);
468-
return A2DP_CHECK_ERR_SAMPLING;
458+
if (a2dp_bit_mapping_lookup(a2dp_lhdc_rates, conf_v.sampling_freq) == -1) {
459+
debug("LHDC: Invalid sample rate: %#x", conf->sampling_freq);
460+
return A2DP_CHECK_ERR_RATE;
469461
}
470462

471463
return A2DP_CHECK_OK;
472464
}
473465

474466
static int a2dp_lhdc_transport_init(struct ba_transport *t) {
475467

476-
ssize_t sampling_i;
477-
if ((sampling_i = a2dp_bit_mapping_lookup(a2dp_lhdc_samplings,
468+
ssize_t rate_i;
469+
if ((rate_i = a2dp_bit_mapping_lookup(a2dp_lhdc_rates,
478470
t->a2dp.configuration.lhdc_v3.sampling_freq)) == -1)
479471
return -1;
480472

@@ -485,10 +477,10 @@ static int a2dp_lhdc_transport_init(struct ba_transport *t) {
485477
}
486478

487479
t->a2dp.pcm.channels = 2;
488-
t->a2dp.pcm.sampling = a2dp_lhdc_samplings[sampling_i].value;
480+
t->a2dp.pcm.rate = a2dp_lhdc_rates[rate_i].value;
489481

490-
memcpy(t->a2dp.pcm.channel_map, a2dp_lhdc_channel_map_stereo,
491-
t->a2dp.pcm.channels * sizeof(*t->a2dp.pcm.channel_map));
482+
memcpy(t->a2dp.pcm.channel_map,
483+
a2dp_lhdc_channel_map_stereo, sizeof(a2dp_lhdc_channel_map_stereo));
492484

493485
return 0;
494486
}

src/rtp.h

+13
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ typedef struct rtp_mpeg_audio_header {
7373
uint16_t offset;
7474
} __attribute__ ((packed)) rtp_mpeg_audio_header_t;
7575

76+
/**
77+
* LHDC media payload header. */
78+
typedef struct rtp_lhdc_media_header {
79+
uint8_t seq_number;
80+
#if __BYTE_ORDER == __LITTLE_ENDIAN
81+
uint8_t latency:2;
82+
uint8_t frame_count:6;
83+
#else
84+
uint8_t frame_count:6;
85+
uint8_t latency:2;
86+
#endif
87+
} __attribute__ ((packed)) rtp_lhdc_media_header_t;
88+
7689
void *rtp_a2dp_init(void *s, rtp_header_t **hdr, void **phdr, size_t phdr_size);
7790
void *rtp_a2dp_get_payload(const rtp_header_t *hdr);
7891

0 commit comments

Comments
 (0)