参数描述
要配置出正确的鉴权,需要明确以下几个参数:
- 推流地址:完整的推流地址,形如:rtmp://video-center.alivecdn.com/{AppName}/{StreamName}?vhost={yourdomain}
- 鉴权类型:阿里云CDN 兼容并支持A、B、C三种鉴权方式,具体见URL鉴权方式。这里选择的是A类型
- 鉴权KEY:privatekey字段用户可以自行设置
- 时间戳:时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数
- 有效时间:以秒为单位的整数时间,用来控制直播推流时效
鉴权计算
用户访问加密 URL:
rtmp://video-center.alivecdn.com/{AppName}/{StreamName}?vhost={yourdomain}&auth_key={timestamp}-{rand}-{uid}-{hashvalue}| auth_key字段 | 描述 |
|---|---|
| timestamp | 失效时间=时间戳+有效时间,CDN服务器拿到请求后,首先会判断请求中的失效时间是否小于当前时间,如果小于,则认为过期失效并返回HTTP 403错误。 |
| rand | 随机数,一般设成0 |
| uid | 暂未使用(设置成0) |
| hashvalue | 通过md5加密算法计算出的32位验证串 |
hashvalue 计算方式如下:
sstring = /{AppName}/{StreamName}-{timestamp}-{rand}-{uid}-{privatekey}
hashvalue = md5(sstring)输入OBS中的鉴权内容
- url:rtmp://video-center.alivecdn.com/{AppName}
- 流密钥:{StreamName}?vhost={yourdomain}&auth_key={timestamp}-{rand}-{uid}-{hashvalue}
直播地址计算
rtmpPlayer = /{AppName}/{StreamName}-{timestamp}--{rand}-{uid}-{privatekey}
flvPlayer = /{AppName}/{StreamName.flv}-{timestamp}--{rand}-{uid}-{privatekey}
m3u8Player = /{AppName}/{StreamName.m3u8}-{timestamp}--{rand}-{uid}-{privatekey}直播播放地址
rtmpURL:http://{yourdomain}/{AppName}/{StreamName}?auth_key={timestamp}-{rand}-{uid}-{md5(rtmpPlayer)}
flvURL:http://{yourdomain}/{AppName}/{StreamName.flv}?auth_key={timestamp}-{rand}-{uid}-{md5(flvPlayer)}
m3u8URL:http://{yourdomain}/{AppName}/{StreamName.m3u8}?auth_key={timestamp}-{rand}-{uid}-{md5(m3u8Player)}PHP鉴权代码示例
<?php
function get_live_url($streamName, $domain, $appName, $privateKey, $validTime = 3600) {
$timestamp = time() + $validTime;
$rand = 0;
$uid = 0;
// RTMP播放鉴权
$sstring = "/{$appName}/{$streamName}-{$timestamp}--{$rand}-{$uid}-{$privateKey}";
$hashValue = md5($sstring);
$rtmpUrl = "http://{$domain}/{$appName}/{$streamName}?auth_key={$timestamp}-{$rand}-{$uid}-{$hashValue}";
// FLV播放鉴权
$sstring = "/{$appName}/{$streamName}.flv-{$timestamp}--{$rand}-{$uid}-{$privateKey}";
$hashValue = md5($sstring);
$flvUrl = "http://{$domain}/{$appName}/{$streamName}.flv?auth_key={$timestamp}-{$rand}-{$uid}-{$hashValue}";
// M3U8播放鉴权
$sstring = "/{$appName}/{$streamName}.m3u8-{$timestamp}--{$rand}-{$uid}-{$privateKey}";
$hashValue = md5($sstring);
$m3u8Url = "http://{$domain}/{$appName}/{$streamName}.m3u8?auth_key={$timestamp}-{$rand}-{$uid}-{$hashValue}";
return [
"rtmp" => $rtmpUrl,
"flv" => $flvUrl,
"m3u8" => $m3u8Url
];
}
?>注意事项
- 鉴权KEY需要妥善保管,不要泄露给第三方
- 时间戳有效性建议设置为1-2小时,避免过长导致安全隐患
- OBS推流时,鉴权地址分为服务器地址和流密钥两部分配置
- 如需停止直播,等待auth_key过期即可自动失效

发表评论 取消回复