Browse Source

Merge remote-tracking branch 'origin/master_saas' into master_saas

zouxuan 2 years ago
parent
commit
699353ada0

+ 12 - 1
mec-biz/src/main/java/com/ym/mec/biz/dal/vo/ImLiveBroadcastRoomDetailVo.java

@@ -30,9 +30,12 @@ public class ImLiveBroadcastRoomDetailVo implements Serializable {
     @ApiModelProperty(value = "点赞数")
     private Integer totalLikeNum;
 
-    @ApiModelProperty(value = "直播时长")
+    @ApiModelProperty(value = "直播时长 分钟 平台统计")
     private Integer totalLiveTime;
 
+    @ApiModelProperty("推流时长 毫秒 三方统计")
+    private Integer liveTotalTime;
+
     @ApiModelProperty(value = "直播开始时间")
     private Date liveStartTime;
 
@@ -44,6 +47,14 @@ public class ImLiveBroadcastRoomDetailVo implements Serializable {
 
     private List<ImLiveRoomVideoVo> videoList;
 
+    public Integer getLiveTotalTime() {
+        return liveTotalTime;
+    }
+
+    public void setLiveTotalTime(Integer liveTotalTime) {
+        this.liveTotalTime = liveTotalTime;
+    }
+
     public Date getLiveStartTime() {
         return liveStartTime;
     }

+ 6 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/ImLiveBroadcastRoomService.java

@@ -156,5 +156,11 @@ public interface ImLiveBroadcastRoomService extends IService<ImLiveBroadcastRoom
      * 直播间销毁定时任务
      */
     void destroyLiveRoom();
+
+    /**
+     * 更新直播推流时间
+     * @param event TencentData.CallbackStreamStateEvent
+     */
+    void updateLiveRoomPushStreamTime(TencentData.CallbackStreamStateEvent event);
 }
 

+ 69 - 5
mec-biz/src/main/java/com/ym/mec/biz/service/impl/ImLiveBroadcastRoomServiceImpl.java

@@ -49,7 +49,6 @@ import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
 import com.ym.mec.util.collection.MapUtil;
 import com.ym.mec.util.date.DateUtil;
 import com.ym.mec.util.excel.POIUtil;
-import com.ym.mec.util.http.HttpUtil;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@@ -738,7 +737,7 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
             liveData.setLikeNum(like);
             liveData.setTotalUserNum(CollectionUtils.isNotEmpty(memberList) ? memberList.size() : 0);
             liveData.setUpdatedTime(now);
-            liveData.setLiveTime(getLookMinutes(speakerInfo.getStartLiveTime(), now, speakerInfo.getTotalLiveTime()));
+            liveData.setLiveTime(getLookMillisecond(speakerInfo.getStartLiveTime(), now, speakerInfo.getTotalLiveTime()));
             liveBroadcastRoomDataService.save(liveData);
             //删除房间主讲人数据
             speakerCache.delete();
@@ -1423,10 +1422,10 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
             roomSpeakerInfo.setEndLiveTime(now);
             roomSpeakerInfo.setState(1);
             //如果开播时间和本次操作结束播放时间小于1分钟则不计算观看时间
-            int liveMinutes = getLookMinutes(roomSpeakerInfo.getStartLiveTime(), null);
+            int liveMinutes = getLookMillisecond(roomSpeakerInfo.getStartLiveTime(), null);
             if (liveMinutes > 1) {
                 //写入本次直播时长
-                roomSpeakerInfo.setTotalLiveTime(getLookMinutes(roomSpeakerInfo.getStartLiveTime(), roomSpeakerInfo.getTotalLiveTime()));
+                roomSpeakerInfo.setTotalLiveTime(getLookMillisecond(roomSpeakerInfo.getStartLiveTime(), roomSpeakerInfo.getTotalLiveTime()));
                 //关闭直播后异步执行计算房间人员观看时长
                 CompletableFuture.runAsync(() -> this.asyncOpsLiveLookTime(roomSpeakerInfo.getRoomUid(), 2, now));
             }
@@ -1903,7 +1902,8 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
         imLiveBroadcastRoom.setSpeakerStatus(liveRoom.getSpeakerStatus());
         imLiveBroadcastRoom.setPushStatus(liveRoom.getPushStatus());
         imLiveBroadcastRoom.setBanStatus(liveRoom.getBanStatus());
-        imLiveBroadcastRoom.setLiveTotalTime(liveRoom.getLiveTotalTime());
+        // 不用前端传时间
+        // imLiveBroadcastRoom.setLiveTotalTime(liveRoom.getLiveTotalTime());
 
         return this.updateById(imLiveBroadcastRoom);
     }
