|
|
@@ -1,8 +1,10 @@
|
|
|
package com.inspur.netty.stream.service.impl;
|
|
|
|
|
|
import com.aizuda.zlm4j.structure.MK_MEDIA_SOURCE;
|
|
|
+import com.inspur.device.domain.constants.DeviceTaskConstants;
|
|
|
import com.inspur.device.domain.vo.SmsbDeviceVo;
|
|
|
import com.inspur.device.service.ISmsbDeviceService;
|
|
|
+import com.inspur.device.service.ISmsbDeviceTaskService;
|
|
|
import com.inspur.netty.domain.MediaServerConstants;
|
|
|
import com.inspur.netty.domain.vo.StartStreamResultVo;
|
|
|
import com.inspur.netty.message.push.PushMessageType;
|
|
|
@@ -12,6 +14,7 @@ import lombok.RequiredArgsConstructor;
|
|
|
import org.dromara.common.core.utils.StringUtils;
|
|
|
import org.dromara.common.redis.utils.RedisUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.time.Duration;
|
|
|
@@ -32,6 +35,9 @@ public class StreamServiceImpl implements IStreamService {
|
|
|
|
|
|
private static final String DEVICE_STREAM_VIEW_KEY = "global:stream:device:view:";
|
|
|
|
|
|
+ @Value("${server.zlm4j.ip}")
|
|
|
+ private String zlmIp;
|
|
|
+
|
|
|
private static final String schema = "rtmp";
|
|
|
|
|
|
private static final String app = "live";
|
|
|
@@ -39,9 +45,32 @@ public class StreamServiceImpl implements IStreamService {
|
|
|
@Autowired
|
|
|
private ISmsbDeviceService smsbDeviceService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ISmsbDeviceTaskService smsbDeviceTaskService;
|
|
|
|
|
|
@Override
|
|
|
public StartStreamResultVo startStream(Long deviceId) {
|
|
|
+ StartStreamResultVo resultVo = new StartStreamResultVo();
|
|
|
+ resultVo.setPushResult(true);
|
|
|
+ // 1 根据设备ID查询设备信息
|
|
|
+ SmsbDeviceVo smsbDeviceVo = smsbDeviceService.getDeviceCacheById(deviceId);
|
|
|
+ // 2 生成推流地址
|
|
|
+ String streamUrl = schema + "://" + zlmIp + ":1935/" + app + "/" + smsbDeviceVo.getIdentifier();
|
|
|
+ // 3 前端看流地址为flv
|
|
|
+ String viewUrl = "http://" + zlmIp + ":7788/" + app + "/" + smsbDeviceVo.getIdentifier() + ".live.flv";
|
|
|
+ // 4 任务中心创建任务
|
|
|
+ String taskParam = PushMessageType.CONTROL_START_STREAM.getValue();
|
|
|
+ smsbDeviceTaskService.createNewDeviceTask(DeviceTaskConstants.DEVICE_TASK_STREAM_START, smsbDeviceVo, taskParam);
|
|
|
+ // 5 发送netty消息,通知设备开始推流
|
|
|
+ String nettyMessage = PushMessageType.CONTROL_START_STREAM.getValue() + "|" + streamUrl;
|
|
|
+ // boolean pushResult = PushMsgUtil.sendV2(smsbDeviceVo.getIdentifier(), nettyMessage);
|
|
|
+ boolean pushResult = true;
|
|
|
+ resultVo.setPushResult(pushResult);
|
|
|
+ resultVo.setViewUrl(viewUrl);
|
|
|
+ return resultVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public StartStreamResultVo startStreamV1(Long deviceId) {
|
|
|
StartStreamResultVo resultVo = new StartStreamResultVo();
|
|
|
resultVo.setPushResult(true);
|
|
|
// 根据设备ID查询设备信息
|
|
|
@@ -77,6 +106,34 @@ public class StreamServiceImpl implements IStreamService {
|
|
|
|
|
|
@Override
|
|
|
public void stopView(Long deviceId) {
|
|
|
+ // 1 根据设备ID查询设备信息
|
|
|
+ SmsbDeviceVo smsbDeviceVo = smsbDeviceService.getDeviceCacheById(deviceId);
|
|
|
+ String nettyMessage = PushMessageType.CONTROL_STOP_STREAM.getValue();
|
|
|
+ String streamUrl = schema + "://" + zlmIp + ":1935/" + app + "/" + smsbDeviceVo.getIdentifier();
|
|
|
+ // 2 根据当前流地址查询观看人数
|
|
|
+ MK_MEDIA_SOURCE mkMediaSource = ZLM_API.mk_media_source_find2(schema, MediaServerConstants.DEFAULT_VHOST, app, streamUrl, 0);
|
|
|
+ if (mkMediaSource == null) {
|
|
|
+ // 强制关闭
|
|
|
+ // 3 任务中心创建任务
|
|
|
+ String taskParam = PushMessageType.CONTROL_STOP_STREAM.getValue();
|
|
|
+ smsbDeviceTaskService.createNewDeviceTask(DeviceTaskConstants.DEVICE_TASK_STREAM_STOP, smsbDeviceVo, taskParam);
|
|
|
+ PushMsgUtil.sendV2(smsbDeviceVo.getIdentifier(), nettyMessage);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ int readerCount = ZLM_API.mk_media_source_get_reader_count(mkMediaSource);
|
|
|
+ if (readerCount > 1) {
|
|
|
+ // 说明不止一个人在观看
|
|
|
+
|
|
|
+ } else {
|
|
|
+ // 3 任务中心创建任务
|
|
|
+ String taskParam = PushMessageType.CONTROL_STOP_STREAM.getValue();
|
|
|
+ smsbDeviceTaskService.createNewDeviceTask(DeviceTaskConstants.DEVICE_TASK_STREAM_STOP, smsbDeviceVo, taskParam);
|
|
|
+ // 强制关闭
|
|
|
+ PushMsgUtil.sendV2(smsbDeviceVo.getIdentifier(), nettyMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void stopViewV1(Long deviceId) {
|
|
|
String pushUrl = RedisUtils.getCacheObject(DEVICE_STREAM_PUSH_KEY + deviceId);
|
|
|
if (StringUtils.isEmpty(pushUrl)) {
|
|
|
return;
|