All Products
Search
Document Center

:How do I use PHP or Java code to generate an ingest URL and a streaming URL?

Last Updated:Oct 30, 2023

ApsaraVideo Live provides no API operations that are used to generate ingest or streaming URLs for live streams. You must construct ingest and streaming URLs by using the concatenation rules before you can use a stream ingest tool to ingest streams and use a player to play streams. This topic describes how to use PHP or Java code to generate an ingest URL and a streaming URL for a live stream.

Sample code in PHP

The following PHP code provides an example on how to generate an ingest URL and a streaming URL.

<?php
  
/**
  * Generate an ingest URL.
  * @param $push_domain The ingest domain.
  * @param $push_key The authentication key configured for the ingest domain.
  * @param $expireTime The validity period. Unit: seconds.
  * @param $appName The application name for stream ingest.
  * @param $streamName The name of the ingested stream.
  */
function push_url($push_domain,$push_key,$expireTime,$appName,$streamName){

        $push_url = '';
        // Generate an ingest URL when no authentication key is configured.
        if($push_key==''){
                $push_url = 'rtmp://'.$push_domain.'/'.$appName.'/'.$streamName;
                echo $push_url;
                return;
        }
        $timeStamp = time() + $expireTime;
        $sstring = '/'.$appName.'/'.$streamName.'-'.$timeStamp.'-0-0-'.$push_key;
        $md5hash = md5($sstring);
        $push_url = 'rtmp://'.$push_domain.'/'.$appName.'/'.$streamName.'?auth_key='.$timeStamp.'-0-0-'.$md5hash;
        echo $push_url;
        echo PHP_EOL;
        return;
}
/**
 * Generate a streaming URL.
 * @param $play_domain The streaming domain.
 * @param $play_key The authentication key for playback.
 * @param $expireTime The validity period. Unit: seconds.
 * @param $appName The application name for playback. It is the same as the application name for stream ingest.
 * @param $streamName The name of the played stream. It is the same as the name of ingested stream if the source stream is played. If a transcoded stream is played, add "_{Transcoding template ID}" after the name of the ingested stream.
 */
function play_url($play_domain,$play_key,$expireTime,$appName,$streamName){
        // Generate a streaming URL when no authentication key is configured.
        if($play_key==''){
                $rtmp_play_url = 'rtmp://'.$play_domain.'/'.$appName.'/'.$streamName;
                $flv_play_url = 'http://'.$play_domain.'/'.$appName.'/'.$streamName.'.flv';
                $hls_play_url = 'http://'.$play_domain.'/'.$appName.'/'.$streamName.'.m3u8';
        }else{
                $timeStamp = time() + $expireTime;

                $rtmp_sstring = '/'.$appName.'/'.$streamName.'-'.$timeStamp.'-0-0-'.$play_key;
                $rtmp_md5hash = md5($rtmp_sstring);
                $rtmp_play_url = 'rtmp://'.$play_domain.'/'.$appName.'/'.$streamName.'?auth_key='.$timeStamp.'-0-0-'.$rtmp_md5hash;

                $flv_sstring = '/'.$appName.'/'.$streamName.'.flv-'.$timeStamp.'-0-0-'.$play_key;
                $flv_md5hash = md5($flv_sstring);
                $flv_play_url = 'http://'.$play_domain.'/'.$appName.'/'.$streamName.'.flv?auth_key='.$timeStamp.'-0-0-'.$flv_md5hash;

                $hls_sstring = '/'.$appName.'/'.$streamName.'.m3u8-'.$timeStamp.'-0-0-'.$play_key;
                $hls_md5hash = md5($hls_sstring);
                $hls_play_url = 'http://'.$play_domain.'/'.$appName.'/'.$streamName.'.m3u8?auth_key='.$timeStamp.'-0-0-'.$hls_md5hash;
        }

        echo 'RTMP streaming URL: '.$rtmp_play_url;
        echo PHP_EOL;
        echo 'FLV streaming URL: '.$flv_play_url;
        echo PHP_EOL;
        echo 'HLS streaming URL: '.$hls_play_url;
        echo PHP_EOL;
        return;
}
// Specify the ingest domain.
$push_domain = 'testpush.cn';
// Specify the authentication key configured for the ingest domain.
$push_key = 'test_Push_key';
// Generate a random string for appName.
$appName = md5(uniqid(microtime(true),true));
// Generate a random string for streamName.
$streamName = md5(uniqid(microtime(true),true));
// Set the validity period to 1 hour.
$expireTime = 3600;
// Specify the streaming domain.
$play_domain = 'testPlay.cn';
// Specify the authentication key configured for the streaming domain.
$play_key = 'test_play_key';
push_url($push_domain,$push_key,$expireTime,$appName,$streamName);
play_url($play_domain,$play_key,$expireTime,$appName,$streamName);
?>

Sample code in Java

The following Java code provides an example on how to generate an ingest URL and a streaming URL.

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 hash value.
 * @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);
        // Use 16 to indicate a hexadecimal value.
 String result = bigInt.toString(16);
        // Add zeros in front of the value to construct a 32-digit number.
 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.
  * @param pushKey The authentication key configured for the ingest domain.
  * @param appName The application name for stream ingest.
  * @param streamName The name of the ingested stream.
  * @param expireTime The validity period. Unit: seconds.
  */
public static void generate_push_url(String pushDomain,String pushKey,String appName,String streamName,long expireTime) {
 String pushUrl = "";
 // Generate an ingest URL when URL signing is not enabled for the ingest domain.
 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);
}
/**
 * Generate a streaming URL.
 * @param pullDomain The streaming domain.
 * @param pullKey The authentication key for playback.
 * @param appName The application name for playback. It is the same as the application name for stream ingest.
 * @param streamName The name of the played stream. It is the same as the name of ingested stream if the source stream is played. If a transcoded stream is played, add "_{Transcoding template ID}" after the name of the ingested stream.
 * @param expireTime The validity period. Unit: seconds.
 */
 public static void general_pull_url(String pullDomain,String pullKey,String appName,String streamName,long expireTime) {
 String rtmpUrl = ""; // The streaming URL in the RTMP format.
 String hlsUrl = ""; // The streaming URL in the M3U8 format.
 String flvUrl = ""; // The streaming URL in the FLV format.
 // Generate a streaming URL when URL signing is not enabled for the streaming domain.
 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 streaming URL: "+hlsUrl);
 System.out.println("FLV streaming URL: "+flvUrl);
}
public static void main(String[] args) {
 // TODO Auto-generated method stub
 // Generate random strings for appName and streamName. Each string must be 5 characters in length and contain digits and letters.
 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);
}
}