Thursday, September 2, 2021

Using chan_mobile with Raspberry Pi bluetooth controller

This post addresses fixing of audio issues when using chan_mobile with the built-in bluetooth controller of Raspberry Pi 4 or 3B+ (using CYW43455 chipset aka BCM43455). Audio coming in to Asterisk through the mobile sounds distorted with a tinny quality while the audio going out from Asterisk to the mobile device has no issues. The problem is due to frame size being set in chan_mobile code as 48 bytes as seen here

https://github.com/asterisk/asterisk/blob/master/addons/chan_mobile.c#L81

while bluetooth controller actually uses a frame size of 60 bytes for audio sco packets. The idea behind solution was obtained from gleaning this thread

https://github.com/raspberrypi/linux/issues/2229

Steps to resolve the issue

Stop Asterisk service and edit chan_mobile.c found in the source asterisk addons folder. The line #define DEVICE_FRAME_SIZE 48 should be changed to #define DEVICE_FRAME_SIZE 60. Then rerun 'sudo make' and 'sudo make install'. Restart Astersik service and the audio issue should be fixed.

The issue has been well described here

https://issues.asterisk.org/jira/browse/ASTERISK-29606

Hardware and Software Used: Asterisk 16.19 (Current v16), Ubuntu 20.04 on Raspberry Pi 4

It may also work with Pi 3 with the CYW43438 (BCM43438) chip but this is untested. For older firmware, if there is no audio at all Pi 3 might require running this command 

sudo hcitool cmd 0x3F 0x01C 0x01 0x02 0x00 0x01 0x01

to enable audio through HCI. Then fix given in post can be applied. You can let us know if it works for Pi 3 in comments below.

10 comments:

  1. Hello! Is it only for raspberry pi or for all bluetooth devices it will be the same issue? I'm asking because I'm planning to use an external bluetooth dongle, with something other than a raspberry pi.

    ReplyDelete
  2. This of for Pi and dongles with same or like chipsets. If buying a dongle, better to buy one which does not require the mod, many of the CSR chipset dongles with BT4 work well.

    ReplyDelete
  3. Thank you so much for your answer! I really appreciate it. Do you have any suggestions which might be the best bluetooth dongles compatible with linux?

    ReplyDelete
  4. Glad to be of help! I'd purchased an Orico BT4 CSR based dongle from aliexpress which worked out of the box.

    ReplyDelete
  5. You're a genius and a life saver, I really appreciate your kindness, your responsiveness and your help. I just have a couple of other last questions, I hope it doesn't bother you:
    1 - Is it compatible with bluez like bluetoothctl? I don't know if I said it right, I'm a beginner
    2 - and in case I use this Orico BT4 CSR, do I need to install something on the linux server or it's just plug and play?
    3 - and with this dongle do I need to change the settings of the chan_mobile.c or it's not needed? and if I change them, do they affect the voice or it's okay in both cases?
    Sorry to bother you with all these questions, they're my last questions haha, my name is Julia by the way, thank you for your help

    ReplyDelete
  6. Hey, no problem, you're welcome.
    1 - Yes, compatible with bluez, in fact needs bluez to be installed and better to pair using bluetoothctl before using in asterisk
    2 - Plug and play, needs bluez or other such lib
    3 - With right csr chipset, no mod is required. I'd tested this long before, I'm not too sure but think with mod when not required, it may be marginally worse.

    ReplyDelete
  7. Thank you so much! Just to be reassured, is this the bluetooth dongle that you advised?

    (https://fr.aliexpress.com/item/32384484446.html?spm=a2g0o.productlist.main.17.41de2534G0jREK&algo_pvid=c53f33d9-57ce-4386-9a2d-0e4bd167b8a4&algo_exp_id=c53f33d9-57ce-4386-9a2d-0e4bd167b8a4-8&pdp_ext_f=%7B%22sku_id%22%3A%2212000024036782844%22%7D&pdp_npi=3%40dis%21EUR%219.65%215.31%21%21%21%21%21%40214527c616757778571591387d0702%2112000024036782844%21sea%21FR%210&curPageLogUid=aXcL4tlYqt8u)

    Sorry for adding a long AliExpress link.

    If we want to use the same type of raspberry pi bluetooth and make this change in the chan_mobile.c, is there any dongle that has similar chipset that is also compatible with linux?

    And one last question not related to this subject, since you're an expert in these things, do you have any article on your website that explains how to create a web page that can monitor the channels of asterisk? I mean the CEL? Showing when a channel is dialing, ringing, or answering a call or disconnected for example? I don't know if we must use Asterisk AMI for that.

    Thanks a lot in advance

    ReplyDelete
  8. The model 1 on that listing looks just like the one I'd bought and they do mention the csr chipset. The BCM43455/CYW43455 used in Pi is an integrated wifi and bluetooth chipset. Sorry, not an expert an asterisk, no articles on this site for live CEL embedding. I think a web search would yield examples to get that using AGI or AMI, If you don't wish to use a front end like freepbx which has a CEL report.

    ReplyDelete
  9. Hey, it's me again, I tried this one (https://www.asus.com/networking-iot-servers/adapters/all-series/usbbt400/) while waiting for the one to come from AliExpress, and I tried to put back the settings in the chan_mobile.c to 48, but there was completely no voice from both sides. is there any changes I should make or this bluetooth device is not a good one?
    I used the below rtp.conf settings, I don't know if it's even related:
    rtpstart=5000
    rtpend=31000
    ;rtpchecksums=no
    ;dtmftimeout=3000
    ;rtcpinterval = 5000
    ; strictrtp=yes
    ; icesupport = true
    ;stunaddr=stun.l.google.com:19302

    I really need your help in this, thank you so much in advance

    ReplyDelete
  10. That has the Broadcom BCM20702 chip, don't think you'll be able to get that to work. Look for a BT4 adapter with CSR8510 (CSR8510 A10), they are quite common and inexpensive.

    ReplyDelete

Comments relevant to post and beneficial for others will be published