liusuyi
2025-03-03 6531e781105e073a55a23529e2b7b31e85110951
ard-work/src/main/java/com/ard/tianyu/service/impl/UavStreamManager.java
@@ -4,11 +4,14 @@
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;
@@ -39,39 +42,21 @@
     * @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);
    }
    /**
@@ -102,6 +87,7 @@
            }
        }
    }
    /**
     * 停止拉流
     *
@@ -126,4 +112,30 @@
            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 "";
    }
}