@@ -2068,6 +2068,35 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
         }
     }
 
+    /**
+     * 更新直播推流时间
+     *
+     * @param event TencentData.CallbackStreamStateEvent
+     */
+    @Override
+    public void updateLiveRoomPushStreamTime(TencentData.CallbackStreamStateEvent event) {
+
+        // 直播间ROOM_UID
+        String roomId = event.getStreamId().split("_")[0];
+
+        ImLiveBroadcastRoomVo room = getImLiveBroadcastRoomVo(roomId);
+        if (Objects.isNull(room)) {
+            log.warn("updateLiveRoomPushStreamTime 无效的直播间ID, roomId:{}", roomId);
+            return;
+        }
+
+        // 更新直播时长
+        ImLiveBroadcastRoom entity = new ImLiveBroadcastRoom();
+        // 主键ID
+        entity.setId(room.getId());
+        // 直播累积播放时长
+        int liveTotalTime = Optional.ofNullable(room.getLiveTotalTime()).orElse(0) + Integer.parseInt(event.getPushDuration());
+        // 累积播放时长
+        entity.setLiveTotalTime(liveTotalTime);
+
+        updateById(entity);
+    }
+
 
     /**
      * 查询直播间所有用户信息
@@ -2166,6 +2195,41 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
         return Math.max(minutesBetween, 0);
     }
 
+
+    /**
+     * 计算观看时长差-毫秒数
+     *
+     * @param startDT    开始时间
+     * @param nowMinutes 现在观看时长
+     */
+    private int getLookMillisecond(Date startDT, Integer nowMinutes) {
+        return getLookMillisecond(startDT, new Date(), nowMinutes);
+    }
+    /**
+     * 计算观看时长差-毫秒数
+     *
+     * @param startDT    开始时间
+     * @param endDT      结束时间
+     * @param nowMinutes 现在观看时长
+     */
+    private int getLookMillisecond(Date startDT, Date endDT, Integer nowMinutes) {
+        if (Objects.isNull(nowMinutes)) {
+            nowMinutes = 0;
+        }
+        if (Objects.isNull(startDT)) {
+            return nowMinutes;
+        }
+        if (startDT.getTime() > endDT.getTime()) {
+            return nowMinutes;
+        }
+        //课程结束时间-课程开始时间
+        long durationTime = endDT.getTime() - startDT.getTime();
+        //相差多少分钟
+        int minutesBetween = new Long(durationTime).intValue();
+        minutesBetween += nowMinutes;
+        return Math.max(minutesBetween, 0);
+    }
+
     /**
      * 判断Integer是否相等-null值不相等
      *

+ 2 - 1
mec-biz/src/main/resources/config/mybatis/ImLiveBroadcastRoomDataMapper.xml

@@ -35,7 +35,8 @@
                r.live_remark_              AS liveRemark,
                ifnull(a.total_user_num_, 0) AS totalLookNum,
                ifnull(a.like_num_, 0)      as totalLikeNum,
-               ifnull(a.live_time_, 0)     as totalLiveTime,
+               ifnull(a.live_time_, 0)/60/1000     as totalLiveTime,
+               ifnull(r.live_total_time_, 0)     as liveTotalTime,
                r.live_start_time_      as liveStartTime,
                r.live_end_time_      as liveEndTime,
                r.live_state_      as liveState

+ 8 - 2
mec-im/src/main/java/com/ym/controller/UserController.java

@@ -12,6 +12,7 @@ import com.ym.service.LiveRoomService;
 import com.ym.service.UserService;
 import io.rong.models.user.UserModel;
 import io.swagger.annotations.ApiOperation;
+import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -108,15 +109,20 @@ public class UserController {
 
         // 断流事件通知
         if (event.getEventType() == 0) {
+
+            // 更新推流时长
+            if (StringUtils.isNotBlank(event.getPushDuration()) && event.getPushDuration().matches("\\d+")) {
+                // 更新直播推流时长
+                imLiveBroadcastRoomService.updateLiveRoomPushStreamTime(event);
+            }
+
             // 自动关闭录制
-            // liveRoomService.stopTencentLiveVideoRecord(event.getStreamId());
             imLiveBroadcastRoomService.closeLive(getRoomUid(event.getStreamId()), getSpeakerId(event.getStreamId()));
         }
 
         // 推流事件通知
         if (event.getEventType() == 1) {
             // 自动开启录制
-            // liveRoomService.startTencentLiveVideoRecord(event.getStreamId());
             imLiveBroadcastRoomService.startLive(getRoomUid(event.getStreamId()), getSpeakerId(event.getStreamId()), null);
         }
 

+ 1 - 92
mec-im/src/main/java/com/ym/service/Impl/LiveRoomServiceImpl.java

@@ -1,12 +1,7 @@
 package com.ym.service.Impl;
 
-import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.microsvc.toolkit.middleware.live.LivePluginContext;
-import com.microsvc.toolkit.middleware.live.impl.TencentCloudLivePlugin;
-import com.microsvc.toolkit.middleware.live.message.RTCRequest;
-import com.microsvc.toolkit.middleware.live.message.RTCRoom;
 import com.ym.http.HttpHelper;
 import com.ym.mec.biz.dal.dto.TencentData;
 import com.ym.mec.biz.dal.entity.ImLiveRoomVideo;
@@ -50,8 +45,6 @@ public class LiveRoomServiceImpl implements LiveRoomService {
     private ImLiveRoomVideoService imLiveRoomVideoService;
     @Autowired
     private StoragePluginContext storagePluginContext;
-    @Autowired
-    private LivePluginContext livePluginContext;
 
     /**
      * 创建房间-聊天室
@@ -327,87 +320,6 @@ public class LiveRoomServiceImpl implements LiveRoomService {
         return sessionId;
     }
 
-
-    /**
-     * 创建tencent云直播录制记录
-     *
-     * @param streamId 推流Id
-     */
-    @Override
-    public void startTencentLiveVideoRecord(String streamId) {
-
-        DateTime now = DateTime.now();
-        // 创建直播录制
-        RTCRequest.RecordStart recordStart = RTCRequest.RecordStart.builder()
-                .streamName(streamId)
-                .extra("")
-                .startTime(now.getMillis())
-                .endTime(now.plusDays(1).getMillis())
-                .build();
-        // 创建录制任务失败,重试3次后,发送IM消息通知主播老师
-        int maxRetry = 0;
-        // 录制任务ID
-        String taskId = "";
-        do {
-            try {
-
-                // 创建录制任务
-                RTCRoom.RecordResp resp = livePluginContext.getPluginService(TencentCloudLivePlugin.PLUGIN_NAME)
-                        .rtcRoomRecordStart(recordStart);
-
-                taskId = resp.getRecordId();
-                if (StringUtils.isNotBlank(taskId)) {
-                    maxRetry = 3;
-                }
-                log.info("createTencentLiveRoomVideoRecord resp={}", JSON.toJSONString(resp));
-            } catch (Exception e) {
-                log.error("创建直播录制失败", e);
-            }
-        } while (maxRetry++ < 3);
-
-        log.info("startTencentLiveVideoRecord taskId={}", taskId);
-        // 生成录制记录
-        // 直播间ROOM_UID
-        String roomId = streamId.split("_")[0];
-
-        ImLiveRoomVideo video = imLiveRoomVideoService.getOne(new QueryWrapper<ImLiveRoomVideo>()
-                .eq("room_uid_", roomId)
-                .eq("record_id_", taskId)
-                .eq("type", 0));
-        if (Objects.nonNull(video)) {
-            return;
-        }
-        imLiveRoomVideoService.save(initImLiveRoomVideo(roomId, taskId, new Date()));
-    }
-
-    /**
-     * 关闭tencent云直播录制记录
-     *
-     * @param streamId 推流Id
-     */
-    @Override
-    public void stopTencentLiveVideoRecord(String streamId) {
-
-        // 关闭录制任务失败,重试3次后,发送IM消息通知主播老师
-        int maxRetry = 0;
-        do {
-            try {
-
-                // 关闭直播录制
-                RTCRoom.RecordResp ret = livePluginContext.getPluginService(TencentCloudLivePlugin.PLUGIN_NAME)
-                        .rtcRoomRecordStop(streamId);
-
-                if (StringUtils.isNotBlank(ret.getRequestId())) {
-                    // 重试最大次数
-                    maxRetry = 3;
-                }
-            } catch (Exception e) {
-                log.error("关闭直播录制失败", e);
-            }
-        } while (maxRetry++ < 3);
-
-    }
-
     /**
      * 生成直播录制信息
      *
@@ -419,16 +331,13 @@ public class LiveRoomServiceImpl implements LiveRoomService {
         // 直播间ROOM_UID
         String roomId = event.getStreamId().split("_")[0];
 
-        //云端录制文件地址
-        String fileUrl = storagePluginContext.getPublicUrl(event.getVideoUrl(),"live-rewind");
-
         // 录制开始时间
         long startTime =  (event.getEndTime() - event.getDuration()) * 1000L;
         //保存切片
         ImLiveRoomVideo imLiveRoomVideo = initImLiveRoomVideo(roomId, event.getTaskId(), DateTime.now().toDate());
         imLiveRoomVideo.setStartTime(new DateTime(startTime).toDate());
         imLiveRoomVideo.setEndTime(new DateTime(event.getEndTime() * 1000L).toDate());
-        imLiveRoomVideo.setUrl(fileUrl);
+        imLiveRoomVideo.setUrl(event.getVideoUrl());
         imLiveRoomVideo.setType(2);
 
         imLiveRoomVideoService.save(imLiveRoomVideo);

+ 0 - 12
mec-im/src/main/java/com/ym/service/LiveRoomService.java

@@ -65,18 +65,6 @@ public interface LiveRoomService {
     boolean removeUserUnableSpeak(String roomUid, String userId);
 
     /**
-     * 开始tencent云直播录制记录
-     * @param streamId 推流Id
-     */
-    void startTencentLiveVideoRecord(String streamId);
-
-    /**
-     * 关闭tencent云直播录制记录
-     * @param streamId 推流Id
-     */
-    void stopTencentLiveVideoRecord(String streamId);
-
-    /**
      * 生成直播录制信息
      * @param event TencentData.CallbackSteamRecordEvent
      */

