Skip to content

Commit

Permalink
Implement PeerConnection.getStats() for iOS (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
evdokimovs authored May 30, 2024
1 parent ee4db28 commit c8bcd8b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ data class RtcStats(

/** Converts these [RtcStats] into a [Map] which can be returned to the Flutter side. */
fun asFlutterResult(): Map<String, Any> {
return mapOf("id" to id, "timestampUs" to timestampUs, "kind" to kind, "type" to type)
var report = mapOf("id" to id, "timestampUs" to timestampUs, "type" to type)
return report + kind
}
}
4 changes: 0 additions & 4 deletions example/integration_test/webrtc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1186,10 +1186,6 @@ void main() {
});

testWidgets('Peer connection get stats.', (WidgetTester tester) async {
// TODO: Support stats for iOS platform.
if (Platform.isIOS) {
return;
}
var pc1 = await PeerConnection.create(IceTransportType.all, []);
var pc2 = await PeerConnection.create(IceTransportType.all, []);

Expand Down
9 changes: 9 additions & 0 deletions ios/Classes/controller/PeerConnectionController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ class PeerConnectionController {
self.sendResultFromTask(result, getFlutterError(error))
}
}
case "getStats":
Task {
do {
let report = try await self.peer.getStats()
self.sendResultFromTask(result, report.asFlutterResult())
} catch {
self.sendResultFromTask(result, getFlutterError(error))
}
}
case "addTransceiver":
let mediaType = argsMap!["mediaType"] as? Int
let initArgs = argsMap!["init"] as? [String: Any]
Expand Down
27 changes: 27 additions & 0 deletions ios/Classes/model/RtcStats.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// Representation of an `RTCStatisticsReport`.
class RtcStats {
/// List of all RTC stats reports converted to flat `Map`.
var statsList: [[String: Any]] = []

/// Converts the provided `RTCStatisticsReport` into `RtcStats`.
init(report: RTCStatisticsReport) {
for (_, stats) in report.statistics {
var statDetails: [String: Any] = [:]
statDetails["id"] = stats.id
statDetails["type"] = stats.type
statDetails["timestampUs"] = Int(stats.timestamp_us)

for (statName, statValue) in stats.values {
statDetails[statName] = statValue
}

self.statsList.append(statDetails)
}
}

/// Converts these `RtcStats` into a `Map` which can be returned to the
/// Flutter side.
func asFlutterResult() -> [[String: Any]] {
return self.statsList
}
}
13 changes: 13 additions & 0 deletions ios/Classes/proxy/PeerConnectionProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ class PeerConnectionProxy {
self.id
}

/// Returns `RtcStats` of this `PeerConnectionProxy`.
func getStats() async throws -> RtcStats {
return try await withCheckedThrowingContinuation { continuation in
do {
try self.peer.statistics { report in
continuation.resume(returning: RtcStats(report: report))
}
} catch {
continuation.resume(throwing: error)
}
}
}

/// Synchronizes and returns all the `RtpTransceiverProxy`s of this
/// `PeerConnectionProxy`.
func getTransceivers() -> [RtpTransceiverProxy] {
Expand Down
3 changes: 1 addition & 2 deletions lib/src/model/stats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ class RtcStats {

/// Creates [RTCStats] basing on the [Map] received from the native side.
static RtcStats? fromMap(dynamic stats) {
stats['kind']['type'] = stats['type'];
var kind = RtcStatsType.fromMap(stats['kind']);
var kind = RtcStatsType.fromMap(stats);
if (kind == null) {
return null;
} else {
Expand Down

0 comments on commit c8bcd8b

Please sign in to comment.