This topic describes the programming model of Native RTS SDK.

Procedure

  1. Write code.
    Note The following sample code is for macOS. Write code based on the actual operating system that you use.
    //test_rtssdk.c
    
    #include "rts_api.h"
    #include <stdarg.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <assert.h>
    #include <sys/time.h>
    #include <unistd.h>
    
    
    static char *addr_to_string(const void *v, char buf[64])
    {
        sprintf(buf, "%llu", (unsigned long long)v);
        return buf;
    }
    
    // The log callback function.
    static int output_log(void *s, int level, const char *fmt, va_list args)
    {
        (void) s;
        char str[1024];
        vsnprintf(str, 1000, fmt, args);
        printf("> %d %s\n", level, str);
        return 0;
    }
    
    // The message callback function.
    static int on_event(void *s,
        int type,
        void *data, // temp data, do not cache it for later use!
        long long data_size)
    {
        (void) s;
        printf("msg: %d %s\n", type, (data == NULL ? "null" : (const char *)data));
        return 0;
    }
    
    int main(int argc, const char **argv)
    {
        const struct rts_glue_funcs *fs = get_rts_funcs(2); // 2: current version
        if(fs == NULL) {
            printf("Error: failed to get rts functions. check version.\n");
            return -1;
        }
    
        if(argc != 2) {
            printf("Usage: %s <artc url>\n", argv[0]);
            return -1;
        }
    
        const char *url = argv[1];
    
        char buf[64];
        // Set the prefix of the help support ID.
        fs->preconfig("HelpSupportIDPrefix", "yourInstanceID");
        // Set the logging function for external logs.
        fs->preconfig("LogCallback",         addr_to_string(output_log, buf));
        fs->preconfig("LogCbParam",          addr_to_string(NULL, buf));
        // Configure message callbacks.
        fs->preconfig("MessageCallback",     addr_to_string(on_event, buf));
        fs->preconfig("MessageCbParam",      addr_to_string(NULL, buf));
    
        // Open the connection.
        void *h = fs->open(url, "r");
    
        if(h == NULL) {
            printf("Error: failed to open %s!\n", url);
            return -1;
        }
    
        while(1) {
            struct rts_frame *frm = NULL;
    
            int c = fs->read(&frm, h);
            if(c == 1) {
                printf("got one frame\n");
                assert(frm != NULL);
                // TODO: Send this frame to the decoder.
                frm->free_ptr(frm);
                frm = NULL;
            }
            else if(c == 0) {
                assert(frm == NULL);
                printf("try again\n");
                // Sleep for a while. This prevents frequent reads from occupying excessive CPU resources.
                usleep(5 * 1000);
            }
            else {
                assert(frm == NULL);
                printf("Error: unknown\n");
                break;
            }
        }
    
        // Close the connection.
        fs->close(h);
    
        return 0;
    }         
  2. Run the following command to compile the preceding test_rtssdk.c file:

    gcc -I <path to header files> -o test_rtssdk test_rtssdk.c -L<path to library file> -lRtsSDK

  3. Run the generated executable file test_rtssdk.

    export DYLD_LIBRARY_PATH=<path to library file>

    ./test_rtssdk artc://<domain/app/name>

    > 2 Start @1599029585691, Net sdk version 0.0.1_networkfd7ced1_LIBd84a462_sophoncb55d0435c_sophontb55d0435c_builde5fbe24_date2020.09.02.14:26-tag_rtssdk_1.3.0_startpoint-17-gfd7c
    > 2 OS: Mac
    > 2 Command queue thread running
    > 2 help support id: <yourInstanceID-pull%2Ertsdemo%2Egrtn%2Ealiyunlive%2Ecom-mac-20200902065305-wxnArRGbLt5gGgJN>
    try again
    msg: 100 code=100,when=1599029585691,where=,who=0,desc="yourInstanceID-pull%2Ertsdemo%2Egrtn%2Ealiyunlive%2Ecom-mac-20200902065305-wxnArRGbLt5gGgJN"
    <Output data>
    got one frame
    got one frame
    got one frame
    try again
    try again
    got one frame
    try again
    got one frame
    got one frame
    try again
    try again
    The content of the statistics message:
    msg: 105 code=105,when=1599029589707,where=,who=0,desc="haveVideo:1,videoCodecType:1,width:720,height:1280,receivedKeyFramePerSec:0.50,receivedVideoKbps:735,videoPktLostPerSec:0,receivedVideoPktPerSec:90,receivedVideoFramePerSec:12,videoNackPerSec:0,haveAudio:1,channels:2,sampleRate:48000,codecType:1,receivedAudioKbps:70,audioPktLostPerSec:0,receivedAudioPktPerSec:31,receivedAudioFramePerSec:31,audioNackPerSec:45,cacheDuration:0"