forked from AgoraIO/API-Examples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatisticsInfo.swift
More file actions
executable file
·197 lines (172 loc) · 6.46 KB
/
StatisticsInfo.swift
File metadata and controls
executable file
·197 lines (172 loc) · 6.46 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// 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?
var audioVolume : UInt?
}
struct RemoteInfo {
var videoStats : AgoraRtcRemoteVideoStats?
var audioStats : AgoraRtcRemoteAudioStats?
var audioVolume : UInt?
}
enum StatisticsType {
case local(LocalInfo), remote(RemoteInfo)
var isLocal: Bool {
switch self {
case .local: return true
case .remote: return false
}
}
}
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
}
}
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
}
}
mutating func updateVolume(_ volume: UInt) {
switch type {
case .local(let info):
var new = info
new.audioVolume = volume
self.type = .local(new)
case .remote(let info):
var new = info
new.audioVolume = volume
self.type = .remote(new)
}
}
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 {
var results:[String] = []
if(!audioOnly) {
if let volume = info.audioVolume {
results.append("Volume: \(volume)")
}
if let videoStats = info.videoStats, let channelStats = info.channelStats, let audioStats = info.audioStats {
results.append("\(Int(videoStats.encodedFrameWidth))×\(Int(videoStats.encodedFrameHeight)),\(videoStats.sentFrameRate)fps")
results.append("LM Delay: \(channelStats.lastmileDelay)ms")
results.append("VSend: \(videoStats.sentBitrate)kbps")
results.append("ASend: \(audioStats.sentBitrate)kbps")
results.append("CPU: \(channelStats.cpuAppUsage)%/\(channelStats.cpuTotalUsage)%")
results.append("VSend Loss: \(videoStats.txPacketLossRate)%")
results.append("ASend Loss: \(audioStats.txPacketLossRate)%")
}
} else {
if let volume = info.audioVolume {
results.append("Volume: \(volume)")
}
if let channelStats = info.channelStats, let audioStats = info.audioStats {
results.append("LM Delay: \(channelStats.lastmileDelay)ms")
results.append("ASend: \(audioStats.sentBitrate)kbps")
results.append("CPU: \(channelStats.cpuAppUsage)%/\(channelStats.cpuTotalUsage)%")
results.append("ASend Loss: \(audioStats.txPacketLossRate)%")
}
}
return results.joined(separator: "\n")
}
func remoteDescription(info: RemoteInfo, audioOnly: Bool) -> String {
var results:[String] = []
if(!audioOnly) {
if let volume = info.audioVolume {
results.append("Volume: \(volume)")
}
if let videoStats = info.videoStats, let audioStats = info.audioStats {
let audioQuality:AgoraNetworkQuality = AgoraNetworkQuality(rawValue: audioStats.quality) ?? .unknown
results.append("\(Int(videoStats.width))×\(Int(videoStats.height)),\(videoStats.decoderOutputFrameRate)fps")
results.append("VRecv: \(videoStats.receivedBitrate)kbps")
results.append("ARecv: \(audioStats.receivedBitrate)kbps")
results.append("VLoss: \(videoStats.packetLossRate)%")
results.append("ALoss: \(audioStats.audioLossRate)%")
results.append("AQuality: \(audioQuality.description())")
}
} else {
if let volume = info.audioVolume {
results.append("Volume: \(volume)")
}
if let audioStats = info.audioStats {
let audioQuality:AgoraNetworkQuality = AgoraNetworkQuality(rawValue: audioStats.quality) ?? .unknown
results.append("ARecv: \(audioStats.receivedBitrate)kbps")
results.append("ALoss: \(audioStats.audioLossRate)%")
results.append("AQuality: \(audioQuality.description())")
}
}
return results.joined(separator: "\n")
}
}