-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve statistics collection (#281)
- Loading branch information
1 parent
4cd59c8
commit de11e14
Showing
11 changed files
with
1,042 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
Sources/StreamVideo/WebRTC/Statistics/Statistics+Convenience.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// | ||
// Copyright © 2024 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import StreamWebRTC | ||
|
||
/// A wrapper around RTCStatistics that can be used to easily access its properties. | ||
@dynamicMemberLookup | ||
struct StreamRTCStatistics { | ||
/// A structure providing a type-safe way to access values from RTCStatistics' values dictionary. | ||
@objcMembers final class CodingKeys: NSObject { | ||
var trackIdentifier: String = "" | ||
var codecId: String = "" | ||
var mimeType: String = "" | ||
var bytesSent: Int = 0 | ||
var bytesReceived: Int = 0 | ||
var jitter: Double = 0 | ||
var currentRoundTripTime: Double = 0 | ||
var qualityLimitationReason: String = "" | ||
var frameWidth: Int = 0 | ||
var frameHeight: Int = 0 | ||
var framesPerSecond: Int = 0 | ||
var transportId: String = "" | ||
var dtlsState: String = "" | ||
var selectedCandidatePairId: String = "" | ||
var kind: String = "" | ||
var rid: String = "" | ||
var ssrc: Int = 0 | ||
} | ||
|
||
private let source: StreamStatisticsProtocol? | ||
|
||
/// The of the `source` or empty. | ||
var type: String { source?.type ?? "" } | ||
|
||
/// The id of the `source` or empty. | ||
var id: String { source?.id ?? "" } | ||
|
||
init?(_ source: StreamStatisticsProtocol?) { | ||
guard let source else { return nil } | ||
self.source = source | ||
} | ||
|
||
/// A handy way to access values from the source's `values` storage. | ||
subscript<T>(dynamicMember keyPath: KeyPath<CodingKeys, T>) -> T? { | ||
/// Implement logic to dynamically access the members of RTCStatisticsReport | ||
let value = NSExpression(forKeyPath: keyPath).keyPath | ||
return source?.values[value] as? T | ||
} | ||
} | ||
|
||
/// A wrapper around RTCStatisticsReport that can be used to easily access its properties. | ||
struct StreamRTCStatisticsReport { | ||
var statistics: [StreamRTCStatistics] | ||
var timestamp: TimeInterval | ||
var source: RTCStatisticsReport? | ||
|
||
init(_ source: RTCStatisticsReport?) { | ||
self.init( | ||
statistics: source?.statistics.compactMap { StreamRTCStatistics($0.value) } ?? [], | ||
timestamp: source?.timestamp_us ?? Date().timeIntervalSince1970, | ||
source: source | ||
) | ||
} | ||
|
||
init( | ||
statistics: [StreamRTCStatistics], | ||
timestamp: TimeInterval, | ||
source: RTCStatisticsReport? | ||
) { | ||
self.statistics = statistics | ||
self.timestamp = timestamp | ||
self.source = source | ||
} | ||
} | ||
|
||
/// Describes and object that can be queried to get information about a RTCStatistics source. | ||
protocol StreamStatisticsProtocol { | ||
|
||
var type: String { get } | ||
|
||
var id: String { get } | ||
|
||
var values: [String: NSObject] { get } | ||
} | ||
|
||
extension RTCStatistics: StreamStatisticsProtocol {} |
Oops, something went wrong.