LiveRoomServiceImpl.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package com.ym.service.Impl;
  2. import com.ym.mec.common.entity.ImRoomMessage;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.ym.http.HttpHelper;
  7. import com.ym.mec.biz.dal.dao.ImLiveBroadcastRoomDao;
  8. import com.ym.mec.biz.dal.entity.ImLiveBroadcastRoom;
  9. import com.ym.mec.biz.dal.entity.TenantAssetsInfo;
  10. import com.ym.mec.common.exception.BizException;
  11. import com.ym.mec.im.IMHelper;
  12. import com.ym.pojo.IMApiResultInfo;
  13. import com.ym.pojo.RecordConfig;
  14. import com.ym.pojo.RecordNotify;
  15. import com.ym.service.LiveRoomService;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.redisson.api.RBucket;
  19. import org.redisson.api.RedissonClient;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import java.net.HttpURLConnection;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. /**
  26. * @author hgw
  27. * Created by 2022-02-21
  28. */
  29. @Slf4j
  30. @Service
  31. public class LiveRoomServiceImpl implements LiveRoomService {
  32. @Autowired
  33. private IMHelper imHelper;
  34. @Autowired
  35. private HttpHelper httpHelper;
  36. @Autowired
  37. private RedissonClient redissonClient;
  38. @Autowired
  39. private ImLiveBroadcastRoomDao imLiveBroadcastRoomDao;
  40. /**
  41. * 创建房间-聊天室
  42. *
  43. * @param roomId 房间Uid
  44. * @param roomName 房间名称
  45. */
  46. public IMApiResultInfo createLiveRoom(String roomId, String roomName) {
  47. IMApiResultInfo resultInfo;
  48. try {
  49. resultInfo = imHelper.createChatRoom(roomId, roomName);
  50. } catch (Exception e) {
  51. log.error("create chatRoom error >>>", e.getCause());
  52. throw new BizException("创建聊天室失败!");
  53. }
  54. if (!resultInfo.isSuccess()) {
  55. log.error("create chatRoom error: {}", resultInfo.getErrorMessage());
  56. throw new BizException("创建聊天室失败!");
  57. }
  58. log.info("create chatRoom success: {}", roomId);
  59. return resultInfo;
  60. }
  61. /**
  62. * 销毁房间-聊天室
  63. *
  64. * @param roomId 房间Uid
  65. */
  66. public IMApiResultInfo destroyLiveRoom(String roomId) {
  67. //删除服务器房间
  68. List<String> deleteRoomIds = new ArrayList<String>() {{
  69. add(roomId);
  70. }};
  71. IMApiResultInfo resultInfo;
  72. try {
  73. resultInfo = imHelper.deleteChrm(deleteRoomIds);
  74. } catch (Exception e) {
  75. throw new BizException("关闭聊天室失败!");
  76. }
  77. if (!resultInfo.isSuccess()) {
  78. log.error("destroy chatRoom error: {}", resultInfo.getErrorMessage());
  79. throw new BizException("关闭聊天室失败!");
  80. }
  81. return resultInfo;
  82. }
  83. /**
  84. * 发送消息
  85. *
  86. * @param fromUserId 发送者id
  87. * @param toChatroomId 房间id
  88. * @param message
  89. */
  90. public IMApiResultInfo publishRoomMessage(ImRoomMessage message) {
  91. IMApiResultInfo resultInfo;
  92. try {
  93. resultInfo = imHelper.publishRoomMessage(message.getFromUserId(), message.getToChatroomId(), message);
  94. } catch (Exception e) {
  95. throw new BizException("消息发送失败" + e.getMessage());
  96. }
  97. if (!resultInfo.isSuccess()) {
  98. log.error("publishRoomMessage chatRoom error: {}", resultInfo.getErrorMessage());
  99. throw new BizException("关闭聊天室失败!");
  100. }
  101. return resultInfo;
  102. }
  103. @Override
  104. public void startRecord(String roomId) throws Exception {
  105. log.error("开始录制直播:roomId : {} ",roomId);
  106. JSONObject paramJson = new JSONObject();
  107. paramJson.put("sessionId",getRoomSessionId(roomId));
  108. paramJson.put("config",new RecordConfig());
  109. String body = paramJson.toJSONString();
  110. HttpURLConnection conn = httpHelper.createIMRtcPostHttpConnection("/rtc/record/start.json", "application/json",roomId);
  111. httpHelper.setBodyParameter(body, conn);
  112. IMApiResultInfo resultInfo = JSON.parseObject(httpHelper.returnResult(conn, body), IMApiResultInfo.class);
  113. if(resultInfo.getCode() != 200){
  114. log.error("直播视频录制失败:resultInfo : {} ",resultInfo);
  115. }
  116. }
  117. @Override
  118. public void stopRecord(String roomId) throws Exception {
  119. JSONObject paramJson = new JSONObject();
  120. paramJson.put("sessionId",getRoomSessionId(roomId));
  121. String body = paramJson.toJSONString();
  122. HttpURLConnection conn = httpHelper.createIMRtcPostHttpConnection("/rtc/record/stop.json", "application/json",roomId);
  123. httpHelper.setBodyParameter(body, conn);
  124. httpHelper.returnResult(conn, body);
  125. RBucket<String> bucket = redissonClient.getBucket("sessionId:" + roomId);
  126. bucket.delete();
  127. log.info("结束录制直播 roomId :{},{}",roomId,httpHelper.returnResult(conn, body));
  128. }
  129. @Override
  130. public void recordSync(RecordNotify recordNotify) {
  131. log.info("recordSync recordNotify:{}",recordNotify);
  132. }
  133. public String getRoomSessionId(String roomId){
  134. RBucket<String> bucket = redissonClient.getBucket("sessionId:" + roomId);
  135. String sessionId = bucket.get();
  136. if(StringUtils.isNotEmpty(sessionId)){
  137. return sessionId;
  138. }
  139. JSONObject jsonObject = new JSONObject();
  140. jsonObject.put("roomId",roomId);
  141. HttpURLConnection conn = null;
  142. try {
  143. conn = httpHelper.createIMRtcPostHttpConnection("/rtc/room/query.json", "application/json",null);
  144. httpHelper.setBodyParameter(jsonObject.toJSONString(), conn);
  145. String returnResult = httpHelper.returnResult(conn, jsonObject.toJSONString());
  146. JSONObject resultObject = JSONObject.parseObject(returnResult);
  147. String code = resultObject.get("code").toString();
  148. if("200".equals(code)){
  149. sessionId = resultObject.get("sessionId").toString();
  150. bucket.set(sessionId);
  151. }else {
  152. log.error("获取sessionId失败 returnResult:{}",returnResult);
  153. throw new BizException("获取sessionId失败");
  154. }
  155. } catch (Exception e) {
  156. e.printStackTrace();
  157. }
  158. return sessionId;
  159. }
  160. }