|
1 |
| -# agora-rest-client-java |
| 1 | +# Agora REST Client for Java |
| 2 | + |
| 3 | +<p> |
| 4 | +<img alt="Maven Central Version" src="https://img.shields.io/maven-central/v/io.agora/agora-rest-client-core?colorB=brightgreen"> |
| 5 | +<img alt="GitHub License" src="https://img.shields.io/github/license/AgoraIO-Community/agora-rest-client-java"> |
| 6 | +<a href="https://github.com/AgoraIO-Community/agora-rest-client-java/actions/workflows/maven.yml"><img alt="Java CI with Maven" src="https://github.com/AgoraIO-Community/agora-rest-client-java/actions/workflows/maven.yml/badge.svg"></a> |
| 7 | +<img alt="GitHub" src="https://img.shields.io/github/v/release/AgoraIO-Community/agora-rest-client-java"> |
| 8 | +<img alt="GitHub Issues or Pull Requests" src="https://img.shields.io/github/issues-pr/AgoraIO-Community/agora-rest-client-java"> |
| 9 | +</p> |
| 10 | + |
| 11 | +`agora-rest-client-java`是用 Java 语言编写的一个开源项目,专门为 Agora REST API 设计。它包含了 Agora 官方提供的 REST |
| 12 | +API 接口的包装和内部实现,可以帮助开发者更加方便的集成服务端 Agora REST API。 |
| 13 | + |
| 14 | +> 注意: 该 SDK 经过一些测试以确保其基本功能正常运作。然而,由于软件开发的复杂性,我们无法保证它是完全没有缺陷的。 |
| 15 | +> |
| 16 | +>该SDK目前可能存在一些潜在的 BUG 或不稳定性。我们鼓励社区的开发者和用户积极参与,共同改进这个项目。 |
| 17 | +
|
| 18 | +## 特性 |
| 19 | + |
| 20 | +* 封装了 Agora REST API 的请求和响应处理,简化与 Agora REST API 的通信流程 |
| 21 | +* 当遇到 DNS 解析失败、网络错误或者请求超时等问题的时候,提供了自动切换最佳域名的能力,以保障请求 REST API 服务的可用性 |
| 22 | +* 提供了易于使用的 API,可轻松地实现调用 Agora REST API 的常见功能,如开启云录制、停止云录制等 |
| 23 | +* 基于 Java 语言,具有异步性、并发性和可扩展性 |
| 24 | + |
| 25 | +## 支持的服务 |
| 26 | + |
| 27 | +* [云端录制 Cloud Recording ](./agora-rest-client-core/src/main/java/io/agora/rest/services/cloudrecording/README.md) |
| 28 | + |
| 29 | +## 环境准备 |
| 30 | + |
| 31 | +* [Java 1.8 或以上版本](https://www.java.com) |
| 32 | +* 在声网 [Console 平台](https://console.shengwang.cn/)申请的 App ID 和 App Certificate |
| 33 | +* 在声网 [Console 平台](https://console.shengwang.cn/)的 Basic Auth 认证信息 |
| 34 | +* 在声网 [Console 平台](https://console.shengwang.cn/)开启相关的服务能力 |
| 35 | + |
| 36 | +## 安装 |
| 37 | + |
| 38 | +首先,在`poml.xml`文件中添加 REST Client 依赖: |
| 39 | + |
| 40 | +```xml |
| 41 | +<dependency> |
| 42 | + <groupId>io.agora</groupId> |
| 43 | + <artifactId>agora-rest-client-core</artifactId> |
| 44 | + <version>0.1.3-BETA</version> |
| 45 | +</dependency> |
| 46 | +``` |
| 47 | + |
| 48 | +## 使用示例 |
| 49 | + |
| 50 | +以调用云录制服务为例: |
| 51 | + |
| 52 | +```java |
| 53 | +package com.company.example; |
| 54 | + |
| 55 | +import io.agora.rest.AgoraException; |
| 56 | +import io.agora.rest.AgoraService; |
| 57 | +import io.agora.rest.core.AgoraProperty; |
| 58 | +import io.agora.rest.core.BasicAuthCredential; |
| 59 | +import io.agora.rest.core.Credential; |
| 60 | +import io.agora.rest.core.RegionArea; |
| 61 | +import io.agora.rest.services.cloudrecording.api.req.StartResourceReq; |
| 62 | +import io.agora.rest.services.cloudrecording.api.res.AcquireResourceRes; |
| 63 | +import io.agora.rest.services.cloudrecording.api.res.StartResourceRes; |
| 64 | +import io.agora.rest.services.cloudrecording.api.res.StopResourceRes; |
| 65 | +import io.agora.rest.services.cloudrecording.scenario.mix.req.AcquireMixRecordingResourceClientReq; |
| 66 | +import io.agora.rest.services.cloudrecording.scenario.mix.req.StartMixRecordingResourceClientReq; |
| 67 | +import io.agora.rest.services.cloudrecording.scenario.mix.res.QueryMixHLSAndMP4RecordingResourceRes; |
| 68 | + |
| 69 | +import java.util.Arrays; |
| 70 | +import java.util.Collections; |
| 71 | + |
| 72 | +public class Main { |
| 73 | + private static String appId = "<your appId>"; |
| 74 | + |
| 75 | + private static String cname = "<your cname>"; |
| 76 | + |
| 77 | + private static String uid = "<your uid>"; |
| 78 | + |
| 79 | + private static String username = "<the username of basic auth credential>"; |
| 80 | + |
| 81 | + private static String password = "<the password of basic auth credential>"; |
| 82 | + |
| 83 | + private static String token = "<your token>"; |
| 84 | + |
| 85 | + private static String accessKey = "<your accessKey>"; |
| 86 | + |
| 87 | + private static String secretKey = "<your secretKey>"; |
| 88 | + |
| 89 | + private static Integer region = 0; // <your region> |
| 90 | + |
| 91 | + private static String bucket = "<your bucket>"; |
| 92 | + |
| 93 | + private static Integer vendor = 0; // <your vendor> |
| 94 | + |
| 95 | + public static void main(String[] args) throws Exception { |
| 96 | + |
| 97 | + Credential basicAuthCredential = new BasicAuthCredential(username, password); |
| 98 | + |
| 99 | + // Initialize AgoraService |
| 100 | + AgoraService agoraService = new AgoraService( |
| 101 | + AgoraProperty.builder() |
| 102 | + .appId(appId) |
| 103 | + .credential(basicAuthCredential) |
| 104 | + // Specify the region where the server is located. |
| 105 | + // Optional values are CN, NA, EU, AP, and the client will automatically |
| 106 | + // switch to use the best domain name according to the configured region |
| 107 | + .regionArea(RegionArea.CNRegionArea) |
| 108 | + .build() |
| 109 | + ); |
| 110 | + |
| 111 | + |
| 112 | + AcquireResourceRes acquireResourceRes; |
| 113 | + |
| 114 | + // Acquire resource |
| 115 | + try { |
| 116 | + acquireResourceRes = agoraService.cloudRecording() |
| 117 | + .mixScenario() |
| 118 | + .acquire(cname, uid, AcquireMixRecordingResourceClientReq.builder() |
| 119 | + .build()) |
| 120 | + .block(); |
| 121 | + } catch (AgoraException e) { |
| 122 | + System.out.printf("agora error:%s", e.getMessage()); |
| 123 | + return; |
| 124 | + } catch (Exception e) { |
| 125 | + System.out.printf("unknown error:%s", e.getMessage()); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + // Check if the response is null |
| 130 | + if (acquireResourceRes == null || acquireResourceRes.getResourceId() == null) { |
| 131 | + System.out.println("failed to get resource"); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + System.out.printf("resourceId:%s", acquireResourceRes.getResourceId()); |
| 137 | + |
| 138 | + System.out.println("acquire resource success"); |
| 139 | + |
| 140 | + // Define storage config |
| 141 | + StartResourceReq.StorageConfig storageConfig = StartResourceReq.StorageConfig.builder() |
| 142 | + .accessKey(accessKey) |
| 143 | + .secretKey(secretKey) |
| 144 | + .bucket(bucket) |
| 145 | + .vendor(vendor) |
| 146 | + .region(region) |
| 147 | + .build(); |
| 148 | + |
| 149 | + // Define start resource request |
| 150 | + StartMixRecordingResourceClientReq startResourceReq = StartMixRecordingResourceClientReq.builder() |
| 151 | + .token(token) |
| 152 | + .recordingConfig(StartResourceReq.RecordingConfig.builder() |
| 153 | + .channelType(1) |
| 154 | + .streamTypes(2) |
| 155 | + .maxIdleTime(30) |
| 156 | + .audioProfile(2) |
| 157 | + .transcodingConfig(StartResourceReq.TranscodingConfig.builder() |
| 158 | + .width(640) |
| 159 | + .height(480) |
| 160 | + .fps(15) |
| 161 | + .bitrate(800) |
| 162 | + .mixedVideoLayout(0) |
| 163 | + .backgroundColor("#000000") |
| 164 | + .build()) |
| 165 | + .subscribeAudioUIDs(Collections.singletonList("#allstream#")) |
| 166 | + .subscribeVideoUIDs(Collections.singletonList("#allstream#")) |
| 167 | + .build()) |
| 168 | + .recordingFileConfig(StartResourceReq.RecordingFileConfig.builder() |
| 169 | + .avFileType(Arrays.asList("hls", "mp4")) |
| 170 | + .build()) |
| 171 | + .storageConfig(storageConfig) |
| 172 | + .build(); |
| 173 | + |
| 174 | + |
| 175 | + StartResourceRes startResourceRes; |
| 176 | + |
| 177 | + // Start resource |
| 178 | + try { |
| 179 | + startResourceRes = agoraService.cloudRecording() |
| 180 | + .mixScenario() |
| 181 | + .start(cname, uid, |
| 182 | + acquireResourceRes.getResourceId(), |
| 183 | + startResourceReq) |
| 184 | + .block(); |
| 185 | + |
| 186 | + } catch (AgoraException e) { |
| 187 | + System.out.printf("agora error:%s", e.getMessage()); |
| 188 | + return; |
| 189 | + } catch (Exception e) { |
| 190 | + System.out.printf("unknown error:%s", e.getMessage()); |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + // Check if the response is null |
| 195 | + if (startResourceRes == null || startResourceRes.getSid() == null) { |
| 196 | + System.out.println("failed to start resource"); |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + System.out.printf("sid:%s", startResourceRes.getSid()); |
| 201 | + |
| 202 | + System.out.println("start resource success"); |
| 203 | + |
| 204 | + Thread.sleep(3000); |
| 205 | + |
| 206 | + QueryMixHLSAndMP4RecordingResourceRes queryResourceResp; |
| 207 | + |
| 208 | + // Query resource |
| 209 | + try { |
| 210 | + queryResourceRes = agoraService.cloudRecording() |
| 211 | + .mixScenario() |
| 212 | + .queryHLSAndMP4(startResourceRes.getResourceId(), startResourceRes.getSid()) |
| 213 | + .block(); |
| 214 | + |
| 215 | + } catch (AgoraException e) { |
| 216 | + System.out.printf("agora error:%s", e.getMessage()); |
| 217 | + return; |
| 218 | + } catch (Exception e) { |
| 219 | + System.out.printf("unknown error:%s", e.getMessage()); |
| 220 | + return; |
| 221 | + } |
| 222 | + |
| 223 | + if (queryResourceRes == null || queryResourceResp.getServerResponse() == null) { |
| 224 | + System.out.println("failed to query resource"); |
| 225 | + return; |
| 226 | + } |
| 227 | + |
| 228 | + System.out.println("query resource success"); |
| 229 | + |
| 230 | + Thread.sleep(3000); |
| 231 | + |
| 232 | + StopResourceRes stopResourceRes; |
| 233 | + |
| 234 | + // Stop resource |
| 235 | + try { |
| 236 | + stopResourceRes = agoraService.cloudRecording() |
| 237 | + .mixScenario() |
| 238 | + .stop(cname, uid, startResourceRes.getResourceId(), startResourceRes.getSid(), |
| 239 | + true) |
| 240 | + .block(); |
| 241 | + } catch (AgoraException e) { |
| 242 | + System.out.printf("agora error:%s", e.getMessage()); |
| 243 | + return; |
| 244 | + } catch (Exception e) { |
| 245 | + System.out.printf("unknown error:%s", e.getMessage()); |
| 246 | + return; |
| 247 | + } |
| 248 | + |
| 249 | + // Check if the response is null |
| 250 | + if (stopResourceRes == null || stopResourceRes.getSid() == null) { |
| 251 | + System.out.println("failed to stop resource"); |
| 252 | + } else { |
| 253 | + System.out.println("stop resource success"); |
| 254 | + } |
| 255 | + |
| 256 | + } |
| 257 | +} |
| 258 | + |
| 259 | + |
| 260 | +``` |
| 261 | + |
| 262 | +更多的示例可在 [Example](./examples) 查看 |
| 263 | + |
| 264 | +## 集成遇到困难,该如何联系声网获取协助 |
| 265 | + |
| 266 | +> 方案1:如果您已经在使用声网服务或者在对接中,可以直接联系对接的销售或服务 |
| 267 | +> |
| 268 | + |
| 269 | +> |
| 270 | +> 方案3:扫码加入我们的微信交流群提问 |
| 271 | +> |
| 272 | +> <img src="https://download.agora.io/demo/release/SDHY_QA.jpg" width="360" height="360"> |
| 273 | +--- |
| 274 | + |
| 275 | +## 贡献 |
| 276 | + |
| 277 | +本项目欢迎并接受贡献。如果您在使用中遇到问题或有改进建议,请提出 issue 或向我们提交 Pull Request。 |
| 278 | + |
| 279 | +# SemVer 版本规范 |
| 280 | + |
| 281 | +本项目使用语义化版本号规范 (SemVer) 来管理版本。格式为 MAJOR.MINOR.PATCH。 |
| 282 | + |
| 283 | +* MAJOR 版本号表示不向后兼容的重大更改。 |
| 284 | +* MINOR 版本号表示向后兼容的新功能或增强。 |
| 285 | +* PATCH 版本号表示向后兼容的错误修复和维护。 |
| 286 | + 有关详细信息,请参阅 [语义化版本](https://semver.org/lang/zh-CN/) 规范。 |
| 287 | + |
| 288 | +## 参考 |
| 289 | + |
| 290 | +* [Agora API 文档](https://doc.shengwang.cn/) |
| 291 | + |
| 292 | +## 许可证 |
| 293 | + |
| 294 | +该项目使用MIT许可证,详细信息请参阅LICENSE文件。 |
0 commit comments