生成直播地址

7007

代码拼接生成1.拼接URI

拼接规则为{协议}://{直播域名}/{AppName}/{streamName},各种协议示例请参考推流地址示例或播流地址示例。

// 伪代码

protocol = "rtmp"

domain = "example.aliyundoc.com"

appName = "liveApp"

streamName = "liveStream"

uri = protocol + "://" + domain + "/" + appName + "/" + streamName

// 结果: rtmp://example.aliyundoc.com/liveApp/liveStream2.获取鉴权Key

鉴权Key用于生成鉴权串,您可以通过控制台URL鉴权配置或调用查询直播域名配置API获取鉴权Key。

说明 推流地址使用推流域名的鉴权Key,播放地址使用播流域名的鉴权Key。

3.拼接为直播地址

以下示例代码以RTMP协议为例,先生成{鉴权串},再拼接出完整的直播地址:

Javaimport java.math.BigInteger;

import java.nio.charset.StandardCharsets;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class AuthDemo {

private static String md5Sum(String src) {

MessageDigest md5 = null;

try {

md5 = MessageDigest.getInstance("MD5");

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

}

md5.update(StandardCharsets.UTF_8.encode(src));

return String.format("%032x", new BigInteger(1, md5.digest()));

}

private static String aAuth(String uri, String key, long exp) {

String pattern = "^(rtmp://)?([^/?]+)(/[^?]*)?(\\\\?.*)?$";

Pattern r = Pattern.compile(pattern);

Matcher m = r.matcher(uri);

String scheme = "", host = "", path = "", args = "";

if (m.find()) {

scheme = m.group(1) == null ? "rtmp://" : m.group(1);

host = m.group(2) == null ? "" : m.group(2);

path = m.group(3) == null ? "/" : m.group(3);

args = m.group(4) == null ? "" : m.group(4);

} else {

System.out.println("NO MATCH");

}

String rand = "0"; // "0" by default, other value is ok

String uid = "0"; // "0" by default, other value is ok

String sString = String.format("%s-%s-%s-%s-%s", path, exp, rand, uid, key);

String hashValue = md5Sum(sString);

String authKey = String.format("%s-%s-%s-%s", exp, rand, uid, hashValue);

if (args.isEmpty()) {

return String.format("%s%s%s%s?auth_key=%s", scheme, host, path, args, authKey);

} else {

return String.format("%s%s%s%s&auth_key=%s", scheme, host, path, args, authKey);

}

}

public static void main(String[] args) {

// 待加密的推/播流地址,example.aliyundoc.com为推/播流域名,liveApp为AppName,liveStream为StreamName

// 推流和播流URL采用同样的加密方法

// AppName或StreamName不超过256字符,支持数字、大小写字母、短划线(-)、下划线(_)、等号(=)。

String uri = "rtmp://example.aliyundoc.com/liveApp/liveStream";

// 鉴权Key,推流地址使用推流域名URL鉴权Key,播流地址使用播流域名URL鉴权Key

String key = "";

// exp值为UNIX时间戳,单位秒,最终失效时间由该值加上域名URL鉴权有效时长决定

// 比如您在此处设置exp为:当前时间+3600秒,那最终失效时间为:当前时间+3600秒+域名URL鉴权有效时长。如果设置exp为:当前时间,那最终失效时间为:当前时间+域名URL鉴权有效时长

long exp = System.currentTimeMillis() / 1000 + 1 * 3600;

String authUri = aAuth(uri, key, exp);

System.out.printf("URL : %s\nAuth: %s", uri, authUri);

}

}Pythonimport re

import time

import hashlib

import datetime

def md5sum(src):

m = hashlib.md5()

if isinstance(src, str):

src = src.encode('utf-8')

m.update(src)

return m.hexdigest()

def a_auth(uri, key, exp):

p = re.compile("^(rtmp://)?([^/?]+)(/[^?]*)?(\\?.*)?$")

if not p:

return None

m = p.match(uri)

scheme, host, path, args = m.groups()

if not scheme: scheme = "rtmp://"

if not path: path = "/"

if not args: args = ""

rand = "0" # "0" by default, other value is ok

uid = "0" # "0" by default, other value is ok

sstring = "%s-%s-%s-%s-%s" %(path, exp, rand, uid, key)

hashvalue = md5sum(sstring.encode('utf-8'))

