diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..67a798c Binary files /dev/null and b/.DS_Store differ diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c321dcc --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/virtual-rtsp.iml b/.idea/virtual-rtsp.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/virtual-rtsp.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6bdb008 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,43 @@ +FROM golang:1.16.1-alpine AS BUILD + +RUN apk --update add git + +#RTSP SIMPLE SERVER +WORKDIR /tmp +RUN git clone https://github.com/aler9/rtsp-simple-server.git +WORKDIR /tmp/rtsp-simple-server + +RUN go mod download +RUN go build -o /go/bin/rtsp-simple-server . + +#RTSP SIMPLE PROXY +WORKDIR /tmp +RUN git clone https://github.com/aler9/rtsp-simple-proxy.git +WORKDIR /tmp/rtsp-simple-proxy + +RUN go mod download +RUN go build -o /go/bin/rtsp-simple-proxy . + +FROM jrottenberg/ffmpeg:4.0-alpine + +ENV SOURCE_URL '' +ENV STREAM_NAME 'stream' +ENV RTSP_PROXY_SOURCE_TCP 'yes' +ENV FORCE_FFMPEG 'true' +ENV FFMPEG_INPUT_ARGS '' +ENV FFMPEG_OUTPUT_ARGS='-c copy' + +RUN apk --update add gettext bash + +COPY --from=BUILD /go/bin/rtsp-simple-server /bin/rtsp-simple-server +COPY --from=BUILD /go/bin/rtsp-simple-proxy /bin/rtsp-simple-proxy + +ADD proxy.yml /tmp/proxy.yml +ADD start-relay.sh / + +EXPOSE 8554 +EXPOSE 8000 +EXPOSE 8001 + +ENTRYPOINT [ "/bin/bash" ] +CMD ["/start-relay.sh"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..823aca3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 stutzlab + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3452feb --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# Virtual RTSP + +This project creates a virtual RTSP connection, based on a looping MP4. It is inspired by and build on top of [flaviostutz's work](https://github.com/flaviostutz/rtsp-relay). + +The idea of a virtual RTSP is to simulate real-world IP cameras forwarding a RTSP H264 encoded stream. The project is build for demo purposes, when no RTSP connection is available. + +## Build with Docker + +To build the container you can simply build the Dockerfile using following command. + + docker build -t kerberos/virtual-rtsp . + +## Run Docker container + +To run the container you can have to specify a couple of environment variables, and important load in a m4p which can be streamed (and looped) through a RTSP connection. + +### Download MP4 + +We've published a couple of MP4s in the [v1.0.0 release](https://github.com/kerberos-io/virtual-rtsp/releases/tag/v1.0.0) which you can use to stream. The MP4s are IP camera footage recorded by real-world IP cameras (Axis/HIKVision). + +Go ahead and download a mp4 from the [v1.0.0 release](https://github.com/kerberos-io/virtual-rtsp/releases/tag/v1.0.0). + + mkdir samples && cd samples + wget https://github.com/kerberos-io/virtual-rtsp/releases/download/v1.0.0/cars.mp4 + +If the mp4 is downloaded, we can inject the mp4 into our container by using volumes and specifying the environment variable `-e SOURCE_URL`. + + docker run -p 8554:8554 \ + -e SOURCE_URL=file:///samples/cars.mp4 \ + -v $(pwd)/samples:/samples \ + kerberos/virtual-rtsp + +## Deploy to Kubernetes + +To be written \ No newline at end of file diff --git a/proxy.yml b/proxy.yml new file mode 100644 index 0000000..8b68211 --- /dev/null +++ b/proxy.yml @@ -0,0 +1,15 @@ +server: + # supported protocols + protocols: [ tcp, udp ] + # port of the RTSP UDP/TCP listener + rtspPort: 8554 + # port of the RTP UDP listener + rtpPort: 8000 + # port of the RTCP UDP listener + rtcpPort: 8001 + +streams: + $STREAM_NAME: + url: $SOURCE_URL + # whether to receive this stream in udp or tcp + useTcp: $RTSP_PROXY_SOURCE_TCP diff --git a/samples/.DS_Store b/samples/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/samples/.DS_Store differ diff --git a/start-relay.sh b/start-relay.sh new file mode 100755 index 0000000..c6da767 --- /dev/null +++ b/start-relay.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +set -e + +if [[ $SOURCE_URL == file://* ]]; then + export FFMPEG_INPUT_ARGS="$FFMPEG_INPUT_ARGS -re -stream_loop -1" +fi + +if [[ $SOURCE_URL == rtsp://* ]] && [ "$FORCE_FFMPEG_SOURCE" == "false" ]; then + envsubst < /tmp/proxy.yml > /proxy.yml + echo "/proxy.yml" + cat /proxy.yml + echo "Starting rtsp proxy from $SOURCE_URL to rtsp://0.0.0.0:8554/$STREAM_NAME..." + rtsp-simple-proxy /proxy.yml + +else + + if [ "$SOURCE_URL" != "" ]; then + echo "Starting rtsp server..." + rtsp-simple-server & + sleep 2 + + echo "Start relaying from $SOURCE_URL to rtsp://0.0.0.0:8554/$STREAM_NAME" + while true; do + set -x + ffmpeg $FFMPEG_INPUT_ARGS -i $SOURCE_URL $FFMPEG_OUTPUT_ARGS -f rtsp rtsp://127.0.0.1:8554/$STREAM_NAME + set +x + echo "Reconnecting..." + sleep 1 + done + else + echo "Won't restream a source feed to the server because SOURCE_URL was not defined" + echo "Starting rtsp server. You can publish feeds to it (ex.: ffmpeg -i somesource.mjpg -c copy -f rtsp rtsp://localhost:8554/myfeed)" + rtsp-simple-server + fi +fi