Fixed resync on lost segments
authorKristian Kræmmer Nielsen <jkkn@jkkn.dk>
Fri, 31 Mar 2017 16:52:18 +0000 (18:52 +0200)
committerKristian Kræmmer Nielsen <jkkn@jkkn.dk>
Fri, 31 Mar 2017 16:52:44 +0000 (18:52 +0200)
monitor-dai.sh

index 09fe0cfdbd447e6a9f3cb9e6b02b090d9f06b079..c89c46788c977f16485b5befae9afdf948829551 100755 (executable)
@@ -115,17 +115,7 @@ function monitorStream() {
     local lastUsedSegment="" lastUsedStreamTime=0 # Stored for use of recording previous segment when seeing ad block
     local doRecord=0 doStopRecord=0
     local syncMode=-1 # (-1=get first, x=saw last at, 0=synched) In syncmode we try to request every second until we see a change to get a more precise time sync
-
-    real_time=$(date +%s)
-
-    if [ -n "$streamDelay" ]; then
-        stream_time=$(($real_time - $streamDelay))
-        pretty_time=$($DATE${stream_time%.*} +"%Y-%m-%d %H:%M:%S")
-        echo >&2 "NOTICE: Using time-offset of -$streamDelay seconds so time is $pretty_time, real time is $(date ${realtime} +"%Y-%m-%d %H:%M:%S")"
-    else
-        stream_time="$real_time"
-        pretty_time=$($DATE${stream_time%.*} +"%Y-%m-%d %H:%M:%S")
-    fi
+    local stream_time="" # unknown
 
     echo >&2 "$pretty_time: INFO: Started monitoring using: $stream"
     
@@ -133,6 +123,19 @@ function monitorStream() {
         starttime=$(date +%s)
         warn_not_a_playlist=1
 
+        # Set time to real time first request
+        if [ -z "$stream_time" ]; then
+            if [ -n "$streamDelay" ]; then
+                stream_time=$(($starttime - $streamDelay))
+                pretty_time=$($DATE${stream_time%.*} +"%Y-%m-%d %H:%M:%S")
+                echo >&2 "NOTICE: Using time-offset of -${streamDelay}s. Stream time is $pretty_time, real time is $(date ${realtime} +"%Y-%m-%d %H:%M:%S")"
+            else
+                stream_time="$starttime"
+                pretty_time=$($DATE${stream_time%.*} +"%Y-%m-%d %H:%M:%S")
+                echo >&2 "NOTICE: Setting stream time to real time: $pretty_time"
+            fi
+        fi
+
         TMPFILE=$(mktemp) || fatal "Can not write tmp-file"
         curl $BASIC_CURL_PARAMS -o "$TMPFILE" "$stream"
         if [ $? -eq 0 ]; then
@@ -166,8 +169,9 @@ 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: WARN: Lost segments ($curSeq > $nextSeq)... resetting..."
-                                failed=10
+                                echo >&2 "$pretty_time: WARN: Lost segments ($curSeq > $nextSeq)."
+                                stream_time=
+                                syncMode=-1
                             fi
                             #echo >&2 "$pretty_time: Current mode length: $curLength"
                             nextSeq=$(($curSeq + 1))