Skip to content

Commit f7f655c

Browse files
author
dimaspirit
committed
add functionaly for StatsReport
Need: - check every OS and browsers - made ui/logic for FF
1 parent 0f5940f commit f7f655c

File tree

6 files changed

+194
-100
lines changed

6 files changed

+194
-100
lines changed

js/modules/webrtc/qbRTCPeerConnection.js

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ RTCPeerConnection.prototype.init = function(delegate, userID, sessionID, type) {
4545
/** We use this timer interval to dial a user - produce the call requests each N seconds. */
4646
this.dialingTimer = null;
4747
this.answerTimeInterval = 0;
48+
this.statsReportTimer = null;
4849

4950
/** timer to detect network blips */
5051
this.reconnectTimer = 0;
@@ -54,6 +55,7 @@ RTCPeerConnection.prototype.init = function(delegate, userID, sessionID, type) {
5455

5556
RTCPeerConnection.prototype.release = function(){
5657
this._clearDialingTimer();
58+
this._clearStatsReportTimer();
5759

5860
if(this.signalingState !== 'closed'){
5961
this.close();
@@ -174,14 +176,84 @@ RTCPeerConnection.prototype.onIceCandidateCallback = function(event) {
174176

175177
/** handler of remote media stream */
176178
RTCPeerConnection.prototype.onAddRemoteStreamCallback = function(event) {
179+
var self = this;
180+
177181
if (typeof this.delegate._onRemoteStreamListener === 'function'){
178182
this.delegate._onRemoteStreamListener(this.userID, event.stream);
179183
}
184+
185+
if (config.webrtc && config.webrtc.statsReportTimeInterval) {
186+
if(isNaN(+config.webrtc.statsReportTimeInterval)) {
187+
Helpers.traceError('statsReportTimeInterval (' + config.webrtc.statsReportTimeInterval + ') must be integer.');
188+
} else {
189+
self.getStatsWrap();
190+
}
191+
}
192+
};
193+
194+
RTCPeerConnection.prototype._clearStatsReportTimer = function(){
195+
if(this.statsReportTimer){
196+
Helpers.trace('_clearStatsReportTimer');
197+
198+
clearInterval(this.statsReportTimer);
199+
this.statsReportTimer = null;
200+
}
201+
};
202+
203+
RTCPeerConnection.prototype.getStatsWrap = function() {
204+
var self = this,
205+
statsReportInterval = config.webrtc.statsReportTimeInterval * 1000;
206+
207+
function _getStats(peer, cb) {
208+
if (!!navigator.mozGetUserMedia) {
209+
peer.getStats(
210+
function (res) {
211+
var items = [];
212+
res.forEach(function (result) {
213+
items.push(result);
214+
});
215+
cb(items);
216+
},
217+
cb
218+
);
219+
} else {
220+
peer.getStats(function (res) {
221+
var items = [];
222+
res.result().forEach(function (result) {
223+
var item = {};
224+
result.names().forEach(function (name) {
225+
item[name] = result.stat(name);
226+
});
227+
item.id = result.id;
228+
item.type = result.type;
229+
item.timestamp = result.timestamp;
230+
items.push(item);
231+
});
232+
cb(items);
233+
});
234+
}
235+
}
236+
237+
var _statsReportCallback = function() {
238+
_getStats(self, function (results) {
239+
for (var i = 0; i < results.length; ++i) {
240+
var res = results[i];
241+
242+
if (res.googCodecName == 'opus' && res.bytesReceived) {
243+
self.delegate._onCallStatsReport(self.userID, res.bytesReceived);
244+
}
245+
}
246+
});
247+
};
248+
249+
self.statsReportTimer = setInterval(_statsReportCallback, statsReportInterval);
180250
};
181251

252+
253+
182254
RTCPeerConnection.prototype.onIceConnectionStateCallback = function() {
183255
var newIceConnectionState = this.iceConnectionState;
184-
256+
185257
Helpers.trace("onIceConnectionStateCallback: " + this.iceConnectionState);
186258

187259

@@ -292,4 +364,4 @@ RTCPeerConnection.prototype._startDialingTimer = function(extension, withOnNotAn
292364
_dialingCallback(extension, withOnNotAnswerCallback, true);
293365
};
294366

295-
module.exports = RTCPeerConnection;
367+
module.exports = RTCPeerConnection;

js/modules/webrtc/qbWebRTCClient.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ WebRTCClient.prototype._createAndStoreSession = function(sessionID, callerID, op
8080
newSession.onRemoteStreamListener = this.onRemoteStreamListener;
8181
newSession.onSessionConnectionStateChangedListener = this.onSessionConnectionStateChangedListener;
8282
newSession.onSessionCloseListener = this.onSessionCloseListener;
83+
newSession.onCallStatsReport = this.onCallStatsReport;
8384

8485
this.sessions[newSession.ID] = newSession;
8586
return newSession;

js/modules/webrtc/qbWebRTCSession.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* - onRemoteStreamListener(session, userID, stream)
1010
* - onSessionConnectionStateChangedListener(session, userID, connectionState)
1111
* - onSessionCloseListener(session)
12+
* - onCallStatsReport(session, userId, bytesReceived)
1213
*/
1314

1415
var config = require('../../qbConfig');
@@ -594,6 +595,12 @@ WebRTCSession.prototype._onRemoteStreamListener = function(userID, stream) {
594595
}
595596
};
596597

598+
WebRTCSession.prototype._onCallStatsReport = function(userId, bytesReceived) {
599+
if (typeof this.onCallStatsReport === 'function'){
600+
Utils.safeCallbackCall(this.onCallStatsReport, this, userId, bytesReceived);
601+
}
602+
};
603+
597604
WebRTCSession.prototype._onSessionConnectionStateChangedListener = function(userID, connectionState) {
598605
var self = this;
599606

js/qbConfig.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
*
44
* Configuration Module
55
*
6+
* NOTE:
7+
* - config.webrtc.statsReportTimeInterval [integer, sec]:
8+
* could add listener onCallStatsReport(session, userId, bytesReceived) if
9+
* want to get stats (bytesReceived) about peer every X sec;
610
*/
711

812
var config = {
@@ -26,6 +30,7 @@ var config = {
2630
answerTimeInterval: 60,
2731
dialingTimeInterval: 5,
2832
disconnectTimeInterval: 30,
33+
statsReportTimeInterval: 3,
2934
iceServers: [
3035
{
3136
'url': 'stun:stun.l.google.com:19302'

quickblox.min.js

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/webrtc/js/app.js

Lines changed: 97 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,98 +3,98 @@
33
/** when DOM is ready */
44
$(function() {
55
var ui = {
6-
$usersTitle: $('.j-users__title'),
7-
$usersList: $('.j-users__list'),
8-
9-
$panel: $('.j-pl'),
10-
$callees: $('.j-callees'),
11-
12-
$btnCall: $('.j-call'),
13-
$btnHangup: $('.j-hangup'),
14-
15-
$ctrlBtn: $('.j-caller__ctrl'),
16-
filterClassName: '.j-filter',
17-
18-
modal: {
19-
'income_call': '#income_call'
20-
},
21-
22-
sounds: {
23-
'call': 'callingSignal',
24-
'end': 'endCallSignal',
25-
'rington': 'ringtoneSignal'
26-
},
27-
setPositionFooter: function() {
28-
var $footer = $('.j-footer'),
29-
invisibleClassName = 'invisible',
30-
footerFixedClassName = 'footer-fixed';
31-
32-
if( $(window).outerHeight() > $('.j-wrapper').outerHeight() ) {
33-
$footer.addClass(footerFixedClassName);
34-
} else {
35-
$footer.removeClass(footerFixedClassName);
36-
}
6+
$usersTitle: $('.j-users__title'),
7+
$usersList: $('.j-users__list'),
8+
9+
$panel: $('.j-pl'),
10+
$callees: $('.j-callees'),
11+
12+
$btnCall: $('.j-call'),
13+
$btnHangup: $('.j-hangup'),
14+
15+
$ctrlBtn: $('.j-caller__ctrl'),
16+
filterClassName: '.j-filter',
17+
18+
modal: {
19+
'income_call': '#income_call'
20+
},
21+
22+
sounds: {
23+
'call': 'callingSignal',
24+
'end': 'endCallSignal',
25+
'rington': 'ringtoneSignal'
26+
},
27+
setPositionFooter: function() {
28+
var $footer = $('.j-footer'),
29+
invisibleClassName = 'invisible',
30+
footerFixedClassName = 'footer-fixed';
31+
32+
if( $(window).outerHeight() > $('.j-wrapper').outerHeight() ) {
33+
$footer.addClass(footerFixedClassName);
34+
} else {
35+
$footer.removeClass(footerFixedClassName);
36+
}
3737

38-
$footer.removeClass(invisibleClassName);
39-
},
40-
togglePreloadMain: function(action) {
41-
var $main = $('.j-main'),
42-
preloadClassName = 'main-preload';
38+
$footer.removeClass(invisibleClassName);
39+
},
40+
togglePreloadMain: function(action) {
41+
var $main = $('.j-main'),
42+
preloadClassName = 'main-preload';
4343

44-
if(action === 'show') {
45-
$main.addClass( preloadClassName );
46-
} else {
47-
$main.removeClass( preloadClassName );
48-
}
49-
},
50-
createUsers: function(users, $node) {
51-
var tpl = _.template( $('#user_tpl').html() ),
52-
usersHTML = '';
53-
54-
$node.empty();
44+
if(action === 'show') {
45+
$main.addClass( preloadClassName );
46+
} else {
47+
$main.removeClass( preloadClassName );
48+
}
49+
},
50+
createUsers: function(users, $node) {
51+
var tpl = _.template( $('#user_tpl').html() ),
52+
usersHTML = '';
5553

56-
_.each(users, function(user, i, list) {
57-
usersHTML += tpl(user);
58-
});
54+
$node.empty();
5955

60-
$node.append(usersHTML);
61-
},
62-
showCallBtn: function() {
63-
this.$btnHangup.addClass('hidden');
64-
this.$btnCall.removeClass('hidden');
65-
},
66-
hideCallBtn: function() {
67-
this.$btnHangup.removeClass('hidden');
68-
this.$btnCall.addClass('hidden');
69-
},
70-
toggleRemoteVideoView: function(userID, action) {
71-
var $video = $('#remote_video_' + userID);
72-
73-
if(!_.isEmpty(app.currentSession) && $video.length){
74-
if(action === 'show') {
75-
$video.parents('.j-callee').removeClass('callees__callee-wait');
76-
} else if(action === 'hide') {
77-
$video.parents('.j-callee').addClass('callees__callee-wait');
78-
} else if(action === 'clear') {
79-
/** detachMediaStream take videoElementId */
80-
app.currentSession.detachMediaStream('remote_video_' + userID);
81-
$video.removeClass('fw-video-wait');
82-
}
83-
}
84-
},
85-
classesNameFilter: 'no aden reyes perpetua inkwell toaster walden hudson gingham mayfair lofi xpro2 _1977 brooklyn',
86-
changeFilter: function(selector, filterName) {
87-
$(selector)
88-
.removeClass(this.classesNameFilter)
89-
.addClass( filterName );
90-
},
91-
callTime: 0,
92-
updTimer: function() {
93-
this.callTime += 1000;
56+
_.each(users, function(user, i, list) {
57+
usersHTML += tpl(user);
58+
});
9459

95-
$('#timer').removeClass('hidden')
96-
.text( new Date(this.callTime).toUTCString().split(/ /)[4] );
97-
}
60+
$node.append(usersHTML);
61+
},
62+
showCallBtn: function() {
63+
this.$btnHangup.addClass('hidden');
64+
this.$btnCall.removeClass('hidden');
65+
},
66+
hideCallBtn: function() {
67+
this.$btnHangup.removeClass('hidden');
68+
this.$btnCall.addClass('hidden');
69+
},
70+
toggleRemoteVideoView: function(userID, action) {
71+
var $video = $('#remote_video_' + userID);
72+
73+
if(!_.isEmpty(app.currentSession) && $video.length){
74+
if(action === 'show') {
75+
$video.parents('.j-callee').removeClass('callees__callee-wait');
76+
} else if(action === 'hide') {
77+
$video.parents('.j-callee').addClass('callees__callee-wait');
78+
} else if(action === 'clear') {
79+
/** detachMediaStream take videoElementId */
80+
app.currentSession.detachMediaStream('remote_video_' + userID);
81+
$video.removeClass('fw-video-wait');
82+
}
83+
}
84+
},
85+
classesNameFilter: 'no aden reyes perpetua inkwell toaster walden hudson gingham mayfair lofi xpro2 _1977 brooklyn',
86+
changeFilter: function(selector, filterName) {
87+
$(selector)
88+
.removeClass(this.classesNameFilter)
89+
.addClass( filterName );
90+
},
91+
callTime: 0,
92+
updTimer: function() {
93+
this.callTime += 1000;
94+
95+
$('#timer').removeClass('hidden')
96+
.text( new Date(this.callTime).toUTCString().split(/ /)[4] );
97+
}
9898
},
9999
app = {
100100
caller: {},
@@ -108,6 +108,8 @@
108108
authorizationing = false,
109109
callTimer;
110110

111+
window.app = app;
112+
111113
function initializeUI(arg) {
112114
var params = arg || {};
113115

@@ -480,6 +482,13 @@
480482
authorizationing = false;
481483
};
482484

485+
QB.webrtc.onCallStatsReport = function onCallStatsReport(session, userId, bytesReceived) {
486+
console.group('onCallStatsReport');
487+
console.log('userId: ' + userId);
488+
console.log('bytesReceived: ' + bytesReceived);
489+
console.groupEnd();
490+
};
491+
483492
QB.webrtc.onSessionCloseListener = function onSessionCloseListener(session){
484493
console.log('onSessionCloseListener: ' + session);
485494

@@ -662,7 +671,7 @@
662671
}
663672

664673
if(!callTimer) {
665-
callTimer = setInterval( function(){ ui.updTimer.call(ui) }, 1000);
674+
callTimer = setInterval( function(){ ui.updTimer.call(ui); }, 1000);
666675
}
667676
};
668677

0 commit comments

Comments
 (0)