我需要只有一个演示者流向许多观众,但在某些时候,其中一个观众可以成为演示者,而不会断开整个管道。
那可能吗?我在看……
回答我自己的问题,您可以使用“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); }
我希望这可以帮助别人,快乐的编码。