PlayController.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package com.genersoft.iot.vmp.vmanager.play;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.genersoft.iot.vmp.common.StreamInfo;
  5. import com.genersoft.iot.vmp.conf.MediaServerConfig;
  6. import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
  7. import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage;
  8. import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
  9. import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
  10. import com.genersoft.iot.vmp.vmanager.service.IPlayService;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.http.HttpStatus;
  16. import org.springframework.http.ResponseEntity;
  17. import org.springframework.web.bind.annotation.CrossOrigin;
  18. import org.springframework.web.bind.annotation.GetMapping;
  19. import org.springframework.web.bind.annotation.PathVariable;
  20. import org.springframework.web.bind.annotation.PostMapping;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.RestController;
  23. import com.alibaba.fastjson.JSONObject;
  24. import com.genersoft.iot.vmp.gb28181.bean.Device;
  25. import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
  26. import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
  27. import org.springframework.web.context.request.async.DeferredResult;
  28. import java.text.DecimalFormat;
  29. import java.util.UUID;
  30. @CrossOrigin
  31. @RestController
  32. @RequestMapping("/api")
  33. public class PlayController {
  34. private final static Logger logger = LoggerFactory.getLogger(PlayController.class);
  35. @Autowired
  36. private SIPCommander cmder;
  37. @Autowired
  38. private IVideoManagerStorager storager;
  39. @Autowired
  40. private ZLMRESTfulUtils zlmresTfulUtils;
  41. @Autowired
  42. private DeferredResultHolder resultHolder;
  43. @Autowired
  44. private IPlayService playService;
  45. @GetMapping("/play/{deviceId}/{channelId}")
  46. public DeferredResult<ResponseEntity<String>> play(@PathVariable String deviceId,
  47. @PathVariable String channelId) {
  48. Device device = storager.queryVideoDevice(deviceId);
  49. StreamInfo streamInfo = storager.queryPlayByDevice(deviceId, channelId);
  50. UUID uuid = UUID.randomUUID();
  51. DeferredResult<ResponseEntity<String>> result = new DeferredResult<ResponseEntity<String>>();
  52. // 录像查询以channelId作为deviceId查询
  53. resultHolder.put(DeferredResultHolder.CALLBACK_CMD_PlAY + uuid, result);
  54. if (streamInfo == null) {
  55. // 发送点播消息
  56. cmder.playStreamCmd(device, channelId, (JSONObject response) -> {
  57. logger.info("收到订阅消息: " + response.toJSONString());
  58. playService.onPublishHandlerForPlay(response, deviceId, channelId, uuid.toString());
  59. });
  60. } else {
  61. String streamId = streamInfo.getStreamId();
  62. JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(streamId);
  63. if (rtpInfo.getBoolean("exist")) {
  64. RequestMessage msg = new RequestMessage();
  65. msg.setId(DeferredResultHolder.CALLBACK_CMD_PlAY + uuid);
  66. msg.setData(JSON.toJSONString(streamInfo));
  67. resultHolder.invokeResult(msg);
  68. } else {
  69. storager.stopPlay(streamInfo);
  70. cmder.playStreamCmd(device, channelId, (JSONObject response) -> {
  71. logger.info("收到订阅消息: " + response.toJSONString());
  72. playService.onPublishHandlerForPlay(response, deviceId, channelId, uuid.toString());
  73. });
  74. }
  75. }
  76. // 超时处理
  77. result.onTimeout(()->{
  78. logger.warn(String.format("设备点播超时,deviceId:%s ,channelId:%s", deviceId, channelId));
  79. // 释放rtpserver
  80. cmder.closeRTPServer(device, channelId);
  81. RequestMessage msg = new RequestMessage();
  82. msg.setId(DeferredResultHolder.CALLBACK_CMD_PlAY + uuid);
  83. msg.setData("Timeout");
  84. resultHolder.invokeResult(msg);
  85. });
  86. return result;
  87. }
  88. @PostMapping("/play/{streamId}/stop")
  89. public ResponseEntity<String> playStop(@PathVariable String streamId) {
  90. cmder.streamByeCmd(streamId);
  91. StreamInfo streamInfo = storager.queryPlayByStreamId(streamId);
  92. if (streamInfo == null)
  93. return new ResponseEntity<String>("streamId not found", HttpStatus.OK);
  94. storager.stopPlay(streamInfo);
  95. if (logger.isDebugEnabled()) {
  96. logger.debug(String.format("设备预览停止API调用,streamId:%s", streamId));
  97. }
  98. if (streamId != null) {
  99. JSONObject json = new JSONObject();
  100. json.put("streamId", streamId);
  101. return new ResponseEntity<String>(json.toString(), HttpStatus.OK);
  102. } else {
  103. logger.warn("设备预览停止API调用失败!");
  104. return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
  105. }
  106. }
  107. /**
  108. * 将不是h264的视频通过ffmpeg 转码为h264 + aac
  109. * @param streamId 流ID
  110. * @return
  111. */
  112. @PostMapping("/play/{streamId}/convert")
  113. public ResponseEntity<String> playConvert(@PathVariable String streamId) {
  114. StreamInfo streamInfo = storager.queryPlayByStreamId(streamId);
  115. if (streamInfo == null) {
  116. logger.warn("视频转码API调用失败!, 视频流已经停止!");
  117. return new ResponseEntity<String>("未找到视频流信息, 视频流可能已经停止", HttpStatus.OK);
  118. }
  119. JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(streamId);
  120. if (!rtpInfo.getBoolean("exist")) {
  121. logger.warn("视频转码API调用失败!, 视频流已停止推流!");
  122. return new ResponseEntity<String>("推流信息在流媒体中不存在, 视频流可能已停止推流", HttpStatus.OK);
  123. } else {
  124. MediaServerConfig mediaInfo = storager.getMediaInfo();
  125. String dstUrl = String.format("rtmp://%s:%s/convert/%s", "127.0.0.1", mediaInfo.getRtmpPort(),
  126. streamId );
  127. String srcUrl = String.format("rtsp://%s:%s/rtp/%s", "127.0.0.1", mediaInfo.getRtspPort(), streamId);
  128. JSONObject jsonObject = zlmresTfulUtils.addFFmpegSource(srcUrl, dstUrl, "1000000");
  129. System.out.println(jsonObject);
  130. JSONObject result = new JSONObject();
  131. if (jsonObject != null && jsonObject.getInteger("code") == 0) {
  132. result.put("code", 0);
  133. JSONObject data = jsonObject.getJSONObject("data");
  134. if (data != null) {
  135. result.put("key", data.getString("key"));
  136. StreamInfo streamInfoResult = new StreamInfo();
  137. streamInfoResult.setRtmp(dstUrl);
  138. streamInfoResult.setRtsp(String.format("rtsp://%s:%s/convert/%s", mediaInfo.getWanIp(), mediaInfo.getRtspPort(), streamId));
  139. streamInfoResult.setStreamId(streamId);
  140. streamInfoResult.setFlv(String.format("http://%s:%s/convert/%s.flv", mediaInfo.getWanIp(), mediaInfo.getHttpPort(), streamId));
  141. streamInfoResult.setWs_flv(String.format("ws://%s:%s/convert/%s.flv", mediaInfo.getWanIp(), mediaInfo.getHttpPort(), streamId));
  142. streamInfoResult.setHls(String.format("http://%s:%s/convert/%s/hls.m3u8", mediaInfo.getWanIp(), mediaInfo.getHttpPort(), streamId));
  143. streamInfoResult.setWs_hls(String.format("ws://%s:%s/convert/%s/hls.m3u8", mediaInfo.getWanIp(), mediaInfo.getHttpPort(), streamId));
  144. streamInfoResult.setFmp4(String.format("http://%s:%s/convert/%s.live.mp4", mediaInfo.getWanIp(), mediaInfo.getHttpPort(), streamId));
  145. streamInfoResult.setWs_fmp4(String.format("ws://%s:%s/convert/%s.live.mp4", mediaInfo.getWanIp(), mediaInfo.getHttpPort(), streamId));
  146. streamInfoResult.setTs(String.format("http://%s:%s/convert/%s.live.ts", mediaInfo.getWanIp(), mediaInfo.getHttpPort(), streamId));
  147. streamInfoResult.setWs_ts(String.format("ws://%s:%s/convert/%s.live.ts", mediaInfo.getWanIp(), mediaInfo.getHttpPort(), streamId));
  148. result.put("data", streamInfoResult);
  149. }
  150. }else {
  151. result.put("code", 1);
  152. result.put("msg", "cover fail");
  153. }
  154. return new ResponseEntity<String>( result.toJSONString(), HttpStatus.OK);
  155. }
  156. }
  157. /**
  158. * 结束转码
  159. * @param key
  160. * @return
  161. */
  162. @PostMapping("/play/convert/stop/{key}")
  163. public ResponseEntity<String> playConvertStop(@PathVariable String key) {
  164. JSONObject jsonObject = zlmresTfulUtils.delFFmpegSource(key);
  165. System.out.println(jsonObject);
  166. JSONObject result = new JSONObject();
  167. if (jsonObject != null && jsonObject.getInteger("code") == 0) {
  168. result.put("code", 0);
  169. JSONObject data = jsonObject.getJSONObject("data");
  170. if (data != null && data.getBoolean("flag")) {
  171. result.put("code", "0");
  172. result.put("msg", "success");
  173. }else {
  174. }
  175. }else {
  176. result.put("code", 1);
  177. result.put("msg", "delFFmpegSource fail");
  178. }
  179. return new ResponseEntity<String>( result.toJSONString(), HttpStatus.OK);
  180. }
  181. }