Commit 04375d70 by zhangxingmin

push

parent 3b15b4e7
package com.yd.communication.api.config; //package com.yd.communication.api.config;
//
import lombok.Data; //import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; //import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; //import org.springframework.stereotype.Component;
//
@Data //@Data
@Component //@Component
@ConfigurationProperties(prefix = "aliyun.rtc") //@ConfigurationProperties(prefix = "aliyun.rtc")
public class AliyunRtcConfig { //public class AliyunRtcConfig {
private String accessKeyId; // private String accessKeyId;
private String accessKeySecret; // private String accessKeySecret;
private String regionId = "cn-shanghai"; // private String regionId = "cn-shanghai";
private String appId; // private String appId;
private String ossBucket; // private String ossBucket;
private String ossEndpoint = "oss-cn-shanghai.aliyuncs.com"; // private String ossEndpoint = "oss-cn-shanghai.aliyuncs.com";
} //}
\ No newline at end of file \ No newline at end of file
package com.yd.communication.api.record; //package com.yd.communication.api.record;
//
import com.alibaba.fastjson2.JSONObject; //import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.CommonRequest; //import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse; //import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient; //import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient; //import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType; //import com.aliyuncs.http.MethodType;
import com.aliyuncs.live.model.v20161101.StopRtcCloudRecordingRequest; //import com.aliyuncs.live.model.v20161101.StopRtcCloudRecordingRequest;
import com.aliyuncs.live.model.v20161101.StopRtcCloudRecordingResponse; //import com.aliyuncs.live.model.v20161101.StopRtcCloudRecordingResponse;
import com.aliyuncs.profile.DefaultProfile; //import com.aliyuncs.profile.DefaultProfile;
import com.yd.communication.api.config.AliyunRtcConfig; //import com.yd.communication.api.config.AliyunRtcConfig;
import lombok.extern.slf4j.Slf4j; //import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; //import org.springframework.stereotype.Service;
import javax.annotation.Resource; //import javax.annotation.Resource;
import java.util.HashMap; //import java.util.HashMap;
import java.util.Map; //import java.util.Map;
//
@Service //@Service
@Slf4j //@Slf4j
public class AliyunRtcRecordService { //public class AliyunRtcRecordService {
//
@Resource // @Resource
private AliyunRtcConfig rtcConfig; // private AliyunRtcConfig rtcConfig;
//
private IAcsClient getClient() { // private IAcsClient getClient() {
DefaultProfile profile = DefaultProfile.getProfile( // DefaultProfile profile = DefaultProfile.getProfile(
rtcConfig.getRegionId(), // rtcConfig.getRegionId(),
rtcConfig.getAccessKeyId(), // rtcConfig.getAccessKeyId(),
rtcConfig.getAccessKeySecret() // rtcConfig.getAccessKeySecret()
); // );
return new DefaultAcsClient(profile); // return new DefaultAcsClient(profile);
} // }
//
public String startRecording(String channelId) { // public String startRecording(String channelId) {
try { // try {
IAcsClient client = getClient(); // IAcsClient client = getClient();
CommonRequest request = new CommonRequest(); // CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST); // request.setSysMethod(MethodType.POST);
request.setSysDomain("live.aliyuncs.com"); // request.setSysDomain("live.aliyuncs.com");
request.setSysVersion("2016-11-01"); // request.setSysVersion("2016-11-01");
request.setSysAction("StartRtcCloudRecording"); // request.setSysAction("StartRtcCloudRecording");
//
request.putQueryParameter("AppId", rtcConfig.getAppId()); // request.putQueryParameter("AppId", rtcConfig.getAppId());
request.putQueryParameter("ChannelId", channelId); // request.putQueryParameter("ChannelId", channelId);
// 订阅所有用户(不传订阅参数则默认订阅所有) // // 订阅所有用户(不传订阅参数则默认订阅所有)
request.putQueryParameter("RecordParams.RecordMode", "1"); // request.putQueryParameter("RecordParams.RecordMode", "1");
request.putQueryParameter("StorageParams.StorageType", "1"); // request.putQueryParameter("StorageParams.StorageType", "1");
request.putQueryParameter("StorageParams.OSSParams.OSSEndpoint", rtcConfig.getOssEndpoint()); // request.putQueryParameter("StorageParams.OSSParams.OSSEndpoint", rtcConfig.getOssEndpoint());
request.putQueryParameter("StorageParams.OSSParams.OSSBucket", rtcConfig.getOssBucket()); // request.putQueryParameter("StorageParams.OSSParams.OSSBucket", rtcConfig.getOssBucket());
//
CommonResponse response = client.getCommonResponse(request); // CommonResponse response = client.getCommonResponse(request);
String responseData = response.getData(); // String responseData = response.getData();
JSONObject jsonObject = JSONObject.parseObject(responseData); // JSONObject jsonObject = JSONObject.parseObject(responseData);
JSONObject body = jsonObject.getJSONObject("Body"); // JSONObject body = jsonObject.getJSONObject("Body");
String taskId = body.getString("TaskId"); // String taskId = body.getString("TaskId");
if (taskId == null || taskId.isEmpty()) { // if (taskId == null || taskId.isEmpty()) {
throw new RuntimeException("启动录制失败,未返回 TaskId,响应:" + responseData); // throw new RuntimeException("启动录制失败,未返回 TaskId,响应:" + responseData);
} // }
log.info("启动录制成功, TaskId: {}", taskId); // log.info("启动录制成功, TaskId: {}", taskId);
return taskId; // return taskId;
} catch (Exception e) { // } catch (Exception e) {
log.error("启动录制失败", e); // log.error("启动录制失败", e);
throw new RuntimeException("启动云端录制失败: " + e.getMessage(), e); // throw new RuntimeException("启动云端录制失败: " + e.getMessage(), e);
} // }
} // }
//
public void stopRecording(String taskId) { // public void stopRecording(String taskId) {
try { // try {
IAcsClient client = getClient(); // IAcsClient client = getClient();
StopRtcCloudRecordingRequest request = new StopRtcCloudRecordingRequest(); // StopRtcCloudRecordingRequest request = new StopRtcCloudRecordingRequest();
request.setTaskId(taskId); // request.setTaskId(taskId);
StopRtcCloudRecordingResponse response = client.getAcsResponse(request); // StopRtcCloudRecordingResponse response = client.getAcsResponse(request);
log.info("停止录制成功, TaskId: {}", taskId); // log.info("停止录制成功, TaskId: {}", taskId);
} catch (Exception e) { // } catch (Exception e) {
log.error("停止录制失败", e); // log.error("停止录制失败", e);
throw new RuntimeException("停止云端录制失败: " + e.getMessage(), e); // throw new RuntimeException("停止云端录制失败: " + e.getMessage(), e);
} // }
} // }
//
/** // /**
* 查询录制任务详情(文件信息) // * 查询录制任务详情(文件信息)
* @param taskId // * @param taskId
* @return // * @return
*/ // */
public Map<String, String> queryRecordingInfo(String taskId) { // public Map<String, String> queryRecordingInfo(String taskId) {
try { // try {
IAcsClient client = getClient(); // IAcsClient client = getClient();
CommonRequest request = new CommonRequest(); // CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST); // request.setSysMethod(MethodType.POST);
request.setSysDomain("live.aliyuncs.com"); // request.setSysDomain("live.aliyuncs.com");
request.setSysVersion("2016-11-01"); // request.setSysVersion("2016-11-01");
request.setSysAction("DescribeCloudRecording"); // request.setSysAction("DescribeCloudRecording");
request.putQueryParameter("TaskId", taskId); // request.putQueryParameter("TaskId", taskId);
//
CommonResponse response = client.getCommonResponse(request); // CommonResponse response = client.getCommonResponse(request);
String responseData = response.getData(); // String responseData = response.getData();
log.info("查询录制信息响应: {}", responseData); // log.info("查询录制信息响应: {}", responseData);
//
JSONObject jsonObject = JSONObject.parseObject(responseData); // JSONObject jsonObject = JSONObject.parseObject(responseData);
JSONObject body = jsonObject.getJSONObject("Body"); // JSONObject body = jsonObject.getJSONObject("Body");
if (body == null) { // if (body == null) {
return new HashMap<>(); // return new HashMap<>();
} // }
//
Map<String, String> info = new HashMap<>(); // Map<String, String> info = new HashMap<>();
// 根据实际返回字段解析,常见字段名 // // 根据实际返回字段解析,常见字段名
if (body.containsKey("FileUrl")) { // if (body.containsKey("FileUrl")) {
info.put("fileUrl", body.getString("FileUrl")); // info.put("fileUrl", body.getString("FileUrl"));
} else if (body.containsKey("fileUrl")) { // } else if (body.containsKey("fileUrl")) {
info.put("fileUrl", body.getString("fileUrl")); // info.put("fileUrl", body.getString("fileUrl"));
} // }
if (body.containsKey("Duration")) { // if (body.containsKey("Duration")) {
info.put("duration", body.getString("Duration")); // info.put("duration", body.getString("Duration"));
} else if (body.containsKey("duration")) { // } else if (body.containsKey("duration")) {
info.put("duration", body.getString("duration")); // info.put("duration", body.getString("duration"));
} // }
if (body.containsKey("FileSize")) { // if (body.containsKey("FileSize")) {
info.put("size", body.getString("FileSize")); // info.put("size", body.getString("FileSize"));
} else if (body.containsKey("fileSize")) { // } else if (body.containsKey("fileSize")) {
info.put("size", body.getString("fileSize")); // info.put("size", body.getString("fileSize"));
} // }
// 可能还有其他字段,如 RecordingStartTime, RecordingEndTime 等 // // 可能还有其他字段,如 RecordingStartTime, RecordingEndTime 等
return info; // return info;
} catch (Exception e) { // } catch (Exception e) {
log.error("查询录制信息失败", e); // log.error("查询录制信息失败", e);
throw new RuntimeException("查询录制信息失败: " + e.getMessage(), e); // throw new RuntimeException("查询录制信息失败: " + e.getMessage(), e);
} // }
} // }
} //}
\ No newline at end of file \ No newline at end of file
...@@ -74,19 +74,19 @@ ...@@ -74,19 +74,19 @@
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<!-- 阿里云 Java SDK 核心库 --> <!-- &lt;!&ndash; 阿里云 Java SDK 核心库 &ndash;&gt;-->
<dependency> <!-- <dependency>-->
<groupId>com.aliyun</groupId> <!-- <groupId>com.aliyun</groupId>-->
<artifactId>aliyun-java-sdk-core</artifactId> <!-- <artifactId>aliyun-java-sdk-core</artifactId>-->
<version>4.6.3</version> <!-- <version>4.6.3</version>-->
</dependency> <!-- </dependency>-->
<!-- 阿里云视频直播 SDK(包含 RTC 云端录制 API) --> <!-- &lt;!&ndash; 阿里云视频直播 SDK(包含 RTC 云端录制 API) &ndash;&gt;-->
<dependency> <!-- <dependency>-->
<groupId>com.aliyun</groupId> <!-- <groupId>com.aliyun</groupId>-->
<artifactId>aliyun-java-sdk-live</artifactId> <!-- <artifactId>aliyun-java-sdk-live</artifactId>-->
<version>3.9.76</version> <!-- <version>3.9.76</version>-->
</dependency> <!-- </dependency>-->
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment