@Override
public synchronized void onContentRequest(
WebRtcContentSession contentSession) throws Exception {
if (firstWebRtcEndpoint == null) {
// Media Pipeline creation
MediaPipeline mp = contentSession.getMediaPipelineFactory()
.create();
contentSession.releaseOnTerminate(mp);
// First WebRTC enpoint in loopback
firstWebRtcEndpoint = mp.newWebRtcEndpoint().build();
sessionId = contentSession.getSessionId();
contentSession.releaseOnTerminate(firstWebRtcEndpoint);
firstWebRtcEndpoint.connect(firstWebRtcEndpoint);
contentSession.start(firstWebRtcEndpoint);
} else {
// Media Pipeline reusing
MediaPipeline mp = firstWebRtcEndpoint.getMediaPipeline();
// Next WebRTC endpoints connected to the first one
WebRtcEndpoint newWebRtcEndpoint = mp.newWebRtcEndpoint().build();
contentSession.releaseOnTerminate(newWebRtcEndpoint);
newWebRtcEndpoint.connect(firstWebRtcEndpoint);
firstWebRtcEndpoint.connect(newWebRtcEndpoint);
contentSession.start(newWebRtcEndpoint);