Skip to content

Commit c8bcd8b

Browse files
authored
Implement PeerConnection.getStats() for iOS (#161)
1 parent ee4db28 commit c8bcd8b

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

android/src/main/kotlin/com/instrumentisto/medea_flutter_webrtc/model/RtcStats.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ data class RtcStats(
2828

2929
/** Converts these [RtcStats] into a [Map] which can be returned to the Flutter side. */
3030
fun asFlutterResult(): Map<String, Any> {
31-
return mapOf("id" to id, "timestampUs" to timestampUs, "kind" to kind, "type" to type)
31+
var report = mapOf("id" to id, "timestampUs" to timestampUs, "type" to type)
32+
return report + kind
3233
}
3334
}

example/integration_test/webrtc_test.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,10 +1186,6 @@ void main() {
11861186
});
11871187

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

ios/Classes/controller/PeerConnectionController.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ class PeerConnectionController {
139139
self.sendResultFromTask(result, getFlutterError(error))
140140
}
141141
}
142+
case "getStats":
143+
Task {
144+
do {
145+
let report = try await self.peer.getStats()
146+
self.sendResultFromTask(result, report.asFlutterResult())
147+
} catch {
148+
self.sendResultFromTask(result, getFlutterError(error))
149+
}
150+
}
142151
case "addTransceiver":
143152
let mediaType = argsMap!["mediaType"] as? Int
144153
let initArgs = argsMap!["init"] as? [String: Any]

ios/Classes/model/RtcStats.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// Representation of an `RTCStatisticsReport`.
2+
class RtcStats {
3+
/// List of all RTC stats reports converted to flat `Map`.
4+
var statsList: [[String: Any]] = []
5+
6+
/// Converts the provided `RTCStatisticsReport` into `RtcStats`.
7+
init(report: RTCStatisticsReport) {
8+
for (_, stats) in report.statistics {
9+
var statDetails: [String: Any] = [:]
10+
statDetails["id"] = stats.id
11+
statDetails["type"] = stats.type
12+
statDetails["timestampUs"] = Int(stats.timestamp_us)
13+
14+
for (statName, statValue) in stats.values {
15+
statDetails[statName] = statValue
16+
}
17+
18+
self.statsList.append(statDetails)
19+
}
20+
}
21+
22+
/// Converts these `RtcStats` into a `Map` which can be returned to the
23+
/// Flutter side.
24+
func asFlutterResult() -> [[String: Any]] {
25+
return self.statsList
26+
}
27+
}

ios/Classes/proxy/PeerConnectionProxy.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ class PeerConnectionProxy {
3939
self.id
4040
}
4141

42+
/// Returns `RtcStats` of this `PeerConnectionProxy`.
43+
func getStats() async throws -> RtcStats {
44+
return try await withCheckedThrowingContinuation { continuation in
45+
do {
46+
try self.peer.statistics { report in
47+
continuation.resume(returning: RtcStats(report: report))
48+
}
49+
} catch {
50+
continuation.resume(throwing: error)
51+
}
52+
}
53+
}
54+
4255
/// Synchronizes and returns all the `RtpTransceiverProxy`s of this
4356
/// `PeerConnectionProxy`.
4457
func getTransceivers() -> [RtpTransceiverProxy] {

lib/src/model/stats.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ class RtcStats {
5454

5555
/// Creates [RTCStats] basing on the [Map] received from the native side.
5656
static RtcStats? fromMap(dynamic stats) {
57-
stats['kind']['type'] = stats['type'];
58-
var kind = RtcStatsType.fromMap(stats['kind']);
57+
var kind = RtcStatsType.fromMap(stats);
5958
if (kind == null) {
6059
return null;
6160
} else {

0 commit comments

Comments
 (0)