Skip to content

Commit

Permalink
http: add --http-listen=<ip4>, and listen by default on 127.0.0.1 (
Browse files Browse the repository at this point in the history
…#81)

Co-authored-by: AndrolGenhald <AndrolGenhald@gmail.com>
  • Loading branch information
ayufan and AndrolGenhald committed Jul 1, 2023
1 parent f1966ab commit 8d0c04c
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 6 deletions.
2 changes: 2 additions & 0 deletions cmd/camera-streamer/opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ camera_options_t camera_options = {
};

http_server_options_t http_options = {
.listen = "127.0.0.1",
.port = 8080,
.maxcons = 10
};
Expand Down Expand Up @@ -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."),

Expand Down
2 changes: 2 additions & 0 deletions service/camera-streamer-arducam-16MP.service
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions service/camera-streamer-arducam-64MP.service
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions service/camera-streamer-generic-usb-cam.service
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions service/camera-streamer-raspi-usb-cam.service
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions service/camera-streamer-raspi-v2-8MP.service
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions service/camera-streamer-raspi-v3-12MP.service
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tools/csi_camera.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions tools/libcamera_camera.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
"$@"
5 changes: 4 additions & 1 deletion tools/usb_camera.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
"$@"
21 changes: 16 additions & 5 deletions util/http/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions util/http/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8d0c04c

Please # to comment.