This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
run.sh
executable file
·405 lines (379 loc) · 10.6 KB
/
run.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/bin/bash -e
#
# Copyright (C) 2019-2020 Intel Corporation.
#
# SPDX-License-Identifier: BSD-3-Clause
#
RUN_PREFIX=
MODELS=
PIPELINES=
FRAMEWORK=
IMAGE=
VOLUME_MOUNT=
MODE=SERVICE
PORTS=
DEVICES=
GPU_DEVICE=
DEFAULT_GSTREAMER_IMAGE="dlstreamer-pipeline-server-gstreamer"
DEFAULT_FFMPEG_IMAGE="dlstreamer-pipeline-server-ffmpeg"
ENTRYPOINT=
ENTRYPOINT_ARGS=
PRIVILEGED=
NETWORK=
USER=
INTERACTIVE=-it
DEVICE_CGROUP_RULE=
USER_GROUPS=
ENABLE_RTSP=${ENABLE_RTSP:-"false"}
ENABLE_WEBRTC=${ENABLE_WEBRTC:-"false"}
RTSP_PORT=8554
HOST_NAME=
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
SOURCE_DIR=$(dirname $SCRIPT_DIR)
ENVIRONMENT=$(env | cut -f1 -d= | grep -E '_(proxy)$' | sed 's/^/-e / ' | tr '\n' ' ')
show_options() {
echo ""
echo "Running Pipeline Server Image: '${IMAGE}'"
echo " Models: '${MODELS}'"
echo " Pipelines: '${PIPELINES}'"
echo " Framework: '${FRAMEWORK}'"
echo " Environment: '${ENVIRONMENT}'"
echo " Volume Mounts: '${VOLUME_MOUNT}'"
echo " Mode: '${MODE}'"
echo " Ports: '${PORTS}'"
echo " Name: '${NAME}'"
echo " Network: '${NETWORK}'"
echo " Hostname: '${HOST_NAME}'"
echo " Entrypoint: '${ENTRYPOINT}'"
echo " EntrypointArgs: '${ENTRYPOINT_ARGS}'"
echo " User: '${USER}'"
echo " User Groups: '${USER_GROUPS}'"
echo " Devices: '${DEVICES}'"
echo " Device CGroup Rule: '${DEVICE_CGROUP_RULE}'"
echo ""
}
show_help() {
echo "usage: run.sh"
echo " [--image image]"
echo " [--framework ffmpeg || gstreamer]"
echo " [--models path to models directory]"
echo " [--pipelines path to pipelines directory]"
echo " [-v additional volume mount to pass to docker run]"
echo " [-e additional environment to pass to docker run]"
echo " [--entrypoint-args additional parameters to pass to entrypoint in docker run]"
echo " [-p additional ports to pass to docker run]"
echo " [--network name network to pass to docker run]"
echo " [--hostname set hostname of the container to pass to docker run]"
echo " [--user name of user to pass to docker run]"
echo " [--group-add name of user group to pass to docker run]"
echo " [--name container name to pass to docker run]"
echo " [--gpu-device select GPU device]"
echo " [--device device to pass to docker run]"
echo " [--enable-rtsp To enable rtsp re-streaming]"
echo " [--disable-http-port Specify to close web service port e.g. 8080 in docker]"
echo " [--rtsp-port Specify the port to use for rtsp re-streaming]"
echo " [--enable-webrtc To enable WebRTC frame destination]"
echo " [--dev run in developer mode]"
exit 0
}
error() {
printf '%s\n' "$1" >&2
exit
}
enable_hardware_access() {
# GPU
if [ -z $GPU_DEVICE ]; then
if [ -e /dev/dri/renderD128 ] ; then
GPU_DEVICE="/dev/dri/renderD128"
echo "Found $GPU_DEVICE - enabling GPU"
fi
fi
if [ ! -z $GPU_DEVICE ]; then
if [ ! -e $GPU_DEVICE ]; then
echo GPU device $GPU_DEVICE not found - exiting
exit 1
fi
DEVICES+="--device $GPU_DEVICE "
ENVIRONMENT+="-e GST_VAAPI_DRM_DEVICE=$GPU_DEVICE "
render_group=$(stat -c '%g' $GPU_DEVICE)
USER_GROUPS+="--group-add $render_group "
fi
# Intel(R) NCS2
if [ -d /dev/bus/usb ]; then
echo "Found /dev/bus/usb - enabling for Intel(R) NCS2"
DEVICE_CGROUP_RULE=--device-cgroup-rule=\'c\ 189:*\ rmw\'
VOLUME_MOUNT+="-v /dev/bus/usb:/dev/bus/usb "
fi
# HDDL
if compgen -G /dev/myriad* > /dev/null ; then
echo "Found /dev/myriad devices - enabling for HDDL-R"
VOLUME_MOUNT+="-v /var/tmp:/var/tmp -v /dev/shm:/dev/shm "
fi
# Webcam
for device in $(ls /dev | grep video); do
echo "Found /dev/$device - enabling webcam"
DEVICES+="--device /dev/$device "
done
# Microphone
if [ -e /dev/snd ]; then
echo "Found /dev/snd - enabling microphone"
DEVICES+="--device /dev/snd "
fi
}
while [[ "$#" -gt 0 ]]; do
case $1 in
-h | -\? | --help)
show_help # Display a usage synopsis.
exit
;;
--dry-run)
RUN_PREFIX=echo
;;
--image) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
IMAGE=$2
shift
else
error 'ERROR: "--image" requires a non-empty option argument.'
fi
;;
--models)
if [ "$2" ]; then
MODELS=$(realpath $2)
shift
else
error 'ERROR: "--models" requires a non-empty option argument.'
fi
;;
--user)
if [ "$2" ]; then
USER="--user $2"
shift
else
error 'ERROR: "--user" requires a non-empty option argument.'
fi
;;
--group-add)
if [ "$2" ]; then
USER_GROUPS+="--group-add $2 "
shift
else
error 'ERROR: "--group-add" requires a non-empty option argument.'
fi
;;
--device)
if [ "$2" ]; then
DEVICES+="--device $2 "
shift
else
error 'ERROR: "--device" requires a non-empty option argument.'
fi
;;
--gpu-device)
if [ "$2" ]; then
GPU_DEVICE=$2
shift
else
error 'ERROR: "--gpu-device" requires a non-empty option argument.'
fi
;;
--privileged)
PRIVILEGED="--privileged "
;;
--device-cgroup-rule)
if [ "$2" ]; then
DEVICE_CGROUP_RULE="--device-cgroup-rule=$2 "
shift
else
error 'ERROR: "--device-cgroup-rule" requires a non-empty option argument.'
fi
;;
--pipelines)
if [ "$2" ]; then
PIPELINES=$(realpath $2)
shift
else
error 'ERROR: "--pipelines" requires a non-empty option argument.'
fi
;;
--framework)
if [ "$2" ]; then
FRAMEWORK=$2
shift
else
error 'ERROR: "--framework" requires a non-empty option argument.'
fi
;;
-e)
if [ "$2" ]; then
ENVIRONMENT+="-e $2 "
shift
else
error 'ERROR: "-e" requires a non-empty option argument.'
fi
;;
--entrypoint-args)
if [ "$2" ]; then
ENTRYPOINT_ARGS+="$2 "
shift
else
error 'ERROR: "--entrypoint-args" requires a non-empty option argument.'
fi
;;
-p)
if [ "$2" ]; then
PORTS+="-p $2 "
shift
else
error 'ERROR: "-p" requires a non-empty option argument.'
fi
;;
-v)
if [ "$2" ]; then
VOLUME_MOUNT+="-v $2 "
shift
else
error 'ERROR: "-v" requires a non-empty option argument.'
fi
;;
--dev)
MODE=DEV
;;
--name)
if [ "$2" ]; then
NAME=$2
shift
else
error 'ERROR: "--name" requires a non-empty option argument.'
fi
;;
--network)
if [ "$2" ]; then
NETWORK="--network $2"
shift
else
error 'ERROR: "--network" requires a non-empty option argument.'
fi
;;
--entrypoint)
if [ "$2" ]; then
ENTRYPOINT="--entrypoint $2"
shift
else
error 'ERROR: "--entrypoint" requires a non-empty option argument.'
fi
;;
--rtsp-port)
if [ "$2" ]; then
RTSP_PORT=$2
shift
else
error 'ERROR: "--rtsp-port" requires a non-empty option argument.'
fi
;;
--hostname)
if [ "$2" ]; then
HOST_NAME="--hostname "$2
shift
else
error 'ERROR: "--hostname" requires a non-empty option argument.'
fi
;;
--disable-http-port)
MODE=DISABLE_HTTP_PORT
;;
--enable-rtsp)
ENABLE_RTSP=true
;;
--enable-webrtc)
ENABLE_WEBRTC=true
;;
--non-interactive)
unset INTERACTIVE
;;
--) # End of all options.
shift
break
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*) # Default case: No more options, so break out of the loop.
break ;;
esac
shift
done
if [ -z "$FRAMEWORK" ]; then
FRAMEWORK="gstreamer"
elif [ $FRAMEWORK != 'gstreamer' ] && [ $FRAMEWORK != 'ffmpeg' ]; then
echo "Invalid framework"
show_help
fi
if [ -z "$IMAGE" ]; then
IMAGE=DEFAULT_${FRAMEWORK^^}_IMAGE
IMAGE=${!IMAGE}
fi
if [ -z "$NAME" ]; then
# Convert tag separator if exists
NAME=${IMAGE//[\:\/]/_}
fi
if [ "${MODE}" == "DEV" ]; then
VOLUME_MOUNT+="-v $SOURCE_DIR:/home/pipeline-server/ "
VOLUME_MOUNT+="-v /tmp:/tmp "
VOLUME_MOUNT+="-v /dev:/dev "
if [ -z "$NETWORK" ]; then
NETWORK="--network=host"
fi
if [ -z "$ENTRYPOINT" ]; then
ENTRYPOINT="--entrypoint /bin/bash"
fi
if [ -z "$MODELS" ]; then
MODELS=$SOURCE_DIR/models
fi
if [ -z "$PIPELINES" ]; then
PIPELINES=$SOURCE_DIR/pipelines/$FRAMEWORK
fi
PRIVILEGED="--privileged "
elif [ ! -z "$ENTRYPOINT" ]; then
MODE=CUSTOM_ENTRYPOINT
elif [ "${MODE}" == "SERVICE" ]; then
if [ -z "$PORTS" ]; then
PORTS+="-p 8080:8080 "
fi
elif [ "${MODE}" == "DISABLE_HTTP_PORT" ]; then
echo "HTTP Web Service port has been disabled on Docker!"
else
echo "Invalid Mode"
show_help
fi
enable_hardware_access
if [ "$ENABLE_RTSP" != "false" ]; then
ENVIRONMENT+="-e ENABLE_RTSP=$ENABLE_RTSP -e RTSP_PORT=$RTSP_PORT "
PORTS+="-p $RTSP_PORT:$RTSP_PORT "
fi
if [ "$ENABLE_WEBRTC" != "false" ]; then
ENVIRONMENT+="-e ENABLE_WEBRTC=$ENABLE_WEBRTC "
fi
if [[ ! -z "${MAX_BODY_SIZE}" ]]; then
ENVIRONMENT+="-e MAX_BODY_SIZE=$MAX_BODY_SIZE "
fi
if [ ! -z "$MODELS" ]; then
VOLUME_MOUNT+="-v $MODELS:/home/pipeline-server/models "
fi
if [ ! -z "$PIPELINES" ]; then
VOLUME_MOUNT+="-v $PIPELINES:/home/pipeline-server/pipelines "
fi
if [ ! -z "$VOLUME_MOUNT" ]; then
if [ -z "$USER" ]; then
USER="--user $UID"
fi
fi
if [ ! -z "$USER" ]; then
for group in "audio" "users"
do
USER_GROUPS+="--group-add $group "
done
fi
show_options
# eval must be used to ensure the --device-cgroup-rule string is correctly parsed
eval "$RUN_PREFIX docker run $INTERACTIVE --rm $ENVIRONMENT $VOLUME_MOUNT $DEVICE_CGROUP_RULE $DEVICES $NETWORK $HOST_NAME $PORTS $ENTRYPOINT --name ${NAME} ${PRIVILEGED} ${USER} $USER_GROUPS $IMAGE ${ENTRYPOINT_ARGS}"