TRTCCloud 混流接口 setMixTranscoding

2019-06-16  本文已影响0人  RoaringMe

场景

当一个音视频房间中同时有多路音视频流时,可能就需要用到混流转码,把多条音视频流转成一路,这样既便于录制存储,也便于旁路推流到直播 CDN 上。目前TXLiteAVSDK_TRTC的方案是:

1、在控制台实时音视频服务下功能配置启用自动旁路直播,如果混流画面需要录制存储还需要启用旁路直播自动录制,参考:https://cloud.tencent.com/document/product/647/16826#.E5.A6.82.E4.BD.95.E5.BC.80.E5.90.AF.E6.97.81.E8.B7.AF.E6.9C.8D.E5.8A.A1

2、当需要混流的时候客户端直接调用setMixTranscodingConfig,并传入对应参数,这个时候SDK内部会组装请求并请求腾讯云后台;

3、混流成功后可以通过获取旁路地址播放:https://cloud.tencent.com/document/product/647/16826#.E5.A6.82.E4.BD.95.E8.8E.B7.E5.8F.96.E6.92.AD.E6.94.BE.E5.9C.B0.E5.9D.80

流程图

代码示例

//云端混流转码的示例代码
- (void)enableTranscoding
{
    TRTCTranscodingConfig *config = [[TRTCTranscodingConfig alloc] init];
    config.appId = xxxxxxxxxx;      //appid和bizid可以在腾讯云控制台账号信息里面查到
    config.bizId = xxxx;
    config.videoWidth      = 540;
    config.videoHeight     = 960;
    config.videoBitrate    = 1200;
    config.videoFramerate  = 20;
    config.videoGOP        = 2;
    config.audioSampleRate = 48000;
    config.audioBitrate    = 64;
    config.audioChannels   = 2;
    
    TRTCMixUser *user1 = [[TRTCMixUser alloc] init];
    user1.userId = @"Web_trtc_03";
    user1.zOrder = 0;
    user1.rect = CGRectMake(0, 0, 540, 960);
//    user1.streamType = TRTCVideoStreamTypeBig;
    
    TRTCMixUser *user2 = [[TRTCMixUser alloc] init];
    user2.userId = @"Web_trtc_04";
    user2.zOrder = 1;
    user2.rect = CGRectMake(380, 630, 160, 240);
//    user2.streamType = TRTCVideoStreamTypeBig;
    config.mixUsers = @[user1,user2];
    
    [_trtc setMixTranscodingConfig:config];      //启动混流
}
//开启云端混流转码
public void enableTranscoding() {
    TRTCCloudDef.TRTCTranscodingConfig config = new TRTCCloudDef.TRTCTranscodingConfig();
        // 设置分辨率为 1280x720, 码率为 1500kbps
        config.appId = xxxxxxxxxx;
        config.bizId = xxxx;
        config.videoWidth = 368;
        config.videoHeight = 640;
        config.videoBitrate = 1500;
        config.videoGOP = 2;
        config.videoFramerate = 30;
        config.audioSampleRate = 48000;
        config.audioBitrate = 64;
        config.audioChannels = 2;

        // 设置混流后主播的画面位置
        TRTCCloudDef.TRTCMixUser broadCaster = new TRTCCloudDef.TRTCMixUser();
        broadCaster.userId = "Web_trtc_01"; // 以主播uid为broadcaster为例
        // 铺满屏幕并放到最下面
        broadCaster.zOrder = 0;
        broadCaster.x = 0;
        broadCaster.y = 0;
        broadCaster.width = 368;
        broadCaster.height = 640;

        TRTCCloudDef.TRTCMixUser audience = new TRTCCloudDef.TRTCMixUser();
        audience.userId = "Web_trtc_02"; // 以观众uid为audience为例
        // 放到左下角,置于主播画面之上
        audience.zOrder = 1;
        audience.x = 178;
        audience.y = 310;
        audience.width = 180;
        audience.height = 320;

        config.mixUsers = new ArrayList<>();
        config.mixUsers.add(broadCaster);
        config.mixUsers.add(audience);

        trtcCloud.setMixTranscodingConfig(config);
}
// 开启云端混流转码
void enableTranscoding()
{
    TRTCTranscodingConfig config;
    config.appId = xxxxxxxxxx;      //appid和bizid可以在腾讯云控制台账号信息里面查到
    config.bizId = xxxx;
    // 设置分辨率为 1280x720, 码率为 1500kbps
    config.videoWidth = 1080;   
    config.videoHeight = 720;
    config.videoBitrate = 1500; 
    config.audioSampleRate = 48000;
    config.audioBitrate = 64;
    config.audioChannels = 2;

    std::vector<TRTCMixUser> mixUsers;

    // 设置混流后主播的画面位置
    TRTCMixUser broadCaster;
    broadCaster.userId = "broadcaster"; // 以主播uid为broadcaster为例
    broadCaster.zOrder = 0; // 铺满屏幕并放到最下面
    broadCaster.rect.left = 0;
    broadCaster.rect.top = 0;
    broadCaster.rect.right = 1280;
    broadCaster.rect.bottom = 720;

    // 设置观众位置, 以一个观众放到左下角为例, 左下角边距10个象素
    TRTCMixUser audience;
    audience.userId = "audience"; // 以观众uid为audience为例
    audience.zOrder = 1;    // 放到左下角,置于主播画面之上
    audience.rect.left = 920;
    audience.rect.top = 480;
    audience.rect.right = 1080;
    audience.rect.bottom = 720;

    mixUsers.push_back(std::move(broadCaster));
    mixUsers.push_back(std::move(audience));

    config.mixUsersArray = &mixUsers[0]; // 要求 mixUsers 非空
    config.mixUsersArraySize = mixUsers.size();

    trtcCloud->setMixTranscodingConfig(config);
}

