Skip to content

How to build live stream server with srs

Published: at 03:22 PM

In the previous article, I guided you through setting up a livestream system using nginx-rtmp. However, in practice, the latency was quite high, falling short of my livestreaming requirements. So I looked for another platform with lower latency, which is SRS.

Table of contents

Open Table of contents

Prerequisites and usage

Step 1: Install SRS

  1. Update the system:

    sudo apt update
    sudo apt upgrade -y
  2. Install the necessary dependencies:

    sudo apt install build-essential automake tclsh cmake unzip pkg-config libssl-dev libpq-dev -y
  3. Download and compile SRS:

     git clone -b develop https://github.com/ossrs/srs.git
     cd srs/trunk
     ./configure
     make && sudo make install

Step 2: Configure SRS

  1. Edit the SRS configuration file: Here there are a lot of configurations for srs that you can see in the conf folder. When building srs, it will receive the srs.conf file as default config file. We will edit this config file as suggested by srs low latency.

    sudo apt install nano
    mv ./conf/srs.conf ./conf/srs.old.conf
    nano ./conf/srs.conf

    Here is config:

    listen              1935;
    max_connections     1000;
    daemon              on;
    #srs_log_tank        console;
    srs_log_tank        file;
    srs_log_file        ./objs/srs.log;
    
    http_server {
        enabled on;
        listen 8080;
        dir ./objs/nginx/html;
    }
    
    http_api {
        enabled on;
        listen 1985;
    }
    
    stats {
        network 0;
    }
    
    rtc_server {
        enabled on;
        listen 8000;
        # UDP port # @see https: //ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#config-candidate
        candidate $CANDIDATE;
    }
    
    vhost __defaultVhost__ {
        tcp_nodelay on;
        min_latency on;
    
        play {
            gop_cache off;
            queue_length 10;
            mw_latency 100;
        }
    
        publish {
            mr off;
        }
        http_hooks {
            # http callback when you need notify stream start and stop to your server
            #enabled         on;
            #on_publish      http://localhost:4000/api/livestream/onplay;
            #on_unpublish    http://localhost:4000/api/livestream/onplay;
            #on_play         http://localhost:4000/api/livestream/onplay;
            #on_stop         http://localhost:4000/api/livestream/onplay;
        }
    
        rtc {
            enabled on;
            # @see https: //ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtmp-to-rtc
            rtmp_to_rtc on;
            # @see https: //ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtc-to-rtmp
            rtc_to_rtmp on;
        }
    
        http_remux {
            enabled on;
            mount [vhost]/[app]/[stream].flv;
        }
    }
  2. Save and exit the editor:

Step 3: Start SRS

  1. Compile and link SRS to systemctl: We will compile and link srs.service
    ./configure && make && sudo make install &&
     sudo ln -sf /usr/local/srs/etc/init.d/srs /etc/init.d/srs &&
     sudo cp -f /usr/local/srs/usr/lib/systemd/system/srs.service /usr/lib/systemd/system/srs.service &&
     sudo systemctl daemon-reload && sudo systemctl enable srs
    All file will link from folder srs to /usr/local/srs.
  2. Start SRS service
    sudo systemctl start srs
  3. Check SRS status:
    sudo systemctl status srs

Step 4: Live Stream

To start live streaming, you can use OBS (Open Broadcaster Software) or any streaming software that supports RTMP.

  1. Install OBS:

    • Download and install OBS from the OBS Project website.
  2. Configure OBS:

    • Open OBS, go to Settings -> Stream.
    • Select Service as Custom....
    • Enter Server as rtmp://localhost/live/.
    • Enter Stream Key as any string you want, for example: stream.

Step 5: Check the Live Stream

Open a vlc and enter the URL:

If you are streaming, you will see your video playing. With this, you have completed the setup of a live streaming server with SRS.