forked from AgoraIO/API-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatisticsInfo.swift
More file actions
executable file
·159 lines (137 loc) · 4.88 KB
/
StatisticsInfo.swift
File metadata and controls
executable file
·159 lines (137 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// MediaInfo.swift
// OpenVideoCall
//
// Created by GongYuhua on 4/11/16.
// Copyright © 2016 Agora. All rights reserved.
//
import Foundation
import AgoraRtcKit
struct StatisticsInfo {
struct LocalInfo {
var channelStats = AgoraChannelStats()
var videoStats = AgoraRtcLocalVideoStats()
var audioStats = AgoraRtcLocalAudioStats()
}
struct RemoteInfo {
var videoStats = AgoraRtcRemoteVideoStats()
var audioStats = AgoraRtcRemoteAudioStats()
}
enum StatisticsType {
case local(LocalInfo), remote(RemoteInfo)
var isLocal: Bool {
switch self {
case .local: return true
case .remote: return false
}
}
}
var dimension = CGSize.zero
var fps:UInt = 0
var type: StatisticsType
init(type: StatisticsType) {
self.type = type
}
mutating func updateChannelStats(_ stats: AgoraChannelStats) {
guard self.type.isLocal else {
return
}
switch type {
case .local(let info):
var new = info
new.channelStats = stats
self.type = .local(new)
default:
break
}
}
mutating func updateLocalVideoStats(_ stats: AgoraRtcLocalVideoStats) {
guard self.type.isLocal else {
return
}
switch type {
case .local(let info):
var new = info
new.videoStats = stats
self.type = .local(new)
default:
break
}
dimension = CGSize(width: Int(stats.encodedFrameWidth), height: Int(stats.encodedFrameHeight))
fps = stats.sentFrameRate
}
mutating func updateLocalAudioStats(_ stats: AgoraRtcLocalAudioStats) {
guard self.type.isLocal else {
return
}
switch type {
case .local(let info):
var new = info
new.audioStats = stats
self.type = .local(new)
default:
break
}
}
mutating func updateVideoStats(_ stats: AgoraRtcRemoteVideoStats) {
switch type {
case .remote(let info):
var new = info
new.videoStats = stats
dimension = CGSize(width: Int(stats.width), height: Int(stats.height))
fps = stats.rendererOutputFrameRate
self.type = .remote(new)
default:
break
}
}
mutating func updateAudioStats(_ stats: AgoraRtcRemoteAudioStats) {
switch type {
case .remote(let info):
var new = info
new.audioStats = stats
self.type = .remote(new)
default:
break
}
}
func description(audioOnly:Bool) -> String {
var full: String
switch type {
case .local(let info): full = localDescription(info: info, audioOnly: audioOnly)
case .remote(let info): full = remoteDescription(info: info, audioOnly: audioOnly)
}
return full
}
func localDescription(info: LocalInfo, audioOnly: Bool) -> String {
let dimensionFps = "\(Int(dimension.width))×\(Int(dimension.height)),\(fps)fps"
let lastmile = "LM Delay: \(info.channelStats.lastmileDelay)ms"
let videoSend = "VSend: \(info.videoStats.sentBitrate)kbps"
let audioSend = "ASend: \(info.audioStats.sentBitrate)kbps"
let cpu = "CPU: \(info.channelStats.cpuAppUsage)%/\(info.channelStats.cpuTotalUsage)%"
let vSendLoss = "VSend Loss: \(info.videoStats.txPacketLossRate)%"
let aSendLoss = "ASend Loss: \(info.audioStats.txPacketLossRate)%"
if(audioOnly) {
return [lastmile,audioSend,cpu,aSendLoss].joined(separator: "\n")
}
return [dimensionFps,lastmile,videoSend,audioSend,cpu,vSendLoss,aSendLoss].joined(separator: "\n")
}
func remoteDescription(info: RemoteInfo, audioOnly: Bool) -> String {
let dimensionFpsBit = "\(Int(dimension.width))×\(Int(dimension.height)), \(fps)fps"
var audioQuality: AgoraNetworkQuality
if let quality = AgoraNetworkQuality(rawValue: info.audioStats.quality) {
audioQuality = quality
} else {
audioQuality = AgoraNetworkQuality.unknown
}
let videoRecv = "VRecv: \(info.videoStats.receivedBitrate)kbps"
let audioRecv = "ARecv: \(info.audioStats.receivedBitrate)kbps"
let videoLoss = "VLoss: \(info.videoStats.packetLossRate)%"
let audioLoss = "ALoss: \(info.audioStats.audioLossRate)%"
let aquality = "AQuality: \(audioQuality.description())"
if(audioOnly) {
return [audioRecv,audioLoss,aquality].joined(separator: "\n")
}
return [dimensionFpsBit,videoRecv,audioRecv,videoLoss,audioLoss,aquality].joined(separator: "\n")
}
}