Kodi Community Forum

Full Version: Reboot/suspend can be sluggish
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
After a couple of days toiling with this, I found that tvheadend was the culprit (I've already logged a bug over there as well).

The solution is simple:

1) Replace the existing /etc/init/tvheadend.conf file with this one:
Code:
# tvheadend - DVB/IPTV streaming server
#
# Tvheadend is a TV streaming server for Linux supporting DVB, ATSC, IPTV,
# and Analog video (V4L) as input sources.

description "Tvheadend DVB/IPTV streaming server"
author      "Adam Sutton <[email protected]>"

start on (local-filesystems and net-device-up and started udev-finish)
stop  on starting shutdown

setuid xbmc
setgid video
respawn

script
  [ -r /etc/default/tvheadend ] && . /etc/default/tvheadend
  if [ "$TVH_ENABLED" != "1" ] ; then
    stop
    exit 0
  fi

  CONF="$(readlink -f ~xbmc/.hts/tvheadend)"
  [ -n "$TVH_CONF_DIR"  ] && CONF="$TVH_CONF_DIR"

  ARGS="-c ${CONF}"
  [ -z "$TVH_ADAPTERS"  ] || ARGS="$ARGS -a $TVH_ADAPTERS"
  [ "$TVH_IPV6" = "1"   ] && ARGS="$ARGS -6"
  [ -z "$TVH_HTTP_PORT" ] || ARGS="$ARGS --http_port $TVH_HTTP_PORT"
  [ -z "$TVH_HTTP_ROOT" ] || ARGS="$ARGS --http_root $TVH_HTTP_ROOT"
  [ -z "$TVH_HTSP_PORT" ] || ARGS="$ARGS --htsp_port $TVH_HTSP_PORT"
  [ "$TVH_DEBUG" = "1"  ] && ARGS="$ARGS -s"

  [ ! -z "$TVH_DELAY" ] && sleep $TVH_DELAY

  exec tvheadend $ARGS $TVH_ARGS
end script

2) Add the following script as /etc/pm/sleep.d/90-tvheadend (make sure it's set as executable):
Code:
#!/bin/sh

LOCKFILE="/var/lock/tvheadend.sleep"
STOP_SECS=10
LOGO="/usr/share/tvheadend/docs/docresources/tvheadendlogo.png"

# To install xbmc-send, install the xbmc-eventclients-xbmc-send package
XBMC_SEND="$(which xbmc-send)"

notify() {
    [ -z "${XBMC_SEND}" ] && return 0
    "${XBMC_SEND}" "$@"
}

get_pid() {
    # status tvheadend | grep "start/running" | sed -e 's/^.* //g'
    pidof tvheadend
}

do_start() {
    start tvheadend &>/dev/null
}

do_stop() {
    stop tvheadend &>/dev/null
    for seq in $(seq 1 ${STOP_SECS}) ; do
        [ -z "$(get_pid)" ] && return 0
        sleep 1
    done
    killall -9 tvheadend &>/dev/null
    [ -z "$(get_pid)" ]
}

case "${1}" in
    hibernate|suspend)
        if [ "$(pidof tvheadend)" ]; then
            notify -a "Notification(tvheadend, Shutting down HTS TVHeadend for suspend..., 5000, ${LOGO})"
            do_stop
            touch "${LOCKFILE}"
            exit ${?}
        fi
        ;;

    thaw|resume)
        if [ -f "${LOCKFILE}" ] ; then
            notify -a "Notification(tvheadend, Restarting HTS TVHeadend for wakeup, 5000, ${LOGO})"
            do_stop ${STOP_SECS} && rm -f "${LOCKFILE}"
            do_start
            exit ${?}
        fi
        ;;
esac
exit 0

Please note that the script is smart enough to function even if you don't have "xbmc-send" installed. However, it works best if you install it (xbmc-eventclients-xbmc-send package).

This should clear up any slow reboots/suspends.

Cheers!
Ok here's an updated upstart configuration, for your enjoyment:
Code:
# tvheadend - DVB/IPTV streaming server
#
# Tvheadend is a TV streaming server for Linux supporting DVB, ATSC, IPTV,
# and Analog video (V4L) as input sources.

description "Tvheadend DVB/IPTV streaming server"
author      "Adam Sutton <[email protected]>"

start on (local-filesystems and net-device-up and started udev-finish)
stop  on starting shutdown

expect fork
respawn

pre-start script
  [ -r /etc/default/tvheadend ] && . /etc/default/tvheadend
  if [ "$TVH_ENABLED" != "1" ] ; then
    stop
    exit 0
  fi

  [ -z "${TVH_DELAY_DVB}" ] && TVH_DELAY_DVB=0
  for n in $(seq 1 ${TVH_DELAY_DVB}) ; do
    DEVICES="$(find /dev -type c -path '/dev/dvb/adapter*/frontend*' | wc -l)"
    [ ${DEVICES} -gt 0 ] && break
    sleep 1 || true
  done
end script

script
  [ -r /etc/default/tvheadend ] && . /etc/default/tvheadend
  if [ "$TVH_ENABLED" != "1" ] ; then
    stop
    exit 0
  fi

  ARGS="-f"
  [ -z "$TVH_USER"      ] || ARGS="$ARGS -u $TVH_USER"
  [ -z "$TVH_GROUP"     ] || ARGS="$ARGS -g $TVH_GROUP"
  [ -z "$TVH_ADAPTERS"  ] || ARGS="$ARGS -a $TVH_ADAPTERS"
  [ "$TVH_IPV6" == "1"   ] && ARGS="$ARGS -6"
  [ -z "$TVH_HTTP_PORT" ] || ARGS="$ARGS --http_port $TVH_HTTP_PORT"
  [ -z "$TVH_HTTP_ROOT" ] || ARGS="$ARGS --http_root $TVH_HTTP_ROOT"
  [ -z "$TVH_HTSP_PORT" ] || ARGS="$ARGS --htsp_port $TVH_HTSP_PORT"
  [ "$TVH_DEBUG" == "1"  ] && ARGS="$ARGS -s"
  [ -z "$TVH_CONF_DIR"  ] || ARGS="$ARGS -c $TVH_CONF_DIR"


  exec tvheadend $ARGS $TVH_ARGS
end script

And the following variable must be added to /etc/default/tvheadend (can replace the old TVH_DELAY variable, which is no longer used):

Code:
# TVH_DELAY_DVB
#   The maximum number of seconds to wait for DVB hardware to become available.
#   Devices will be sought (using find) in /dev/dvb/adapter*/frontend* every second
#   until at least one becomes available, or the timer expires. At that point, startup
#   will continue normally. This has the added benefit of allowing hardware to initialize
#   prior to startup, but also eliminating the delay altogether when it is not needed, or
#   cutting it short as appropriate.
TVH_DELAY_DVB="30"

These changes will make the upstart script wait for up to 30 seconds for hardware to become available, and then continue startup. If no hardware becomes available, then startup is still performed regardless. However, the 30 second wait can be cut short the minute DVB devices become available. This means that it is also possible for no delay to be incurred whatsoever under optimal circumstances.

Hopefully, this will help someone out there.