|
@@ -1,6 +1,8 @@
|
|
|
package com.fs.live.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.exception.base.BaseException;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
|
import com.fs.common.utils.StringUtils;
|
|
@@ -11,11 +13,17 @@ import com.fs.live.mapper.LiveMapper;
|
|
|
import com.fs.live.service.ILiveDataService;
|
|
|
import com.fs.live.service.ILiveService;
|
|
|
import com.fs.live.service.ILiveVideoService;
|
|
|
+import com.fs.system.domain.SysConfig;
|
|
|
+import com.fs.system.service.ISysConfigService;
|
|
|
+import com.qcloud.cos.utils.Md5Utils;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 直播Service业务层处理
|
|
@@ -31,6 +39,9 @@ public class LiveServiceImpl extends ServiceImpl<LiveMapper, Live> implements IL
|
|
|
private final ILiveVideoService liveVideoService;
|
|
|
private final ILiveDataService liveDataService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ISysConfigService sysConfigService;
|
|
|
+
|
|
|
/**
|
|
|
* 查询直播
|
|
|
*
|
|
@@ -166,4 +177,62 @@ public class LiveServiceImpl extends ServiceImpl<LiveMapper, Live> implements IL
|
|
|
public List<Live> liveList() {
|
|
|
return liveMapper.liveList();
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> createLiveRoom(Long liveId) {
|
|
|
+ Map<String, Object> resultMap = new HashMap<>();
|
|
|
+ Live live = this.selectLiveByLiveId(liveId);
|
|
|
+ if (live == null || live.getStatus() != 1 || live.getIsShow() != 1 || live.getIsAudit() != 1) {
|
|
|
+ resultMap.put("code", "500");
|
|
|
+ resultMap.put("msg", "您未拥有直播权限");
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+ SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("living.config");
|
|
|
+ Map<String, String> livingConfigMap = JSON.parseObject(sysConfig.getConfigValue(), Map.class);
|
|
|
+ if (livingConfigMap == null || livingConfigMap.isEmpty()) {
|
|
|
+ resultMap.put("code", "500");
|
|
|
+ resultMap.put("msg", "缺失直播配置");
|
|
|
+ return Collections.emptyMap();
|
|
|
+ }
|
|
|
+ String rtmpPushUrl = generateRtmpPushUrl(livingConfigMap.get("domain"), livingConfigMap.get("app"), liveId.toString());
|
|
|
+ String flvPlayUrl = generateFlvPlayUrl(livingConfigMap.get("http"), livingConfigMap.get("app"), liveId.toString());
|
|
|
+ live.setRtmpUrl(rtmpPushUrl);
|
|
|
+ live.setFlvHlsUrl(flvPlayUrl);
|
|
|
+ this.updateLive(live);
|
|
|
+ resultMap.put("code", "200");
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getLiveRoom(String liveId) {
|
|
|
+ Map<String, Object> resultMap = new HashMap<>();
|
|
|
+ Live live = this.selectLiveByLiveId(Long.valueOf(liveId));
|
|
|
+ if (live == null) {
|
|
|
+ resultMap.put("code", "500");
|
|
|
+ resultMap.put("msg", "未查询到直播间");
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+ resultMap.put("code", "200");
|
|
|
+ resultMap.put("livingUrl", live.getFlvHlsUrl());
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static String generateRtmpPushUrl(String domain, String app, String liveId) {
|
|
|
+ long timestamp = System.currentTimeMillis() / 1000; // 秒级时间戳
|
|
|
+ String SECRET_KEY = liveId + timestamp;
|
|
|
+ String sign = Md5Utils.md5Hex(liveId + SECRET_KEY + timestamp);
|
|
|
+ return String.format("rtmp://%s/%s/%s?timestamp=%d&sign=%s", domain, app, liveId,timestamp,sign);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成 FLV 播放地址(用于前端播放)
|
|
|
+ */
|
|
|
+ public static String generateFlvPlayUrl(String domain, String app, String liveId) {
|
|
|
+ long timestamp = System.currentTimeMillis() / 1000;
|
|
|
+ String SECRET_KEY = liveId + timestamp;
|
|
|
+ String sign = Md5Utils.md5Hex(liveId + SECRET_KEY + timestamp);
|
|
|
+ return String.format("http://%s/%s/%s.flv?timestamp=%d&sign=%s", domain, app, liveId, timestamp, sign);
|
|
|
+ }
|
|
|
}
|