From 7d191b64d978483839cee8ca2176ac6970b1c3f3 Mon Sep 17 00:00:00 2001 From: "Igor V. Kovalenko" Date: Tue, 2 Mar 2021 15:00:04 +0300 Subject: [PATCH] bluetooth: mSBC: ignore all-zero packets This is a workaround for hardware/driver which inserts all-zero packets in what otherwise looks like a valid mSBC stream. Part-of: --- src/modules/bluetooth/bt-codec-msbc.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/modules/bluetooth/bt-codec-msbc.c b/src/modules/bluetooth/bt-codec-msbc.c index af99a4774..107e0673f 100644 --- a/src/modules/bluetooth/bt-codec-msbc.c +++ b/src/modules/bluetooth/bt-codec-msbc.c @@ -182,6 +182,16 @@ static size_t encode_buffer(void *codec_info, uint32_t timestamp, const uint8_t return MSBC_PACKET_SIZE; } +static inline bool is_all_zero(const uint8_t *ptr, size_t len) { + size_t i; + + for (i = 0; i < len; ++i) + if (ptr[i] != 0) + return false; + + return true; +} + /* * We build a msbc frame up in the sbc_info buffer until we have a whole one */ @@ -191,6 +201,12 @@ static struct msbc_frame *msbc_find_frame(struct sbc_info *si, ssize_t *len, int i; uint8_t *p = si->input_buffer; + /* skip input if it has all zero bytes + * this could happen with older kernels inserting all-zero blocks + * inside otherwise valid mSBC stream */ + if (*len > 0 && is_all_zero(buf, *len)) + *len = 0; + for (i = 0; i < *len; i++) { union msbc_h2_id1 id1;