How to stream audio from OSMC to Sonos One speaker

I’m trying to play videos from OSMC and have the video output to a projector via hdmi and have the audio play on a Sonos One speaker, which unfortunately does not support Bluetooth and doesn’t have a line in jack.
I’ve searched / tried a few things but haven’t found a solution.

Saw this bit on airplay in the OSMC docs, but it just describes airplaying TO OSMC, not airplaying FROM OSMC.

Tried this tutorial which caused OSMC to crash.

Asked about a utility called AirConnect and was told that audio / video synchronization was not possible.

Any ideas?

The Sonos wifi streaming is pretty much a closed ecosystem. Unfortunately I think the only thing you could do is get a Sonos Port and plug the OSMC device into that. No guarantees you’d ever get the video and audio to sync though.

And at $450 US that seems like a very expensive option.

1 Like

Oof, yeah, trying not to drop that kind of cash for this.

Well, the sonos one supports AirPlay, and the tutorial I linked describes setting up the raspberry pi to stream audio to a local url which you then set as a radio station for the sonos to pick up, and it also mentions adjusting the audio / video synchronization. I just can’t get the configuration steps described in that tutorial to work without it causing OSMC to fail to boot.

I’ve been playing with this. Installed icecast2 and darkice. Not using ezstream or avconv since darkice does what they do between them. I didn’t mess with the alsa config yet. It seems that’s only necessary to send sound to the default sound device and to icecast at the same time. I recommend you get rid of your .asoundrc.

My /etc/darkice.cfg:

# sample DarkIce configuration file, edit for your needs before using
# see the darkice.cfg man page for details

# this section describes general aspects of the live streaming session
[general]
duration        = 0        # duration of encoding, in seconds. 0 means forever
bufferSecs      = 1         # size of internal slip buffer, in seconds
reconnect       = yes       # reconnect to the server(s) if disconnected
realtime        = yes       # run the encoder with POSIX realtime priority
rtprio          = 3         # scheduling priority for the realtime threads

# this section describes the audio input that will be streamed
[input]
device          = plughw:2,1    # Loopback
sampleRate      = 48000     # sample rate in Hz. try 11025, 22050 or 44100
bitsPerSample   = 16        # bits per sample. try 16
channel         = 2         # channels. 1 = mono, 2 = stereo

# this section describes a streaming connection to an IceCast2 server
# there may be up to 8 of these sections, named [icecast2-0] ... [icecast2-7]
# these can be mixed with [icecast-x] and [shoutcast-x] sections
[icecast2-0]
bitrateMode     = vbr       # average bit rate
format          = mp3       # format of the stream: ogg vorbis
bitrate         = 96        # bitrate of the stream sent to the server
quality         = 0.5
server          = localhost
                            # host name of the server
port            = 8000      # port of the IceCast2 server, usually 8000
password        = icing     # source password to the IceCast2 server
mountPoint      = audio.mpg # mount point of this stream on the IceCast2 server
name            = DarkIce trial
                            # name of the stream
description     = This is only a trial
                            # description of the stream
genre           = my own    # genre of the stream
public          = yes       # advertise this stream?
localDumpFile   = dump.mp3  # local dump file

It looks like darkice looks in /etc/ for darkice.cfg. It’s started the old-fashioned way with init.d. Icecast2 uses systemd. A re-boot should start them both up.

You need to enable alsa’s aloop device sudo modprobe snd-aloop then set Kodi to send sound to Loopback. Kodi needs to be (re)started for it to recognise the Loopback device (use Power->Exit). (see below)

I don’t have a Sonos so testing playing http://<server_ip_address>:8000/audio.mpg with HTML5. Even with a 1 second buffer in darkice, the audio comes out of the browser 5 or more seconds delayed. No chance of syncing it with video. Maybe your Sonos will do better.

HTH

1 Like

Thanks @grahamh. Very helpful.

I followed all this exactly but am not getting anything to play in the audio stream. OSMC logs at https://paste.osmc.tv/hovahoyiti

I had removed .asoundrc earlier.