auth_key = "%s-%s-%s-%s" %(exp, rand, uid, hashvalue)

if args:

return "%s%s%s%s&auth_key=%s" %(scheme, host, path, args, auth_key)

else:

return "%s%s%s%s?auth_key=%s" %(scheme, host, path, args, auth_key)

def main():

# 待加密的推/播流地址,example.aliyundoc.com为推/播流域名,liveApp为AppName,liveStream为StreamName

# 推流和播流URL采用同样的加密方法

# AppName或StreamName不超过256字符,支持数字、大小写字母、短划线(-)、下划线(_)、等号(=)。

uri = "rtmp://example.aliyundoc.com/liveApp/liveStream"

# 鉴权Key,推流地址使用推流域名URL鉴权Key,播流地址使用播流域名URL鉴权Key

key = ""

# exp值为UNIX时间戳,单位秒,最终失效时间由该值加上域名URL鉴权有效时长决定

# 比如您在此处设置exp为:当前时间+3600秒,那最终失效时间为:当前时间+3600秒+域名URL鉴权有效时长。如果设置exp为:当前时间,那最终失效时间为:当前时间+域名URL鉴权有效时长

exp = int(time.time()) + 1 * 3600

authuri = a_auth(uri, key, exp)

print("URL : %s\nAUTH: %s" %(uri, authuri))

if __name__ == "__main__":

main()Gopackage main

import (

"crypto/md5"

"encoding/hex"

"fmt"

"regexp"

"time"

)

func md5sum(src string) string {

h := md5.New()

h.Write([]byte(src))

return hex.EncodeToString(h.Sum(nil))

}

func a_auth(uri, key string, exp int64) string {

p, err := regexp.Compile("^(rtmp://)?([^/?]+)(/[^?]*)?(\\?.*)?$")

if err != nil {

fmt.Println(err)

return ""

}

m := p.FindStringSubmatch(uri)

var scheme, host, path, args string

if len(m) == 5 {

scheme, host, path, args = m[1], m[2], m[3], m[4]

} else {

scheme, host, path, args = "rtmp://", "", "/", ""

}

rand := "0" // "0" by default, other value is ok

uid := "0" // "0" by default, other value is ok

sstring := fmt.Sprintf("%s-%d-%s-%s-%s", path, exp, rand, uid, key)

hashvalue := md5sum(sstring)

auth_key := fmt.Sprintf("%d-%s-%s-%s", exp, rand, uid, hashvalue)

if len(args) != 0 {

return fmt.Sprintf("%s%s%s%s&auth_key=%s", scheme, host, path, args, auth_key)

} else {

return fmt.Sprintf("%s%s%s%s?auth_key=%s", scheme, host, path, args, auth_key)

}

}

func main() {

// 待加密的推/播流地址,example.aliyundoc.com为推/播流域名,liveApp为AppName,liveStream为StreamName

// 推流和播流URL采用同样的加密方法

// AppName或StreamName不超过256字符,支持数字、大小写字母、短划线(-)、下划线(_)、等号(=)。

uri := "rtmp://example.aliyundoc.com/liveApp/liveStream"

// 鉴权Key,推流地址使用推流域名URL鉴权Key,播流地址使用播流域名URL鉴权Key

key := ""

// exp值为UNIX时间戳,单位秒,最终失效时间由该值加上域名URL鉴权有效时长决定

// 比如您在此处设置exp为:当前时间+3600秒,那最终失效时间为:当前时间+3600秒+域名URL鉴权有效时长。如果设置exp为:当前时间,那最终失效时间为:当前时间+域名URL鉴权有效时长

exp := time.Now().Unix() + 3600

authuri := a_auth(uri, key, exp)

fmt.Printf("URL : %s\nAUTH: %s", uri, authuri)

}PHP

function a_auth($uri, $key, $exp) {

preg_match("/^(rtmp:\/\/)?([^\/?]+)?(\/[^?]*)?(\?.*)?$/", $uri, $matches);

$scheme = $matches[1];

$host = $matches[2];

$path = $matches[3];

$args = $matches[4];

if (empty($args)) {

$args ="";

}

if (empty($scheme)) {

$scheme ="rtmp://";

}

if (empty($path)) {

$path ="/";

}

$rand = "0";

// "0" by default, other value is ok

$uid = "0";

// "0" by default, other value is ok

$sstring = sprintf("%s-%u-%s-%s-%s", $path, $exp, $rand, $uid, $key);

$hashvalue = md5($sstring);

$auth_key = sprintf("%u-%s-%s-%s", $exp, $rand, $uid, $hashvalue);

if ($args) {

return sprintf("%s%s%s%s&auth_key=%s", $scheme, $host, $path, $args, $auth_key);

} else {

return sprintf("%s%s%s%s?auth_key=%s", $scheme, $host, $path, $args, $auth_key);

}

}

