@@ -542,92 +542,110 @@ function _getStats(peer, lastResults, successCallback, errorCallback) {
542542
543543 peer . getStats ( null ) . then ( function ( results ) {
544544 results . forEach ( function ( result ) {
545- var item ;
546-
547- if ( result . bytesReceived && result . type === 'inbound-rtp' ) {
548- item = statistic . remote [ result . mediaType ] ;
549- item . bitrate = _getBitratePerSecond ( result , lastResults , false ) ;
550- item . bytesReceived = result . bytesReceived ;
551- item . packetsReceived = result . packetsReceived ;
552- item . timestamp = result . timestamp ;
553- if ( result . mediaType === 'video' && result . framerateMean ) {
554- item . framesPerSecond = Math . round ( result . framerateMean * 10 ) / 10 ;
555- }
556- } else if ( result . bytesSent && result . type === 'outbound-rtp' ) {
557- item = statistic . local [ result . mediaType ] ;
558- item . bitrate = _getBitratePerSecond ( result , lastResults , true ) ;
559- item . bytesSent = result . bytesSent ;
560- item . packetsSent = result . packetsSent ;
561- item . timestamp = result . timestamp ;
562- if ( result . mediaType === 'video' && result . framerateMean ) {
563- item . framesPerSecond = Math . round ( result . framerateMean * 10 ) / 10 ;
564- }
565- } else if ( result . type === 'local-candidate' ) {
566- item = statistic . local . candidate ;
567- if ( result . candidateType === 'host' && result . mozLocalTransport === 'udp' && result . transport === 'udp' ) {
568- item . protocol = result . transport ;
569- item . ip = result . ipAddress ;
570- item . port = result . portNumber ;
571- } else if ( ! Helpers . getVersionFirefox ( ) ) {
572- item . protocol = result . protocol ;
573- item . ip = result . ip ;
574- item . port = result . port ;
575- }
576- } else if ( result . type === 'remote-candidate' ) {
577- item = statistic . remote . candidate ;
578- item . protocol = result . protocol || result . transport ;
579- item . ip = result . ip || result . ipAddress ;
580- item . port = result . port || result . portNumber ;
581- } else if ( result . type === 'track' && result . kind === 'video' && ! Helpers . getVersionFirefox ( ) ) {
582- if ( result . remoteSource ) {
583- item = statistic . remote . video ;
584- item . frameHeight = result . frameHeight ;
585- item . frameWidth = result . frameWidth ;
586- item . framesPerSecond = _getFramesPerSecond ( result , lastResults , false ) ;
587- } else {
588- item = statistic . local . video ;
589- item . frameHeight = result . frameHeight ;
590- item . frameWidth = result . frameWidth ;
591- item . framesPerSecond = _getFramesPerSecond ( result , lastResults , true ) ;
592- }
593- }
545+ _applyStatReport ( statistic , result , lastResults ) ;
594546 } ) ;
595547 successCallback ( statistic , results ) ;
596548 } , errorCallback ) ;
549+ }
597550
598- function _getBitratePerSecond ( result , lastResults , isLocal ) {
599- var lastResult = lastResults && lastResults . get ( result . id ) ,
600- seconds = lastResult ? ( ( result . timestamp - lastResult . timestamp ) / 1000 ) : 5 ,
601- kilo = 1024 ,
602- bit = 8 ,
603- bitrate ;
604-
605- if ( ! lastResult ) {
606- bitrate = 0 ;
607- } else if ( isLocal ) {
608- bitrate = bit * ( result . bytesSent - lastResult . bytesSent ) / ( kilo * seconds ) ;
551+ function _applyStatReport ( statistic , result , lastResults ) {
552+ var item ;
553+ // mediaType is deprecated in the W3C webrtc-stats spec and replaced by kind.
554+ // Safari/WebKit omits mediaType on some reports and provides only kind, so we
555+ // normalize once and use it for both the statistic lookup and the 'video' check.
556+ var mediaType = result . mediaType || result . kind ;
557+
558+ if ( result . bytesReceived && result . type === 'inbound-rtp' ) {
559+ item = statistic . remote [ mediaType ] ;
560+
561+ if ( item ) {
562+ item . bitrate = _getBitratePerSecond ( result , lastResults , false ) ;
563+ item . bytesReceived = result . bytesReceived ;
564+ item . packetsReceived = result . packetsReceived ;
565+ item . timestamp = result . timestamp ;
566+ if ( mediaType === 'video' && result . framerateMean ) {
567+ item . framesPerSecond = Math . round ( result . framerateMean * 10 ) / 10 ;
568+ }
569+ } else {
570+ Helpers . traceWarning ( '_getStats: skipping inbound-rtp report with unknown mediaType/kind: ' + mediaType ) ;
571+ }
572+ } else if ( result . bytesSent && result . type === 'outbound-rtp' ) {
573+ item = statistic . local [ mediaType ] ;
574+
575+ if ( item ) {
576+ item . bitrate = _getBitratePerSecond ( result , lastResults , true ) ;
577+ item . bytesSent = result . bytesSent ;
578+ item . packetsSent = result . packetsSent ;
579+ item . timestamp = result . timestamp ;
580+ if ( mediaType === 'video' && result . framerateMean ) {
581+ item . framesPerSecond = Math . round ( result . framerateMean * 10 ) / 10 ;
582+ }
583+ } else {
584+ Helpers . traceWarning ( '_getStats: skipping outbound-rtp report with unknown mediaType/kind: ' + mediaType ) ;
585+ }
586+ } else if ( result . type === 'local-candidate' ) {
587+ item = statistic . local . candidate ;
588+ if ( result . candidateType === 'host' && result . mozLocalTransport === 'udp' && result . transport === 'udp' ) {
589+ item . protocol = result . transport ;
590+ item . ip = result . ipAddress ;
591+ item . port = result . portNumber ;
592+ } else if ( ! Helpers . getVersionFirefox ( ) ) {
593+ item . protocol = result . protocol ;
594+ item . ip = result . ip ;
595+ item . port = result . port ;
596+ }
597+ } else if ( result . type === 'remote-candidate' ) {
598+ item = statistic . remote . candidate ;
599+ item . protocol = result . protocol || result . transport ;
600+ item . ip = result . ip || result . ipAddress ;
601+ item . port = result . port || result . portNumber ;
602+ } else if ( result . type === 'track' && result . kind === 'video' && ! Helpers . getVersionFirefox ( ) ) {
603+ if ( result . remoteSource ) {
604+ item = statistic . remote . video ;
605+ item . frameHeight = result . frameHeight ;
606+ item . frameWidth = result . frameWidth ;
607+ item . framesPerSecond = _getFramesPerSecond ( result , lastResults , false ) ;
609608 } else {
610- bitrate = bit * ( result . bytesReceived - lastResult . bytesReceived ) / ( kilo * seconds ) ;
609+ item = statistic . local . video ;
610+ item . frameHeight = result . frameHeight ;
611+ item . frameWidth = result . frameWidth ;
612+ item . framesPerSecond = _getFramesPerSecond ( result , lastResults , true ) ;
611613 }
614+ }
615+ }
612616
613- return Math . round ( bitrate ) ;
617+ function _getBitratePerSecond ( result , lastResults , isLocal ) {
618+ var lastResult = lastResults && lastResults . get ( result . id ) ,
619+ seconds = lastResult ? ( ( result . timestamp - lastResult . timestamp ) / 1000 ) : 5 ,
620+ kilo = 1024 ,
621+ bit = 8 ,
622+ bitrate ;
623+
624+ if ( ! lastResult ) {
625+ bitrate = 0 ;
626+ } else if ( isLocal ) {
627+ bitrate = bit * ( result . bytesSent - lastResult . bytesSent ) / ( kilo * seconds ) ;
628+ } else {
629+ bitrate = bit * ( result . bytesReceived - lastResult . bytesReceived ) / ( kilo * seconds ) ;
614630 }
615631
616- function _getFramesPerSecond ( result , lastResults , isLocal ) {
617- var lastResult = lastResults && lastResults . get ( result . id ) ,
618- seconds = lastResult ? ( ( result . timestamp - lastResult . timestamp ) / 1000 ) : 5 ,
619- framesPerSecond ;
632+ return Math . round ( bitrate ) ;
633+ }
620634
621- if ( ! lastResult ) {
622- framesPerSecond = 0 ;
623- } else if ( isLocal ) {
624- framesPerSecond = ( result . framesSent - lastResult . framesSent ) / seconds ;
625- } else {
626- framesPerSecond = ( result . framesReceived - lastResult . framesReceived ) / seconds ;
627- }
635+ function _getFramesPerSecond ( result , lastResults , isLocal ) {
636+ var lastResult = lastResults && lastResults . get ( result . id ) ,
637+ seconds = lastResult ? ( ( result . timestamp - lastResult . timestamp ) / 1000 ) : 5 ,
638+ framesPerSecond ;
628639
629- return Math . round ( framesPerSecond * 10 ) / 10 ;
640+ if ( ! lastResult ) {
641+ framesPerSecond = 0 ;
642+ } else if ( isLocal ) {
643+ framesPerSecond = ( result . framesSent - lastResult . framesSent ) / seconds ;
644+ } else {
645+ framesPerSecond = ( result . framesReceived - lastResult . framesReceived ) / seconds ;
630646 }
647+
648+ return Math . round ( framesPerSecond * 10 ) / 10 ;
631649}
632650
633651// Find the line in sdpLines[startLine...endLine - 1] that starts with |prefix|
@@ -814,4 +832,7 @@ function setMediaBitrate(sdp, media, bitrate) {
814832 return newLines . join ( '\n' ) ;
815833}
816834
835+ // PRIVATE - exposed for unit tests only, not part of the public SDK contract.
836+ qbRTCPeerConnection . _applyStatReport = _applyStatReport ;
837+
817838module . exports = qbRTCPeerConnection ;
0 commit comments