I created an html page with an audio player using the following code

<audio controls src="http://192.168.0.1:8000/audio.mpg" id="audio"></audio>

and got this console error in Chrome when I tried to play it.

GET http://192.168.0.1:8000/audio.mpg net::ERR_CONNECTION_TIMED_OUT

I wasn’t sure what to use for the server IP address, so the above is my router’s IP address.

I also tried the IP address I use to SSH into my Raspberry Pi (it’s a static IP) - set the src to http://192.168.000.094:8000/audio.mpg and got the same error.

I then tried http://localhost:8000/audio.mpg and got a slightly different error in the console: Failed to load resource: net::ERR_CONNECTION_REFUSED.

In the step of “set Kodi to send sound to Loopback”, I saw two options containing “loopback” under “audio output device”. I tried each of them one by one but got the same error both times. The audio stopped playing to the projector through HDMI when I changed it, so something happened…

I wasn’t quite sure of the sequence of steps I should do when you said

but I did sudo modprobe snd-aloop then sudo shutdown -r now then sudo modprobe snd-aloop again and changed the audio settings from within the UI.

Any ideas?

I can’t see in that log where you actually attempt to play a stream. The audio.mpg thingy won’t be there unless you start to play something (or you set audio keep device alive to always). With no stream, you should still be able to navigate to http://192.168.0.94:8000/admin to check the server is working.

You need to use the IP address of the RPi. As far as darkice and icecast are concerned, the server is at localhost, but from another device use the RPi’s IP which is the same as you use for ssh.

That’s a bug which I thought we’d fixed already but will be fixed in the next upgrade. Use the @ device.

You did get the Loopback device showing up in Kodi so it should be OK. What I meant was do the modprobe then restart Kodi only (ie don’t reboot/shutdown). You can do that by

  • clicking Power->Exit in the GUI or
  • sudo systemctl restart mediacenter in the commandline

I haven’t actually tested this on Pi, only Vero, and I get 48000 as the only available samplerate. You have a range

2020-08-16 20:10:13.086 T:1884287712  NOTICE:     Device 3
2020-08-16 20:10:13.086 T:1884287712  NOTICE:         m_deviceName      : @:CARD=Loopback,DEV=0
2020-08-16 20:10:13.086 T:1884287712  NOTICE:         m_displayName     : Loopback (@:CARD=Loopback,DEV=0)
2020-08-16 20:10:13.086 T:1884287712  NOTICE:         m_displayNameExtra: Loopback PCM
2020-08-16 20:10:13.087 T:1884287712  NOTICE:         m_deviceType      : AE_DEVTYPE_PCM
2020-08-16 20:10:13.087 T:1884287712  NOTICE:         m_channels        : FL, FR, BL, BR, FC, LFE, SL, SR, UNKNOWN1, UNKNOWN2, UNKNOWN3, UNKNOWN4, UNKNOWN5, UNKNOWN6, UNKNOWN7, UNKNOWN8
2020-08-16 20:10:13.087 T:1884287712  NOTICE:         m_sampleRates     : 8000,11025,16000,22050,32000,44100,48000,64000,88200,96000,176400,192000
2020-08-16 20:10:13.087 T:1884287712  NOTICE:         m_dataFormats     : AE_FMT_S32NE,AE_FMT_S16NE,AE_FMT_S16LE,AE_FMT_S16BE
2020-08-16 20:10:13.087 T:1884287712  NOTICE:         m_streamTypes     : No passthrough capabilities

When I get time, I’ll set this up on a Pi and see if you need a different setup.

Another thing - I see the guy in the howto you linked seems to be using two Sonoses in stereo and that’s the reason for his .asoundrc gymnastics.

1 Like

Okay, thanks for the clarifications. I set keep device alive to always and selected the loopback audio device with the “@” in it.


And then set up the YouTube add-on with an API key to play a Bob Ross video for good measure.

Went to http://192.168.0.094:8000/admin and got

404 - The file you requested could not be found

