Skip to content

Commit 7f20f5a

Browse files
committed
Merge remote-tracking branch 'qiniu/master' into develop
2 parents 01df555 + c671b0b commit 7f20f5a

File tree

7 files changed

+362
-0
lines changed

7 files changed

+362
-0
lines changed

docs/rtc/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Rtc Streaming Cloud Server-Side Library For PHP
2+
3+
## Features
4+
5+
- App
6+
- [x] 创建房间: App->createApp()
7+
- [x] 查看房间: App->getApp()
8+
- [x] 删除房间: App->deleteApp()
9+
- [x] 生成房间token: App->AppToken()
10+
11+
12+
13+
## Contents
14+
15+
- [Installation](#installation)
16+
- [Usage](#usage)
17+
- [Configuration](#configuration)
18+
- [App](#app)
19+
- [Create a app](#create-a-app)
20+
- [Get a app](#get-a-app)
21+
- [Delete a app](#delete-a-app)
22+
- [Generate a app token](#generate-a-app-token)
23+
24+
25+
## Usage
26+
27+
### App
28+
29+
#### Create a app
30+
31+
```php
32+
$ak = "gwd_gV4gPKZZsmEOvAuNU1AcumicmuHooTfu64q5";
33+
$sk = "xxxx";
34+
$mac = new QiniuRtc\Mac($ak, $sk);
35+
$client = new QiniuRtc\AppClient($mac);
36+
$resp=$client->createApp("901","testApp");
37+
print_r($resp);
38+
```
39+
40+
#### Get a app
41+
42+
```php
43+
$ak = "gwd_gV4gPKZZsmEOvAuNU1AcumicmuHooTfu64q5";
44+
$sk = "xxxx";
45+
$mac = new QiniuRtc\Mac($ak, $sk);
46+
$client = new QiniuRtc\AppClient($mac);
47+
$resp=$client->getApp("deq02uhb6");
48+
print_r($resp);
49+
```
50+
51+
#### Delete a app
52+
53+
```php
54+
$ak = "gwd_gV4gPKZZsmEOvAuNU1AcumicmuHooTfu64q5";
55+
$sk = "xxxx";
56+
$mac = new QiniuRtc\Mac($ak, $sk);
57+
$client = new QiniuRtc\AppClient($mac);
58+
$resp=$client->deleteApp("deq02uhb6");
59+
print_r($resp);
60+
```
61+
62+
#### Generate a app token
63+
64+
```php
65+
$ak = "gwd_gV4gPKZZsmEOvAuNU1AcumicmuHooTfu64q5";
66+
$sk = "xxxx";
67+
$mac = new QiniuRtc\Mac($ak, $sk);
68+
$client = new QiniuRtc\AppClient($mac);
69+
$resp=$client->appToken("deq02uhb6", "lfx", '1111', (time()+3600), 'user');
70+
print_r($resp);
71+
```

docs/rtc/example.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
require_once("../../autoload.php");
3+
4+
use \Qiniu\Auth;
5+
6+
$ak = 'gwd_gV4gPKZZsmEOvAuNU1AcumicmuHooTfu64q5';
7+
$sk = 'xxxx';
8+
9+
$auth = new Auth($ak, $sk);
10+
$client = new Qiniu\Rtc\AppClient($auth);
11+
$hub = 'lfxlive';
12+
$title = 'lfxl';
13+
try {
14+
//创建app
15+
$resp = $client->createApp($hub, $title, $maxUsers);
16+
print_r($resp);
17+
// 获取app状态
18+
$resp = $client->getApp('dgdl5ge8y');
19+
print_r($resp);
20+
//修改app状态
21+
$mergePublishRtmp = null;
22+
$mergePublishRtmp['enable'] = true;
23+
$resp = $client->updateApp('dgdl5ge8y', $hub, $title, $maxUsers, $mergePublishRtmp);
24+
print_r($resp);
25+
//删除app
26+
$resp = $client->deleteApp('dgdl5ge8y');
27+
print_r($resp);
28+
//获取房间连麦的成员
29+
$resp=$client->listUser("dgbfvvzid", 'lfxl');
30+
print_r($resp);
31+
//剔除房间的连麦成员
32+
$resp=$client->kickUser("dgbfvvzid", 'lfx', "qiniu-f6e07b78-4dc8-45fb-a701-a9e158abb8e6");
33+
print_r($resp);
34+
// 列举房间
35+
$resp=$client->listActiveRooms("dgbfvvzid", 'lfx', null, null);
36+
print_r($resp);
37+
//鉴权的有效时间: 1个小时.
38+
$resp = $client->appToken("dgd4vecde", "lfxl", '1111', (time()+3600), 'user');
39+
print_r($resp);
40+
} catch (\Exception $e) {
41+
echo "Error:", $e, "\n";
42+
}

examples/rs_change_status.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
require_once __DIR__ . '/../autoload.php';
3+
4+
use \Qiniu\Auth;
5+
6+
$accessKey = getenv('QINIU_ACCESS_KEY');
7+
$secretKey = getenv('QINIU_SECRET_KEY');
8+
$bucket = getenv('QINIU_TEST_BUCKET');
9+
10+
$key = "qiniu.jpg";
11+
$auth = new Auth($accessKey, $secretKey);
12+
$config = new \Qiniu\Config();
13+
$bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
14+
15+
$status = 1;//0表示启用;1表示禁用
16+
17+
$err = $bucketManager->changeStatus($bucket, $key, $status);
18+
if ($err) {
19+
print_r($err);
20+
}

src/Qiniu/Config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ final class Config
1111
const API_HOST = 'api.qiniu.com';
1212
const RS_HOST = 'rs.qiniu.com'; //RS Host
1313
const UC_HOST = 'https://api.qiniu.com'; //UC Host
14+
const RTCAPI_HOST = 'http://rtc.qiniuapi.com';
15+
const RTCAPI_VERSION = 'v3';
1416

1517
// Zone 空间对应的机房
1618
public $zone;

src/Qiniu/Http/Client.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ public static function get($url, array $headers = array())
1313
return self::sendRequest($request);
1414
}
1515

16+
public static function delete($url, array $headers = array())
17+
{
18+
$request = new Request('DELETE', $url, $headers);
19+
return self::sendRequest($request);
20+
}
21+
1622
public static function post($url, $body, array $headers = array())
1723
{
1824
$request = new Request('POST', $url, $headers, $body);

src/Qiniu/Rtc/AppClient.php

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<?php
2+
namespace Qiniu\Rtc;
3+
4+
use Qiniu\Http\Client;
5+
use Qiniu\Http\Error;
6+
use Qiniu\Config;
7+
use Qiniu\Auth;
8+
9+
class AppClient
10+
{
11+
private $auth;
12+
private $baseURL;
13+
14+
public function __construct(Auth $auth)
15+
{
16+
$this->auth = $auth;
17+
18+
$this->baseURL = sprintf("%s/%s/apps", Config::RTCAPI_HOST, Config::RTCAPI_VERSION);
19+
}
20+
21+
/*
22+
* hub: 直播空间名
23+
* title: app 的名称 注意,Title 不是唯一标识,重复 create 动作将生成多个 app
24+
* maxUsers:人数限制
25+
* NoAutoKickUser: bool 类型,可选,禁止自动踢人(抢流)。默认为 false ,
26+
即同一个身份的 client (app/room/user) ,新的连麦请求可以成功,旧连接被关闭。
27+
*/
28+
public function createApp($hub, $title, $maxUsers = null, $noAutoKickUser = null)
29+
{
30+
$params['hub'] = $hub;
31+
$params['title'] = $title;
32+
if (!empty($maxUsers)) {
33+
$params['maxUsers'] = $maxUsers;
34+
}
35+
if (!empty($noAutoKickUser)) {
36+
$params['noAutoKickUser'] = $noAutoKickUser;
37+
}
38+
$body = json_encode($params);
39+
$ret = $this->post($this->baseURL, $body);
40+
return $ret;
41+
}
42+
43+
/*
44+
* appId: app 的唯一标识,创建的时候由系统生成。
45+
* Title: app 的名称, 可选。
46+
* Hub: 绑定的直播 hub,可选,用于合流后 rtmp 推流。
47+
* MaxUsers: int 类型,可选,连麦房间支持的最大在线人数。
48+
* NoAutoKickUser: bool 类型,可选,禁止自动踢人。
49+
* MergePublishRtmp: 连麦合流转推 RTMP 的配置,可选择。其详细配置包括如下
50+
Enable: 布尔类型,用于开启和关闭所有房间的合流功能。
51+
AudioOnly: 布尔类型,可选,指定是否只合成音频。
52+
Height, Width: int64,可选,指定合流输出的高和宽,默认为 640 x 480。
53+
OutputFps: int64,可选,指定合流输出的帧率,默认为 25 fps 。
54+
OutputKbps: int64,可选,指定合流输出的码率,默认为 1000 。
55+
URL: 合流后转推旁路直播的地址,可选,支持魔法变量配置按照连麦房间号生成不同的推流地址。如果是转推到七牛直播云,不建议使用该配置。
56+
StreamTitle: 转推七牛直播云的流名,可选,支持魔法变量配置按照连麦房间号生成不同的流名。例如,配置 Hub 为 qn-zhibo ,配置 StreamTitle 为 $(roomName) ,则房间 meeting-001 的合流将会被转推到 rtmp://pili-publish.qn-zhibo.***.com/qn-zhibo/meeting-001地址。详细配置细则,请咨询七牛技术支持。
57+
*/
58+
public function updateApp($appId, $hub, $title, $maxUsers = null, $mergePublishRtmp = null, $noAutoKickUser = null)
59+
{
60+
$url = $this->baseURL . '/' . $appId;
61+
$params['hub'] = $hub;
62+
$params['title'] = $title;
63+
if (!empty($maxUsers)) {
64+
$params['maxUsers'] = $maxUsers;
65+
}
66+
if (!empty($noAutoKickUser)) {
67+
$params['noAutoKickUser'] = $noAutoKickUser;
68+
}
69+
if (!empty($mergePublishRtmp)) {
70+
$params['mergePublishRtmp'] = $mergePublishRtmp;
71+
}
72+
$body = json_encode($params);
73+
$ret = $this->post($url, $body);
74+
return $ret;
75+
}
76+
77+
/*
78+
* appId: app 的唯一标识,创建的时候由系统生成。
79+
*/
80+
public function getApp($appId)
81+
{
82+
$url = $this->baseURL . '/' . $appId;
83+
$ret = $this->get($url);
84+
return $ret;
85+
}
86+
87+
/*
88+
* appId: app 的唯一标识,创建的时候由系统生成
89+
*/
90+
public function deleteApp($appId)
91+
{
92+
$url = $this->baseURL . '/' . $appId;
93+
list(, $err) = $this->delete($url);
94+
return $err;
95+
}
96+
97+
/*
98+
* 获取房间的人数
99+
* appId: app 的唯一标识,创建的时候由系统生成。
100+
* roomName: 操作所查询的连麦房间。
101+
*/
102+
public function listUser($appId, $roomName)
103+
{
104+
$url = sprintf("%s/%s/rooms/%s/users", $this->baseURL, $appId, $roomName);
105+
$ret = $this->get($url);
106+
return $ret;
107+
}
108+
109+
/*
110+
* 踢出玩家
111+
* appId: app 的唯一标识,创建的时候由系统生成。
112+
* roomName: 连麦房间
113+
* userId: 请求加入房间的用户ID
114+
*/
115+
public function kickUser($appId, $roomName, $userId)
116+
{
117+
$url = sprintf("%s/%s/rooms/%s/users/%s", $this->baseURL, $appId, $roomName, $userId);
118+
list(, $err) = $this->delete($url);
119+
return $err;
120+
}
121+
122+
/*
123+
* 获取房间的人数
124+
* appId: app 的唯一标识,创建的时候由系统生成。
125+
* prefix: 所查询房间名的前缀索引,可以为空。
126+
* offset: int 类型,分页查询的位移标记。
127+
* limit: int 类型,此次查询的最大长度。
128+
* GET /v3/apps/<AppID>/rooms?prefix=<RoomNamePrefix>&offset=<Offset>&limit=<Limit>
129+
*/
130+
public function listActiveRooms($appId, $prefix = null, $offset = null, $limit = null)
131+
{
132+
if (isset($prefix)) {
133+
$query['prefix'] = $prefix;
134+
}
135+
if (isset($offset)) {
136+
$query['offset'] = $offset;
137+
}
138+
if (isset($limit)) {
139+
$query['limit'] = $limit;
140+
}
141+
if ($query != null) {
142+
$query = '?' . http_build_query($query);
143+
$url = sprintf("%s/%s/rooms%s", $this->baseURL, $appId, $query);
144+
} else {
145+
$url = sprintf("%s/%s/rooms", $this->baseURL, $appId);
146+
}
147+
$ret = $this->get($url);
148+
return $ret;
149+
}
150+
151+
/*
152+
* appId: app 的唯一标识,创建的时候由系统生成。
153+
* roomName: 房间名称,需满足规格 ^[a-zA-Z0-9_-]{3,64}$
154+
* userId: 请求加入房间的用户 ID,需满足规格 ^[a-zA-Z0-9_-]{3,50}$
155+
* expireAt: int64 类型,鉴权的有效时间,传入以秒为单位的64位Unix
156+
绝对时间,token 将在该时间后失效。
157+
* permission: 该用户的房间管理权限,"admin" 或 "user",默认为 "user" 。
158+
当权限角色为 "admin" 时,拥有将其他用户移除出房间等特权.
159+
*/
160+
public function appToken($appId, $roomName, $userId, $expireAt, $permission)
161+
{
162+
$params['appId'] = $appId;
163+
$params['userId'] = $userId;
164+
$params['roomName'] = $roomName;
165+
$params['permission'] = $permission;
166+
$params['expireAt'] = $expireAt;
167+
$appAccessString = json_encode($params);
168+
return $this->auth->signWithData($appAccessString);
169+
}
170+
171+
private function get($url, $cType = null)
172+
{
173+
$rtcToken = $this->auth->authorizationV2($url, "GET", $body, $cType);
174+
$rtcToken['Content-Type'] = $cType;
175+
$ret = Client::get($url, $rtcToken);
176+
if (!$ret->ok()) {
177+
return array(null, new Error($url, $ret));
178+
}
179+
return array($ret->json(), null);
180+
}
181+
182+
private function delete($url, $cType = 'application/json')
183+
{
184+
$rtcToken = $this->auth->authorizationV2($url, "DELETE", $body, $cType);
185+
$rtcToken['Content-Type'] = $cType;
186+
$ret = Client::delete($url, $rtcToken);
187+
if (!$ret->ok()) {
188+
return array(null, new Error($url, $ret));
189+
}
190+
return array($ret->json(), null);
191+
}
192+
193+
private function post($url, $body, $cType = 'application/json')
194+
{
195+
$rtcToken = $this->auth->authorizationV2($url, "POST", $body, $cType);
196+
$rtcToken['Content-Type'] = $cType;
197+
$ret = Client::post($url, $body, $rtcToken);
198+
if (!$ret->ok()) {
199+
return array(null, new Error($url, $ret));
200+
}
201+
$r = ($ret->body === null) ? array() : $ret->json();
202+
return array($r, null);
203+
}
204+
}

src/Qiniu/Storage/BucketManager.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,23 @@ public function changeType($bucket, $key, $fileType)
225225
return $error;
226226
}
227227

228+
/**
229+
* 修改文件的存储状态,即禁用状态和启用状态间的的互相转换
230+
*
231+
* @param $bucket 待操作资源所在空间
232+
* @param $key 待操作资源文件名
233+
* @param $status 待操作文件目标文件类型
234+
*
235+
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
236+
* @link https://developer.qiniu.com/kodo/api/4173/modify-the-file-status
237+
*/
238+
public function changeStatus($bucket, $key, $status)
239+
{
240+
$resource = \Qiniu\entry($bucket, $key);
241+
$path = '/chstatus/' . $resource . '/status/' . $status;
242+
list(, $error) = $this->rsPost($path);
243+
return $error;
244+
}
228245

229246
/**
230247
* 从指定URL抓取资源,并将该资源存储到指定空间中

0 commit comments

Comments
 (0)