LinHES Forums
http://forum.linhes.org/

Using external tuning scripts to fix PVR parameter issues
http://forum.linhes.org/viewtopic.php?f=3&t=10001
Page 1 of 2

Author:  tjc [ Wed May 10, 2006 11:26 pm ]
Post subject:  Using external tuning scripts to fix PVR parameter issues

PVR cards can have wildly different volume levels for capture and often need different video capture parameters as well. Sadly there's no obvious way to get MythTV to do these card by card adjustments. Here is a hack to deal with situations where you've got say one PVR-250 and one PVR-150, or some other combination, which need different settings for control parameters like volume, color saturation, ... to get uniform quality recordings.

1) Create the following scripts in /usr/local/bin and make sure that they're executable:
/usr/local/bin/delayed_settings.sh
Code:
#!/bin/bash
# Uncomment this for debugging
#set -vx

DEVICE=$1
exec >>/tmp/changer$DEVICE.log 2>&1

                     # controls:
BRIGHTNESS=${2:-127} # Picture brightness, actually the black level. [0 - 255]
HUE=${3:-0}          # Hue or color balance. [-128 - 127]
SATURATION=${4:-63}  # Picture color saturation or chroma gain. [0 - 127]
CONTRAST=${5:-63}    # Picture contrast or luma gain. [0 - 127]
VOLUME=${6:-60000}   # Overall audio volume. [0 - 65535]

CTL_OPTS="-d /dev/video$DEVICE"
echo ; echo Settings before:
/usr/local/bin/ivtvctl $CTL_OPTS -Y
for i in 0 2 4 6 8 10 ; do
    /usr/local/bin/ivtvctl $CTL_OPTS -y brightness=$BRIGHTNESS >/dev/null
    /usr/local/bin/ivtvctl $CTL_OPTS -y hue=$HUE               >/dev/null
    /usr/local/bin/ivtvctl $CTL_OPTS -y saturation=$SATURATION >/dev/null
    /usr/local/bin/ivtvctl $CTL_OPTS -y contrast=$CONTRAST     >/dev/null
    /usr/local/bin/ivtvctl $CTL_OPTS -y volume=$VOLUME         >/dev/null

    # Add any extra ivtvctl commands here, before the sleep.
    sleep 2
done
echo ; echo Settings after:
/usr/local/bin/ivtvctl $CTL_OPTS -Y

/usr/local/bin/change_tuner0.sh
Code:
#!/bin/bash
# Uncomment this for debugging
#set -vx

DEVICE="0"     # The PVR-250 card
exec >>/tmp/changer$DEVICE.log 2>&1

FREQ_TBL="us-cable-irc" # Possibly "us-cable", "us-cable-hrc", ...
BRIGHTNESS=127 # Picture brightness, actually the black level. [0 - 255]
HUE=0          # Hue or color balance. [-128 - 127]
SATURATION=63  # Picture color saturation or chroma gain. [0 - 127]
CONTRAST=63    # Picture contrast or luma gain. [0 - 127]
VOLUME=64000   # Overall audio volume. [0 - 65535]

export HOME=/home/mythtv
echo "Setting channel to $1"
#env
/usr/local/bin/ivtv-tune -d /dev/video$DEVICE -t $FREQ_TBL -c $1
(nohup /usr/local/bin/delayed_settings.sh $DEVICE \
                                          $BRIGHTNESS \
                                          $HUE \
                                          $SATURATION \
                                          $CONTRAST \
                                          $VOLUME & exit) &
exit 0

/usr/local/bin/change_tuner1.sh
Code:
#!/bin/bash
# Uncomment this for debugging
#set -vx

DEVICE="1"     # The PVR-150 card
exec >>/tmp/changer$DEVICE.log 2>&1

FREQ_TBL="us-cable-irc" # Possibly "us-cable", "us-cable-hrc", ...
#BRIGHTNESS=95  # Picture brightness, actually the black level. [0 - 255]
BRIGHTNESS=127 # Picture brightness, actually the black level. [0 - 255]
HUE=0          # Hue or color balance. [-128 - 127]
SATURATION=63  # Picture color saturation or chroma gain. [0 - 127]
CONTRAST=63    # Picture contrast or luma gain. [0 - 127]
VOLUME=57000   # Overall audio volume. [0 - 65535]

export HOME=/home/mythtv
echo "Setting channel to $1"
#env
/usr/local/bin/ivtv-tune -d /dev/video$DEVICE -t $FREQ_TBL -c $1
(nohup /usr/local/bin/delayed_settings.sh $DEVICE \
                                          $BRIGHTNESS \
                                          $HUE \
                                          $SATURATION \
                                          $CONTRAST \
                                          $VOLUME & exit) &
exit 0

Note that the settings variables in change_tuner0.sh and change_tuner1.sh scripts are where you would make your parameter adjustments. You can create as many more change_tunerX.sh scripts as needed to support all your tuners. Don't forget to update the DEVICE number and the FREQ_TBL settings!

