Stream Auto Reconnect

Hi,

I have an OSMC installed on a Rasperry PI 2. I have a .strm file and I play it using the following command: kodi-send --action=“PlayMedia(”/home/osmc/stream/stream.strm")". For various reasons after some time the stream crashes and I cannot make it to auto reconnect.

I tried different things to make it auto reconnect but none of them worked as expected. I think the best option is from the video player (like --loop that VLC has) but I cannot seem to find something like that for XBMC.

Can any of you help me please ?

Thank you in advance for taking the time to help a greenhorn like myself.

Only method I know is a .pls file with repeated entries for the stream.

The issue with .pls or .m3u files is that you have to put infinite entries there. I am looking for something that in the case that: network fails, stream server goes down, power goes down, etc the player will automatically restart the stream.

I only want to have one video stream played. Only one channel.

But thank you very much for taking the time !

Probably a raspbian with omx player and a script that starts it automatically at boot and then starts another instance of it when the stream is finished and omx player shuts down.
You can make this loop infinitely quite easily. Just use an until loop in bash for example.

Pseudo code:

#!/bin/bash
until omx-player yourstreamurl do
sleep 1
done

@mcobit Will try it right now. I am downloading Raspian and installing the OMX Player. I was gonna try something different than OSMC but I didn’t knew a player that has hardware accel. I see that OMX is perfect for what I need and it’s made by Edgar Hucek from the OSMC project.

Thank you very much for your suggestion! Will update as soon as I have a result.

@mcobit Thank you again for the suggestion. I finally managed to make it work.

I will post my solution here in case others will find it useful.

So I installed the Raspbian Jessie Lite from the official site and then sudo apt-get installed omxplayer.

I created two files bash scripts:
stream.sh

#!/bin/bash

rm /tmp/stream.mjpeg
mkfifo /tmp/stream.mjpeg
wget -O /tmp/stream.mjpeg http://ip.of.your.stream &> /dev/null &
omxplayer -r --live /tmp/stream.mjpeg

stream_loop.sh

#!/bin/bash

Script=/home/pi/streams/stream.sh

ps cax | grep omxplayer.bin > /dev/null
if [ $? -eq 0 ]; then
exit
else
exec $Script
fi

After this using crontab -e I added the stream_loop.sh to run every minute.

I am very happy with the result so far, I don’t even need to make a script to run at startup since the crontab takes care of this also.

Thank you very much all for help, especially to @mcobit.

You should be able to run mjpeg streams in omxplayer without using a temp file.

@mcobit Again thank you vey much. You are right, it doesn’t :slight_smile:

I saw that the process is stoping if for example something happens with the stream server or the buffering, etc and you are returned to the shell in those cases. But there are some cases like for example you pull out your internet cable or something happens with the internet connection. In these case the process omxplayer.bin is stil alive but the stream will not be able to re-connect.

I know I abused your knowledge but do you know something for this kind of scenario ?

Thank you !

I’d suggest to check for connectivity by pinging the streamserver from time to time and kill omxplayer if it is not responding.

So I managed to get it done finally. It covers every possible situation I could of think of.
All the tests I made with the script worked very good. I chosen Python for the language of the script. The code looks like this:

import subprocess

def check_stream_server():
#check if the server has the stream up
response = subprocess.call(“nc ip.of.your.server port_number > /dev/null”, shell=True)
if response == 0:
return True
else:
return False

def check_network():
#check if we have internet connection
value_in_file = int(open("/sys/class/net/eth0/carrier").read())
if (value_in_file == 1):
return True
else:
return False

def check_pid():
#check if the process exists
is_pid = subprocess.call(“pidof omxplayer.bin > /dev/null”, shell=True)
if is_pid == 0:
return True
else:
return False

if (check_stream_server() == False and check_pid() == True):
subprocess.call(“kill $(pidof omxplayer.bin)”, shell=True)
elif (check_stream_server() == False and check_network() == True):
subprocess.call(‘echo “The Streaming Server just went DOWN.” | mail -s “From RaspBerry Streaming Server” your.email.address@provider.com’, shell=True)
elif (check_stream_server() == True and check_pid() == False):
subprocess.call(“omxplayer -r --live http://ip.of.your.sever:port”, shell=True)
else:
pass

Thank you again for your help and suggestions !

1 Like

I forgot to mention I have it croned to run every minute. I prefered this over the while loop resource wise.

I wanted to get this stream for retransmitting it on a cable network so that is the reason I needed a way to be 100% sure that the stream is working without any manual intervention.

I realised that probably this is not the best place to post this info but since I started it maybe other will benefit from it.

Again, thank you !

1 Like

This is great! It looks like exactly what I need. Thanks a million!!