日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

如何通過PHP或Java代碼生成推流地址和播放地址?

直播推流地址和播放地址本身沒有API接口,需要在客戶端自行拼接地址,然后使用推流工具或者播放器對其推流或播放。本文介紹如何通過PHP、Java代碼生成直播的推流以及播放地址。

PHP代碼示例

視頻直播生成推流地址和播放地址的PHP示例代碼如下所示。

<?php
  
/**
  * 生成推流地址
  * @param $push_domain 推流域名
  * @param $push_key 推流域名配置的鑒權Key
  * @param $expireTime 過期時間(單位是秒)
  * @param $appName 推流AppName
  * @param $streamName 推流StreamName
  */
function push_url($push_domain,$push_key,$expireTime,$appName,$streamName){

        $push_url = '';
        //未開啟鑒權Key的情況下
        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;
}
/**
 * 生成播放地址
 * @param $play_domain 播放域名
 * @param $play_key 播放鑒權Key
 * @param $expireTime 過期時間(單位是秒)
 * @param $appName 播放appName(同推流appName)
 * @param $streamName 播放streamName,播放源流時,streamName 同推流streamName;播放轉碼流時,streamName 為推流streamName_{轉碼模板ID}
 */
function play_url($play_domain,$play_key,$expireTime,$appName,$streamName){
        //未開啟鑒權Key的情況下
        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播放地址: '.$rtmp_play_url;
        echo PHP_EOL;
        echo 'flv播放地址: '.$flv_play_url;
        echo PHP_EOL;
        echo 'hls播放地址: '.$hls_play_url;
        echo PHP_EOL;
        return;
}
//推流域名
$push_domain = 'testpush.cn';
//推流域名配置的鑒權Key
$push_key = 'test_Push_key';
//生成隨機的AppName
$appName = md5(uniqid(microtime(true),true));
//生成隨機的StreamName
$streamName = md5(uniqid(microtime(true),true));
//配置過期時間為1小時
$expireTime = 3600;
//播放域名
$play_domain = 'testPlay.cn';
//播放域名配置的鑒權Key
$play_key = 'test_play_key';
push_url($push_domain,$push_key,$expireTime,$appName,$streamName);
play_url($play_domain,$play_key,$expireTime,$appName,$streamName);
?>

Java代碼示例

視頻直播生成推流地址和播放地址的Java示例代碼如下所示。

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 同推流streamName;播放轉碼流時,streamName 為推流streamName_{轉碼模板ID}
 * @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);
}
}