private void start(final WebSocketSession session, JsonObject jsonMessage)
{
// 1. Media pipeline
final UserSession user = new UserSession();
MediaPipeline pipeline = kurento.createMediaPipeline();
user.setMediaPipeline(pipeline);
WebRtcEndpoint webRtcEndpoint = new WebRtcEndpoint.Builder(pipeline).build();
user.setWebRtcEndpoint(webRtcEndpoint);
RtpEndpoint rtpEndpoint = new RtpEndpoint.Builder(pipeline).build();
user.setRtpEndpoint(rtpEndpoint);
//String videourl = jsonMessage.get("videourl").getAsString();
final PlayerEndpoint playerEndpoint = new PlayerEndpoint.Builder(pipeline, URL_BARCODES/*videourl*/).build();
user.setPlayerEndpoint(playerEndpoint);
users.put(session.getId(), user);
String requestSdp = "v=0\r\n"
+ "o=- 12345 12345 IN IP4 127.0.0.1\r\n"
+ "s=-\r\n"
+ "c=IN IP4 127.0.0.01r\n"
+ "t=0 0\r\n"
+ "m=video 5000 RTP/AVP 96 97 98\r\n"
+ "a=rtpmap:96 H264/90000\r\n"
+ "a=rtpmap:97 MP4V-ES/90000\r\n"
+ "a=rtpmap:98 H263-1998/90000\r\n"
+ "a=recvonly\r\n"
+ "b=AS:384\r\n";
rtpEndpoint.processOffer(requestSdp);
rtpEndpoint.connect(webRtcEndpoint);
playerEndpoint.connect(rtpEndpoint, MediaType.VIDEO);
// 2. WebRtcEndpoint
// ICE candidates
webRtcEndpoint.addOnIceCandidateListener(new EventListener<OnIceCandidateEvent>() {
@Override
public void onEvent(OnIceCandidateEvent event) {
JsonObject response = new JsonObject();
response.addProperty("id", "iceCandidate");
response.add("candidate", JsonUtils.toJsonObject(event.getCandidate()));
try {
synchronized (session) {
session.sendMessage(new TextMessage(response.toString()));
}
} catch (IOException e) {
log.debug(e.getMessage());
}
}
});
String sdpOffer = jsonMessage.get("sdpOffer").getAsString();
String sdpAnswer = webRtcEndpoint.processOffer(sdpOffer);
JsonObject response = new JsonObject();
response.addProperty("id", "startResponse");
response.addProperty("sdpAnswer", sdpAnswer);
sendMessage(session, response.toString());
webRtcEndpoint.addMediaStateChangedListener(new EventListener<MediaStateChangedEvent>() {
@Override
public void onEvent(MediaStateChangedEvent event) {
if (event.getNewState() == MediaState.CONNECTED) {
VideoInfo videoInfo = playerEndpoint.getVideoInfo();
JsonObject response = new JsonObject();
response.addProperty("id", "videoInfo");
response.addProperty("isSeekable", videoInfo.getIsSeekable());
response.addProperty("initSeekable", videoInfo.getSeekableInit());
response.addProperty("endSeekable", videoInfo.getSeekableEnd());
response.addProperty("videoDuration", videoInfo.getDuration());
sendMessage(session, response.toString());
}
}
});
webRtcEndpoint.gatherCandidates();
// 3. RtpEndpoint
rtpEndpoint.addErrorListener(new EventListener<ErrorEvent>() {
@Override
public void onEvent(ErrorEvent event) {
log.info("ErrorEvent: {}", event.getDescription());
sendRtpEvent(session);
}
});
// 3. PlayEndpoint
playerEndpoint.addErrorListener(new EventListener<ErrorEvent>() {
@Override
public void onEvent(ErrorEvent event) {
log.info("ErrorEvent: {}", event.getDescription());
sendPlayErr(session);
}
});
playerEndpoint.addEndOfStreamListener(new EventListener<EndOfStreamEvent>() {
@Override
public void onEvent(EndOfStreamEvent event) {
log.info("EndOfStreamEvent: {}", event.getTimestamp());
sendPlayEnd(session);
}
});
playerEndpoint.play();
}