<commit_before>/**
 * Created by Quentin on 20/07/14.
 */
'use strict';

angular.module('mean.system').
    factory('WebRTC', ['Global', 'mySocket', '$window', '$sce', function (Global, mySocket, $window, $sce) {
        var self = this;
        var SIGNALING_SERVER = '/',
            defaultChannel = 'default-channel';
        self.socket = null;
        var connection = new RTCMultiConnection(defaultChannel);
        var observerCallbacks = [];
        var notifyObservers = function(){
            angular.forEach(observerCallbacks, function(callback){
                callback();
            });
        };

        self.videoStream = null;
        self.audioStream = null;
        self.screenStream = null;

        self.users = [];
        self.myuser = null;
        self.screen = null;
        self.mysock = null;
        self.fake_screen = false;
        connection.session = {
            audio: true,
            video: true
        };

        //Clean : check if we need the id variable.
        connection.extra = {
            username: Global.user.username,
            fullname: Global.user.name,
            id: Global.user._id
        };

        self.switchPresenter = function(id) {

        }


//        var onMessageCallbacks = {};
//        var socketio = io.connect(SIGNALING_SERVER);
//
//        socketio.on('message', function(data) {
//            if(data.sender == connection.userid) return;
//            onMessageCallbacks[data.channel](data.message);
//            console.log(data.message);
////            if (onMessageCallbacks[data.channel]) {
////                onMessageCallbacks[data.channel](data.message);
////            };
//        });

//        connection.openSignalingChannel = function (config) {
//            var channel = config.channel || this.channel;
//            onMessageCallbacks[channel] = config.onmessage;
//
//            if (config.onopen) setTimeout(config.onopen, 1000);
//            return {
//                send: function (message) {
//                    socketio.emit('message', {
//                        sender: connection.userid,
//                        username: connection.extra.fullname,
//                        channel: channel,
//                        message: message
//                    });
//                },
//                channel: channel
//            };
//        };


        connection.openSignalingChannel = function(config) {
            var channel = config.channel || defaultChannel;
            var sender = Global.user._id;
            console.log("client wants channel : " + channel);
            io.connect(SIGNALING_SERVER).emit('new-channel', {
                channel: channel,
                sender: sender
            });

            var socket = io.connect(SIGNALING_SERVER + channel);
            socket.channel = channel;
            socket.on('connect', function() {
                if (config.callback) config.callback(socket);
            });




            socket.send = function(message) {
                socket.emit('message', {
                    sender: sender,
                    data: message
                });
            };
            socket.on('message', config.onmessage);

            connection.socket = socket;
        };


        var openCustomActionsChannel = function(channel, connection)
        {
            io.connect(SIGNALING_SERVER).emit('new-custom-channel', {
                channel: channel,
                sender:  Global.user._id
            });

            self.mysock = io.connect(SIGNALING_SERVER + channel, {custom : true});
            self.mysock.setPresenter = function(id){self.mysock.emit('setPresenter', {
                id: id
            })}

            self.mysock.on('presenterGiven', function(id){
                console.log(connection);
                if (id === Global.user._id)
                {
                    connection.addStream({
                        screen: true,
                        oneway: true})
                }
            });
        }


        var onOpen = function (roomId) {
            console.log("---> onOpen() triggered- Room created with id:" + roomId);
            connection.open(roomId);
            openCustomActionsChannel(roomId, connection);
            console.log("current session :");
            console.log(connection.sessionid);
        };

        var onJoin = function(roomId)
        {
            console.log("---> join() called - joined room: "+ roomId);
            connection.join(roomId);
            openCustomActionsChannel(roomId, connection);
            console.log("current session :");
            console.log(connection.sessionid);
        }

        connection.onstream = function(e) {
            console.log(e);

            if((e.type == 'local' || e.type == 'remote') && e.isScreen) {
                var url = $window.URL.createObjectURL(e.stream);
                console.log("!!!! SCREEN !!!!");
                self.screen = {'id': e.extra.id, 'username': e.extra.username, 'stream-type': 'screen', 'stream': $sce.trustAsResourceUrl(url)};
                notifyObservers();
                if (e.type == 'local'){
                    self.screenStream = e.streamid;
                     console.log("video stream : " + self.videoStream + " / audio stream : " + self.audioStream + " / srean stream : " + self.screenStream);
               }
            }
            else if (e.type === 'local') {
                if (e.isAudio){
                    self.audioStream = e.streamid;
                }
                if (e.isVideo){
                    self.videoStream = e.streamid;
                }
                if (e.isScreen){
                    self.screenStream = e.streamid;
                }
                console.log("video stream : " + self.videoStream + " / audio stream : " + self.audioStream + " / srean stream : " + self.screenStream);
                var url = $window.URL.createObjectURL(e.stream);
                self.myuser = {'id' : e.extra.id, 'username' : e.extra.username, 'stream' : $sce.trustAsResourceUrl(url)};
                notifyObservers();
               
            }
            else if (e.type === 'remote') {
                var url = $window.URL.createObjectURL(e.stream);
                self.users.push({'id': e.extra.id, 'username': e.extra.username, 'stream-type': 'video', 'stream': $sce.trustAsResourceUrl(url)});
                notifyObservers();
            }
        };

        connection.onstreamended = function(e) {
            if (e.type == 'local'){
                if (e.isAudio){
                    self.audioStream = null;
                }
                if (e.isVideo){
                    self.videoStream = null;
                }
                if (e.isScreen){
                    self.screenStream = null;
                }                    
            }
        }
<commit_msg>Remove share screen when we change the presenter<commit_after>/**
 * Created by Quentin on 20/07/14.
 */
'use strict';

angular.module('mean.system').
    factory('WebRTC', ['Global', 'mySocket', '$window', '$sce', function (Global, mySocket, $window, $sce) {
        var self = this;
        var SIGNALING_SERVER = '/',
            defaultChannel = 'default-channel';
        self.socket = null;
        var connection = new RTCMultiConnection(defaultChannel);
        var observerCallbacks = [];
        var notifyObservers = function(){
            angular.forEach(observerCallbacks, function(callback){
                callback();
            });
        };

        self.videoStream = null;
        self.audioStream = null;
        self.screenStream = null;

        self.users = [];
        self.myuser = null;
        self.screen = null;
        self.mysock = null;
        self.fake_screen = false;
        connection.session = {
            audio: true,
            video: true
        };

        //Clean : check if we need the id variable.
        connection.extra = {
            username: Global.user.username,
            fullname: Global.user.name,
            id: Global.user._id
        };

        self.switchPresenter = function(id) {

        }


//        var onMessageCallbacks = {};
//        var socketio = io.connect(SIGNALING_SERVER);
//
//        socketio.on('message', function(data) {
//            if(data.sender == connection.userid) return;
//            onMessageCallbacks[data.channel](data.message);
//            console.log(data.message);
////            if (onMessageCallbacks[data.channel]) {
////                onMessageCallbacks[data.channel](data.message);
////            };
//        });

//        connection.openSignalingChannel = function (config) {
//            var channel = config.channel || this.channel;
//            onMessageCallbacks[channel] = config.onmessage;
//
//            if (config.onopen) setTimeout(config.onopen, 1000);
//            return {
//                send: function (message) {
//                    socketio.emit('message', {
//                        sender: connection.userid,
//                        username: connection.extra.fullname,
//                        channel: channel,
//                        message: message
//                    });
//                },
//                channel: channel
//            };
//        };


        connection.openSignalingChannel = function(config) {
            var channel = config.channel || defaultChannel;
            var sender = Global.user._id;
            console.log("client wants channel : " + channel);
            io.connect(SIGNALING_SERVER).emit('new-channel', {
                channel: channel,
                sender: sender
            });

            var socket = io.connect(SIGNALING_SERVER + channel);
            socket.channel = channel;
            socket.on('connect', function() {
                if (config.callback) config.callback(socket);
            });




            socket.send = function(message) {
                socket.emit('message', {
                    sender: sender,
                    data: message
                });
            };
            socket.on('message', config.onmessage);

            connection.socket = socket;
        };


        var openCustomActionsChannel = function(channel, connection)
        {
            io.connect(SIGNALING_SERVER).emit('new-custom-channel', {
                channel: channel,
                sender:  Global.user._id
            });

            self.mysock = io.connect(SIGNALING_SERVER + channel, {custom : true});
            self.mysock.setPresenter = function(id){
                if (self.screenStream !== null){
                    connection.removeStream(self.screenStream);
                }
                self.mysock.emit('setPresenter', {
                    id: id
                })
            }

            self.mysock.on('presenterGiven', function(id){
                console.log(connection);
                if (id === Global.user._id){
                    if (self.screenStream == null){
                        connection.addStream({
                            screen: true,
                            oneway: true});
                    }
                }
            });
        }


        var onOpen = function (roomId) {
            console.log("---> onOpen() triggered- Room created with id:" + roomId);
            connection.open(roomId);
            openCustomActionsChannel(roomId, connection);
            console.log("current session :");
            console.log(connection.sessionid);
        };

        var onJoin = function(roomId)
        {
            console.log("---> join() called - joined room: "+ roomId);
            connection.join(roomId);
            openCustomActionsChannel(roomId, connection);
            console.log("current session :");
            console.log(connection.sessionid);
        }

        connection.onstream = function(e) {
            console.log(e);

            if((e.type == 'local' || e.type == 'remote') && e.isScreen) {
                var url = $window.URL.createObjectURL(e.stream);
                console.log("!!!! SCREEN !!!!");
                self.screen = {'id': e.extra.id, 'username': e.extra.username, 'stream-type': 'screen', 'stream': $sce.trustAsResourceUrl(url)};
                notifyObservers();
                if (e.type == 'local'){
                    self.screenStream = e.streamid;
                     console.log("video stream : " + self.videoStream + " / audio stream : " + self.audioStream + " / srean stream : " + self.screenStream);
               }
            }
            else if (e.type === 'local') {
                if (e.isAudio){
                    self.audioStream = e.streamid;
                }
                if (e.isVideo){
                    self.videoStream = e.streamid;
                }
                if (e.isScreen){
                    self.screenStream = e.streamid;
                }
                console.log("video stream : " + self.videoStream + " / audio stream : " + self.audioStream + " / srean stream : " + self.screenStream);
                var url = $window.URL.createObjectURL(e.stream);
                self.myuser = {'id' : e.extra.id, 'username' : e.extra.username, 'stream' : $sce.trustAsResourceUrl(url)};
                notifyObservers();
               
            }
            else if (e.type === 'remote') {
                var url = $window.URL.createObjectURL(e.stream);
                self.users.push({'id': e.extra.id, 'username': e.extra.username, 'stream-type': 'video', 'stream': $sce.trustAsResourceUrl(url)});
                notifyObservers();
            }
        };

        connection.onstreamended = function(e) {
            console.log("stream removed : type : " + e.type );
            if (e.type === 'local'){
                if (e.isAudio){
                    console.log("audio");
                    self.audioStream = null;
                }
                if (e.isVideo){
                    console.log("video");
                    self.videoStream = null;
                }
                if (e.isScreen){
                    console.log("screen");
                    self.screenStream = null;
                }                    
            }
        }

{'fraction_non_alphanumeric': 0.12897810729523676, 'fraction_numerical': 0.0013988948730502902, 'mean_word_length': 1.3605745418524022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2209271', 'n_tokens_mistral': 3768, 'n_tokens_neox': 3569, 'n_words': 678}