Skip to content

Commit 2c6e18f

Browse files
CYGCYG
authored andcommitted
[Android]add report api
1 parent 74ee72d commit 2c6e18f

File tree

8 files changed

+231
-104
lines changed

8 files changed

+231
-104
lines changed

KTVAPI/Android/app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ android {
2323
buildConfigField "String", "TOOLBOX_SERVER_HOST", "\"${properties.getProperty("TOOLBOX_SERVER_HOST", "")}\""
2424
buildConfigField "String", "AGORA_APP_ID", "\"${properties.getProperty("AGORA_APP_ID", "")}\""
2525
buildConfigField "String", "AGORA_APP_CERTIFICATE", "\"${properties.getProperty("AGORA_APP_CERTIFICATE", "")}\""
26+
buildConfigField "String", "RESTFUL_API_KEY", "\"${RESTFUL_API_KEY}\""
27+
buildConfigField "String", "RESTFUL_API_SECRET", "\"${RESTFUL_API_SECRET}\""
2628
}
2729

2830
buildTypes {

KTVAPI/Android/app/src/main/java/io/agora/ktvdemo/api/CloudApiManager.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class CloudApiManager private constructor() {
2525
return InstanceHolder.apiManager
2626
}
2727

28-
private const val testIp = "218.205.37.50"
29-
private const val domain = "http://218.205.37.50:16000"
28+
//private const val testIp = "218.205.37.50"
29+
private const val domain = "https://api.sd-rtn.com"
3030
private const val TAG = "ApiManager"
3131
private const val cloudRtcUid = 20232023
3232
}
@@ -54,7 +54,7 @@ class CloudApiManager private constructor() {
5454
try {
5555
val acquireOjb = JSONObject()
5656
acquireOjb.put("instanceId", System.currentTimeMillis().toString() + "")
57-
acquireOjb.put("testIp", testIp)
57+
//acquireOjb.put("testIp", testIp)
5858
val request: Request = Builder()
5959
.url(getTokenUrl(domain, BuildConfig.AGORA_APP_ID))
6060
.addHeader("Content-Type", "application/json")
@@ -188,7 +188,7 @@ class CloudApiManager private constructor() {
188188
private val basicAuth: String
189189
private get() {
190190
// 拼接客户 ID 和客户密钥并使用 base64 编码
191-
val plainCredentials = BuildConfig.AGORA_APP_ID + ":" + BuildConfig.AGORA_APP_CERTIFICATE
191+
val plainCredentials = BuildConfig.RESTFUL_API_KEY + ":" + BuildConfig.RESTFUL_API_SECRET
192192
var base64Credentials: String? = null
193193
base64Credentials = String(Base64.getEncoder().encode(plainCredentials.toByteArray()))
194194
// 创建 authorization header

KTVAPI/Android/app/src/main/java/io/agora/ktvdemo/rtc/RtcEngineController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ object RtcEngineController {
2525
if (innerRtcEngine == null) {
2626
val config = RtcEngineConfig()
2727
config.mContext = MyApplication.app()
28-
config.mAppId = BuildConfig.AGORA_APP_ID
28+
config.mAppId = "*****************************"
2929
config.mEventHandler = object : IRtcEngineEventHandler() {
3030
override fun onError(err: Int) {
3131
super.onError(err)

KTVAPI/Android/gradle.properties

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ TOOLBOX_SERVER_HOST=https://service.agora.io/toolbox
2929
#----------- Config Agora Keys -----------
3030
# RTM RTC SDK key Config
3131
AGORA_APP_ID=
32-
AGORA_APP_CERTIFICATE=
32+
AGORA_APP_CERTIFICATE=
33+
34+
# Restful Api Config
35+
RESTFUL_API_KEY=
36+
RESTFUL_API_SECRET=

KTVAPI/Android/lib_ktvapi/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ dependencies {
7676
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
7777
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
7878

79-
api "io.agora.rtc:agora-special-full:4.1.1.27"
79+
api "io.agora.rtc:agora-special-full:4.1.1.29"
8080
implementation 'com.google.protobuf:protobuf-java:3.19.3'
8181
implementation 'com.google.protobuf:protobuf-java-util:3.19.3'
8282
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package io.agora.ktvapi
2+
3+
import android.util.Log
4+
import io.agora.rtc2.Constants
5+
import io.agora.rtc2.RtcEngine
6+
import org.json.JSONObject
7+
import java.util.HashMap
8+
9+
enum class ApiEventType(val value: Int) {
10+
API(0),
11+
COST(1),
12+
CUSTOM(2)
13+
}
14+
15+
object ApiEventKey {
16+
const val TYPE = "type"
17+
const val DESC = "desc"
18+
const val API_VALUE = "apiValue"
19+
const val TIMESTAMP = "ts"
20+
const val EXT = "ext"
21+
}
22+
23+
object ApiCostEvent {
24+
const val CHANNEL_USAGE = "channelUsage" //频道使用耗时
25+
const val FIRST_FRAME_ACTUAL = "firstFrameActual" //首帧实际耗时
26+
const val FIRST_FRAME_PERCEIVED = "firstFramePerceived" //首帧感官耗时
27+
}
28+
29+
class APIReporter(
30+
private val category: String,
31+
private val rtcEngine: RtcEngine
32+
) {
33+
private val tag = "APIReporter"
34+
private val messageId = "agora:scenarioAPI"
35+
private val durationEventStartMap = HashMap<String, Long>()
36+
37+
init {
38+
configParameters()
39+
}
40+
41+
// 上报普通场景化API
42+
fun reportFuncEvent(name: String, value: Map<String, Any>, ext: Map<String, Any>) {
43+
Log.d(tag, "reportFuncEvent: $name value: $value ext: $ext")
44+
val eventMap = mapOf(ApiEventKey.TYPE to ApiEventType.API.value, ApiEventKey.DESC to name)
45+
val labelMap = mapOf(ApiEventKey.API_VALUE to value, ApiEventKey.TIMESTAMP to getCurrentTs(), ApiEventKey.EXT to ext)
46+
val event = convertToJSONString(eventMap) ?: ""
47+
val label = convertToJSONString(labelMap) ?: ""
48+
rtcEngine.sendCustomReportMessage(messageId, category, event, label, 0)
49+
}
50+
51+
fun startDurationEvent(name: String) {
52+
Log.d(tag, "startDurationEvent: $name")
53+
durationEventStartMap[name] = getCurrentTs()
54+
}
55+
56+
fun endDurationEvent(name: String) {
57+
Log.d(tag, "endDurationEvent: $name")
58+
val beginTs = durationEventStartMap[name] ?: return
59+
durationEventStartMap.remove(name)
60+
val ts = getCurrentTs()
61+
val cost = (ts - beginTs).toInt()
62+
63+
innerReportCostEvent(ts, name, cost)
64+
}
65+
66+
// 上报耗时打点信息
67+
fun reportCostEvent(name: String, cost: Int) {
68+
durationEventStartMap.remove(name)
69+
innerReportCostEvent(
70+
ts = getCurrentTs(),
71+
name = name,
72+
cost = cost
73+
)
74+
}
75+
76+
// 上报自定义信息
77+
fun reportCustomEvent(name: String, ext: Map<String, Any>) {
78+
Log.d(tag, "reportCustomEvent: $name ext: $ext")
79+
val eventMap = mapOf(ApiEventKey.TYPE to ApiEventType.CUSTOM.value, ApiEventKey.DESC to name)
80+
val labelMap = mapOf(ApiEventKey.TIMESTAMP to getCurrentTs(), ApiEventKey.EXT to ext)
81+
val event = convertToJSONString(eventMap) ?: ""
82+
val label = convertToJSONString(labelMap) ?: ""
83+
rtcEngine.sendCustomReportMessage(messageId, category, event, label, 0)
84+
}
85+
86+
fun writeLog(content: String, level: Int) {
87+
rtcEngine.writeLog(level, content)
88+
}
89+
90+
fun cleanCache() {
91+
durationEventStartMap.clear()
92+
}
93+
94+
// ---------------------- private ----------------------
95+
96+
private fun configParameters() {
97+
//rtcEngine.setParameters("{\"rtc.qos_for_test_purpose\": true}") //测试环境使用
98+
// 数据上报
99+
rtcEngine.setParameters("{\"rtc.direct_send_custom_event\": true}")
100+
// 日志写入
101+
rtcEngine.setParameters("{\"rtc.log_external_input\": true}")
102+
}
103+
104+
private fun getCurrentTs(): Long {
105+
return System.currentTimeMillis()
106+
}
107+
108+
private fun innerReportCostEvent(ts: Long, name: String, cost: Int) {
109+
Log.d(tag, "reportCostEvent: $name cost: $cost ms")
110+
writeLog("reportCostEvent: $name cost: $cost ms", Constants.LOG_LEVEL_INFO)
111+
val eventMap = mapOf(ApiEventKey.TYPE to ApiEventType.COST.value, ApiEventKey.DESC to name)
112+
val labelMap = mapOf(ApiEventKey.TIMESTAMP to ts)
113+
val event = convertToJSONString(eventMap) ?: ""
114+
val label = convertToJSONString(labelMap) ?: ""
115+
rtcEngine.sendCustomReportMessage(messageId, category, event, label, cost)
116+
}
117+
118+
private fun convertToJSONString(dictionary: Map<String, Any>): String? {
119+
return try {
120+
JSONObject(dictionary).toString()
121+
} catch (e: Exception) {
122+
writeLog("[$tag]convert to json fail: $e dictionary: $dictionary", Constants.LOG_LEVEL_WARNING)
123+
null
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)