Skip to content

Commit

Permalink
Merge pull request #145 from team-telnyx/feat/WEBRTC-2347-stats
Browse files Browse the repository at this point in the history
[WEBRTC-2347] [feat] Stats
  • Loading branch information
gbattistel authored Jan 9, 2025
2 parents 6b7a4cd + 01fee33 commit 0fce8a4
Show file tree
Hide file tree
Showing 33 changed files with 915 additions and 383 deletions.
234 changes: 176 additions & 58 deletions TelnyxRTC.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions TelnyxRTC/Telnyx/Extensions/Dictionary+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// MARK: - Dictionary
extension Dictionary {

var telnyx_webrtc_json: String {
let invalidJson = "Not a valid JSON"
do {
let jsonData = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return String(bytes: jsonData, encoding: String.Encoding.utf8) ?? invalidJson
} catch {
return invalidJson
}
}

func printJson() {
Logger.log.i(message: "\(telnyx_webrtc_json)")
}
}
11 changes: 9 additions & 2 deletions TelnyxRTC/Telnyx/Models/TxConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public struct TxConfig {
public internal(set) var ringtone: String?
public internal(set) var reconnectClient: Bool = true
public internal(set) var pushEnvironment: PushEnvironment?

// To enable stats debuggin
public internal(set) var debug: Bool = false

// MARK: - Initializers

Expand All @@ -44,7 +47,8 @@ public struct TxConfig {
ringBackTone: String? = nil,
pushEnvironment: PushEnvironment? = nil,
logLevel: LogLevel = .none,
reconnectClient:Bool = true
reconnectClient: Bool = true,
debug: Bool = false
) {
self.sipUser = sipUser
self.password = password
Expand All @@ -56,6 +60,7 @@ public struct TxConfig {
self.ringtone = ringtone
self.reconnectClient = reconnectClient
self.pushEnvironment = pushEnvironment
self.debug = debug
Logger.log.verboseLevel = logLevel
}

Expand All @@ -72,7 +77,8 @@ public struct TxConfig {
ringtone: String? = nil,
ringBackTone: String? = nil,
pushEnvironment: PushEnvironment? = nil,
logLevel: LogLevel = .none) {
logLevel: LogLevel = .none,
debug: Bool = false) {
self.token = token
if let pushToken = pushDeviceToken {
//Create a notification configuration if there's an available a device push notification token
Expand All @@ -81,6 +87,7 @@ public struct TxConfig {
self.ringBackTone = ringBackTone
self.ringtone = ringtone
self.pushEnvironment = pushEnvironment
self.debug = debug
Logger.log.verboseLevel = logLevel
}

Expand Down
13 changes: 7 additions & 6 deletions TelnyxRTC/Telnyx/TxClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,9 @@ extension TxClient {
delegate: self,
ringtone: self.txConfig?.ringtone,
ringbackTone: self.txConfig?.ringBackTone,
iceServers: self.serverConfiguration.webRTCIceServers)
call.newCall(callerName: callerName, callerNumber: callerNumber, destinationNumber: destinationNumber, clientState: clientState,customHeaders: customHeaders)
iceServers: self.serverConfiguration.webRTCIceServers,
debug: self.txConfig?.debug ?? false)
call.newCall(callerName: callerName, callerNumber: callerNumber, destinationNumber: destinationNumber, clientState: clientState, customHeaders: customHeaders)

currentCallId = callId
self.calls[callId] = call
Expand Down Expand Up @@ -509,8 +510,8 @@ extension TxClient {
ringtone: self.txConfig?.ringtone,
ringbackTone: self.txConfig?.ringBackTone,
iceServers: self.serverConfiguration.webRTCIceServers,
isAttach: isAttach
)
isAttach: isAttach,
debug: self.txConfig?.debug ?? false)
call.callInfo?.callerName = callerName
call.callInfo?.callerNumber = callerNumber
call.callOptions = TxCallOptions(audio: true)
Expand Down Expand Up @@ -593,11 +594,11 @@ extension TxClient {
if(noActiveCalls){
do {
Logger.log.i(message: "TxClient:: No Active Calls Connecting Again")
try self.connectFromPush(txConfig: txConfig, serverConfiguration: pnServerConfig)
try self.connectFromPush(txConfig: txConfig, serverConfiguration: pnServerConfig)

// Create an initial call_object to handle early bye message
if let newCallId = (pushMetaData["call_id"] as? String) {
self.calls[UUID(uuidString: newCallId)!] = Call(callId: UUID(uuidString: newCallId)! , sessionId: newCallId, socket: self.socket!, delegate: self, iceServers: self.serverConfiguration.webRTCIceServers)
self.calls[UUID(uuidString: newCallId)!] = Call(callId: UUID(uuidString: newCallId)! , sessionId: newCallId, socket: self.socket!, delegate: self, iceServers: self.serverConfiguration.webRTCIceServers, debug: self.txConfig?.debug ?? false)
}
} catch let error {
Logger.log.e(message: "TxClient:: push flow connect error \(error.localizedDescription)")
Expand Down
11 changes: 11 additions & 0 deletions TelnyxRTC/Telnyx/Utils/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public enum LogLevel: Int {
case info
/// Print `verto` messages. Incoming and outgoing verto messages are printed.
case verto
/// Print `Debug Report` messages. Statistics of the RTCP connection
case stats
/// All the SDK logs are printed.
case all
}
Expand Down Expand Up @@ -62,6 +64,8 @@ class Logger {
/// represents the current log level: `all` is set as default
internal var verboseLevel: LogLevel = .all

private var statsGlyph: String = "\u{1F4CA}" // Glyph for messages of level .Stats

private var rightArrowGlyph: String = "\u{25B6}"
private var leftArrowGlyph: String = "\u{25C0}"

Expand Down Expand Up @@ -115,6 +119,12 @@ class Logger {
print("TxClient : \(timeStamp.printTimestamp())" + buildMessage(level: .verto, message: message, direction: direction))
}
}

public func stats(message: String) {
if verboseLevel == .all || verboseLevel == .stats {
print("TxClient : \(timeStamp.printTimestamp())" + buildMessage(level: .stats, message: message))
}
}

private func getLogGlyph(level: LogLevel, direction: VertoDirection = .none) -> String {
switch(level) {
Expand All @@ -125,6 +135,7 @@ class Logger {
case .info: return infoGlyph
case .success: return successGlyph
case .warning: return warningGlyph
case .stats: return statsGlyph
}
}

Expand Down
9 changes: 5 additions & 4 deletions TelnyxRTC/Telnyx/Verto/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Message {
}
}

private var jsonMessage: [String: Any] = [String: Any]()
internal var jsonMessage: [String: Any] = [String: Any]()

let jsonrpc = PROTOCOL_VERSION
var id: String = UUID.init().uuidString.lowercased()
Expand All @@ -31,12 +31,13 @@ class Message {
var result: [String: Any]?
var serverError: [String: Any]?

init() {}
init() {
self.jsonMessage["jsonrpc"] = self.jsonrpc
self.jsonMessage["id"] = self.id
}

init(_ params: [String: Any], method: Method) {
self.method = method

self.jsonMessage = [String: Any]()
self.jsonMessage["jsonrpc"] = self.jsonrpc
self.jsonMessage["id"] = self.id
self.jsonMessage["method"] = self.method?.rawValue
Expand Down
15 changes: 15 additions & 0 deletions TelnyxRTC/Telnyx/Verto/Stats/DebugReportDataMessage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

class DebugReportDataMessage: StatsMessage {
init(reportID: String, reportData: [String: Any]) {

var data = reportData
data["timeTaken"] = 1
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
let timestamp = dateFormatter.string(from: Date())
data["timestamp"] = timestamp
super.init(type: .DEBUG_REPORT_DATA,
reportID: reportID,
reportData: data)
}
}
8 changes: 8 additions & 0 deletions TelnyxRTC/Telnyx/Verto/Stats/DebugReportStartMessage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

class DebugReportStartMessage: StatsMessage {
init(reportID: String) {
super.init(type: .DEBUG_REPORT_START,
reportID: reportID,
reportData: nil)
}
}
8 changes: 8 additions & 0 deletions TelnyxRTC/Telnyx/Verto/Stats/DebugReportStopMessage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

class DebugReportStopMessage: StatsMessage {
init(reportID: String) {
super.init(type: .DEBUG_REPORT_STOP,
reportID: reportID,
reportData: nil)
}
}
28 changes: 28 additions & 0 deletions TelnyxRTC/Telnyx/Verto/Stats/StatsMessage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// StatsMessage.swift
// TelnyxRTC
//
// Created by Isaac Akakpo on 8/16/24.
//

import Foundation

private let DEBUG_REPORT_VERSION: Int = 1

enum StatsMessageType : String {
case DEBUG_REPORT_STOP = "debug_report_stop"
case DEBUG_REPORT_START = "debug_report_start"
case DEBUG_REPORT_DATA = "debug_report_data"
}

class StatsMessage: Message {
init(type: StatsMessageType,
reportID: String,
reportData: [String:Any]?) {
super.init()
self.jsonMessage["debug_report_version"] = DEBUG_REPORT_VERSION
self.jsonMessage["debug_report_data"] = reportData
self.jsonMessage["type"] = type.rawValue
self.jsonMessage["debug_report_id"] = reportID
}
}
40 changes: 0 additions & 40 deletions TelnyxRTC/Telnyx/Verto/StatsMessage.swift

This file was deleted.

Loading

0 comments on commit 0fce8a4

Please sign in to comment.