2) Stop the backend, run mythtv-setup, and under Input Connections specify the appropriate tuner script for each active tuner. Then restart the backend and do some testing.

BTW - This is a fairly crude approach. It does not play nicely with the recording controls provided by the 'G' key, channel by channel recording adjustments, or any of the similar mechanisms built into MythTV. On the other hand it's the only solution I'm aware of for this basic problem.

Edit - Updated to my current better documented versions.

Author:  Type82 [ Mon May 15, 2006 6:12 pm ]
Post subject: 

So I added /usr/local/bin/change_tunerX.sh to the change channel field under Input Connections in mythtv-setup for both of my tuners. Once I did that, myth wasn't able to change channels (standard coax in on tuner0 on both pvr cards).

Author:  tjc [ Mon May 15, 2006 6:18 pm ]
Post subject: 

Did you put those scripts in the appropriate place, make sure they where executable,
Code:
chmod +x /usr/local/bin/delayed_settings.sh /usr/local/bin/change_tuner?.sh
and test them by hand first?

Author:  SadaraX [ Tue May 16, 2006 11:06 am ]
Post subject: 

Type82 wrote:
So I added /usr/local/bin/change_tunerX.sh to the change channel field under Input Connections in mythtv-setup for both of my tuners. Once I did that, myth wasn't able to change channels (standard coax in on tuner0 on both pvr cards).


I had the same problem, I cannot change channels. I followed your instructions and my scripts are executable (chmod a+x scripts....)

When I ran mythtv-setup (with the backend stopped) I I went to Input Connections, selected my appropriate source (in my case a PVR-150-1 MPEG) and entered in the "External channel change command: " field I entered "/usr/local/bin/change_tuner0.sh" which is appropriate.

The good news is that it successfully changes the brightness, contrast and saturation for me. Still, I need find a way to change channels....

Author:  tjc [ Tue May 16, 2006 3:39 pm ]
Post subject: 

Again, did you test them manually first? Have you looked at the log files that they write? What version of the ivtv driver are you using?

Author:  SadaraX [ Tue May 16, 2006 4:15 pm ]
Post subject: 

tjc wrote:
Again, did you test them manually first? Have you looked at the log files that they write? What version of the ivtv driver are you using?


IVTV v0.4.4

Author:  tjc [ Tue May 16, 2006 4:46 pm ]
Post subject: 

So what do the log files say? They capture all the output, so if the tuning script has a problem, it should be really obvious. For example, that's how I knew that it needed HOME set and exported.
Code:
more /tmp/changer*.log

If that doesn't provide enough info add a line that says "set -vx" right after the exec as shown in the updated examples.

Author:  SadaraX [ Tue May 16, 2006 8:32 pm ]
Post subject: 

tjc wrote:
So what do the log files say? They capture all the output, so if the tuning script has a problem, it should be really obvious. For example, that's how I knew that it needed HOME set and exported.
Code:
more /tmp/changer*.log

If that doesn't provide enough info add a line that says "set -vx" right after the exec as shown in the updated examples.


I removed the script for now (so I could continue to watch TV). But here is the log...

Quote:
Invalid channel for 'us-cable-irc'

Settings before:
ioctl: VIDIOC_QUERYCTRL
Brightness = 127
Contrast = 63
Saturation = 63
Hue = 0
Volume = 60928
Mute = 0


It repeats that about 20 times. Then it finally has:

Quote:
Settings after:
ioctl: VIDIOC_QUERYCTRL
Brightness = 227
Contrast = 80
Saturation = 63
Hue = 0
Volume = 60928
Mute = 0


I am not sure what caused this. A temporary solution I am using is to set the colors for each channel manually through the web interface. It took a bit of math to get it right, but about 58-60% for brightness, constrast and saturation is alright.

Author:  tjc [ Tue May 16, 2006 8:49 pm ]
Post subject: 

Aha! You need to set the frequency set to be whatever you use rather than "us-cable-irc". Remember these scripts are more at the proof of concept stage than being really polished fully automatic solutions. I've updated the examples so that they use a FREQ_TBL variable to make this more obvious.

BTW - Running these from the command line for testing is really, really easy. To test changing tuner 0 to channel 60:
Code:
/usr/local/bin/change_tuner0.sh 60

Author:  SadaraX [ Tue May 16, 2006 8:59 pm ]
Post subject: 

tjc wrote:
Aha! You need to set the frequency set to be whatever you use rather than "us-cable-irc". Remember these scripts are more at the proof of concept stage than being really polished fully automatic solutions.


Alright. How do I set the cable type? I am surprised it says its wrong because not only can I view cable and record fairly well, but I am in the United States, so I cannot think of anything else it should be....

Author:  tjc [ Tue May 16, 2006 9:04 pm ]
Post subject: 

