前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >腾讯云实时音视频服务端实现A+B->A和A+B->C

腾讯云实时音视频服务端实现A+B->A和A+B->C

原创
作者头像
singleli
发布2022-11-28 00:28:07
9430
发布2022-11-28 00:28:07
举报

上文介绍了TRTC的一些基本的进房知识点和客户端进行A+B->A、A+B->C的两中混流方式。

这篇文章我们主要介绍服务端实现两种混流的方式。

一、混流前提条件(补充) 1、混流前必须要在控制台开启旁路推流配置(云端录制后面再讲解)

2、创建TRTC直播间的时候,如果设置了param.strRoomId,那必须设置param.roomId = 0。

场景:创建一个TRTC的直播房间,两人进房。一个主播,一个观众,然后观众发起连麦,服务端进行混流。

主播:

流id:1400505488_661_666_main,上一篇文章已经解释这个默认的流id是如何生成的了,记不住的回顾一下上一篇文章。

连麦观众:

流id:1400505488_661_555_main

上面流id和画面我们都可以通过云直播的控制台->流管理->在线流->查看具体的流id和预览流画面

准备工作已经搞定,那我们开始混流。下面以Jave为例,讲解一下!!!

二、服务端 REST API 混流方案

开始混流:由您的服务器调用 REST API StartMCUMixTranscode可以启动云端混流

相关描述请看:https://cloud.tencent.com/document/product/647/44270,接口请求域名: trtc.tencentcloudapi.com 。

首先导入需要的包名:

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.trtc.v20190722.TrtcClient;
import com.tencentcloudapi.trtc.v20190722.models.*;;

开始混流的具体实现代码:

