| | |
| | | import com.ard.tianyu.service.ITyService; |
| | | import com.ard.tianyu.websocket.utils.WebSocketUtil; |
| | | import com.ard.vtdu.domain.ArdVtdu; |
| | | import com.ard.vtdu.domain.Conf; |
| | | import com.ard.vtdu.service.IMediaService; |
| | | import com.dtflys.forest.exceptions.ForestNetworkException; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.Objects; |
| | | import java.util.Set; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.concurrent.ConcurrentMap; |
| | |
| | | * @param userId 用户ID |
| | | */ |
| | | public void startPullStream(String boxSn, String userId) { |
| | | String webrtcUrl = ""; |
| | | STREAM_USER_MAP.computeIfAbsent(boxSn, k -> ConcurrentHashMap.newKeySet()).add(userId); |
| | | |
| | | String webrtcUrl = BOX_STREAM_PULL_MAP.get(boxSn); |
| | | if (webrtcUrl != null) { |
| | | log.info("{}Already pulled stream: {}", STREAM_LOG_PREFIX, webrtcUrl); |
| | | sendMessageToUser(userId, webrtcUrl); |
| | | return; |
| | | } |
| | | |
| | | try { |
| | | BoxStreamUrlReceiver boxLiveUrl = tyService.getBoxLiveUrl(boxSn); |
| | | if (boxLiveUrl == null) { |
| | | //不管谁拉流都判断流媒体中是否存在 |
| | | boolean exist = mediaService.checkNameExist(boxSn); |
| | | if (!exist) { |
| | | webrtcUrl = addVtdu(boxSn); |
| | | if (Objects.equals(webrtcUrl, "")) { |
| | | log.warn("{}{}{}", STREAM_LOG_PREFIX, STREAM_NOT_FOUND_MSG, boxSn); |
| | | sendMessageToUser(userId, STREAM_NOT_FOUND_MSG + boxSn); |
| | | return; |
| | | } |
| | | |
| | | ArdVtdu vtdu = ArdVtdu.builder() |
| | | .name(boxSn) |
| | | .mode(0) |
| | | .isCode(0) |
| | | .rtspSource(boxLiveUrl.getRtsp()) |
| | | .build(); |
| | | ArdVtdu ardVtdu = mediaService.addPath(vtdu); |
| | | webrtcUrl = ardVtdu.getWebrtcUrl(); |
| | | BOX_STREAM_PULL_MAP.put(boxSn, webrtcUrl); |
| | | |
| | | log.info("{}First pulled stream: {}", STREAM_LOG_PREFIX, webrtcUrl); |
| | | sendMessageToUser(userId, webrtcUrl); |
| | | } catch (Exception e) { |
| | | log.error("{}Failed to start pull stream for box: {}", STREAM_LOG_PREFIX, boxSn, e); |
| | | sendMessageToUser(userId, "Failed to start pull stream due to an internal error."); |
| | | } else { |
| | | webrtcUrl = BOX_STREAM_PULL_MAP.get(boxSn); |
| | | } |
| | | sendMessageToUser(userId, webrtcUrl); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 停止拉流 |
| | | * |
| | |
| | | log.error("{}Failed to send message to user: {}", STREAM_LOG_PREFIX, userId, e); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 通过盒子编号获取rtsp加入流媒体中获取webrtc地址 |
| | | * |
| | | * @param boxSn 盒子编号 |
| | | * @return String webrtcUrl |
| | | */ |
| | | private String addVtdu(String boxSn) { |
| | | try { |
| | | BoxStreamUrlReceiver boxLiveUrl = tyService.getBoxLiveUrl(boxSn); |
| | | if (boxLiveUrl != null) { |
| | | ArdVtdu vtdu = ArdVtdu.builder() |
| | | .name(boxSn) |
| | | .mode(0) |
| | | .isCode(0) |
| | | .rtspSource(boxLiveUrl.getRtsp()) |
| | | .build(); |
| | | ArdVtdu ardVtdu = mediaService.addPath(vtdu); |
| | | return ardVtdu.getWebrtcUrl(); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("获取媒体地址异常:{}", e.getMessage()); |
| | | } |
| | | return ""; |
| | | } |
| | | } |