1

我正在尝试在 Swift 的 iOS SDK 中获取 ICE 候选配对统计信息。我看到了这个特定的界面:statsForTrack但我不确定如何使用它。以前有人做过吗?

4

2 回答 2

1

您可以stats像这样在 PeerConnection 中使用函数:

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

// reports here
}

您可以为参数 RTCMediaStreamTrack 设置 nil 以获取所有报告或设置特殊 mediaStreamTack 以仅获取此媒体流的报告。

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

// reports all mediaStreams
}
于 2021-03-02T18:01:22.253 回答
0

在您创建 peerConnection 的班级中,您可以启动一个计时器,如下所示:

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

getStats 是这样的:

    - (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;
    }
于 2021-03-03T05:42:05.327 回答