sasc: Stand-alone SoftCAM

sasc 0.5: For use with MythTv 0.19 or SVN r8925 and sc-0.5.8+

What is sasc?
  sasc is an application that allows using the vdr 'sc' plugin as a stand alone 
executable.  This gives you the capability to use the powerful softCam library 
in programs other than vdr.  In sasc-0.6, support has been additionally added to
support evocamd, which is a binary-only softcam.

Why did you write sasc?
  sasc was written to give MythTV (www.mythtv.org) the ability to use SoftCAM to
decode channels. My original patch (based off work done by schiaparellit) 
worked well for BEV and DishNetwork, however it didn't support any other
encoding schemes, nor did it have the full functionality of SC (it couldn't use
ROMs for instance). Looking to the future, it was going to be a lot of work to
maintain that patch given how rapidly myth changes, and the features that were
being requested (other encryption, decsa, etc). A member of the DVBN forum
(red321z) asked whether a stand alone sc could be built. My initial thoughts
were that it would be difficult because of sharing the dvb card between the SC
app and the stream decoder, but I then realized that there was not much overlap,
and decided to give it a shot.

What license is the code released under?
  GPL. See the COPYING file for more information.

How does it work?
  I have no idea. Well, actually I understand how SC works pretty well now 
after getting it working in myth the first time, but it isn't really relevant.
I also have no knowledge whatsoever as to how the algorithms are implemented.
Suffice it to say that the app just lets the sc plugin do all the work.

I looked at the code, and it looks like Sh*t!
  Yep. Basically my goal was to implement the app in such a way that no 
modifications to the SC plugin were needed. Since SC is really tied quite 
tightly to vdr, I had to stub out a lot of functions which aren't central to the
SC algorithm, but were needed to get the code to compile. The only two files of
interest are sasc.cpp and misc.cpp. The rest are either stubs or copies from
vdr.

How do I use it?
  Well sasc isn't completely stand alone. It needs to be fed pmt data whenever
the channel changes. The pmt contains the encoding algorithm for the channel as
well as indicating that SC needs to recompute the keys.  As currently, only
MythTV is supported, you must recompile Myth with the relevant patches to
support communicating with sasc.  More techinical details of the sasc API
can be found in the 'Developers' document (if I've gotten around to writing it
yet)

Enough with the dawdling, I want to use sasc NOW!
  Ok.  Actually, I recommend getting VDR working on your system first, but it
isn't realy necessary, and myth may actally be easier to setup than vdr these
days, so feel free to just jump right in.

1. Building sasc

  a. Obtain vdr-sc-0.5.x and extract it under PLUGINS/src
     the latest vdr-sc release can be found here:
     http://207.44.152.197/vdr-sc-0.5.9.tar.gz

  b. Patch it as needed (see patches/PATCHES):

  c. Edit sasc Makefile, and specify the version of sc you want to use.

  d. Type 'make' and it should all just work.

  There is currently no 'install' step, so just put sasc into your path
  somewhere :

    $ cp sasc /usr/local/bin

  It is also required that you install the 'sc' shared libraries, but that step
  is covered in steps 6 and 7 below.

2. Checkout MythTV from SVN (or get the 0.19-fixes branch):

    $ svn co http://svn.mythtv.org/svn/trunk/mythtv

  To use the 'fixes' branch:

    $ svn co http://svn.mythtv.org/svn/branches/release-0-19-fixes/mythtv
  If you want to checkout a specific version of mythtv, use -r :

    $ svn co -r 8925 http://svn.mythtv.org/svn/trunk/mythtv 

  Later, if you want to sync to the current SVN, just run an normal checkout.

3. Apply the relevant sasc patches (see patches/PATCHES).  At a minimum, you
   will need the sasc_base.dff patch.

  If you get failed hunks, you may want to checkout an earlier revision (-r) 
  from SVN or figure out what did not apply propertly using the .rej.


4. Compile FFdecsa (you only need to do this if you have a budget card):

    $ cd libs/libmythtv/FFdecsa
  (view the README and follow directions)
    $ make
    $ cd ../../../

5. Compile MythTV

    $ ./configure --dvb-path=/usr/src/dvb-kernel/linux/include
  (Before proceeding, verify that options you need will be compiled. For more details, use ./configure --help)
    $ make
    # make install

