package com.ym.service.Impl; import com.ym.mec.common.entity.ImRoomMessage; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.ym.http.HttpHelper; import com.ym.mec.biz.dal.dao.ImLiveBroadcastRoomDao; import com.ym.mec.biz.dal.entity.ImLiveBroadcastRoom; import com.ym.mec.biz.dal.entity.TenantAssetsInfo; import com.ym.mec.common.exception.BizException; import com.ym.mec.im.IMHelper; import com.ym.pojo.IMApiResultInfo; import com.ym.pojo.RecordConfig; import com.ym.pojo.RecordNotify; import com.ym.service.LiveRoomService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.redisson.api.RBucket; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.List; /** * @author hgw * Created by 2022-02-21 */ @Slf4j @Service public class LiveRoomServiceImpl implements LiveRoomService { @Autowired private IMHelper imHelper; @Autowired private HttpHelper httpHelper; @Autowired private RedissonClient redissonClient; @Autowired private ImLiveBroadcastRoomDao imLiveBroadcastRoomDao; /** * 创建房间-聊天室 * * @param roomId 房间Uid * @param roomName 房间名称 */ public IMApiResultInfo createLiveRoom(String roomId, String roomName) { IMApiResultInfo resultInfo; try { resultInfo = imHelper.createChatRoom(roomId, roomName); } catch (Exception e) { log.error("create chatRoom error >>>", e.getCause()); throw new BizException("创建聊天室失败!"); } if (!resultInfo.isSuccess()) { log.error("create chatRoom error: {}", resultInfo.getErrorMessage()); throw new BizException("创建聊天室失败!"); } log.info("create chatRoom success: {}", roomId); return resultInfo; } /** * 销毁房间-聊天室 * * @param roomId 房间Uid */ public IMApiResultInfo destroyLiveRoom(String roomId) { //删除服务器房间 List deleteRoomIds = new ArrayList() {{ add(roomId); }}; IMApiResultInfo resultInfo; try { resultInfo = imHelper.deleteChrm(deleteRoomIds); } catch (Exception e) { throw new BizException("关闭聊天室失败!"); } if (!resultInfo.isSuccess()) { log.error("destroy chatRoom error: {}", resultInfo.getErrorMessage()); throw new BizException("关闭聊天室失败!"); } return resultInfo; } /** * 发送消息 * * @param fromUserId 发送者id * @param toChatroomId 房间id * @param message */ public IMApiResultInfo publishRoomMessage(ImRoomMessage message) { IMApiResultInfo resultInfo; try { resultInfo = imHelper.publishRoomMessage(message.getFromUserId(), message.getToChatroomId(), message); } catch (Exception e) { throw new BizException("消息发送失败" + e.getMessage()); } if (!resultInfo.isSuccess()) { log.error("publishRoomMessage chatRoom error: {}", resultInfo.getErrorMessage()); throw new BizException("关闭聊天室失败!"); } return resultInfo; } @Override public void startRecord(String roomId) throws Exception { log.error("开始录制直播:roomId : {} ",roomId); JSONObject paramJson = new JSONObject(); paramJson.put("sessionId",getRoomSessionId(roomId)); paramJson.put("config",new RecordConfig()); String body = paramJson.toJSONString(); HttpURLConnection conn = httpHelper.createIMRtcPostHttpConnection("/rtc/record/start.json", "application/json",roomId); httpHelper.setBodyParameter(body, conn); IMApiResultInfo resultInfo = JSON.parseObject(httpHelper.returnResult(conn, body), IMApiResultInfo.class); if(resultInfo.getCode() != 200){ log.error("直播视频录制失败:resultInfo : {} ",resultInfo); } } @Override public void stopRecord(String roomId) throws Exception { JSONObject paramJson = new JSONObject(); paramJson.put("sessionId",getRoomSessionId(roomId)); String body = paramJson.toJSONString(); HttpURLConnection conn = httpHelper.createIMRtcPostHttpConnection("/rtc/record/stop.json", "application/json",roomId); httpHelper.setBodyParameter(body, conn); httpHelper.returnResult(conn, body); RBucket bucket = redissonClient.getBucket("sessionId:" + roomId); bucket.delete(); log.info("结束录制直播 roomId :{},{}",roomId,httpHelper.returnResult(conn, body)); } @Override public void recordSync(RecordNotify recordNotify) { log.info("recordSync recordNotify:{}",recordNotify); } public String getRoomSessionId(String roomId){ RBucket bucket = redissonClient.getBucket("sessionId:" + roomId); String sessionId = bucket.get(); if(StringUtils.isNotEmpty(sessionId)){ return sessionId; } JSONObject jsonObject = new JSONObject(); jsonObject.put("roomId",roomId); HttpURLConnection conn = null; try { conn = httpHelper.createIMRtcPostHttpConnection("/rtc/room/query.json", "application/json",null); httpHelper.setBodyParameter(jsonObject.toJSONString(), conn); String returnResult = httpHelper.returnResult(conn, jsonObject.toJSONString()); JSONObject resultObject = JSONObject.parseObject(returnResult); String code = resultObject.get("code").toString(); if("200".equals(code)){ sessionId = resultObject.get("sessionId").toString(); bucket.set(sessionId); }else { log.error("获取sessionId失败 returnResult:{}",returnResult); throw new BizException("获取sessionId失败"); } } catch (Exception e) { e.printStackTrace(); } return sessionId; } }