Skip to content

Commit f0831ab

Browse files
authored
Merge pull request #267 from ankadada/master
Add Rtc
2 parents 2cf6f50 + fba3728 commit f0831ab

File tree

7 files changed

+359
-0
lines changed

7 files changed

+359
-0
lines changed

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+
}

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+
```

rtc/example/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);exit;
17+
// 获取app状态
18+
$resp = $client->getApp('dgd330nc2');
19+
print_r($resp);exit;
20+
//修改app状态
21+
$mergePublishRtmp = null;
22+
$mergePublishRtmp['enable'] = true;
23+
$resp = $client->UpdateApp('dgbrj7ghp', $hub, $title, $maxUsers, $mergePublishRtmp);
24+
print_r($resp);exit;
25+
//删除app
26+
$resp = $client->deleteApp('dgbrj7ghp');
27+
print_r($resp);exit;
28+
//获取房间连麦的成员
29+
$resp=$client->listUser("dgbfvvzid", 'lfxl');
30+
print_r($resp);exit;
31+
//剔除房间的连麦成员
32+
$resp=$client->kickUser("dgbfvvzid", 'lfx', "qiniu-f6e07b78-4dc8-45fb-a701-a9e158abb8e6");
33+
print_r($resp);exit;
34+
// 列举房间
35+
$resp=$client->listRooms("dgbfvvzid", 'lfx', null, null);
36+
print_r($resp);exit;
37+
//鉴权的有效时间: 1个小时.
38+
$resp = $client->appToken("dgd4vecde", "lfxl", '1111', (time()+3600), 'user');
39+
print_r($resp);exit;
40+
} catch (\Exception $e) {
41+
echo "Error:", $e, "\n";
42+
}

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

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)