Most likely "us-cable" or "us-cable-hrc". All you've go to do is a simple replacement on /usr/local/bin/change_tuner?.sh

You might want to throw a simple echo in there to print out the channel paramter being passed. Something like:
Code:
echo "Setting channel to $1"

Author:  tjc [ Mon Jun 12, 2006 9:24 pm ]
Post subject: 

I've updated the scripts to match my curent versions.

Author:  alarson [ Mon Jun 12, 2006 10:57 pm ]
Post subject: 

I didn't realize there was a need to delay the update. That might explain some strange issues I've had in the past. Hmm.

Anyway, a small suggestion. I think the scripts would be clearer (and there would be fewer of them), if they were converted to a single script like this:

Code:
#!/bin/bash
# Uncomment this for debugging
#set -vx

DEVICE="0"     # Which /dev/videoN to use.
FREQ_TBL="us-cable-irc" # Possibly "us-cable", "us-cable-hrc", ...
BRIGHTNESS=127 # Picture brightness, actually the black level. [0 - 255]
CONTRAST=63    # Picture contrast or luma gain. [0 - 127]
SATURATION=63  # Picture color saturation or chroma gain. [0 - 127]
HUE=0          # Hue or color balance. [-128 - 127]
VOLUME=60000   # Overall audio volume. [0 - 65535]

exec >>/tmp/changer$DEVICE.log 2>&1
CTL_OPTS="-d /dev/video$DEVICE"

function delayed_tune()
{
  echo ; echo Settings before:
  /usr/local/bin/ivtvctl $CTL_OPTS -Y
  for i in 0 2 4 6 8 10 ; do
      /usr/local/bin/ivtvctl $CTL_OPTS -y brightness=$BRIGHTNESS >/dev/null
      /usr/local/bin/ivtvctl $CTL_OPTS -y contrast=$CONTRAST     >/dev/null
      /usr/local/bin/ivtvctl $CTL_OPTS -y saturation=$SATURATION >/dev/null
      /usr/local/bin/ivtvctl $CTL_OPTS -y hue=$HUE               >/dev/null
      /usr/local/bin/ivtvctl $CTL_OPTS -y volume=$VOLUME         >/dev/null
      sleep 2
  done
  echo ; echo Settings after:
  /usr/local/bin/ivtvctl $CTL_OPTS -Y
}

export HOME=/home/mythtv
echo "Setting channel to $1"
#env
/usr/local/bin/ivtv-tune $CTL_OPTS -t $FREQ_TBL -c $1
delayed_tune args&
disown -h %1

That way there is no need to pass lots of parameters, and thus the commands can be inline.

Author:  tjc [ Tue Jun 13, 2006 8:14 pm ]
Post subject: 

I don't think that does quite the same thing. You need the script that MythTV calls (A) to exit and return success before MythTV will go on about it's business. This means that the spawned child (B) needs to be detached so that A isn't waiting for it. From my reading of the bash help on "disown -h" it doesn't do that, B is still left in your process table.

The "subshell which forks a child under nohup in the background and then exits" trick is the most reliable way I know to really detach a process in shell, and unfortuantely it doesn't work with functions.

Author:  Greg Frost [ Tue Jun 13, 2006 9:20 pm ]
Post subject: 

I had a go at experimenting and there seemed to be no problem backgrounding the function call.
Here's the script I used to experiment:
Code:
#!/bin/bash
echo $(date) $0 $1 $2

function delayed_tune ()
{
  echo $(date) In delayed tune
  sleep 12
  echo $(date) Delayed tune done
}

echo $(date) Calling delayed tune
delayed_tune&
echo $(date) Done
I set the channel change command to
Code:
/home/mythtv/playground/background_test.bash wow
And here is an extract from the backend log:
Code:
2006-06-14 12:41:54.902 TVRec(3): Changing from None to WatchingLiveTV
Wed Jun 14 12:41:55 CST 2006 /home/mythtv/playground/background_test.bash wow 999
Wed Jun 14 12:41:55 CST 2006 Calling delayed tune
Wed Jun 14 12:41:55 CST 2006 In delayed tune
Wed Jun 14 12:41:55 CST 2006 Done
2006-06-14 12:41:55.958 ret_pid(29257) child(29257) status(0x0)
2006-06-14 12:41:55.962 External Tuning program exited with no error
strange error flushing buffer ...
Wed Jun 14 12:42:07 CST 2006 Delayed tune done
2006-06-14 12:42:16.038 TVRec(3): Changing from WatchingLiveTV to None
A few things are obvious from this test.
1. Myth went about it's business straight away after calling the channel change script.
2. It is possible to pass an additional parameter to the channel change script (which could be used to identify the tuner the script is being called for).
3. I didn't need to play any funny buggers with disowning the backgrounded function call.

The thing alarson's script is missing is the ability to have different settings for different tuner cards, and "obvious thing 2" is the solution for that.

Page 1 of 2 All times are UTC - 6 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/