Search code examples
javavideo-streamingwebrtckurento

Kurento, One to many; being able to change the source


I need to have just one presenter streaming to many viewers but at some point one of these viewer can become the presenter without disconnecting the entire pipeline.

Is that possible? I'm looking at the documentation and examples but I can't see this usecase anywhere


Solution

  • Answering my own question, you can use "DispatcherOneToMany" and then change the source of the dispacher while you want to switch the "Presenter" role. It works like a charm.

    Example creating the dispatcher and adding a new client to it:

        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);
        }
    [...]
    

    And then switch the source while you want, adding a new message for this purpose and doing it in this way:

            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);
        }
    

    I hope this would help someone, happy coding.