|
| 1 | +package com.qiniu.caster; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.GsonBuilder; |
| 5 | +import com.google.gson.JsonElement; |
| 6 | +import com.qiniu.caster.model.CasterParams; |
| 7 | +import com.qiniu.caster.model.Overlay; |
| 8 | +import com.qiniu.caster.model.Text; |
| 9 | +import com.qiniu.common.Constants; |
| 10 | +import com.qiniu.common.QiniuException; |
| 11 | +import com.qiniu.http.Client; |
| 12 | +import com.qiniu.http.Response; |
| 13 | +import com.qiniu.util.Auth; |
| 14 | +import com.qiniu.util.Json; |
| 15 | +import com.qiniu.util.StringMap; |
| 16 | + |
| 17 | + |
| 18 | +public class CasterManager { |
| 19 | + private final Auth auth; |
| 20 | + private final String server; |
| 21 | + private final Client client; |
| 22 | + |
| 23 | + public CasterManager(Auth auth) { |
| 24 | + this(auth, "http://pili-caster.qiniuapi.com"); |
| 25 | + } |
| 26 | + |
| 27 | + private CasterManager(Auth auth, String server) { |
| 28 | + this.auth = auth; |
| 29 | + this.server = server; |
| 30 | + this.client = new Client(); |
| 31 | + } |
| 32 | + |
| 33 | + public CasterManager(Auth auth, String server, Client client) { |
| 34 | + this.auth = auth; |
| 35 | + this.server = server; |
| 36 | + this.client = client; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * |
| 41 | + * 创建云导播 |
| 42 | + * @param name 导播台name |
| 43 | + * @param casterParams 导播台参数 |
| 44 | + * @return |
| 45 | + */ |
| 46 | + public Response createCaster(String name, CasterParams casterParams) { |
| 47 | + String url = server + "/v1/casters"; |
| 48 | + Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| 49 | + JsonElement jsonElement = gson.toJsonTree(casterParams); |
| 50 | + jsonElement.getAsJsonObject().addProperty("name", name); |
| 51 | + byte[] body = gson.toJson(jsonElement).getBytes(Constants.UTF_8); |
| 52 | + String a = gson.toJson(jsonElement); |
| 53 | + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); |
| 54 | + Response response = null; |
| 55 | + try { |
| 56 | + response = client.post(url, body, headers, Client.JsonMime); |
| 57 | + } catch (QiniuException e) { |
| 58 | + e.printStackTrace(); |
| 59 | + } |
| 60 | + return response; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * |
| 65 | + * 云导播列表 |
| 66 | + * @param name 云导播名称前缀匹配 |
| 67 | + * @param marker 如果上一次返回的结果超过了单次查询限制,则会返回marker表示上一次读取到哪条记录;这一次请求带上marker后,继续从该marker后开始读取,设置为null为从头开始 |
| 68 | + * @param limit 返回的最大数量,设置为-1默认为50 |
| 69 | + * @return |
| 70 | + */ |
| 71 | + public Response findCasters(String name, String marker, int limit) { |
| 72 | + StringMap map = new StringMap().putNotEmpty("marker", marker) |
| 73 | + .putNotEmpty("name", name).putWhen("limit", limit, limit > 0); |
| 74 | + String queryString = map.formString(); |
| 75 | + if (map.size() > 0) { |
| 76 | + queryString = "?" + queryString; |
| 77 | + } |
| 78 | + String url = String.format("%s/v1/casters/%s", server, queryString); |
| 79 | + StringMap headers = auth.authorizationV2(url); |
| 80 | + Response response = null; |
| 81 | + try { |
| 82 | + response = client.get(url, headers); |
| 83 | + } catch (QiniuException e) { |
| 84 | + e.printStackTrace(); |
| 85 | + } |
| 86 | + return response; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * |
| 91 | + * 云导播信息 |
| 92 | + * @param casterId 导播台id |
| 93 | + * @return |
| 94 | + */ |
| 95 | + public Response getCasterInfo(String casterId) { |
| 96 | + String url = String.format("%s/v1/casters/%s", server, casterId); |
| 97 | + StringMap headers = auth.authorizationV2(url); |
| 98 | + Response response = null; |
| 99 | + try { |
| 100 | + response = client.get(url, headers); |
| 101 | + } catch (QiniuException e) { |
| 102 | + e.printStackTrace(); |
| 103 | + } |
| 104 | + return response; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * |
| 109 | + * 开启云导播 |
| 110 | + * @param casterId 导播台id |
| 111 | + * @param start 开启时间,设置为-1立即启动;导播台已开启时,禁止设置开启时间,规格为时间戳(秒级) |
| 112 | + * @param hour 开启时长,设置为-1为默认为1小时;导播台已开启时,表示延长开启时长 [1.~] |
| 113 | + * @return |
| 114 | + */ |
| 115 | + public Response startCaster(String casterId, int start, int hour) { |
| 116 | + String url = String.format("%s/v1/casters/%s/start", server, casterId); |
| 117 | + StringMap map = new StringMap().putWhen("start", start, start > 0) |
| 118 | + .putWhen("hour", hour, hour > 0); |
| 119 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 120 | + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); |
| 121 | + Response response = null; |
| 122 | + try { |
| 123 | + response = client.post(url, body, headers, Client.JsonMime); |
| 124 | + } catch (QiniuException e) { |
| 125 | + e.printStackTrace(); |
| 126 | + } |
| 127 | + return response; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * |
| 132 | + * 关闭云导播 |
| 133 | + * @param casterId 导播台id |
| 134 | + * @return |
| 135 | + */ |
| 136 | + public Response stopCaster(String casterId) { |
| 137 | + String url = String.format("%s/v1/casters/%s/stop", server, casterId); |
| 138 | + StringMap map = new StringMap().putNotNull("CasterId", casterId); |
| 139 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 140 | + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); |
| 141 | + Response response = null; |
| 142 | + try { |
| 143 | + response = client.post(url, body, headers, Client.JsonMime); |
| 144 | + } catch (QiniuException e) { |
| 145 | + e.printStackTrace(); |
| 146 | + } |
| 147 | + return response; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * |
| 152 | + * 删除云导播 |
| 153 | + * @param casterId 导播台id |
| 154 | + * @return |
| 155 | + */ |
| 156 | + public Response deleteCaster(String casterId) { |
| 157 | + String url = String.format("%s/v1/casters/%s", server, casterId); |
| 158 | + StringMap map = new StringMap().putNotNull("CasterId", casterId); |
| 159 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 160 | + StringMap headers = auth.authorizationV2(url, "DELETE", body, Client.JsonMime); |
| 161 | + Response response = null; |
| 162 | + try { |
| 163 | + response = client.delete(url, body, headers, Client.JsonMime); |
| 164 | + } catch (QiniuException e) { |
| 165 | + e.printStackTrace(); |
| 166 | + } |
| 167 | + return response; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * |
| 172 | + * 更新云导播 |
| 173 | + * @param casterId 导播台id |
| 174 | + * @param casterParams 导播台更新参数 |
| 175 | + * @return |
| 176 | + */ |
| 177 | + public Response update(String casterId, CasterParams casterParams) { |
| 178 | + String url = String.format("%s/v1/casters/%s", server, casterId); |
| 179 | + byte[] body = Json.encode(casterParams).getBytes(Constants.UTF_8); |
| 180 | + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); |
| 181 | + Response response = null; |
| 182 | + try { |
| 183 | + response = client.post(url, body, headers, Client.JsonMime); |
| 184 | + } catch (QiniuException e) { |
| 185 | + e.printStackTrace(); |
| 186 | + } |
| 187 | + return response; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * |
| 192 | + * 更新PVW监视器频道 |
| 193 | + * @param casterId 导播台id |
| 194 | + * @param channel 监视器频道 [0,7] |
| 195 | + * @param staticKey 静态密钥 |
| 196 | + * @return |
| 197 | + */ |
| 198 | + public Response updatePvw(String casterId, int channel, String staticKey) { |
| 199 | + String url = String.format("%s/v1/static/casters/%s/pvw", server, casterId); |
| 200 | + StringMap map = new StringMap().putNotNull("CasterID", casterId) |
| 201 | + .putWhen("channel", channel, channel >= 0); |
| 202 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 203 | + StringMap headers = new StringMap().put("Authorization", staticKey); |
| 204 | + Response response = null; |
| 205 | + try { |
| 206 | + response = client.post(url, body, headers, Client.JsonMime); |
| 207 | + } catch (QiniuException e) { |
| 208 | + e.printStackTrace(); |
| 209 | + } |
| 210 | + return response; |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * |
| 215 | + * 更新PGM监视器频道 |
| 216 | + * @param casterId 导播台id |
| 217 | + * @param channel 监视器频道 [0,7] |
| 218 | + * @param staticKey 静态密钥 |
| 219 | + * @return |
| 220 | + */ |
| 221 | + public Response updatePgm(String casterId, int channel, String staticKey) { |
| 222 | + String url = String.format("%s/v1/static/casters/%s/pgm", server, casterId); |
| 223 | + StringMap map = new StringMap().putNotNull("CasterID", casterId) |
| 224 | + .putWhen("channel", channel, channel >= 0); |
| 225 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 226 | + StringMap headers = new StringMap().put("Authorization", staticKey); |
| 227 | + Response response = null; |
| 228 | + try { |
| 229 | + response = client.post(url, body, headers, Client.JsonMime); |
| 230 | + } catch (QiniuException e) { |
| 231 | + e.printStackTrace(); |
| 232 | + } |
| 233 | + return response; |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * |
| 238 | + * PVW切换至PGM |
| 239 | + * @param casterId 导播台id |
| 240 | + * @param switchVol 是否同步切换音频 |
| 241 | + * @param staticKey 静态密钥 |
| 242 | + * @return |
| 243 | + */ |
| 244 | + public Response pvwSwitchToPgm(String casterId, boolean switchVol, String staticKey) { |
| 245 | + String url = String.format("%s/v1/static/casters/%s/switch", server, casterId); |
| 246 | + StringMap map = new StringMap().putNotNull("CasterID", casterId) |
| 247 | + .putNotNull("switchVol", switchVol); |
| 248 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 249 | + StringMap headers = new StringMap().put("Authorization", staticKey); |
| 250 | + Response response = null; |
| 251 | + try { |
| 252 | + response = client.post(url, body, headers, Client.JsonMime); |
| 253 | + } catch (QiniuException e) { |
| 254 | + e.printStackTrace(); |
| 255 | + } |
| 256 | + return response; |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * |
| 261 | + * 更新监视器配置 |
| 262 | + * @param casterId 导播台id |
| 263 | + * @param channel 监视器频道 [0,7] |
| 264 | + * @param newUrl 监视器源流地址,支持直播流、静态视频、图片等 |
| 265 | + * @param vol 音量[0,300] |
| 266 | + * @param muted 是否静音 |
| 267 | + * @param staticKey 静态密钥 |
| 268 | + * @return |
| 269 | + */ |
| 270 | + public Response updateMonitors(String casterId, int channel, String newUrl, int vol, boolean muted, String staticKey) { |
| 271 | + String url = String.format("%s/v1/static/casters/%s/monitors", server, casterId); |
| 272 | + StringMap map = new StringMap().putNotNull("CasterID", casterId) |
| 273 | + .putWhen("channel", channel, channel >= 0) |
| 274 | + .putNotNull("url", newUrl).putWhen("vol", vol, vol >= 0).putNotNull("muted", muted); |
| 275 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 276 | + StringMap headers = new StringMap().put("Authorization", staticKey); |
| 277 | + Response response = null; |
| 278 | + try { |
| 279 | + response = client.post(url, body, headers, Client.JsonMime); |
| 280 | + } catch (QiniuException e) { |
| 281 | + e.printStackTrace(); |
| 282 | + } |
| 283 | + return response; |
| 284 | + } |
| 285 | + |
| 286 | + /** |
| 287 | + * |
| 288 | + * 更新输出配置 |
| 289 | + * @param casterId 导播台id |
| 290 | + * @param newUrl PGM推流地址 |
| 291 | + * @param ab [64,512] 音频码率(Kbps),默认值为64,-1为默认值 |
| 292 | + * @param vb [300,10240] 视频码率(Kbps),默认值为1000,-1为默认值 |
| 293 | + * @param closed 关闭推流 |
| 294 | + * @param emergencyMode 紧急模式开关 |
| 295 | + * @param emergencyChannel 紧急模式监视器频道,取值范围是[0,7],默认使用0频道 [0,7] |
| 296 | + * @param delay [0,180] 延时播放,单位: 秒 |
| 297 | + * @param staticKey 静态密钥 |
| 298 | + * @return |
| 299 | + */ |
| 300 | + public Response updatePublish(String casterId, String newUrl, int ab, int vb, boolean closed, boolean emergencyMode, int emergencyChannel, int delay, String staticKey) { |
| 301 | + String url = String.format("%s/v1/static/casters/%s/publish", server, casterId); |
| 302 | + StringMap map = new StringMap().putNotNull("CasterID", casterId) |
| 303 | + .putNotNull("url", newUrl).putWhen("vb", vb, vb >= 0) |
| 304 | + .putWhen("ab", ab, ab >= 0) |
| 305 | + .putNotNull("closed", closed).putNotNull("emergencyMode", emergencyMode) |
| 306 | + .putWhen("emergencyChannel", emergencyChannel, emergencyChannel >= 0) |
| 307 | + .putWhen("delay", delay, delay >= 0); |
| 308 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 309 | + StringMap headers = new StringMap().put("Authorization", staticKey); |
| 310 | + Response response = null; |
| 311 | + try { |
| 312 | + response = client.post(url, body, headers, Client.JsonMime); |
| 313 | + } catch (QiniuException e) { |
| 314 | + e.printStackTrace(); |
| 315 | + } |
| 316 | + return response; |
| 317 | + } |
| 318 | + |
| 319 | + /** |
| 320 | + * |
| 321 | + * 更新PVW布局ID |
| 322 | + * @param casterId 导播台id |
| 323 | + * @param layout 布局ID,-1表示不使用布局 |
| 324 | + * @param staticKey 静态密钥 |
| 325 | + * @return |
| 326 | + */ |
| 327 | + public Response changeLayout(String casterId, int layout, String staticKey) { |
| 328 | + String url = String.format("%s/v1/static/casters/%s/pvw/layouts", server, casterId); |
| 329 | + StringMap map = new StringMap().putNotNull("CasterID", casterId) |
| 330 | + .putWhen("layout", layout, layout >= 0); |
| 331 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 332 | + StringMap headers = new StringMap().put("Authorization", staticKey); |
| 333 | + Response response = null; |
| 334 | + try { |
| 335 | + response = client.post(url, body, headers, Client.JsonMime); |
| 336 | + } catch (QiniuException e) { |
| 337 | + e.printStackTrace(); |
| 338 | + } |
| 339 | + return response; |
| 340 | + } |
| 341 | + |
| 342 | + /** |
| 343 | + * |
| 344 | + * 更新布局配置 |
| 345 | + * @param casterId 导播台ID |
| 346 | + * @param layout [0,7] 布局ID |
| 347 | + * @param title 布局标题,可留空 |
| 348 | + * @param overlay 画中画配置,key为对应的监视器频道,详细参数见画中画配置(overlay)详细参数 |
| 349 | + * @param text 文字水印配置,key为文字水印标题,详细参数见文字水印配置(text)详细参数 |
| 350 | + * @param staticKey 静态密钥 |
| 351 | + * @return |
| 352 | + */ |
| 353 | + public Response updateLayout(String casterId, int layout, String title, Overlay overlay, Text text, String staticKey) { |
| 354 | + String url = String.format("%s/v1/static/casters/%s/layouts", server, casterId); |
| 355 | + StringMap map = new StringMap().putNotNull("CasterID", casterId) |
| 356 | + .putWhen("layout", layout, layout >= 0) |
| 357 | + .putNotNull("title", title).putNotNull("overlay", overlay).putNotNull("text", text); |
| 358 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 359 | + StringMap headers = new StringMap().put("Authorization", staticKey); |
| 360 | + Response response = null; |
| 361 | + try { |
| 362 | + response = client.post(url, body, headers, Client.JsonMime); |
| 363 | + } catch (QiniuException e) { |
| 364 | + e.printStackTrace(); |
| 365 | + } |
| 366 | + return response; |
| 367 | + } |
| 368 | +} |
0 commit comments