// 待加密的推/播流地址,example.aliyundoc.com为推/播流域名,liveApp为AppName,liveStream为StreamName

// 推流和播流URL采用同样的加密方法

// AppName或StreamName不超过256字符,支持数字、大小写字母、短划线(-)、下划线(_)、等号(=)。

$uri = "rtmp://example.aliyundoc.com/liveApp/liveStream";

// 鉴权Key,推流地址使用推流域名URL鉴权Key,播流地址使用播流域名URL鉴权Key

$key = "";

// exp值为UNIX时间戳,单位秒,最终失效时间由该值加上域名URL鉴权有效时长决定

// 比如您在此处设置exp为:当前时间+3600秒,那最终失效时间为:当前时间+3600秒+域名URL鉴权有效时长。如果设置exp为:当前时间,那最终失效时间为:当前时间+域名URL鉴权有效时长

$exp = time() + 3600;

$authuri = a_auth($uri, $key, $exp);

echo "URL :" . $uri;

echo PHP_EOL;

echo "AUTH:" . $authuri;

?>C#using System;

using System.Text.RegularExpressions;

using System.Security.Cryptography;

using System.Text;

public class Test

{

public static void Main()

{

// 待加密的推/播流地址,example.aliyundoc.com为推/播流域名,liveApp为AppName,liveStream为StreamName

// 推流和播流URL采用同样的加密方法

// AppName或StreamName不超过256字符,支持数字、大小写字母、短划线(-)、下划线(_)、等号(=)。

string uri= "rtmp://example.aliyundoc.com/liveApp/liveStream";

// 鉴权Key,推流地址使用推流域名URL鉴权Key,播流地址使用播流域名URL鉴权Key

string key= "";

DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0);

// exp值为UNIX时间戳,单位秒,最终失效时间由该值加上域名URL鉴权有效时长决定

// 比如您在此处设置exp为:当前时间+3600秒,那最终失效时间为:当前时间+3600秒+域名URL鉴权有效时长。如果设置exp为:当前时间,那最终失效时间为:当前时间+域名URL鉴权有效时长

string exp = Convert.ToInt64((DateTime.Now - dateStart).TotalSeconds+3600).ToString();

string authUri = aAuth(uri, key, exp);

Console.WriteLine (String.Format("URL :{0}",uri));

Console.WriteLine (String.Format("AUTH :{0}",authUri));

}

public static string aAuth(string uri, string key, string exp)

{

Regex regex = new Regex("^(rtmp://)?([^/?]+)(/[^?]*)?(\\\\?.*)?$");

Match m = regex.Match(uri);

string scheme = "rtmp://", host = "", path = "/", args = "";

if (m.Success)

{

scheme=m.Groups[1].Value;

host=m.Groups[2].Value;

path=m.Groups[3].Value;

args=m.Groups[4].Value;

}else{

Console.WriteLine ("NO MATCH");

}

string rand = "0"; // "0" by default, other value is ok

string uid = "0"; // "0" by default, other value is ok

string u = String.Format("{0}-{1}-{2}-{3}-{4}", path, exp, rand, uid, key);

string hashValue = Md5(u);

string authKey = String.Format("{0}-{1}-{2}-{3}", exp, rand, uid, hashValue);

if (args=="")

{

return String.Format("{0}{1}{2}{3}?auth_key={4}", scheme, host, path, args, authKey);

} else

{

return String.Format("{0}{1}{2}{3}&auth_key={4}", scheme, host, path, args, authKey);

}

}

public static string Md5(string value)

{

MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

byte[] bytes = Encoding.ASCII.GetBytes(value);

byte[] encoded = md5.ComputeHash(bytes);

StringBuilder sb = new StringBuilder();

for(int i=0; i

{

sb.Append(encoded[i].ToString("x2"));

}

return sb.ToString();

}

}