代码之家  ›  专栏  ›  技术社区  ›  forlayo

库兰托,一对多;能够改变源头

  •  1
  • forlayo  · 技术社区  · 7 年前

    我只需要有一个演示者流到许多观众,但在某些时候,这些观众之一可以成为演示者没有断开整个管道。

    1 回复  |  直到 7 年前
        1
  •  3
  •   forlayo    7 年前

    回答我自己的问题,您可以使用“dispatchentomany”,然后在切换“Presenter”角色时更改dispatcher的源。它就像一个符咒。

    创建dispatcher并向其中添加新客户端的示例:

        private void start(final WebSocketSession session, JsonObject jsonMessage)
    {
        // ---- Media pipeline
        log.info("[Handler::start] Adding a new client!");
    
        final UserSession user = new UserSession();
        users.put(session.getId(), user);
    
        if(pipeline==null){
            log.info("[Handler::start] Create Media Pipeline");
            pipeline = kurento.createMediaPipeline();
            dispatcher = new DispatcherOneToMany.Builder(pipeline).build();
        }
    
        final WebRtcEndpoint webRtcEp = new WebRtcEndpoint.Builder(pipeline).build();
        user.setWebRtcEndpoint(webRtcEp);
    
        HubPort hubPort = new HubPort.Builder(dispatcher).build();
        user.setHubPort(hubPort);
        hubPort.connect(webRtcEp);
        webRtcEp.connect(hubPort);
    
        if(users.size()==1) {
            log.info("[Handler::start] It's first user, then set it as source");
            dispatcher.setSource(hubPort);
        }
    [...]
    

    然后根据需要切换源,为此目的添加新消息并按以下方式执行:

            UserSession user = users.get(sessionId);
    
        if (user != null) {
            log.info("[Handler::presenterSwitch] Switching presenter to: {} ", sessionId);
            dispatcher.setSource(user.getHubPort());
        }else{
            log.error("[Handler::presenterSwitch] Trying to switch to an no-existent session: {}", sessionId);
        }
    

    我希望这能帮助别人,快乐编码。