zx
2022-08-19 4ee8924bd9dbd36046d922d9480de310ee16b6ac
src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/play/PlayController.java
@@ -15,10 +15,10 @@
import com.genersoft.iot.vmp.vmanager.gb28181.play.bean.PlayResult;
import com.genersoft.iot.vmp.service.IMediaService;
import com.genersoft.iot.vmp.service.IPlayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -33,15 +33,13 @@
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
import org.springframework.web.context.request.async.DeferredResult;
import java.util.List;
import java.util.UUID;
import javax.sip.message.Response;
@Api(tags = "国标设备点播")
@Tag(name  = "国标设备点播")
@CrossOrigin
@RestController
@RequestMapping("/api/play")
@@ -56,7 +54,7 @@
   private VideoStreamSessionManager streamSession;
   @Autowired
   private IVideoManagerStorager storager;
   private IVideoManagerStorage storager;
   @Autowired
   private IRedisCatchStorage redisCatchStorage;
@@ -76,11 +74,9 @@
   @Autowired
   private IMediaServerService mediaServerService;
   @ApiOperation("开始点播")
   @ApiImplicitParams({
         @ApiImplicitParam(name = "deviceId", value = "设备ID", dataTypeClass = String.class),
         @ApiImplicitParam(name = "channelId", value = "通道ID", dataTypeClass = String.class),
   })
   @Operation(summary = "开始点播")
   @Parameter(name = "deviceId", description = "设备国标编号", required = true)
   @Parameter(name = "channelId", description = "通道国标编号", required = true)
   @GetMapping("/start/{deviceId}/{channelId}")
   public DeferredResult<ResponseEntity<String>> play(@PathVariable String deviceId,
                                          @PathVariable String channelId) {
@@ -88,16 +84,15 @@
      // 获取可用的zlm
      Device device = storager.queryVideoDevice(deviceId);
      MediaServerItem newMediaServerItem = playService.getNewMediaServerItem(device);
      PlayResult playResult = playService.play(newMediaServerItem, deviceId, channelId, null, null);
      PlayResult playResult = playService.play(newMediaServerItem, deviceId, channelId, null, null, null);
      return playResult.getResult();
   }
   @ApiOperation("停止点播")
   @ApiImplicitParams({
         @ApiImplicitParam(name = "deviceId", value = "设备ID", dataTypeClass = String.class),
         @ApiImplicitParam(name = "channelId", value = "通道ID", dataTypeClass = String.class),
   })
   @Operation(summary = "停止点播")
   @Parameter(name = "deviceId", description = "设备国标编号", required = true)
   @Parameter(name = "channelId", description = "通道国标编号", required = true)
   @GetMapping("/stop/{deviceId}/{channelId}")
   public DeferredResult<ResponseEntity<String>> playStop(@PathVariable String deviceId, @PathVariable String channelId) {
@@ -109,27 +104,24 @@
      // 录像查询以channelId作为deviceId查询
      String key = DeferredResultHolder.CALLBACK_CMD_STOP + deviceId + channelId;
      resultHolder.put(key, uuid, result);
      Device device = storager.queryVideoDevice(deviceId);
      cmder.streamByeCmd(deviceId, channelId, (event) -> {
         StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
         if (streamInfo == null) {
            RequestMessage msg = new RequestMessage();
            msg.setId(uuid);
            msg.setKey(key);
            msg.setData("点播未找到");
            resultHolder.invokeAllResult(msg);
            storager.stopPlay(deviceId, channelId);
         }else {
            redisCatchStorage.stopPlay(streamInfo);
            storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
            RequestMessage msg = new RequestMessage();
            msg.setId(uuid);
            msg.setKey(key);
            //Response response = event.getResponse();
            msg.setData(String.format("success"));
            resultHolder.invokeAllResult(msg);
         }
         mediaServerService.closeRTPServer(device, channelId);
      StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
      if (streamInfo == null) {
         RequestMessage msg = new RequestMessage();
         msg.setId(uuid);
         msg.setKey(key);
         msg.setData("点播未找到");
         resultHolder.invokeAllResult(msg);
         storager.stopPlay(deviceId, channelId);
         return result;
      }
      cmder.streamByeCmd(deviceId, channelId, streamInfo.getStream(), null, eventResult -> {
         redisCatchStorage.stopPlay(streamInfo);
         storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
         RequestMessage msgForSuccess = new RequestMessage();
         msgForSuccess.setId(uuid);
         msgForSuccess.setKey(key);
         msgForSuccess.setData(String.format("success"));
         resultHolder.invokeAllResult(msgForSuccess);
      });
      if (deviceId != null || channelId != null) {
@@ -153,6 +145,8 @@
      // 超时处理
      result.onTimeout(()->{
         logger.warn(String.format("设备预览/回放停止超时,deviceId/channelId:%s_%s ", deviceId, channelId));
         redisCatchStorage.stopPlay(streamInfo);
         storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
         RequestMessage msg = new RequestMessage();
         msg.setId(uuid);
         msg.setKey(key);
@@ -167,15 +161,13 @@
    * @param streamId 流ID
    * @return
    */
   @ApiOperation("将不是h264的视频通过ffmpeg 转码为h264 + aac")
   @ApiImplicitParams({
         @ApiImplicitParam(name = "streamId", value = "视频流ID", dataTypeClass = String.class),
   })
   @Operation(summary = "将不是h264的视频通过ffmpeg 转码为h264 + aac")
   @Parameter(name = "streamId", description = "视频流ID", required = true)
   @PostMapping("/convert/{streamId}")
   public ResponseEntity<String> playConvert(@PathVariable String streamId) {
      StreamInfo streamInfo = redisCatchStorage.queryPlayByStreamId(streamId);
      if (streamInfo == null) {
         streamInfo = redisCatchStorage.queryPlaybackByStreamId(streamId);
         streamInfo = redisCatchStorage.queryPlayback(null, null, streamId, null);
      }
      if (streamInfo == null) {
         logger.warn("视频转码API调用失败!, 视频流已经停止!");
@@ -198,7 +190,7 @@
            JSONObject data = jsonObject.getJSONObject("data");
            if (data != null) {
                  result.put("key", data.getString("key"));
               StreamInfo streamInfoResult = mediaService.getStreamInfoByAppAndStreamWithCheck("convert", streamId, mediaInfo.getId());
               StreamInfo streamInfoResult = mediaService.getStreamInfoByAppAndStreamWithCheck("convert", streamId, mediaInfo.getId(), false);
               result.put("data", streamInfoResult);
            }
         }else {
@@ -214,10 +206,9 @@
    * @param key
    * @return
    */
   @ApiOperation("结束转码")
   @ApiImplicitParams({
         @ApiImplicitParam(name = "key", value = "视频流key", dataTypeClass = String.class),
   })
   @Operation(summary = "结束转码")
   @Parameter(name = "key", description = "视频流key", required = true)
   @Parameter(name = "mediaServerId", description = "流媒体服务ID", required = true)
   @PostMapping("/convertStop/{key}")
   public ResponseEntity<String> playConvertStop(@PathVariable String key, String mediaServerId) {
      JSONObject result = new JSONObject();
@@ -253,10 +244,8 @@
   }
   @ApiOperation("语音广播命令")
   @ApiImplicitParams({
         @ApiImplicitParam(name = "deviceId", value = "设备Id", dataTypeClass = String.class),
   })
   @Operation(summary = "语音广播命令")
   @Parameter(name = "deviceId", description = "设备国标编号", required = true)
    @GetMapping("/broadcast/{deviceId}")
    @PostMapping("/broadcast/{deviceId}")
    public DeferredResult<ResponseEntity<String>> broadcastApi(@PathVariable String deviceId) {
@@ -316,7 +305,7 @@
      return result;
   }
   @ApiOperation("获取所有的ssrc")
   @Operation(summary = "获取所有的ssrc")
   @GetMapping("/ssrc")
   public WVPResult<JSONObject> getSSRC() {
      if (logger.isDebugEnabled()) {
@@ -329,7 +318,7 @@
         jsonObject.put("deviceId", transaction.getDeviceId());
         jsonObject.put("channelId", transaction.getChannelId());
         jsonObject.put("ssrc", transaction.getSsrc());
         jsonObject.put("streamId", transaction.getStreamId());
         jsonObject.put("streamId", transaction.getStream());
         objects.add(jsonObject);
      }