Added handling of failed stream - reset
authorKristian Kræmmer Nielsen <jkkn@jkkn.dk>
Wed, 1 Mar 2017 12:10:54 +0000 (13:10 +0100)
committerKristian Kræmmer Nielsen <jkkn@jkkn.dk>
Wed, 1 Mar 2017 12:10:54 +0000 (13:10 +0100)
monitor-dai.sh

index dba88cdeb2359a2d550fbf07644f8b236d537cd1..553fbad134db67da2e2e0ec295b51ed82fe75e07 100755 (executable)
@@ -102,6 +102,8 @@ function monitorStream() {
     local expectedAdLength=-1
     local mode="UNKNOWN"
     local curLength=0
+    local failed=0
+    local target_duration=$TARGET_DURATION
 
     real_time=$(date +%s)
     stream_time=$(($real_time - $STREAM_TIME_SCREW))
@@ -134,7 +136,7 @@ function monitorStream() {
                         stream_time=$(echo "$stream_time + $nextSegmentLength" | bc)
                         pretty_time=$($DATE${stream_time%.*} +"%Y-%m-%d %H:%M:%S")
                         if [[ $curSeq -ne $nextSeq ]]; then
-                            echo >&2 "$pretty_time: Skipped a segment by mistake ($curSeq > $nextSeq)"
+                            echo >&2 "$pretty_time: WARN: Skipped a segment by mistake ($curSeq > $nextSeq)"
                         fi
                         #echo >&2 "$pretty_time: Current mode length: $curLength"
                         nextSeq=$(($curSeq + 1))
@@ -149,9 +151,9 @@ function monitorStream() {
                 tag="${BASH_REMATCH[1]}"
                 value="${BASH_REMATCH[2]}"
                 if [ "$tag" == "EXT-X-TARGETDURATION" ]; then
-                    if [ "$value" != "$TARGET_DURATION" ]; then
-                        echo "$pretty_time: Changing target durationt to: $value"
-                        TARGET_DURATION="$value"
+                    if [ "$value" != "$target_duration" ]; then
+                        echo "$pretty_time: Changing target duration to: $value"
+                        target_duration="$value"
                     fi
                 elif [ "$tag" == "EXTINF" ]; then
                     nextSegmentLength=$value
@@ -193,25 +195,37 @@ function monitorStream() {
 
            if [ $warn_not_a_playlist -eq 1 ]; then
                 echo >&2 "$pretty_time: ERROR: Not a valid playlist ($stream)"
+                failed=$(($failed + 1))
+           else
+                failed=0
            fi
             
         else
-           echo >&2 "$pretty_time: Unable to request: $stream" 
+            echo >&2 "$pretty_time: Unable to request: $stream" 
+            failed=$(($failed + 1))
         fi
         rm "$TMPFILE"
 
         endtime=$(date +%s)
-        waittime=$(($TARGET_DURATION - ($endtime-$starttime)))
+        waittime=$(($target_duration - ($endtime-$starttime)))
         if [ "$waittime" -gt 0 ]; then
             sleep $waittime
         fi
+
+        if [ "$failed" -ge 2 ]; then
+            echo >&2 "$pretty_time: ERROR: Stream failed twice.. resetting..."
+            return
+        fi
     done
 }
 
-streamurl=$(resolveFirstStream "$1")
-if [ -e "$streamurl" ]; then
-    echo >&2 "Invalid HLS-stream: $1"
-fi
+while true; do
+    streamurl=$(resolveFirstStream "$1")
+    if [ -e "$streamurl" ]; then
+        echo >&2 "Invalid HLS-stream: $1"
+        sleep 10
+    fi
 
+    monitorStream "$streamurl"
+done
 
-monitorStream "$streamurl"