springboot集成阿里云直播,低延时直播

springboot集成阿里云直播,低延时直播

  1. 首先要在阿里云上配置推流域名、播流域名、推流鉴权key、拉流鉴权key、鉴权时间
  2. 引入pom依赖
 	<dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-live</artifactId>
        <version>3.9.17</version>
    </dependency>
  1. 配置文件配置直播属性
	#阿里云直播
	#阿里云推流域名
	aliyun.live.push.domain=xxx.xxx.com
	#阿里云拉流域名
	aliyun.live.pull.domain=xxx.xxx.com
	#推流鉴权url key
	aliyun.live.push.ident.key=xxxxxxxxxxxxxxxx
	#拉流鉴权的key
	aliyun.live.pull.ident.key=xxxxxxxxxxxxxxxx
	#鉴权时间 30*2分钟
	aliyun.live.ident.url.validTime=3600
  1. 配置文件获取属性值
	import org.springframework.beans.factory.annotation.Value;
	import org.springframework.context.annotation.Configuration;
	import org.springframework.context.annotation.PropertySource;
	
	/**
	 * Created with IntelliJ IDEA.
	 *
	 * @Author: Administrator
	 * @Date: 2022/04/26/15:58
	 * @Description:
	 */
	@Configuration
	@PropertySource("classpath:application.properties")
	public class AliYunhiZhiboConfig {
	
	    /**
	     * 推流域名
	     */
	    @Value("${aliyun.live.push.domain}")
	    private String aliyunLivePushDomain;
	    /**
	     * 拉流域名
	     */
	    @Value("${aliyun.live.pull.domain}")
	    private String aliyunLivePullDomain;
	
	    /**
	     * 推流鉴权url key
	     */
	    @Value("${aliyun.live.push.ident.key}")
	    private String aliyunLivePushIdentKey;
	    /**
	     * 拉流鉴权url key
	     */
	    @Value("${aliyun.live.pull.ident.key}")
	    private String aliyunLivePullIdentKey;
	
	    /**
	     * 鉴权url的有效时间(秒),默认30分钟,1800秒 key
	     */
	    @Value("${aliyun.live.ident.url.validTime}")
	    private Integer aliyunLiveIdentUrlValidTime;
	    /**
	     * OSS-区域代码
	     */
	    @Value("cn-shanghai")
	    private String regionId;
	
	    public String getAliyunLivePushDomain() {
	        return aliyunLivePushDomain;
	    }
	
	    public void setAliyunLivePushDomain(String aliyunLivePushDomain) {
	        this.aliyunLivePushDomain = aliyunLivePushDomain;
	    }
	
	    public String getAliyunLivePullDomain() {
	        return aliyunLivePullDomain;
	    }
	
	    public void setAliyunLivePullDomain(String aliyunLivePullDomain) {
	        this.aliyunLivePullDomain = aliyunLivePullDomain;
	    }
	
	    public String getAliyunLivePushIdentKey() {
	        return aliyunLivePushIdentKey;
	    }
	
	    public void setAliyunLivePushIdentKey(String aliyunLivePushIdentKey) {
	        this.aliyunLivePushIdentKey = aliyunLivePushIdentKey;
	    }
	
	    public String getAliyunLivePullIdentKey() {
	        return aliyunLivePullIdentKey;
	    }
	
	    public void setAliyunLivePullIdentKey(String aliyunLivePullIdentKey) {
	        this.aliyunLivePullIdentKey = aliyunLivePullIdentKey;
	    }
	
	    public Integer getAliyunLiveIdentUrlValidTime() {
	        return aliyunLiveIdentUrlValidTime;
	    }
	
	    public void setAliyunLiveIdentUrlValidTime(Integer aliyunLiveIdentUrlValidTime) {
	        this.aliyunLiveIdentUrlValidTime = aliyunLiveIdentUrlValidTime;
	    }
	}
  1. 控制器
	/**
	 * Created with IntelliJ IDEA.
	 *
	 * @Author: Administrator
	 * @Date: 2022/04/28/14:29
	 * @Description:
	 */
	@RestController
	@RequestMapping("/zhibo")
	public class ZhiboController {
		@Autowired
    	private AliYunhiZhiboConfig aliyunConfig;
    	/**
	     * 返回的信息:
	     * RTMP播放地址为: rtmp://playtest.gotyun.com/aUEVx/6nJg0?auth_key=1651052008-0-0-543efb99fe67ec487e404da7e312ab30
	     * m3u8播放地址为: http://playtest.gotyun.com/aUEVx/6nJg0.m3u8?auth_key=1651052008-0-0-b4bf93d26805ae77f9e7cd79948cf963
	     * flv播放地址为: http://playtest.gotyun.com/aUEVx/6nJg0.flv?auth_key=1651052008-0-0-a72fe00d653b27a2e675395703199e3f
	     * [RTMP播放地址为: rtmp://playtest.gotyun.com/aUEVx/6nJg0?auth_key=1651052008-0-0-543efb99fe67ec487e404da7e312ab30, m3u8播放地址为: http://playtest.gotyun.com/aUEVx/6nJg0.m3u8?auth_key=1651052008-0-0-b4bf93d26805ae77f9e7cd79948cf963, flv播放地址为: http://playtest.gotyun.com/aUEVx/6nJg0.flv?auth_key=1651052008-0-0-a72fe00d653b27a2e675395703199e3f]
	     * 推流地址是: rtmp://sendtest.gotyun.com/aUEVx/6nJg0?auth_key=1651052008-0-0-16a8511d9573709e37f33e6983661ff0
	     * 推流地址是: rtmp://sendtest.gotyun.com/aUEVx/6nJg0?auth_key=1651052008-0-0-16a8511d9573709e37f33e6983661ff0
	     */
	    @GetMapping("/dizhi")
	    public void dizhi() {
	        String appName = RandomStringUtils.randomAlphanumeric(5);;
	        String streamName = RandomStringUtils.randomAlphanumeric(5);;
	        long expireTime = 3600L;
	        System.out.println(AliYunUtil.general_pull_url(aliyunConfig.getAliyunLivePullDomain(), null, appName, streamName, expireTime));
	        System.out.println(AliYunUtil.generate_push_url(aliyunConfig.getAliyunLivePushDomain(), aliyunConfig.getAliyunLivePushIdentKey(), appName, streamName, expireTime));
	    }
	}
  1. AliYunUtil直播工具类
	import org.apache.commons.lang3.RandomStringUtils;
	import org.slf4j.Logger;
	import org.slf4j.LoggerFactory;
	
	import java.math.BigInteger;
	import java.security.MessageDigest;
	import java.security.NoSuchAlgorithmException;
	
	/**
	 * Created with IntelliJ IDEA.
	 *
	 * @Author: Administrator
	 * @Date: 2022/04/26/16:00
	 * @Description:
	 */
	public class AliYunUtil {
		/**
	     * 计算md5
	     * @param param
	     * @return
	     */
	    public static String md5(String param) {
	        if(param == null || param.length() == 0) {
	            return null;
	        }
	        try {
	            MessageDigest md5 = MessageDigest.getInstance("MD5");
	            md5.update(param.getBytes());
	            byte[] byteArray = md5.digest();
	
	            BigInteger bigInt = new BigInteger(1, byteArray);
	            // 参数16表示16进制
	            String result = bigInt.toString(16);
	            // 不足32位高位补零
	            while(result.length() < 32) {
	                result = "0" + result;
	            }
	            return result;
	        } catch (NoSuchAlgorithmException e) {
	            e.printStackTrace();
	        }
	        return null;
	    }
	    /**
	     * 生成推流地址
	     * @param pushDomain 推流域名
	     * @param pushKey 推流域名配置的鉴权Key
	     * @param appName 推流AppName
	     * @param streamName 推流StreamName
	     * @param expireTime 过期时间(单位是秒)
	     */
	    public static String generate_push_url(String pushDomain,String pushKey,String appName,String streamName,long expireTime) {
	        String pushUrl = "";
	        //推流域名未开启鉴权功能的情况下
	        if(pushKey=="") {
	            pushUrl = "rtmp://"+pushDomain+"/"+appName+"/"+streamName;
	        }else {
	            long timeStamp = System.currentTimeMillis()/1000L + expireTime;
	            String stringToMd5 = "/"+appName+"/"+streamName+"-"+Long.toString(timeStamp)+"-0-0-"+pushKey;
	            String authKey = md5(stringToMd5);
	            pushUrl = "rtmp://"+pushDomain+"/"+appName+"/"+streamName+"?auth_key="+Long.toString(timeStamp)+"-0-0-"+authKey;
	        }
	        System.out.println("推流地址是: "+pushUrl);
	        return pushUrl;
	    }
	    /**
	     * 生成播放地址
	     * @param pullDomain 播放域名
	     * @param pullKey 播放鉴权Key
	     * @param appName 播放appName(同推流appName)
	     * @param streamName 播放streamName (同推流streamName)
	     * @param expireTime 过期时间(单位是秒
	     */
	    public static String general_pull_url(String pullDomain, String pullKey, String appName, String streamName, long expireTime) {
	        //rtmp的拉流地址
	        String rtmpUrl = "";
	        //m3u8的拉流地址
	        String hlsUrl = "";
	        //flv的拉流地址
	        String flvUrl = "";
	        //播放域名未配置鉴权Key的情况下
	        if(pullKey == "") {
	            rtmpUrl = "rtmp://"+pullDomain+"/"+appName+"/"+streamName;
	            hlsUrl = "http://"+pullDomain+"/"+appName+"/"+streamName+".m3u8";
	            flvUrl = "http://"+pullDomain+"/"+appName+"/"+streamName+".flv";
	        }else {
	            long timeStamp = System.currentTimeMillis()/1000L + expireTime;
	            String rtmpToMd5 = "/"+appName+"/"+streamName+"-"+Long.toString(timeStamp)+"-0-0-"+pullKey;
	            String rtmpAuthKey = md5(rtmpToMd5);
	            rtmpUrl = "rtmp://"+pullDomain+"/"+appName+"/"+streamName+"?auth_key="+Long.toString(timeStamp)+"-0-0-"+rtmpAuthKey;
	
	            String hlsToMd5 = "/"+appName+"/"+streamName+".m3u8-"+Long.toString(timeStamp)+"-0-0-"+pullKey;
	            String hlsAuthKey = md5(hlsToMd5);
	            hlsUrl = "http://"+pullDomain+"/"+appName+"/"+streamName+".m3u8"+"?auth_key="+Long.toString(timeStamp)+"-0-0-"+hlsAuthKey;
	
	            String flvToMd5 = "/"+appName+"/"+streamName+".flv-"+Long.toString(timeStamp)+"-0-0-"+pullKey;
	            String flvAuthKey = md5(flvToMd5);
	            flvUrl = "http://"+pullDomain+"/"+appName+"/"+streamName+".flv"+"?auth_key="+Long.toString(timeStamp)+"-0-0-"+flvAuthKey;
	        }
	        System.out.println("RTMP播放地址为: "+rtmpUrl);
	        System.out.println("m3u8播放地址为: "+hlsUrl);
	        System.out.println("flv播放地址为: "+flvUrl);
	        /*List<String> url = new ArrayList<>();
	        url.add("RTMP播放地址为: "+rtmpUrl);
	        url.add("m3u8播放地址为: "+hlsUrl);
	        url.add("flv播放地址为: "+flvUrl);*/
	        return hlsUrl;
	    }

		/**
		* main方法测试
		*/
	     public static void main(String[] args) {
	        // TODO Auto-generated method stub
	        //生成长度为5的随机字符串作为appName和streamName(字母和数字组合)
	        String appName = RandomStringUtils.randomAlphanumeric(5);;
	        String streamName = RandomStringUtils.randomAlphanumeric(5);;
	
	        long expireTime = 3600L;
	        String pullDomain = "play.gotyun.com";//播流域名
	        String pullKey = "";//播流key
	
	        String pushDomain = "send.gotyun.com";//推流域名
	        String pushKey = "";//推流key
	        System.out.println(general_pull_url(pullDomain, pullKey, appName, streamName, expireTime));
	        System.out.println(generate_push_url(pushDomain, pushKey, appName, streamName, expireTime));
	    }
	}
	

