Skip to content

Commit

Permalink
fix cross-region playback issues
Browse files Browse the repository at this point in the history
  • Loading branch information
AderanFeng committed Mar 19, 2024
1 parent 989efad commit 7699c8e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
package io.agora.flat.data

import io.agora.flat.di.NetworkModule
import io.agora.flat.http.api.CloudRecordService
import io.agora.flat.http.api.RoomService
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Inject
import javax.inject.Singleton


/**
* Fetch room service by uuid
*
* For joining rooms between different regions
*/
@Singleton
class RoomServiceFetcher @Inject constructor(
class ServiceFetcher @Inject constructor(
@NetworkModule.NormalOkHttpClient private val client: OkHttpClient,
private val appEnv: AppEnv
) {
companion object {
val regions = listOf(
private val regions = listOf(
"CN",
"SG",
)

val codeMap = mapOf(
private val codeMap = mapOf(
"1" to "CN",
"2" to "SG",
)
Expand All @@ -48,23 +50,31 @@ class RoomServiceFetcher @Inject constructor(
}
}

private val cache = mutableMapOf<String, RoomService>()
private val allCache = mutableMapOf<Pair<String, String>, Any>()

fun fetch(uuid: String): RoomService {
val env = fetchEnv(uuid, appEnv.getEnv())
val envServiceUrl = appEnv.getEnvServiceUrl(env)
fun fetchRoomService(uuid: String): RoomService {
return getApiService<RoomService>(uuid)
}

return cache[env] ?: createRoomService(envServiceUrl).also {
cache[env] = it
}
fun fetchCloudRecordService(uuid: String): CloudRecordService {
return getApiService<CloudRecordService>(uuid)
}

private inline fun <reified T> getApiService(uuid: String): T {
val env = fetchEnv(uuid, appEnv.getEnv())
val name = T::class.java.simpleName
return allCache.getOrPut(env to name) {
val serviceUrl = appEnv.getEnvServiceUrl(env)
createService<T>(serviceUrl)!!
} as T
}

private fun createRoomService(serviceUrl: String): RoomService {
private inline fun <reified T> createService(serviceUrl: String): T {
return Retrofit.Builder()
.baseUrl(serviceUrl)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(RoomService::class.java)
.create(T::class.java)
}
}
1 change: 1 addition & 0 deletions app/src/main/java/io/agora/flat/data/model/RecordInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ data class RecordInfo constructor(
val title: String,
val ownerUUID: String,
val roomType: RoomType,
val region: String,
val whiteboardRoomToken: String,
val whiteboardRoomUUID: String,
val rtmToken: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.agora.flat.data.repository

import io.agora.flat.data.Result
import io.agora.flat.data.ServiceFetcher
import io.agora.flat.data.model.*
import io.agora.flat.data.toResult
import io.agora.flat.http.api.CloudRecordService
Expand All @@ -12,6 +13,7 @@ import javax.inject.Singleton
@Singleton
class CloudRecordRepository @Inject constructor(
private val cloudRecordService: CloudRecordService,
private val serviceFetcher: ServiceFetcher,
) {
suspend fun acquireRecord(roomUUID: String, expiredHour: Int = 24): Result<RecordAcquireRespData> {
return withContext(Dispatchers.IO) {
Expand Down Expand Up @@ -100,7 +102,11 @@ class CloudRecordRepository @Inject constructor(

suspend fun getRecordInfo(roomUUID: String): Result<RecordInfo> {
return withContext(Dispatchers.IO) {
cloudRecordService.getRecordInfo(PureRoomReq(roomUUID)).toResult()
fetchService(roomUUID).getRecordInfo(PureRoomReq(roomUUID)).toResult()
}
}

private fun fetchService(roomUUID: String): CloudRecordService {
return serviceFetcher.fetchCloudRecordService(roomUUID)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package io.agora.flat.data.repository

import io.agora.flat.common.android.I18NFetcher
import io.agora.flat.data.AppEnv
import io.agora.flat.data.AppKVCenter
import io.agora.flat.data.Result
import io.agora.flat.data.RoomServiceFetcher
import io.agora.flat.data.ServiceFetcher
import io.agora.flat.data.manager.JoinRoomRecordManager
import io.agora.flat.data.model.CancelRoomReq
import io.agora.flat.data.model.JoinRoomRecord
Expand Down Expand Up @@ -32,13 +31,13 @@ import javax.inject.Singleton
@Singleton
class RoomRepository @Inject constructor(
private val roomService: RoomService,
private val roomServiceFetcher: RoomServiceFetcher,
private val serviceFetcher: ServiceFetcher,
private val joinRoomRecordManager: JoinRoomRecordManager,
private val appKVCenter: AppKVCenter,
private val i18NFetcher: I18NFetcher,
) {
private fun fetchService(uuid: String): RoomService {
return roomServiceFetcher.fetch(uuid)
return serviceFetcher.fetchRoomService(uuid)
}

suspend fun getRoomListAll(page: Int): Result<List<RoomInfo>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import com.herewhite.sdk.WhiteSdk
import com.herewhite.sdk.WhiteSdkConfiguration
import com.herewhite.sdk.domain.PlayerConfiguration
import com.herewhite.sdk.domain.Promise
import com.herewhite.sdk.domain.Region
import com.herewhite.sdk.domain.SDKError
import com.herewhite.sdk.domain.WindowParams
import com.herewhite.sdk.domain.WindowPrefersColorScheme.Dark
import com.herewhite.sdk.domain.WindowPrefersColorScheme.Light
import io.agora.board.fast.model.FastRegion
import io.agora.flat.BuildConfig
import io.agora.flat.R
import io.agora.flat.data.AppEnv
Expand Down Expand Up @@ -182,6 +184,7 @@ class ReplayPlayerComponent(
createWhitePlayer(
it.recordInfo.whiteboardRoomUUID,
it.recordInfo.whiteboardRoomToken,
it.recordInfo.region,
it.beginTime,
it.duration
)
Expand All @@ -208,7 +211,18 @@ class ReplayPlayerComponent(
}
}

private fun createWhitePlayer(roomUUID: String, roomToken: String, beginTime: Long, duration: Long) {
private fun String.toRegion(): Region {
val region = Region.values().find { it.name.lowercase().replace('_', '-') == this }
return region ?: Region.cn
}

private fun createWhitePlayer(
roomUUID: String,
roomToken: String,
region: String,
beginTime: Long,
duration: Long
) {
val conf = PlayerConfiguration(roomUUID, roomToken).apply {
val styleMap = hashMapOf(
"bottom" to "30px",
Expand All @@ -224,6 +238,7 @@ class ReplayPlayerComponent(
windowParams.scrollVerticalOnly = true
windowParams.prefersColorScheme = if (activity.isDarkMode()) Dark else Light
}
conf.region = region.toRegion()
conf.beginTimestamp = beginTime
conf.duration = duration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class RoomServiceFetcherTest {
)

cases.forEach { it ->
assertEquals(it.expected, RoomServiceFetcher.fetchEnv(it.uuid, it.env))
assertEquals(it.expected, ServiceFetcher.fetchEnv(it.uuid, it.env))
}
}
}

0 comments on commit 7699c8e

Please sign in to comment.