List of usage examples for com.google.gson JsonObject toString
@Override
public String toString()
From source file:com.elasticrtc.tutorial.one2one.ws.CallHandler.java
License:Apache License
private void incomingCallResponse(final UserSession callee, JsonObject jsonMessage) throws IOException { String callResponse = jsonMessage.get("callResponse").getAsString(); String from = jsonMessage.get("from").getAsString(); final UserSession calleer = registry.getByName(from); String to = calleer.getCallingTo(); if ("accept".equals(callResponse)) { log.debug("Accepted call from '{}' to '{}'", from, to); CallMediaPipeline pipeline = null; try {//from w ww.j a v a2 s . co m pipeline = new CallMediaPipeline(kurento); pipelines.put(calleer.getSessionId(), pipeline); pipelines.put(callee.getSessionId(), pipeline); callee.setWebRtcEndpoint(pipeline.getCalleeWebRtcEp()); pipeline.getCalleeWebRtcEp() .addIceCandidateFoundListener(new EventListener<IceCandidateFoundEvent>() { @Override public void onEvent(IceCandidateFoundEvent event) { JsonObject response = new JsonObject(); response.addProperty("id", "iceCandidate"); response.add("candidate", JsonUtils.toJsonObject(event.getCandidate())); try { synchronized (callee.getSession()) { callee.getSession().sendMessage(new TextMessage(response.toString())); } } catch (IOException e) { log.debug(e.getMessage()); } } }); calleer.setWebRtcEndpoint(pipeline.getCallerWebRtcEp()); pipeline.getCallerWebRtcEp() .addIceCandidateFoundListener(new EventListener<IceCandidateFoundEvent>() { @Override public void onEvent(IceCandidateFoundEvent event) { JsonObject response = new JsonObject(); response.addProperty("id", "iceCandidate"); response.add("candidate", JsonUtils.toJsonObject(event.getCandidate())); try { synchronized (calleer.getSession()) { calleer.getSession().sendMessage(new TextMessage(response.toString())); } } catch (IOException e) { log.debug(e.getMessage()); } } }); String calleeSdpOffer = jsonMessage.get("sdpOffer").getAsString(); String calleeSdpAnswer = pipeline.generateSdpAnswerForCallee(calleeSdpOffer); JsonObject startCommunication = new JsonObject(); startCommunication.addProperty("id", "startCommunication"); startCommunication.addProperty("sdpAnswer", calleeSdpAnswer); synchronized (callee) { callee.sendMessage(startCommunication); } pipeline.getCalleeWebRtcEp().gatherCandidates(); String callerSdpOffer = registry.getByName(from).getSdpOffer(); String callerSdpAnswer = pipeline.generateSdpAnswerForCaller(callerSdpOffer); JsonObject response = new JsonObject(); response.addProperty("id", "callResponse"); response.addProperty("response", "accepted"); response.addProperty("sdpAnswer", callerSdpAnswer); synchronized (calleer) { calleer.sendMessage(response); } pipeline.getCallerWebRtcEp().gatherCandidates(); } catch (Throwable t) { log.error(t.getMessage(), t); if (pipeline != null) { pipeline.release(); } pipelines.remove(calleer.getSessionId()); pipelines.remove(callee.getSessionId()); JsonObject response = new JsonObject(); response.addProperty("id", "callResponse"); response.addProperty("response", "rejected"); calleer.sendMessage(response); response = new JsonObject(); response.addProperty("id", "stopCommunication"); callee.sendMessage(response); } } else { JsonObject response = new JsonObject(); response.addProperty("id", "callResponse"); response.addProperty("response", "rejected"); calleer.sendMessage(response); } }
From source file:com.elasticrtc.tutorial.one2one.ws.UserSession.java
License:Apache License
public void sendMessage(JsonObject message) throws IOException { log.debug("Sending message from user '{}': {}", name, message); session.sendMessage(new TextMessage(message.toString())); }
From source file:com.elasticrtc.tutorial.player.ws.PlayerHandler.java
License:Apache License
private void start(final WebSocketSession session, JsonObject jsonMessage) { // 1. Media pipeline final UserSession user = new UserSession(); MediaPipeline pipeline = kurento.createMediaPipeline(); user.setMediaPipeline(pipeline);//from w w w .j a v a 2 s . c om WebRtcEndpoint webRtcEndpoint = new WebRtcEndpoint.Builder(pipeline).build(); user.setWebRtcEndpoint(webRtcEndpoint); String videourl = jsonMessage.get("videourl").getAsString(); final PlayerEndpoint playerEndpoint = new PlayerEndpoint.Builder(pipeline, videourl).build(); user.setPlayerEndpoint(playerEndpoint); users.put(session.getId(), user); playerEndpoint.connect(webRtcEndpoint); // 2. WebRtcEndpoint // ICE candidates webRtcEndpoint.addIceCandidateFoundListener(new EventListener<IceCandidateFoundEvent>() { @Override public void onEvent(IceCandidateFoundEvent 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. PlayEndpoint playerEndpoint.addErrorListener(new EventListener<ErrorEvent>() { @Override public void onEvent(ErrorEvent event) { log.info("ErrorEvent: {}", event.getDescription()); sendPlayEnd(session); } }); playerEndpoint.addEndOfStreamListener(new EventListener<EndOfStreamEvent>() { @Override public void onEvent(EndOfStreamEvent event) { log.info("EndOfStreamEvent: {}", event.getTimestamp()); sendPlayEnd(session); } }); playerEndpoint.play(); }
From source file:com.elasticrtc.tutorial.player.ws.PlayerHandler.java
License:Apache License
private void resume(final WebSocketSession session) { UserSession user = users.get(session.getId()); if (user != null) { user.getPlayerEndpoint().play(); VideoInfo videoInfo = user.getPlayerEndpoint().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()); }/*w w w.j av a2 s .c o m*/ }
From source file:com.elasticrtc.tutorial.player.ws.PlayerHandler.java
License:Apache License
private void doSeek(final WebSocketSession session, JsonObject jsonMessage) { UserSession user = users.get(session.getId()); if (user != null) { try {// w w w . j a v a 2 s . c o m user.getPlayerEndpoint().setPosition(jsonMessage.get("position").getAsLong()); } catch (KurentoException e) { log.debug("The seek cannot be performed"); JsonObject response = new JsonObject(); response.addProperty("id", "seek"); response.addProperty("message", "Seek failed"); sendMessage(session, response.toString()); } } }
From source file:com.elasticrtc.tutorial.player.ws.PlayerHandler.java
License:Apache License
private void getPosition(final WebSocketSession session) { UserSession user = users.get(session.getId()); if (user != null) { long position = user.getPlayerEndpoint().getPosition(); JsonObject response = new JsonObject(); response.addProperty("id", "position"); response.addProperty("position", position); sendMessage(session, response.toString()); }/*from w ww . j a va 2s. co m*/ }
From source file:com.elasticrtc.tutorial.player.ws.PlayerHandler.java
License:Apache License
public void sendPlayEnd(WebSocketSession session) { if (users.containsKey(session.getId())) { JsonObject response = new JsonObject(); response.addProperty("id", "playEnd"); sendMessage(session, response.toString()); }/*from w w w . j a v a2 s . c o m*/ }
From source file:com.elasticrtc.tutorial.player.ws.PlayerHandler.java
License:Apache License
private void sendError(WebSocketSession session, String message) { if (users.containsKey(session.getId())) { JsonObject response = new JsonObject(); response.addProperty("id", "error"); response.addProperty("message", message); sendMessage(session, response.toString()); }//from w w w . ja v a 2 s. c o m }
From source file:com.elasticrtc.tutorial.recording.ws.RecorderHandler.java
License:Apache License
private void start(final WebSocketSession session, JsonObject jsonMessage) { try {// w ww . j ava 2 s. c o m // 1. Media logic (webRtcEndpoint in loopback) MediaPipeline pipeline = kurento.createMediaPipeline(); WebRtcEndpoint webRtcEndpoint = new WebRtcEndpoint.Builder(pipeline).build(); webRtcEndpoint.connect(webRtcEndpoint); MediaProfileSpecType profile = getMediaProfileFromMessage(jsonMessage); RecorderEndpoint recorder = new RecorderEndpoint.Builder(pipeline, RECORDER_FILE_PATH) .withMediaProfile(profile).build(); recorder.addRecordingListener(new EventListener<RecordingEvent>() { @Override public void onEvent(RecordingEvent event) { JsonObject response = new JsonObject(); response.addProperty("id", "recording"); try { synchronized (session) { session.sendMessage(new TextMessage(response.toString())); } } catch (IOException e) { log.error(e.getMessage()); } } }); recorder.addStoppedListener(new EventListener<StoppedEvent>() { @Override public void onEvent(StoppedEvent event) { JsonObject response = new JsonObject(); response.addProperty("id", "stopped"); try { synchronized (session) { session.sendMessage(new TextMessage(response.toString())); } } catch (IOException e) { log.error(e.getMessage()); } } }); recorder.addPausedListener(new EventListener<PausedEvent>() { @Override public void onEvent(PausedEvent event) { JsonObject response = new JsonObject(); response.addProperty("id", "paused"); try { synchronized (session) { session.sendMessage(new TextMessage(response.toString())); } } catch (IOException e) { log.error(e.getMessage()); } } }); connectAccordingToProfile(webRtcEndpoint, recorder, profile); // 2. Store user session UserSession user = new UserSession(session); user.setMediaPipeline(pipeline); user.setWebRtcEndpoint(webRtcEndpoint); user.setRecorderEndpoint(recorder); registry.register(user); // 3. SDP negotiation String sdpOffer = jsonMessage.get("sdpOffer").getAsString(); String sdpAnswer = webRtcEndpoint.processOffer(sdpOffer); // 4. Gather ICE candidates webRtcEndpoint.addIceCandidateFoundListener(new EventListener<IceCandidateFoundEvent>() { @Override public void onEvent(IceCandidateFoundEvent 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.error(e.getMessage()); } } }); JsonObject response = new JsonObject(); response.addProperty("id", "startResponse"); response.addProperty("sdpAnswer", sdpAnswer); synchronized (user) { session.sendMessage(new TextMessage(response.toString())); } webRtcEndpoint.gatherCandidates(); recorder.record(); } catch (Throwable t) { log.error("Start error", t); sendError(session, t.getMessage()); } }
From source file:com.elasticrtc.tutorial.recording.ws.RecorderHandler.java
License:Apache License
private void play(UserSession user, final WebSocketSession session, JsonObject jsonMessage) { try {// w w w .j a v a 2 s. c o m // 1. Media logic final MediaPipeline pipeline = kurento.createMediaPipeline(); WebRtcEndpoint webRtcEndpoint = new WebRtcEndpoint.Builder(pipeline).build(); PlayerEndpoint player = new PlayerEndpoint.Builder(pipeline, RECORDER_FILE_PATH).build(); player.connect(webRtcEndpoint); // Player listeners player.addErrorListener(new EventListener<ErrorEvent>() { @Override public void onEvent(ErrorEvent event) { log.info("ErrorEvent for session '{}': {}", session.getId(), event.getDescription()); sendPlayEnd(session, pipeline); } }); player.addEndOfStreamListener(new EventListener<EndOfStreamEvent>() { @Override public void onEvent(EndOfStreamEvent event) { log.info("EndOfStreamEvent for session '{}'", session.getId()); sendPlayEnd(session, pipeline); } }); // 2. Store user session user.setMediaPipeline(pipeline); user.setWebRtcEndpoint(webRtcEndpoint); // 3. SDP negotiation String sdpOffer = jsonMessage.get("sdpOffer").getAsString(); String sdpAnswer = webRtcEndpoint.processOffer(sdpOffer); JsonObject response = new JsonObject(); response.addProperty("id", "playResponse"); response.addProperty("sdpAnswer", sdpAnswer); // 4. Gather ICE candidates webRtcEndpoint.addIceCandidateFoundListener(new EventListener<IceCandidateFoundEvent>() { @Override public void onEvent(IceCandidateFoundEvent 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.error(e.getMessage()); } } }); // 5. Play recorded stream player.play(); synchronized (session) { session.sendMessage(new TextMessage(response.toString())); } webRtcEndpoint.gatherCandidates(); } catch (Throwable t) { log.error("Play error", t); sendError(session, t.getMessage()); } }