0

I need to add video conferencing capabilities to my web site developed using J2EE technology and deployed in JBoss server. After many researches I found WebRTC, but I would like to know how can I use it with jboss since most exemples use it with node.js a simple code example would be soo helpful. Thank you.

Edit
I've tried webRTC but I got some issues on signaling part :
I use webSocket and this is my class code

package com.bean;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.bean.ApplicationScoped;
import javax.websocket.OnError;
import javax.websocket.OnMessage;

import javax.websocket.OnOpen;

import javax.websocket.Session;

import javax.websocket.server.ServerEndpoint;

import org.json.JSONException;

import org.json.JSONObject;

@ApplicationScoped
@ServerEndpoint("/webrtc")
public class WebRTCSocket {

    public static Map < String, Hashtable < String, Object[] >> sessionMap = new Hashtable < String, Hashtable < String, Object[] >> ();

    @SuppressWarnings("unchecked")
    @OnMessage
    public void message(String message, Session session) {
        System.out.println("entre a la methode");
        try {

            JSONObject jsonObject = new JSONObject(message);

            try {

                Object isOpen = jsonObject.get("open");

                if (isOpen != null && (Boolean) isOpen == true) {

                    String channel = (String) jsonObject.get("channel");

                    Object value = sessionMap.get(channel);

                    Hashtable < String, Object[] > sourceDestMap = null;

                    if (value == null) {

                        sourceDestMap = new Hashtable < String, Object[] > ();

                    } else sourceDestMap = (Hashtable < String, Object[] > ) value;

                    sourceDestMap.put(session.getId(), new Object[] {
                        session
                    });

                    sessionMap.put(channel, sourceDestMap);

                }

            } catch (JSONException je) {

                je.printStackTrace();

            }

            try {

                Object dataObj = jsonObject.get("data");

                JSONObject dataMapObj = (JSONObject) dataObj;

                //Object thisUserId = dataMapObj.get("userid");

                String channel = null;

                try {

                    channel = (String) dataMapObj.get("sessionid");

                } catch (JSONException json) {

                    channel = (String) jsonObject.get("channel");

                }

                /*
                JSONObject dataMapObj = (JSONObject) dataObj;

                Object thisUserId = dataMapObj.get("userid");

                String channel = (String) dataMapObj.get("sessionid");

                Hashtable < string, object > sourceDestMap = sessionMap.get(channel);

                if (thisUserId != null && sourceDestMap.get((String) thisUserId) == null) {

                    sourceDestMap.put((String) thisUserId, new Object[] {
                        message, session
                    });

                }

                for (String userId: sourceDestMap.keySet()) {

                    if (!userId.equals(thisUserId)) {

                        Session otherSession = (Session)((Object[]) sourceDestMap.get(userId))[1];

                        otherSession.getBasicRemote().sendText(message);

                    }

                }
                */

                Hashtable < String, Object[] > sourceDestMap = sessionMap.get(channel);

                if (sourceDestMap != null)

                for (String id: sourceDestMap.keySet()) {

                    if (!id.equals(session.getId())) {

                        Session otherSession = (Session)((Object[]) sourceDestMap.get(id))[0];

                        if (otherSession.isOpen())

                        otherSession.getBasicRemote().sendText(dataMapObj.toString());

                    }

                }

            } catch (JSONException je) {

                je.printStackTrace();

            }

        } catch (JSONException  je) {

            je.printStackTrace();

        }
     catch (IOException  je) {

        je.printStackTrace();

    }

        System.out.println("Message received:" + message);

    }

    @OnOpen
    public void open(Session session) {

        System.out.println("Channel opened");

    }

    // need to implement @OnClose too
    @OnError
    public void onError(Throwable error) {
        Logger.getLogger(WebRTCSocket.class.getName()).log(Level.SEVERE, null, error);
    }
}

this is my xhtml page

<html>
    <head>

    </head>

    <body>

          <script >

    var localVideo;
    var remoteVideo;
    var peerConnection;
    var peerConnectionConfig = {'iceServers': [{'url': 'stun:stun.services.mozilla.com'}, {'url': 'stun:stun.l.google.com:19302'}]};

    navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia;
    window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
    window.RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate || window.webkitRTCIceCandidate;
    window.RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription;

    function pageReady() {
        localVideo = document.getElementById('localVideo');
        remoteVideo = document.getElementById('remoteVideo');

        serverConnection = new WebSocket('ws://localhost:8080/jasper/webrtc');
        console.log('serverConnection' + serverConnection.url);

        serverConnection.onmessage = gotMessageFromServer;

        var constraints = {
            video: true,
            audio: true,
        };

        if(navigator.getUserMedia) {
            navigator.getUserMedia(constraints, getUserMediaSuccess, errorHandler);
        } else {
            alert('Your browser does not support getUserMedia API');
        }
    }

    function getUserMediaSuccess(stream) {
        localStream = stream;
        localVideo.src = window.URL.createObjectURL(stream);
    }

    function start(isCaller) {
        peerConnection = new RTCPeerConnection(peerConnectionConfig);
        peerConnection.onicecandidate = gotIceCandidate;
        peerConnection.onaddstream = gotRemoteStream;
        peerConnection.addStream(localStream);

        if(isCaller) {
            peerConnection.createOffer(gotDescription, errorHandler);
        }
    }

    function gotMessageFromServer(event) {
        console.log('gotMessageFromServer' + JSON.parse(event.data));
        if(!peerConnection) start(false);

        var signal = JSON.parse(event.data);
        if(signal.sdp) {
            peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp), function() {
                peerConnection.createAnswer(gotDescription, errorHandler);
            }, errorHandler);
        } else if(signal.ice) {
            peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice));
        }
    }

    function gotIceCandidate(event) {
        if(event.candidate != null) {
            serverConnection.send(JSON.stringify({'ice': event.candidate}));
        }
    }

    function gotDescription(description) {
        console.log('got description');
        peerConnection.setLocalDescription(description, function () {
            serverConnection.send(JSON.stringify({'sdp': description}));
        }, function() {console.log('set description error')});
    }

    function gotRemoteStream(event) {
        console.log('got remote stream');
        remoteVideo.src = window.URL.createObjectURL(event.stream);
    }

    function errorHandler(error) {
        console.log(error);
    }


    </script>

     <video id="localVideo" autoplay="autoplay"  style="width:40%;"></video>
        <video id="remoteVideo" autoplay style="width:40%;"></video>

        <br />

        <input type="button" id="start" onclick="start(true)" value="Start Video"></input>

        <script type="text/javascript">
            pageReady();
        </script>
    </body>
