AppClient.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace Qiniu\Rtc;
  3. use Qiniu\Auth;
  4. use Qiniu\Config;
  5. use Qiniu\Http\Error;
  6. use Qiniu\Http\Client;
  7. use Qiniu\Http\Proxy;
  8. class AppClient
  9. {
  10. private $auth;
  11. private $baseURL;
  12. private $proxy;
  13. public function __construct(Auth $auth, $proxy = null, $proxy_auth = null, $proxy_user_password = null)
  14. {
  15. $this->auth = $auth;
  16. $this->baseURL = sprintf("%s/%s/apps", Config::RTCAPI_HOST, Config::RTCAPI_VERSION);
  17. $this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password);
  18. }
  19. /**
  20. * 创建应用
  21. *
  22. * @param string $hub 绑定的直播 hub
  23. * @param string $title app 的名称 注意,Title 不是唯一标识,重复 create 动作将生成多个 app
  24. * @param int $maxUsers 连麦房间支持的最大在线人数
  25. * @param bool $noAutoKickUser 禁止自动踢人(抢流),默认为 false
  26. * @return array
  27. * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_1
  28. */
  29. public function createApp($hub, $title, $maxUsers = null, $noAutoKickUser = null)
  30. {
  31. $params = array();
  32. $params['hub'] = $hub;
  33. $params['title'] = $title;
  34. if (!empty($maxUsers)) {
  35. $params['maxUsers'] = $maxUsers;
  36. }
  37. if ($noAutoKickUser !== null) {
  38. $params['noAutoKickUser'] = $noAutoKickUser;
  39. }
  40. $body = json_encode($params);
  41. return $this->post($this->baseURL, $body);
  42. }
  43. /**
  44. * 更新一个应用的配置信息
  45. *
  46. * @param string $appId app 的唯一标识,创建的时候由系统生成
  47. * @param string $hub app 的名称,可选
  48. * @param string $title 绑定的直播 hub,可选,用于合流后 rtmp 推流
  49. * @param int $maxUsers 连麦房间支持的最大在线人数,可选
  50. * @param bool $noAutoKickUser 禁止自动踢人,可选
  51. * @param null $mergePublishRtmp 连麦合流转推 RTMP 的配置,可选择。其详细配置可以参考文档
  52. * @return array
  53. * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_1
  54. */
  55. public function updateApp($appId, $hub, $title, $maxUsers = null, $noAutoKickUser = null, $mergePublishRtmp = null)
  56. {
  57. $url = $this->baseURL . '/' . $appId;
  58. $params = array();
  59. $params['hub'] = $hub;
  60. $params['title'] = $title;
  61. if (!empty($maxUsers)) {
  62. $params['maxUsers'] = $maxUsers;
  63. }
  64. if ($noAutoKickUser !== null) {
  65. $params['noAutoKickUser'] = $noAutoKickUser;
  66. }
  67. if (!empty($mergePublishRtmp)) {
  68. $params['mergePublishRtmp'] = $mergePublishRtmp;
  69. }
  70. $body = json_encode($params);
  71. return $this->post($url, $body);
  72. }
  73. /**
  74. * 获取应用信息
  75. *
  76. * @param string $appId
  77. * @return array
  78. * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_1
  79. */
  80. public function getApp($appId)
  81. {
  82. $url = $this->baseURL . '/' . $appId;
  83. return $this->get($url);
  84. }
  85. /**
  86. * 删除应用
  87. *
  88. * @param string $appId app 的唯一标识,创建的时候由系统生成
  89. * @return array
  90. * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_1
  91. */
  92. public function deleteApp($appId)
  93. {
  94. $url = $this->baseURL . '/' . $appId;
  95. return $this->delete($url);
  96. }
  97. /**
  98. * 获取房间内用户列表
  99. *
  100. * @param string $appId app 的唯一标识,创建的时候由系统生成
  101. * @param string $roomName 操作所查询的连麦房间
  102. * @return array
  103. * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_2
  104. */
  105. public function listUser($appId, $roomName)
  106. {
  107. $url = sprintf("%s/%s/rooms/%s/users", $this->baseURL, $appId, $roomName);
  108. return $this->get($url);
  109. }
  110. /**
  111. * 指定一个用户踢出房间
  112. *
  113. * @param string $appId app 的唯一标识,创建的时候由系统生成
  114. * @param string $roomName 连麦房间
  115. * @param string $userId 操作所剔除的用户
  116. * @return mixed
  117. * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_2
  118. */
  119. public function kickUser($appId, $roomName, $userId)
  120. {
  121. $url = sprintf("%s/%s/rooms/%s/users/%s", $this->baseURL, $appId, $roomName, $userId);
  122. return $this->delete($url);
  123. }
  124. /**
  125. * 停止一个房间的合流转推
  126. *
  127. * @param string $appId
  128. * @param string $roomName
  129. * @return array
  130. * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_2
  131. */
  132. public function stopMerge($appId, $roomName)
  133. {
  134. $url = sprintf("%s/%s/rooms/%s/merge", $this->baseURL, $appId, $roomName);
  135. return $this->delete($url);
  136. }
  137. /**
  138. * 获取应用中活跃房间
  139. *
  140. * @param string $appId 连麦房间所属的 app
  141. * @param null $prefix 所查询房间名的前缀索引,可以为空。
  142. * @param int $offset 分页查询的位移标记
  143. * @param int $limit 此次查询的最大长度
  144. * @return array
  145. * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_2
  146. */
  147. public function listActiveRooms($appId, $prefix = null, $offset = null, $limit = null)
  148. {
  149. $query = array();
  150. if (isset($prefix)) {
  151. $query['prefix'] = $prefix;
  152. }
  153. if (isset($offset)) {
  154. $query['offset'] = $offset;
  155. }
  156. if (isset($limit)) {
  157. $query['limit'] = $limit;
  158. }
  159. if (isset($query) && !empty($query)) {
  160. $query = '?' . http_build_query($query);
  161. $url = sprintf("%s/%s/rooms%s", $this->baseURL, $appId, $query);
  162. } else {
  163. $url = sprintf("%s/%s/rooms", $this->baseURL, $appId);
  164. }
  165. return $this->get($url);
  166. }
  167. /**
  168. * 生成加入房间的令牌
  169. *
  170. * @param string $appId app 的唯一标识,创建的时候由系统生成
  171. * @param string $roomName 房间名称,需满足规格 ^[a-zA-Z0-9_-]{3,64}$
  172. * @param string $userId 请求加入房间的用户 ID,需满足规格 ^[a-zA-Z0-9_-]{3,50}$
  173. * @param int $expireAt 鉴权的有效时间,传入以秒为单位的64位 Unix 绝对时间
  174. * @param string $permission 该用户的房间管理权限,"admin" 或 "user",默认为 "user"
  175. * @return string
  176. * @link https://doc.qnsdk.com/rtn/docs/server_overview#1
  177. */
  178. public function appToken($appId, $roomName, $userId, $expireAt, $permission)
  179. {
  180. $params = array();
  181. $params['appId'] = $appId;
  182. $params['userId'] = $userId;
  183. $params['roomName'] = $roomName;
  184. $params['permission'] = $permission;
  185. $params['expireAt'] = $expireAt;
  186. $appAccessString = json_encode($params);
  187. return $this->auth->signWithData($appAccessString);
  188. }
  189. private function get($url, $cType = null)
  190. {
  191. $rtcToken = $this->auth->authorizationV2($url, "GET", null, $cType);
  192. $rtcToken['Content-Type'] = $cType;
  193. $ret = Client::get($url, $rtcToken, $this->proxy->makeReqOpt());
  194. if (!$ret->ok()) {
  195. return array(null, new Error($url, $ret));
  196. }
  197. return array($ret->json(), null);
  198. }
  199. private function delete($url, $contentType = 'application/json')
  200. {
  201. $rtcToken = $this->auth->authorizationV2($url, "DELETE", null, $contentType);
  202. $rtcToken['Content-Type'] = $contentType;
  203. $ret = Client::delete($url, $rtcToken, $this->proxy->makeReqOpt());
  204. if (!$ret->ok()) {
  205. return array(null, new Error($url, $ret));
  206. }
  207. return array($ret->json(), null);
  208. }
  209. private function post($url, $body, $contentType = 'application/json')
  210. {
  211. $rtcToken = $this->auth->authorizationV2($url, "POST", $body, $contentType);
  212. $rtcToken['Content-Type'] = $contentType;
  213. $ret = Client::post($url, $body, $rtcToken, $this->proxy->makeReqOpt());
  214. if (!$ret->ok()) {
  215. return array(null, new Error($url, $ret));
  216. }
  217. $r = ($ret->body === null) ? array() : $ret->json();
  218. return array($r, null);
  219. }
  220. }