Skip to content

Commit 9b0bcee

Browse files
author
Isaac
committed
Call debugging
1 parent da2849d commit 9b0bcee

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

submodules/TelegramCallsUI/Sources/PresentationGroupCall.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,15 @@ private final class ConferenceCallE2EContextStateImpl: ConferenceCallE2EContextS
335335
func getParticipants() -> [ConferenceCallE2EContext.BlockchainParticipant] {
336336
return self.call.participants().map { ConferenceCallE2EContext.BlockchainParticipant(userId: $0.userId, internalId: $0.internalId) }
337337
}
338+
339+
func getParticipantLatencies() -> [Int64: Double] {
340+
let dict = self.call.participantLatencies()
341+
var result: [Int64: Double] = [:]
342+
for (k, v) in dict {
343+
result[k.int64Value] = v.doubleValue
344+
}
345+
return result
346+
}
338347

339348
func getParticipantIds() -> [Int64] {
340349
return self.call.participants().map { $0.userId }

submodules/TelegramCore/Sources/State/ConferenceCallE2EContext.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public protocol ConferenceCallE2EContextState: AnyObject {
55
func getEmojiState() -> Data?
66
func getParticipantIds() -> [Int64]
77
func getParticipants() -> [ConferenceCallE2EContext.BlockchainParticipant]
8+
func getParticipantLatencies() -> [Int64: Double]
89

910
func applyBlock(block: Data)
1011
func applyBroadcastBlock(block: Data)
@@ -166,7 +167,7 @@ public final class ConferenceCallE2EContext {
166167
let keyPair = self.keyPair
167168
let userId = self.userId
168169
let initializeState = self.initializeState
169-
let (outBlocks, outEmoji, outBlockchainParticipants) = self.state.with({ callState -> ([Data], Data, [BlockchainParticipant]) in
170+
let (outBlocks, outEmoji, outBlockchainParticipants, participantLatencies) = self.state.with({ callState -> ([Data], Data, [BlockchainParticipant], [Int64: Double]) in
170171
if let state = callState.state {
171172
for block in blocks {
172173
if subChainId == 0 {
@@ -175,26 +176,26 @@ public final class ConferenceCallE2EContext {
175176
state.applyBroadcastBlock(block: block)
176177
}
177178
}
178-
return (state.takeOutgoingBroadcastBlocks(), state.getEmojiState() ?? Data(), state.getParticipants())
179+
return (state.takeOutgoingBroadcastBlocks(), state.getEmojiState() ?? Data(), state.getParticipants(), state.getParticipantLatencies())
179180
} else {
180181
if subChainId == 0 {
181182
guard let block = blocks.last else {
182-
return ([], Data(), [])
183+
return ([], Data(), [], [:])
183184
}
184185
guard let state = initializeState(keyPair, userId, block) else {
185-
return ([], Data(), [])
186+
return ([], Data(), [], [:])
186187
}
187188
callState.state = state
188189
for block in callState.pendingIncomingBroadcastBlocks {
189190
state.applyBroadcastBlock(block: block)
190191
}
191192
callState.pendingIncomingBroadcastBlocks.removeAll()
192-
return (state.takeOutgoingBroadcastBlocks(), state.getEmojiState() ?? Data(), state.getParticipants())
193+
return (state.takeOutgoingBroadcastBlocks(), state.getEmojiState() ?? Data(), state.getParticipants(), state.getParticipantLatencies())
193194
} else if subChainId == 1 {
194195
callState.pendingIncomingBroadcastBlocks.append(contentsOf: blocks)
195-
return ([], Data(), [])
196+
return ([], Data(), [], [:])
196197
} else {
197-
return ([], Data(), [])
198+
return ([], Data(), [], [:])
198199
}
199200
}
200201
})
@@ -204,6 +205,10 @@ public final class ConferenceCallE2EContext {
204205
for outBlock in outBlocks {
205206
let _ = self.engine.calls.sendConferenceCallBroadcast(callId: self.callId, accessHash: self.accessHash, block: outBlock).startStandalone()
206207
}
208+
209+
#if DEBUG
210+
print("Latencies: \(participantLatencies)")
211+
#endif
207212
}
208213

209214
private func e2ePoll(subChainId: Int) {

third-party/td/TdBinding/Public/TdBinding/TdBinding.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ NS_ASSUME_NONNULL_BEGIN
3737
- (NSData *)emojiState;
3838
- (NSArray<TdCallParticipant *> *)participants;
3939

40+
- (NSDictionary<NSNumber *, NSNumber *> *)participantLatencies;
41+
4042
- (void)applyBlock:(NSData *)block;
4143
- (void)applyBroadcastBlock:(NSData *)block;
4244

third-party/td/TdBinding/Sources/TdBinding.mm

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,52 @@ - (NSData *)emojiState {
190190
return participants;
191191
}
192192

193+
- (NSDictionary<NSNumber *, NSNumber *> *)participantLatencies {
194+
auto describeResult = tde2e_api::call_describe(_callId);
195+
if (describeResult.is_ok()) {
196+
NSString *string = [[NSString alloc] initWithData:[NSData dataWithBytes:describeResult.value().data() length:describeResult.value().size()] encoding:NSASCIIStringEncoding];
197+
198+
NSRegularExpression *pairRe = [NSRegularExpression regularExpressionWithPattern:@"(\\d+):(\\d+\\.\\d+)s" options:0 error:NULL];
199+
200+
NSMutableDictionary<NSNumber*, NSNumber*> *commitTimes = [NSMutableDictionary dictionary];
201+
NSMutableDictionary<NSNumber*, NSNumber*> *revealTimes = [NSMutableDictionary dictionary];
202+
203+
// split into lines and look for the two lines
204+
[string enumerateLinesUsingBlock:^(NSString * _Nonnull line, BOOL * _Nonnull stop) {
205+
if ([line containsString:@"commit ="]) {
206+
[pairRe enumerateMatchesInString:line options:0 range:NSMakeRange(0, line.length) usingBlock:^(NSTextCheckingResult * _Nullable match, NSMatchingFlags flags, BOOL * _Nonnull stop) {
207+
NSString *userIdStr = [line substringWithRange:[match rangeAtIndex:1]];
208+
NSString *durStr = [line substringWithRange:[match rangeAtIndex:2]];
209+
NSNumber *uid = @([userIdStr longLongValue]);
210+
NSNumber *dur = @([durStr doubleValue]);
211+
commitTimes[uid] = dur;
212+
}];
213+
}
214+
else if ([line containsString:@"reveal ="]) {
215+
[pairRe enumerateMatchesInString:line options:0 range:NSMakeRange(0, line.length) usingBlock:^(NSTextCheckingResult * _Nullable match, NSMatchingFlags flags, BOOL * _Nonnull stop) {
216+
NSString *userIdStr = [line substringWithRange:[match rangeAtIndex:1]];
217+
NSString *durStr = [line substringWithRange:[match rangeAtIndex:2]];
218+
NSNumber *uid = @([userIdStr longLongValue]);
219+
NSNumber *dur = @([durStr doubleValue]);
220+
revealTimes[uid] = dur;
221+
}];
222+
}
223+
}];
224+
225+
// build final result = commit+reveal
226+
NSMutableDictionary<NSNumber*, NSNumber*> *result = [NSMutableDictionary dictionary];
227+
for (NSNumber *uid in commitTimes) {
228+
double commit = commitTimes[uid].doubleValue;
229+
double reveal = revealTimes[uid].doubleValue; // will be 0 if missing
230+
result[uid] = @(commit + reveal);
231+
}
232+
233+
return result;
234+
}
235+
236+
return @{};
237+
}
238+
193239
- (void)applyBlock:(NSData *)block {
194240
std::string mappedBlock((uint8_t *)block.bytes, ((uint8_t *)block.bytes) + block.length);
195241

0 commit comments

Comments
 (0)