Skip to content

Commit

Permalink
Improve function for mSBC codec initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Dec 25, 2023
1 parent 5f3eeef commit a960a94
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
52 changes: 37 additions & 15 deletions src/codec-msbc.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* BlueALSA - codec-msbc.c
* Copyright (c) 2016-2022 Arkadiusz Bokowy
* Copyright (c) 2016-2023 Arkadiusz Bokowy
* Copyright (c) 2017 Juha Kuikka
*
* This file is a part of bluez-alsa.
Expand All @@ -10,7 +10,10 @@
*/

#include "codec-msbc.h"
/* IWYU pragma: no_include "config.h" */

#if HAVE_CONFIG_H
# include <config.h>
#endif

#include <endian.h>
#include <errno.h>
Expand Down Expand Up @@ -75,24 +78,33 @@ static void *msbc_find_h2_header(const void *data, size_t *len) {
return ptr;
}

/**
* Initialize mSBC codec structure.
*
* This function is idempotent, so it can be called multiple times in order
* to reinitialize the codec structure.
*
* @param msbc Codec structure which shall be initialized.
* @return This function returns 0 on success or a negative error value
* in case of initialization failure. */
int msbc_init(struct esco_msbc *msbc) {

int err;

if (!msbc->initialized) {
debug("Initializing mSBC codec");
if ((errno = -sbc_init_msbc(&msbc->sbc, 0)) != 0)
goto fail;
if (ffb_init_uint8_t(&msbc->data, sizeof(esco_msbc_frame_t) * 3) == -1)
goto fail;
goto fail_init;
/* Allocate buffer for 1 decoded frame, optional 3 PLC frames and
* some extra frames to account for async PCM samples reading. */
if (ffb_init_int16_t(&msbc->pcm, MSBC_CODESAMPLES * 6) == -1)
goto fail;
goto fail_init;
if ((errno = -sbc_init_msbc(&msbc->sbc, 0)) != 0)
goto fail_init;
}
else {
debug("Re-initializing mSBC codec");
if ((errno = -sbc_reinit_msbc(&msbc->sbc, 0)) != 0)
goto fail_init;
}

if ((errno = -sbc_reinit_msbc(&msbc->sbc, 0)) != 0)
return -1;

/* ensure libsbc uses little-endian PCM on all architectures */
msbc->sbc.endian = SBC_LE;
Expand Down Expand Up @@ -127,10 +139,11 @@ int msbc_init(struct esco_msbc *msbc) {
return 0;

fail:
err = errno;
msbc_finish(msbc);
errno = err;
return -1;
sbc_finish(&msbc->sbc);
fail_init:
ffb_free(&msbc->data);
ffb_free(&msbc->pcm);
return -errno;
}

void msbc_finish(struct esco_msbc *msbc) {
Expand Down Expand Up @@ -266,3 +279,12 @@ ssize_t msbc_encode(struct esco_msbc *msbc) {

return sizeof(*frame);
}

/**
* Get string representation of the mSBC encode/decode error.
*
* @param err The encode/decode error code.
* @return Human-readable string. */
const char *msbc_strerror(int err) {
return sbc_strerror(err);
}
4 changes: 3 additions & 1 deletion src/codec-msbc.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* BlueALSA - codec-msbc.h
* Copyright (c) 2016-2022 Arkadiusz Bokowy
* Copyright (c) 2016-2023 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
*
Expand Down Expand Up @@ -79,4 +79,6 @@ void msbc_finish(struct esco_msbc *msbc);
ssize_t msbc_decode(struct esco_msbc *msbc);
ssize_t msbc_encode(struct esco_msbc *msbc);

const char *msbc_strerror(int err);

#endif
13 changes: 5 additions & 8 deletions src/sco.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@
#include "ba-transport-pcm.h"
#include "bluealsa-config.h"
#include "bluealsa-dbus.h"
#if ENABLE_MSBC
# include "codec-msbc.h"
#endif
#include "codec-sbc.h"
#include "codec-msbc.h"
#include "hci.h"
#include "hfp.h"
#include "io.h"
Expand Down Expand Up @@ -379,7 +376,7 @@ static void *sco_msbc_enc_thread(struct ba_transport_pcm *t_pcm) {
struct esco_msbc msbc = { .initialized = false };
pthread_cleanup_push(PTHREAD_CLEANUP(msbc_finish), &msbc);

if (msbc_init(&msbc) != 0) {
if ((errno = -msbc_init(&msbc)) != 0) {
error("Couldn't initialize mSBC codec: %s", strerror(errno));
goto fail_msbc;
}
Expand Down Expand Up @@ -408,7 +405,7 @@ static void *sco_msbc_enc_thread(struct ba_transport_pcm *t_pcm) {

int err;
if ((err = msbc_encode(&msbc)) < 0) {
error("mSBC encoding error: %s", sbc_strerror(err));
error("mSBC encoding error: %s", msbc_strerror(err));
break;
}

Expand Down Expand Up @@ -465,7 +462,7 @@ static void *sco_msbc_dec_thread(struct ba_transport_pcm *t_pcm) {
struct esco_msbc msbc = { .initialized = false };
pthread_cleanup_push(PTHREAD_CLEANUP(msbc_finish), &msbc);

if (msbc_init(&msbc) != 0) {
if ((errno = -msbc_init(&msbc)) != 0) {
error("Couldn't initialize mSBC codec: %s", strerror(errno));
goto fail_msbc;
}
Expand All @@ -492,7 +489,7 @@ static void *sco_msbc_dec_thread(struct ba_transport_pcm *t_pcm) {
while ((err = msbc_decode(&msbc)) > 0)
continue;
if (err < 0) {
error("mSBC decoding error: %s", sbc_strerror(err));
error("mSBC decoding error: %s", msbc_strerror(err));
continue;
}

Expand Down

0 comments on commit a960a94

Please sign in to comment.