原理解释

这里以iOS端为例,参考上述代码调用setMixTranscodingConfig接口后,SDK内部会转化成如下请求,实际上开发者也可以使用下面这种方式自己请求,这样更加灵活,SDK出于降低复杂度做了这一层处理。混流接口文档参考:https://cloud.tencent.com/document/product/267/8832#.E6.8E.A5.E5.8F.A3.E6.8F.8F.E8.BF.B0

{
    "domain":"2157.live.push.com",
    "interface":"set_live_mix_transcoding",
    "session_id":"2157_bf49b24917d4f42543ef886f601134da",
    "event_id":1551428085,
    "output_param":{
        "output_audio_channels":2,
        "output_stream_gop":2,
        "output_sei":"{}",
        "output_stream_type":0,
        "output_stream_id":"2157_bf49b24917d4f42543ef886f601134da",
        "output_audio_sample_rate":48000,
        "output_audio_bitrate":64,
        "output_stream_frame_rate":20,
        "output_stream_bitrate":1200
    },
    "path":"live",
    "timestamp":1551428085,
    "appid":1251783440,
    "input_stream_list":[
        {
            "input_stream_id":"canvas1",
            "layout_params":{
                "image_layer":1,
                "color":"0x000000",
                "input_type":3,
                "image_width":540,
                "location_x":0,
                "image_height":960,
                "location_y":0
            }
        },
        {
            "input_stream_id":"2157_bf49b24917d4f42543ef886f601134da",
            "layout_params":{
                "location_y":0,
                "image_layer":2,
                "image_width":540,
                "image_height":960,
                "location_x":0
            }
        },
        {
            "input_stream_id":"2157_1ec16ae5326160affe9f6e3117e5b918",
            "layout_params":{
                "location_y":630,
                "image_layer":3,
                "image_width":160,
                "image_height":240,
                "location_x":380
            }
        }
    ]
}

混流成功响应示例:

{"code":0,"event_id":"1551428085","message":"","timestamp":1551428085} 

混流失败响应示例:

{"code":256,"event_id":"1551430294","message":"upload report get usr info failed, dal_config.GetLiveUserInfoByAppid ret: config no exist, appid: 1251783441","timestamp":1551430295}
混流效果示例
上一篇下一篇

猜你喜欢

热点阅读