本文为您介绍Linux RTS推流SDK的集成和实现方法。

操作步骤

  1. 下载并解压Linux RTS 推流 SDK。下载地址,请参见SDK下载
  2. 编写以下代码:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdarg.h>
    #include <unistd.h>
    #include <stdint.h>
    
    //引入头文件
    #include "rts_api.h"
    
    struct rts_frame* make_frame(uint8_t *data, uint32_t size, int64_t pts, int64_t dts, int is_audio)
    {
        struct rts_frame *frame = (struct rts_frame*)malloc(sizeof(struct rts_frame));
        memset(frame, 0, sizeof(struct rts_frame));
    
        frame->buf = (uint8_t*)malloc(size);
        if (data) {
            memcpy(frame->buf, data, size);
        }
        frame->size = size;
        frame->pts  = pts;
        frame->dts  = dts;
        frame->is_audio = is_audio;
        return frame;
    }
    
    static void free_frame(struct rts_frame *f)
    {
        if (f == NULL) {
            return;
        }
        if (f->buf != NULL) {
            free(f->buf);
        }
        free(f);
    }
    
    int main(int argc, const char **argv)
    {
        const struct rts_glue_funcs *fs = get_rts_funcs(1);
        if (fs == NULL) {
            printf("get_rts_funcs failed");
            return -1;
        }
    
        if (argc != 2) {
            printf("Usage: %s <artc url>\n", argv[0]);
            return -1;
        }
        const char *url = argv[1];
    
        //初始化全局配置,全局只能调用一次
        rts_golbal_config_t golbal_config;
        memset(&golbal_config, 0, sizeof(golbal_config));
        golbal_config.log_config.max_file_size = 100;
        golbal_config.log_config.log_keep_count = 10;
        golbal_config.log_config.log_level = RTS_LOG_ERROR;
        fs->preconfig(&golbal_config);
    
        //设置推流参数
        rts_config_t rts_config;
        memset(&rts_config, 0, sizeof(rts_config));
        rts_config.stream_config.pub_audio_codec = RTS_CODEC_OPUS;
        rts_config.stream_config.pub_audio_sample_rate = 48000;
        rts_config.stream_config.pub_audio_channels = 2;
        rts_config.stream_config.pub_video_codec = RTS_CODEC_H264;
        rts_config.event_handle.on_key_frame_req = on_key_frame_req;
        rts_config.event_handle.on_disconnected = on_disconnected;
        rts_config.event_handle.on_publish = on_publish;
        rts_config.event_handle.user_data = NULL; //用户对象指针
    
        //开始推流, 指定URL和流配置
        void *h = fs->open(url, &rts_config, "w");
        if (h == NULL) {
            printf("fs open failed");
            return -1;
        }
    
        //循环发送数据
        struct rts_frame *af = make_frame(data, length, pts, dts, 0);
        int ret = fs->write(af, h);
        free_frame(af);
    
        //结束推流
        fs->close(h);
    
        //释放实例,进程退出时调用,全局只能调用一次。
        fs->destroy();
        return 0;
    }
    
  3. 编译上述test_rtssdk.c文件。
    需要先安装 zlib-devel库:
    sudo yum install  zlib-devel

    再执行以下命令编译test_rtssdk.c文件:

    gcc -I <path to header files> -o test_rtssdk test_rtssdk.c -L<path to library file> -lRtsSDK -lpthread -ldl -lz -lm
  4. 运行生成的可执行文件test_rtssdk。
    export LD_LIBRARY_PATH=<path to library file> 
    ./test_rtssdk artc://<domain/app/name>