summaryrefslogtreecommitdiffstats
path: root/webapp/client/webrtc_client.jsx
blob: 85204c25b2de7bae107d9eaf86710da0267e0988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

export default class WebrtcClient {
    constructor() {
        this.init = this.init.bind(this);
        this.listDevices = this.listDevices.bind(this);
        this.isExtensionEnabled = this.isExtensionEnabled.bind(this);
        this.isWebrtcSupported = this.isWebrtcSupported.bind(this);

        this.initDone = false;
        this.sessions = {};
    }

    noop() {} //eslint-disable-line no-empty-function

    // this function is going to be needed when we enable screen sharing
    isExtensionEnabled() {
        if (window.navigator.userAgent.match('Chrome')) {
            const chromever = parseInt(window.navigator.userAgent.match(/Chrome\/(.*) /)[1], 10);
            let maxver = 33;
            if (window.navigator.userAgent.match('Linux')) {
                maxver = 35;	// "known" crash in chrome 34 and 35 on linux
            }
            if (chromever >= 26 && chromever <= maxver) {
                // Older versions of Chrome don't support this extension-based approach, so lie
                return true;
            }
            return (document.getElementById('mattermost-extension-installed') !== null);
        }

        // Firefox of others, no need for the extension (but this doesn't mean it will work)
        return true;
    }

    init(opts) {
        const options = opts || {};

        if (this.initDone === true) {
            // Already initialized
            return;
        }

        this.trace = this.noop;
        this.debug = this.noop;
        this.log = this.noop;
        this.warn = this.noop;
        this.error = this.noop;

        /* eslint-disable */
        if (options.debug === true || options.debug === 'all') {
            // Enable all debugging levels
            this.trace = console.trace.bind(console);
            this.debug = console.debug.bind(console);
            this.log = console.log.bind(console);
            this.warn = console.warn.bind(console);
            this.error = console.error.bind(console);
        } else if (Array.isArray(options.debug)) {
            for (const i in options.debug) {
                if (options.debug.hasOwnProperty(i)) {
                    const d = options.debug[i];
                    switch (d) {
                    case 'trace':
                        this.trace = console.trace.bind(console);
                        break;
                    case 'debug':
                        this.debug = console.debug.bind(console);
                        break;
                    case 'log':
                        this.log = console.log.bind(console);
                        break;
                    case 'warn':
                        this.warn = console.warn.bind(console);
                        break;
                    case 'error':
                        this.error = console.error.bind(console);
                        break;
                    default:
                        console.error("Unknown debugging option '" + d + "' (supported: 'trace', 'debug', 'log', warn', 'error')");
                        break;
                    }
                }
            }
        }
        /* eslint-enable */

        this.log('Initializing WebRTC Client library');

        // Detect tab close
        window.onbeforeunload = () => {
            this.log('Closing window');
            for (const s in this.sessions) {
                if (this.sessions.hasOwnProperty(s)) {
                    if (this.sessions[s] && this.sessions[s].destroyOnUnload) {
                        this.log('Destroying session ' + s);
                        this.sessions[s].destroy();
                    }
                }
            }
        };

        this.initDone = true;
    }

    // Helper method to enumerate devices
    listDevices(cb) {
        const callback = (typeof cb == 'function') ? cb : this.noop;

        if (navigator.mediaDevices) {
            navigator.getUserMedia({audio: true, video: true}, (stream) => {
                navigator.mediaDevices.enumerateDevices().then((devices) => {
                    this.debug(devices);
                    callback(devices);

                    // Get rid of the now useless stream
                    try {
                        stream.stop();
                    } catch (e) {
                        this.error(e);
                    }

                    this.stopMedia(stream);
                });
            }, (err) => {
                this.error(err);
                callback([]);
            });
        } else {
            this.warn('navigator.mediaDevices unavailable');
            callback([]);
        }
    }

    // Helper method to check whether WebRTC is supported by this browser
    isWebrtcSupported() {
        return window.RTCPeerConnection && navigator.getUserMedia;
    }

    stopMedia(stream) {
        const tracks = stream.getTracks();
        tracks.forEach((track) => {
            track.stop();
        });
    }
}