A few things, mostly technical notes...

Thursday, January 10, 2008

How to ensure Set-Top-Box (STB) is Powered On?

.

How to ensure Set-Top-Box (STB) is Powered On?



STB - in most cases, Set Top Boxes are the boxes the Cable/DishTV companies leave at your place with a remote control for you to flip channels. STBs are external, and in case of models like mine, there appeared to be no reliable method to ensure that it is powered on (Like in case of small power disruption. The computer would probably boot back up, but what about the dumb STB?. Read more about the uncertainity here in this thread. Or, here.)

I initially proposed something naive like this (alt link here) and went on to finalize this working script.

Requisites:
You will need ffmpeg and jp2a installed. On my Ubuntu 7.10, they were installed by running:

sudo apt-get install jp2a ffmpeg 


check_stb (link)

#! /bin/bash

# Authored by Evuraan_AT_gmail_DOT_com

# ABSOLUTELY NO WARRANTY, to the extent permitted by
# applicable law.

# YMMV.
# Use at your own risk.


pvr="/dev/video0"
t="$RANDOM-$RANDOM"
out="/tmp/$t.mpg"
mark="1727"
blank="0"
say="/usr/bin/logger -p info -t mythstb-check"
POWEROFF="0"
POWERON="0"
wakeup="/usr/local/bin/irsend SEND_ONCE blaster1 POWER"
info="/usr/local/bin/irsend SEND_ONCE blaster1 DISPLAY"



send_power_on() {
$say "`date` STB seems to be turned off, last count was $blank, attempting to Power it on" &
$wakeup 1>/dev/null 2>/dev/null || :
sleep 2
}

scan_images(){
for i in `echo /tmp/$t*.jpeg`
do
if [ -s "$i" ]
then
#blank="`jp2a $i --invert --size=72x24 | sed -e 's/./& \n/g'|grep -c "M"`"
blank="`jp2a $i --invert --size=72x24 | sed 's/W/M/g' | sed -e 's/./& \n/g'|grep -c "M"`"
 rm -f "$i" 1>/dev/null 2>/dev/null || :
else
 :
fi
[ "$blank" -gt "$mark" ] && export POWERON="100"
[ "$blank" -le "$mark" ] && export POWEROFF="100"
done
if [ "$POWERON" -eq "100" -a "$POWEROFF" -ne "100" ]
then
# its off
send_power_on
else
# power is on, non blank frames
$say "`date` STB seems to be turned on, last count was $blank"
:
fi

}

purge_stuff(){
[ -f "$out" ] && rm -f "$out" 1>/dev/null >/dev/null || :
}

#ivtv-tune -c 3 -d "$pvr" 1>/dev/null 2>/dev/null &

cd /tmp
$info &

dd if="$pvr" of="$out" bs=64K count=2  1>/dev/null 2>/dev/null

if [ -s "$out" ]
then
ffmpeg -i $out -f image2 -vcodec mjpeg /tmp/$t%d.jpeg 1>/dev/null 2>/dev/null
[ -s "/tmp/${t}1.jpeg" ] && scan_images
else
$say "`date` Unable to query $pvr, as it seems to be in use."
:
fi

purge_stuff &

The Idea:
The idea is pretty simple - read from your ivtv device using dd. ffmpeg then converts them into jpeg files (1 per frame). jp2a analyses each of those frames to check whether they're blank or not.


Implementation:
I placed this in my channel change script (example ), so mythtv ensures that the STB is powered on before changing channels. You can also cron it, but it is better to call this in the beginning of your channel change script, as that way, you have a higher chance of unhindered read access to $pvr.

This can be downloaded to your machine as:
wget http://evuraan.info/evuraan/check_stb.txt  -O /tmp/check_stb

To suite specific needs/situations, one may accordingly need to change the parameters pvr, mark and wakeup.

Feedbacks are welcome.

9 comments:

new2linx said...

MAN! This looks very promising. Thanks so much for doing this. I would ask though, how long does this whole process take to take capture video, convert it to a .jpeg and check the file for an image? I would think it's not instantanous, meaning, by the time the channel changes would I be missing seconds of the show? Some shows start out right away without an intro or anything. Also, if I incorporate something like record 5 mins before the show, that messes with back to back recordings. Any comments.

evuraan said...

Thanks.

It takes just about 2 seconds to do all that.

I modified my channel change script to call check_stb before it flips the channel #, see the timings from my log entries:

Jan 10 16:00:04 mynode calling mythstb-check
Jan 10 16:00:06 mynode mythstb-check: Thu Jan 10 16:00:06 EST 2008 STB seems to be turned on.
Jan 10 16:00:06 mynode mythchannel: Thu Jan 10 16:00:06 EST 2008 coin is 18, score is 18, digit is 1
Jan 10 16:00:07 mynode mythchannel: Thu Jan 10 16:00:07 EST 2008 coin is 18, score is 18, digit is 8

new2linx said...

it looks like my stb (SA3250HD) is being turned off every sunday at sometime. I noticed it this sunday around 5 pm, it's possible that's it's even saturday night sometime because I misssed a 10am recording of Rock of love. So my question is, how ddo I incorporate your check_stb script into my channel changing script. Here is the external channel changing script that I use.
#!/bin/sh
REMOTE_NAME=SAE8000 #note, ensure that the REMOTE_NAME is the same as what you have in your /etc/ledxmitd.conf file
for digit in $(echo $1 | sed -e 's/./& /g'); do
/usr/local/lirc-ledxmit/bin/ledxmit-irsend SEND_ONCE $REMOTE_NAME $digit
sleep 0.4 # note, you may have to tweak the interdigit delay up a bit, depending on your DISH receiver model
done
/usr/local/lirc-ledxmit/bin/ledxmit-irsend SEND_ONCE $REMOTE_NAME select

thanks in advance.

new2linx said...

also, I am using Ubuntu Feisty and when I issue
sudo aptitude search jp2a
it doesn't return anything. so where can I download that program?

evuraan said...

(1) jp2a seems to be absent in ubuntu Feisty , but can be downloaded from here.

You will have to follow their instructions to install jp2a

(2) Here's my channel change script, call the check script at the very beginning - your STB needs to be turned on for you to flip channels

new2linx said...

thank you for responding however I am no bash script expert, I am not even a novice bash script person. Can you please help me edit my channel changing script to include you check_stb thingy? I don't know how to look at your channel changing script and apply the same reasoning to mine as they look totally different. i would really appreciate it.

dshay said...

Awesome! Just added this to my change channel script. In my case, I had to adjust the "MARK" value. With j2pa called without any parameters for width/height, and input of 720x480, it assumed 72 x 24 for output, so only generated 1728 marks maximum. To make the script work, I needed to set mark to 1727. I also added an explicit "width" parameter so that this would be consistent.

Anonymous said...

this script is awesome. I was having trouble at first because I am capturing thru s-video into a PVR-350 and sometimes a blank image converted with jp2a would be full of W's instead of M's so the author updated his script to accomodate this. VERY COOL SCRIPT!!!!

Anonymous said...

i am trying to use this script again but this time with a fresh install of Ubuntu Hardy Heron. I have installed all dependencies but when I run check_stb I get an error. Here they are:
/usr/local/bin/check_stb: line 71: unexpected EOF while looking for matching `"'
/usr/local/bin/check_stb: line 76: syntax error: unexpected end of file

Can you please help me again with this? I am the one you helped awhile ago at darfsten@hotmail.com or on instant messaging using screenname new2linx on AIM. Thank you so much if you could help me.

Followers


Creative Commons License
This work is licensed under a Creative Commons License.