</html>

I use Jboss-as-7.1.0 final so I added this dependency

<dependency>
    <groupId>org.jboss.spec.javax.websocket</groupId>
    <artifactId>jboss-websocket-api_1.0_spec</artifactId>
    <version>1.0.0.Final</version>
    <scope>provided</scope>
</dependency>

in the console i got this error :

WebSocket connection to 'ws://localhost:8080/jasper/webrtc' failed: Error during WebSocket handshake: Unexpected response code: 404

EDIT I discovered that JBoss as 7.1.0 does not support websocket

  • Have you looked at the https://github.com/kurento project? – Michael Gorham Dec 28 '15 at 18:38
  • if you looked closely at the examples you would have noticed the bulk of the code is in client-side, server would merely be used for signalling... – mido Dec 29 '15 at 02:02
  • If you want to have a precise answer you should improve your question. Indeed www.kurento.org is an option, but everything depends on the features you want. Are you requiring point-to-point video calls?, is it groups calls? do you require recording? do you need to interoperate with other RTC mechanisms? All that information is necessary for figuring out what's your best option. – lulop Dec 30 '15 at 16:12
  • Thanks for your answers ! I 've looked at kurento but I think it can't be installed on windows – user3928031 Dec 30 '15 at 20:26
  • is there any way to install kurento on windows ? , @lulop : I need only point-to-point video calls without recording (Just basics) – user3928031 Dec 30 '15 at 20:38
  • For point-to-point video calls you don't need Kurento Media Server nor any other kind of media infrastructure. Point-to-point calls just require some signaling for the exchange of SDPs. Otherwise, Kurenot Media Server cannot be installed in Windows, but your application server can run in Windows a consume the services of a Kurento Media Server installed in another network node. – lulop Jan 01 '16 at 12:20

1 Answers1

0

java code

package org.acode;

import java.util.Hashtable;
import java.util.Map;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.json.JSONException;
import org.json.JSONObject;

@ServerEndpoint("/webrtc")
public class WebRTCSocket {

    public static Map<String, Hashtable<String, Object[]>> sessionMap = new Hashtable<>();

    @OnMessage
    public void message(String message, Session session) {
        try {
            System.out.println("receive message:  " + message);
            JSONObject jsonObject = new JSONObject(message);
            String channel = null;
            JSONObject dataMapObj = null;

            Hashtable<String, Object[]> sourceDestMap = null;

            try {
                boolean isOpen = jsonObject.optBoolean("open");
                channel = (String) jsonObject.get("channel");
                sourceDestMap = (Hashtable<String, Object[]>) sessionMap.get(channel);
                if (sourceDestMap == null) {
                    sourceDestMap = new Hashtable<String, Object[]>();
                }
                if (isOpen) {
                    /* Открытие канала */
                    sourceDestMap.put(session.getId(), new Object[]{session});
                    sessionMap.put(channel, sourceDestMap);
                } else {
                    /*Остальные переговоры*/
                    dataMapObj = jsonObject.optJSONObject("data");
                    if (dataMapObj != null) {
                        sourceDestMap = sessionMap.get(channel);
                        for (String id : sourceDestMap.keySet()) {
                            if (!id.equals(session.getId())) {
                                Session otherSession = (Session) ((Object[]) sourceDestMap.get(id))[0];
                                if (otherSession.isOpen()) {
                                    otherSession.getBasicRemote().sendText(dataMapObj.toString());
                                }
                            }
                        }
                    }
                }
            } catch (Exception je) {
                je.printStackTrace();
            }
        } catch (JSONException je) {
            je.printStackTrace();
        }
    }

    @OnOpen
    public void open(Session session
    ) {
        System.out.println("Channel opened");
    }

    // need to implement @OnClose too
}

js openSignallingChannel

var SIGNALING_SERVER = "ws://192.168.1.46:8080/webrtc";
    connection.openSignalingChannel = function (config) {
        config.channel = config.channel || this.channel;
        var websocket = new WebSocket(SIGNALING_SERVER);
        websocket.channel = config.channel;
        websocket.onopen = function () {
            websocket.push(JSON.stringify({
                open: true,
                channel: config.channel
            }));
            if (config.callback)
                config.callback(websocket);
        };
        websocket.onmessage = function (event) {
            config.onmessage(JSON.parse(event.data));
        };
        websocket.push = websocket.send;
        websocket.send = function (data) {
            websocket.push(JSON.stringify({
                data: data,
                channel: config.channel
            }));
        };
    };