Firewire Priming
From MythTV
Contents |
Introduction
Below are two firewire scripts which change channels and prime the firewire before MythTV starts to record. The first is for a one tuner setup and the second is a more flexible two tuner setup.
Both require the 6200ch helper program:
Required 6200ch change channel script
You also need the 6200ch change channel script which you can compile in the /usr/share/doc/mythtv-0.20/contrib/channel_changers directory (read the README). Don't forget you will need to get the headers for the kernel (yum install kernel-devel). On RedHat Fedora 5, you will also need the libavc1394-devel package, (yum install libavc1394-devel).
- Update: the sa3250hd primer doesn't need 6200ch but does look for jwestfall's firewire_tester script which should be located in the contrib directory. That section can be commented out if you don't have it though.
- Update: there also exists a sa3250ch channel changer program. It is located in the contrib directory as well. It may require some hand tuning as there appears to be two ways to change the channel depending on the vendor ID in your hardware. For now you have to hand tweak the source based on the diffs floating around. I've found that changing the channel to channel 1 (TWC NYC) before priming or when the backend loses the cable box fixes everything. I'll detail the procedure as it has my setup running rock solid.
One Tuner Priming Script
This is a script which is invoked each time a channel change is requested and a firewire connection needs to be working. It first tests the existing p2p and channel settings and if OK, test to see if the result from test-mpeg2 is a non-zero file which means the firewire connection is working. If the result is negative for any of the p2p, channel and non-zero file tests, it then tries to use plugctl to reset the firewire connection, and then runs the test-mpeg2 test again. It repeats ten times and if it gets 10 non-zero results, it fails.
Note:
- It relies on the 6200ch change channel script.
- It assumes the firewire connection is on node 1 and p2p routing (not bcast).
- If your node is not 1, be sure to add the correct node in front of the channel change command. IE: '/home/6200ch -n2 $1' rather than '/home/6200ch $1'
- If you run mythbackend with a log file, i.e. "mythbackend >> /home/mythbackend_log.log &", the echo statements below will appear in the log which is useful for debugging.
#!/bin/bash
# v1.02, a Firewire change channel and test script by Sean Donovan, techonfoot.com
# Released under GPL, 2006
# Revision history
# v1.02 - Fixed the unnecessary kill_test.sh script
# v1.01 - Added detection for conditions that work intermittently, i.e. test-mpeg2 will work
# but the next capture by MythTV won't. Usually when p2p is 0 or channel <> 63.
# Fixed a bug whereby the script expects the kill_test.sh script in the /home directory.
# v1.00 - Genesis
#
# ------------------------
# Prime the variables used to exit the while loop and set error messages
# C2 is used to avoid the while loop or exit it, if the firewire is primed
# C3 is used to notify either an error condition (=1) or not (=0)
C2=0
C3=1
# Change the channel
/home/6200ch $1
# Then change into the directory with the kill_test.sh script
cd /home
# First check to make sure the firewire port is set up correctly or the
# "Is it primed?" test could report positive but the firewire is in a failing state.
T1=`plugctl -n 1 opcr[0].n_p2p_connections`
T2=`plugctl -n 1 opcr[0].channel`
if [ $T1 != "1" ] ; then
# Oops, p2p isn't one, let it go into the while loop for reset.
echo "###### Oops, p2p isn't one! Going to reset Firewire"
elif [ $T2 != "63" ] ; then
# Oops, the channel isn't 63
echo "###### Oops, the channel isn't 63! Going to reset Firewire"
else
# Now check if the firewire is primed already, then do nothing:
test-mpeg2 -r 1 > testcap.ts &
sleep 1
killall test-mpeg2
if [ -s testcap.ts ] ; then
echo "#################"
echo "# Firewire is primed!!!!!!!!!!!!"
echo "################"
# No error and
C3=0
# Don't enter the while loop, the firewire is working already...
C2=11
fi
fi
# While the counter is less then or equal to 10
# Enter the while loop and reset the firewire
while [ $C2 -le 10 ]
do
C2=$((C2+=1))
echo "##### Now Changing the Firewire setup for the $C2 time "
# First set p2p to 0 and do a capture which seems to reset everything
plugctl -n 1 opcr[0].n_p2p_connections=0
test-mpeg2 -r 1 > testcap.ts &
sleep 1
killall test-mpeg2
# Now set it to what seems to work best, at least for the DCT-6200 series tuner...
plugctl -n 1 ompr.bcast_channel=0
plugctl -n 1 opcr[0].channel=63
plugctl -n 1 opcr[0].bcast_connection=0
plugctl -n 1 opcr[0].n_p2p_connections=1
test-mpeg2 -r 1 > testcap.ts &
sleep 1
killall test-mpeg2
if [ -s testcap.ts ] ; then
echo "################"
echo "# Firewire is primed!!!!!!!!!!!!"
echo "################"
C3=0
# Exit the loop
C2=11
fi
# Otherwise repeat the loop
done
# Now print the error codes if any.
if [ $C3 -eq 1 ] ; then
echo "XXXXXXXXXXXXXXXX"
echo "X Craappp it failed after ten tries - the tuner on?"
echo "XXXXXXXXXXXXXXX"
fi
# Clean Up
rm testcap.ts
What about node changes?
The above works great for me except if my node unexpectedly changes. You'll need to know in advance what the GUID of your settopbox is and I've added the following to the top of the above script to account for node changes:
GUID=your-guid-here plugreport | grep $GUID | cut -c 6 | sed 's/^[ \t]*//;s/[ \t]*$//' > /tmp/node NODE=`cat /tmp/node`
and substitute "-n $NODE" and "-r $NODE" for the "-n 1" and "-r 1". Also add "-g $GUID" to your 6200ch command as well.
Alternative one-tuner version using the broadcast connection
This scriptlet relies only on firewire_tester, 6200ch and the broadcast mode in case you can't get a p2p connection.
#!/bin/bash # v0.05 # 6200-broadcast-helper, another firewire channel changer # Written by Josh Rosenbluh # Released under GPL, 2008 # # =Intended Use= # This script is intended for MythTVers who can't get p2p working # and can only use broadcast mode for their firewire connection. # It isn't as fancy as some other scripts, but it does the job. # =Required software in /usr/local/bin/= # 1. '6200ch' channel changer binary # 2. 'firewire_tester' binary # # Please feel free to improve upon this scriptlet. # My email: <my_last_name>@gmail.com #Changing the channel /usr/bin/time -f "Channel Changing took %e seconds.\n" /usr/local/bin/6200ch $1 # This section loops up to LIMIT times to get a link. # firewire_tester has a -r option to repeat the tests, # but that occasionally doesn't work on known good channels. # on those occasions, mythtv fails to record a program. # Assumes the 1st node of the firewire card. This is usually the same after each # reboot or reconnection of the cable. # The loop below could probably be written with a bash function and # a while statement. LIMIT=10 EXIT_STATUS=1 for ((a=1; a <=LIMIT && EXIT_STATUS; a++)) do echo "Attempt $a out of a possible $LIMIT" /usr/bin/time -f "That took %e seconds.\n" /usr/local/bin/firewire_tester -n1 -B EXIT_STATUS=$? done exit $EXIT_STATUS
Fryfrog's Multi Tuner Version
The latest version is a fairly big cleanup and re-tune. It now uses the firewire bus reset available in the "latest" version of firewire_tester. This means you may need to grab the source for it (it is only one file) from SVN head. It is worth it, I think. You can always modify the script a little if you don't want to use it (just deleting two lines should do it). It should now be faster in cases where things are working, and it uses the "single" packet method with 6200ch. I am having "better" luck with it now, but firewire still isn't work 100% for me. I'm almost ready to move to SVN with all the new firewire features.
#!/bin/bash
# Released under the GPL, 2006
# Current Version: 1.0.6
#
# Revision history:
# Version 1.0.6, Added auto-detection of neccessary programs. Still includes
# manual locations, but if that fails it will try auto-detecting
# and finally fail telling you it can't find the utility.
# Version 1.0.5, Lots of minor changes. The lenght of time spent fixing will
# now depend on how many times it has looped. This means that if
# everything is working, it will be fast. As it fails, it will spend
# longer and longer trying to fix. I also added in a re-tune
# of the channel after X attemps and a firewire port reset after
# Y attempts. I cleaned up the code a bit, and it seems to be
# working a bit better for me.
# ** IMPORTANT ** -- The mode options "p" and "b" where changed to
# "p" and "B" (sorry).
# Version 1.0.4, Added support for the -R reset firewire bus command in svn versions of the
# firewire_tester program.
# Version 1.0.3, Added support for p2p *and* broadcast, using "firewire_tester"
# Version 1.0.2, Added a "seconds" option controlling how long it gets
# input from firewire before testing.
# Version 1.0.1, Improved debugging output/formatting!
# Version 1.0.0, Initial release!
#
# Remember, the -h option (and making a mistake) gives a usage summary!
#
# Firewire channel changing and test script, re-written by fryfrog at gmail.com, fryfrog.com
# Original idea from Sean Donovan, techonfoot.com
#
# This script has been completely re-written to be more robust and useful than
# the original version. It can be passed the port, node and number of tries.
# it also has a verbose and debug mode to help track down issues. The defaults
# should be very similar to the original script. Since this one can take port
# and node, the same script can be used for those with more than one fw tuner.
#
# The script also uses variables for a large portion of the script, allowing
# (hopefully) it to be much easier to change and adapt to the future or your
# needs.
#
# Requirements:
# You will need a channel changer (like 6200ch for the DCT series of STBs),
# the "firewire_tester" program that comes in ./contrib of MythTV, and finally
# the "test-mpeg2" & "plugctl" programs that should be part of libiec61883
#
# Example usage:
# 2x firewire cable boxes. `plugreport` shows them on adapter 0, node 0 & 1.
# After a bit of manual, 5 tries is found to be "ideal" and you would like
# debug messages. Script can be found in /usr/local/bin/sure-change.sh
#
# Entry in mythtv-setup, input connections, channel changing script.
# /usr/local/bin/sure-change.sh -p 0 -n 0 -t 5 -d
# Entry for second box
# /usr/local/bin/sure-change.sh -p 0 -n 1 -t 5 -d
#
# Default values for port, node, debug and verbose
# These can be changed if desired, or fed via options
MODE="p"
PORT=0
NODE=0
DEBUG=0
VERBOSE=0
TRIES=5
TIME=1
# Values for external binarys / scripts
# You probably want to point these to the correct place
CHANNEL_CHANGER="/usr/local/bin/6200ch"
FIREWIRE_TESTER="/usr/local/bin/firewire_tester"
TEST_MPEG2="/usr/local/bin/test-mpeg2"
PLUGCTL_PROG="/usr/bin/plugctl"
TEST_FILE="/tmp/testcap.ts"
# Lonely variable used for loop, dont touch!
X=1
# Usage function
usage() {
echo " Usage: [-p <port>] [-n <node>] [-t <tries>] [-v] [-d] [-h] <channel>"
echo " -m <mode> ** [p]2p or [B]roadcast"
echo " -p <port> ** The adapter port, default of 0"
echo " -n <node> ** The node of device, default of 0"
echo " -t <tries> ** Number of reset and test attemps"
echo " -s <seconds> ** Number of seconds to record for test, default of 1"
echo " strongly suggest something in the 1-2 range, 5 tries"
echo " with 5 second time out is 25 seconds until fail!"
echo " -v ** Verbose output"
echo " -d ** Debug output"
echo " -h ** Help and usage"
echo " <channel> ** Channel being changed to"
}
# Debug & Verbose output
inform() {
TIMESTAMP=`date +"%F %T"`
MSG="$1"
echo "${TIMESTAMP} - ${MSG}"
}
# Mode setting function
set_mode() {
MODE="$1"
if [[ "${MODE}" != "p" ]] && [[ "${MODE}" != "B" ]]; then
echo "Invalid mode specified, use [p]2p or [B]roadcast"
echo "${MODE}"
usage
exit 1
fi
(( DEBUG )) && inform "Mode used: ${MODE}"
}
# Port setting function
set_port() {
PORT=$1
(( DEBUG )) && inform "Port used: ${PORT}."
}
# Node setting function
set_node() {
NODE=$1
(( DEBUG )) && inform "Node used: ${NODE}."
}
set_p2p() {
${PLUGCTL_PROG} -p ${PORT} -n ${NODE} ompr.bcast_channel=0
${PLUGCTL_PROG} -p ${PORT} -n ${NODE} opcr[0].channel=${NODE}
${PLUGCTL_PROG} -p ${PORT} -n ${NODE} opcr[0].bcast_connection=0
${PLUGCTL_PROG} -p ${PORT} -n ${NODE} opcr[0].n_p2p_connections=1
(( DEBUG )) && inform "Default firewire settings applied to port ${PORT}, node ${NODE}."
}
set_channel() {
# set mode, node and other options
CHAN_OPTS="-s -p ${PORT} -n ${NODE} ${CHANNEL}"
# set debug option
(( DEBUG )) && CHAN_OPTS="${CHAN_OPTS} -v"
if [[ ${DEBUG} -eq 1 ]]; then
${CHANNEL_CHANGER} ${CHAN_OPTS}
else
${CHANNEL_CHANGER} ${CHAN_OPTS} 2>&1 >/dev/null
fi
(( $? )) && inform "External ${CHANNEL_CHANGER} failure!" && exit 1
(( DEBUG )) || (( VERBOSE )) && inform "Selected channel ${CHANNEL}."
}
reset_p2p() {
${PLUGCTL_PROG} -p ${PORT} -n ${NODE} opcr[0].n_p2p_connections=0
read_p2p
(( DEBUG )) && inform "Reset firewire settings on port ${PORT}, node ${NODE}."
}
read_p2p() {
${TEST_MPEG2} -r ${NODE} 2> /dev/null > ${TEST_FILE} &
sleep ${TIME}
killall ${TEST_MPEG2}
(( DEBUG )) && inform "Attempted read of port ${PORT}, node ${NODE}."
}
# Reset the firewire bus, using new firewire_tester -R option
reset_firewire_bus() {
# set mode, node and port options
TEST_OPTS="-R -P ${PORT}"
# set debug/verbose option
(( DEBUG )) || (( VERBOSE )) && TEST_OPTS="${TEST_OPTS} -v"
# run test with above assigned options
if [[ ${DEBUG} -eq 1 ]]; then
${FIREWIRE_TESTER} ${TEST_OPTS}
else
${FIREWIRE_TESTER} ${TEST_OPTS} 2>&1 >/dev/null
fi
if [[ $? -eq 0 ]]; then
(( DEBUG )) || (( VERBOSE )) && inform "Reset of firewire port ${PORT} successful."
else
(( DEBUG )) || (( VERBOSE )) && inform "Reset of firewire port ${PORT} failed."
fi
}
# Test firewire
test_firewire() {
# set mode, node and port options (using X results in
# longer attempts later in the loop
TEST_OPTS="-${MODE} -P ${PORT} -n ${NODE} -r ${X}"
# set debug/verbose option
(( DEBUG )) && TEST_OPTS="${TEST_OPTS} -v"
# run test with above assigned options
if [[ ${DEBUG} -eq 1 ]]; then
${FIREWIRE_TESTER} ${TEST_OPTS}
else
${FIREWIRE_TESTER} ${TEST_OPTS} 2>&1 >/dev/null
fi
if [[ $? -eq 0 ]]; then
(( DEBUG )) || (( VERBOSE )) && inform "Firewire working after ${X} attempts!"
go_byebye
else
(( DEBUG )) || (( VERBOSE )) && inform "Firewire NOT working yet after ${X} attempts."
fi
}
# Exit
go_byebye() {
clean
(( $1 )) && exit 1
exit 0
}
# Cleanup
clean() {
if [ -f ${TEST_FILE} ]; then
(( DEBUG )) && inform "Test file found, deleting."
rm ${TEST_FILE}
fi
}
# Begin main!
if [ $# == 0 ]; then
usage
go_byebye
fi
# Test 6200ch, try to find it or fail.
if [[ ! -x ${CHANNEL_CHANGER} ]]; then
if [[ -x `which 6200ch` ]]; then
CHANNEL_CHANGER="`which 6200ch`"
inform "${CHANNEL_CHANGER} auto-detected."
else
inform "6200ch not found."
go_byebye
fi
fi
# Test firewire_tester, try to find it or fail
if [[ ! -x ${FIREWIRE_TESTER} ]]; then
if [[ -x `which firewire_tester` ]]; then
FIREWIRE_TESTER="`which firewire_tester`"
inform "${FIREWIRE_TESTER} auto-detected."
else
inform "firewire_tester not found."
go_byebye
fi
fi
# Test test-mpeg2, try to find it or fail
if [[ ! -x ${TEST_MPEG2} ]]; then
if [[ -x `which test-mpeg2` ]]; then
TEST_MPEG2="`which test-mpeg2`"
inform "${TEST_MPEG2} auto-detected."
else
inform "test-mpeg2 not found."
go_byebye
fi
fi
# Test plugctl, try to find it or fail
if [[ ! -x ${PLUGCTL_PROG} ]]; then
if [[ -x `which plugctl` ]]; then
PLUGCTL_PROG="`which plugctl`"
inform "${PLUGCTL_PROG} auto-detected."
else
inform "plugctl not found."
go_byebye
fi
fi
while getopts m:p:n:t:s:vdh OPTION;
do
case ${OPTION} in
m) set_mode ${OPTARG}
;;
p) set_port ${OPTARG}
;;
n) set_node ${OPTARG}
;;
v) VERBOSE=1
;;
d) DEBUG=1
;;
t) TRIES=${OPTARG}
;;
s) TIME=${OPTARG}
;;
*) usage
exit 0
;;
esac;
done
shift $(($OPTIND - 1))
if [ "$1" == "" ]; then
echo "You forgot to specify a <channel>!"
usage
go_byebye
else
CHANNEL="$1"
fi
clean
# Attempt to change the channel
(( DEBUG )) || (( VERBOSE )) && inform "Trying ${TRIES} times to get channel ${CHANNEL}"
set_channel
# If fire wire isn't working, try to reset it
while [ ${X} -le ${TRIES} ]
do
(( DEBUG )) && inform "Attempt ${X} of ${TRIES}..."
if [[ "${MODE}" == "p" ]]; then
# Reset p2p and do a capture, aparantly this helps with the reset.
reset_p2p
# Reset to known-good settings
set_p2p
fi
# If it fails twice, try re-tuning the channel
if [ $(( X % 3 )) -eq 0 ]; then
set_channel
fi
# If it fails 3 times, reset the firewire bus
if [ $(( X % 5 )) -eq 0 ]; then
reset_firewire_bus
fi
# Test again after reset, hope it works!
test_firewire
X=$((X+=1))
done
(( DEBUG )) || (( VERBOSE )) && inform "${TRIES} attempts failed, could not change to ${CHANNEL} and recieve signal via firewire"
go_byebye 1
Steve Adeff's bus reset safe (ie multi-tuner safe) w/ channel changer primer
From Steve Adeff's personal wiki page:
I wrote a script for handling my DCT-6200 cable boxes connected via firewire that changes the channel and uses firewire_tester to stabilize the connection before Myth tries to record. This has, over the last two years, allowed me to not fail in recording a single show.
To use, set it up in mythtv-setup as the channel change script. Give it the GUID of the box as shown in mythtv-setup. The script takes two variables, the first being the GUID (which is why you have to specify it) and the second is the channel number (which mythtv automatically feeds it at the end of the command you give it). The script then changes the channel on that GUID and runs firewire_tester. In running firewire_tester it requires that it show the connection is stable twice. If it iterates 10 times it will automatically re-tune the channel, and after 50 attempts total it will send Myth the "failed" result, otherwise it will send Myth the "success" result.
You can change the script to use another channel changer if you require.
#!/bin/bash
#
GUID=`echo "$1"|sed -r "s/[A-Z]+/\L&/g"`
CHANNEL=$2
NODE=`plugreport | grep $GUID | awk '{if (($1 == "Node") && ($4 == "'"0x$GUID"'
")) print $2}'`
echo "Node: '$NODE'"
echo "Changing to channel $CHANNEL"
6200ch -v -s -g $GUID $CHANNEL
#maybe a pause is needed?
sleep 4
STABILIZE="1"
COUNT=0
TOTALCOUNT=0
STABLECOUNT=0
echo "Stabilizing Firewire Connection!..."
#maybe an initial run followed by a verification run?
#initial run
firewire_tester -B -r2 -n$NODE
#while [ "$STABILIZE" != "Broadcast Fix: Success (already stable)" ]
while [ $STABLECOUNT -lt 2 ]
do
STABILIZE=`firewire_tester -B -n$NODE|grep "already stable"`
if [[ "$STABILIZE" != "Broadcast Fix: Success (already stable)" ]]
then
echo "Not Stable! Attempt $COUNT"
((COUNT+=1))
((TOTALCOUNT+=1))
if [ $TOTALCOUNT -gt 50 ]
then
echo "FAILED to Stabilize!!"
exit 1
fi
if [ $COUNT -gt 10 ]
then
6200ch -v -s -g $GUID $CHANNEL
COUNT=0
firewire_tester -B -r2 -n$NODE
fi
else
echo $STABILIZE
((STABLECOUNT+=1))
fi
done
echo "Stable!"
exit 0
SA3250HD Primer
Primer Script for SA3250HD cable boxes written by jwestfall. Written for two boxes but works equally well on one box. Still can't reliably record from two of these boxes but this will get them going. I put this in my /usr/bin directory and call it from the startup programs in the sessions application in Ubuntu and it works every time for me. Post something to the mailing list if you have problems getting it going.
#!/bin/bash
# sa3520_fixer.sh [-v]
#
# This should only be used prior to starting the backend. Using it as
# part of an external channel changer would be a bad idea since its doing
# a bus reset.
#
# jwestfall@surrealistic.net
# List of nodes to fix
NODES="0 1"
# The number of times to try and fix a node
ATTEMPTS=5
# The number of consecutive tests that must be successful
# for a node to be considered working
TESTS=5
# verify firewire_tester and plugctl are installed
if [ "`which firewire_tester`" = "" ]; then
echo "firewire_tester is required, but not found"
exit
fi
if [ "`which plugctl`" = "" ]; then
echo "plugctl is required, but not found"
exit
fi
# deal with -v commandline
VERBOSE="/dev/null"
if [ "$1" = "-v" ]; then
echo "verbose output enabled"
VERBOSE="/dev/stdout"
fi
# global failed variable for when the script exits
FAILED=0
# reset the firewire bus
# this should normalize all the plugs and reset n_p2p to 0
echo Resetting the Firewire bus
firewire_tester -R > $VERBOSE 2>&1
for NODE in $NODES;
do
TRY=0
SUCCESS=0
echo -n "Setting up node $NODE, ";
while [ "$TRY" -lt "$ATTEMPTS" -a "$SUCCESS" -ne "1" ];do
# tell stb to send data
plugctl -n $NODE oPCR[0].n_p2p_connections=1
# see if the stb is sending data
firewire_tester -p -n $NODE -r $TESTS > $VERBOSE 2>&1
if [ "$?" -eq "0" ];then
SUCCESS=1
else
# stop sending data
plugctl -n $NODE oPCR[0].n_p2p_connections=0
fi
let TRY+=1
done
if [ "$SUCCESS" -eq "1" ];then
echo "SUCCESS (after $TRY attempts)"
else
echo "FAILED"
FAILED=1
fi
done
exit $FAILED
