From 8d0c04ccd5853e9a5e24e9cdbabbb4ebb5b7f179 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Sat, 1 Jul 2023 16:38:36 +0200 Subject: [PATCH] http: add `--http-listen=`, and listen by default on `127.0.0.1` (#81) Co-authored-by: AndrolGenhald --- cmd/camera-streamer/opts.c | 2 ++ service/camera-streamer-arducam-16MP.service | 2 ++ service/camera-streamer-arducam-64MP.service | 2 ++ .../camera-streamer-generic-usb-cam.service | 2 ++ service/camera-streamer-raspi-usb-cam.service | 2 ++ service/camera-streamer-raspi-v2-8MP.service | 2 ++ service/camera-streamer-raspi-v3-12MP.service | 2 ++ tools/csi_camera.sh | 1 + tools/libcamera_camera.sh | 1 + tools/usb_camera.sh | 5 ++++- util/http/http.c | 21 ++++++++++++++----- util/http/http.h | 1 + 12 files changed, 37 insertions(+), 6 deletions(-) diff --git a/cmd/camera-streamer/opts.c b/cmd/camera-streamer/opts.c index 756dd29..d535222 100644 --- a/cmd/camera-streamer/opts.c +++ b/cmd/camera-streamer/opts.c @@ -46,6 +46,7 @@ camera_options_t camera_options = { }; http_server_options_t http_options = { + .listen = "127.0.0.1", .port = 8080, .maxcons = 10 }; @@ -126,6 +127,7 @@ option_t all_options[] = { DEFINE_OPTION_DEFAULT(camera, list_options, bool, "1", "List all available options and exit."), + DEFINE_OPTION_PTR(http, listen, string, "Set the IP address the HTTP web-server will bind to. Set to 0.0.0.0 to listen on all interfaces."), DEFINE_OPTION(http, port, uint, "Set the HTTP web-server port."), DEFINE_OPTION(http, maxcons, uint, "Set maximum number of concurrent HTTP connections."), diff --git a/service/camera-streamer-arducam-16MP.service b/service/camera-streamer-arducam-16MP.service index 09d3af4..dffecb9 100644 --- a/service/camera-streamer-arducam-16MP.service +++ b/service/camera-streamer-arducam-16MP.service @@ -23,6 +23,8 @@ ExecStart=/usr/local/bin/camera-streamer \ -camera-options=brightness=0.1 \ ; disable auto-focus -camera-auto_focus=1 \ + --http-listen=0.0.0.0 \ + --http-port=8080 \ -rtsp-port DynamicUser=yes diff --git a/service/camera-streamer-arducam-64MP.service b/service/camera-streamer-arducam-64MP.service index 4afdd1e..af9924a 100644 --- a/service/camera-streamer-arducam-64MP.service +++ b/service/camera-streamer-arducam-64MP.service @@ -25,6 +25,8 @@ ExecStart=/usr/local/bin/camera-streamer \ -camera-options=brightness=0.1 \ ; disable auto-focus -camera-auto_focus=0 \ + --http-listen=0.0.0.0 \ + --http-port=8080 \ -rtsp-port DynamicUser=yes diff --git a/service/camera-streamer-generic-usb-cam.service b/service/camera-streamer-generic-usb-cam.service index 822deb3..b9287e5 100644 --- a/service/camera-streamer-generic-usb-cam.service +++ b/service/camera-streamer-generic-usb-cam.service @@ -11,6 +11,8 @@ ExecStart=/usr/local/bin/camera-streamer \ -camera-fps=30 \ ; use two memory buffers to optimise usage -camera-nbufs=3 \ + --http-listen=0.0.0.0 \ + --http-port=8080 \ ; disable video streaming (WebRTC, RTSP, H264) ; on non-supported platforms -camera-video.disabled diff --git a/service/camera-streamer-raspi-usb-cam.service b/service/camera-streamer-raspi-usb-cam.service index 38e603b..15f59d2 100644 --- a/service/camera-streamer-raspi-usb-cam.service +++ b/service/camera-streamer-raspi-usb-cam.service @@ -17,6 +17,8 @@ ExecStart=/usr/local/bin/camera-streamer \ -camera-video.height=720 \ ; the stream is 853x480 -camera-stream.height=480 \ + --http-listen=0.0.0.0 \ + --http-port=8080 \ -rtsp-port DynamicUser=yes diff --git a/service/camera-streamer-raspi-v2-8MP.service b/service/camera-streamer-raspi-v2-8MP.service index d3a7e14..58d7624 100644 --- a/service/camera-streamer-raspi-v2-8MP.service +++ b/service/camera-streamer-raspi-v2-8MP.service @@ -24,6 +24,8 @@ ExecStart=/usr/local/bin/camera-streamer \ -camera-stream.height=480 \ ; bump brightness slightly -camera-options=brightness=0.1 \ + --http-listen=0.0.0.0 \ + --http-port=8080 \ -rtsp-port DynamicUser=yes diff --git a/service/camera-streamer-raspi-v3-12MP.service b/service/camera-streamer-raspi-v3-12MP.service index 35c5c6a..f68c758 100644 --- a/service/camera-streamer-raspi-v3-12MP.service +++ b/service/camera-streamer-raspi-v3-12MP.service @@ -25,6 +25,8 @@ ExecStart=/usr/local/bin/camera-streamer \ ; enable continuous autofocus -camera-options=AfMode=2 \ -camera-options=AfRange=2 \ + --http-listen=0.0.0.0 \ + --http-port=8080 \ -rtsp-port DynamicUser=yes diff --git a/tools/csi_camera.sh b/tools/csi_camera.sh index 10b367d..fa81ae4 100755 --- a/tools/csi_camera.sh +++ b/tools/csi_camera.sh @@ -22,6 +22,7 @@ fi set -xeo pipefail make -j$(nproc) $GDB ./camera-streamer -camera-path="${CAMERA_PATH[0]}" \ + --http-listen=0.0.0.0 \ -camera-options=vertical_blanking=728 \ -camera-options=exposure=2444 \ -camera-options=analogue_gain=600 \ diff --git a/tools/libcamera_camera.sh b/tools/libcamera_camera.sh index 90d296d..adefd31 100755 --- a/tools/libcamera_camera.sh +++ b/tools/libcamera_camera.sh @@ -6,6 +6,7 @@ cd "$SCRIPT_DIR/.." set -xeo pipefail make -j$(nproc) $GDB ./camera-streamer \ + --http-listen=0.0.0.0 \ -camera-type=libcamera \ -camera-format=YUYV \ "$@" diff --git a/tools/usb_camera.sh b/tools/usb_camera.sh index 5e8e652..7cd8f32 100755 --- a/tools/usb_camera.sh +++ b/tools/usb_camera.sh @@ -7,4 +7,7 @@ CAMERA_PATH=( $(echo /dev/v4l/by-id/usb-*-video-index0) ) set -xeo pipefail make -j$(nproc) -$GDB ./camera-streamer -camera-path="${CAMERA_PATH[${CAMERA_INDEX:-0}]}" "$@" +$GDB ./camera-streamer \ + -camera-path="${CAMERA_PATH[${CAMERA_INDEX:-0}]}" + --http-listen=0.0.0.0 \ + "$@" diff --git a/util/http/http.c b/util/http/http.c index f50020a..56de433 100644 --- a/util/http/http.c +++ b/util/http/http.c @@ -19,19 +19,23 @@ #define HEADER_USER_AGENT "User-Agent:" #define HEADER_HOST "Host:" -static int http_listen(int port, int maxcons) +static int http_listen(char *addr4, int port, int maxcons) { struct sockaddr_in server = {0}; int listenfd = -1; + char listen_addr[INET_ADDRSTRLEN]; // getaddrinfo for host server.sin_family = AF_INET; - server.sin_addr.s_addr = INADDR_ANY; + if (inet_pton(server.sin_family, addr4, &server.sin_addr) != 1) { + perror("inet_pton"); + return -1; + } server.sin_port = htons(port); - listenfd = socket(AF_INET, SOCK_STREAM, 0); + listenfd = socket(server.sin_family, SOCK_STREAM, 0); if (listenfd < 0) { - perror("socket"); + LOG_INFO(NULL, "Invalid HTTP listen address: %s", addr4); return -1; } @@ -48,6 +52,13 @@ static int http_listen(int port, int maxcons) goto error; } + if (inet_ntop(server.sin_family, &server.sin_addr, listen_addr, sizeof(listen_addr)) == NULL) { + perror("inet_ntop"); + goto error; + } + + LOG_INFO(NULL, "HTTP listening on %s:%d.", listen_addr, port); + return listenfd; error: if (listenfd >= 0) @@ -240,7 +251,7 @@ static int http_worker(http_worker_t *worker) int http_server(http_server_options_t *options, http_method_t *methods) { - int listen_fd = http_listen(options->port, options->maxcons); + int listen_fd = http_listen(options->listen, options->port, options->maxcons); if (listen_fd < 0) { return -1; } diff --git a/util/http/http.h b/util/http/http.h index cf7b354..a26a66a 100644 --- a/util/http/http.h +++ b/util/http/http.h @@ -26,6 +26,7 @@ typedef struct http_method_s { } http_method_t; typedef struct http_server_options_s { + char listen[512]; unsigned port; unsigned maxcons; } http_server_options_t;