编程是非常直接的:open一个artc的url,然后不断read,最后调用close关闭:

model

Example (以Mac平台为例):


// 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;
}

// 日志回调函数
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;
}

// 消息回调函数
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];
    // 设置support id prefix
    fs->preconfig("HelpSupportIDPrefix", "yourInstanceID");
    // 设置外置log function
    fs->preconfig("LogCallback",         addr_to_string(output_log, buf));
    fs->preconfig("LogCbParam",          addr_to_string(NULL, buf));
    // 设置消息回调
    fs->preconfig("MessageCallback",     addr_to_string(on_event, buf));
    fs->preconfig("MessageCbParam",      addr_to_string(NULL, buf));

    // 打开连接
    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: 将该帧送给解码器
            frm->free_ptr(frm);
            frm = NULL;
        }
        else if(c == 0) {
            assert(frm == NULL);
            printf("try again\n");
            // sleep a while, 防止过于频繁的read占用太多cpu
            usleep(5 * 1000);
        }
        else {
            assert(frm == NULL);
            printf("Error: unknown\n");
            break;
        }
    }

    // 关闭连接
    fs->close(h);

    return 0;
}                
            

编译:


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

运行:


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"
<此处省略大量的打印>
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
统计数据消息的内容:
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"