库伦托,一对多;能够改变来源

时间:2018-09-14 14:52:56

标签: java video-streaming webrtc kurento

我只需要一个演示者流式传输给许多观众,但是在某些时候,这些观众中的一个可以成为演示者而无需断开整个管道。

有可能吗?我正在查看文档和示例,但在任何地方都看不到该用例

1 个答案:

答案 0 :(得分:2)

回答我自己的问题,您可以使用“ DispatcherOneToMany”,然后在要切换“ Presenter”角色时更改分发程序的来源。它像一种魅力。

创建调度程序并为其添加新客户端的示例:

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

我希望这可以对编码愉快的人有所帮助。