Skip to content

Commit 829c2f5

Browse files
authored
Merge branch 'release/4.3.0' into dev/4.3.0_cp
2 parents 01bcf52 + fad81a1 commit 829c2f5

File tree

14 files changed

+277
-119
lines changed

14 files changed

+277
-119
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/app/src/main/java/io/agora/ktvdemo/ui/MainFragment.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.core.content.res.ResourcesCompat
88
import androidx.core.widget.doAfterTextChanged
99
import androidx.navigation.fragment.findNavController
1010
import io.agora.ktvapi.KTVSingRole
11+
import io.agora.ktvdemo.BuildConfig
1112
import io.agora.ktvdemo.rtc.IChannelEventListener
1213
import io.agora.ktvdemo.R
1314
import io.agora.ktvdemo.rtc.RtcEngineController
@@ -60,6 +61,14 @@ class MainFragment : BaseFragment<FragmentMainBinding>() {
6061

6162
// 开始体验按钮
6263
btnStartChorus.setOnClickListener {
64+
if (BuildConfig.AGORA_APP_ID.isEmpty()) {
65+
toast(getString(R.string.app_appid_check))
66+
return@setOnClickListener
67+
}
68+
if (!KeyCenter.isNormalChorus && BuildConfig.RESTFUL_API_KEY.isEmpty()) {
69+
toast(getString(R.string.app_restful_check))
70+
return@setOnClickListener
71+
}
6372
if (KeyCenter.channelId.isEmpty()){
6473
toast(getString(R.string.app_input_channel_name))
6574
return@setOnClickListener

KTVAPI/Android/app/src/main/res/values-zh/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@
2222
<string name="app_normal_ktvapi_tag">独唱、小合唱</string>
2323
<string name="app_giant_ktvapi_tag">大合唱</string>
2424
<string name="app_start_enjoy_tag">开始体验</string>
25+
<string name="app_appid_check">请检查 gradle.properties 文件内 APPID APP 证书是否正确填写</string>
26+
<string name="app_restful_check">请检查 gradle.properties 文件内 RESTFUL API KEY 是否正确填写</string>
2527
</resources>

KTVAPI/Android/app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@
2121
<string name="app_normal_ktvapi_tag">独唱、小合唱</string>
2222
<string name="app_giant_ktvapi_tag">大合唱</string>
2323
<string name="app_start_enjoy_tag">开始体验</string>
24+
<string name="app_appid_check">请检查 gradle.properties 文件内 APPID APP 证书是否正确填写</string>
25+
<string name="app_restful_check">请检查 gradle.properties 文件内 RESTFUL API KEY 是否正确填写</string>
2426
</resources>

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 (Giant Chorus Only)
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: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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 APIType(val value: Int) {
10+
KTV(1), // K歌
11+
CALL(2), // 呼叫连麦
12+
BEAUTY(3), // 美颜
13+
VIDEO_LOADER(4), // 秒开秒切
14+
PK(5), // 团战
15+
VIRTUAL_SPACE(6), //
16+
SCREEN_SPACE(7), // 屏幕共享
17+
AUDIO_SCENARIO(8) // 音频
18+
}
19+
20+
enum class ApiEventType(val value: Int) {
21+
API(0),
22+
COST(1),
23+
CUSTOM(2)
24+
}
25+
26+
object ApiEventKey {
27+
const val TYPE = "type"
28+
const val DESC = "desc"
29+
const val API_VALUE = "apiValue"
30+
const val TIMESTAMP = "ts"
31+
const val EXT = "ext"
32+
}
33+
34+
object ApiCostEvent {
35+
const val CHANNEL_USAGE = "channelUsage" //频道使用耗时
36+
const val FIRST_FRAME_ACTUAL = "firstFrameActual" //首帧实际耗时
37+
const val FIRST_FRAME_PERCEIVED = "firstFramePerceived" //首帧感官耗时
38+
}
39+
40+
class APIReporter(
41+
private val type: APIType,
42+
private val version: String,
43+
private val rtcEngine: RtcEngine
44+
) {
45+
private val tag = "APIReporter"
46+
private val messageId = "agora:scenarioAPI"
47+
private val durationEventStartMap = HashMap<String, Long>()
48+
private val category = "${type.value}_Android_$version"
49+
50+
init {
51+
configParameters()
52+
}
53+
54+
// 上报普通场景化API
55+
fun reportFuncEvent(name: String, value: Map<String, Any>, ext: Map<String, Any>) {
56+
Log.d(tag, "reportFuncEvent: $name value: $value ext: $ext")
57+
val eventMap = mapOf(ApiEventKey.TYPE to ApiEventType.API.value, ApiEventKey.DESC to name)
58+
val labelMap = mapOf(ApiEventKey.API_VALUE to value, ApiEventKey.TIMESTAMP to getCurrentTs(), ApiEventKey.EXT to ext)
59+
val event = convertToJSONString(eventMap) ?: ""
60+
val label = convertToJSONString(labelMap) ?: ""
61+
rtcEngine.sendCustomReportMessage(messageId, category, event, label, 0)
62+
}
63+
64+
fun startDurationEvent(name: String) {
65+
Log.d(tag, "startDurationEvent: $name")
66+
durationEventStartMap[name] = getCurrentTs()
67+
}
68+
69+
fun endDurationEvent(name: String, ext: Map<String, Any>) {
70+
Log.d(tag, "endDurationEvent: $name")
71+
val beginTs = durationEventStartMap[name] ?: return
72+
durationEventStartMap.remove(name)
73+
val ts = getCurrentTs()
74+
val cost = (ts - beginTs).toInt()
75+
76+
innerReportCostEvent(ts, name, cost, ext)
77+
}
78+
79+
// 上报耗时打点信息
80+
fun reportCostEvent(name: String, cost: Int, ext: Map<String, Any>) {
81+
durationEventStartMap.remove(name)
82+
innerReportCostEvent(
83+
ts = getCurrentTs(),
84+
name = name,
85+
cost = cost,
86+
ext = ext
87+
)
88+
}
89+
90+
// 上报自定义信息
91+
fun reportCustomEvent(name: String, ext: Map<String, Any>) {
92+
Log.d(tag, "reportCustomEvent: $name ext: $ext")
93+
val eventMap = mapOf(ApiEventKey.TYPE to ApiEventType.CUSTOM.value, ApiEventKey.DESC to name)
94+
val labelMap = mapOf(ApiEventKey.TIMESTAMP to getCurrentTs(), ApiEventKey.EXT to ext)
95+
val event = convertToJSONString(eventMap) ?: ""
96+
val label = convertToJSONString(labelMap) ?: ""
97+
rtcEngine.sendCustomReportMessage(messageId, category, event, label, 0)
98+
}
99+
100+
fun writeLog(content: String, level: Int) {
101+
rtcEngine.writeLog(level, content)
102+
}
103+
104+
fun cleanCache() {
105+
durationEventStartMap.clear()
106+
}
107+
108+
// ---------------------- private ----------------------
109+
110+
private fun configParameters() {
111+
//rtcEngine.setParameters("{\"rtc.qos_for_test_purpose\": true}") //测试环境使用
112+
// 数据上报
113+
rtcEngine.setParameters("{\"rtc.direct_send_custom_event\": true}")
114+
// 日志写入
115+
rtcEngine.setParameters("{\"rtc.log_external_input\": true}")
116+
}
117+
118+
private fun getCurrentTs(): Long {
119+
return System.currentTimeMillis()
120+
}
121+
122+
private fun innerReportCostEvent(ts: Long, name: String, cost: Int, ext: Map<String, Any>) {
123+
Log.d(tag, "reportCostEvent: $name cost: $cost ms ext: $ext")
124+
writeLog("reportCostEvent: $name cost: $cost ms", Constants.LOG_LEVEL_INFO)
125+
val eventMap = mapOf(ApiEventKey.TYPE to ApiEventType.COST.value, ApiEventKey.DESC to name)
126+
val labelMap = mapOf(ApiEventKey.TIMESTAMP to ts, ApiEventKey.EXT to ext)
127+
val event = convertToJSONString(eventMap) ?: ""
128+
val label = convertToJSONString(labelMap) ?: ""
129+
rtcEngine.sendCustomReportMessage(messageId, category, event, label, cost)
130+
}
131+
132+
private fun convertToJSONString(dictionary: Map<String, Any>): String? {
133+
return try {
134+
JSONObject(dictionary).toString()
135+
} catch (e: Exception) {
136+
writeLog("[$tag]convert to json fail: $e dictionary: $dictionary", Constants.LOG_LEVEL_WARNING)
137+
null
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)