How to getStats() in WebRTC iOS SDK in Swift?

1.2k Views Asked by At

I am trying to get ICE Candidate Pair Stats in iOS SDK in Swift. I see this specific interface: statsForTrack but I am unsure about how to use it. Has anybody done this before?

2

There are 2 best solutions below

0
On

In your class where you creating peerConnection there you can start a timer like below:

statsTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
               target:self
               selector:@selector(getStats)
               userInfo:nil
               repeats:YES];

And getStats be like this:

    - (void)getStats {
        
        NSArray<RTCMediaStreamTrack *> *tracks = mediaStream.videoTracks;
        tracks = [tracks arrayByAddingObjectsFromArray:// Here mediaStream is instance of RTCMediaStream (NSArray<RTCMediaStreamTrack *> *)_publishStream.mediaStream.audioTracks];
    
        for (RTCMediaStreamTrack *track in tracks) {
            [peerConnection statsForTrack:track
                                       statsOutputLevel:RTCStatsOutputLevelStandard
                                      completionHandler:^(NSArray<RTCLegacyStatsReport *> * _Nonnull stats) {
    
        for (RTCLegacyStatsReport *stat in stats) {
           if ([stat.type isEqualToString:kRTCStatsTypeSSRC]) {
            [self processRTCLegacyStatsReport:stat];
            [statsBySSRC setObject:@{           kRTCStatsBytesSent: [stat.values objectForKey:kRTCStatsBytesSent],
kRTCStatsLastDate: [NSDate date]
            } forKey:[stat.values objectForKey:kRTCStatsTypeSSRC]];
          }
        }
      }];
     }
   }
    
    - (void)processRTCLegacyStatsReport:(RTCLegacyStatsReport *)statsReport {
    
        NSString *ssrc = [statsReport.values objectForKey:kRTCStatsTypeSSRC];
        NSString *mediaType = [statsReport.values objectForKey:kRTCStatsMediaTypeKey];
        unsigned long kbps = [self calculateBitrateForStatsReport:statsReport];
            L_INFO(@"RTC Publishing %@ Stats Type: %@, ID: %@ Dict: %@",
               mediaType, statsReport.type, statsReport.reportId, statsReport.values);
        L_INFO(@"RTC Publishing %@ kbps: %lld", mediaType, kbps)
    
    }
    
    - (unsigned long)calculateBitrateForStatsReport:(RTCLegacyStatsReport *)statsReport {
    
        NSString *ssrc = [statsReport.values objectForKey:kRTCStatsTypeSSRC];
        unsigned long bytesSent = [[statsReport.values objectForKey:kRTCStatsBytesSent] intValue];
        unsigned long lastBytesSent = [[[statsBySSRC objectForKey:ssrc] objectForKey:kRTCStatsBytesSent] intValue];
        NSDate *lastStatsDate = [[statsBySSRC objectForKey:ssrc] objectForKey:kRTCStatsLastDate];
        NSTimeInterval seconds = [lastStatsDate timeIntervalSinceNow];
        unsigned long kbps = (((bytesSent - lastBytesSent) * 8) / fabs(seconds)) / 1000.0;
        return kbps;
    }
0
On

you can use stats function in PeerConnection like this:

peerConnection.stats(for: RTCMediaStreamTrack?, statsOutputLevel: .debug, completionHandler: { reports in

// reports here
}

you can set nil for parameter RTCMediaStreamTrack for get all reports or set special mediaStreamTack to get reports only this mediaStream.

peerConnection.stats(for: nil, statsOutputLevel: .debug, completionHandler: { reports in

// reports all mediaStreams
}