Also, thanks for the clarification on IP address. I’ve set my HTML on the test page to use
<audio controls src="http://192.168.000.094:8000/audio.mpg" id="audio"></audio>

And I’m still getting this console error in Chrome, even while playing a video from the Pi:

Failed to load resource: net::ERR_CONNECTION_TIMED_OUT

Logs: https://paste.osmc.tv/acoyuluxom

Here’s my icecast.xml with the passwords redacted: http://paste.osmc.tv/adagedobuv.xml

I can’t see anything different between your icecast.xml and mine.

what does systemctl status icecast2 say?

Update: I can’t get it work here now. The server is running and I get the admin screen but no mountpoints are listed and I’ve no idea how to debug that.

Just realised I gave you this:

# this section describes the audio input that will be streamed
[input]
device          = plughw:2,1    # Loopback

The device should be plughw:1,1 or you could try plughw:CARD="Loopback",DEV=1

Update2: I see you have pulseaudio installed. Is there a reason for that or was it part of the attempt to connect to Sonos?

osmc@osmc:~$ systemctl status icecast2
icecast2.service - LSB: Icecast2 streaming media server
Loaded: loaded (/etc/init.d/icecast2; generated; vendor preset: enabled)
Active: active (running) since Mon 2020-08-17 20:26:02 EDT; 9min ago
Docs: man:systemd-sysv-generator(8)
Process: 354 ExecStart=/etc/init.d/icecast2 start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/icecast2.service
└─477 /usr/bin/icecast2 -b -c /etc/icecast2/icecast.xml

Aug 17 20:26:00 osmc systemd[1]: Starting LSB: Icecast2 streaming media server...
Aug 17 20:26:02 osmc icecast2[354]: Starting streaming media server: icecast2[2020-08-17  20:26:02] WARN CONFIG/_parse_root Warning, <hostname> not configured, using default value "localhost". This will cause problems, e.g. with YP directory listings.
Aug 17 20:26:02 osmc icecast2[354]: [2020-08-17  20:26:02] WARN CONFIG/_parse_root Warning, <location> not configured, using default value "Earth".
Aug 17 20:26:02 osmc icecast2[354]: [2020-08-17  20:26:02] WARN CONFIG/_parse_root Warning, <admin> contact not configured, using default value "icemaster@localhost".
Aug 17 20:26:02 osmc icecast2[354]: .
Aug 17 20:26:02 osmc systemd[1]: Started LSB: Icecast2 streaming media server.
osmc@osmc:~$ 

Okay, I changed that device line in darkice.cfg to plughw:1,1.

Not sure why pulseaudio is installed. I have very little idea what I’m doing here :wink: but yes, that was probably left over from some other attempt to connect to the Sonos. I removed it with sudo apt remove pulseaudio and then restarted. Did sudo modprobe snd-aloop and sudo systemctl restart mediacenter. Checked audio settings. Keep device alive was set to always and the Audio Output Device was set to the loopback device showing the @ when I got into the settings.

Result: Same situation as before while playing a video in YouTube from within OSMC.

  1. Logs
  2. http://192.168.0.094:8000/admin shows 404 - The file you requested could not be found
  3. <audio controls src="http://192.168.000.094:8000/audio.mpg" id="audio"></audio> in the webpage I built to test it shows the console error Failed to load resource: net::ERR_CONNECTION_TIMED_OUT

===
Attempt 2:

Changed the device setting in darkice.cfg to plughw:CARD="Loopback",DEV=1 and sudo shutdown -r now.

osmc@osmc:~$ sudo modprobe snd-aloop
osmc@osmc:~$ sudo systemctl restart mediacenter

Started a video again. Same result on all points.

Updated logs

osmc@osmc:~$ systemctl status icecast2
icecast2.service - LSB: Icecast2 streaming media server
Loaded: loaded (/etc/init.d/icecast2; generated; vendor preset: enabled)
Active: active (running) since Mon 2020-08-17 20:55:28 EDT; 6min ago
Docs: man:systemd-sysv-generator(8)
Process: 345 ExecStart=/etc/init.d/icecast2 start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/icecast2.service
└─453 /usr/bin/icecast2 -b -c /etc/icecast2/icecast.xml

