CommonChannelController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package com.genersoft.iot.vmp.gb28181.controller;
  2. import com.genersoft.iot.vmp.common.StreamInfo;
  3. import com.genersoft.iot.vmp.conf.UserSetting;
  4. import com.genersoft.iot.vmp.conf.security.JwtUtils;
  5. import com.genersoft.iot.vmp.gb28181.bean.*;
  6. import com.genersoft.iot.vmp.gb28181.controller.bean.ChannelToGroupByGbDeviceParam;
  7. import com.genersoft.iot.vmp.gb28181.controller.bean.ChannelToGroupParam;
  8. import com.genersoft.iot.vmp.gb28181.controller.bean.ChannelToRegionByGbDeviceParam;
  9. import com.genersoft.iot.vmp.gb28181.controller.bean.ChannelToRegionParam;
  10. import com.genersoft.iot.vmp.gb28181.service.IGbChannelPlayService;
  11. import com.genersoft.iot.vmp.gb28181.service.IGbChannelService;
  12. import com.genersoft.iot.vmp.media.service.IMediaServerService;
  13. import com.genersoft.iot.vmp.service.bean.ErrorCallback;
  14. import com.genersoft.iot.vmp.service.bean.InviteErrorCode;
  15. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  16. import com.genersoft.iot.vmp.vmanager.bean.StreamContent;
  17. import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
  18. import com.github.pagehelper.PageInfo;
  19. import io.swagger.v3.oas.annotations.Operation;
  20. import io.swagger.v3.oas.annotations.Parameter;
  21. import io.swagger.v3.oas.annotations.security.SecurityRequirement;
  22. import io.swagger.v3.oas.annotations.tags.Tag;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.util.Assert;
  26. import org.springframework.util.ObjectUtils;
  27. import org.springframework.web.bind.annotation.*;
  28. import org.springframework.web.context.request.async.DeferredResult;
  29. import javax.servlet.http.HttpServletRequest;
  30. import javax.sip.message.Response;
  31. import java.net.MalformedURLException;
  32. import java.net.URL;
  33. import java.util.List;
  34. @Tag(name = "全局通道管理")
  35. @RestController
  36. @Slf4j
  37. @RequestMapping(value = "/api/common/channel")
  38. public class CommonChannelController {
  39. @Autowired
  40. private IRedisCatchStorage redisCatchStorage;
  41. @Autowired
  42. private IGbChannelService channelService;
  43. @Autowired
  44. private IMediaServerService mediaServerService;
  45. @Autowired
  46. private IGbChannelPlayService channelPlayService;
  47. @Autowired
  48. private UserSetting userSetting;
  49. @Operation(summary = "查询通道信息", security = @SecurityRequirement(name = JwtUtils.HEADER))
  50. @Parameter(name = "id", description = "通道的数据库自增Id", required = true)
  51. @GetMapping(value = "/one")
  52. public CommonGBChannel getOne(int id){
  53. return channelService.getOne(id);
  54. }
  55. @Operation(summary = "获取行业编码列表", security = @SecurityRequirement(name = JwtUtils.HEADER))
  56. @GetMapping("/industry/list")
  57. public List<IndustryCodeType> getIndustryCodeList(){
  58. return channelService.getIndustryCodeList();
  59. }
  60. @Operation(summary = "获取编码列表", security = @SecurityRequirement(name = JwtUtils.HEADER))
  61. @GetMapping("/type/list")
  62. public List<DeviceType> getDeviceTypeList(){
  63. return channelService.getDeviceTypeList();
  64. }
  65. @Operation(summary = "获取编码列表", security = @SecurityRequirement(name = JwtUtils.HEADER))
  66. @GetMapping("/network/identification/list")
  67. public List<NetworkIdentificationType> getNetworkIdentificationTypeList(){
  68. return channelService.getNetworkIdentificationTypeList();
  69. }
  70. @Operation(summary = "更新通道", security = @SecurityRequirement(name = JwtUtils.HEADER))
  71. @PostMapping("/update")
  72. public void update(@RequestBody CommonGBChannel channel){
  73. channelService.update(channel);
  74. }
  75. @Operation(summary = "重置国标通道", security = @SecurityRequirement(name = JwtUtils.HEADER))
  76. @PostMapping("/reset")
  77. public void reset(Integer id){
  78. channelService.reset(id);
  79. }
  80. @Operation(summary = "增加通道", security = @SecurityRequirement(name = JwtUtils.HEADER))
  81. @PostMapping("/add")
  82. public CommonGBChannel add(@RequestBody CommonGBChannel channel){
  83. channelService.add(channel);
  84. return channel;
  85. }
  86. @Operation(summary = "获取通道列表", security = @SecurityRequirement(name = JwtUtils.HEADER))
  87. @Parameter(name = "page", description = "当前页", required = true)
  88. @Parameter(name = "count", description = "每页查询数量", required = true)
  89. @Parameter(name = "query", description = "查询内容")
  90. @Parameter(name = "online", description = "是否在线")
  91. @Parameter(name = "hasRecordPlan", description = "是否已设置录制计划")
  92. @Parameter(name = "channelType", description = "通道类型, 0:国标设备,1:推流设备,2:拉流代理")
  93. @GetMapping("/list")
  94. public PageInfo<CommonGBChannel> queryList(int page, int count,
  95. @RequestParam(required = false) String query,
  96. @RequestParam(required = false) Boolean online,
  97. @RequestParam(required = false) Boolean hasRecordPlan,
  98. @RequestParam(required = false) Integer channelType){
  99. if (ObjectUtils.isEmpty(query)){
  100. query = null;
  101. }
  102. return channelService.queryList(page, count, query, online, hasRecordPlan, channelType);
  103. }
  104. @Operation(summary = "获取关联行政区划通道列表", security = @SecurityRequirement(name = JwtUtils.HEADER))
  105. @Parameter(name = "page", description = "当前页", required = true)
  106. @Parameter(name = "count", description = "每页查询数量", required = true)
  107. @Parameter(name = "query", description = "查询内容")
  108. @Parameter(name = "online", description = "是否在线")
  109. @Parameter(name = "channelType", description = "通道类型, 0:国标设备,1:推流设备,2:拉流代理")
  110. @Parameter(name = "civilCode", description = "行政区划")
  111. @GetMapping("/civilcode/list")
  112. public PageInfo<CommonGBChannel> queryListByCivilCode(int page, int count,
  113. @RequestParam(required = false) String query,
  114. @RequestParam(required = false) Boolean online,
  115. @RequestParam(required = false) Integer channelType,
  116. @RequestParam(required = false) String civilCode){
  117. if (ObjectUtils.isEmpty(query)){
  118. query = null;
  119. }
  120. return channelService.queryListByCivilCode(page, count, query, online, channelType, civilCode);
  121. }
  122. @Operation(summary = "获取关联业务分组通道列表", security = @SecurityRequirement(name = JwtUtils.HEADER))
  123. @Parameter(name = "page", description = "当前页", required = true)
  124. @Parameter(name = "count", description = "每页查询数量", required = true)
  125. @Parameter(name = "query", description = "查询内容")
  126. @Parameter(name = "online", description = "是否在线")
  127. @Parameter(name = "channelType", description = "通道类型, 0:国标设备,1:推流设备,2:拉流代理")
  128. @Parameter(name = "groupDeviceId", description = "业务分组下的父节点ID")
  129. @GetMapping("/parent/list")
  130. public PageInfo<CommonGBChannel> queryListByParentId(int page, int count,
  131. @RequestParam(required = false) String query,
  132. @RequestParam(required = false) Boolean online,
  133. @RequestParam(required = false) Integer channelType,
  134. @RequestParam(required = false) String groupDeviceId){
  135. if (ObjectUtils.isEmpty(query)){
  136. query = null;
  137. }
  138. return channelService.queryListByParentId(page, count, query, online, channelType, groupDeviceId);
  139. }
  140. @Operation(summary = "通道设置行政区划", security = @SecurityRequirement(name = JwtUtils.HEADER))
  141. @PostMapping("/region/add")
  142. public void addChannelToRegion(@RequestBody ChannelToRegionParam param){
  143. Assert.notEmpty(param.getChannelIds(),"通道ID不可为空");
  144. Assert.hasLength(param.getCivilCode(),"未添加行政区划");
  145. channelService.addChannelToRegion(param.getCivilCode(), param.getChannelIds());
  146. }
  147. @Operation(summary = "通道删除行政区划", security = @SecurityRequirement(name = JwtUtils.HEADER))
  148. @PostMapping("/region/delete")
  149. public void deleteChannelToRegion(@RequestBody ChannelToRegionParam param){
  150. Assert.isTrue(!param.getChannelIds().isEmpty() || !ObjectUtils.isEmpty(param.getCivilCode()),"参数异常");
  151. channelService.deleteChannelToRegion(param.getCivilCode(), param.getChannelIds());
  152. }
  153. @Operation(summary = "通道设置行政区划-根据国标设备", security = @SecurityRequirement(name = JwtUtils.HEADER))
  154. @PostMapping("/region/device/add")
  155. public void addChannelToRegionByGbDevice(@RequestBody ChannelToRegionByGbDeviceParam param){
  156. Assert.notEmpty(param.getDeviceIds(),"参数异常");
  157. Assert.hasLength(param.getCivilCode(),"未添加行政区划");
  158. channelService.addChannelToRegionByGbDevice(param.getCivilCode(), param.getDeviceIds());
  159. }
  160. @Operation(summary = "通道删除行政区划-根据国标设备", security = @SecurityRequirement(name = JwtUtils.HEADER))
  161. @PostMapping("/region/device/delete")
  162. public void deleteChannelToRegionByGbDevice(@RequestBody ChannelToRegionByGbDeviceParam param){
  163. Assert.notEmpty(param.getDeviceIds(),"参数异常");
  164. channelService.deleteChannelToRegionByGbDevice(param.getDeviceIds());
  165. }
  166. @Operation(summary = "通道设置业务分组", security = @SecurityRequirement(name = JwtUtils.HEADER))
  167. @PostMapping("/group/add")
  168. public void addChannelToGroup(@RequestBody ChannelToGroupParam param){
  169. Assert.notEmpty(param.getChannelIds(),"通道ID不可为空");
  170. Assert.hasLength(param.getParentId(),"未添加上级分组编号");
  171. Assert.hasLength(param.getBusinessGroup(),"未添加业务分组");
  172. channelService.addChannelToGroup(param.getParentId(), param.getBusinessGroup(), param.getChannelIds());
  173. }
  174. @Operation(summary = "通道删除业务分组", security = @SecurityRequirement(name = JwtUtils.HEADER))
  175. @PostMapping("/group/delete")
  176. public void deleteChannelToGroup(@RequestBody ChannelToGroupParam param){
  177. Assert.isTrue(!param.getChannelIds().isEmpty()
  178. || (!ObjectUtils.isEmpty(param.getParentId()) && !ObjectUtils.isEmpty(param.getBusinessGroup())),
  179. "参数异常");
  180. channelService.deleteChannelToGroup(param.getParentId(), param.getBusinessGroup(), param.getChannelIds());
  181. }
  182. @Operation(summary = "通道设置业务分组-根据国标设备", security = @SecurityRequirement(name = JwtUtils.HEADER))
  183. @PostMapping("/group/device/add")
  184. public void addChannelToGroupByGbDevice(@RequestBody ChannelToGroupByGbDeviceParam param){
  185. Assert.notEmpty(param.getDeviceIds(),"参数异常");
  186. Assert.hasLength(param.getParentId(),"未添加上级分组编号");
  187. Assert.hasLength(param.getBusinessGroup(),"未添加业务分组");
  188. channelService.addChannelToGroupByGbDevice(param.getParentId(), param.getBusinessGroup(), param.getDeviceIds());
  189. }
  190. @Operation(summary = "通道删除业务分组-根据国标设备", security = @SecurityRequirement(name = JwtUtils.HEADER))
  191. @PostMapping("/group/device/delete")
  192. public void deleteChannelToGroupByGbDevice(@RequestBody ChannelToGroupByGbDeviceParam param){
  193. Assert.notEmpty(param.getDeviceIds(),"参数异常");
  194. channelService.deleteChannelToGroupByGbDevice(param.getDeviceIds());
  195. }
  196. @Operation(summary = "播放通道", security = @SecurityRequirement(name = JwtUtils.HEADER))
  197. @GetMapping("/play")
  198. public DeferredResult<WVPResult<StreamContent>> deleteChannelToGroupByGbDevice(HttpServletRequest request, Integer channelId){
  199. Assert.notNull(channelId,"参数异常");
  200. CommonGBChannel channel = channelService.getOne(channelId);
  201. Assert.notNull(channel, "通道不存在");
  202. DeferredResult<WVPResult<StreamContent>> result = new DeferredResult<>(userSetting.getPlayTimeout().longValue());
  203. ErrorCallback<StreamInfo> callback = (code, msg, streamInfo) -> {
  204. if (code == InviteErrorCode.SUCCESS.getCode()) {
  205. WVPResult<StreamContent> wvpResult = WVPResult.success();
  206. if (streamInfo != null) {
  207. if (userSetting.getUseSourceIpAsStreamIp()) {
  208. streamInfo=streamInfo.clone();//深拷贝
  209. String host;
  210. try {
  211. URL url=new URL(request.getRequestURL().toString());
  212. host=url.getHost();
  213. } catch (MalformedURLException e) {
  214. host=request.getLocalAddr();
  215. }
  216. streamInfo.channgeStreamIp(host);
  217. }
  218. if (!ObjectUtils.isEmpty(streamInfo.getMediaServer().getTranscodeSuffix())
  219. && !"null".equalsIgnoreCase(streamInfo.getMediaServer().getTranscodeSuffix())) {
  220. streamInfo.setStream(streamInfo.getStream() + "_" + streamInfo.getMediaServer().getTranscodeSuffix());
  221. }
  222. wvpResult.setData(new StreamContent(streamInfo));
  223. }else {
  224. wvpResult.setCode(code);
  225. wvpResult.setMsg(msg);
  226. }
  227. result.setResult(wvpResult);
  228. }else {
  229. result.setResult(WVPResult.fail(code, msg));
  230. }
  231. };
  232. if (channel.getGbDeviceDbId() != null) {
  233. // 国标通道
  234. channelPlayService.playGbDeviceChannel(channel, callback);
  235. } else if (channel.getStreamProxyId() != null) {
  236. // 拉流代理
  237. channelPlayService.playProxy(channel, callback);
  238. } else if (channel.getStreamPushId() != null) {
  239. // 推流
  240. channelPlayService.playPush(channel, null, null, callback);
  241. } else {
  242. // 通道数据异常
  243. log.error("[点播通用通道] 通道数据异常,无法识别通道来源: {}({})", channel.getGbName(), channel.getGbDeviceId());
  244. throw new PlayException(Response.SERVER_INTERNAL_ERROR, "server internal error");
  245. }
  246. return result;
  247. }
  248. }