注意如果需要在移动端播放,直接返回m3u8地址,其它地址都不能正常在移动端播放

经过测试,这个还是有15s左右的延迟
在阿里云配置推流和拉流域名参考链接

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot集成RocketMQ的消费端中,可以通过设置重试次数和延时等级来控制消费失败后的重试机制。 首先,在消费端的`@RocketMQMessageListener`注解中,可以设置`consumerGroup`、`topic`和`selectorExpression`等属性。其中,`consumerGroup`表示消费组,`topic`表示消费的主题,`selectorExpression`表示消息的过滤条件。 接着,在消费端的处理方法中,可以通过抛出`RocketMQLocalException`异常来进行重试。通过在`@RocketMQMessageListener`注解中设置`maxRetryTimesWhenSendFailed`属性来设置重试次数,如果在重试次数内仍然无法消费成功,则会将消息发送到死信队列。 另外,可以通过设置延时等级来控制消息的消费重试时间。在发送消息时,可以通过设置`DelayTimeLevel`属性来指定消息的延时等级,RocketMQ会根据该等级来决定消息发送到消费端的时间,从而实现消息消费失败后的延迟重试。 下面是一个示例代码: ```java @Component @RocketMQMessageListener(consumerGroup = "my-consumer-group", topic = "my-topic", selectorExpression = "my-selector") public class MyRocketMQConsumer implements RocketMQListener<String> { private static final Logger logger = LoggerFactory.getLogger(MyRocketMQConsumer.class); @Override public void onMessage(String message) { try { // 消费消息的业务逻辑 logger.info("Received message: {}", message); } catch (Exception e) { logger.error("Failed to consume message: {}", message, e); throw new RocketMQLocalException(e.getMessage()); } } } ``` 在上面的代码中,通过抛出`RocketMQLocalException`异常来触发RocketMQ的重试机制。同时,可以在`@RocketMQMessageListener`注解中设置`maxRetryTimesWhenSendFailed`属性来设置重试次数。如果在重试次数内仍然无法消费成功,则会将消息发送到死信队列。 另外,在发送消息时,可以通过设置`DelayTimeLevel`属性来指定消息的延时等级,RocketMQ会根据该等级来决定消息发送到消费端的时间。例如,设置等级为3,表示消息将在10秒后发送到消费端。具体的延时等级和时间可以参考RocketMQ官方文档中的说明。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值