+ 9 - 9
mec-teacher/src/main/java/com/ym/mec/teacher/controller/TeacherImLiveBroadcastRoomController.java

@@ -116,20 +116,20 @@ public class TeacherImLiveBroadcastRoomController extends BaseController {
         return succeed();
     }
 
-    @ApiOperation("开启/关闭直播的录像-接口废弃")
+    @ApiOperation("开启/关闭直播的录像-融云使用")
     @GetMapping("/opsLiveVideo")
     public HttpResponseResult<Object> opsLiveVideo(@ApiParam(value = "房间uid", required = true) String roomUid,
                                                    @ApiParam(value = "用户id", required = true) Integer userId,
                                                    @ApiParam(value = "type 1:开始直播-开始录像     2:关闭直播关闭录像", required = true) Integer type,
                                                    @ApiParam(value = "录制视频的尺寸-默认值是720x1280") String videoResolution) {
-        // if (type == 1) {
-        //     videoResolution = Optional.ofNullable(videoResolution).orElse("720x1280");
-        //     imLiveBroadcastRoomService.startLive(roomUid, userId, videoResolution);
-        // } else if (type == 2) {
-        //     imLiveBroadcastRoomService.closeLive(roomUid, userId);
-        // } else {
-        //     failed("type参数错误");
-        // }
+        if (type == 1) {
+            videoResolution = Optional.ofNullable(videoResolution).orElse("720x1280");
+            imLiveBroadcastRoomService.startLive(roomUid, userId, videoResolution);
+        } else if (type == 2) {
+            imLiveBroadcastRoomService.closeLive(roomUid, userId);
+        } else {
+            failed("type参数错误");
+        }
         return succeed();
     }
 

