import cn.hutool.crypto.digest.DigestUtil;
import org.apache.commons.lang.StringUtils;
import java.util.HashMap;
import java.util.Map;
public class AliLiveUtils {
//推流域名
private final static String pushDomainName = "";
//拉流域名
private final static String pullDomainName = "";
//推流鉴权KEY
private final static String pushKey = "";
//拉流鉴权KEY
private final static String pullKey = "";
public static Map createUrl(String appName , String streamName, Long timestamp , String templateId) {
HashMap map = new HashMap<>();
// if (!StringUtils.isEmpty(templateId))
// streamName = streamName + "_" + templateId;
Long newTimestamp = (System.currentTimeMillis() / 1000L) + timestamp;
// 推流地址
String rtmpURL = "rtmp://" + pushDomainName + "/" + appName + "/" + streamName ;
String rtsURL = "artc://" + pushDomainName + "/" + appName + "/" + streamName ;
// 拉流地址
//如果转码id存在
String pullRtmpURL = null;
String udp = null;
String flvUrl = null;
String M3U8Url = null;
if (!StringUtils.isEmpty(templateId)){
pullRtmpURL = "rtmp://" + pullDomainName + "/" + appName + "/" + streamName + '_' + templateId ;
udp = "artc://" + pullDomainName + "/" + appName + "/" + streamName + '_' + templateId;
flvUrl = "http://" + pullDomainName + "/" + appName + "/" + streamName + '_' + templateId + ".flv";
M3U8Url = "http://" + pullDomainName + "/" + appName + "/" + streamName + '_' + templateId + ".m3u8";
map.put("udp", createAuthKey(udp, appName, streamName + '_' + templateId, newTimestamp, pullKey));
map.put("pull", createAuthKey(pullRtmpURL, appName, streamName + '_' + templateId, newTimestamp, pullKey));
map.put("flv", createAuthKey(flvUrl, appName, streamName + + '_' + templateId + ".flv", newTimestamp, pullKey));
map.put("m3u8", createAuthKey(M3U8Url, appName, streamName + + '_' + templateId + ".m3u8", newTimestamp, pullKey));
}else{
pullRtmpURL = "rtmp://" + pullDomainName + "/" + appName + "/" + streamName ;
udp = "artc://" + pullDomainName + "/" + appName + "/" + streamName ;
flvUrl = "http://" + pullDomainName + "/" + appName + "/" + streamName + ".flv";
M3U8Url = "http://" + pullDomainName + "/" + appName + "/" + streamName + ".m3u8";
map.put("udp", createAuthKey(udp, appName, streamName , newTimestamp, pullKey));
map.put("pull", createAuthKey(pullRtmpURL, appName, streamName, newTimestamp, pullKey));
map.put("flv", createAuthKey(flvUrl, appName, streamName + ".flv", newTimestamp, pullKey));
map.put("m3u8", createAuthKey(M3U8Url, appName, streamName + ".m3u8", newTimestamp, pullKey));
}
// 生成推流拉流地址
map.put("push", createAuthKey(rtmpURL, appName, streamName, newTimestamp, pushKey));
map.put("rts", createAuthKey(rtsURL, appName, streamName , newTimestamp, pushKey));
return map;
}
public static String createAuthKey(String url, String appName, String streamName, Long timestamp, String key) {
String hashValue = "/" + appName + "/" + streamName + "-" + timestamp + "-0-0-" + key;
String md5Value = DigestUtil.md5Hex(hashValue);
String auth_key = timestamp + "-0-0-" + md5Value;
String newUrl = url + "?auth_key=" + auth_key;
return newUrl;
}
}
大佬的模板地址生成稍微有些问题,稍作了修改
转载:阿里视频直播自定义推拉流地址生成_给我一包烟丶的博客-CSDN博客_生成拉流地址



