Ali cloud Live authentication java code examples

Period of time the company needs to do a live service, so I study a little cloud Ali live, live in it, the most important is the url of the authentication operation (push to verify the effectiveness of stream flow or pull), the Internet to find a lot of code, We have not found the demo java, so you write an article about podcasting recording, to facilitate future use and memory, where if there is an error, please indicate correct.

Ali cloud provides a live triggered a plug flow and play, you do not need to create a resource in advance, just add plug flow through the domain name and domain name for the record of the broadcast stream, and complete domain name resolution, authentication and other operations, the address can be manually splicing splicing rules to quickly obtain a corresponding multicast address and a plug flow stream address. This article describes the stitching method is not provided transcoded stream live event pushing address and multicast address of the stream.
Original article addresses: https: //help.aliyun.com/document_detail/87396.html spm = a2c4g.11174283.6.609.7eb3454eniwqXA?
What this means is that users do not need to create a resource in advance, when there is a live scene, directly generate a url and live through authentication can begin live coverage (see live is similar).

1, push-pull tools flow:
/ **
* aliyun Air Tools
* /
public class AliyunLiveUtil {

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

/ **
* Address sliding flow example:
* rtmp: //www.ttest.ygdjonline.com/a/a auth_key = 1558065152-0-0-c3cb54d946c0590ca9aeee63573201ee?
* Multicast stream address
* original painting
* 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
* /

/ **
* Create based on the source id of the id of the plug flow URL
*
* @param sourceId
* @param aliyunLiveConfig
* @return
* /
public static createPushUrl String (Integer sourceId, LiveTypeEnum liveTypeEnum, AliyunLiveConfig aliyunLiveConfig) {

Streaming // domain name
String pushDomain aliyunLiveConfig.getAliyunLivePushDomain = ();
// Application Name
String appName = aliyunLiveConfig.getAliyunLiveAppName ();
// Stream Name
String streamName = StrUtil.format (aliyunLiveConfig.getAliyunLiveStreamName () , liveTypeEnum.getValue (), sourceId);
// streaming signature Key
String pushIdentKey aliyunLiveConfig.getAliyunLivePushIdentKey = ();
// url valid time signature
Integer identUrlValidTime = aliyunLiveConfig.getAliyunLiveIdentUrlValidTime ();

// Calculation expiration time
String timestamp = String.valueOf ((System.currentTimeMillis ( ) / 1000) + identUrlValidTime);

// combination of plug flow domain prefix
@ RTMP: // {pushDomain} / {appName} / {} which streamName is
String rtmpUrl = StrUtil.format ( "RTMP: // {} / {} / {}", pushDomain, appName, which streamName is);
log.debug ( "plug flow domain prefix, rtmpUrl =" + rtmpUrl);

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

// md5 encryption
String md5Str = DigestUtil.md5Hex (md5Url);
log.debug ( "MD5 encryption string, md5Url =" + md5Url + " ------ md5 encryption result, md5Str =" + md5Str);

// The final composition of the authentication plug-flow over the domain
?} // {rtmpUrl auth_key timestamp = {}} -0-0- {md5Str
String finallyPushUrl = StrUtil.format ( "auth_key = {} {} -? 0-0- } { ", rtmpUrl, timestamp, md5Str);
log.debug (" plug-flow over the final authentication domain = "+ finallyPushUrl);

return finallyPushUrl;
}

/ **
* Create pull flow domain, key = rtmpUrl, flvUrl, m3u8Url , represent three types of pull flow domain name
*
* @param sourceId
* @param aliyunLiveConfig
* @return
* /
public static the Map <String, String> createPullUrl (Integer sourceId , liveTypeEnum liveTypeEnum, AliyunLiveConfig aliyunLiveConfig) {

// pull flow domain name
String pullDomain = aliyunLiveConfig.getAliyunLivePullDomain ();
// Application Name
String appName = aliyunLiveConfig.getAliyunLiveAppName ();
// stream name
String streamName = StrUtil.format (aliyunLiveConfig.getAliyunLiveStreamName () , liveTypeEnum.getValue (), sourceId);
// pull flow signature Key
String pullIdentKey aliyunLiveConfig.getAliyunLivePullIdentKey = ();
// url valid time signature
Integer identUrlValidTime = aliyunLiveConfig.getAliyunLiveIdentUrlValidTime ();

// Calculation expiration time
String timestamp = String.valueOf ((System.currentTimeMillis ( ) / 1000) + identUrlValidTime);

// combination generic domain name
// {pullDomain} / {appName} / {} which streamName is
String pullUrl = StrUtil.format ( "{} / {} / {}", pullDomain, appName, which streamName is);
log.debug ( "Universal composition domain, 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 encryption
String md5Str = DigestUtil.md5Hex (md5Url);
String md5FlvStr = DigestUtil.md5Hex (md5FlvUrl);
String md5M3u8Str = DigestUtil.md5Hex (md5M3u8Url);
log.debug ( "MD5 encryption string, md5Url =" + md5Url + " ------ md5 encryption result, md5Str = "+ md5Str);
log.debug (" MD5 encryption string, md5FlvUrl = "+ md5FlvUrl +" ------ md5 encryption result, md5FlvStr = "+ md5FlvStr);
log.debug ( "md5 encryption string, md5M3u8Url =" + md5M3u8Url + " ------ md5 encryption result, 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 ( "pull-off final stream rtmp authentication domain =" + rtmpUrl);
log.debug ( "pull-off final stream flv authentication domain =" + flvUrl);
log.debug ( "final authentication off pull flow m3u8 domain = "+ 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
1 16
117
1 18
119
120
121
122
123
124
125
126
127
128
2 aliyun broadcast configuration parameters:
/ **
* aliyun broadcast configuration parameters
* /
@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;

/ **
* test which streamName is broadcast live type} {_} {type 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;

/ **
* authentication url effective time (sec), 30 minutes by default, 1800 seconds Key
* /
@Value ( "aliyun.live.ident.url.validTime $ {}")
Private aliyunLiveIdentUrlValidTime Integer;

}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
39
40
41 is
42 is
43 is
44 is
45
46 is
47
48
49
50
3. live type:
/ **
* live type
* /
@ApiModel (description = "live type")
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 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
39
40
41 is
42 is
43 is
44 is
45
46 is
47
48
49
. 4, the application.properties parameters:
# Ali goes live configure the begin
# plug flow domain
aliyun.live.push.domain = www.xxx.xxx.com
# flowmakers Key authentication url
aliyun.live.push.ident.key = xxx
# pull flow domain
aliyun.live = www.xxx.xxx.com .pull.domain
# pull stream Key authentication URL
aliyun.live.pull.ident.key = XXX
# live test appName
aliyun.live.appName = TTEST
# broadcast live type test which streamName is {{} } type ID
aliyun.live.streamName = {} {}
valid authentication url # time (sec), 30 minutes by default, 1800 seconds
aliyun.live.ident.url.validTime = 1800
# aliyun Air arranged End
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 5, POM address inside the tools, may not need to write their own
<-! Hutool Kits ->
<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, the result of initiating the request:

Guess you like

Origin www.cnblogs.com/ly570/p/10942283.html