Skip to content

Commit f7959f8

Browse files
authored
[Enhancement]Improve Stats for Join and UpdateSubscriptions (#893)
1 parent 5cacb27 commit f7959f8

File tree

2 files changed

+91
-24
lines changed

2 files changed

+91
-24
lines changed

Sources/StreamVideo/Utils/SelectiveEncodable/Protobuf+SelectiveEncodable.swift

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44

55
import Foundation
66

7+
// MARK: - Standard Encodable Conformances
8+
9+
//
10+
// The types below are simple enums or value types and are made to conform to
11+
// Encodable using the compiler's default synthesis.
12+
13+
extension Stream_Video_Sfu_Models_TrackType: Encodable {}
14+
extension Stream_Video_Sfu_Models_CallEndedReason: Encodable {}
15+
extension Stream_Video_Sfu_Models_GoAwayReason: Encodable {}
16+
extension Stream_Video_Sfu_Models_ErrorCode: Encodable {}
17+
extension Stream_Video_Sfu_Models_WebsocketReconnectStrategy: Encodable {}
18+
719
// MARK: - SelectiveEncodable Conformances
820

921
//
@@ -33,14 +45,12 @@ extension Stream_Video_Sfu_Signal_AudioMuteChanged: SelectiveEncodable {}
3345
extension Stream_Video_Sfu_Signal_VideoMuteChanged: SelectiveEncodable {}
3446
extension Stream_Video_Sfu_Signal_UpdateSubscriptionsRequest: SelectiveEncodable {}
3547
extension Stream_Video_Sfu_Signal_UpdateSubscriptionsResponse: SelectiveEncodable {}
36-
extension Stream_Video_Sfu_Signal_TrackSubscriptionDetails: SelectiveEncodable {}
3748
extension Stream_Video_Sfu_Signal_SendAnswerRequest: SelectiveEncodable {}
3849
extension Stream_Video_Sfu_Signal_SendAnswerResponse: SelectiveEncodable {}
3950
extension Stream_Video_Sfu_Signal_ICETrickleResponse: SelectiveEncodable {}
4051
extension Stream_Video_Sfu_Signal_SetPublisherRequest: SelectiveEncodable {}
4152
extension Stream_Video_Sfu_Signal_SetPublisherResponse: SelectiveEncodable {}
4253
extension Stream_Video_Sfu_Models_ICETrickle: SelectiveEncodable {}
43-
extension Stream_Video_Sfu_Event_JoinRequest: SelectiveEncodable {}
4454
extension Stream_Video_Sfu_Event_LeaveCallRequest: SelectiveEncodable {}
4555
extension Stream_Video_Sfu_Event_ChangePublishQuality: SelectiveEncodable {}
4656
extension Stream_Video_Sfu_Event_CallEnded: SelectiveEncodable {}
@@ -51,15 +61,58 @@ extension Stream_Video_Sfu_Event_VideoSender: SelectiveEncodable {}
5161
extension Stream_Video_Sfu_Models_Codec: SelectiveEncodable {}
5262
extension Stream_Video_Sfu_Event_VideoLayerSetting: SelectiveEncodable {}
5363
extension Stream_Video_Sfu_Models_Error: SelectiveEncodable {}
64+
extension Stream_Video_Sfu_Models_VideoDimension: SelectiveEncodable {}
5465

55-
// MARK: - Standard Encodable Conformances
66+
// MARK: - Extended Selective Encodable Conformances
5667

57-
//
58-
// The types below are simple enums or value types and are made to conform to
59-
// Encodable using the compiler's default synthesis.
68+
extension Stream_Video_Sfu_Event_JoinRequest: SelectiveEncodable {
69+
struct EncodableRepresentation: SelectiveEncodable {
70+
var token: String
71+
var sessionID: String
72+
var subscriberSdp: String
73+
var publisherSdp: String
74+
var clientDetails: Stream_Video_Sfu_Models_ClientDetails
75+
var migration: Stream_Video_Sfu_Event_Migration
76+
var fastReconnect: Bool
77+
var reconnectDetails: Stream_Video_Sfu_Event_ReconnectDetails
78+
var preferredPublishOptions: [Stream_Video_Sfu_Models_PublishOption]
79+
var capabilities: [Stream_Video_Sfu_Models_ClientCapability]
6080

61-
extension Stream_Video_Sfu_Models_TrackType: Encodable {}
62-
extension Stream_Video_Sfu_Models_CallEndedReason: Encodable {}
63-
extension Stream_Video_Sfu_Models_GoAwayReason: Encodable {}
64-
extension Stream_Video_Sfu_Models_ErrorCode: Encodable {}
65-
extension Stream_Video_Sfu_Models_WebsocketReconnectStrategy: Encodable {}
81+
init(_ source: Stream_Video_Sfu_Event_JoinRequest) {
82+
token = source.token
83+
sessionID = source.sessionID
84+
subscriberSdp = source.subscriberSdp
85+
publisherSdp = source.publisherSdp
86+
clientDetails = source.clientDetails
87+
migration = source.migration
88+
fastReconnect = source.fastReconnect
89+
reconnectDetails = source.reconnectDetails
90+
preferredPublishOptions = source.preferredPublishOptions
91+
capabilities = source.capabilities
92+
}
93+
}
94+
95+
var encodableRepresentation: any Encodable {
96+
EncodableRepresentation(self)
97+
}
98+
}
99+
100+
extension Stream_Video_Sfu_Signal_TrackSubscriptionDetails: SelectiveEncodable {
101+
struct EncodableRepresentation: SelectiveEncodable {
102+
var userID: String
103+
var sessionID: String
104+
var trackType: Stream_Video_Sfu_Models_TrackType
105+
var dimension: Stream_Video_Sfu_Models_VideoDimension
106+
107+
init(_ source: Stream_Video_Sfu_Signal_TrackSubscriptionDetails) {
108+
userID = source.userID
109+
sessionID = source.sessionID
110+
trackType = source.trackType
111+
dimension = source.dimension
112+
}
113+
}
114+
115+
var encodableRepresentation: any Encodable {
116+
EncodableRepresentation(self)
117+
}
118+
}

Sources/StreamVideo/Utils/SelectiveEncodable/SelectiveEncodable.swift

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import Foundation
3434
/// - This protocol uses reflection and may have performance implications
3535
/// compared to manual encoding.
3636
protocol SelectiveEncodable: Encodable {
37+
var encodableRepresentation: any Encodable { get }
38+
3739
/// A type alias for a closure that takes a property name and returns
3840
/// whether it should be ignored during encoding.
3941
typealias IgnoreRule = (String) -> Bool
@@ -49,6 +51,8 @@ protocol SelectiveEncodable: Encodable {
4951
}
5052

5153
extension SelectiveEncodable {
54+
var encodableRepresentation: any Encodable { self }
55+
5256
/// Default ignore rules for property names.
5357
///
5458
/// Properties named `unknownFields` or starting with `-` are ignored.
@@ -93,25 +97,35 @@ extension SelectiveEncodable {
9397
// Create a keyed container using a generic coding key type.
9498
var container = encoder.container(keyedBy: SelectiveEncodableCodingKeys.self)
9599
// Reflect on self to access all stored properties.
96-
let mirror = Mirror(reflecting: self)
100+
let mirror = Mirror(reflecting: encodableRepresentation)
97101

98102
// Iterate over each child property.
99103
for child in mirror.children {
100-
// Skip properties without a label or those that should be ignored.
101-
guard
102-
let label = child.label,
103-
!shouldIgnoreProperty(label)
104-
else {
105-
continue
104+
do {
105+
try process(child, container: &container)
106+
} catch {
107+
log.warning("Unable to json encode property:\(child.label) on type:\(type(of: self)).")
106108
}
109+
}
110+
}
111+
112+
private func process(
113+
_ child: Mirror.Child,
114+
container: inout KeyedEncodingContainer<SelectiveEncodableCodingKeys>
115+
) throws {
116+
guard
117+
let label = child.label,
118+
// Skip properties without a label or those that should be ignored.
119+
!shouldIgnoreProperty(label),
107120
// Construct a coding key from the property name.
108-
if let codingKey = SelectiveEncodableCodingKeys(stringValue: label) {
109-
// Attempt to encode the property value if it conforms to Encodable.
110-
if let value = child.value as? Encodable {
111-
try value.encode(to: container.superEncoder(forKey: codingKey))
112-
}
113-
}
121+
let codingKey = SelectiveEncodableCodingKeys(stringValue: label),
122+
// Attempt to encode the property value if it conforms to Encodable.
123+
let value = child.value as? Encodable
124+
else {
125+
return
114126
}
127+
128+
try value.encode(to: container.superEncoder(forKey: codingKey))
115129
}
116130
}
117131

0 commit comments

Comments
 (0)