Skip to content

Commit fa675c2

Browse files
committed
更新,增加更多非交互接口
1 parent 06bdc46 commit fa675c2

File tree

1 file changed

+197
-1
lines changed

1 file changed

+197
-1
lines changed

Diff for: src/Client.php

+197-1
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,84 @@ class Client
5151
*/
5252
public $baseUri = 'http://live.aliyuncs.com/';
5353

54+
/**
55+
* @var string 应用名称
56+
*/
57+
public $appName;
58+
59+
/**
60+
* @var string 应用域名
61+
*/
62+
public $domain;
63+
64+
/**
65+
* @var string 推流鉴权
66+
*/
67+
public $pushAuth;
68+
69+
/**
70+
* @var string 媒体中心地址
71+
*/
72+
public $pushDomain = 'video-center.alivecdn.com';
73+
74+
/**
75+
* @var bool 是否使用安全连接
76+
*/
77+
public $secureConnection = false;
78+
79+
/**
80+
* @var int 签名有效期,默认有效期是一周
81+
*/
82+
public $authTime = 604800;
83+
84+
/**
85+
* @var int 秘钥过期时间
86+
*/
87+
private $expirationTime;
88+
89+
/**
90+
* @var string 播放协议
91+
*/
92+
private $playScheme;
93+
94+
/**
95+
* @var string 播放地址
96+
*/
97+
private $httpPlayUrl;
98+
5499
/**
55100
* @var HttpClient
56101
*/
57102
private $_httpClient;
58103

59104
/**
60-
* Request constructor.
105+
* Client constructor.
61106
* @param array $config
107+
* @throws \Exception
62108
*/
63109
public function __construct($config = [])
64110
{
65111
foreach ($config as $name => $value) {
66112
$this->{$name} = $value;
67113
}
114+
115+
$this->expirationTime = time() + $this->authTime;
116+
$this->playScheme = $this->secureConnection ? 'https://' : 'http://';
117+
$this->httpPlayUrl = $this->playScheme . $this->domain;
118+
119+
if (empty ($this->accessKeyId)) {
120+
throw new \Exception ('The "accessKeyId" property must be set.');
121+
}
122+
if (empty ($this->accessSecret)) {
123+
throw new \Exception ('The "accessSecret" property must be set.');
124+
}
125+
if (empty ($this->appName)) {
126+
throw new \Exception ('The "appName" property must be set.');
127+
}
128+
129+
if (empty ($this->domain)) {
130+
throw new \Exception ('The "domain" property must be set.');
131+
}
68132
}
69133

70134
/**
@@ -103,4 +167,136 @@ public function createRequest(array $params)
103167
{
104168
return $this->getHttpClient()->get('/', ['query' => $params]);
105169
}
170+
171+
/**
172+
* 直播签名
173+
* @param string $streamName
174+
* @return string
175+
*/
176+
protected function getSign($streamName)
177+
{
178+
$uri = "/{$this->appName}/{$streamName}";
179+
if ($this->pushAuth) {
180+
$authKey = "?vhost={$this->domain}&auth_key={$this->expirationTime}-0-0-" . md5("{$uri}-{$this->expirationTime}-0-0-{$this->pushAuth}");
181+
} else {
182+
$authKey = "?vhost={$this->domain}";
183+
}
184+
return $authKey;
185+
}
186+
187+
/**
188+
* 获取推流地址
189+
* @return string
190+
*/
191+
public function getPushPath()
192+
{
193+
return "rtmp://{$this->pushDomain}/{$this->appName}/";
194+
}
195+
196+
/**
197+
* 获取串码流
198+
* @param string $streamName 流名称
199+
* @return string
200+
*/
201+
public function getPushArg($streamName)
202+
{
203+
return $streamName . $this->getSign($streamName);
204+
}
205+
206+
/**
207+
* 获取直播推流地址
208+
* @param string $streamName
209+
* @return string
210+
*/
211+
public function getPushUrl($streamName)
212+
{
213+
$uri = "/{$this->appName}/{$streamName}";
214+
return "rtmp://{$this->pushDomain}" . $uri . $this->getSign($streamName);
215+
}
216+
217+
/**
218+
* 验证签名
219+
* @param string $streamName
220+
* @param string $usrargs
221+
* @return bool
222+
*/
223+
public function checkSign($streamName, $usrargs)
224+
{
225+
parse_str($usrargs, $args);
226+
if (isset($args['vhost']) && isset($args['auth_key'])) {
227+
if ($args['vhost'] != $this->domain) {
228+
return false;
229+
}
230+
$params = explode('-', $args['auth_key'], 4);
231+
if (isset($params[0]) && $params[3]) {
232+
$uri = "/{$this->appName}/{$streamName}";
233+
if ($params[3] == md5("{$uri}-{$params[0]}-0-0-{$this->pushAuth}")) {
234+
return true;
235+
}
236+
237+
}
238+
}
239+
return false;
240+
}
241+
242+
/**
243+
* 获取签名
244+
* @param string $uri
245+
* @return string
246+
*/
247+
protected function getAuthKey($uri)
248+
{
249+
$authKey = '';
250+
if ($this->pushAuth) {
251+
$authKey = "?auth_key={$this->expirationTime}-0-0-" . md5("{$uri}-{$this->expirationTime}-0-0-{$this->pushAuth}");
252+
}
253+
return $authKey;
254+
}
255+
256+
/**
257+
* 获取RTMP拉流地址
258+
* @param string $streamName
259+
* @return string
260+
*/
261+
public function getPlayUrlForRTMP($streamName)
262+
{
263+
$uri = "/{$this->appName}/{$streamName}";
264+
return 'rtmp://' . $this->domain . $uri . $this->getAuthKey($uri);
265+
}
266+
267+
/**
268+
* 获取FLV播放地址
269+
* @param string $streamName
270+
* @return string
271+
*/
272+
public function getPlayUrlForFLV($streamName)
273+
{
274+
$uri = "/{$this->appName}/{$streamName}.flv";
275+
return $this->httpPlayUrl . $uri . $this->getAuthKey($uri);
276+
}
277+
278+
/**
279+
* 获取M3U8播放地址
280+
* @param string $streamName
281+
* @return string
282+
*/
283+
public function getPlayUrlForM3U8($streamName)
284+
{
285+
$uri = "/{$this->appName}/{$streamName}.m3u8";
286+
return $this->httpPlayUrl . $uri . $this->getAuthKey($uri);
287+
}
288+
289+
/**
290+
* 获取阿里云播放地址
291+
* @param string $streamName
292+
* @return array
293+
*/
294+
public function getPlayUrls($streamName)
295+
{
296+
return [
297+
'rtmp' => $this->getPlayUrlForRTMP($streamName),
298+
'flv' => $this->getPlayUrlForFLV($streamName),
299+
'm3u8' => $this->getPlayUrlForM3U8($streamName)
300+
];
301+
}
106302
}

0 commit comments

Comments
 (0)