段时间公司需要做直播服务,所以就研究了一下阿里云的直播,在直播里面,最重要的就是url的鉴权操作(验证推流或者拉流的有效性),在网上找了很多代码,都没有发现java的demo,所以就写篇播客记录一下,方便以后的使用和记忆,如果哪里有误,请指出改正。

阿里云直播提供的是触发式的推流与播放,您无需提前创建资源,只要添加经过备案的推流域名和播流域名,并完成域名解析、鉴权等操作,即可根据地址拼接规则手动拼接,以快速获取对应的推流地址和播流地址。本文介绍未设置转码的直播活动的推流地址和播流地址的拼接方法。
原文章地址:https://help.aliyun.com/document_detail/87396.html?spm=a2c4g.11174283.6.609.7eb3454eniwqXA
这句话的意思就是用户无需提前创建资源,在有直播场景的时候,直接生成一个直播url并且通过鉴权就可以开始直播了(看直播也是类似的)。

1,推拉流工具类:
/**
* 阿里云直播工具类
*/
public class AliyunLiveUtil {

private static final Logger log = LoggerFactory.getLogger(AliyunLiveUtil.class);

/**
* 推拉流地址示例:
* rtmp://www.ttest.ygdjonline.com/a/a?auth_key=1558065152-0-0-c3cb54d946c0590ca9aeee63573201ee
* 播流地址
* 原画
* rtmp://www.btest.ygdjonline.com/a/a?auth_key=1558065152-0-0-fc711455c0815aeb581385f33451d5b4
* http://www.btest.ygdjonline.com/a/a.flv?auth_key=1558065152-0-0-221abff1da1ee32151e365cf0dd42a53
* http://www.btest.ygdjonline.com/a/a.m3u8?auth_key=1558065152-0-0-72124fcc3aee3404b0d65dcc114e207f
*/

/**
* 根据源id创建该id的推流url
*
* @param sourceId
* @param aliyunLiveConfig
* @return
*/
public static String createPushUrl(Integer sourceId, LiveTypeEnum liveTypeEnum, AliyunLiveConfig aliyunLiveConfig) {

// 推流域名
String pushDomain = aliyunLiveConfig.getAliyunLivePushDomain();
// 应用名称
String appName = aliyunLiveConfig.getAliyunLiveAppName();
// 流名称
String streamName = StrUtil.format(aliyunLiveConfig.getAliyunLiveStreamName(), liveTypeEnum.getValue(), sourceId);
// 推流签名key
String pushIdentKey = aliyunLiveConfig.getAliyunLivePushIdentKey();
// 签名url有效时间
Integer identUrlValidTime = aliyunLiveConfig.getAliyunLiveIdentUrlValidTime();

// 计算过期时间
String timestamp = String.valueOf((System.currentTimeMillis() / 1000) + identUrlValidTime);

// 组合推流域名前缀
// rtmp://{pushDomain}/{appName}/{streamName}
String rtmpUrl = StrUtil.format("rtmp://{}/{}/{}", pushDomain, appName, streamName);
log.debug("推流域名前缀,rtmpUrl=" + rtmpUrl);

// 组合md5加密串
// /{appName}/{streamName}-{timestamp}-0-0-{pushIdentKey}
String md5Url = StrUtil.format("/{}/{}-{}-0-0-{}", appName, streamName, timestamp, pushIdentKey);

// md5加密
String md5Str = DigestUtil.md5Hex(md5Url);
log.debug("md5加密串,md5Url=" + md5Url + "------md5加密结果,md5Str=" + md5Str);

// 组合最终鉴权过的推流域名
// {rtmpUrl}?auth_key={timestamp}-0-0-{md5Str}
String finallyPushUrl = StrUtil.format("{}?auth_key={}-0-0-{}", rtmpUrl, timestamp, md5Str);
log.debug("最终鉴权过的推流域名=" + finallyPushUrl);

return finallyPushUrl;
}

/**
* 创建拉流域名,key=rtmpUrl、flvUrl、m3u8Url,代表三种拉流类型域名
*
* @param sourceId
* @param aliyunLiveConfig
* @return
*/
public static Map<String, String> createPullUrl(Integer sourceId, LiveTypeEnum liveTypeEnum, AliyunLiveConfig aliyunLiveConfig) {

// 拉流域名
String pullDomain = aliyunLiveConfig.getAliyunLivePullDomain();
// 应用名称
String appName = aliyunLiveConfig.getAliyunLiveAppName();
// 流名称
String streamName = StrUtil.format(aliyunLiveConfig.getAliyunLiveStreamName(), liveTypeEnum.getValue(), sourceId);
// 拉流签名key
String pullIdentKey = aliyunLiveConfig.getAliyunLivePullIdentKey();
// 签名url有效时间
Integer identUrlValidTime = aliyunLiveConfig.getAliyunLiveIdentUrlValidTime();

// 计算过期时间
String timestamp = String.valueOf((System.currentTimeMillis() / 1000) + identUrlValidTime);

// 组合通用域名
// {pullDomain}/{appName}/{streamName}
String pullUrl = StrUtil.format("{}/{}/{}", pullDomain, appName, streamName);
log.debug("组合通用域名,pullUrl=" + pullUrl);

// 组合md5加密串
// /{appName}/{streamName}-{timestamp}-0-0-{pullIdentKey}
String md5Url = StrUtil.format("/{}/{}-{}-0-0-{}", appName, streamName, timestamp, pullIdentKey);
String md5FlvUrl = StrUtil.format("/{}/{}.flv-{}-0-0-{}", appName, streamName, timestamp, pullIdentKey);
String md5M3u8Url = StrUtil.format("/{}/{}.m3u8-{}-0-0-{}", appName, streamName, timestamp, pullIdentKey);

// md5加密
String md5Str = DigestUtil.md5Hex(md5Url);
String md5FlvStr = DigestUtil.md5Hex(md5FlvUrl);
String md5M3u8Str = DigestUtil.md5Hex(md5M3u8Url);
log.debug("md5加密串,md5Url =" + md5Url + " ------ md5加密结果,md5Str=" + md5Str);
log.debug("md5加密串,md5FlvUrl =" + md5FlvUrl + " ------ md5加密结果,md5FlvStr=" + md5FlvStr);
log.debug("md5加密串,md5M3u8Url=" + md5M3u8Url + " ------ md5加密结果,md5M3u8Str=" + md5M3u8Str);

// 组合三种拉流域名前缀
// rtmp://{pullUrl}?auth_key={timestamp}-0-0-{md5Str}
String rtmpUrl = StrUtil.format("rtmp://{}?auth_key={}-0-0-{}", pullUrl, timestamp, md5Str);
// http://{pullUrl}.flv?auth_key={timestamp}-0-0-{md5FlvStr}
String flvUrl = StrUtil.format("http://{}.flv?auth_key={}-0-0-{}", pullUrl, timestamp, md5FlvStr);
// http://{pullUrl}.m3u8?auth_key={timestamp}-0-0-{md5M3u8Str}
String m3u8Url = StrUtil.format("http://{}.m3u8?auth_key={}-0-0-{}", pullUrl, timestamp, md5M3u8Str);

log.debug("最终鉴权过的拉流rtmp域名=" + rtmpUrl);
log.debug("最终鉴权过的拉流flv域名 =" + flvUrl);
log.debug("最终鉴权过的拉流m3u8域名=" + m3u8Url);

HashMap<String, String> urlMap = new HashMap<>();
urlMap.put("rtmpUrl", rtmpUrl);
urlMap.put("flvUrl", flvUrl);
urlMap.put("m3u8Url", m3u8Url);

return urlMap;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
2,阿里云直播配置参数:
/**
* 阿里云直播配置参数
*/
@Component
@Data
public class AliyunLiveConfig {

/**
* 推流域名
*/
@Value("${aliyun.live.push.domain}")
private String aliyunLivePushDomain;

/**
* 拉流域名
*/
@Value("${aliyun.live.pull.domain}")
private String aliyunLivePullDomain;

/**
* 直播测试appName
*/
@Value("${aliyun.live.appName}")
private String aliyunLiveAppName;

/**
* 直播测试streamName{直播类型}_{类型id}
*/
@Value("${aliyun.live.streamName}")
private String aliyunLiveStreamName;

/**
* 推流鉴权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;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
3,直播类型:
/**
* 直播类型
*/
@ApiModel(description = "直播类型")
public enum LiveTypeEnum {

course("course", "课程");

LiveTypeEnum(String value, String text) {
this.value = value;
this.text = text;
}

private String value;
private String text;

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

/**
* 根据value获取text
*
* @param value
* @return
*/
public static String getTextByValue(String value) {
LiveTypeEnum[] dictTypeEnums = LiveTypeEnum.values();
for (LiveTypeEnum dictTypeEnum : dictTypeEnums) {
if (dictTypeEnum.getValue().equals(value)) {
return dictTypeEnum.getText();
}

}
return "";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
4,application.properties 参数配置:
#阿里云直播配置 begin
#推流域名
aliyun.live.push.domain=www.xxx.xxx.com
#推流鉴权url key
aliyun.live.push.ident.key=xxx
#拉流域名
aliyun.live.pull.domain=www.xxx.xxx.com
#拉流鉴权url key
aliyun.live.pull.ident.key=xxx
#直播测试appName
aliyun.live.appName=ttest
#直播测试streamName{直播类型}{类型id}
aliyun.live.streamName={}{}
#鉴权url的有效时间(秒),默认30分钟,1800秒
aliyun.live.ident.url.validTime=1800
#阿里云直播配置 end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
5,里面工具类的pom地址,不需要也可以自己写
<!-- Hutool工具包 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.0.12</version>
</dependency>
1
2
3
4
5
6
工具类使用api官网:http://hutool.mydoc.io/undefined

6,发起请求的结果:

阿里云直播鉴权java代码示例的更多相关文章

  1. iOS直播集成和问题总结(阿里云直播)

    https://www.jianshu.com/p/714ce954e628 最近接手公司的直播项目,对以前遗留的问题做处理和优化, 于是顺便看了下阿里云直播的文档,在下面写下对直播的理解和遇到的问题 ...

  2. 基于阿里云直播实现视频推流(ffmpeg)/拉流(Django2.0)以及在线视频直播播放(支持http/https)功能

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_146 由于5g网络的光速推广,视频业务又被推上了风口浪尖,在2019年初我们还在谈论照片,短视频等关键字,而进入2020年,我们津 ...

  3. 阿里云直播 C# SDK 如何使用

    阿里云直播SDK的坑 1.直播云没有单独的SDK,直播部分被封装在CDN的相关SDK当中. 2.针对SDK,没有相关Demo. 3.针对SDK,没有相关的文档说明. 4.针对SDK的说明,官网上的说明 ...

  4. 阿里云直播PHP SDK如何使用

    前一篇聊了聊关于阿里云直播,如何进行进行调试,ok,那这篇我们就聊一聊关于阿里云直播的SDK(当然是关于PHP的),基于下面的原因: 1.直播云没有单独的SDK,直播部分的SDK是直接封装在CDN的相 ...

  5. 阿里云服务器ubuntu安装java运行环境

    服务器 阿里云服务器ubuntu安装java运行环境 转:http://www.codingyun.com/article/45.html 今天来给大家介绍一下在阿里云ubuntu服务器下安装java ...

  6. jwt鉴权学习 (php示例代码)

    前段时间听朋友讲起 jwt鉴权,博主我是一脸懵逼,通过朋友坚持不懈的讲解,我终于听懂了,jwt就是登陆token校验嘛 然而事情并不是博主想象的那么简单,在一个艳阳高照,晴空万里的夜晚,博主手贱百度了 ...

  7. apigw鉴权分析(1-1)阿里数加 - 鉴权方式分析

    一.访问方式 1.访问阿里云首页 https://www.aliyun.com/?utm_medium=text&utm_source=bdbrand&utm_campaign=bdb ...

  8. EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器功能简介---视频直播、直播鉴权(如何完美将EasyDSS过渡到新版)

    作为RTMP流媒体服务器,接受RTMP推流.进行实时的直播流分发是EasyDSS流媒体服务自身一大核心功能.写本篇博文的一个目的是向大家介绍EasyDSS新版的直播间.匿名直播.和虚拟直播的功能, 另 ...

  9. 阿里云SDK手册之java SDK

    进行阿里云sdk开发的前提是已经购买阿里云的相关服务才能调用阿里的相关接口进行开发.最近公司在做云管控的项目,于是进行下摘录总结. 一. 环境准备 阿里云针对不同的开发语言提供不同的sdk,由于项目用 ...

随机推荐

  1. hdu_1049_Climbing Worm_201311061331

    Climbing Worm Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  2. C - Reading comprehension 二分法 求等比数列前N项和

    Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024 ...

  3. Train Problem II HDU 1023 卡特兰数

    Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want ...

  4. ORACLE11G 将dataguard的rman备份恢复到測试环境的单机oracle中的具体过程

    . 也就是说此时数据库仅仅能进行不全然恢复了,在打开数据库时得使用resetlogs打开. recover database until scn 11412370952; RMAN> recov ...

  5. Java 递归、尾递归、非递归、栈 处理 三角数问题

    import java.io.BufferedReader; import java.io.InputStreamReader; //1,3,6,10,15...n 三角数 /* * # 1 * ## ...

  6. ios调用dismissViewController的一个小陷阱

    我们的APP从启动到进入主页面.是通过presentViewController构造了一个ViewController序列,类似于首页 -> 登陆页 -> 启动载入页 -> 主页面 ...

  7. 4、angularJS过滤器

    一.过滤器的作用 过滤器用来格式化须要展示给用户的数据. 在HTML中的模板绑定符号{{ }}内通过|符号来调用过滤器. 比如.如果我们希望将字符串转换成大写能够对字符串中的每一个字符都单独进行转换操 ...

  8. Pig 在 shell script中被调用,批量载入处理文件

    首先,我想达到的目的是批量的处理一个目录下的的很多文档,这些文档保存了我要处理的数据,由于pig是初学..所以不知到该怎么批量的load,没有写过 自己的UDF,仅仅能一个一个文件的load,然后处理 ...

  9. CSS学习(十四)-CSS颜色之中的一个

    一.理论: 1.RGB色彩模式  a.CMYK色彩模式  b.索引色彩模式 (主要用于web) c.灰度模式  d.双色调模式 2.opacity: a.alphavalue:透明度 b.inheri ...

  10. Hypercall

    在Linux中.大家应该对syscall很的了解和熟悉,其是用户态进入内核态的一种途径或者说是一种方式.完毕了两个模式之间的切换:而在虚拟环境中,有没有一种类似于syscall这样的方式.可以从no ...