public class StartMCUMixTranscode
{
public static void main(String [] args) {
try{
            Credential cred = new Credential("xxx", "xxx");//配置个人秘钥(SecretId和SecretKey)
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("trtc.tencentcloudapi.com");//配置请求接口的域名
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            TrtcClient client = new TrtcClient(cred, "ap-guangzhou", clientProfile);//服务器地域
            StartMCUMixTranscodeRequest req = new StartMCUMixTranscodeRequest();
            req.setSdkAppId(1400505488L);
            req.setRoomId(661L);
            OutputParams outputParams1 = new OutputParams();
            outputParams1.setStreamId("streamid_test");
            outputParams1.setPureAudioStream(0L);
            outputParams1.setRecordId("rongkeRecord");
            outputParams1.setRecordAudioOnly(0L);
            req.setOutputParams(outputParams1);
            EncodeParams encodeParams1 = new EncodeParams();
            encodeParams1.setVideoWidth(720L);
            encodeParams1.setVideoHeight(1280L);
            encodeParams1.setVideoBitrate(1500L);
            encodeParams1.setVideoFramerate(20L);
            encodeParams1.setVideoGop(2L);
            encodeParams1.setAudioSampleRate(48000L);
            encodeParams1.setAudioBitrate(64L);
            encodeParams1.setAudioChannels(2L);
            req.setEncodeParams(encodeParams1);
            LayoutParams layoutParams1 = new LayoutParams();
            layoutParams1.setTemplate(0L);
            layoutParams1.setMainVideoUserId("666");
            layoutParams1.setMainVideoStreamType(0L);
            SmallVideoLayoutParams smallVideoLayoutParams1 = new SmallVideoLayoutParams();
            smallVideoLayoutParams1.setUserId("555");
            smallVideoLayoutParams1.setStreamType(1L);
            smallVideoLayoutParams1.setImageWidth(180L);
            smallVideoLayoutParams1.setImageHeight(240L);
            smallVideoLayoutParams1.setLocationX(540L);
            smallVideoLayoutParams1.setLocationY(780L);
            layoutParams1.setSmallVideoLayoutParams(smallVideoLayoutParams1);
            req.setLayoutParams(layoutParams1);
            StartMCUMixTranscodeResponse resp = client.StartMCUMixTranscode(req);
            System.out.println(StartMCUMixTranscodeResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
                System.out.println(e.toString());
        }
    }
}

1、配置个人秘钥(SecretId和SecretKey)(https://console.cloud.tencent.com/cam/capi)

Credential cred = new Credential("xxx", "xxx");//配置个人秘钥(SecretId和SecretKey)

2、配置请求接口的域名:trtc.tencentcloudapi.com及服务器的地域

HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("trtc.tencentcloudapi.com");//配置请求接口的域名
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
TrtcClient client = new TrtcClient(cred, "ap-guangzhou", clientProfile);//服务器地域

3、配置混流输出控制参数:OutputParams (参数后续详细介绍)

StartMCUMixTranscodeRequest req = new StartMCUMixTranscodeRequest();
req.setSdkAppId(1400505488L);//sdkappid
req.setRoomId(661L);//房间号(int类型)
OutputParams outputParams1 = new OutputParams();//混流输出参数
outputParams1.setStreamId("streamid_test");//混流后的流id
outputParams1.setPureAudioStream(0L);//取值范围[0,1], 填0:直播流为音视频(默认); 填1:直播流为纯音频
outputParams1.setRecordId("rongkeRecord");//自定义录制文件名称前缀。请先在实时音视频控制台开通录制功能
outputParams1.setRecordAudioOnly(0L);//取值范围[0,1],填0无实际含义; 填1:指定录制文件格式为mp3
req.setOutputParams(outputParams1);

4、配置混流输出编码参数:EncodeParams(参数后续详细介绍)

EncodeParams encodeParams1 = new EncodeParams();
encodeParams1.setVideoWidth(720L);//混流-输出流宽,音视频输出时必填。取值范围[0,1920],单位为像素值。
encodeParams1.setVideoHeight(1280L);//混流-输出流高,音视频输出时必填。取值范围[0,1080],单位为像素值
encodeParams1.setVideoBitrate(1500L);//混流-输出流码率,音视频输出时必填。取值范围[1,10000],单位为kbps。
encodeParams1.setVideoFramerate(20L);//混流-输出流帧率,音视频输出时必填。取值范围[1,60],表示混流的输出帧率可选范围为1到60fps。
encodeParams1.setVideoGop(2L);//混流-输出流gop,音视频输出时必填。取值范围[1,5],单位为秒。
encodeParams1.setAudioSampleRate(48000L);//混流-输出流音频采样率。取值为[48000, 44100, 32000, 24000, 16000, 8000],单位是Hz。
encodeParams1.setAudioBitrate(64L); //混流-输出流音频码率。取值范围[8,500],单位为kbps。
encodeParams1.setAudioChannels(2L);//混流-输出流音频声道数,取值范围[1,2],1表示混流输出音频为单声道,2表示混流输出音频为双声道.
req.setEncodeParams(encodeParams1);

5、混流输出布局参数:LayoutParams(参数后续详细介绍)

LayoutParams layoutParams1 = new LayoutParams();
layoutParams1.setTemplate(0L);//混流布局模板ID,0为悬浮模板(默认);1为九宫格模板;2为屏幕分享模板;3为画中画模板;4为自定义模板。
layoutParams1.setMainVideoUserId("666");//屏幕分享模板、悬浮模板、画中画模板中有效,代表大画面对应的用户ID。
layoutParams1.setMainVideoStreamType(0L);//屏幕分享模板、悬浮模板、画中画模板中有效,代表大画面对应的流类型,0为摄像头,1为屏幕分享。左侧大画面为web用户时此值填0。
SmallVideoLayoutParams smallVideoLayoutParams1 = new SmallVideoLayoutParams();
smallVideoLayoutParams1.setUserId("555");//代表小画面对应的用户ID。
smallVideoLayoutParams1.setStreamType(1L); //代表小画面对应的流类型,0为摄像头,1为屏幕分享。小画面为web用户时此值填0。
smallVideoLayoutParams1.setImageWidth(180L);//小画面在输出时的宽度,单位为像素值,不填默认为0
smallVideoLayoutParams1.setImageHeight(240L);//小画面在输出时的高度,单位为像素值,不填默认为0。
smallVideoLayoutParams1.setLocationX(540L);//小画面在输出时的X偏移,单位为像素值,LocationX与ImageWidth之和不能超过混流输出的总宽度,不填默认为0。
smallVideoLayoutParams1.setLocationY(780L);//小画面在输出时的Y偏移,单位为像素值,LocationY与ImageHeight之和不能超过混流输出的总高度,不填默认为0。
layoutParams1.setSmallVideoLayoutParams(smallVideoLayoutParams1);

6、开始请求混流:

StartMCUMixTranscodeResponse resp = client.StartMCUMixTranscode(req);
System.out.println(StartMCUMixTranscodeResponse.toJsonString(resp));

混流后的流id和相应的画面:同样可以在云直播控制台查看

三、混流方式: 1、A+B->A

上面参数OutputParams设置的时候有一个人流id(StreamId),这个就是混流后我们设置的输出混流的流id

根据上面我们的房间背景,outputParams1.setStreamId("streamid_test");设置为主播666时,就是把主播(666)和连麦观众(555)的数据流混合到666上,即:1400505488_661_666_main+1400505488_661_555_main->1400505488_661_666_main.

代码实现:outputParams1.setStreamId("1400505488_661_666_main");

2、A+B->C

而我的事例代码,设置的就是A+B->C的方式。

如上图:就是将混流的流id改为c(streamid_test),代码:outputParams1.setStreamId("streamid_test");

四、服务端REST API 结束混流 结束混流:由您的服务器调用 REST API StopMCUMixTranscode即可结束混流。

相关描述请看:https://cloud.tencent.com/document/product/647/44269 接口请求域名: trtc.tencentcloudapi.com 。

同样是导入包名:

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.trtc.v20190722.TrtcClient;
import com.tencentcloudapi.trtc.v20190722.models.*;;

结束混流的具体实现代码:

public class StopMCUMixTranscode{
public static void main(String [] args) {
try{
Credential cred = new Credential("xxx", "xxx");//配置个人秘钥(SecretId和SecretKey)
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("trtc.tencentcloudapi.com");//配置请求接口的域名
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
TrtcClient client = new TrtcClient(cred, "ap-guangzhou", clientProfile);//服务器地域
StopMCUMixTranscodeRequest req = new StopMCUMixTranscodeRequest();
req.setSdkAppId(1400505488L);//sdkappid
req.setRoomId(661L);//房间号
StopMCUMixTranscodeResponse resp = client.StopMCUMixTranscode(req);//请求结束混流
System.out.println(StopMCUMixTranscodeResponse.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
}
}

结束混流比较简单,上面的代码已经很清楚介绍了。

重要提示:目前TRTC创建房间有两种类型的房间号,使用服务端混流时需要特别注意,如果是string类型的房间号,需要调用字符串房间号的混流方法。具体介绍请查看:https://cloud.tencent.com/document/product/647/50236。

五、在线调试工具: 腾讯云实时音视频,提供了服务端REST API的在线调试工具:https://cloud.tencent.com/document/product/647/44270

其中的混流参数都有详细的介绍,包括生成相对应的代码(Java、Python、Node.js、PHP、GO、.NET、C++)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云直播
云直播(Cloud Streaming Services,CSS)为您提供极速、稳定、专业的云端直播处理服务,根据业务的不同直播场景需求,云直播提供了标准直播、快直播、云导播台三种服务,分别针对大规模实时观看、超低延时直播、便捷云端导播的场景,配合腾讯云视立方·直播 SDK,为您提供一站式的音视频直播解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档