#!/bin/bash # Test camera RTSP connection # shellcheck disable=SC1091 # Handle both dropshell execution and manual execution if [ -z "${AGENT_PATH}" ]; then # Manual execution - set paths relative to template directory AGENT_PATH="../../../agent/" # Load from the actual deployed config, not template config source "../config/.template_info.env" source "../config/service.env" fi source "${AGENT_PATH}/common.sh" _check_required_env_vars "CAMERA_IP" "CAMERA_USER" "CAMERA_PASSWORD" "CAMERA_RTSP_PORT" echo "Testing camera connection..." echo "Camera IP: ${CAMERA_IP}" echo "Camera User: ${CAMERA_USER}" echo "RTSP Port: ${CAMERA_RTSP_PORT}" # Test basic network connectivity first echo "" echo "Testing network connectivity to camera..." if ping -c 1 -W 2 "${CAMERA_IP}" > /dev/null 2>&1; then echo "✓ Camera IP is reachable" else echo "✗ Cannot reach camera at ${CAMERA_IP}" echo " Please check:" echo " - Camera IP address is correct" echo " - Camera is powered on" echo " - Network routing/firewall allows connection" exit 1 fi # Test RTSP port echo "" echo "Testing RTSP port ${CAMERA_RTSP_PORT}..." if timeout 2 bash -c "echo > /dev/tcp/${CAMERA_IP}/${CAMERA_RTSP_PORT}" 2>/dev/null; then echo "✓ RTSP port is open" else echo "✗ RTSP port ${CAMERA_RTSP_PORT} is not accessible" echo " Please check:" echo " - RTSP is enabled on the camera" echo " - Port number is correct" echo " - No firewall blocking port ${CAMERA_RTSP_PORT}" exit 1 fi # URL-encode the password if it contains special characters ENCODED_PASSWORD=$(echo -n "${CAMERA_PASSWORD}" | sed 's/!/%21/g; s/@/%40/g; s/#/%23/g; s/\$/%24/g; s/&/%26/g') RTSP_URL="rtsp://${CAMERA_USER}:${ENCODED_PASSWORD}@${CAMERA_IP}:${CAMERA_RTSP_PORT}/cam/realmonitor?channel=1&subtype=0" echo "" echo "Testing RTSP URL (password hidden): rtsp://${CAMERA_USER}:****@${CAMERA_IP}:${CAMERA_RTSP_PORT}/cam/realmonitor?channel=1&subtype=0" # Test with ffprobe echo "" echo "Testing with ffprobe (10 second timeout)..." timeout 10 docker run --rm --network host \ --entrypoint ffprobe \ linuxserver/ffmpeg:latest \ -v error \ -rtsp_transport tcp \ -timeout 5000000 \ "${RTSP_URL}" 2>&1 | head -20 if [ $? -eq 0 ]; then echo "✓ Camera connection successful!" else echo "✗ Camera connection failed" echo "" echo "Trying alternative RTSP paths..." # Try without subtype parameter ALT_URL="rtsp://${CAMERA_USER}:${ENCODED_PASSWORD}@${CAMERA_IP}:${CAMERA_RTSP_PORT}/cam/realmonitor?channel=1" docker run --rm --network host --entrypoint ffprobe linuxserver/ffmpeg:latest \ -v error "${ALT_URL}" 2>&1 | head -5 # Try simple path SIMPLE_URL="rtsp://${CAMERA_USER}:${ENCODED_PASSWORD}@${CAMERA_IP}:${CAMERA_RTSP_PORT}/" docker run --rm --network host --entrypoint ffprobe linuxserver/ffmpeg:latest \ -v error "${SIMPLE_URL}" 2>&1 | head -5 fi echo "" echo "Checking MediaMTX container logs..." docker logs "${PROJECT_NAME}-mediamtx" --tail 20 2>&1 | grep -E "(ERR|WARN|connected|disconnected|error)"