Repository navigation
Expand file tree
/
Copy pathconference.js
More file actions
111 lines (73 loc) · 2.39 KB
/
Copy pathconference.js
File metadata and controls
111 lines (73 loc) · 2.39 KB
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
var configuration = { "iceServers": [ /* WRITE YOUR STUN/TURN SERVER LIST */ ] };
var RTCPeerConnection = webkitRTCPeerConnection || mozRTCPeerConnection;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
function Peer( localVid, addstream, icecandidate ) {
var state = 0;
var pc = new RTCPeerConnection( configuration );
pc.onaddstream = addstream;
pc.onicecandidate = icecandidate;
this.local = localVid;
this.offer = function ( callElements, callback ) {
if ( state === 0 ) {
navigator.getUserMedia( callElements, function ( stream ) {
this.stream = stream;
pc.addStream( stream );
this.local.src = URL.createObjectURL( stream );
this.local.play();
pc.createOffer( function ( desc ) {
this.pcDesc = new RTCSessionDescription( desc );
pc.setLocalDescription( this.pcDesc );
state = 1;
callback( false, desc );
});
});
} else {
callback("PEER CAN'T CREATE OFFER", null);
}
}
this.answer = function ( callElements, remoteDesc, callback ) {
if ( state === 0 ) {
navigator.getUserMedia( callElements, function ( stream ) {
this.stream = stream;
pc.addStream( stream );
pc.setRemoteDescription( new RTCSessionDescription( remoteDesc ) );
this.local.src = URL.createObjectURL( stream );
this.local.play();
pc.createAnswer( function ( desc ) {
this.pcDesc = new RTCSessionDescription( desc );
pc.setLocalDescription( this.pcDesc );
state = 1;
callback( false, desc );
});
});
} else if ( state === 1 ) {
callback( "PEER CAN'T ANSWER", null );
} else {
callElements( "PEER OBJECT IS UNUSABLE", null );
}
}
this.initCall = function ( remoteDesc, callback ) {
if ( state === 1 ) {
pc.setRemoteDescription( new RTCSessionDescription( remoteDesc ) );
callback(false);
} else {
callback("THIS METHOD CAN'T BE CALLED AT THIS MOMENT");
}
}
this.addIceCandidate = function ( ICEInfo ) {
pc.addIceCandidate( new RTCIceCandidate( ICEInfo ), function () {}, function ( err ) {
throw err;
});
}
this.close = function ( callback ) {
if ( state === 0 ) {
callback( "NOT CONNECTION OPENED" );
} else if ( state === 1 ) {
pc.close();
this.stream.stop();
state = 2;
} else {
callback( "CONNECTION ALREADY CLOSED" );
}
}
}