Aug 17 20:55:27 osmc systemd[1]: Starting LSB: Icecast2 streaming media server…
Aug 17 20:55:28 osmc icecast2[345]: Starting streaming media server: icecast2.
Aug 17 20:55:28 osmc systemd[1]: Started LSB: Icecast2 streaming media server.
Aug 17 20:55:28 osmc icecast2[345]: [2020-08-17 20:55:28] WARN CONFIG/_parse_root Warning, not configured, using default value “localhost”. This will
Aug 17 20:55:28 osmc icecast2[345]: [2020-08-17 20:55:28] WARN CONFIG/_parse_root Warning, not configured, using default value “Earth”.
Aug 17 20:55:28 osmc icecast2[345]: [2020-08-17 20:55:28] WARN CONFIG/_parse_root Warning, contact not configured, using default value "icemaster@localho
lines 1-14/14 (END)

This might not be significant but you probably want to remove the leading zeroes in the IP addresses. They do seem to cause a few problems:

osmc@osmc:~$ ping 192.168.008.009
ping: bad address '192.168.008.009'
osmc@osmc:~$ wget http://192.168.008.009
--2020-08-18 07:06:02--  http://192.168.008.009/
failed: Name or service not known.
wget: unable to resolve host address ‘192.168.008.009’
osmc@osmc:~$ wget http://192.168.8.9
--2020-08-18 07:06:17--  http://192.168.8.9/
Connecting to 192.168.8.9:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10701 (10K) [text/html]
Saving to: ‘index.html.1’

index.html.1                               100%[=====================================================================================>]  10.45K  --.-KB/s    in 0s      

2020-08-18 07:06:17 (43.8 MB/s) - ‘index.html.1’ saved [10701/10701]

We’re all learning on this one. This morning I managed to get my installation working again. Here is what I’ve found, albeit on vero, not Pi:

  • it’s not necessary to re-start Kodi after starting snd-aloop
  • it is necessary to start darkice after starting snd-aloop
  • to confirm icecast is working, go to 192.168.0.94:8000 (ie not /admin). You will get a login prompt - use admin and your password

To start darkice, sudo darkice start & and press enter twice.

In case you’re wondering, I also get those WARN CONFIG messages but it still works.

I can confirm plughw:CARD=Loopback,DEV=1 works for me (with or without quotes round Loopback). It should be safer using that than plughw:1,1 if there are any other ALSA devices in your setup.

1 Like

@mrengy I guess you got it to work, then. Can you let us know if you can get the audio to sync with your projector?

Yeah, I was just testing it a bit.

On startup, I did the following from the Terminal:
sudo modprobe snd-aloop
sudo darkice start &
[press enter twice]

I got the audio to play on my Sonos One, by (in the desktop Sonos Controller app) going to “TuneIn” > “My Radio Stations” > “Add new radio station” and setting it to http://192.168.0.94:8000/audio.mpg . Removing the leading zeros was important here - with them included, I was getting an error in the Sonos app.

The video was about 10 seconds ahead of the audio initially. I adjusted the audio settings in the OSMC interface by pressing enter, then going to the speaker icon on the player, and choosing “audio offset”.

When I set audio ahead greater than 7.25 seconds, the video and audio start and stop constantly every fraction of a second. I also saw this in the terminal:

AlsaDspSource :: Buffer overrun!

So it’s still a bit off at the best it seems to be able to do this way.

Is there another way (maybe in Icecast or Darkice) to set the audio offset that may be less resource intensive and allow a further offset without causing the audio and video to stutter?

I don’t think anyone at Kodi thought AV sync was ever going to be that far off. I was hoping most of that audio delay was in my browser, but it seems the Sonos is just as bad.

I would have to do some digging to see if the buffer they talking about can be made bigger or exactly where this huge delay is coming from. You might like to have a poke around in the icecast config file to see if anything there can improve it.