PlayController.java 9.0 KB

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