commit 869a18fe9fe0981d2d93e6bc259d6135fe077e78 Author: Gregory Lirent Date: Fri Apr 19 14:19:18 2024 +0300 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f96ff15 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.ipaddr diff --git a/bin/h264-retranslate b/bin/h264-retranslate new file mode 100755 index 0000000..a9193ee --- /dev/null +++ b/bin/h264-retranslate @@ -0,0 +1,20 @@ +#!/bin/bash + +LOCAL_IP="$(cat $(readlink -f $(dirname $(dirname $0)))/.ipaddr)" + +/usr/bin/ffmpeg -v quiet -rtbufsize 100m -listen 1 \ + -f flv -i "rtmp://${LOCAL_IP}:$1/live/mystream" \ + -an -c:v copy -preset ultrafast -tune zerolatency \ + -f rtsp "rtsp://${LOCAL_IP}/$1" & + +while true; do + FAIL=0 + + /usr/bin/ffprobe -v quiet "rtsp://${LOCAL_IP}/$1" || FAIL=1 + + if [[ $FAIL -eq 0 ]]; then + /usr/bin/systemd-notify WATCHDOG=1 + fi + + sleep $(( $WATCHDOG_USEC / 2 / 1000000 )) +done diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..a17255a --- /dev/null +++ b/install.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +WORK_DIR="$(readlink -f $(dirname $0))" +CERT_DIR="/usr/local/share/mediamtx/certs" + +echo Please input the local ip of this machine, where video services would be started && read LOCAL_IP +echo "${LOCAL_IP}" > "${WORK_DIR}/.ipaddr" + +mkdir -p /var/lib/mediamtx +mkdir -p "${CERT_DIR}" + +ln -s "$WORK_DIR/mediamtx.yml" /etc +ln -s "$WORK_DIR/bin/h264-retranslate" /usr/local/bin +ln -s "$WORK_DIR/system/h264-node@.service" /etc/systemd/system +ln -s "$WORK_DIR/system/mediamtx.service" /etc/systemd/system + +openssl req -x509 -nodes -days 3650 \ + -newkey rsa:2048 \ + -keyout "${CERT_DIR}/mediamtx.key" \ + -out "${CERT_DIR}/mediamtx.crt" \ + -subj "/C=RU/ST=0/L=0/O=0/OU=0/CN=0" + +systemctl enable mediamtx.service +systemctl start mediamtx.service + +#systemctl enable h264-node@1000 +#systemctl enable h264-node@1001 +#systemctl enable h264-node@1002 +#systemctl enable h264-node@1003 +#systemctl enable h264-node@1004 + +#systemctl start h264-node@1000 +#systemctl start h264-node@1001 +#systemctl start h264-node@1002 +#systemctl start h264-node@1003 +#systemctl start h264-node@1004 diff --git a/mediamtx.yml b/mediamtx.yml new file mode 100644 index 0000000..84f722e --- /dev/null +++ b/mediamtx.yml @@ -0,0 +1,108 @@ +logLevel: warn +logDestinations: [stdout] + +readTimeout: 10s +writeTimeout: 10s +writeQueueSize: 512 +udpMaxPayloadSize: 1472 +externalAuthenticationURL: + +authMethods: [basic] +authHTTPExclude: +- action: api +- action: metrics +- action: pprof + +playback: no +api: yes +metrics: yes +pprof: no + +playbackAddress: 0.0.0.0:9996 +apiAddress: 0.0.0.0:9997 +metricsAddress: 0.0.0.0:9998 +pprofAddress: 0.0.0.0:9999 + +runOnConnect: +runOnConnectRestart: no +runOnDisconnect: + +rtsp: yes +protocols: [udp, multicast, tcp] +encryption: "no" + +rtspAddress: 0.0.0.0:554 +rtspsAddress: 0.0.0.0:322 + +rtpAddress: 0.0.0.0:8000 +rtcpAddress: 0.0.0.0:8001 + +multicastIPRange: 224.0.0.0/16 +multicastRTPPort: 8002 +multicastRTCPPort: 8003 + +serverKey: /usr/local/share/mediamtx/certs/mediamtx.key +serverCert: /usr/local/share/mediamtx/certs/mediamtx.crt + +rtmp: yes +rtmpAddress: 0.0.0.0:1935 +rtmpEncryption: "no" +rtmpsAddress: :1936 +rtmpServerKey: /usr/local/share/mediamtx/certs/mediamtx.key +rtmpServerCert: /usr/local/share/mediamtx/certs/mediamtx.crt + +hls: no +webrtc: no +srt: no + +pathDefaults: + source: publisher + sourceFingerprint: + sourceOnDemand: no + maxReaders: 0 + srtReadPassphrase: + fallback: + + record: yes + playback: yes + recordPath: /var/lib/mediamtx/records/%path/%Y-%m-%d_%H-%M-%S-%f + recordFormat: fmp4 + recordPartDuration: 100ms + recordSegmentDuration: 24h + recordDeleteAfter: 0s + + publishUser: + publishPass: + publishIPs: [] + + readUser: + readPass: + readIPs: [] + + overridePublisher: yes + srtPublishPassphrase: + + runOnInit: + runOnInitRestart: no + + runOnDemand: + runOnDemandRestart: no + runOnDemandStartTimeout: 10s + runOnDemandCloseAfter: 10s + runOnUnDemand: + + runOnReady: + runOnReadyRestart: no + runOnNotReady: + + runOnRead: + runOnReadRestart: no + runOnUnread: + + runOnRecordSegmentCreate: + runOnRecordSegmentComplete: + + + +paths: + all_others: diff --git a/system/h264-node@.service b/system/h264-node@.service new file mode 100644 index 0000000..cf1a2ff --- /dev/null +++ b/system/h264-node@.service @@ -0,0 +1,14 @@ +[Unit] +Description=Retranslation of the RTMP stream '%I' +After=mediamtx.service + +[Service] +Type=simple +NotifyAccess=all +WatchdogSec=30 +Restart=always +ExecStart=/usr/local/bin/h264-retranslate %i +SuccessExitStatus=255 + +[Install] +WantedBy=multi-user.target diff --git a/system/mediamtx.service b/system/mediamtx.service new file mode 100644 index 0000000..4c236da --- /dev/null +++ b/system/mediamtx.service @@ -0,0 +1,19 @@ +[Unit] +Description=MediaMTX +Requires=docker.service +After=docker.service + +[Service] +TimeoutStartSec=0 +Restart=on-failure +ExecStartPre=-/usr/bin/docker exec %n stop +ExecStartPre=-/usr/bin/docker rm %n +ExecStartPre=/usr/bin/docker pull bluenviron/mediamtx:latest +ExecStart=/usr/bin/docker run --rm --name %n --network=host \ + -e TZ=Europe/Moscow \ + -v /etc/mediamtx.yml:/mediamtx.yml \ + -v /var/lib/mediamtx:/var/lib/mediamtx \ + bluenviron/mediamtx:latest + +[Install] +WantedBy=multi-user.target