Skip to content

Commit 01bcf52

Browse files
committed
update ktvapi
1 parent 900a8ae commit 01bcf52

File tree

5 files changed

+389
-100
lines changed

5 files changed

+389
-100
lines changed

KTVAPI/iOS/Example/KTVApiDemo/KTVApiDemo.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
E39BAF772B6CC695002C692F /* LrcTime.proto in Sources */ = {isa = PBXBuildFile; fileRef = E39BAF752B6CC695002C692F /* LrcTime.proto */; };
1414
E39BAF792B6CCC65002C692F /* 不如跳舞.xml in Resources */ = {isa = PBXBuildFile; fileRef = E39BAF782B6CCC65002C692F /* 不如跳舞.xml */; };
1515
E39BAF7B2B6CCC74002C692F /* 不如跳舞.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = E39BAF7A2B6CCC74002C692F /* 不如跳舞.mp4 */; };
16+
E3EC073C2BD8BB8600CB8279 /* APIReporter.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3EC073B2BD8BB8600CB8279 /* APIReporter.swift */; };
1617
E3ED270B2A822E9D0087B7AA /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3ED270A2A822E9D0087B7AA /* AppDelegate.swift */; };
1718
E3ED270D2A822E9D0087B7AA /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3ED270C2A822E9D0087B7AA /* SceneDelegate.swift */; };
1819
E3ED270F2A822E9D0087B7AA /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3ED270E2A822E9D0087B7AA /* ViewController.swift */; };
@@ -46,6 +47,7 @@
4647
E39BAF752B6CC695002C692F /* LrcTime.proto */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.protobuf; path = LrcTime.proto; sourceTree = "<group>"; };
4748
E39BAF782B6CCC65002C692F /* 不如跳舞.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "不如跳舞.xml"; sourceTree = "<group>"; };
4849
E39BAF7A2B6CCC74002C692F /* 不如跳舞.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = "不如跳舞.mp4"; sourceTree = "<group>"; };
50+
E3EC073B2BD8BB8600CB8279 /* APIReporter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIReporter.swift; sourceTree = "<group>"; };
4951
E3ED27072A822E9D0087B7AA /* KTVApiDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = KTVApiDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
5052
E3ED270A2A822E9D0087B7AA /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
5153
E3ED270C2A822E9D0087B7AA /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
@@ -143,6 +145,7 @@
143145
E3ED271E2A822ED30087B7AA /* KTVAPI */ = {
144146
isa = PBXGroup;
145147
children = (
148+
E3EC073B2BD8BB8600CB8279 /* APIReporter.swift */,
146149
E3ED271F2A822ED30087B7AA /* KTVApiImpl.swift */,
147150
E3ED27202A822ED30087B7AA /* KTVApi.swift */,
148151
E38BDE772B6F7A77007A2834 /* KTVGiantChorusApiImpl.swift */,
@@ -287,6 +290,7 @@
287290
E3ED27392A8312120087B7AA /* AgoraStringExtention.swift in Sources */,
288291
E3ED273B2A8312120087B7AA /* AgoraDownLoadManager.swift in Sources */,
289292
E3ED27212A822ED30087B7AA /* KTVApiImpl.swift in Sources */,
293+
E3EC073C2BD8BB8600CB8279 /* APIReporter.swift in Sources */,
290294
E39BAF772B6CC695002C692F /* LrcTime.proto in Sources */,
291295
E3ED27222A822ED30087B7AA /* KTVApi.swift in Sources */,
292296
E3ED270F2A822E9D0087B7AA /* ViewController.swift in Sources */,
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
//
2+
// APIReporter.swift
3+
// CallAPI
4+
//
5+
// Created by wushengtao on 2024/4/8.
6+
//
7+
8+
import AgoraRtcKit
9+
10+
11+
/// 场景化类型
12+
public enum APIType: Int {
13+
case ktv = 1 //K歌
14+
case call = 2 //呼叫
15+
case beauty = 3 //美颜
16+
case videoLoader = 4 //秒开/秒切
17+
case pk = 5 //团战
18+
case vitualSpace = 6 //
19+
case screenSpace = 7 //屏幕共享
20+
case audioScenario = 8 //音频scenario
21+
}
22+
23+
enum APIEventType: Int {
24+
case api = 0 //api事件
25+
case cost //耗时事件
26+
case custom //自定义事件
27+
}
28+
29+
struct ApiEventKey {
30+
static let type = "type"
31+
static let desc = "desc"
32+
static let apiValue = "apiValue"
33+
static let ts = "ts"
34+
static let ext = "ext"
35+
}
36+
37+
struct APICostEvent {
38+
static let channelUsage = "channelUsage" //频道使用耗时
39+
static let firstFrameActual = "firstFrameActual" //首帧实际耗时
40+
static let firstFramePerceived = "firstFramePerceived" //首帧感官耗时
41+
}
42+
43+
let formatter = DateFormatter()
44+
func debugApiPrint(_ message: String) {
45+
#if DEBUG
46+
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
47+
let timeString = formatter.string(from: Date())
48+
print("\(timeString) \(message)")
49+
#endif
50+
}
51+
52+
@objcMembers
53+
public class APIReporter: NSObject {
54+
private var engine: AgoraRtcEngineKit
55+
private let messsageId: String = "agora:scenarioAPI"
56+
private var category: String
57+
private var durationEventStartMap: [String: Int64] = [:]
58+
59+
//MARK: public
60+
public init(type: APIType, version: String, engine: AgoraRtcEngineKit) {
61+
self.category = "\(type.rawValue)_iOS_\(version)"
62+
self.engine = engine
63+
super.init()
64+
65+
configParameters()
66+
}
67+
68+
public func reportFuncEvent(name: String, value: [String: Any], ext: [String: Any]) {
69+
let content = "[APIReporter]reportFuncEvent: \(name) value: \(value) ext: \(ext)"
70+
debugApiPrint(content)
71+
let eventMap: [String: Any] = [ApiEventKey.type: APIEventType.api.rawValue, ApiEventKey.desc: name]
72+
let labelMap: [String: Any] = [ApiEventKey.apiValue: value, ApiEventKey.ts: getCurrentTs(), ApiEventKey.ext: ext]
73+
let event = convertToJSONString(eventMap) ?? ""
74+
let label = convertToJSONString(labelMap) ?? ""
75+
engine.sendCustomReportMessage(messsageId,
76+
category: category,
77+
event: event,
78+
label: label,
79+
value: 0)
80+
}
81+
82+
public func startDurationEvent(name: String) {
83+
durationEventStartMap[name] = getCurrentTs()
84+
}
85+
86+
public func endDurationEvent(name: String, ext: [String: Any]) {
87+
guard let beginTs = durationEventStartMap[name] else {return}
88+
durationEventStartMap.removeValue(forKey: name)
89+
let ts = getCurrentTs()
90+
let cost = Int(ts - beginTs)
91+
92+
reportCostEvent(ts: ts, name: name, cost: cost, ext: ext)
93+
}
94+
95+
public func reportCostEvent(name: String, cost: Int, ext: [String: Any]) {
96+
durationEventStartMap.removeValue(forKey: name)
97+
reportCostEvent(ts: getCurrentTs(), name: name, cost: cost, ext: ext)
98+
}
99+
100+
public func reportCustomEvent(name: String, ext: [String: Any]) {
101+
let content = "[APIReporter]reportCustomEvent: \(name) ext: \(ext)"
102+
debugApiPrint(content)
103+
let eventMap: [String: Any] = [ApiEventKey.type: APIEventType.custom.rawValue, ApiEventKey.desc: name]
104+
let labelMap: [String: Any] = [ApiEventKey.ts: getCurrentTs(), ApiEventKey.ext: ext]
105+
let event = convertToJSONString(eventMap) ?? ""
106+
let label = convertToJSONString(labelMap) ?? ""
107+
engine.sendCustomReportMessage(messsageId,
108+
category: category,
109+
event: event,
110+
label: label,
111+
value: 0)
112+
}
113+
114+
public func writeLog(content: String, level: AgoraLogLevel) {
115+
engine.writeLog(level, content: content)
116+
}
117+
118+
public func cleanCache() {
119+
durationEventStartMap.removeAll()
120+
}
121+
122+
//MARK: private
123+
private func reportCostEvent(ts: Int64, name: String, cost: Int, ext: [String: Any]) {
124+
let content = "[APIReporter]reportCostEvent: \(name) cost: \(cost) ms ext: \(ext)"
125+
debugApiPrint(content)
126+
writeLog(content: content, level: .info)
127+
let eventMap: [String: Any] = [ApiEventKey.type: APIEventType.cost.rawValue, ApiEventKey.desc: name]
128+
let labelMap: [String: Any] = [ApiEventKey.ts: ts, ApiEventKey.ext: ext]
129+
let event = convertToJSONString(eventMap) ?? ""
130+
let label = convertToJSONString(labelMap) ?? ""
131+
engine.sendCustomReportMessage(messsageId,
132+
category: category,
133+
event: event,
134+
label: label,
135+
value: cost)
136+
}
137+
138+
private func configParameters() {
139+
// engine.setParameters("{\"rtc.qos_for_test_purpose\": true}")
140+
engine.setParameters("{\"rtc.direct_send_custom_event\": true}")
141+
engine.setParameters("{\"rtc.log_external_input\": true}")
142+
}
143+
144+
private func getCurrentTs() -> Int64 {
145+
return Int64(round(Date().timeIntervalSince1970 * 1000.0))
146+
}
147+
148+
private func convertToJSONString(_ dictionary: [String: Any]) -> String? {
149+
do {
150+
let jsonData = try JSONSerialization.data(withJSONObject: dictionary, options: [])
151+
if let jsonString = String(data: jsonData, encoding: .utf8) {
152+
return jsonString
153+
}
154+
} catch {
155+
writeLog(content: "[APIReporter]convert to json fail: \(error) dictionary: \(dictionary)", level: .warn)
156+
}
157+
return nil
158+
}
159+
}

KTVAPI/iOS/Example/KTVApiDemo/KTVApiDemo/KTVAPI/KTVApi.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -260,22 +260,22 @@ enum GiantChorusRouteSelectionType: Int {
260260
public var mainSingerUid: Int = 0 //主唱uid
261261
public var mode: KTVLoadMusicMode = .loadMusicAndLrc
262262
public var songCutter: Bool = false
263-
func printObjectContent() -> String {
264-
var content = ""
265-
266-
let mirror = Mirror(reflecting: self)
267-
for child in mirror.children {
268-
if let propertyName = child.label {
269-
if let propertyValue = child.value as? CustomStringConvertible {
270-
content += "\(propertyName): \(propertyValue)\n"
271-
} else {
272-
content += "\(propertyName): \(child.value)\n"
273-
}
274-
}
275-
}
276-
277-
return content
278-
}
263+
// func printObjectContent() -> String {
264+
// var content = ""
265+
//
266+
// let mirror = Mirror(reflecting: self)
267+
// for child in mirror.children {
268+
// if let propertyName = child.label {
269+
// if let propertyValue = child.value as? CustomStringConvertible {
270+
// content += "\(propertyName): \(propertyValue)\n"
271+
// } else {
272+
// content += "\(propertyName): \(child.value)\n"
273+
// }
274+
// }
275+
// }
276+
//
277+
// return content
278+
// }
279279
}
280280

281281

0 commit comments

Comments
 (0)