Skip to content

Commit a1c2421

Browse files
author
dimaspirit
committed
finish QBWEBSDK-238
1 parent f7f655c commit a1c2421

File tree

3 files changed

+106
-75
lines changed

3 files changed

+106
-75
lines changed

js/modules/webrtc/qbRTCPeerConnection.js

Lines changed: 68 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -186,77 +186,16 @@ RTCPeerConnection.prototype.onAddRemoteStreamCallback = function(event) {
186186
if(isNaN(+config.webrtc.statsReportTimeInterval)) {
187187
Helpers.traceError('statsReportTimeInterval (' + config.webrtc.statsReportTimeInterval + ') must be integer.');
188188
} else {
189-
self.getStatsWrap();
189+
self._getStatsWrap();
190190
}
191191
}
192192
};
193193

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);
250-
};
251-
252-
253-
254194
RTCPeerConnection.prototype.onIceConnectionStateCallback = function() {
255195
var newIceConnectionState = this.iceConnectionState;
256196

257197
Helpers.trace("onIceConnectionStateCallback: " + this.iceConnectionState);
258198

259-
260199
/**
261200
* read more about all states:
262201
* http://w3c.github.io/webrtc-pc/#idl-def-RTCIceConnectionState
@@ -298,6 +237,40 @@ RTCPeerConnection.prototype.onIceConnectionStateCallback = function() {
298237
/**
299238
* PRIVATE
300239
*/
240+
RTCPeerConnection.prototype._clearStatsReportTimer = function(){
241+
if(this.statsReportTimer){
242+
Helpers.trace('_clearStatsReportTimer');
243+
244+
clearInterval(this.statsReportTimer);
245+
this.statsReportTimer = null;
246+
}
247+
};
248+
249+
RTCPeerConnection.prototype._getStatsWrap = function() {
250+
var self = this,
251+
statsReportInterval = config.webrtc.statsReportTimeInterval * 1000;
252+
253+
var _statsReportCallback = function() {
254+
_getStats(self, function (results) {
255+
for (var i = 0; i < results.length; ++i) {
256+
var res = results[i],
257+
is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
258+
259+
/** for firefox */
260+
if(is_firefox && res.bytesReceived) {
261+
self.delegate._onCallStatsReport(self.userID, res.bytesReceived);
262+
}
263+
/** for chrome */
264+
if (res.googCodecName == 'opus' && res.bytesReceived) {
265+
self.delegate._onCallStatsReport(self.userID, res.bytesReceived);
266+
}
267+
}
268+
});
269+
};
270+
271+
self.statsReportTimer = setInterval(_statsReportCallback, statsReportInterval);
272+
};
273+
301274
RTCPeerConnection.prototype._clearWaitingReconnectTimer = function() {
302275
if(this.waitingReconnectTimeoutCallback){
303276
Helpers.trace('_clearWaitingReconnectTimer');
@@ -364,4 +337,37 @@ RTCPeerConnection.prototype._startDialingTimer = function(extension, withOnNotAn
364337
_dialingCallback(extension, withOnNotAnswerCallback, true);
365338
};
366339

340+
/**
341+
* PRIVATE
342+
*/
343+
function _getStats(peer, cb) {
344+
if (!!navigator.mozGetUserMedia) {
345+
peer.getStats(peer.getLocalStreams()[0].getAudioTracks()[0],
346+
function (res) {
347+
var items = [];
348+
res.forEach(function (result) {
349+
items.push(result);
350+
});
351+
cb(items);
352+
},
353+
cb
354+
);
355+
} else {
356+
peer.getStats(function (res) {
357+
var items = [];
358+
res.result().forEach(function (result) {
359+
var item = {};
360+
result.names().forEach(function (name) {
361+
item[name] = result.stat(name);
362+
});
363+
item.id = result.id;
364+
item.type = result.type;
365+
item.timestamp = result.timestamp;
366+
items.push(item);
367+
});
368+
cb(items);
369+
});
370+
}
371+
}
372+
367373
module.exports = RTCPeerConnection;

0 commit comments

Comments
 (0)