6. From whatever user you use to run mythbackend:

    $ mkdir -p ~/.mythtv/externcam_0/lib  #replace '0' with the dvb card number
    $ cp <sasc src path>/PLUGINS/lib/* ~/.mythtv/externcam_0/lib
    $ cp <some path>SoftCam.key ~/.mythtv/externcam_0/

  For Nagravision2 (DN/BEV) you probably want to do the following as well (for
  other providers configure as you would vdr):

    $ mkdir ~/.mythtv/externcam_0/nagra
    $ cp <some path>/rom102.bin ~/.mythtv/externcam_0/nagra/

  You may also need to copy other files for your encryption. I am not familiar
  with any other providers, but you should setup the externcam_0 directory the
  same way you would the vdr 'plugins' direcory (except for any ca.cache files)

7. Run mythtv-setup (just follow the instrcutions at www.mythtv.org)
  a) Add 'sasc' as the 'External Cam App' under the 'General' settings.
     If sasc isn't in your search path, place the full path here instead
     NOTE: You can install the sasc libs in a directory other than
     externcam_0/lib.  If you do, use 'sasc --lib <path to libs>' instead
  
  b) Channel scanning should work fine, so there is no need to import channels
     usually.

  c) If you have a FF card, make sure that you enable HW Encoding (under
     'Recording Options' for a given capture card. (This only applies to 0.19)

8. With any luck, you are ready to try it out now.
  Start up mythbackend and you are ready to roll.

Additional Notes
----------------
If you have more than one DVB card, you need to repeat step 6 for each card
(substitute _0 for _1 etc)

Using evocamd
-------------
Evocamd is a static binary-only softcam, which makes it more difficult to use.
It only supports full-featured cards, and only a single one (at adapter0).
To allow evocamd to work with multiple cards, as well as budget cards, sasc
includes some additional features.

<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>
Because these hacks involve a custom kernel module, a setuid application, and
a binary-only application for which you cannot inspect the source code, it is
quite possible that using the following will make your system less secure.
Use it at your own risk!
<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>


1. Compile the caloopback kernel module.  I recomend at least glancing at the
   caloopback/README file

   NOTE: This step is ONLY needed for budget cards!

  $ cd caloopback
  $ make
  $ su -c make install

2. Create a camd.tar.gz file.
  While evocamd is included in the supplied camd.tar.gz file, the keys and
  ROM images are NOT included.  At this time, the best place to get the needed
  collateral files would be to locate a copy of 
  Evocamd-Next Level version 2.11.zip

  $ mkdir /tmp/chroot
  $ cd /tmp/chroot
  $ tar -xzf <sasc build path>/camd.tar.gz
  $ cp <some dir>/keys/* keys
  $ cp <some dir>/scce/ROM102.bin var/scce/
  $ tar -czf <sasc build path>/camd.tar.gz .

  As the user you run mythtv as:
  $ cp <sasc build path>/camd.tar.gz ~/.mythtv/externcam_0/

3. Build sasc-chroot-dvb
  $ make sasc-dvb-chroot

  NOTE: The following command will install an suid binary in /usr/local/bin
        Only users in the 'video' group will have execute permission to it

  $ su -c make sasc-dvb-chroot-install

4. Update the sasc commandline    
   Run mythtv-setup, and add 'sasc -e' under the 'General' settings.

5. Force budget-card mode (only if using the caloopback module)

  $ touch ~/.mythtv/externcam_0/force_sw

6. Load the caloopback module (budget cards only).  This step will need to be
   repeated everytime you reboot or /load/unload dvb modules.  you might want
   to configure it to run automatically after all dvb modules load.

  $ su -c modprobe caloopback devnum=0

  If you have multiple budget cards, you should use something like:

  $ su -c modprobe caloopback devnum=0,1,2

Known problems
--------------
NOTE: The following solutions were mostly not written by or tested by myself.
I have no idea if they are correct, or up to date.

1. Starting the backend from init scripts

  Since sasc uses stdin to get it's information, starting the backend from an init script
   will not work. One possible workaround is to start the backend from screen:

    $ vi /etc/inittab
    
  Modify :
    1:2345:respawn:/sbin/mingetty tty1
  To:
    1:2345:respawn:/sbin/mingetty --noclear --noissue --nonewline --autologin=root tty1

  Then, add these to the root's .bash_profile:

    if [ -z "$DISPLAY" ] && [ $(tty) == /dev/tty1 ]; then
      screen mythbackend
    fi

  Upon boot, you will see the backend starting on the terminal. if you want to connect
   to that, open a session via telnet or ssh and type:

    $ screen -dr

2. The starting channel changes

  This is not a sasc issue but I think it's nice to know how to specify the
   starting channel without loading mythtv-setup everytime for troubleshooting
   purposes since trying to tune to a bad channel may hung either mythbackend,
   mythfrontend or both.

  Here's a simple SQL query that you can run :

    $ mysql -u mythtv -pmythtv mythconverg -e "update cardinput set startchan=198 where cardinputid=1";

  Or you can modify MythTV's code:

    $ vi libs/libmythtv/tv_rec.cpp

  The function that will update the startchan is called "StoreInputChannels". Look for it in the code
   then comment out :

/*
        query.prepare(querystr);

        if (!query.exec() || !query.isActive())
            MythContext::DBError("StoreInputChannels", query);
*/

