aijinhui
2023-10-24 8a87e4226aa802d6a0e3566c66824fedf68e77da
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: webrtcstreamer.js</title>
 
    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
 
<body>
 
<div id="main">
 
    <h1 class="page-title">Source: webrtcstreamer.js</h1>
 
    
 
 
 
    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>
/** 
 * Interface with WebRTC-streamer API
 * @constructor
 * @param {string} videoElement - id of the video element tag
 * @param {string} srvurl -  url of webrtc-streamer (default is current location)
*/
function WebRtcStreamer (videoElement, srvurl) {
    this.videoElement     = videoElement;    
    this.srvurl           = srvurl || location.protocol+"//"+window.location.hostname+":"+window.location.port;
    this.pc               = null;    
 
    this.pcOptions        = { "optional": [{"DtlsSrtpKeyAgreement": true} ] };
 
    this.mediaConstraints = { offerToReceiveAudio: true, offerToReceiveVideo: true };
 
    this.iceServers = null;
    this.earlyCandidates = [];
}
 
/** 
 * Connect a WebRTC Stream to videoElement 
 * @param {string} videourl - id of WebRTC video stream
 * @param {string} audiourl - id of WebRTC audio stream
 * @param {string} options -  options of WebRTC call
 * @param {string} stream  -  local stream to send
*/
WebRtcStreamer.prototype.connect = function(videourl, audiourl, options, localstream) {
    this.disconnect();
    
    // getIceServers is not already received
    if (!this.iceServers) {
        console.log("Get IceServers");
        
        var bind = this;
        request("GET" , this.srvurl + "/api/getIceServers")
            .done( function (response) { 
                if (response.statusCode === 200) {
                    bind.onReceiveGetIceServers.call(bind,JSON.parse(response.body), videourl, audiourl, options, localstream);
                }
                else {
                    bind.onError(response.statusCode);
                }
            }
        );        
    } else {
        this.onReceiveGetIceServers(this.iceServers, videourl, audiourl, options, localstream);
    }
}
 
/** 
 * Disconnect a WebRTC Stream and clear videoElement source
*/
WebRtcStreamer.prototype.disconnect = function() {        
    var videoElement = document.getElementById(this.videoElement);
    if (videoElement) {
        videoElement.src = "";
    }
    if (this.pc) {
        request("GET" , this.srvurl + "/api/hangup?peerid="+this.pc.peerid);
        
        try {
            this.pc.close();
        }
        catch (e) {
            console.log ("Failure close peer connection:" + e);
        }
        this.pc = null;
    }
}    
 
/*
* GetIceServers callback
*/
WebRtcStreamer.prototype.onReceiveGetIceServers = function(iceServers, videourl, audiourl, options, stream) {
    this.iceServers       = iceServers;
    this.pcConfig         = iceServers || {"iceServers": [] };
    try {            
        this.pc = this.createPeerConnection();
 
        var peerid = Math.random();            
        this.pc.peerid = peerid;
        
        var callurl = this.srvurl + "/api/call?peerid="+ peerid+"&amp;url="+encodeURIComponent(videourl);
        if (audiourl) {
            callurl += "&amp;audiourl="+encodeURIComponent(audiourl);
        }
        if (options) {
            callurl += "&amp;options="+encodeURIComponent(options);
        }
        
        if (stream) {
            this.pc.addStream(stream);
        }
 
                // clear early candidates
        this.earlyCandidates.length = 0;
        
        // create Offer
        var bind = this;
        this.pc.createOffer(this.mediaConstraints).then(function(sessionDescription) {
            console.log("Create offer:" + JSON.stringify(sessionDescription));
            
            bind.pc.setLocalDescription(sessionDescription
                , function() {
                    request("POST" , callurl, { body: JSON.stringify(sessionDescription) })
                        .done( function (response) { 
                            if (response.statusCode === 200) {
                                bind.onReceiveCall.call(bind,JSON.parse(response.body));
                            }
                            else {
                                bind.onError(response.statusCode);
                            }
                        }
                    );                    
                }
                , function() {} );
            
        }, function(error) { 
            alert("Create offer error:" + JSON.stringify(error));
        });
 
    } catch (e) {
        this.disconnect();
        alert("connect error: " + e);
    }        
}
 
