Using MythTV with AFN

From MythTV Official Wiki
Revision as of 21:17, 5 November 2011 by Redxine (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
A PowerVu D9835 decoder, typically used with AFN

AFN, or American Forces Network, is the name of the television and radio service provided by the American Armed Forces for its entertainment and command internal information networks worldwide. AFN programming is provided at zero cost for those who serve, and is usually provided over ATSC cable within military installations and base housing. However, for those living off-base and scattered installations too small to provide base housing, the service is provided over satellite signals using the PowerVu conditional access system, meaning the only way to watch AFN programs is with a PowerVu receiver. This guide focuses on using such receiver with MythTV.

Overview

While the Scientific Atlanta (now Cisco) PowerVu receivers will work with DVB-S signals, AFN is broadcasted over the PowerVu conditional access system, and requires the receiver's TID and UA to be registered. Therefore, the only way to get these working with MythTV is by use of an analogue input card and control with an IR blaster (While some models such as the D9834 do have ethernet and RS232 connections, the newer D9835 and later Cisco models do not. As such, this guide will focus on the use of an IR blaster to control the decoder). Another interesting challenge in lack of feature with these decoders, particularly the two D9835's that I had the misfortune of working with is that the only way to change channels on the remote is the channel up/down button, or through the less-than simple menu - typing the channel number produces no response from the receiver, and no apparent menu option can enable it.

Requirements

  • A working MythTV frontend/backend combo (Mythbuntu 11.10 was used in my example)
  • An analogue capture card (Bt878 or similar) with line-in audio (or alternatively input from an ALSA sound card)
  • A subscription to Schedules Direct (for AFN guide)
  • A LIRC compatible IR-blaster. I built a serial adapter for this this.

Configuring LIRC

In Debian-based distros, LIRC is configured using dpkg. If you need to reconfigure it after installing, run sudo dpkg-reconfigure lirc. Select the appropriate receiver and transmitter driver for your IR hardware. If using the serial IR blaster, select Serial Port (UART) : Scientific Atlanta Cable box or any of the Serial Port (UART) entries and /dev/ttyS0 as the device.

Now we need to add the decoder IR codes by editing lircd.conf (/etc/lirc/lircd.conf for most distros). Append this to your lircd.conf:

begin remote
 name  PowerVu
 bits           22
 flags SPACE_ENC|CONST_LENGTH
 eps            30
 aeps          100
 header       3351  3237
 one           813  2404
 zero          813   730
 ptrail        771
 gap          97655
 toggle_bit_mask 0x0
     begin codes
         KEY_POWER                0x37C107
         KEY_SLEEP                0x37211B
         KEY_FAVORITES            0x37F101
         KEY_1                    0x36113D
         KEY_2                    0x37111D
         KEY_3                    0x36912D
         KEY_4                    0x37910D
         KEY_5                    0x365135
         KEY_6                    0x375115
         KEY_7                    0x36D125
         KEY_8                    0x37D105
         KEY_9                    0x363139
         KEY_0                    0x373119
         KEY_MENU                 0x36A12B
         KEY_EPG                  0x36C127
         KEY_INFO                 0x36213B
         KEY_CHANNELUP            0x377111
         KEY_CHANNELDOWN          0x36F121
         KEY_LAST                 0x36E123
         KEY_UP                   0x36812F
         KEY_RIGHT                0x364137
         KEY_DOWN                 0x37A10B
         KEY_LEFT                 0x37810F
         KEY_SELECT               0x366133
         KEY_NEXT                 0x37011F
         KEY_PREVIOUS             0x36013F
         KEY_BACK                 0x37810F
         KEY_PAUSE                0x374117
         KEY_FORWARD              0x364137
         KEY_MUTE                 0x36892E
         KEY_VOLUMEDOWN           0x37091E
         KEY_VOLUMEUP             0x36093E
     end codes
end remote

Restart LIRC after making this change:

sudo service lirc restart

To test the configuration connect and position your IR blaster, and run:

irsend -d /dev/lircd1 send_once PowerVu KEY_POWER

Channel changing scripts

To handle channel changing I've written a python script which handles channel changing in a sensible way with the limitations noted above. Also because of some #Problems encountered with the front end in Mythbuntu 11.10, I use the forking feature in bash to initiate a work around script (resetTv.sh). So channel changing is handled by two scripts. (If you don't experience the audio problem after changing channels, you can replace the bash script with just the python code). I saved these scripts in my home folder to simplify editing. The script will also need write access to a state file which I keep in the same directory (run touch channel-state; chmod 666 channel-state). Change the paths accordingly.

