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
|
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)
}.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,
OfferToReceiveVideo: false
}
}
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)
}
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)
}
}) |