@@ -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
5556RTCPeerConnection . prototype . release = function ( ) {
5657 this . _clearDialingTimer ( ) ;
58+ this . _clearStatsReportTimer ( ) ;
5759
5860 if ( this . signalingState !== 'closed' ) {
5961 this . close ( ) ;
@@ -174,16 +176,19 @@ RTCPeerConnection.prototype.onIceCandidateCallback = function(event) {
174176
175177/** handler of remote media stream */
176178RTCPeerConnection . 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+ self . _getStatsWrap ( ) ;
180186} ;
181187
182188RTCPeerConnection . prototype . onIceConnectionStateCallback = function ( ) {
183189 var newIceConnectionState = this . iceConnectionState ;
184-
185- Helpers . trace ( "onIceConnectionStateCallback: " + this . iceConnectionState ) ;
186190
191+ Helpers . trace ( "onIceConnectionStateCallback: " + this . iceConnectionState ) ;
187192
188193 /**
189194 * read more about all states:
@@ -226,6 +231,45 @@ RTCPeerConnection.prototype.onIceConnectionStateCallback = function() {
226231/**
227232 * PRIVATE
228233 */
234+ RTCPeerConnection . prototype . _clearStatsReportTimer = function ( ) {
235+ if ( this . statsReportTimer ) {
236+ clearInterval ( this . statsReportTimer ) ;
237+ this . statsReportTimer = null ;
238+ }
239+ } ;
240+
241+ RTCPeerConnection . prototype . _getStatsWrap = function ( ) {
242+ var self = this ,
243+ selector = self . delegate . callType == 1 ? self . getLocalStreams ( ) [ 0 ] . getVideoTracks ( ) [ 0 ] : self . getLocalStreams ( ) [ 0 ] . getAudioTracks ( ) [ 0 ] ,
244+ statsReportInterval ;
245+
246+ if ( ! config . webrtc && ! config . webrtc . statsReportTimeInterval ) {
247+ return ;
248+ }
249+
250+ if ( isNaN ( + config . webrtc . statsReportTimeInterval ) ) {
251+ Helpers . traceError ( 'statsReportTimeInterval (' + config . webrtc . statsReportTimeInterval + ') must be integer.' ) ;
252+ return ;
253+ }
254+
255+ statsReportInterval = config . webrtc . statsReportTimeInterval * 1000 ;
256+
257+ var _statsReportCallback = function ( ) {
258+ _getStats ( self , selector ,
259+ function ( results ) {
260+ self . delegate . _onCallStatsReport ( self . userID , results ) ;
261+ } ,
262+ function errorLog ( err ) {
263+ Helpers . traceError ( "_getStats error. " + err . name + ": " + err . message ) ;
264+ }
265+ ) ;
266+ } ;
267+
268+ Helpers . trace ( 'Stats tracker has been started.' ) ;
269+
270+ self . statsReportTimer = setInterval ( _statsReportCallback , statsReportInterval ) ;
271+ } ;
272+
229273RTCPeerConnection . prototype . _clearWaitingReconnectTimer = function ( ) {
230274 if ( this . waitingReconnectTimeoutCallback ) {
231275 Helpers . trace ( '_clearWaitingReconnectTimer' ) ;
@@ -292,4 +336,40 @@ RTCPeerConnection.prototype._startDialingTimer = function(extension, withOnNotAn
292336 _dialingCallback ( extension , withOnNotAnswerCallback , true ) ;
293337} ;
294338
295- module . exports = RTCPeerConnection ;
339+ /**
340+ * PRIVATE
341+ */
342+ function _getStats ( peer , selector , successCallback , errorCallback ) {
343+ /**
344+ * http://stackoverflow.com/questions/25069419/webrtc-getstat-api-set-up
345+ */
346+ if ( navigator . mozGetUserMedia ) {
347+ peer . getStats ( selector ,
348+ function ( res ) {
349+ var items = [ ] ;
350+ res . forEach ( function ( result ) {
351+ items . push ( result ) ;
352+ } ) ;
353+ successCallback ( items ) ;
354+ } ,
355+ errorCallback
356+ ) ;
357+ } else {
358+ peer . getStats ( function ( res ) {
359+ var items = [ ] ;
360+ res . result ( ) . forEach ( function ( result ) {
361+ var item = { } ;
362+ result . names ( ) . forEach ( function ( name ) {
363+ item [ name ] = result . stat ( name ) ;
364+ } ) ;
365+ item . id = result . id ;
366+ item . type = result . type ;
367+ item . timestamp = result . timestamp ;
368+ items . push ( item ) ;
369+ } ) ;
370+ successCallback ( items ) ;
371+ } ) ;
372+ }
373+ }
374+
375+ module . exports = RTCPeerConnection ;
0 commit comments