MediaServiceImpl.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.alibaba.fastjson2.JSONArray;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.genersoft.iot.vmp.common.StreamInfo;
  5. import com.genersoft.iot.vmp.conf.MediaConfig;
  6. import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
  7. import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
  8. import com.genersoft.iot.vmp.media.zlm.dto.StreamAuthorityInfo;
  9. import com.genersoft.iot.vmp.service.IMediaServerService;
  10. import com.genersoft.iot.vmp.service.IMediaService;
  11. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  12. import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.util.ObjectUtils;
  18. @Service
  19. public class MediaServiceImpl implements IMediaService {
  20. private final static Logger logger = LoggerFactory.getLogger(MediaServiceImpl.class);
  21. @Autowired
  22. private IRedisCatchStorage redisCatchStorage;
  23. @Autowired
  24. private IVideoManagerStorage storager;
  25. @Autowired
  26. private IMediaServerService mediaServerService;
  27. @Autowired
  28. private MediaConfig mediaConfig;
  29. @Autowired
  30. private ZLMRESTfulUtils zlmresTfulUtils;
  31. @Override
  32. public StreamInfo getStreamInfoByAppAndStream(MediaServerItem mediaInfo, String app, String stream, Object tracks, String callId) {
  33. return getStreamInfoByAppAndStream(mediaInfo, app, stream, tracks, null, callId, true);
  34. }
  35. @Override
  36. public StreamInfo getStreamInfoByAppAndStreamWithCheck(String app, String stream, String mediaServerId, String addr, boolean authority) {
  37. StreamInfo streamInfo = null;
  38. if (mediaServerId == null) {
  39. mediaServerId = mediaConfig.getId();
  40. }
  41. MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
  42. if (mediaInfo == null) {
  43. return null;
  44. }
  45. String calld = null;
  46. StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo(app, stream);
  47. if (streamAuthorityInfo != null) {
  48. calld = streamAuthorityInfo.getCallId();
  49. }
  50. JSONObject mediaList = zlmresTfulUtils.getMediaList(mediaInfo, app, stream);
  51. logger.info("[zlm getMediaList]结果: /n {}", mediaList);
  52. if (mediaList != null) {
  53. if (mediaList.getInteger("code") == 0) {
  54. JSONArray data = mediaList.getJSONArray("data");
  55. if (data == null) {
  56. return null;
  57. }
  58. JSONObject mediaJSON = data.getJSONObject(0);
  59. JSONArray tracks = mediaJSON.getJSONArray("tracks");
  60. if (authority) {
  61. streamInfo = getStreamInfoByAppAndStream(mediaInfo, app, stream, tracks, addr, calld, true);
  62. }else {
  63. streamInfo = getStreamInfoByAppAndStream(mediaInfo, app, stream, tracks, addr,null, true);
  64. }
  65. }
  66. }
  67. return streamInfo;
  68. }
  69. @Override
  70. public StreamInfo getStreamInfoByAppAndStreamWithCheck(String app, String stream, String mediaServerId, boolean authority) {
  71. return getStreamInfoByAppAndStreamWithCheck(app, stream, mediaServerId, null, authority);
  72. }
  73. @Override
  74. public StreamInfo getStreamInfoByAppAndStream(MediaServerItem mediaInfo, String app, String stream, Object tracks, String addr, String callId, boolean isPlay) {
  75. StreamInfo streamInfoResult = new StreamInfo();
  76. streamInfoResult.setStream(stream);
  77. streamInfoResult.setApp(app);
  78. if (addr == null) {
  79. addr = mediaInfo.getStreamIp();
  80. }
  81. streamInfoResult.setIp(addr);
  82. streamInfoResult.setMediaServerId(mediaInfo.getId());
  83. String callIdParam = ObjectUtils.isEmpty(callId)?"":"?callId=" + callId;
  84. streamInfoResult.setRtmp(addr, mediaInfo.getRtmpPort(),mediaInfo.getRtmpSSlPort(), app, stream, callIdParam);
  85. streamInfoResult.setRtsp(addr, mediaInfo.getRtspPort(),mediaInfo.getRtspSSLPort(), app, stream, callIdParam);
  86. streamInfoResult.setFlv(addr, mediaInfo.getHttpPort(),mediaInfo.getHttpSSlPort(), app, stream, callIdParam);
  87. streamInfoResult.setFmp4(addr, mediaInfo.getHttpPort(),mediaInfo.getHttpSSlPort(), app, stream, callIdParam);
  88. streamInfoResult.setHls(addr, mediaInfo.getHttpPort(),mediaInfo.getHttpSSlPort(), app, stream, callIdParam);
  89. streamInfoResult.setTs(addr, mediaInfo.getHttpPort(),mediaInfo.getHttpSSlPort(), app, stream, callIdParam);
  90. streamInfoResult.setRtc(addr, mediaInfo.getHttpPort(),mediaInfo.getHttpSSlPort(), app, stream, callIdParam, isPlay);
  91. streamInfoResult.setTracks(tracks);
  92. if (!"broadcast".equalsIgnoreCase(app) && !ObjectUtils.isEmpty(mediaInfo.getTranscodeSuffix()) && !"null".equalsIgnoreCase(mediaInfo.getTranscodeSuffix())) {
  93. String newStream = stream + "_" + mediaInfo.getTranscodeSuffix();
  94. mediaInfo.setTranscodeSuffix(null);
  95. StreamInfo transcodeStreamInfo = getStreamInfoByAppAndStream(mediaInfo, app, newStream, tracks, addr, callId, isPlay);
  96. streamInfoResult.setTranscodeStream(transcodeStreamInfo);
  97. }
  98. return streamInfoResult;
  99. }
  100. }