forked from ngoral/nginx-rtmp-backup
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.sh
87 lines (73 loc) · 2.48 KB
/
utils.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# This file contains functions required by other nginx-rtmp-backup scripts
die() { # Exit with the proper stderr output
echo "$CURRENT_APPLICATION_NAME: $*" >&2
exit 1
}
parse_argv() { # Checks that the streamname for scripts is provided
# and set the variable
[ "$#" -ge 1 ] || die "too few arguments"
STREAMNAME=$1
}
pid_for() { # Gets a pid, according to stream kind (main/backup) and streamname
echo "$PIDS_FOLDER/$1_$STREAMNAME.pid"
}
is_running() { # Checks if the process pushing stream
# of provided kind (main/backup) is running
pidfile="$(pid_for "$1")"
echo $pidfile
[ -r "$pidfile" ] && pgrep --pidfile "$pidfile"
}
get_var() { # Gets variable value by its name
eval echo "$"$1""
}
kill() { # Kills a process pushing stream of provided kind (main/backup)
# if it is running and removes its pidfile
if is_running "$1"; then
pidfile="$(pid_for "$1")"
echo "got pidfile for killing"
/bin/kill -9 "$(cat "$pidfile")" > /dev/null
rm -f "$pidfile"
fi
}
assert_one_of() { # Checks that the value for a variable provided in config is rigth
varname="$1"; shift # Get variable name and remove it from arguments list
value="$(get_var $varname)"
expected="$*" # Values left in arguments list are expected values
while [ "$#" -gt 0 ]; do
if [ "$value" = "$1" ]; then return; fi # If a value of the varibale is one of the expected, return
shift
done
# If we are here, the variable value do not match any of expected, exit
die "unexpected value \`$value' for \`$varname' (expected one of '$expected')"
}
push_stream() { # Starts pushing stream
stream_kind="$1" # backup or main
# Get a value of either $MAIN_STREAM_NAME or $BACKUP_STREAM_NAME
appname="$(get_var "$(echo "${stream_kind}_STREAM_APPNAME" | tr '[:lower:]' '[:upper:]')")"
LOGFILE="$LOGS_FOLDER/${appname}_${STREAMNAME}.log"
assert_one_of RUNNER gst avconv ffmpeg
if [ "$RUNNER" = "gst" ]; then
nohup gst-launch-1.0 \
rtmpsrc location="rtmp://localhost/$appname/$STREAMNAME" do-timestamp=true ! queue ! flvdemux name=demux \
flvmux name=mux \
demux.video ! queue ! mux.video \
demux.audio ! queue ! mux.audio \
mux.src ! queue ! rtmpsink location="rtmp://localhost/$OUT_STREAM_APPNAME/$STREAMNAME" \
\
</dev/null \
>"$LOGFILE" \
2>&1 \
&
else
nohup "$RUNNER" \
-re -i "rtmp://localhost/$appname/$STREAMNAME" \
-c copy -f flv \
"rtmp://localhost/$OUT_STREAM_APPNAME/$STREAMNAME" \
\
</dev/null \
>"$LOGFILE" \
2>&1 \
&
fi
echo $! > "$(pid_for "$stream_kind")"
}