package com.wqh.fsrm.common.ali.live;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.live.model.v20161101.AddLivePullStreamInfoConfigRequest;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.wqh.fsrm.common.util.DateUtils;
import com.wqh.fsrm.common.util.RandomUtils;
import com.wqh.fsrm.project.business.util.MD5;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
public class AliLiveUtil {
public static final String domainName = "pull.polz.cn";
public static final String appName = "polz1";
public static final String sourceUrl = "pull.polz.cn";
public static final String host = "live.aliyuncs.com";
public static final String product = "live";
public static final String regionId = "cn-hangzhou";
public static final String auth = "xDltjn7BP9";
public static final String livePullUrl = "https://pull.polz.cn";
public static final String streamName = "polz_12345675";
public static final Integer accountId = 16;
public static final LocalDateTime startTime = LocalDateTime.now();
public static final LocalDateTime stopTime = LocalDateTime.now().plusDays(1);
public IAcsClient getIAcsClient() throws ClientException {
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G3v1LWxqKzveww1TtWX", "l0ybeHJ4H2uEbXtTISfUQP19KfbpIy");
//DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "live", "live.aliyuncs.com"); //添加自定义endpoint
IAcsClient client = new DefaultAcsClient(profile);
//System.setProperty("http.proxyHost", "127.0.0.1"); //用于设置代理,可用fiddler拦截查看HTTP请求,便于调试
//System.setProperty("http.proxyPort", "8888");
return client;
}
public JSONObject pull(){
String str = (DateUtils.toEpochSecond(LocalDateTime.now()) + 172800) + "-" + RandomUtils.createRandomNumber(4) + "-" + accountId + "-";
String authKey = MD5.md5("/polz/" + streamName + "-" + str + "" + auth);
String push_url = "rtmp://push.polz.cn/polz/" + streamName + "?auth_key=" + str + authKey;
String pull_url = livePullUrl + "/polz/" + streamName + ".m3u8?auth_key=" + str + MD5.md5("/polz/" + streamName + ".m3u8-" + str + auth) + "&aliyun_uuid=" + accountId;
String HD = livePullUrl + "/polz/" + streamName + "_lud.m3u8?auth_key=" + str + MD5.md5("/polz/" + streamName + "_lud.m3u8-" + str + auth) + "&aliyun_uuid=" + accountId;
String SD = livePullUrl + "/polz/" + streamName + "_lud.m3u8?auth_key=" + str + MD5.md5("/polz/" + streamName + "_lhd.m3u8-" + str + auth) + "&aliyun_uuid=" + accountId;
String LD = livePullUrl + "/polz/" + streamName + "_lud.m3u8?auth_key=" + str + MD5.md5("/polz/" + streamName + "_lsd.m3u8-" + str + auth) + "&aliyun_uuid=" + accountId;
String FD = livePullUrl + "/polz/" + streamName + "_lud.m3u8?auth_key=" + str + MD5.md5("/polz/" + streamName + "_lld.m3u8-" + str + auth) + "&aliyun_uuid=" + accountId;
Map<String, Object> data = new HashMap<>();
data.put("auth_key", authKey);
data.put("push_url", push_url);
data.put("stream_name", streamName);
Map<String, Object> play = new HashMap<>();
play.put("HD", HD);
play.put("SD", SD);
play.put("LD", LD);
play.put("FD", FD);
Map<String, Object> config = new HashMap<>();
config.put("isLive", true);
config.put("autoplay", true);
config.put("source", JSON.toJSONString(play));
JSONObject jsonObject = new JSONObject();
jsonObject.put("data", data);
jsonObject.put("config", config);
jsonObject.put("pull_url",pull_url);
return jsonObject;
}
public Map<String,Object> addLive() throws Exception{
Map<String,Object> map = new HashMap<>();
AddLivePullStreamInfoConfigRequest addLivePullStreamInfoConfigRequest = new AddLivePullStreamInfoConfigRequest();
addLivePullStreamInfoConfigRequest.setDomainName(domainName);
addLivePullStreamInfoConfigRequest.setActionName("AddLivePullStreamInfoConfig");
addLivePullStreamInfoConfigRequest.setStartTime(DateUtils.localDateTimeToString(startTime,"yyyy-MM-dd'T'HH:mm:ss'Z'"));
addLivePullStreamInfoConfigRequest.setEndTime(DateUtils.localDateTimeToString(stopTime,"yyyy-MM-dd'T'HH:mm:ss'Z'"));
addLivePullStreamInfoConfigRequest.setSourceUrl(sourceUrl);
addLivePullStreamInfoConfigRequest.setStreamName(streamName);
addLivePullStreamInfoConfigRequest.setRegionId(regionId);
addLivePullStreamInfoConfigRequest.setAppName(appName);
addLivePullStreamInfoConfigRequest.setVersion("2016-11-01");
try {
IAcsClient client = getIAcsClient();
HttpResponse httpResponse = client.doAction(addLivePullStreamInfoConfigRequest);
System.out.println(httpResponse.getUrl());
System.out.println(new String(httpResponse.getHttpContent()));
if(httpResponse.getStatus() >= 200 && httpResponse.getStatus() < 300 ){
String str = (DateUtils.toEpochSecond(LocalDateTime.now()) + 172800)+"-"+ RandomUtils.createRandomNumber(4) +"-"+accountId+"-";
String authKey = MD5.md5("/polz/"+streamName+"-"+str+""+auth);
String push_url = "rtmp://push.polz.cn/polz/"+streamName+"?auth_key="+str+authKey;
String pull_url = livePullUrl+"/polz/"+streamName+".m3u8?auth_key="+str+MD5.md5("/polz/"+streamName+".m3u8-"+str+auth);
String pull_url_lud = livePullUrl+"/polz/"+streamName+"_lud.m3u8?auth_key="+str+MD5.md5("/polz/"+streamName+"_lud.m3u8-"+str+auth);
String pull_url_lhd = livePullUrl+"/polz/"+streamName+"_lud.m3u8?auth_key="+str+MD5.md5("/polz/"+streamName+"_lhd.m3u8-"+str+auth);
String pull_url_lsd = livePullUrl+"/polz/"+streamName+"_lud.m3u8?auth_key="+str+MD5.md5("/polz/"+streamName+"_lsd.m3u8-"+str+auth);
String pull_url_lld = livePullUrl+"/polz/"+streamName+"_lud.m3u8?auth_key="+str+MD5.md5("/polz/"+streamName+"_lld.m3u8-"+str+auth);
map.put("auth_key",authKey);
map.put("push_url",push_url);
map.put("pull_url",pull_url);
map.put("pull_url_lud",pull_url_lud);
map.put("pull_url_lhd",pull_url_lhd);
map.put("pull_url_lsd",pull_url_lsd);
map.put("pull_url_lld",pull_url_lld);
map.put("stream_name",streamName);
}else{
throw new Exception("客户端错误");
}
} catch (Exception e) {
throw new Exception("服务端错误");
}
return map;
}
}
Java对接阿里云直播流
最新推荐文章于 2024-07-19 17:37:28 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)