-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrecallTest.swift
153 lines (134 loc) · 5.65 KB
/
PrecallTest.swift
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
//
// JoinChannelVC.swift
// APIExample
//
// Created by 张乾泽 on 2020/4/17.
// Copyright © 2020 Agora Corp. All rights reserved.
//
import UIKit
import AGEVideoLayout
import AgoraRtcKit
class PrecallTestEntry : UIViewController
{
var agoraKit: AgoraRtcEngineKit!
var timer:Timer?
var echoTesting:Bool = false
@IBOutlet weak var lastmileBtn: UIButton!
@IBOutlet weak var echoTestBtn: UIButton!
@IBOutlet weak var lastmileResultLabel: UILabel!
@IBOutlet weak var lastmileProbResultLabel: UILabel!
@IBOutlet weak var lastmileActivityView: UIActivityIndicatorView!
@IBOutlet weak var echoTestCountDownLabel: UILabel!
@IBOutlet weak var echoTestPopover: UIView!
@IBOutlet weak var echoValidateCountDownLabel: UILabel!
@IBOutlet weak var echoValidatePopover: UIView!
@IBOutlet weak var preview: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// set up agora instance when view loadedlet config = AgoraRtcEngineConfig()
let config = AgoraRtcEngineConfig()
config.appId = KeyCenter.AppId
config.areaCode = GlobalSettings.shared.area.rawValue
// setup log file path
let logConfig = AgoraLogConfig()
logConfig.level = .info
config.logConfig = logConfig
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
// have to be a broadcaster for doing echo test
agoraKit.setChannelProfile(.liveBroadcasting)
agoraKit.setClientRole(.broadcaster)
}
@IBAction func doLastmileTest(sender: UIButton) {
lastmileActivityView.startAnimating()
let config = AgoraLastmileProbeConfig()
// do uplink testing
config.probeUplink = true;
// do downlink testing
config.probeDownlink = true;
// expected uplink bitrate, range: [100000, 5000000]
config.expectedUplinkBitrate = 100000;
// expected downlink bitrate, range: [100000, 5000000]
config.expectedDownlinkBitrate = 100000;
agoraKit.startLastmileProbeTest(config)
}
@IBAction func doEchoTest(sender: UIButton) {
agoraKit.startEchoTest(withInterval: 10, successBlock: nil)
showPopover(isValidate: false, seconds: 10) {[unowned self] in
self.showPopover(isValidate: true, seconds: 10) {[unowned self] in
self.agoraKit.stopEchoTest()
}
}
}
@IBAction func doEchoVideoTest(sender: UIButton) {
if(echoTesting){
agoraKit.stopEchoTest()
echoTestBtn.title = "Start Video/Audio Test".localized
echoTesting = false
}
else{
let config = AgoraEchoTestConfiguration()
echoTestBtn.title = "Stop Video/Audio Test".localized
config.channelId = "randomChannel"
config.view = self.preview
config.enableAudio = true
config.enableVideo = true
agoraKit.startEchoTest(withConfig: config)
echoTesting = true
}
}
// show popover and hide after seconds
func showPopover(isValidate:Bool, seconds:Int, callback:@escaping (() -> Void)) {
var count = seconds
var countDownLabel:UILabel?
var popover:UIView?
if(isValidate) {
countDownLabel = echoValidateCountDownLabel
popover = echoValidatePopover
} else {
countDownLabel = echoTestCountDownLabel
popover = echoTestPopover
}
countDownLabel?.text = "\(count)"
popover?.isHidden = false
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {[unowned self] (timer) in
count -= 1
countDownLabel?.text = "\(count)"
if(count == 0) {
self.timer?.invalidate()
popover?.isHidden = true
callback()
}
}
}
override func willMove(toParent parent: UIViewController?) {
if parent == nil {
// clean up
// important, you will not be able to join a channel
// if you are in the middle of a testing
timer?.invalidate()
agoraKit.stopEchoTest()
agoraKit.stopLastmileProbeTest()
}
}
}
extension PrecallTestEntry:AgoraRtcEngineDelegate
{
/// callback to get lastmile quality 2seconds after startLastmileProbeTest
func rtcEngine(_ engine: AgoraRtcEngineKit, lastmileQuality quality: AgoraNetworkQuality) {
lastmileResultLabel.text = "Quality: \(quality.description())"
}
/// callback to get more detail lastmile quality after startLastmileProbeTest
func rtcEngine(_ engine: AgoraRtcEngineKit, lastmileProbeTest result: AgoraLastmileProbeResult) {
let rtt = "Rtt: \(result.rtt)ms"
let downlinkBandwidth = "DownlinkAvailableBandwidth: \(result.downlinkReport.availableBandwidth)Kbps"
let downlinkJitter = "DownlinkJitter: \(result.downlinkReport.jitter)ms"
let downlinkLoss = "DownlinkLoss: \(result.downlinkReport.packetLossRate)%"
let uplinkBandwidth = "UplinkAvailableBandwidth: \(result.uplinkReport.availableBandwidth)Kbps"
let uplinkJitter = "UplinkJitter: \(result.uplinkReport.jitter)ms"
let uplinkLoss = "UplinkLoss: \(result.uplinkReport.packetLossRate)%"
lastmileProbResultLabel.text = [rtt, downlinkBandwidth, downlinkJitter, downlinkLoss, uplinkBandwidth, uplinkJitter, uplinkLoss].joined(separator: "\n")
// stop testing after get last mile detail result
engine.stopLastmileProbeTest()
lastmileActivityView.stopAnimating()
}
}