Java Alibaba Cloud live broadcast configuration and push-pull stream address acquisition

1. Enable Alibaba Cloud Live Streaming

  1. First, go to the homepage of Alibaba Cloud Live Products: https://www.aliyun.com/product/live . Click "Activate Now" below. If you are a user who has not yet registered, please follow the prompts on the page to complete the registration and verify your real name.

insert image description here

2. When entering for the first time, you will be prompted to activate the service, click "Activate Service", then select the billing method, agree to the service agreement, and activate immediately.

  • According to the use, 流量计费it is suitable for the situation where the number of people watching the live broadcast is small,
  • It 宽带峰值计费is suitable for use when there are a large number of viewers. If you are not sure, you will be charged according to the traffic used.
    insert image description here
    The initial usage is not too much. After choosing to “流量计费”
    insert image description here
    activate according to the service, according to the official website process, the next step is the basic configuration of the live broadcast:域名管理

insert image description here

2. Access push domain name

1. Since Alibaba Cloud Live does not provide a streaming domain name, we need to access 自己的the streaming domain name. Click on the left “加速服务~添加域名”.

insert image description here
2. Follow the example in the picture below, fill in according to your actual situation, and then submit.
insert image description here

3. Go back to "Domain Name Management", and you will see the streaming domain name you just added, copy the “CNAME”value on the right side of the domain name, and go to your domain name service provider for analysis.

insert image description here

insert image description here
4. After configuring the previous step, return to the domain name management, wait a few minutes for the resolution to take effect, and you will see that the domain name status is already “正常运行”. Click on the right side of the domain name “域名配置”to enter the configuration page.

insert image description here

Note: It is recommended to configure first 推流. Because you will need to associate the push stream when configuring the streaming later, so configure it first

5. Click on the left “访问控制”, then modify URL鉴权the configuration, fill in the example shown in the figure below, and then submit.
insert image description here
Pop-up window, modify configuration
insert image description here

3. Access the streaming domain name

The first four steps are similar to the process of accessing the streaming domain name, so I won’t repeat them here

5. Associate push domain name.点击“基本配置~推流信息”

insert image description here

#######################Dividing line#######################

insert image description here
#######################Dividing line#######################
insert image description here

Click "OK". After the confirmation is completed, basically our streaming domain name and streaming domain name are configured.

4. Test tools

After completing the configuration of the streaming domain name and the streaming domain name, you can use the tools provided by Ali to generate the streaming/push streaming. Click ####################
on the left “工具箱~地址生成器”
insert image description here
#####Dividing line#######################
insert image description here

Five, java code generates push-pull flow address

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.lang3.*;
public class Create_Live_Url {
    
     

 /**
  * 计算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 void 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);
 }

/**
* 生成播放地址
* @param pullDomain 播放域名
* @param pullKey 播放鉴权Key
* @param appName 播放appName(同推流appName)
* @param streamName 播放streamName 同推流streamName)
* @param expireTime 过期时间(单位是秒
*/ 

public static void general_pull_url(String pullDomain,String pullKey,String appName,String streamName,long expireTime) {
    
    
  	String rtmpUrl = ""; //rtmp的拉流地址
  	String hlsUrl = "";  //m3u8的拉流地址
  	String flvUrl = "";  //flv的拉流地址
  	//播放域名未配置鉴权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);
}
 	
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 = "mxl-pull.pier39.cn";
  	String pullKey = "querty1234";
  
    String pushDomain = "mxl-push.pier39.cn";
  	String pushKey = "querty123";
  	Create_Live_Url.general_pull_url(pullDomain, pullKey, appName, streamName, expireTime);
  	Create_Live_Url.generate_push_url(pushDomain, pushKey, appName, streamName, expireTime);
}


} //end class

Reference: https://help.aliyun.com/document_detail/456848.htm?spm=5176.13499635.help.dexternal.6b482699KJQJLs

Guess you like

Origin blog.csdn.net/zhanglei5415/article/details/131551685