SOP TRTC SDK 仪表盘 Android TRTC 发送自定义采集的视频数据 Android TRTC 发送自定义采集音频解决方案 Android TRTC 实现横屏视频通话 iOS端 TRTC 发送自定义采集视频解决方案 iOS端 TRTC 发送自定义采集音频解决方案 APNS推送 脏字过滤 TXLiteAVSDK中使用 AVAudioSession 问题总结 AndroidStudio编译SDK报错 RoomService部署验证 Xcode编译SDK报错 iOS编译库冲突问题 iOS端移动直播自定义采集实现 iOS端TXLiteAVSDK与IMSDK 3.x集成冲突报错问题 Android端TXLiteAVSDK与IMSDK 3.x集成冲突报错问题 Android端LiteIM sdk升级IM4.x版本报错问题 移动直播iOS 12兼容问题 如何实现好的画质 如何计算PCM音量大小 使用播放器播放视频有黑边 直播拉流播放失败 直播拉流端卡顿现象 短视频上传失败 移动直播SDK对接第三方美颜库 移动直播连麦解决方案 Android移动直播推自定义采集的视频数据 Android移动直播推自定义采集的声音数据 Android直播播放如何获取YUV数据 Android直播播放如何自定义渲染 实时音视频画面黑屏 实时音视频订阅流显示 iOS 12默认新编译系统下文件名冲突问题 TXLiteAVSDK指标监控 进阶:小程序实时音视频参数透传 移动直播 Android 9.0 无法拉流问题 移动直播推流事件回调 移动直播拉流事件回调 短视频实现视频缩略图列表转GIF功能 roomService加入群组时报错invalid group id NTP时间戳转换 提示Role not exists 角色不存在 如何播放背景音乐 iOS端短视频添音频相关问题总结 Web同步终端离线推送TIMOfflinePushInfo说明文档 web端自定义消息发送 web端同步终端的已读回执 web端对群组内用户禁言操作 TRTC v2混流接口setMixTranscodingConfig使用指引

TRTC v2混流接口setMixTranscodingConfig使用指引

发布日期:2019年3月1日 更新日期:2018年3月1日 贡献者:zilongye、achaoli

场景

当一个音视频房间中同时有多路音视频流时,可能就需要用到混流转码,把多条音视频流转成一路,这样既便于录制存储,也便于旁路推流到直播 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。

代码示例

  • Objective-C
//云端混流转码的示例代码
- (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];      //启动混流
}
  • Android
//开启云端混流转码
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);
}
  • C++
// 开启云端混流转码
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

  • 请求url http://fcgi.video.qcloud.com/common_access?appid=1252500000&interface=Mix_StreamV2&t=t&sign=sign

  • 请求体

    {
      "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代表成功,返回其它状态码说明混流失败,可以参考文档里面常见错误码说明:https://cloud.tencent.com/document/product/267/8832#.E6.8E.A5.E5.8F.A3.E6.8F.8F.E8.BF.B0

混流成功响应示例:

{"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}
  • 旁路播放效果

results matching ""

    No results matching ""