1
|
|
|
<?php |
2
|
|
|
namespace Qiniu\Rtc; |
3
|
|
|
|
4
|
|
|
use Qiniu\Zone; |
5
|
|
|
use Qiniu\Http\Client; |
6
|
|
|
use Qiniu\Http\Error; |
7
|
|
|
use Qiniu\Config; |
8
|
|
|
use Qiniu\Auth; |
9
|
|
|
|
10
|
|
|
class AppClient |
11
|
|
|
{ |
12
|
|
|
private $auth; |
13
|
|
|
private $_baseURL; |
14
|
|
|
|
15
|
|
|
public function __construct(Auth $auth) |
16
|
|
|
{ |
17
|
|
|
$this->auth = $auth; |
18
|
|
|
|
19
|
|
|
$this->_baseURL = sprintf("%s/%s/apps", Config::RTCAPI_HOST, Config::RTCAPI_VERSION); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/* |
23
|
|
|
* hub: 直播空间名 |
24
|
|
|
* title: app 的名称 注意,Title 不是唯一标识,重复 create 动作将生成多个 app |
25
|
|
|
* maxUsers:人数限制 |
26
|
|
|
* NoAutoKickUser: bool 类型,可选,禁止自动踢人(抢流)。默认为 false ,即同一个身份的 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
|
|
|
list($ret, $err) = $this->post($this->_baseURL, $body); |
40
|
|
|
if ($err !== null) { |
41
|
|
|
return $err; |
42
|
|
|
} else { |
43
|
|
|
return $ret; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/* |
48
|
|
|
* appId: app 的唯一标识,创建的时候由系统生成。 |
49
|
|
|
* Title: app 的名称, 可选。 |
50
|
|
|
* Hub: 绑定的直播 hub,可选,用于合流后 rtmp 推流。 |
51
|
|
|
* MaxUsers: int 类型,可选,连麦房间支持的最大在线人数。 |
52
|
|
|
* NoAutoKickUser: bool 类型,可选,禁止自动踢人。 |
53
|
|
|
* MergePublishRtmp: 连麦合流转推 RTMP 的配置,可选择。其详细配置包括如下 |
54
|
|
|
Enable: 布尔类型,用于开启和关闭所有房间的合流功能。 |
55
|
|
|
AudioOnly: 布尔类型,可选,指定是否只合成音频。 |
56
|
|
|
Height, Width: int64,可选,指定合流输出的高和宽,默认为 640 x 480。 |
57
|
|
|
OutputFps: int64,可选,指定合流输出的帧率,默认为 25 fps 。 |
58
|
|
|
OutputKbps: int64,可选,指定合流输出的码率,默认为 1000 。 |
59
|
|
|
URL: 合流后转推旁路直播的地址,可选,支持魔法变量配置按照连麦房间号生成不同的推流地址。如果是转推到七牛直播云,不建议使用该配置。 |
60
|
|
|
StreamTitle: 转推七牛直播云的流名,可选,支持魔法变量配置按照连麦房间号生成不同的流名。例如,配置 Hub 为 qn-zhibo ,配置 StreamTitle 为 $(roomName) ,则房间 meeting-001 的合流将会被转推到 rtmp://pili-publish.qn-zhibo.***.com/qn-zhibo/meeting-001地址。详细配置细则,请咨询七牛技术支持。 |
61
|
|
|
*/ |
62
|
|
|
public function updateApp($appId, $hub, $title, $maxUsers = null, $mergePublishRtmp = null, $noAutoKickUser = null) |
63
|
|
|
{ |
64
|
|
|
$url = $this->_baseURL . '/' . $appId; |
65
|
|
|
$params['hub'] = $hub; |
|
|
|
|
66
|
|
|
$params['title'] = $title; |
67
|
|
|
if (!empty($maxUsers)) { |
68
|
|
|
$params['maxUsers'] = $maxUsers; |
69
|
|
|
} |
70
|
|
|
if (!empty($noAutoKickUser)) { |
71
|
|
|
$params['noAutoKickUser'] = $noAutoKickUser; |
72
|
|
|
} |
73
|
|
|
if (!empty($mergePublishRtmp)) { |
74
|
|
|
$params['mergePublishRtmp'] = $mergePublishRtmp; |
75
|
|
|
} |
76
|
|
|
$body = json_encode($params); |
77
|
|
|
list($ret, $err) = $this->post($url, $body); |
78
|
|
|
if ($err !== null) { |
79
|
|
|
return $err; |
80
|
|
|
} else { |
81
|
|
|
return $ret; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/* |
86
|
|
|
* appId: app 的唯一标识,创建的时候由系统生成。 |
87
|
|
|
*/ |
88
|
|
|
public function getApp($appId) |
89
|
|
|
{ |
90
|
|
|
$url = $this->_baseURL . '/' . $appId; |
91
|
|
|
list($ret, $err) = $this->get($url); |
92
|
|
|
if ($err !== null) { |
93
|
|
|
return $err; |
94
|
|
|
} else { |
95
|
|
|
return $ret; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/* |
100
|
|
|
* appId: app 的唯一标识,创建的时候由系统生成 |
101
|
|
|
*/ |
102
|
|
|
public function deleteApp($appId) |
103
|
|
|
{ |
104
|
|
|
$url = $this->_baseURL . '/' . $appId; |
105
|
|
|
list(, $err) = $this->delete($url); |
106
|
|
|
return $err; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/* |
110
|
|
|
* 获取房间的人数 |
111
|
|
|
* appId: app 的唯一标识,创建的时候由系统生成。 |
112
|
|
|
* roomName: 操作所查询的连麦房间。 |
113
|
|
|
*/ |
114
|
|
|
public function getappUserNum($appId, $roomName) |
115
|
|
|
{ |
116
|
|
|
$url = sprintf("%s/%s/rooms/%s/users", $this->_baseURL, $appId, $roomName); |
117
|
|
|
list($ret, $err) = $this->get($url); |
118
|
|
|
if ($err !== null) { |
119
|
|
|
return $err; |
120
|
|
|
} else { |
121
|
|
|
return $ret; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/* |
126
|
|
|
* 踢出玩家 |
127
|
|
|
* appId: app 的唯一标识,创建的时候由系统生成。 |
128
|
|
|
* roomName: 连麦房间 |
129
|
|
|
* userId: 请求加入房间的用户ID |
130
|
|
|
*/ |
131
|
|
|
public function kickingPlayer($appId, $roomName, $userId) |
132
|
|
|
{ |
133
|
|
|
$url = sprintf("%s/%s/rooms/%s/users/%s", $this->_baseURL, $appId, $roomName, $userId); |
134
|
|
|
list(, $err) = $this->delete($url); |
135
|
|
|
return $err; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/* |
139
|
|
|
* 获取房间的人数 |
140
|
|
|
* appId: app 的唯一标识,创建的时候由系统生成。 |
141
|
|
|
* prefix: 所查询房间名的前缀索引,可以为空。 |
142
|
|
|
* offset: int 类型,分页查询的位移标记。 |
143
|
|
|
* limit: int 类型,此次查询的最大长度。 |
144
|
|
|
* GET /v3/apps/<AppID>/rooms?prefix=<RoomNamePrefix>&offset=<Offset>&limit=<Limit> |
145
|
|
|
*/ |
146
|
|
|
public function listRooms($appId, $prefix = null, $offset = null, $limit = null) |
147
|
|
|
{ |
148
|
|
|
if(isset($prefix)){ |
149
|
|
|
$query['prefix'] = $prefix; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
if(isset($offset)){ |
152
|
|
|
$query['offset'] = $offset; |
|
|
|
|
153
|
|
|
} |
154
|
|
|
if(isset($limit)){ |
155
|
|
|
$query['limit'] = $limit; |
156
|
|
|
} |
157
|
|
|
if ($query != null) { |
158
|
|
|
$query = '?' . http_build_query($query); |
159
|
|
|
$url = sprintf("%s/%s/rooms%s", $this->_baseURL, $appId, $query); |
160
|
|
|
} else { |
161
|
|
|
$url = sprintf("%s/%s/rooms", $this->_baseURL, $appId); |
162
|
|
|
} |
163
|
|
|
list($ret, $err) = $this->get($url); |
164
|
|
|
if ($err !== null) { |
165
|
|
|
return $err; |
166
|
|
|
} else { |
167
|
|
|
return $ret; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/* |
172
|
|
|
* appId: app 的唯一标识,创建的时候由系统生成。 |
173
|
|
|
* roomName: 房间名称,需满足规格 ^[a-zA-Z0-9_-]{3,64}$ |
174
|
|
|
* userId: 请求加入房间的用户 ID,需满足规格 ^[a-zA-Z0-9_-]{3,50}$ |
175
|
|
|
* expireAt: int64 类型,鉴权的有效时间,传入以秒为单位的64位Unix绝对时间,token 将在该时间后失效。 |
176
|
|
|
* permission: 该用户的房间管理权限,"admin" 或 "user",默认为 "user" 。当权限角色为 "admin" 时,拥有将其他用户移除出房间等特权. |
177
|
|
|
*/ |
178
|
|
|
public function appToken($appId, $roomName, $userId, $expireAt, $permission) |
179
|
|
|
{ |
180
|
|
|
$params['appId'] = $appId; |
|
|
|
|
181
|
|
|
$params['userId'] = $userId; |
182
|
|
|
$params['roomName'] = $roomName; |
183
|
|
|
$params['permission'] = $permission; |
184
|
|
|
$params['expireAt'] = $expireAt; |
185
|
|
|
$appAccessString = json_encode($params); |
186
|
|
|
$encodedappAccess = \Qiniu\base64_urlSafeEncode($appAccessString); |
187
|
|
|
$sign = hash_hmac('sha1', $encodedappAccess, $this->auth->secretKey, true); |
188
|
|
|
$encodedSign = \Qiniu\base64_urlSafeEncode($sign); |
189
|
|
|
return $this->auth->accessKey . ":" . $encodedSign . ":" . $encodedappAccess; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
private function get($url) |
193
|
|
|
{ |
194
|
|
|
$cType = null; |
195
|
|
|
$rtcToken = $this->auth->authorizationV2($url, "GET", $body, $cType); |
|
|
|
|
196
|
|
|
$rtcToken['Content-Type'] = $cType; |
197
|
|
|
$ret = Client::get($url, $rtcToken); |
198
|
|
|
if (!$ret->ok()) { |
199
|
|
|
return array(null, new Error($url, $ret)); |
200
|
|
|
} |
201
|
|
|
return array($ret->json(), null); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
private function delete($url) |
205
|
|
|
{ |
206
|
|
|
$cType = 'application/json'; |
207
|
|
|
$rtcToken = $this->auth->authorizationV2($url, "DELETE", $body, $cType); |
|
|
|
|
208
|
|
|
$rtcToken['Content-Type'] = $cType; |
209
|
|
|
$ret = Client::delete($url, $rtcToken); |
210
|
|
|
if (!$ret->ok()) { |
211
|
|
|
return array(null, new Error($url, $ret)); |
212
|
|
|
} |
213
|
|
|
return array($ret->json(), null); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
private function post($url, $body) |
217
|
|
|
{ |
218
|
|
|
$cType = 'application/json'; |
219
|
|
|
$rtcToken = $this->auth->authorizationV2($url, "POST", $body, $cType); |
220
|
|
|
$rtcToken['Content-Type'] = $cType; |
221
|
|
|
$ret = Client::post($url, $body, $rtcToken); |
222
|
|
|
if (!$ret->ok()) { |
223
|
|
|
return array(null, new Error($url, $ret)); |
224
|
|
|
} |
225
|
|
|
$r = ($ret->body === null) ? array() : $ret->json(); |
226
|
|
|
return array($r, null); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.