{ 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} ''; }; }; }; }