{"id":992,"date":"2025-02-16T16:55:45","date_gmt":"2025-02-16T16:55:45","guid":{"rendered":"https:\/\/james-batchelor.com\/?p=992"},"modified":"2025-02-16T16:55:45","modified_gmt":"2025-02-16T16:55:45","slug":"sip-device-as-pi-audio-output","status":"publish","type":"post","link":"https:\/\/james-batchelor.com\/index.php\/2025\/02\/16\/sip-device-as-pi-audio-output\/","title":{"rendered":"SIP device as Pi Audio Output"},"content":{"rendered":"\n<p>In my Development Den (the spare room) I have a Raspberry Pi 4 setup with a monitor for use as a quick reference station when working on nearby devices.<\/p>\n\n\n\n<p>With no speakers connected this can sometimes pose an issue when trying to follw a tutorial video, and when I do need audio, a Bluetooth speaker is never around.<\/p>\n\n\n\n<p>There is a SIP phone next to the Pi on my desk, and so I thought; that has a decent network connected speaker, why not use that? <\/p>\n\n\n\n<p>This is what ensued\u2026<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2025\/01\/sipaudio_1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"734\" src=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2025\/01\/sipaudio_1.jpg\" alt=\"\" class=\"wp-image-993\"\/><\/a><\/figure>\n\n\n\n<!--more-->\n\n\n\n<p>The idea is to have a SIP endpoint running as a service on the Pi in auto-answer mode. This will allow a desk phone to dial the Pi extension and receive the Pi\u2019s audio through the loudspeaker.<\/p>\n\n\n\n<p>The Pi is running stock Raspbian Bullseye with the desktop environment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SIP Endpoint<\/h2>\n\n\n\n<p>Baresip looks a good choice for this project due to its modularity.<\/p>\n\n\n\n<p>As this will be a service running in the background, only the core module of Baresip needs to be installed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install --no-install-recommends baresip-core<\/code><\/pre>\n\n\n\n<p>When installed, run the program briefly to have it create its default configuration files:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>baresip<\/code><\/pre>\n\n\n\n<p>This will create the default template files in a .baresip directory within the home folder. We\u2019ll need to edit the accounts and config files to get it to a call answering state.<\/p>\n\n\n\n<p>Starting with the accounts file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nano .baresip\/accounts<\/code><\/pre>\n\n\n\n<p>Add the following line to the bottom of the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;sip:{endpoint}@{sip_server}&gt;;auth_pass={sip_secret};answermode=auto<\/code><\/pre>\n\n\n\n<p>Where:<br>{endpoint} \u2013 SIP extension number<br>{sip_server} \u2013 IP\/hostname of the PBX<br>{sip_secret} \u2013 Extension password<\/p>\n\n\n\n<p>Answermode flag has been added to allow calls to this extension to be answered automatically.<\/p>\n\n\n\n<p>After the accounts file, move onto the config:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nano .baresip\/config<\/code><\/pre>\n\n\n\n<p>This file can be left as default, however a few quality-of-life improvements will be made\u2026<\/p>\n\n\n\n<p>Uncomment the following lines:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; opus.so<br>module&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g722.so<\/code><\/pre>\n\n\n\n<p>These allow the use of the higher quality codecs commonly in use; g722 is the elder and while it offers higher quality from a phone call audio point of view, may fall short for music. Opus is the newer and can be configured for excellent quality overall, but being newer may not be an available choice on older phones.<\/p>\n\n\n\n<p>If Opus is available, the bitrate can be increased via this line further down the config file (higher bitrate, higher audio quality):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>opus_bitrate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 28000 # 6000-510000<\/code><\/pre>\n\n\n\n<p>Make sure both you SIP devices and PBX are capable and configured to offer these codecs at the highest priority.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing<\/h2>\n\n\n\n<p>Good time for a sanity check, for this an audio file or stream playing through VLC, or any source of audio will do.<\/p>\n\n\n\n<p>Run the application in the terminal<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>baresip<\/code><\/pre>\n\n\n\n<p>And make a call to its extension, you should see output in the terminal, and hear the audio through the phone.<\/p>\n\n\n\n<p>If it\u2019s successful, a service can be created to have this running in the background on startup\u2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create Service<\/h2>\n\n\n\n<p>To allow baresip to start at boot, its best to create a service for it and restart it if it ever stops.<\/p>\n\n\n\n<p>Create and edit a service file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/systemd\/system\/sipaudio.service<\/code><\/pre>\n\n\n\n<p>Add the following to this file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Unit]\nDescription=SIP endpoint for Pi audio\nAfter=sound.target network.target\n\n&#91;Service]\nExecStart=\/usr\/bin\/baresip\nRestart=always\nUser=pi\nWorkingDirectory=\/home\/{username}\nStandardOutput=journal\nStandardError=journal\nEnvironment=HOME=\/home\/{username}\nEnvironment=XDG_RUNTIME_DIR=\/run\/user\/1000\n\n&#91;Install]\nWantedBy=multi-user.target\n<\/code><\/pre>\n\n\n\n<p>When saved, reload services:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl daemon-reload<\/code><\/pre>\n\n\n\n<p>Start the service and enable it to start at boot:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl enable --now sipaudio<\/code><\/pre>\n\n\n\n<p>With some audio playing, try another call to make sure its answering and picking up audio from the desktop environment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>It\u2019s a niche solution for those who have an audio-less Pi and a SIP phone next to it, but the results are plesently convenient for those rare times when audio is needed.<\/p>\n\n\n\n<p>My accompanying desk phone (Yealink T46S) only offers g722 has the higher codec, but still is perfectly fine for speech output and fine (not great) for audio. I\u2019m sure using Opus at the higher bitrate will put it on par with some of the streaming services. Afterall, YouTube uses opus as audio for its videos, as noted by the &#8220;stats for nerds&#8221; section:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2025\/02\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1801\" height=\"630\" src=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2025\/02\/image.png\" alt=\"\" class=\"wp-image-1001\"\/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In my Development Den (the spare room) I have a Raspberry Pi 4 setup with a monitor for use as a quick reference station when working on nearby devices. With no speakers connected this can sometimes pose an issue when trying to follw a tutorial video, and when I do need audio, a Bluetooth speaker &hellip; <a href=\"https:\/\/james-batchelor.com\/index.php\/2025\/02\/16\/sip-device-as-pi-audio-output\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;SIP device as Pi Audio Output&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,247],"tags":[252,410,63,70,254,408,409],"class_list":["post-992","post","type-post","status-publish","format-standard","hentry","category-raspberry-pi","category-voip","tag-asterisk","tag-baresip","tag-raspberry-pi","tag-rpi","tag-sip","tag-sip-audio","tag-sip-spearker"],"_links":{"self":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts\/992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/comments?post=992"}],"version-history":[{"count":4,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts\/992\/revisions"}],"predecessor-version":[{"id":1002,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts\/992\/revisions\/1002"}],"wp:attachment":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/media?parent=992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/categories?post=992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/tags?post=992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}