Skip to content

An MP4f server for live streaming from an RTSP service to an MSE browser front end

License

Notifications You must be signed in to change notification settings

richard-austin/mp4f-ws-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RTSPToFmp4

A fragmented MP4 media server for live streaming from an RTSP service to an MSE browser front end

Features

  • Stream RTSP sources to the web
  • fmp4 streaming for low latency
  • ffmpeg links this server with RTSP sources, giving flexiblity in source format and transcoding
  • Basic web page to check the streams
  • Stream url with codecs preamble for easy setup of MSE clients
  • Stream url without codecs preamble for other clients (such as ffmpeg)

Configuration

The configuration files are cameras.json to specify the camera streams and config.json for general server configuration.

cameras.json

{
  "camera1": {
    "name": "Doorbell",
    "username": "user",
    "password": "password",
    "rtsp_transport": "udp",
    "streams": {
      "stream1": {
        "audio": true,
        "audio_encoding": "aac",
        "descr": "HD",
        "netcam_uri": "rtsp://192.168.1.43:554",
        "media_server_input_uri": "http://localhost:8081/live/stream?suuid=stream1",
        "uri": "http://localhost:8081/ws/stream/suuid=stream1"
      }
    }
  },
  "camera2": {
    "name": "Garden PTZ",
    "username": "",
    "password": "",
    "rtsp_transport": "tcp",
    "streams": {
      "stream2": {
        "audio": true,
        "audio_encoding": "ulaw",
        "descr": "HD",
        "netcam_uri": "rtsp://192.168.1.30:554/11",
        "media_server_input_uri": "http://localhost:8081/live/stream?suuid=stream2",
        "uri": "http://localhost:8081/ws/stream/suuid=stream2"
      },
      "stream3": {
        "audio": true,
        "audio_encoding": "ulaw",
        "descr": "SD",
        "netcam_uri": "rtsp://192.168.1.30:554/12",
        "media_server_input_uri": "http://localhost:8081/live/stream?suuid=stream3",
        "uri": "http://localhost:8081/ws/stream/suuid=stream3"
      }
    }
  }
}

The parameters in cameras.json are as described below.

Parameters

  • camera(n)
    • name The camera name. This followed by the the relevant stream description is the descriptive text on the stream selector buttons on the test web page.
    • username The username idf required for authentication. If not required, set to empty string
    • password The password if required for authentication. If not required, set to empty string
    • rtsp_transport The RTSP transport used by ffmpeg, may be tcp or udp
    • streams
      • stream(n)
        • audio Set to true to use cameras rtsp audio stream, else set to false if not supported or to ignore the audio.
        • audio_encoding If the cameras audio format is AAC, set to aac, so ffmpeg will use copy for the audio. Setting to anything else will cause ffmpeg to encode the audio to AAC.
        • descr Description of stream (say HD or SD). This follows the camera name for the descriptive text on the stream selector buttons on the test web page.
        • netcam_uri The URL of this stream from the net camera.
        • client_uri The URL ffmpeg must use to connect to the server input side. This is generally of the form [http://localhost:8081/live/stream?suuid=stream(n)]
        • uri The websocket URL which MSE connects to. This is generally of the form [http://my-IP-address:8081/ws/stream?suuid=stream(n)]

config.json

{
  "log_path": "/var/log/mp4f-server/mp4f-server.log",
  "log_level": "INFO",
  "server_port": 8081,
  "default_latency_limit": 0.8,
  "gop_cache": true
}

The parameters in config.json are as described below.

Parameters

  • log_path The path where the log files will be written. The ffmpeg logs will also be written to the path as the server log.
  • log_level The required level of logging (can be "PANIC", "FATAL", "ERROR", "WARN", "INFO", "DEBUG", or "TRACE")
  • server_port The port the server will listen on (for web page, ffmpeg input and websocket output)
  • default_latency_limit This is the initial value for the latency limit when the web page is initially loaded. The value in use can be changed dynamically on the web page with a selector. When a new stream is selected, it will revert to the value given here. The latency limit determines how far behind real time the video must run before it is pulled in to a shorter delay. If this value is too high the latency can get larger than you might want. If set too low, poor stability can result. The optimum value depends on the network quality and the data rate of the stream.
  • gop_cache Enables the GOP cache if true. Without the GOP cache, the server will wait for the next keyframe to start the video stream with, resulting in an initial delay. When set to true, the video will start immediately.

Setting up

ffmpeg is required, If not already installed

Note that ffmpeg version 5 and above do not work correctly with RTSP streams which include audio. The raw H264/H265 do not have the timestamps those versions require to sync with the audio. So far I've not found any parameters which fix the problem with versions 5 and 6.

sudo apt install ffmpeg
(Or as appropriate for your OS)

Download repo and set up log directory

git clone git@github.com:richard-austin/mp4f-ws-server.git
sudo mkdir /var/log/mp4f-server
sudo chown your-user:your-user /var/log/mp4f-server

Run

cd mp4f-ws-server
GO111MODULE=on go run src/*.go

Build and run

cd mp4f-ws-server/src
go build -o mp4f-ws-server
cd ..
src/mp4f-ws-server

Build for arm64 e.g. Raspberry pi

An executable file for arm64 processors can be built on an x86 devlopment system

env GOOS=linux GOARCH=arm64 go build -o mp4f-ws_server_arm64

View the streams on the web page

set browser to http://localhost:8081

Websocket URL for mse

ws://localhost:8081/ws/stream?suuid=stream1 (for stream1)

General (no codec header) http URL

http://localhost:8081/h/stream?suuid=stream1 (for stream1)

Record stream1 with ffmpeg

ffmpeg -f mp4 -i http://localhost:8081/h/stream?suuid=stream1 -f mp4 output.mp4f

go version go1.18.1 linux/amd64

About

An MP4f server for live streaming from an RTSP service to an MSE browser front end

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages