From 8c1db3839881ce5544ff3dcae2e82c06cb3790c9 Mon Sep 17 00:00:00 2001 From: fwastring Date: Sun, 29 Mar 2026 14:30:34 +0200 Subject: [PATCH] Added RTSP cam script --- maskiner/laptop/configuration.nix | 9 ++++ moduler/services/mediamtx/default.nix | 41 +++++++++++++++++ moduler/services/webcam-rtsp/default.nix | 57 ++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 moduler/services/mediamtx/default.nix create mode 100644 moduler/services/webcam-rtsp/default.nix diff --git a/maskiner/laptop/configuration.nix b/maskiner/laptop/configuration.nix index 4ae9d12..6877f3e 100644 --- a/maskiner/laptop/configuration.nix +++ b/maskiner/laptop/configuration.nix @@ -26,6 +26,7 @@ in (modulesDirectory + /sound.nix) (modulesDirectory + /services/base) + (modulesDirectory + /services/webcam-rtsp) (modulesDirectory + /programs/hyprland) (modulesDirectory + /programs/kubernetes-tools.nix) @@ -46,6 +47,14 @@ in base16Scheme = "${pkgs.base16-schemes}/share/themes/catppuccin-${theme}.yaml"; }; + webcam-rtsp = { + enable = true; + device = "/dev/v4l/by-id/usb-GENERAL_GENERAL_WEBCAM-video-index0"; + rtspUrl = "rtsp://192.168.1.143:8554/laptop"; + framerate = 30; + videoSize = "1280x720"; + }; + nixvim = { enable = true; theme = theme; diff --git a/moduler/services/mediamtx/default.nix b/moduler/services/mediamtx/default.nix new file mode 100644 index 0000000..abf7d43 --- /dev/null +++ b/moduler/services/mediamtx/default.nix @@ -0,0 +1,41 @@ +{ + lib, + config, + ... +}: +with lib; +{ + options = { + mediamtx = { + enable = mkEnableOption "enables mediamtx"; + host = mkOption { + type = types.str; + default = "0.0.0.0"; + description = "The host address to bind RTSP on."; + }; + rtspPort = mkOption { + type = types.int; + default = 8554; + description = "RTSP port exposed by MediaMTX."; + }; + }; + }; + + config = mkMerge [ + (mkIf config.mediamtx.enable { + virtualisation.podman.enable = true; + + virtualisation.oci-containers = { + backend = "podman"; + containers = { + mediamtx = { + image = "bluenviron/mediamtx:latest"; + ports = [ "${config.mediamtx.host}:${toString config.mediamtx.rtspPort}:8554" ]; + }; + }; + }; + + networking.firewall.allowedTCPPorts = [ config.mediamtx.rtspPort ]; + }) + ]; +} diff --git a/moduler/services/webcam-rtsp/default.nix b/moduler/services/webcam-rtsp/default.nix new file mode 100644 index 0000000..96a5888 --- /dev/null +++ b/moduler/services/webcam-rtsp/default.nix @@ -0,0 +1,57 @@ +{ + lib, + config, + pkgs, + ... +}: +with lib; +{ + options = { + webcam-rtsp = { + enable = mkEnableOption "enables webcam RTSP publisher"; + device = mkOption { + type = types.str; + default = "/dev/v4l/by-id/usb-GENERAL_GENERAL_WEBCAM-video-index0"; + description = "V4L2 device used as input for ffmpeg."; + }; + rtspUrl = mkOption { + type = types.str; + default = "rtsp://192.168.1.143:8554/laptop"; + description = "Destination RTSP URL where ffmpeg publishes the stream."; + }; + framerate = mkOption { + type = types.int; + default = 30; + description = "Input framerate for the webcam stream."; + }; + videoSize = mkOption { + type = types.str; + default = "1280x720"; + description = "Input video size for the webcam stream."; + }; + }; + }; + + config = mkIf config.webcam-rtsp.enable { + systemd.services.webcam-rtsp-publisher = { + description = "Publish USB webcam to MediaMTX over RTSP"; + wantedBy = [ "multi-user.target" ]; + after = [ "network-online.target" ]; + wants = [ "network-online.target" ]; + + serviceConfig = { + Type = "simple"; + Restart = "always"; + RestartSec = "2"; + ExecStart = '' + ${pkgs.ffmpeg}/bin/ffmpeg \ + -hide_banner -loglevel warning \ + -f v4l2 -framerate ${toString config.webcam-rtsp.framerate} -video_size ${config.webcam-rtsp.videoSize} \ + -i ${config.webcam-rtsp.device} \ + -vcodec libx264 -tune zerolatency -preset veryfast \ + -f rtsp ${config.webcam-rtsp.rtspUrl} + ''; + }; + }; + }; +}