Skip to content

Commit 00dfd9b

Browse files
committedSep 30, 2018
Update
1 parent 386b2cc commit 00dfd9b

6 files changed

+215
-75
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* HLS iOS / MPEG DASH Android
2020
* WebRTC HTML5 broadcast/playback server support
2121

22+
[PHP Live Streaming Webcam Demo](https://videowhisper.com/demos/livestreaming/)
23+
2224

2325
## Installation Instructions for PHP Live Video Streaming Software
2426
* Before installing, make sure your hosting environment meets all [requirements](https://videowhisper.com/?p=Requirements) (including a supported RTMP server).

‎js/vwrtc-play.js

+50-37
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
//VideoWhisper.com WebRTC implemetation
12

23
var remoteVideo = null;
34
var peerConnection = null;
45
var peerConnectionConfig = {'iceServers': []};
56
var localStream = null;
67
var wsConnection = null;
78
var repeaterRetryCount = 0;
8-
var newAPI = false;
9-
var doGetAvailableStreams = false;
10-
9+
var newAPI = true;
1110

1211

1312
//WebRTC Playback
@@ -17,6 +16,9 @@ function browserReady()
1716
remoteVideo = document.getElementById('remoteVideo');
1817

1918
if(navigator.mediaDevices.getUserMedia)
19+
{
20+
newAPI = true;
21+
}else if (navigator.getUserMedia)
2022
{
2123
newAPI = false;
2224
}
@@ -36,7 +38,12 @@ function wsConnect(url)
3638
console.log("wsConnection.onopen");
3739

3840
peerConnection = new RTCPeerConnection(peerConnectionConfig);
39-
peerConnection.onicecandidate = gotIceCandidate;
41+
42+
peerConnection.addTransceiver('audio');
43+
peerConnection.addTransceiver('video');
44+
45+
peerConnection.onicecandidate = gotIceCandidate;
46+
4047

4148
if (newAPI)
4249
{
@@ -47,16 +54,10 @@ function wsConnect(url)
4754
peerConnection.onaddstream = gotRemoteStream;
4855
}
4956

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+
6061
}
6162

6263
function sendPlayGetOffer()
@@ -65,11 +66,6 @@ function wsConnect(url)
6566
wsConnection.send('{"direction":"play", "command":"getOffer", "streamInfo":'+JSON.stringify(streamInfo)+', "userData":'+JSON.stringify(userData)+'}');
6667
}
6768

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-
}
7369

7470
wsConnection.onmessage = function(evt)
7571
{
@@ -89,18 +85,18 @@ function wsConnect(url)
8985
}
9086
else
9187
{
92-
$("#sdpDataTag").html('Live stream repeater timeout: '+streamName);
88+
jQuery("#sdpDataTag").html('Live stream repeater timeout: '+streamName);
9389
stopPlay();
9490
}
9591
}
9692
else if (msgStatus != 200)
9793
{
98-
$("#sdpDataTag").html(msgJSON['statusDescription']);
94+
jQuery("#sdpDataTag").html(msgJSON['statusDescription']);
9995
stopPlay();
10096
}
10197
else
10298
{
103-
$("#sdpDataTag").html("");
99+
jQuery("#sdpDataTag").html("");
104100

105101
var streamInfoResponse = msgJSON['streamInfo'];
106102
if (streamInfoResponse !== undefined)
@@ -135,11 +131,7 @@ function wsConnect(url)
135131
wsConnection.close();
136132
wsConnection = null;
137133
}
138-
// now check for getAvailableResponse command to close the connection
139-
if ('getAvailableStreams'.localeCompare(msgCommand) == 0)
140-
{
141-
stopPlay();
142-
}
134+
143135
}
144136

145137
wsConnection.onclose = function()
@@ -151,16 +143,10 @@ function wsConnect(url)
151143
{
152144
console.log("wsConnection.onerror: "+JSON.stringify(evt));
153145

154-
$("#sdpDataTag").html('WebSocket connection failed: '+wsURL);
146+
jQuery("#sdpDataTag").html('WebSocket connection failed: '+wsURL);
155147
}
156148
}
157149

158-
function getAvailableStreams()
159-
{
160-
doGetAvailableStreams=true;
161-
startPlay();
162-
}
163-
164150
function startPlay()
165151
{
166152
repeaterRetryCount = 0;
@@ -182,14 +168,14 @@ function stopPlay()
182168
wsConnection = null;
183169

184170
remoteVideo.src = ""; // this seems like a chrome bug - if set to null it will make HTTP request
171+
//remoteVideo.srcObject = null; //2
185172

186173
console.log("stopPlay");
187174
}
188175

189176
// start button clicked
190177
function start()
191178
{
192-
doGetAvailableStreams=false;
193179

194180
if (peerConnection == null)
195181
startPlay();
@@ -204,7 +190,7 @@ function gotMessageFromServer(message)
204190
{
205191
if (signal.sdp.type == 'offer')
206192
{
207-
console.log('sdp:offser');
193+
console.log('sdp:offer');
208194
console.log(signal.sdp.sdp);
209195
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp), function() {
210196
peerConnection.createAnswer(gotDescription, errorHandler);
@@ -228,11 +214,15 @@ function gotIceCandidate(event)
228214
if(event.candidate != null)
229215
{
230216
}
217+
218+
console.log('gotIceCandidate: ICE candidate:' + (event.candidate ? event.candidate.candidate : '(null)'));
219+
231220
}
232221

233222
function gotDescription(description)
234223
{
235224
console.log('gotDescription');
225+
236226
peerConnection.setLocalDescription(description, function ()
237227
{
238228
console.log('sendAnswer');
@@ -242,17 +232,40 @@ function gotDescription(description)
242232
}, function() {console.log('set description error')});
243233
}
244234

235+
245236
function gotRemoteTrack(event)
246237
{
238+
247239
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;
249247
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+
*/
250262

251263
}
252264

253265
function gotRemoteStream(event)
254266
{
255267
console.log('gotRemoteStream: '+event.stream);
268+
256269
remoteVideo.srcObject = event.stream;
257270
}
258271

0 commit comments

Comments
 (0)
Please sign in to comment.