StreamProxyController.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package com.genersoft.iot.vmp.streamProxy.controller;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.genersoft.iot.vmp.common.StreamInfo;
  4. import com.genersoft.iot.vmp.conf.exception.ControllerException;
  5. import com.genersoft.iot.vmp.conf.security.JwtUtils;
  6. import com.genersoft.iot.vmp.media.bean.MediaServer;
  7. import com.genersoft.iot.vmp.media.service.IMediaServerService;
  8. import com.genersoft.iot.vmp.streamProxy.bean.StreamProxy;
  9. import com.genersoft.iot.vmp.streamProxy.bean.StreamProxyParam;
  10. import com.genersoft.iot.vmp.streamProxy.service.IStreamProxyService;
  11. import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
  12. import com.genersoft.iot.vmp.vmanager.bean.StreamContent;
  13. import com.github.pagehelper.PageInfo;
  14. import io.swagger.v3.oas.annotations.Operation;
  15. import io.swagger.v3.oas.annotations.Parameter;
  16. import io.swagger.v3.oas.annotations.security.SecurityRequirement;
  17. import io.swagger.v3.oas.annotations.tags.Tag;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Controller;
  21. import org.springframework.util.ObjectUtils;
  22. import org.springframework.web.bind.annotation.*;
  23. import java.util.Map;
  24. @SuppressWarnings("rawtypes")
  25. /**
  26. * 拉流代理接口
  27. */
  28. @Tag(name = "拉流代理", description = "")
  29. @Controller
  30. @Slf4j
  31. @RequestMapping(value = "/api/proxy")
  32. public class StreamProxyController {
  33. @Autowired
  34. private IMediaServerService mediaServerService;
  35. @Autowired
  36. private IStreamProxyService streamProxyService;
  37. @Operation(summary = "分页查询流代理", security = @SecurityRequirement(name = JwtUtils.HEADER))
  38. @Parameter(name = "page", description = "当前页")
  39. @Parameter(name = "count", description = "每页查询数量")
  40. @Parameter(name = "query", description = "查询内容")
  41. @Parameter(name = "pulling", description = "是否正在拉流")
  42. @Parameter(name = "mediaServerId", description = "流媒体ID")
  43. @GetMapping(value = "/list")
  44. @ResponseBody
  45. public PageInfo<StreamProxy> list(@RequestParam(required = false)Integer page,
  46. @RequestParam(required = false)Integer count,
  47. @RequestParam(required = false)String query,
  48. @RequestParam(required = false)Boolean pulling,
  49. @RequestParam(required = false)String mediaServerId){
  50. if (ObjectUtils.isEmpty(mediaServerId)) {
  51. mediaServerId = null;
  52. }
  53. if (ObjectUtils.isEmpty(query)) {
  54. query = null;
  55. }
  56. return streamProxyService.getAll(page, count, query, pulling, mediaServerId);
  57. }
  58. @Operation(summary = "查询流代理", security = @SecurityRequirement(name = JwtUtils.HEADER))
  59. @Parameter(name = "app", description = "应用名")
  60. @Parameter(name = "stream", description = "流Id")
  61. @GetMapping(value = "/one")
  62. @ResponseBody
  63. public StreamProxy one(String app, String stream){
  64. return streamProxyService.getStreamProxyByAppAndStream(app, stream);
  65. }
  66. @Operation(summary = "保存代理(已存在会覆盖)", security = @SecurityRequirement(name = JwtUtils.HEADER), parameters = {
  67. @Parameter(name = "param", description = "代理参数", required = true),
  68. })
  69. @PostMapping(value = "/save")
  70. @ResponseBody
  71. public StreamContent save(@RequestBody StreamProxyParam param){
  72. log.info("添加代理: " + JSONObject.toJSONString(param));
  73. if (ObjectUtils.isEmpty(param.getMediaServerId())) {
  74. param.setMediaServerId("auto");
  75. }
  76. if (ObjectUtils.isEmpty(param.getType())) {
  77. param.setType("default");
  78. }
  79. StreamInfo streamInfo = streamProxyService.save(param);
  80. if (param.isEnable()) {
  81. if (streamInfo == null) {
  82. throw new ControllerException(ErrorCode.ERROR100.getCode(), ErrorCode.ERROR100.getMsg());
  83. }else {
  84. return new StreamContent(streamInfo);
  85. }
  86. }else {
  87. return null;
  88. }
  89. }
  90. @Operation(summary = "新增代理", security = @SecurityRequirement(name = JwtUtils.HEADER), parameters = {
  91. @Parameter(name = "param", description = "代理参数", required = true),
  92. })
  93. @PostMapping(value = "/add")
  94. @ResponseBody
  95. public StreamProxy add(@RequestBody StreamProxy param){
  96. log.info("添加代理: " + JSONObject.toJSONString(param));
  97. if (ObjectUtils.isEmpty(param.getMediaServerId())) {
  98. param.setMediaServerId(null);
  99. }
  100. if (ObjectUtils.isEmpty(param.getType())) {
  101. param.setType("default");
  102. }
  103. if (ObjectUtils.isEmpty(param.getGbId())) {
  104. param.setGbDeviceId(null);
  105. }
  106. streamProxyService.add(param);
  107. return param;
  108. }
  109. @Operation(summary = "更新代理", security = @SecurityRequirement(name = JwtUtils.HEADER), parameters = {
  110. @Parameter(name = "param", description = "代理参数", required = true),
  111. })
  112. @PostMapping(value = "/update")
  113. @ResponseBody
  114. public StreamProxy update(@RequestBody StreamProxy param){
  115. log.info("更新代理: " + JSONObject.toJSONString(param));
  116. if (param.getId() == 0) {
  117. throw new ControllerException(ErrorCode.ERROR400.getCode(), "缺少代理信息的ID");
  118. }
  119. if (ObjectUtils.isEmpty(param.getGbId())) {
  120. param.setGbDeviceId(null);
  121. }
  122. streamProxyService.update(param);
  123. return param;
  124. }
  125. @GetMapping(value = "/ffmpeg_cmd/list")
  126. @ResponseBody
  127. @Operation(summary = "获取ffmpeg.cmd模板", security = @SecurityRequirement(name = JwtUtils.HEADER))
  128. @Parameter(name = "mediaServerId", description = "流媒体ID", required = true)
  129. public Map<String, String> getFFmpegCMDs(@RequestParam String mediaServerId){
  130. log.debug("获取节点[ {} ]ffmpeg.cmd模板", mediaServerId );
  131. MediaServer mediaServerItem = mediaServerService.getOne(mediaServerId);
  132. if (mediaServerItem == null) {
  133. throw new ControllerException(ErrorCode.ERROR100.getCode(), "流媒体: " + mediaServerId + "未找到");
  134. }
  135. return streamProxyService.getFFmpegCMDs(mediaServerItem);
  136. }
  137. @DeleteMapping(value = "/del")
  138. @ResponseBody
  139. @Operation(summary = "移除代理", security = @SecurityRequirement(name = JwtUtils.HEADER))
  140. @Parameter(name = "app", description = "应用名", required = true)
  141. @Parameter(name = "stream", description = "流id", required = true)
  142. public void del(@RequestParam String app, @RequestParam String stream){
  143. log.info("移除代理: " + app + "/" + stream);
  144. if (app == null || stream == null) {
  145. throw new ControllerException(ErrorCode.ERROR400.getCode(), app == null ?"app不能为null":"stream不能为null");
  146. }else {
  147. streamProxyService.delteByAppAndStream(app, stream);
  148. }
  149. }
  150. @DeleteMapping(value = "/delete")
  151. @ResponseBody
  152. @Operation(summary = "移除代理", security = @SecurityRequirement(name = JwtUtils.HEADER))
  153. @Parameter(name = "id", description = "代理ID", required = true)
  154. public void delte(int id){
  155. log.info("移除代理: {}", id);
  156. streamProxyService.delete(id);
  157. }
  158. @GetMapping(value = "/start")
  159. @ResponseBody
  160. @Operation(summary = "启用代理", security = @SecurityRequirement(name = JwtUtils.HEADER))
  161. @Parameter(name = "id", description = "代理Id", required = true)
  162. public StreamContent start(int id){
  163. log.info("播放代理: {}", id);
  164. StreamInfo streamInfo = streamProxyService.start(id);
  165. if (streamInfo == null) {
  166. throw new ControllerException(ErrorCode.ERROR100.getCode(), ErrorCode.ERROR100.getMsg());
  167. }else {
  168. return new StreamContent(streamInfo);
  169. }
  170. }
  171. @GetMapping(value = "/stop")
  172. @ResponseBody
  173. @Operation(summary = "停用代理", security = @SecurityRequirement(name = JwtUtils.HEADER))
  174. @Parameter(name = "app", description = "应用名", required = true)
  175. @Parameter(name = "stream", description = "流id", required = true)
  176. public void stop(String app, String stream){
  177. log.info("停用代理: " + app + "/" + stream);
  178. streamProxyService.stopByAppAndStream(app, stream);
  179. }
  180. }