Java对接阿里云直播流

本文介绍了如何使用阿里云LiveSDK在Java中创建直播流配置,包括获取AcsClient、设置流名、URL生成以及调用AddLivePullStreamInfoConfig接口的过程。
摘要由CSDN通过智能技术生成
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;
    }


}

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浮生若梦01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值