+ 8 - 9
mec-web/src/main/java/com/ym/mec/web/controller/ImLiveBroadcastRoomController.java

@@ -197,19 +197,18 @@ public class ImLiveBroadcastRoomController extends BaseController {
         return succeed();
     }
 
-    @ApiOperation("开启/关闭直播的录像 -接口废弃")
+    @ApiOperation("开启/关闭直播的录像 -融云使用")
     @GetMapping("/opsLiveVideo")
     public HttpResponseResult<Object> opsLiveVideo(@ApiParam(value = "房间uid", required = true) String roomUid,
                                                    @ApiParam(value = "用户id", required = true) Integer userId,
                                                    @ApiParam(value = "type 1:开始直播-开始录像     2:关闭直播关闭录像", required = true) Integer type) {
-        // 接口废弃
-        // if (type == 1) {
-        //     imLiveBroadcastRoomService.startLive(roomUid, userId, null);
-        // } else if (type == 2) {
-        //     imLiveBroadcastRoomService.closeLive(roomUid, userId);
-        // } else {
-        //     failed("type参数错误");
-        // }
+        if (type == 1) {
+            imLiveBroadcastRoomService.startLive(roomUid, userId, null);
+        } else if (type == 2) {
+            imLiveBroadcastRoomService.closeLive(roomUid, userId);
+        } else {
+            failed("type参数错误");
+        }
         return succeed();
     }