🌐 English

    RTSP API#

    Oasis supports an RTSP server.

    Supported video codecs are H264 and H265, and audio codecs are OPUS, AAC, MP3, and PCM. For live streaming, OPUS operates as the default codec.

    Header File#

    OasisRtsp.h

    URL Format#

    The URL format used when a client media player, such as VLC, connects to the Oasis RTSP server is as follows:

    rtsp://<Server Address>[:Port Number][/RTSP Streaming Session Configuration Key-Value List]/<Streaming Destination Path>
    
    • Server Address: The address of the RTSP server.

    • Port Number: If not specified, the default value is 554.

    • RTSP Streaming Session Configuration Key-Value List: This is optional and supports the following key-values. Each key-value pair is separated by "&".

    Key Description
    resolution Video resolution. Can be specified as one of the following values: 2160p, 1440p, 1080p, 720p, 480p. If it is smaller than the camera video size, it is scaled. Applies only to live streaming.
    out-width Video width. Measured in pixels. Applies only to live streaming.
    out-height Video height. Measured in pixels. Applies only to live streaming.
    bitrate Video transmission bitrate. Applies only to live streaming.
    fps Video fps. Applies only to live streaming.
    audio-only Streams audio only. No separate value is required.
    video-only Streams video only. No separate value is required.

    Note

    If resolution, out-width, and out-height exist simultaneously, resolution takes priority.

    • Streaming Destination Path: Specifies the target to be streamed. A live camera path or a file path can be specified. In the case of a file path, it must be located within the root-dir specified when creating the RTSP server. The live camera path is formatted as Live/<Camera Device ID>. <Camera Device ID> is the value specified as a key-value map in the oasis::configCameras API. In the case of a file path, a relative path is specified based on root-dir. If a video file contains multiple video tracks, a specific track can be specified in the format of /<File Path>/TrackID=<Track Number>.

    Below are examples of URLs:

    URL Description
    rtsp://192.168.10.2:554/example.mp4 Connects to port 554 of the server 192.168.10.2 and streams the example.mp4 file.
    rtsp://192.168.10.2:554/example.mp4/TrackID=1 Connects to port 554 of the server 192.168.10.2 and streams track 1 of the example.mp4 file.
    rtsp://192.168.10.2:554/Live/0 Connects to port 554 of the server 192.168.10.2 and streams the video of camera ID 0.
    rtsp://192.168.10.2:554/resolution=720p&fps=10&bitrate=128000/Live/0 Connects to port 554 of the server 192.168.10.2 and streams the video of camera ID 0 at 720p, 10fps, and 128Kbps.

    Note

    If the TCP interleave method is selected in the RTSP streaming options of the media player, data is sent via TCP instead of using RTP/RTCP. This option can be used for reasons such as firewalls.

    RtspEventDelegate Interface#

    The user can verify events occurring during streaming between the RTSP server and connected clients by creating an object derived from the RtspEventDelegate interface.

    This interface is not mandatory for the operation of the RTSP server.

    class RtspEventDelegate : public std::enable_shared_from_this<RtspEventDelegate>
    {
    public:
        RtspEventDelegate();
        virtual ~RtspEventDelegate();
    
    public:
        virtual void onDescription(const char *uri, const char *remote, const char *sdp);
        virtual void onSetup(const char *uri, const char *remote, const char *transport_line);
        virtual void onPlay(const char *uri, const char *remote, const char *rtp_info);
        virtual void onTeardown(const char *uri, const char *remote);
        virtual void onPause(const char *uri, const char *remote);
        virtual void onError(int error_code, const char *error_msg, const char *error_desc);
        virtual void onClosed(const char *uri, const char *remote);
    };
    
    void onDescription ( const char * uri , const char * remote , const char * sdp )
    OasisRtsp.h
    A callback function called when the RTSP server processes a Describe request message and sends a response.
    Parameters
    uri  The URI used by the client when requesting the connection.
    remote  The client address. A string formatted as <IPv4 Address>/<Port Number>.
    sdp  The SDP content delivered by the client.
    void onSetup ( const char * uri , const char * remote , const char * transport_line )
    OasisRtsp.h
    A callback function called when the RTSP server processes a Setup request message and sends a response.
    Parameters
    uri  The URI used by the client when requesting the connection.
    remote  The client address. A string formatted as <IPv4 Address>/<Port Number>.
    transport_line  The content of the Transport header field. Formatted like "Transport: RTP/AVP;unicast;client_port=8000-8001" or "Transport: RTP/AVP/TCP;interleaved=0-1".
    void onPlay ( const char * uri , const char * remote , const char * rtp_info )
    OasisRtsp.h
    A callback function called when the RTSP server processes a Play request message and sends a response.
    Parameters
    uri  The URI used by the client when requesting the connection.
    remote  The client address. A string formatted as <IPv4 Address>/<Port Number>.
    rtp_info  The content of the Rtp-Info header field. Formatted like "RTP-Info: url=rtsp://example.com/media.mp4/streamid=0;seq=9810092;rtptime=3450012".
    void onTeardown ( const char * uri , const char * remote )
    OasisRtsp.h
    A callback function called when the RTSP server processes a Teardown request message and sends a response.
    Parameters
    uri  The URI used by the client when requesting the connection.
    remote  The client address. A string formatted as <IPv4 Address>/<Port Number>.
    void onPause ( const char * uri , const char * remote )
    OasisRtsp.h
    A callback function called when the RTSP server processes a Pause request message and sends a response.
    Parameters
    uri  The URI used by the client when requesting the connection.
    remote  The client address. A string formatted as <IPv4 Address>/<Port Number>.
    void onError ( int error_code , const char * error_msg , const char * error_desc )
    OasisRtsp.h
    A callback function called when the RTSP server responds with an error.
    Parameters
    error_code  The error code.
    error_msg  The error code message.
    error_desc  The detailed description of the error code.

    The error response codes sent by the RTSP server are as follows:

    Error Code Message Detailed Description
    400 Bad Request DESCRIBE invalid uri
    400 Bad Request SETUP bad uri
    400 Bad Request OPTIONS bad uri
    400 Bad Request PLAY bad uri
    400 Bad Request PLAY transport not found
    400 Bad Request PAUSE bad uri
    400 Bad Request PAUSE transport not found
    400 Bad Request TEARDOWN bad uri
    454 Session Not Found SETUP session not found
    454 Session Not Found PLAY session not found
    454 Session Not Found PAUSE session not found
    454 Session Not Found TEARDOWN session not found
    459 Aggregate operation not allowed SETUP aggregate operation not supported
    500 Internal Server Error DESCRIBE no SDP available
    500 Internal Server Error PAUSE failed to pause the transport
    501 Not Implemented RECORD not supported
    501 Not Implemented REDIRECT not supported
    501 Not Implemented SET PARAMETER not supported
    503 Service Unavailable DESCRIBE session limited
    503 Service Unavailable OPTIONS session limited
    503 Service Unavailable SETUP session limited
    503 Service Unavailable SETUP new session not available
    503 Service Unavailable Session Play failed
    503 Service Unavailable PAUSE failed to pause the session
    503 Service Unavailable TEARDOWN failed to teardown the session
    void onClosed ( const char * uri , const char * remote )
    OasisRtsp.h
    A callback function called when the connection is closed.
    Parameters
    uri  The URI used by the client when requesting the connection.
    remote  The client address. A string formatted as <IPv4 Address>/<Port Number>.

    Functions#

    RtspServerRef createRtspServer ( key_value_map_t & parameters )
    OasisRtsp.h
    Creates an RTSP server object.
    Parameters
    parameters  The key-value map required to create the RTSP server object.
    Return Value
    Returns the RTSP server object on success. Returns nullptr on failure.
    Key
    Default
    M
    Description
    port
    554
     
    The port number of the RTSP server.
    root-dir
     
    The root directory path of files for file playback.
    remote-pool-size
    10
     
    The maximum number of allowed connections. Unlimited if "0".
    scheduler-size
    10
     
    The number of server scheduler tasks.
    upstreamer-size
    10
     
    The number of upstream tasks to deliver to the Delegate.
    transmitter-size
    10
     
    The number of transmission tasks.
    timer-size
    3
     
    The number of timer tasks.
    disable-rtcp
    0
     
    Does not use RTCP.
    enable-stun
    0
     
    Enables STUN. Not used.
    stun-server-addr
     
    The STUN server address. Not used.
    stun-server-port
     
    The STUN server port. Not used.
    file-session-max
    3
     
    The maximum number of simultaneous file playback streaming sessions. Unlimited if "0".
    live-session-max
    3
     
    The maximum number of simultaneous live sessions (camera video monitoring). Unlimited if "0".
    linger-time-max
    1000
     
    The maximum retention time after closing the connection. Measured in milliseconds.
    rtp-data-size-max
    1324
     
    The maximum size of RTP data, measured in bytes. The minimum size is 512 bytes. Adjusted according to network characteristics.
    rtp-packet-pool-count-max
    3
     
    The maximum number of RTP packet pools.
    rtp-packet-pool-size
    512
     
    The size held per RTP packet pool.
    default-video-bitrate
     
    The default video bitrate. Applied to live video.
    fps
    30
     
    The video FPS. Applied to live video; if already configured on the camera device, this value might not apply.
    snd-path
    default
     
    The audio device path.
    aec-disabled
    1
     
    Specifies whether to disable AEC. If "1", AEC is not used.
    audio-only
    0
     
    Streams audio only if "1".
    video-only
    0
     
    Streams video only if "1".
    RtspServerRef createRtspServer ( key_value_map_t & parameters , const std::shared_ptr<RtspEventDelegate> & delegate )
    OasisRtsp.h
    Creates an RTSP server object applying a user-defined object derived from RtspEventDelegate.
    Parameters
    parameters  The key-value map required to create the RTSP server object.
    delegate  The user-defined object derived from RtspEventDelegate.
    Return Value
    Returns the RTSP server object on success. Returns nullptr on failure.
    int32_t destroyRtspServer ( RtspServerRef & server )
    OasisRtsp.h
    Releases the RTSP server.
    Parameters
    server  The RTSP server object.
    Return Value
    • 0: Success
    • -1: Failure
    int32_t startRtspServer ( const RtspServerRef & server )
    OasisRtsp.h
    Starts the RTSP server.
    Parameters
    server  The RTSP server object.
    Return Value
    • 0: Success
    • -1: Failure
    int32_t stopRtspServer ( const RtspServerRef & server )
    OasisRtsp.h
    Stops the RTSP server.
    Parameters
    server  The RTSP server object.
    Return Value
    • 0: Success
    • -1: Failure
    int32_t enableRtspIseBypassMode ( const RtspServerRef & server , bool enable )
    OasisRtsp.h
    Specifies whether to output video with ISE functions (such as Warping) applied. Applied to AllWinner chipsets.
    Parameters
    server  The RTSP server object.
    enable  Outputs the video directly without applying ISE functions if true. Outputs the video with ISE functions applied if false.
    Return Value
    • 0: Success
    • -1: Failure

    Example#

    Below is an RTSP server example.

    
    #include "OasisAPI.h"
    #include "OasisLog.h"
    #include "OasisRtsp.h"
    
    #include <thread>
    #include <mutex>
    #include <memory>
    #include <condition_variable>
    
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    #include <signal.h>
    
    #define DLOG_TRACE  0x00080000
    
    #undef DLOG_FLAGS
    #define  DLOG_FLAGS (DLOG_FLAGS_DEFAULT|DLOG_TRACE/***/)
    
    #undef DLOG_TAG
    #define DLOG_TAG "RTSP"
    
    #define RTSP_PORT  554
    
    using namespace oasis;
    
    namespace oasis {
        uint64_t getFreeMemorySize();
    }
    
    static bool continuing = true;
    
    void cancel_handler(int signum)
    {
        continuing = false;
    }
    
    
    
    int main(int argc, char* argv[])
    {
        int32_t err;
        int32_t c;
        std::thread t2, t3;
    
        uint16_t rtsp_port = RTSP_PORT;
        RtspServerRef rtsp_server;
    
        oasis::key_value_map_t parameters;
    
        parameters["offs-disable"] = "1";
        if(oasis::initialize(parameters) < 0) {
            DLOG(DLOG_ERROR, "Oasis init failed\n");
            return -1;
        }
    
        //Oasis set SIGINT
        signal(SIGINT, cancel_handler);
    
    
        // config cameras
        // ...
    
        // Create RTSP server
    
        parameters.clear();
        parameters["port"] = std::to_string(rtsp_port);
        parameters["root-dir"] = "/mnt/extsd";
        parameters["remote-pool-size"] = std::to_string(20); //max con-current network connections
        parameters["scheduler-size"] = std::to_string(10); //max working threads
        parameters["upstreamer-size"] = std::to_string(10); //max working threads
        parameters["transmitter-size"] = std::to_string(15);
        parameters["timer-size"] = std::to_string(3);
        parameters["file-session-max"] = std::to_string(1);  //0: unlimited
        parameters["live-session-max"] = std::to_string(6);  //0: unlimited 
        parameters["linger-time-max"] = std::to_string(1000);   
        parameters["rtp-data-size-max"] = std::to_string(1024); 
        parameters["rtp-packet-pool-count-max"] = std::to_string(6);    
        parameters["rtp-packet-pool-size"] = std::to_string(256);   
    
        rtsp_server = createRtspServer(parameters);
        if(rtsp_server) {
            // Start RTSP server
            err = oasis::startRtspServer(rtsp_server);
            ASSERT(err == 0);
            if(err < 0) {
                DLOG(DLOG_ERROR, "RTSP server failed\n");
                goto done;
            }
            DLOG(DLOG_TRACE, "RTSP running at %d...\n", rtsp_port);
        }
    
        do {
            usleep(10000);
        } while(continuing);
    
    if(rtsp_server) {
            oasis::destroyRtspServer(rtsp_server);
        }
    
        oasis::finalize();
    
        return 0;
    }