All Products
Search
Document Center

Sample Java code for generating ingest and playback URLs for apsaravideo live

Last Updated: Sep 27, 2020

Introduction

You do not need to call the ingest URL and streaming URL of apsaravideo live. You need to concatenate the ingest URL and streaming URL on the client, and then use an ingest tool or player to ingest or play the stream. This topic describes how to generate the ingest URL and streaming URL for live streaming.

Background

The Java sample code for generating the ingest URL and streaming URL for apsaravideo live is as follows:

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.lang3. *;
public class Create_Live_Url {
 
 /**
  * Calculate the 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);
                 // Parameter 16 indicates 16:00.
   String result = bigInt.toString(16);
                 // Less than 32-bit high-order zero fill
   while(result.length() < 32) {
    result = "0" + result;
   }
   return result;
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  }
  return null;
 }
/**
     * Generate an ingest URL.
     * @ param pushDomain The ingest domain name.
     * @ param pushKey the authentication Key configured for the ingest domain name
     * @ param appName the stream ingest AppName.
     * @ param streamName the name of the ingest StreamName.
     * @ param expireTime the expiration time in seconds
     */
 public static void generate_push_url(String pushDomain,String pushKey,String appName,String streamName,long expireTime) {
  String pushUrl = "";
  // When ingest domain name authentication is not enabled
  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("ingest URL:" + pushUrl);
 }
/**
  * Method for generating the playback address
  * @ param pullDomain playback domain name
  * @ param pullKey // The playback authentication Key.
  * @ param appName the playback appName (same as the stream ingest appName)
  * @ param streamName the stream name. This parameter is the same as the stream name parameter.
  * @ param expireTime the expiration time in seconds
  */
 public static void general_pull_url(String pullDomain,String pullKey,String appName,String streamName,long expireTime) {
  String rtmpUrl = ""; // rtmp stream pulling address
  String hlsUrl = ""; // m3u8 stream pulling address
  String flv url=""; // flv URL
  // When the playback domain name is not configured with an authentication 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 streaming URL:" + rtmpUrl);
  System.out.println("m3u8 playback URL:" + hlsUrl);
  System.out.println("flv playback URL:" + flv URL);
 }
public static void main(String[] args) {
  // TODO Auto-generated method stub
  // Generate a random string of 5 as appName and streamName (combination of letters and numbers)
  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);
 }
}

Scope

  • Live video streaming