MediaServiceImpl.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.genersoft.iot.vmp.common.StreamInfo;
  3. import com.genersoft.iot.vmp.conf.MediaConfig;
  4. import com.genersoft.iot.vmp.media.bean.MediaInfo;
  5. import com.genersoft.iot.vmp.media.service.IMediaServerService;
  6. import com.genersoft.iot.vmp.media.zlm.ZLMHttpHookListener;
  7. import com.genersoft.iot.vmp.media.zlm.dto.MediaServer;
  8. import com.genersoft.iot.vmp.media.zlm.dto.StreamAuthorityInfo;
  9. import com.genersoft.iot.vmp.media.zlm.dto.hook.HookResultForOnPublish;
  10. import com.genersoft.iot.vmp.service.IMediaService;
  11. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.util.ObjectUtils;
  17. import java.util.List;
  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 IMediaServerService mediaServerService;
  25. @Autowired
  26. private MediaConfig mediaConfig;
  27. @Override
  28. public StreamInfo getStreamInfoByAppAndStream(MediaServer mediaServerItem, String app, String stream, MediaInfo mediaInfo, String callId) {
  29. return getStreamInfoByAppAndStream(mediaServerItem, app, stream, mediaInfo, null, callId, true);
  30. }
  31. @Override
  32. public StreamInfo getStreamInfoByAppAndStreamWithCheck(String app, String stream, String mediaServerId, String addr, boolean authority) {
  33. StreamInfo streamInfo = null;
  34. if (mediaServerId == null) {
  35. mediaServerId = mediaConfig.getId();
  36. }
  37. MediaServer mediaInfo = mediaServerService.getOne(mediaServerId);
  38. if (mediaInfo == null) {
  39. return null;
  40. }
  41. String calld = null;
  42. StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo(app, stream);
  43. if (streamAuthorityInfo != null) {
  44. calld = streamAuthorityInfo.getCallId();
  45. }
  46. List<StreamInfo> streamInfoList = mediaServerService.getMediaList(mediaInfo, app, stream, calld);
  47. if (streamInfoList.isEmpty()) {
  48. return null;
  49. }else {
  50. return streamInfoList.get(0);
  51. }
  52. }
  53. @Override
  54. public StreamInfo getStreamInfoByAppAndStreamWithCheck(String app, String stream, String mediaServerId, boolean authority) {
  55. return getStreamInfoByAppAndStreamWithCheck(app, stream, mediaServerId, null, authority);
  56. }
  57. @Override
  58. public StreamInfo getStreamInfoByAppAndStream(MediaServer mediaServer, String app, String stream, MediaInfo mediaInfo, String addr, String callId, boolean isPlay) {
  59. StreamInfo streamInfoResult = new StreamInfo();
  60. streamInfoResult.setStream(stream);
  61. streamInfoResult.setApp(app);
  62. if (addr == null) {
  63. addr = mediaServer.getStreamIp();
  64. }
  65. streamInfoResult.setIp(addr);
  66. streamInfoResult.setMediaServerId(mediaServer.getId());
  67. String callIdParam = ObjectUtils.isEmpty(callId)?"":"?callId=" + callId;
  68. streamInfoResult.setRtmp(addr, mediaServer.getRtmpPort(),mediaServer.getRtmpSSlPort(), app, stream, callIdParam);
  69. streamInfoResult.setRtsp(addr, mediaServer.getRtspPort(),mediaServer.getRtspSSLPort(), app, stream, callIdParam);
  70. streamInfoResult.setFlv(addr, mediaServer.getHttpPort(),mediaServer.getHttpSSlPort(), app, stream, callIdParam);
  71. streamInfoResult.setFmp4(addr, mediaServer.getHttpPort(),mediaServer.getHttpSSlPort(), app, stream, callIdParam);
  72. streamInfoResult.setHls(addr, mediaServer.getHttpPort(),mediaServer.getHttpSSlPort(), app, stream, callIdParam);
  73. streamInfoResult.setTs(addr, mediaServer.getHttpPort(),mediaServer.getHttpSSlPort(), app, stream, callIdParam);
  74. streamInfoResult.setRtc(addr, mediaServer.getHttpPort(),mediaServer.getHttpSSlPort(), app, stream, callIdParam, isPlay);
  75. streamInfoResult.setMediaInfo(mediaInfo);
  76. return streamInfoResult;
  77. }
  78. @Override
  79. public boolean authenticatePlay(String app, String stream, String callId) {
  80. if (app == null || stream == null) {
  81. return false;
  82. }
  83. if ("rtp".equals(app)) {
  84. return true;
  85. }
  86. StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo(app, stream);
  87. return (streamAuthorityInfo != null && streamAuthorityInfo.getCallId() != null && !streamAuthorityInfo.getCallId().equals(callId));
  88. }
  89. }