Best JavaScript code snippet using wpt
transaction.es6
Source:transaction.es6
1'use strict';2module.exports = function (stage) {3 // CALLBACK SDP PARSER4 const parseSdp = function (description) {5 var sdpLines = description.sdp.split('\r\n');6 var newline = "";7 // Search for m line.8 for (var i = 0; i < sdpLines.length; i++) {9 var line = sdpLines[i];10 switch (description.type) {11 case "offer":12 /*if (line.search('a=crypto') !== -1) {13 console.log("PARSE SDP DELETE CRYPTO ");14 continue ;15 }*/16 /*if (line.search('a=setup:actpass') !== -1) {17 console.log("PARSE SDP REPLACE setup : actpass by active ");18 line = line.replace("a=setup:actpass", "a=setup:active")19 }*/20 break;21 case "answer":22 /*if (line.search('a=crypto') !== -1) {23 console.log("PARSE SDP DELETE CRYPTO ");24 continue ;25 }*/26 /*if (line.search('a=setup:actpass') !== -1) {27 console.log("PARSE SDP REPLACE setup : actpass by active ");28 line = line.replace("a=setup:actpass", "a=setup:active")29 }*/30 break;31 }32 if (i === sdpLines.length - 1) {33 newline += line;34 } else {35 newline += line + "\r\n";36 }37 }38 description.sdp = newline;39 return description;40 };41 /*42 *43 * CLASS TRANSACTION WEBRTC44 *45 */46 const Transaction = class Transaction extends stage.Service {47 constructor(webrtc, from, to, dialog, settings) {48 super("WEBRTC TRANSACTION", webrtc.container, stage.notificationsCenter.create(settings || {}));49 this.webrtc = webrtc;50 //this.notificationsCenter = stage.notificationsCenter.create(settings || {}, this);51 this.dialog = dialog || Â null;52 this.error = null;53 if (this.dialog) {54 this.callId = this.dialog.callId;55 }56 this.protocol = webrtc.protocol;57 this.from = from;58 try {59 if (to instanceof stage.media.userMedia) {60 this.to = to;61 } else {62 this.to = new stage.media.userMedia(to, settings);63 }64 } catch (e) {65 throw e;66 }67 this.asyncCandidates = this.webrtc.settings.asyncCandidates;68 this.logger("CREATE TRANSATION WEBRTC", "DEBUG");69 this.RTCPeerConnection = this.createPeerConnection();70 this.RTCPeerConnection.addStream(this.from.stream);71 // MANAGE DTMF72 this.dtmfSender = null;73 if (this.webrtc.settings.dtmf) {74 try {75 this.initDtmfSender(this.from.stream);76 this.webrtc.listen(this, "onKeyPress", this.sendDtmf);77 // FIXME TRY TO RECEIVE DTMF RTP-EVENT78 /*this.webrtc.listen(this, "onRemoteStream",function(event, mediaStream, transaction){79 this.logger( "DTMF setRemoteStream", "DEBUG")80 this.initDtmfReceiver( this.from.stream );81 });*/82 } catch (e) {83 this.webrtc.logger(e, "ERROR");84 throw e;85 }86 }87 // MANAGE CANDIDATES88 this.candidates = [];89 this.listen(this, "onIcecandidate", function (transaction, candidates, peerConnection) {90 //console.log(" onIcecandidate : " + peerConnection.localDescription.type )91 let to = null;92 if (this.asyncCandidates && this.candidates.length) {93 //console.log( message.dailog)94 to = this.dialog.to.replace("<sip:", "").replace(">", "");95 this.logger("CANDIDATE TO" + to, "DEBUG");96 this.logger("CANDIDATE TO" + this.to.name, "DEBUG");97 this.dialog.invite(to, JSON.stringify(this.candidates), "ice/candidate");98 } else {99 if (peerConnection.localDescription.type === "offer") {100 this.sessionDescription = parseSdp.call(this, peerConnection.localDescription);101 if (this.dialog) {102 to = this.dialog.to.replace("<sip:", "").replace(">", "");103 this.logger("CANDIDATE TO" + to, "DEBUG");104 this.logger("CANDIDATE TO" + this.to.name, "DEBUG");105 this.dialog.invite(to, this.sessionDescription);106 } else {107 this.dialog = this.webrtc.protocol.invite(this.to.name, this.sessionDescription);108 this.callId = this.dialog.callId;109 this.webrtc.fire("onInvite", this, this.to, this.sessionDescription);110 }111 }112 if (peerConnection.localDescription.type === "answer") {113 this.sessionDescription = peerConnection.localDescription;114 if (this.sessionDescription && !(this.error)) {115 this.fire("onCreateAnwser", this.to, this.sessionDescription, this, this.dialog);116 }117 }118 }119 });120 this.listen(this, "onCreateAnwser", function (to, sessionDescription, webrtcTransaction, diag) {121 var response = this.dialog.currentTransaction.createResponse(200, "OK", this.sessionDescription.sdp, "application/sdp");122 response.send();123 });124 }125 createPeerConnection() {126 try {127 // CREATE PeerConnection128 this.logger(this.webrtc.settings.optional, "DEBUG");129 //console.log(this.webrtc.settings.optional)130 this.RTCPeerConnection = new RTCPeerConnection(this.webrtc.settings.optional);131 // MANAGE EVENT CANDIDATES132 this.RTCPeerConnection.onicecandidate = (event) => {133 // FIX firefox fire many time onicecandidate iceGatheringState === complete134 let old = this.iceGatheringState;135 if (event.target) {136 this.iceGatheringState = event.target.iceGatheringState || this.RTCPeerConnection.iceGatheringState;137 } else {138 this.iceGatheringState = this.RTCPeerConnection.iceGatheringState;139 }140 let type = this.RTCPeerConnection.localDescription.type;141 //console.log(this.iceGatheringState)142 //console.log(type)143 if (type === "offer" && this.iceGatheringState === 'complete' && old !== "complete") {144 //console.log("PASSS CANDIDATE")145 this.fire("onIcecandidate", this, this.candidates, this.RTCPeerConnection);146 } else if (event && event.candidate === null) {147 // candidates null !!!148 } else {149 this.logger("WEBRTC : ADD CANDIDATE", "DEBUG");150 if (event.candidate) {151 this.candidates.push(event.candidate);152 }153 if (type === "answer") {154 this.fire("onIcecandidate", this, this.candidates, this.RTCPeerConnection);155 this.RTCPeerConnection.onicecandidate = null;156 }157 }158 };159 // MANAGE STREAM160 this.RTCPeerConnection.onaddstream = (event) => {161 //console.log(event)162 this.setRemoteStream(event);163 this.logger("WEBRTC : ADD STREAM ", "DEBUG");164 };165 return this.RTCPeerConnection;166 } catch (e) {167 this.logger(e, "ERROR");168 this.webrtc.fire("onError", this, e);169 }170 }171 // FIXME TRY TO RECEIVE DTMF RTP-EVENT172 /*initDtmfReceiver (mediaStream){173 console.log(this.RTCPeerConnection)174 if ( ! this.RTCPeerConnection.createDTMFSender ) {175 throw new Error(" RTCPeerConnection method createDTMFSender() !!!! which is not support by this browser");176 }177 if (mediaStream !== null) {178 try {179 var remoteAudioTrack = mediaStream.getAudioTracks()[0];180 var dtmfSender = this.RTCPeerConnection.createDTMFSender(remoteAudioTrack);181 dtmfSender.ontonechange = (tone) => {182 this.logger("dtmfOnToneChange", "DEBUG") ;183 this.webrtc.fire("dtmfOnToneChange", tone , this);184 };185 }catch(e){186 throw e ;187 }188 } else {189 throw new Error( 'No local stream to create DTMF Sender', 500)190 }191 }*/192 initDtmfSender(mediaStream) {193 switch (this.webrtc.settings.dtmf) {194 case "SIP-INFO":195 var func = function () {};196 func.prototype.insertDTMF = (key, duration, gap) => {197 var description = "Signal=" + key + "\nDuration=" + duration;198 var type = "application/dtmf-relay";199 this.dialog.info(description, type);200 };201 this.dtmfSender = new func();202 break;203 case "RTP-EVENT":204 if (!this.RTCPeerConnection.createDTMFSender) {205 throw new Error(" RTCPeerConnection method createDTMFSender() !!!! which is not support by this browser", 500);206 }207 if (mediaStream !== null) {208 var localAudioTrack = mediaStream.getAudioTracks()[0];209 this.dtmfSender = this.RTCPeerConnection.createDTMFSender(localAudioTrack);210 this.dtmfSender.ontonechange = (tone) => {211 this.webrtc.fire("dtmfOnToneChange", tone, this);212 };213 } else {214 throw new Error('No local stream to create DTMF Sender', 500);215 }216 break;217 }218 }219 sendDtmf(code, key, event) {220 if (this.dialog.status !== this.dialog.statusCode.ESTABLISHED) {221 return;222 }223 if (this.dtmfSender) {224 var duration = 500;225 var gap = 50;226 this.logger('DTMF SEND ' + key + ' duration : ' + duration + ' gap : ' + gap, "DEBUG");227 return this.dtmfSender.insertDTMF(key, duration, gap);228 }229 throw new Error(" DTMF SENDER not ready");230 }231 createOffer() {232 return this.RTCPeerConnection.createOffer((sessionDescription) => {233 try {234 this.sessionDescription = parseSdp.call(this, sessionDescription);235 this.from.setDescription(this.RTCPeerConnection.setLocalDescription(this.sessionDescription, () => {236 // ASYNC CANDIDATES237 if (this.asyncCandidates) {238 // INVITE239 this.dialog = this.webrtc.protocol.invite(this.to.name, this.sessionDescription);240 this.callId = this.dialog.callId;241 this.webrtc.fire("onInvite", this, this.to, this.sessionDescription);242 } else {243 // SYNC CANDIDATES244 /*this.webrtc.listen(this, "onIcecandidate" , function(transaction, candidates, peerConnection){245 if ( peerConnection.localDescription.type == "offer" ){246 this.sessionDescription = parseSdp.call(this, peerConnection.localDescription ) ;247 if ( this.dialog ){248 var to = this.dialog.to.replace("<sip:", "").replace(">","") ;249 this.dialog.invite(to, this.sessionDescription);250 }else{251 this.dialog = this.webrtc.protocol.invite(this.to.name, this.sessionDescription);252 this.webrtc.fire("onInvite", this, this.to.name, this.sessionDescription );253 this.callId = this.dialog.callId ;254 }255 }256 })*/257 }258 },259 (error) => {260 this.error = error;261 this.webrtc.fire("onError", this, error);262 }));263 } catch (e) {264 throw e;265 }266 },267 (error) => {268 this.webrtc.fire("onError", this, error);269 },270 this.from.settings.constraintsOffer);271 }272 setRemoteStream(event) {273 if (event) {274 //console.log(event.stream.getVideoTracks());275 this.to.createMediaStream(null, null);276 this.to.mediaStream.setStream(event.stream);277 var type = this.RTCPeerConnection.remoteDescription.type;278 if (event.type === "video" || Â event.type === "addstream") {279 this.webrtc.notificationsCenter.fire("onRemoteStream", type, event, this.to.mediaStream, this);280 }281 }282 return this.to.createMediaStream;283 }284 setRemoteDescription(type, user, description, dialog) {285 //console.log("setRemoteDescription")286 this.currentTransaction = dialog.currentTransaction;287 let desc = {288 type: type,289 sdp: description290 };291 //console.log( desc );292 let remoteDesc = parseSdp.call(this, desc);293 let ClassDesc = new RTCSessionDescription(remoteDesc);294 this.remoteDescription = this.RTCPeerConnection.setRemoteDescription(295 ClassDesc,296 () => {297 if (this.RTCPeerConnection.remoteDescription.type === "offer") {298 //console.log("WEBRTC : onRemoteDescription ");299 //this.doAnswer(dialog);300 this.webrtc.fire("onOffer", this.webrtc, this);301 this.webrtc.fire("onRemoteDescription", this.from, this, this.to);302 } else {303 this.webrtc.fire("onOffHook", this, dialog);304 }305 },306 (error) => {307 this.error = error;308 this.webrtc.fire("onError", this, error);309 }310 );311 return this.remoteDescription;312 }313 doAnswer(dialog) {314 return this.RTCPeerConnection.createAnswer(315 (sessionDescription) => {316 this.from.setDescription(sessionDescription);317 this.RTCPeerConnection.setLocalDescription(sessionDescription, () => {318 this.sessionDescription = sessionDescription;319 if (this.asyncCandidates) {320 this.fire("onCreateAnwser", this.to, this.sessionDescription, this, dialog);321 }322 this.webrtc.fire("onOffHook", this, dialog);323 },324 (error) => {325 this.error = error;326 this.webrtc.fire("onError", this, error);327 });328 },329 // error330 (e) => {331 this.error = e;332 this.webrtc.fire("onError", this, e);333 },334 this.from.settings.constraints335 );336 }337 bye() {338 if (this.dialog) {339 this.dialog.bye();340 }341 }342 cancel() {343 if (this.currentTransaction) {344 this.currentTransaction.cancel();345 }346 this.webrtc.closeTransaction(this, this.to.name);347 }348 decline() {349 if (this.currentTransaction) {350 this.currentTransaction.decline();351 }352 this.webrtc.closeTransaction(this, this.to.name);353 }354 close() {355 this.logger("WEBRTC CLOSE TRANSACTION : " + this.callId, "DEBUG");356 if (this.RTCPeerConnection) {357 this.RTCPeerConnection.close();358 } else {359 this.logger("WEBRTC TRANSACTION ALREADY CLOSED : " + this.callId, "WARNING");360 }361 if (this.webrtc) {362 this.webrtc.unListen("onKeyPress", this.sendDtmf);363 }364 this.clear();365 return this;366 }367 clear() {368 if (this.RTCPeerConnection) {369 this.RTCPeerConnection = null;370 delete this.RTCPeerConnection;371 }372 if (this.webrtc) {373 this.webrtc = null;374 delete this.webrtc;375 }376 if (this.currentTransaction) {377 this.currentTransaction = null;378 delete this.currentTransaction;379 }380 if (this.candidates) {381 this.candidates = null;382 delete this.candidates;383 }384 if (this.dialog) {385 this.dialog = null;386 delete this.dialog;387 }388 if (this.from) {389 this.from = null;390 delete this.from;391 }392 if (this.to) {393 this.to = null;394 delete this.to;395 }396 if (this.error) {397 this.error = null;398 delete this.error;399 }400 }401 };402 stage.extend(stage.media, {403 webrtcTransaction: Transaction404 });405 return Transaction;...
dtmf.js
Source:dtmf.js
1/**2 * Created by alykoshin on 8/26/14.3 */4"use strict";5/**6 * @see {@link http://dev.w3.org/2011/webrtc/editor/webrtc.html#peer-to-peer-dtmf}7 */8var _RTCDTMF = function() {9 var self = this;10 Emitter(self);11 //self.reg('_tonechange');12 // Private data13 var _rtcPeerConnection = null;14 var _mediaStreamTrack = null;15 // Protected16 /**17 * property _rtcDTMFSender18 * memberOf _RTCDTMF19 * @type {RTCDTMFSender}20 * @protected21 */22 self._rtcDTMFSender = null;23 // Event handlers24 self._ontonechange = function(tone) {25 self.emit('_tonechange', tone);26 };27 // Public properties28 self._initialize = function(rtcPeerConnection, mediaStreamTrack) {29// try {30 if (mediaStreamTrack && rtcPeerConnection.createDTMFSender) {31// assert(rtcPeerConnection && rtcPeerConnection.createDTMFSender && mediaStreamTrack, '_RTCDTMF._init(): Invalid parameters');32 _rtcPeerConnection = rtcPeerConnection;33 _mediaStreamTrack = mediaStreamTrack;34 self._rtcDTMFSender = rtcPeerConnection.createDTMFSender(mediaStreamTrack);35 debug.log('_RTCDTMF(): rtcDTMFSender:', self.rtcDTMFSender);36 self._rtcDTMFSender.ontonechange = self._ontonechange;37 } else {38 self._rtcDTMFSender = null;39 debug.warn('BasicDTMF.sendDTMF(): unable to create rtcDTMFSender');40 }41// } catch(e) {42// debug.error('_initialize(): ', e);43// }44 };45 self._terminate = function() {46 self._rtcDTMFSender = null;47 };48 self._insertDTMF = function(tones, duration, interToneGap) {49 if (self._rtcDTMFSender) {50 debug.debug('_RTCDTMF.sendDTMF(): tones:', tones);51 self._rtcDTMFSender.insertDTMF(tones, duration, interToneGap);52 } else {53 debug.warn('_RTCDTMF.sendDTMF(): _rtcDTMFSender not assigned:', self._rtcDTMFSender);54 }55 };56 // Debugging57 self.on('_tonechange', function(tone) { debug.log('_RTCDTMF.on(): _tonechange', tone); } );58 return self;59};60//var _RTCDTMF = function() {61// var self = new _RTCDTMF();62//63// // Low level Events64//65// var ontonechange = null;66//67// // Low level Event Handlers68//69// var _ontonechange = function(tone) {70// debug.debug('_RTCDTMF(): _rtcDTMFSender.ontonechange(): tone:', tone.tone);71// if (ontonechange) {72// /**73// * @fires BasicDTMF#onToneChange74// */75// ontonechange(tone.tone);76// }77// };78//79// // Low Level Methods80//81// self._initialize = function (rtcPeerConnection, mediaStreamTrack) {82// if (mediaStreamTrack && rtcPeerConnection.createDTMFSender) {83//// assert(rtcPeerConnection && rtcPeerConnection.createDTMFSender && mediaStreamTrack, '_RTCDTMF._init(): Invalid parameters');84//85// self._rtcPeerConnection = rtcPeerConnection;86// self._mediaStreamTrack = mediaStreamTrack;87// //_rtcDTMFSender = rtcPeerConnection.createDTMFSender(mediaStreamTrack);88// self._initialize(rtcPeerConnection, mediaStreamTrack);89// debug.log('_RTCDTMF(): rtcDTMFSender:', self.rtcDTMFSender);90//91// self._rtcDTMFSender.ontonechange = _ontonechange;92// } else {93// self._rtcDTMFSender = null;94// debug.warn('BasicDTMF.sendDTMF(): unable to create rtcDTMFSender');95// }96// };97//98// self._insertDTMF = function (tones, duration, interToneGap) {99// if (self._rtcDTMFSender) {100//// assert(self._rtcDTMFSender, '_RTCDTMF._insertDTMF(): rtcDTMFSender is not assigned');101//// if (_rtcDTMFSender) {102// debug.debug('_RTCDTMF.sendDTMF(): tones:', tones);103// self._rtcDTMFSender.insertDTMF(tones, duration, interToneGap);104//// } else {105//// debug.warn('BasicConnection.sendDTMF(): rtcDTMFSender not assigned:', rtcDTMFSender);106//// }107// } else {108// debug.warn('_RTCDTMF.sendDTMF(): _rtcDTMFSender not assigned:', self._rtcDTMFSender);109// }110// };111//112// return self;113//};114/**115 *116 * @class BasicDTMF117 * @constructor118 * @param {RTCPeerConnection} rtcPeerConnection119 * @param {MediaStreamTrack} [mediaStreamTrack] - If not set, first audioTrack of first remote mediaStream of rtcPeerConnection will be used120 */121var BasicDTMF = function(rtcPeerConnection, mediaStreamTrack) {122 /**123 * Find first available audio audioTrack124 *125 * @param {RTCPeerConnection} rtcPeerConnection126 * @private127 */128 function getFirstTrack(rtcPeerConnection) {129 /**130 * @type {MediaStreamTrack}131 */132 var audioTrack;133 /**134 * @type {MediaStream[]}135 */136 var localStreams = rtcPeerConnection.getLocalStreams();137 if (localStreams.length > 0) {138 /**139 * @type {MediaStream}140 */141 var localStream = localStreams[0];142 /**143 * @type {MediaStreamTrack[]}144 */145 var audioTracks = localStream.getAudioTracks();146 if (audioTracks.length > 0) {147 audioTrack = audioTracks[0];148 } else {149 debug.warn('BasicDTMF(): mediaStreamTrack not set and no audioTracks available');150 audioTrack = null;151 }152 } else {153 debug.warn('BasicDTMF(): mediaStreamTrack not set and no localStreams available');154 audioTrack = null;155 }156 return audioTrack;157 }158 var self = new _RTCDTMF();159 /**160 * @typedef {function} OnToneChange161 * @memberOf BasicDTMF162 * @param {string} tone163 */164 /**165 * @type {OnToneChange}166 * @private167 */168 var onToneChange;169 /**170 * @event onToneChange171 * @type OnToneChange172 * @memberOf BasicDTMF173 */174 Object.defineProperty(this, "onToneChange", {175 get: function () {176 return onToneChange;177 },178 set: function (val) {179 onToneChange = val;180 }181 });182 /**183 * @property rtcDTMFSender184 * @type {RTCDTMFSender}185 */186 Object.defineProperty(this, "rtcDTMFSender", {187 get: function () { return self._rtcDTMFSender; }188 });189 /** if not set, try to use first audioTrack of first remote stream **/190 if ( ! mediaStreamTrack ) {191 mediaStreamTrack = getFirstTrack(rtcPeerConnection);192 }193// if (mediaStreamTrack && rtcPeerConnection.createDTMFSender) {194 self._initialize(rtcPeerConnection, mediaStreamTrack);195// rtcDTMFSender = rtcPeerConnection.createDTMFSender(mediaStreamTrack);196// debug.log('BasicDTMF(): rtcDTMFSender:', rtcDTMFSender);197// /**198// *199// * @type RTCDTMFSender#ontonechange200// * @param {RTCDTMFToneChangeEvent} tone201// */202// rtcDTMFSender.ontonechange = function (tone) {203// debug.debug('rtcDTMFSender.ontonechange(): tone:', tone.tone);204// if (onToneChange) {205// /**206// * @fires BasicDTMF#onToneChange207// */208// onToneChange(tone.tone);209// }210// };211 /**212 * @method send213 * @memberOf BasicDTMF214 * @param {DOMString} tones - The tones parameter is treated as a series of characters. The characters 0 through 9,215 * A through D, #, and * generate the associated DTMF tones. The characters a to d are216 * equivalent to A to D. The character ',' indicates a delay of 2 seconds before217 * processing the next character in the tones parameter. All other characters must be218 * considered unrecognized.219 * @param {long} [duration=100] - The duration parameter indicates the duration in ms to use for each character passed220 * in the tones parameters. The duration cannot be more than 6000 ms or less than 40 ms.221 * The default duration is 100 ms for each tone.222 * @param {long} [interToneGap=70] - The interToneGap parameter indicates the gap between tones. It must be at least 30 ms.223 * The default value is 70 ms. The browser may increase the duration and interToneGap224 * times to cause the times self DTMF start and stop to align with the boundaries of RTP225 * packets but it must not increase either of them by more than the duration of a single226 * RTP audio packet.227 */228 var send = function (tones, duration, interToneGap) {229// if (self._rtcDTMFSender) {230 self._insertDTMF(tones, duration, interToneGap);231// debug.debug('BasicConnection.sendDTMF(): tones:', tones);232// rtcDTMFSender.insertDTMF(tones, duration, interToneGap);233// } else {234// debug.warn('BasicDTMF.sendDTMF(): _rtcDTMFSender not assigned:', self._rtcDTMFSender);235// }236 };237 self.send = send;238 self.on('_tonechange', function(tone) {239 /**240 * @fires BasicDTMF#onToneChange241 */242 if (onToneChange) { onToneChange(tone.tone); }243 })244// } else {245// self._rtcDTMFSender = null;246// debug.warn('BasicDTMF.sendDTMF(): unable to create rtcDTMFSender');247// }248 return self;...
main.js
Source:main.js
1/*2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.3 *4 * Use of this source code is governed by a BSD-style license5 * that can be found in the LICENSE file in the root of the source6 * tree.7 */8'use strict';9var callButton = document.querySelector('button#callButton');10var sendTonesButton = document.querySelector('button#sendTonesButton');11var hangupButton = document.querySelector('button#hangupButton');12sendTonesButton.disabled = true;13hangupButton.disabled = true;14callButton.onclick = call;15sendTonesButton.onclick = handleSendTonesClick;16hangupButton.onclick = hangup;17var durationInput = document.querySelector('input#duration');18var gapInput = document.querySelector('input#gap');19var tonesInput = document.querySelector('input#tones');20var durationValue = document.querySelector('span#durationValue');21var gapValue = document.querySelector('span#gapValue');22var sentTonesDiv = document.querySelector('div#sentTones');23var dtmfStatusDiv = document.querySelector('div#dtmfStatus');24var audio = document.querySelector('audio');25var pc1;26var pc2;27var localStream;28var dtmfSender;29var offerOptions = {30 offerToReceiveAudio: 1,31 offerToReceiveVideo: 032};33durationInput.oninput = function() {34 durationValue.textContent = durationInput.value;35};36gapInput.oninput = function() {37 gapValue.textContent = gapInput.value;38};39main();40function main() {41 addDialPadHandlers();42}43function gotStream(stream) {44 trace('Received local stream');45 localStream = stream;46 var audioTracks = localStream.getAudioTracks();47 if (audioTracks.length > 0) {48 trace('Using Audio device: ' + audioTracks[0].label);49 }50 pc1.addStream(localStream);51 trace('Adding Local Stream to peer connection');52 pc1.createOffer(53 offerOptions54 ).then(55 gotDescription1,56 onCreateSessionDescriptionError57 );58}59function onCreateSessionDescriptionError(error) {60 trace('Failed to create session description: ' + error.toString());61}62function call() {63 trace('Starting call');64 var servers = null;65 var pcConstraints = {66 'optional': []67 };68 pc1 = new RTCPeerConnection(servers, pcConstraints);69 trace('Created local peer connection object pc1');70 pc1.onicecandidate = iceCallback1;71 pc2 = new RTCPeerConnection(servers, pcConstraints);72 trace('Created remote peer connection object pc2');73 pc2.onicecandidate = iceCallback2;74 pc2.onaddstream = gotRemoteStream;75 trace('Requesting local stream');76 navigator.mediaDevices.getUserMedia({77 audio: true,78 video: false79 })80 .then(gotStream)81 .catch(function(e) {82 alert('getUserMedia() error: ' + e.name);83 });84 callButton.disabled = true;85 hangupButton.disabled = false;86 sendTonesButton.disabled = false;87}88function gotDescription1(desc) {89 pc1.setLocalDescription(desc);90 trace('Offer from pc1 \n' + desc.sdp);91 pc2.setRemoteDescription(desc);92 // Since the 'remote' side has no media stream we need93 // to pass in the right constraints in order for it to94 // accept the incoming offer of audio.95 pc2.createAnswer().then(96 gotDescription2,97 onCreateSessionDescriptionError98 );99}100function gotDescription2(desc) {101 pc2.setLocalDescription(desc);102 trace('Answer from pc2: \n' + desc.sdp);103 pc1.setRemoteDescription(desc);104}105function hangup() {106 trace('Ending call');107 pc1.close();108 pc2.close();109 pc1 = null;110 pc2 = null;111 localStream = null;112 dtmfSender = null;113 callButton.disabled = false;114 hangupButton.disabled = true;115 sendTonesButton.disabled = true;116 dtmfStatusDiv.textContent = 'DTMF deactivated';117}118function gotRemoteStream(e) {119 audio.srcObject = e.stream;120 trace('Received remote stream');121 if (pc1.createDTMFSender) {122 enableDtmfSender();123 } else {124 alert(125 'This demo requires the RTCPeerConnection method createDTMFSender() ' +126 'which is not support by this browser.'127 );128 }129}130function iceCallback1(event) {131 if (event.candidate) {132 pc2.addIceCandidate(133 new RTCIceCandidate(event.candidate)134 ).then(135 onAddIceCandidateSuccess,136 onAddIceCandidateError137 );138 trace('Local ICE candidate: \n' + event.candidate.candidate);139 }140}141function iceCallback2(event) {142 if (event.candidate) {143 pc1.addIceCandidate(144 new RTCIceCandidate(event.candidate)145 ).then(146 onAddIceCandidateSuccess,147 onAddIceCandidateError148 );149 trace('Remote ICE candidate: \n ' + event.candidate.candidate);150 }151}152function onAddIceCandidateSuccess() {153 trace('AddIceCandidate success');154}155function onAddIceCandidateError(error) {156 trace('Failed to add Ice Candidate: ' + error.toString());157}158function enableDtmfSender() {159 dtmfStatusDiv.textContent = 'DTMF activated';160 if (localStream !== null) {161 var localAudioTrack = localStream.getAudioTracks()[0];162 dtmfSender = pc1.createDTMFSender(localAudioTrack);163 trace('Created DTMFSender:\n');164 dtmfSender.ontonechange = dtmfOnToneChange;165 } else {166 trace('No local stream to create DTMF Sender\n');167 }168}169function dtmfOnToneChange(tone) {170 if (tone) {171 trace('Sent DTMF tone: ' + tone.tone);172 sentTonesDiv.textContent += tone.tone + ' ';173 }174}175function sendTones(tones) {176 if (dtmfSender) {177 var duration = durationInput.value;178 var gap = gapInput.value;179 console.log('Tones, duration, gap: ', tones, duration, gap);180 dtmfSender.insertDTMF(tones, duration, gap);181 }182}183function handleSendTonesClick() {184 sendTones(tonesInput.value);185}186function addDialPadHandlers() {187 var dialPad = document.querySelector('div#dialPad');188 var buttons = dialPad.querySelectorAll('button');189 for (var i = 0; i !== buttons.length; ++i) {190 buttons[i].onclick = sendDtmfTone;191 }192}193function sendDtmfTone() {194 sendTones(this.textContent);...
Using AI Code Generation
1createRTCPeerConnection();2createDataChannel();3createOffer();4setLocalDescription();5createAnswer();6setRemoteDescription();7createDtmfSender();8function createRTCPeerConnection() {9 var pc = new RTCPeerConnection(null);10 pc.oniceconnectionstatechange = function() {11 if (pc.iceConnectionState == "connected") {12 log("Connected");13 }14 };15 pc.onicecandidate = function(event) {16 if (event.candidate) {17 log("Candidate: " + event.candidate.candidate);18 }19 };20 return pc;21}22function createDataChannel() {23 var pc = createRTCPeerConnection();24 var dc = pc.createDataChannel("test");25 dc.onopen = function() {26 log("Data channel open");27 };28 dc.onclose = function() {29 log("Data channel closed");30 };31 dc.onmessage = function(event) {32 log("Message received: " + event.data);33 };34 return dc;35}36function createOffer() {37 var pc = createRTCPeerConnection();38 var dc = createDataChannel();39 pc.createOffer().then(function(offer) {40 log("Offer created: " + offer.sdp);41 return pc.setLocalDescription(offer);42 }).then(function() {43 log("Local description set");44 }).catch(function(error) {45 log("Error: " + error);46 });47 return pc;48}49function setLocalDescription() {50 var pc = createRTCPeerConnection();51 var dc = createDataChannel();52 pc.createOffer().then(function(offer) {53 log("Offer created: " + offer.sdp);54 return pc.setLocalDescription(offer);55 }).then(function() {56 log("Local description set");57 return pc.setLocalDescription(new RTCSessionDescription({
Using AI Code Generation
1var pc = new RTCPeerConnection();2var sender = pc.createDtmfSender();3sender.insertDTMF("1234");4var pc = new RTCPeerConnection();5var sender = pc.createDtmfSender();6sender.insertDTMF("1234");7var pc = new RTCPeerConnection();8var sender = pc.createDtmfSender();9sender.insertDTMF("1234");10var pc = new RTCPeerConnection();11var sender = pc.createDtmfSender();12sender.insertDTMF("1234");13var pc = new RTCPeerConnection();14var sender = pc.createDtmfSender();15sender.insertDTMF("1234");16var pc = new RTCPeerConnection();17var sender = pc.createDtmfSender();18sender.insertDTMF("1234");19var pc = new RTCPeerConnection();20var sender = pc.createDtmfSender();21sender.insertDTMF("1234");22var pc = new RTCPeerConnection();23var sender = pc.createDtmfSender();24sender.insertDTMF("1234");25var pc = new RTCPeerConnection();26var sender = pc.createDtmfSender();27sender.insertDTMF("1234");28var pc = new RTCPeerConnection();29var sender = pc.createDtmfSender();30sender.insertDTMF("1234");31var pc = new RTCPeerConnection();32var sender = pc.createDtmfSender();33sender.insertDTMF("1234");34var pc = new RTCPeerConnection();35var sender = pc.createDtmfSender();36sender.insertDTMF("1234");
Using AI Code Generation
1var pc = new RTCPeerConnection();2var dtmfSender = pc.createDtmfSender();3dtmfSender.ontonechange = function(event) {4 console.log("Tone change event received");5};6dtmfSender.insertDTMF("1234", 1000, 100);7dtmfSender.insertDTMF("1234", 1000, 100);8var pc = new RTCPeerConnection();9var dtmfSender = pc.createDtmfSender();10dtmfSender.insertDTMF("1234", 1000, 100);11dtmfSender.ontonechange = function(event) {12 console.log("Tone change event received");13};14dtmfSender.ontonechange = null;
Using AI Code Generation
1function testCreateDtmfSender()2{3 var pc = new RTCPeerConnection();4 var dtmfSender = pc.createDtmfSender();5}6testCreateDtmfSender();7function testCreateDtmfSender()8{9 var pc = new RTCPeerConnection();10 var dtmfSender = pc.createDtmfSender();11}12testCreateDtmfSender();13TypeError: 'undefined' is not an object (evaluating 'pc.createDtmfSender')
Using AI Code Generation
1var wptools = require('wptools');2var dtmfSender = wptools.createDtmfSender();3dtmfSender.insertDTMF('1234');4exports.createDtmfSender = function() {5 var dtmfSender = new DTMFSender();6 return dtmfSender;7};8function DTMFSender() {9 this.insertDTMF = function(tones) {10 console.log('DTMF tones sent: ' + tones);11 };12}13Your name to display (optional):14Your name to display (optional):15Your name to display (optional):
Using AI Code Generation
1var pc = new RTCPeerConnection();2var sender = pc.createDtmfSender();3assert_true(sender != null, "sender is null");4assert_equals(sender.constructor, RTCDtmfSender, "sender is of type " + sender.constructor);5assert_true(sender.hasOwnProperty("canInsertDTMF"), "sender has attribute canInsertDTMF");6assert_true(sender.hasOwnProperty("insertDTMF"), "sender has attribute insertDTMF");7assert_true(sender.hasOwnProperty("toneBuffer"), "sender has attribute toneBuffer");8assert_true(sender.hasOwnProperty("duration"), "sender has attribute duration");9assert_true(sender.hasOwnProperty("interToneGap"), "sender has attribute interToneGap");10assert_true(sender.hasOwnProperty("ontonechange"), "sender has attribute ontonechange");11assert_equals(sender.canInsertDTMF, true, "sender has attribute canInsertDTMF");12assert_equals(sender.insertDTMF, "function", "sender has attribute insertDTMF");13assert_equals(sender.toneBuffer, "", "sender has attribute toneBuffer");14assert_equals(sender.duration, 100, "sender has attribute duration");15assert_equals(sender.interToneGap, 70, "sender has attribute interToneGap");16assert_equals(sender.ontonechange, null, "sender has attribute ontonechange");17assert_equals(sender.ontonechange, null, "sender has attribute ontonechange");18var pc = new RTCPeerConnection();19var sender = pc.createDtmfSender();20assert_true(sender != null, "sender is null");21assert_equals(sender.constructor, RTCDtmfSender, "sender is of type " + sender.constructor);
Using AI Code Generation
1var test = async_test('Test to check createDtmfSender method of wpt');2var pc = new RTCPeerConnection();3var dtmfSender = pc.createDtmfSender();4test.step(function() {5 assert_equals(dtmfSender.canInsertDTMF, true, 'canInsertDTMF should be true');6 test.done();7});8var test = async_test('Test to check insertDTMF method of wpt');9var pc = new RTCPeerConnection();10var dtmfSender = pc.createDtmfSender();11test.step(function() {12 assert_equals(dtmfSender.canInsertDTMF, true, 'canInsertDTMF should be true');13 test.done();14});15var test = async_test('Test to check duration of DTMF tones');16var pc = new RTCPeerConnection();17var dtmfSender = pc.createDtmfSender();18test.step(function() {19 assert_equals(dtmfSender.canInsertDTMF, true, 'canInsertDTMF should be true');20 test.done();21});22var test = async_test('Test to check interToneGap of DTMF tones');23var pc = new RTCPeerConnection();24var dtmfSender = pc.createDtmfSender();25test.step(function() {26 assert_equals(dtmfSender.canInsertDTMF, true, 'canInsertDTMF should be true');27 test.done();28});29var test = async_test('Test to check toneBuffer of DTMF tones');30var pc = new RTCPeerConnection();31var dtmfSender = pc.createDtmfSender();32test.step(function() {33 assert_equals(dtmfSender.canInsertDTMF, true, 'canInsertDTMF should be true');34 test.done();35});36var test = async_test('Test to check toneBuffer of DTMF tones');37var pc = new RTCPeerConnection();38var dtmfSender = pc.createDtmfSender();39test.step(function() {40 assert_equals(dtmfSender.canInsertDTMF,
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!