-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtx_functions.sh
executable file
·261 lines (228 loc) · 10.3 KB
/
tx_functions.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
function tx_function {
killall wbc_status > /dev/null 2>&1
/home/pi/wifibroadcast-base/sharedmem_init_tx
if [ "$TXMODE" == "single" ]; then
echo -n "Waiting for wifi card to become ready ..."
COUNTER=0
# loop until card is initialized
while [ $COUNTER -lt 10 ]; do
sleep 0.5
echo -n "."
let "COUNTER++"
if [ -d "/sys/class/net/wlan0" ]; then
echo -n "card ready"
break
fi
done
else
# just wait some time
echo -n "Waiting for wifi cards to become ready ..."
sleep 3
fi
echo
echo
dmesg -c >/dev/null 2>/dev/null
detect_nics
sleep 1
echo
if [ -e "$FC_TELEMETRY_SERIALPORT" ]; then
echo "Configured serial port $FC_TELEMETRY_SERIALPORT found ..."
else
echo "ERROR: $FC_TELEMETRY_SERIALPORT not found!"
collect_errorlog
sleep 365d
fi
echo
RALINK=0
if [ "$TXMODE" == "single" ]; then
DRIVER=`cat /sys/class/net/$NICS/device/uevent | nice grep DRIVER | sed 's/DRIVER=//'`
case $DRIVER in
*881[24]au)
DRIVER=rtl88xxau
;;
esac
if [ "$DRIVER" != "ath9k_htc" ]; then # in single mode and ralink cards always, use frametype 1 (data)
VIDEO_FRAMETYPE=0
if [ "$DRIVER" == "rtl88xxau" ]; then
VIDEO_FRAMETYPE=1
fi
RALINK=1
fi
else # for txmode dual always use frametype 1
VIDEO_FRAMETYPE=1
RALINK=1
fi
if [ "$VIDEO_WIFI_BITRATE" == "19.5" ]; then # set back to 18 to make sure -d parameter works (supports only 802.11b/g datarates)
VIDEO_WIFI_BITRATE=18
fi
if [ "$VIDEO_WIFI_BITRATE" == "5.5" ]; then # set back to 6 to make sure -d parameter works (supports only 802.11b/g datarates)
VIDEO_WIFI_BITRATE=5
fi
DRIVER=`cat /sys/class/net/$NICS/device/uevent | nice grep DRIVER | sed 's/DRIVER=//'`
case $DRIVER in
*881[24]au)
DRIVER=rtl88xxau
;;
esac
if [ "$CTS_PROTECTION" == "auto" ] && [ "$DRIVER" == "ath9k_htc" ]; then # only use CTS protection with Atheros
echo -n "Checking for other wifi traffic ... "
WIFIPPS=`/home/pi/wifibroadcast-base/wifiscan $NICS`
echo -n "$WIFIPPS PPS: "
if [ "$WIFIPPS" != "0" ]; then # wifi networks detected, enable CTS
echo "Wifi traffic detected, CTS enabled"
VIDEO_FRAMETYPE=1
TELEMETRY_CTS=1
CTS=Y
else
echo "No wifi traffic detected, CTS disabled"
CTS=N
fi
else
if [ "$CTS_PROTECTION" == "N" ]; then
echo "CTS Protection disabled in config"
CTS=N
else
if [ "$DRIVER" == "ath9k_htc" ]; then
echo "CTS Protection enabled in config"
CTS=Y
else
echo "CTS Protection not supported!"
CTS=N
fi
fi
fi
# check if over-temp or under-voltage occured before bitrate measuring
if vcgencmd get_throttled | nice grep -q -v "0x0"; then
TEMP=`cat /sys/class/thermal/thermal_zone0/temp`
TEMP_C=$(($TEMP/1000))
if [ "$TEMP_C" -lt 75 ]; then # it must be under-voltage
echo
echo " ---------------------------------------------------------------------------------------------------"
echo " | ERROR: Under-Voltage detected on the TX Pi. Your Pi is not supplied with stable 5 Volts. |"
echo " | Either your power-supply or wiring is not sufficent, check the wiring instructions in the Wiki. |"
echo " | Video Bitrate will be reduced to 1000kbit to reduce current consumption! |"
echo " ---------------------------------------------------------------------------------------------------"
echo
mount -o remount,rw /boot
echo " ---------------------------------------------------------------------------------------------------" >> /boot/UNDERVOLTAGE-ERROR!!!.txt
echo " | ERROR: Under-Voltage detected on the TX Pi. Your Pi is not supplied with stable 5 Volts. |" >> /boot/UNDERVOLTAGE-ERROR!!!.txt
echo " | Either your power-supply or wiring is not sufficent, check the wiring instructions in the Wiki. |" >> /boot/UNDERVOLTAGE-ERROR!!!.txt
echo " | Video Bitrate will be reduced to 1000kbit to reduce current consumption! |" >> /boot/UNDERVOLTAGE-ERROR!!!.txt
echo " | When you have fixed wiring/power-supply, delete this file and make sure it doesn't re-appear! |" >> /boot/UNDERVOLTAGE-ERROR!!!.txt
echo " ---------------------------------------------------------------------------------------------------" >> /boot/UNDERVOLTAGE-ERROR!!!.txt
mount -o remount,ro /boot
UNDERVOLT=1
echo "1" > /tmp/undervolt
else # it was either over-temp or both undervolt and over-temp, we set undervolt to 0 anyway, since overtemp can be seen at the temp display on the rx
UNDERVOLT=0
echo "0" > /tmp/undervolt
fi
else
UNDERVOLT=0
echo "0" > /tmp/undervolt
fi
# if yes, we don't do the bitrate measuring to increase chances we "survive"
if [ "$UNDERVOLT" == "0" ]; then
if [ "$VIDEO_BITRATE" == "auto" ]; then
echo -n "Measuring max. available bitrate .. "
BITRATE_MEASURED=`/home/pi/wifibroadcast-base/tx_measure -p 77 -b $VIDEO_BLOCKS -r $VIDEO_FECS -f $VIDEO_BLOCKLENGTH -t $VIDEO_FRAMETYPE -d $VIDEO_WIFI_BITRATE -y 0 $NICS`
BITRATE=$((BITRATE_MEASURED*$BITRATE_PERCENT/100))
BITRATE_KBIT=$((BITRATE/1000))
BITRATE_MEASURED_KBIT=$((BITRATE_MEASURED/1000))
echo "$BITRATE_MEASURED_KBIT kBit/s * $BITRATE_PERCENT% = $BITRATE_KBIT kBit/s video bitrate"
else
BITRATE=$(($VIDEO_BITRATE*1000))
echo "Using fixed bitrate: $VIDEO_BITRATE kBit"
fi
else
BITRATE=$((1000*1000))
BITRATE_KBIT=1000
BITRATE_MEASURED_KBIT=2000
echo "Using reduced bitrate: 1000 kBit due to undervoltage!"
fi
# check again if over-temp or under-voltage occured after bitrate measuring (but only if it didn't occur before yet)
if [ "$UNDERVOLT" == "0" ]; then
if vcgencmd get_throttled | nice grep -q -v "0x0"; then
TEMP=`cat /sys/class/thermal/thermal_zone0/temp`
TEMP_C=$(($TEMP/1000))
if [ "$TEMP_C" -lt 75 ]; then # it must be under-voltage
echo
echo " ---------------------------------------------------------------------------------------------------"
echo " | ERROR: Under-Voltage detected on the TX Pi. Your Pi is not supplied with stable 5 Volts. |"
echo " | Either your power-supply or wiring is not sufficent, check the wiring instructions in the Wiki. |"
echo " | Video Bitrate will be reduced to 1000kbit to reduce current consumption! |"
echo " ---------------------------------------------------------------------------------------------------"
echo
mount -o remount,rw /boot
echo " ---------------------------------------------------------------------------------------------------" >> /boot/UNDERVOLTAGE-ERROR!!!.txt
echo " | ERROR: Under-Voltage detected on the TX Pi. Your Pi is not supplied with stable 5 Volts. |" >> /boot/UNDERVOLTAGE-ERROR!!!.txt
echo " | Either your power-supply or wiring is not sufficent, check the wiring instructions in the Wiki. |" >> /boot/UNDERVOLTAGE-ERROR!!!.txt
echo " | Video Bitrate will be reduced to 1000kbit to reduce current consumption! |" >> /boot/UNDERVOLTAGE-ERROR!!!.txt
echo " | When you have fixed wiring/power-supply, delete this file and make sure it doesn't re-appear! |" >> /boot/UNDERVOLTAGE-ERROR!!!.txt
echo " ---------------------------------------------------------------------------------------------------" >> /boot/UNDERVOLTAGE-ERROR!!!.txt
mount -o remount,ro /boot
UNDERVOLT=1
echo "1" > /tmp/undervolt
BITRATE=$((1000*1000))
BITRATE_KBIT=1000
BITRATE_MEASURED_KBIT=2000
else # it was either over-temp or both undervolt and over-temp, we set undervolt to 0 anyway, since overtemp can be seen at the temp display on the rx
UNDERVOLT=0
echo "0" > /tmp/undervolt
fi
else
UNDERVOLT=0
echo "0" > /tmp/undervolt
fi
fi
# check for over-current on USB bus (due to card being powered via usb instead directly)
if nice dmesg | nice grep -q over-current; then
echo "ERROR: Over-current detected - potential power supply problems!"
collect_errorlog
sleep 365d
fi
# check for USB disconnects (due to power-supply problems)
if nice dmesg | nice grep -q disconnect; then
echo "ERROR: USB disconnect detected - potential power supply problems!"
collect_errorlog
sleep 365d
fi
echo $BITRATE_KBIT > /tmp/bitrate_kbit
echo $BITRATE_MEASURED_KBIT > /tmp/bitrate_measured_kbit
if [ "$CTS" == "N" ]; then
echo "0" > /tmp/cts
else
if [ "$VIDEO_WIFI_BITRATE" == "11" ] || [ "$VIDEO_WIFI_BITRATE" == "5" ]; then # 11mbit and 5mbit bitrates don't support CTS, so set to 0
echo "0" > /tmp/cts
else
echo "1" > /tmp/cts
fi
fi
if [ "$DEBUG" == "Y" ]; then
collect_debug /boot &
fi
/home/pi/wifibroadcast-base/rssitx $NICS &
echo
echo "Starting transmission in $TXMODE mode, FEC $VIDEO_BLOCKS/$VIDEO_FECS/$VIDEO_BLOCKLENGTH: $WIDTH x $HEIGHT $FPS fps, video bitrate: $BITRATE_KBIT kBit/s, Keyframerate: $KEYFRAMERATE"
nice -n -9 raspivid -w $WIDTH -h $HEIGHT -fps $FPS -b $BITRATE -g $KEYFRAMERATE -t 0 $EXTRAPARAMS -o - | nice -n -9 /home/pi/wifibroadcast-base/tx_rawsock -p 0 -b $VIDEO_BLOCKS -r $VIDEO_FECS -f $VIDEO_BLOCKLENGTH -t $VIDEO_FRAMETYPE -d $VIDEO_WIFI_BITRATE -y 0 $NICS
# Older video transmit commands, might be usefull for USB webcams...
# v4l2-ctl -d /dev/video0 --set-fmt-video=width=1280,height=720,pixelformat='H264' -p 48 --set-ctrl video_bitrate=7000000,repeat_sequence_header=1,h264_i_frame_period=7,white_balance_auto_preset=5
# nice -n -9 cat /dev/video0 | /home/pi/wifibroadcast-base/tx_rawsock -p 0 -b $VIDEO_BLOCKS -r $VIDEO_FECS -f $VIDEO_BLOCKLENGTH -t $VIDEO_FRAMETYPE -d $VIDEO_WIFI_BITRATE -y 0 $NICS
TX_EXITSTATUS=${PIPESTATUS[1]}
# if we arrive here, either raspivid or tx did not start, or were terminated later
# check if NIC has been removed
NICS2=`ls /sys/class/net/ | nice grep -v eth0 | nice grep -v lo | nice grep -v usb | nice grep -v intwifi | nice grep -v relay | nice grep -v wifihotspot`
if [ "$NICS" == "$NICS2" ]; then
# wifi card has not been removed
if [ "$TX_EXITSTATUS" != "0" ]; then
echo "ERROR: could not start tx or tx terminated!"
fi
collect_errorlog
sleep 365d
else
# wifi card has been removed
echo "ERROR: Wifi card removed!"
collect_errorlog
sleep 365d
fi
}