change-channel.py:

#!/usr/bin/env python
import os
import sys
from time import sleep
if len(sys.argv) != 2:
    print "Not enough arguments"
    exit()
channel = str(sys.argv[1]).zfill(5)
os.system('touch /home/mythbox/scripts/channel-state')
f = open('/home/mythbox/scripts/channel-state', 'r')
state = f.readline()
f.close()
if state == : #set a default if file is blank
    state = '5'.zfill(5)
print "State: %s" % str(state)
print "Channel: %s" % str(channel)
if state == channel:
    print "[change-channel]: Nothing to do, exiting...."
    exit(0)	#do nothing, no need to tune
#write channel to state file
f = open('/home/mythbox/scripts/channel-state', 'w')
f.write(channel)
f.close()
print '[change-channel]: Changing to channel %s' % channel
def send(code):
command = '/usr/bin/irsend -d /dev/lircd1 send_once PowerVu'
    sleepTime = 0.3
    print '[change-channel]: send(%s)' % code
    os.system('%s %s' % (command, code))
    sleep(sleepTime)
send('KEY_MENU')
send('KEY_DOWN')
send('KEY_SELECT')
for i in channel:
    send('KEY_%s' % i)
send('KEY_SELECT')
send('KEY_1')
send('KEY_SELECT')
print "[change-channel]: Done."
#tell calling script to fork resetTv.sh
exit(127)

change-channel:

#!/bin/bash
debug=$( /home/mythbox/scripts/change-channel.py $1 )
status=$?
echo -e $debug
if [ $status -ne 0 ];
then
    echo "resetting TV..."
    #optional: used to create a notification for the user.  Must run as the same user the front end
    #uses to run libnotify.  Add 'mythtv ALL = NOPASSWD: ALL' in /etc/sudoers to make this work.
    #sudo su -c '/usr/bin/notify-send -i gtk-info -u critical -t 14000 "Please wait...." "Changing channel"' mythbox
    /home/mythbox/scripts/resetTv.sh &
fi
exit 0

resetTv.sh:

#!/bin/bash
sleep 10 #wait for the frontend to settle.
/bin/nc localhost 6546 <<EOS
key escape
jump livetv
EOS

Set the change-channel bash script as the channel change script in mythbackend.

Problems encountered

  • For my setup, the audio on the frontend would always stop for some reason after changing channels. Perhaps it's just my card or particular setup, but the only sure-fire method of fixing this was exiting live-tv and pulling it back up. To accomplish this, I use a script that forks in the background after the channel-changing script is called and instructs the frontend to reset using Myth's network control provision. You must enable this for the work around to function.
  • The AFN schedule data is available from Schedules Direct. It is available in the line up after keying in any US zipcode.
  • I'm working on being able to control two of the same model receiver using a single IR blaster. More to come.
  • The guide information provided by AFN as published by Schedules Direct does not follow European daylight savings changes. You may need to specify an offset other than 'auto' in the backend general configuration. (For example I need to set it to +0100 during fall and winter otherwise the guide information will be off by one hour. The system clock handled the change correctly, but setting MythTV to use AUTO during the fall offsets the guide information by one hour.)

--Redxine 21:17, 5 November 2011 (UTC)