forked from webrtc/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideopipe.js
More file actions
75 lines (68 loc) · 1.92 KB
/
videopipe.js
File metadata and controls
75 lines (68 loc) · 1.92 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
/*
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree.
*/
//
// A "videopipe" abstraction on top of WebRTC.
//
// The usage of this abstraction:
// var pipe = new VideoPipe(mediastream, handlerFunction);
// handlerFunction = function(mediastream) {
// do_something
// }
// pipe.close();
//
// The VideoPipe will set up 2 PeerConnections, connect them to each
// other, and call HandlerFunction when the stream is available in the
// second PeerConnection.
//
function errorHandler(context) {
return function(error) {
trace('Failure in ' + context + ': ' + error.toString);
}
}
function successHandler(context) {
return function() {
trace('Success in ' + context);
}
}
function noAction() {
}
function VideoPipe(stream, handler) {
var servers = null;
var pc1 = new RTCPeerConnection(servers);
var pc2 = new RTCPeerConnection(servers);
pc1.addStream(stream);
pc1.onicecandidate = function(event) {
if (event.candidate) {
pc2.addIceCandidate(new RTCIceCandidate(event.candidate),
noAction, errorHandler('AddIceCandidate'));
}
}
pc2.onicecandidate = function(event) {
if (event.candidate) {
pc1.addIceCandidate(new RTCIceCandidate(event.candidate),
noAction, errorHandler('AddIceCandidate'));
}
}
pc2.onaddstream = function(e) {
handler(e.stream);
}
pc1.createOffer(function(desc) {
pc1.setLocalDescription(desc);
pc2.setRemoteDescription(desc);
pc2.createAnswer(function(desc2) {
pc2.setLocalDescription(desc2);
pc1.setRemoteDescription(desc2);
}, errorHandler('pc2.createAnswer'));
}, errorHandler('pc1.createOffer'));
this.pc1 = pc1;
this.pc2 = pc2;
}
VideoPipe.prototype.close = function() {
this.pc1.close();
this.pc2.close();
}