瀏覽代碼

修改云端录制逻辑

hgw 3 年之前
父節點
當前提交
9f81f2b3ce

+ 2 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/dao/LiveRoomVideoDao.java

@@ -16,5 +16,7 @@ public interface LiveRoomVideoDao extends BaseMapper<LiveRoomVideo> {
 
     int insertBatch(@Param("entities") List<LiveRoomVideo> entities);
 
+    LiveRoomVideo getLastRecord(@Param("roomId") String roomId, @Param("recordId") String recordId);
+
 }
 

+ 3 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/LiveRoomVideoService.java

@@ -24,6 +24,9 @@ public interface LiveRoomVideoService extends IService<LiveRoomVideo> {
      */
     List<LiveRoomVideo> queryVideo(String roomUid);
 
+    /**
+     * 云端录制回调
+     */
     void recordSync(RecordNotify recordNotify);
 }
 

+ 56 - 29
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/impl/LiveRoomVideoServiceImpl.java

@@ -9,6 +9,7 @@ import com.yonge.cooleshow.biz.dal.entity.LiveRoomVideo;
 import com.yonge.cooleshow.biz.dal.entity.RecordNotify;
 import com.yonge.cooleshow.biz.dal.service.LiveRoomService;
 import com.yonge.cooleshow.biz.dal.service.LiveRoomVideoService;
+import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -50,45 +51,71 @@ public class LiveRoomVideoServiceImpl extends ServiceImpl<LiveRoomVideoDao, Live
         );
     }
 
+    /**
+     * 云端录制回调
+     */
     @Override
     public void recordSync(RecordNotify recordNotify) {
         if (recordNotify.getCode().equals(200)) {
-            //云端录制文件地址
-            String fileUrl = recordNotify.getOutput().getFileUrl();
-            //房间uid
-            String roomUid = recordNotify.getRoomId();
-            //融云唯一id标识
-            String recordId = recordNotify.getRecordId();
-            //回调事件类型
-            Integer notifyType = recordNotify.getType();
-            if (Objects.nonNull(notifyType)) {
-                Date now = new Date();
-                //notifyType 1: 录制开始;4: 文件上传-融云回调完成
+            if (Objects.nonNull(recordNotify.getType())) {
+                //云端录制文件地址
+                String fileUrl = recordNotify.getOutput().getFileUrl();
+                //房间uid
+                String roomUId = recordNotify.getRoomId();
+                //融云唯一id标识
+                String recordId = recordNotify.getRecordId();
+                //回调事件类型
+                Integer notifyType = recordNotify.getType();
+                //查询直播间
+                LiveRoom room = liveRoomService.getOne(new QueryWrapper<LiveRoom>().lambda()
+                        .eq(LiveRoom::getRoomUid, roomUId));
+                //开始录制
                 if (notifyType == 1) {
-                    LiveRoomVideo video = new LiveRoomVideo();
-                    LiveRoom room = liveRoomService.getOne(new QueryWrapper<LiveRoom>().lambda()
-                            .eq(LiveRoom::getRoomUid, roomUid));
-                    video.setCourseGroupId(room.getCourseGroupId());
-                    video.setCourseId(room.getCourseId());
-                    video.setRoomUid(roomUid);
-                    video.setRecordId(recordId);
-                    video.setUrl(fileUrl);
-                    video.setStartTime(now);
-                    video.setType(notifyType);
-                    video.setCreatedTime(now);
-                    this.save(video);
+                    insertVideo(fileUrl, recordId, notifyType, room);
+                    return;
                 }
+                //录制完成
                 if (notifyType == 4) {
-                    LiveRoomVideo video = this.getOne(Wrappers.<LiveRoomVideo>lambdaQuery()
-                            .eq(LiveRoomVideo::getRoomUid, roomUid)
-                            .eq(LiveRoomVideo::getRecordId, recordId));
-                    video.setEndTime(now);
-                    video.setType(notifyType);
-                    this.updateById(video);
+                    //写入数据库
+                    try {
+                        //获取最后一次录制视频
+                        LiveRoomVideo video = baseMapper.getLastRecord(roomUId, recordId);
+                        if (Objects.isNull(video)) {
+                            log.error("recordSync error :roomUId : {} ,recordId:{}", roomUId, recordId);
+                            return;
+                        }
+                        if (StringUtils.isNotEmpty(video.getUrl())) {
+                            //保存切片
+                            insertVideo(fileUrl, recordId, notifyType, room);
+                        } else {
+                            Date now = new Date();
+                            video.setEndTime(now);
+                            video.setType(notifyType);
+                            video.setUrl(fileUrl);
+                            video.setCreatedTime(now);
+                            this.updateById(video);
+                        }
+                    } catch (Exception e) {
+                        log.error("recordSync error : {}", e.getMessage());
+                    }
                 }
             }
         }
     }
 
+    private void insertVideo(String fileUrl, String recordId, Integer notifyType, LiveRoom room) {
+        Date now = new Date();
+        LiveRoomVideo roomVideo = new LiveRoomVideo();
+        roomVideo.setCourseGroupId(room.getCourseGroupId());
+        roomVideo.setCourseId(room.getCourseId());
+        roomVideo.setRoomUid(room.getRoomUid());
+        roomVideo.setRecordId(recordId);
+        roomVideo.setUrl(fileUrl);
+        roomVideo.setStartTime(now);
+        roomVideo.setType(notifyType);
+        roomVideo.setCreatedTime(now);
+        this.save(roomVideo);
+    }
+
 }
 

+ 4 - 0
cooleshow-user/user-biz/src/main/resources/config/mybatis/LiveRoomVideoMapper.xml

@@ -30,4 +30,8 @@
         </foreach>
     </insert>
 
+    <select id="getLastRecord" resultMap="BaseResultMap">
+        SELECT * FROM live_room_video WHERE room_uid_ = #{roomId} AND record_id_ = #{recordId} ORDER BY id_ DESC LIMIT 1
+    </select>
+
 </mapper>

+ 0 - 8
cooleshow-user/user-website/src/main/java/com/yonge/cooleshow/website/controller/LiveRoomVideoController.java

@@ -44,13 +44,5 @@ public class LiveRoomVideoController extends BaseController {
         return succeed(liveRoomVideoService.queryVideo(roomUid));
     }
 
-    @ApiOperation("录制结果回调")
-    @RequestMapping(value = "/recordSync")
-    public void recordSync(@RequestBody String body) {
-        log.info("recordSync body:{}", body);
-        RecordNotify recordNotify = JSONObject.parseObject(body, RecordNotify.class);
-        liveRoomVideoService.recordSync(recordNotify);
-    }
-
 }