All Products
Search
Document Center

Sample 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 PHP sample code for generating the ingest URL and streaming URL for apsaravideo live is as follows:

? php

function push_url($push_domain,$push_key,$expireTime,$appName,$streamName){

        $push_url = '';
        // When authentication Key is disabled
        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;
}

function play_url($play_domain,$play_key,$expireTime,$appName,$streamName){
        // When authentication Key is disabled
        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 playback URL: '.$flv_play_url;
        echo PHP_EOL;
        echo 'hls playback URL: '.$hls_play_url;
        echo PHP_EOL;
        return;
}
// Ingest domain name
$push_domain = 'testpush.cn';
// The authentication Key of the ingest domain name.
$push_key = 'test_Push_key';
// Generate a random AppName
$appName = md5(uniqid(microtime(true),true));
// Generate a random StreamName
$streamName = md5(uniqid(microtime(true),true));
// Configure expiration time to 1 hour
$expireTime = 3600;
// Streaming domain name
$play_domain = 'testPlay.cn';
// The authentication Key configured for the playback domain name.
$play_key = 'test_play_key';
push_url($push_domain,$push_key,$expireTime,$appName,$streamName);
play_url($play_domain,$play_key,$expireTime,$appName,$streamName);
? >

Scope

  • Live video streaming