一、直播前需要准备腾讯云的账号
1.1 在云产品中搜索 即时通讯 IM 开通 即时通讯 IM。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210409172733424.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDM0MzQ3Nw==,size_16,color_FFFFFF,t_70)
1.2 在云产品中搜索 云直播 点击域名管理 添加域名
1.2.1 添加域名。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408141416349.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDM0MzQ3Nw==,size_16,color_FFFFFF,t_70)
1.2.2 点击 资源包/插件管理 查看流量包 如果没有,需要购买。
1.2.2.1 流量包
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408141848976.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDM0MzQ3Nw==,size_16,color_FFFFFF,t_70)
1.2.3 点击 直播SDK 应用管理 创建应用。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408142221390.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDM0MzQ3Nw==,size_16,color_FFFFFF,t_70)
1.2.4 如果推流生成了,使用推流出问题,可以查看 事件中心 断流记录。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408142616751.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDM0MzQ3Nw==,size_16,color_FFFFFF,t_70)
二、生成推流地址,播放地址(拉流地址)
2.1 直播要有推流地址,用户观看要有播放地址(拉流地址)。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408143613561.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDM0MzQ3Nw==,size_16,color_FFFFFF,t_70)
2.1.1 生成的推流地址格式
rtmp://domain/AppName/StreamName?txSecret=secret&txTime=hex(time)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408144437949.png)
domain
域名: 使用推流域名 可以使用自己创建的域名,也可以用默认的域名。
AppName
应用名称: 直播的应用名称 可以使用默认的 live 也可以使用自己创建的应用名称。
StreamName
流ID: 自定义流ID 可以随便写 建议 英文_数学(abc_123) 随机数
鉴权Key (非必需)
包含 txSecret 和 txTime 两部分:txSecret=Md5(key+StreamName+hex(time))&txTime=hex(time)。
开启推流鉴权后需使用包含鉴权 Key 的 URL 进行推流。
若未开启推流鉴权,则推流地址中无需 “?” 及其后内容。
为了防止容易被破解 建议加上鉴权Key
txTime(推流地址有效期)
表示何时该 URL 会过期,格式支持十六进制的 UNIX 时间戳。
说明:例如5867D600代表2017年1月1日0时0点0分过期,
我们的客户一般会将 txTime 设置为当前时间24小时以后过期,
过期时间不要太短也不要太长,
当主播在直播过程中遭遇网络闪断时会重新恢复推流,
如果过期时间太短,主播会因为推流 URL 过期而无法恢复推流。
txSecret(防盗链签名)
用以防止攻击者伪造您的后台生成推流 URL。
2.2 生成防盗链需要用到Key,到腾讯云的直播云里的推流域名管理
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408150834703.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDM0MzQ3Nw==,size_16,color_FFFFFF,t_70)
2.3 生成推流地址和播放地址(拉流地址)
推流地址前缀 rtmp:// 和 播放地址前缀(http://) 播放地址有四种格式,可以参考上面的图
2.3.1
public class LiveUtil {
private static final char[] DIGITS_LOWER =
{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static final String txSecret = "TXSECRET";
private static final String streamName = "txy_";
private static String pushStreamUrl = Setting.getValue("WECHATPAY.PUSH_STREAM_URL");
private static String getStreamUrl = Setting.getValue("WECHATPAY.GET_STREAM_URL");
private static String auth_key = streamName + "?txSecret=" + txSecret;
private static String auth_get_key = streamName + ".flv?txSecret=" + txSecret;
public static Map<String, String> generateEncodeUrl(String userId) {
Long second = LocalDateTime.now().plusDays(2).toEpochSecond(ZoneOffset.of("+8"));
String effectiveTime = Long.toHexString(second);
String md5Code = getTxSecret(Setting.getValue("WECHATPAY.PUSH_STREAM_KEY"),streamName+userId,effectiveTime);
String authKey = auth_key
.replace(streamName, streamName+userId)
.replace(txSecret, md5Code);
String authGetKey = auth_get_key
.replace(streamName, streamName+userId)
.replace(txSecret, md5Code);
HashMap<String, String> map = new HashMap<>();
map.put("pushStreamUrl", pushStreamUrl + authKey);
map.put("getStreamUrl", getStreamUrl + authGetKey);
map.put("streamId", streamName+userId);
return map;
}
private static String getTxSecret(String key, String streamName, String txTime) {
String input = new StringBuilder().
append(key).
append(streamName).
append(txTime.toUpperCase()).toString();
String txSecret = null;
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
txSecret = byteArrayToHexString(
messageDigest.digest(input.getBytes("UTF-8"