1
+ //VideoWhisper.com WebRTC implemetation
1
2
2
3
var remoteVideo = null ;
3
4
var peerConnection = null ;
4
5
var peerConnectionConfig = { 'iceServers' : [ ] } ;
5
6
var localStream = null ;
6
7
var wsConnection = null ;
7
8
var repeaterRetryCount = 0 ;
8
- var newAPI = false ;
9
- var doGetAvailableStreams = false ;
10
-
9
+ var newAPI = true ;
11
10
12
11
13
12
//WebRTC Playback
@@ -17,6 +16,9 @@ function browserReady()
17
16
remoteVideo = document . getElementById ( 'remoteVideo' ) ;
18
17
19
18
if ( navigator . mediaDevices . getUserMedia )
19
+ {
20
+ newAPI = true ;
21
+ } else if ( navigator . getUserMedia )
20
22
{
21
23
newAPI = false ;
22
24
}
@@ -36,7 +38,12 @@ function wsConnect(url)
36
38
console . log ( "wsConnection.onopen" ) ;
37
39
38
40
peerConnection = new RTCPeerConnection ( peerConnectionConfig ) ;
39
- peerConnection . onicecandidate = gotIceCandidate ;
41
+
42
+ peerConnection . addTransceiver ( 'audio' ) ;
43
+ peerConnection . addTransceiver ( 'video' ) ;
44
+
45
+ peerConnection . onicecandidate = gotIceCandidate ;
46
+
40
47
41
48
if ( newAPI )
42
49
{
@@ -47,16 +54,10 @@ function wsConnect(url)
47
54
peerConnection . onaddstream = gotRemoteStream ;
48
55
}
49
56
50
- console . log ( "wsURL: " + wsURL ) ;
51
-
52
- if ( doGetAvailableStreams )
53
- {
54
- sendPlayGetAvailableStreams ( ) ;
55
- }
56
- else
57
- {
58
- sendPlayGetOffer ( ) ;
59
- }
57
+ console . log ( "wsURL: " + wsURL ) ;
58
+
59
+ sendPlayGetOffer ( ) ;
60
+
60
61
}
61
62
62
63
function sendPlayGetOffer ( )
@@ -65,11 +66,6 @@ function wsConnect(url)
65
66
wsConnection . send ( '{"direction":"play", "command":"getOffer", "streamInfo":' + JSON . stringify ( streamInfo ) + ', "userData":' + JSON . stringify ( userData ) + '}' ) ;
66
67
}
67
68
68
- function sendPlayGetAvailableStreams ( )
69
- {
70
- console . log ( "sendPlayGetAvailableStreams: " + JSON . stringify ( streamInfo ) ) ;
71
- wsConnection . send ( '{"direction":"play", "command":"getAvailableStreams", "streamInfo":' + JSON . stringify ( streamInfo ) + ', "userData":' + JSON . stringify ( userData ) + '}' ) ;
72
- }
73
69
74
70
wsConnection . onmessage = function ( evt )
75
71
{
@@ -89,18 +85,18 @@ function wsConnect(url)
89
85
}
90
86
else
91
87
{
92
- $ ( "#sdpDataTag" ) . html ( 'Live stream repeater timeout: ' + streamName ) ;
88
+ jQuery ( "#sdpDataTag" ) . html ( 'Live stream repeater timeout: ' + streamName ) ;
93
89
stopPlay ( ) ;
94
90
}
95
91
}
96
92
else if ( msgStatus != 200 )
97
93
{
98
- $ ( "#sdpDataTag" ) . html ( msgJSON [ 'statusDescription' ] ) ;
94
+ jQuery ( "#sdpDataTag" ) . html ( msgJSON [ 'statusDescription' ] ) ;
99
95
stopPlay ( ) ;
100
96
}
101
97
else
102
98
{
103
- $ ( "#sdpDataTag" ) . html ( "" ) ;
99
+ jQuery ( "#sdpDataTag" ) . html ( "" ) ;
104
100
105
101
var streamInfoResponse = msgJSON [ 'streamInfo' ] ;
106
102
if ( streamInfoResponse !== undefined )
@@ -135,11 +131,7 @@ function wsConnect(url)
135
131
wsConnection . close ( ) ;
136
132
wsConnection = null ;
137
133
}
138
- // now check for getAvailableResponse command to close the connection
139
- if ( 'getAvailableStreams' . localeCompare ( msgCommand ) == 0 )
140
- {
141
- stopPlay ( ) ;
142
- }
134
+
143
135
}
144
136
145
137
wsConnection . onclose = function ( )
@@ -151,16 +143,10 @@ function wsConnect(url)
151
143
{
152
144
console . log ( "wsConnection.onerror: " + JSON . stringify ( evt ) ) ;
153
145
154
- $ ( "#sdpDataTag" ) . html ( 'WebSocket connection failed: ' + wsURL ) ;
146
+ jQuery ( "#sdpDataTag" ) . html ( 'WebSocket connection failed: ' + wsURL ) ;
155
147
}
156
148
}
157
149
158
- function getAvailableStreams ( )
159
- {
160
- doGetAvailableStreams = true ;
161
- startPlay ( ) ;
162
- }
163
-
164
150
function startPlay ( )
165
151
{
166
152
repeaterRetryCount = 0 ;
@@ -182,14 +168,14 @@ function stopPlay()
182
168
wsConnection = null ;
183
169
184
170
remoteVideo . src = "" ; // this seems like a chrome bug - if set to null it will make HTTP request
171
+ //remoteVideo.srcObject = null; //2
185
172
186
173
console . log ( "stopPlay" ) ;
187
174
}
188
175
189
176
// start button clicked
190
177
function start ( )
191
178
{
192
- doGetAvailableStreams = false ;
193
179
194
180
if ( peerConnection == null )
195
181
startPlay ( ) ;
@@ -204,7 +190,7 @@ function gotMessageFromServer(message)
204
190
{
205
191
if ( signal . sdp . type == 'offer' )
206
192
{
207
- console . log ( 'sdp:offser ' ) ;
193
+ console . log ( 'sdp:offer ' ) ;
208
194
console . log ( signal . sdp . sdp ) ;
209
195
peerConnection . setRemoteDescription ( new RTCSessionDescription ( signal . sdp ) , function ( ) {
210
196
peerConnection . createAnswer ( gotDescription , errorHandler ) ;
@@ -228,11 +214,15 @@ function gotIceCandidate(event)
228
214
if ( event . candidate != null )
229
215
{
230
216
}
217
+
218
+ console . log ( 'gotIceCandidate: ICE candidate:' + ( event . candidate ? event . candidate . candidate : '(null)' ) ) ;
219
+
231
220
}
232
221
233
222
function gotDescription ( description )
234
223
{
235
224
console . log ( 'gotDescription' ) ;
225
+
236
226
peerConnection . setLocalDescription ( description , function ( )
237
227
{
238
228
console . log ( 'sendAnswer' ) ;
@@ -242,17 +232,40 @@ function gotDescription(description)
242
232
} , function ( ) { console . log ( 'set description error' ) } ) ;
243
233
}
244
234
235
+
245
236
function gotRemoteTrack ( event )
246
237
{
238
+
247
239
console . log ( 'gotRemoteTrack: kind:' + event . track . kind + ' stream:' + event . streams [ 0 ] ) ;
248
- //remoteVideo.src = window.URL.createObjectURL(event.streams[0]);
240
+
241
+
242
+ if ( event . streams [ 0 ] == remoteVideo . srcObject ) console . log ( 'Same stream received' ) ;
243
+ else
244
+ {
245
+ // reset srcObject to work around minor bugs in Chrome and Edge.
246
+ remoteVideo . srcObject = null ;
249
247
remoteVideo . srcObject = event . streams [ 0 ] ;
248
+ }
249
+
250
+ /*
251
+ var promise = remoteVideo.play();
252
+
253
+ if (promise !== undefined) {
254
+ promise.catch(error => {
255
+ console.log(error);
256
+ // Show a UI element to let the user manually start playback
257
+ }).then(() => {
258
+ console.log('Auto-play started');
259
+ });
260
+ }
261
+ */
250
262
251
263
}
252
264
253
265
function gotRemoteStream ( event )
254
266
{
255
267
console . log ( 'gotRemoteStream: ' + event . stream ) ;
268
+
256
269
remoteVideo . srcObject = event . stream ;
257
270
}
258
271
0 commit comments