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
/* DreemGL is a collaboration between Teeming Society & Samsung Electronics, sponsored by Samsung and others.
   Copyright 2015-2016 Teeming Society. Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License.
   You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and limitations under the License.*/
// WebRTC class

define.class(function(require, exports){
        var WebRTCData = exports
        
        WebRTCData.createOffer = function(){
                var obj = new WebRTCData()
                
                if(typeof webkitRTCPeerConnection === 'undefined') return obj

                obj.pc = new webkitRTCPeerConnection(
                        { "iceServers": [{ "url": "stun:stun.l.google.com:19302" }]
                        }, {optional: [{RtpDataChannels: true}]})

                obj.pc.onicecandidate = function(event){
                        if (!event || !event.candidate) return
                        this.atIceCandidate(event.candidate)
                }.bind(obj)
        
                obj.setChannel(obj.pc.createDataChannel('RTCDataChannel',{reliable:false}))

                obj.pc.createOffer(function(desc){
                        this.pc.setLocalDescription(desc)
                        this.atOffer(desc)
                }.bind(obj), null, obj.media_constraints)

                return obj
        }

        WebRTCData.acceptOffer = function(offer){
                var obj = new WebRTCData()

                obj.pc = new webkitRTCPeerConnection(
                        {"iceServers": [{ "url": "stun:stun.l.google.com:19302" }]},
                        {optional: [{RtpDataChannels: true}]})

                obj.setChannel(obj.pc.createDataChannel('RTCDataChannel', { reliable: false }))

            obj.pc.onicecandidate = function (event) {
                if (!event || !event.candidate) return
                       this.atIceCandidate(event.candidate)
                    //this.pc.addIceCandidate(event.candidate)
            }.bind(obj)

            obj.pc.setRemoteDescription(new RTCSessionDescription(offer))

            obj.pc.createAnswer(function (session){
                    this.pc.setLocalDescription(session)
                this.atAnswer(session)
            }.bind(obj), null, obj.media_constraints)

            return obj
        }

        this.media_constraints = {
                optional: [],
                mandatory: {
                        OfferToReceiveAudio: false, // Hmm!!
                        OfferToReceiveVideo: false // Hmm!!
                }
        }

        this.addCandidate = function(candidate){
                this.pc.addIceCandidate(new RTCIceCandidate(candidate))
        }

        this.acceptAnswer = function(desc){
                this.pc.setRemoteDescription(new RTCSessionDescription(desc))
        }

        this.send = function(msg){
                if(!this.open){
                        if(!this.queue) this.queue = []
                        this.queue.push(msg)
                }
                else this.channel.send(msg)
        }

        // hooks
        this.atOffer = function(){
        }
        
        this.atAnswer = function(){
        }

        this.atIceCandidate = function(){
        }

        this.atOpen = function(){
        }

        this.atClose = function(){
                console.log('webRTC onClose')
        }
        this.atError = function(){
                console.log('webRTC on Error')
        }
        this.atMessage = function(){
        }

        this.setChannel = function(channel){
                this.channel = channel
                channel.onmessage = function(event){
                        this.atMessage(event.data, event.timeStamp)
                }.bind(this)
                channel.onopen = function(){
                        this.open = true
                        this.atOpen.apply(this, arguments)
                }.bind(this)
                channel.onclose = function(){
                        this.open = false
                        this.atClose.apply(this, arguments)
                }.bind(this)
                channel.onerror = function(){
                        this.atError.apply(this, arguments)
                }.bind(this)
        }

        this.acceptOffer = function(offer){
                this.pc.acceptOffer(offer)
        }
})