/*
* create RTCPeerConnection 
*/
WebRtcStreamer.prototype.createPeerConnection = function() {
    console.log("createPeerConnection  config: " + JSON.stringify(this.pcConfig) + " option:"+  JSON.stringify(this.pcOptions));
    var pc = new RTCPeerConnection(this.pcConfig, this.pcOptions);
    var streamer = this;
    pc.onicecandidate = function(evt) { streamer.onIceCandidate.call(streamer, evt); };
    pc.onaddstream    = function(evt) { streamer.onAddStream.call(streamer,evt); };
    pc.oniceconnectionstatechange = function(evt) {  
        console.log("oniceconnectionstatechange  state: " + pc.iceConnectionState);
        var videoElement = document.getElementById(streamer.videoElement);
        if (videoElement) {
            if (pc.iceConnectionState === "connected") {
                videoElement.style.opacity = "1.0";
            }            
            else if (pc.iceConnectionState === "disconnected") {
                videoElement.style.opacity = "0.25";
            }            
            else if ( (pc.iceConnectionState === "failed") || (pc.iceConnectionState === "closed") )  {
                videoElement.style.opacity = "0.5";
            }            
        }
    }
    pc.ondatachannel = function(evt) {  
        console.log("remote datachannel created:"+JSON.stringify(evt));
        
        evt.channel.onopen = function () {
            console.log("remote datachannel open");
            this.send("remote channel openned");
        }
        evt.channel.onmessage = function (event) {
            console.log("remote datachannel recv:"+JSON.stringify(event.data));
        }
    }
 
    try {
        var dataChannel = pc.createDataChannel("ClientDataChannel");
        dataChannel.onopen = function() {
            console.log("local datachannel open");
            this.send("local channel openned");
        }
        dataChannel.onmessage = function(evt) {
            console.log("local datachannel recv:"+JSON.stringify(evt.data));
        }
    } catch (e) {
        console.log("Cannor create datachannel error: " + e);
    }    
    
    console.log("Created RTCPeerConnnection with config: " + JSON.stringify(this.pcConfig) + "option:"+  JSON.stringify(this.pcOptions) );
    return pc;
}
 
 
/*
* RTCPeerConnection IceCandidate callback
*/
WebRtcStreamer.prototype.onIceCandidate = function (event) {
    if (event.candidate) {
                if (this.pc.currentRemoteDescription)  {
            var bind = this;
            request("POST" , this.srvurl + "/api/addIceCandidate?peerid="+this.pc.peerid, { body: JSON.stringify(event.candidate) })
                .done( function (response) { 
                    if (response.statusCode === 200) {
                        console.log("addIceCandidate ok:" + response.body);
                    }
                    else {
                        bind.onError(response.statusCode);
                    }
                }
            );                    
        } else {
            this.earlyCandidates.push(event.candidate);
        }
    } 
    else {
        console.log("End of candidates.");
    }
}
 
/*
* RTCPeerConnection AddTrack callback
*/
WebRtcStreamer.prototype.onAddStream = function(event) {
    console.log("Remote track added:" +  JSON.stringify(event));
    
    var videoElement = document.getElementById(this.videoElement);
    videoElement.srcObject = event.stream;
    videoElement.setAttribute("playsinline", true);
    videoElement.play();
}
        
/*
* AJAX /call callback
*/
WebRtcStreamer.prototype.onReceiveCall = function(dataJson) {
    var bind = this;
    console.log("offer: " + JSON.stringify(dataJson));
    this.pc.setRemoteDescription(new RTCSessionDescription(dataJson)
        , function()      { 
                        console.log ("setRemoteDescription ok");
                        while (bind.earlyCandidates.length) {
                var candidate = bind.earlyCandidates.shift();
                
                request("POST" , bind.srvurl + "/api/addIceCandidate?peerid=" + bind.pc.peerid, { body: JSON.stringify(candidate) })
                    .done( function (response) { 
                        if (response.statusCode === 200) {
                            console.log("addIceCandidate ok:" + response.body);
                        }
                        else {
                            bind.onError(response.statusCode);
                        }
                    }
                );
            }
            
            request("GET" , bind.srvurl + "/api/getIceCandidate?peerid=" + bind.pc.peerid)
                .done( function (response) { 
                    if (response.statusCode === 200) {
                        bind.onReceiveCandidate.call(bind,JSON.parse(response.body));
                    }
                    else {
                        bind.onError(response.statusCode);
                    }
                }
            );
                
        }
        , function(error) { console.log ("setRemoteDescription error:" + JSON.stringify(error)); });
}    
 
/*
* AJAX /getIceCandidate callback
*/
WebRtcStreamer.prototype.onReceiveCandidate = function(dataJson) {
    console.log("candidate: " + JSON.stringify(dataJson));
    if (dataJson) {
        for (var i=0; i&lt;dataJson.length; i++) {
            var candidate = new RTCIceCandidate(dataJson[i]);
            
            console.log("Adding ICE candidate :" + JSON.stringify(candidate) );
            this.pc.addIceCandidate(candidate
                , function()      { console.log ("addIceCandidate OK"); }
                , function(error) { console.log ("addIceCandidate error:" + JSON.stringify(error)); } );
        }
        this.pc.addIceCandidate();
    }
}
 
 
/*
* AJAX callback for Error
*/
WebRtcStreamer.prototype.onError = function(status) {
    console.log("onError:" + status);
}</code></pre>
        </article>
    </section>
 
 
 
 
</div>
 
<nav>
    <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="JanusVideoRoom.html">JanusVideoRoom</a></li><li><a href="WebRtcStreamer.html">WebRtcStreamer</a></li><li><a href="XMPPVideoRoom.html">XMPPVideoRoom</a></li></ul>
</nav>
 
<br class="clear">
 
<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Sat Aug 25 2018 15:00:27 GMT+0200 (CEST)
</footer>
 
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>