|
|
@@ -25,8 +25,6 @@ import com.adealink.weparty.room.sdk.listener.IDeviceListener
|
|
|
import com.adealink.weparty.room.sdk.listener.ISeatListener
|
|
|
import com.adealink.weparty.room.sdk.service.roomService
|
|
|
import io.trtc.tuikit.atomicxcore.api.CompletionHandler
|
|
|
-import io.trtc.tuikit.atomicxcore.api.device.DeviceStatus
|
|
|
-import io.trtc.tuikit.atomicxcore.api.device.DeviceStore
|
|
|
import io.trtc.tuikit.atomicxcore.api.live.DeviceControlPolicy
|
|
|
import io.trtc.tuikit.atomicxcore.api.live.LiveSeatStore
|
|
|
import io.trtc.tuikit.atomicxcore.api.live.SeatInfo
|
|
|
@@ -35,6 +33,8 @@ import kotlinx.coroutines.Job
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
|
import kotlinx.coroutines.flow.StateFlow
|
|
|
import kotlinx.coroutines.flow.asStateFlow
|
|
|
+import kotlinx.coroutines.flow.launchIn
|
|
|
+import kotlinx.coroutines.flow.onEach
|
|
|
import kotlinx.coroutines.flow.update
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
|
@@ -49,21 +49,19 @@ class SeatController(override val ctx: IRoomContext, serialHandler: Handler) :
|
|
|
|
|
|
private var liveSeatRoomId: String? = null
|
|
|
private var liveSeatStore: LiveSeatStore? = null
|
|
|
- private var liveSeatListJob: Job? = null
|
|
|
|
|
|
- private val _micSeatsFlow = MutableStateFlow<MutableMap<Int, SeatInfo>>(hashMapOf())
|
|
|
- override val micSeatsFlow: StateFlow<MutableMap<Int, SeatInfo>> = _micSeatsFlow.asStateFlow()
|
|
|
- private var uid2MicSeat: MutableMap<String, SeatInfo> = hashMapOf() //<用户UID, 麦位>
|
|
|
+ private val _micSeatsFlow = MutableStateFlow<Map<Int, SeatInfo>>(emptyMap())
|
|
|
+ override val micSeatsFlow: StateFlow<Map<Int, SeatInfo>> = _micSeatsFlow.asStateFlow()
|
|
|
|
|
|
- private val _speakingUsersFlow = MutableStateFlow<MutableMap<String, Boolean>>(hashMapOf())
|
|
|
- override val speakingUsersFlow: StateFlow<MutableMap<String, Boolean>> = _speakingUsersFlow.asStateFlow()
|
|
|
+ private val _speakingUsersFlow = MutableStateFlow<Map<String, Boolean>>(hashMapOf())
|
|
|
+ override val speakingUsersFlow: StateFlow<Map<String, Boolean>> = _speakingUsersFlow.asStateFlow()
|
|
|
|
|
|
override fun getMySeatInfo(): SeatInfo? {
|
|
|
- return uid2MicSeat[ctx.appSupplier.selfUid]
|
|
|
+ return _micSeatsFlow.value.values.find { it.userInfo.userID == ctx.appSupplier.selfUid }
|
|
|
}
|
|
|
|
|
|
- override fun getMicIndex(): Int? {
|
|
|
- return uid2MicSeat[ctx.appSupplier.selfUid]?.index
|
|
|
+ override fun getMyMicIndex(): Int? {
|
|
|
+ return _micSeatsFlow.value.values.find { it.userInfo.userID == ctx.appSupplier.selfUid }?.index
|
|
|
}
|
|
|
|
|
|
override fun onRoomIn(flowStateInfo: FlowStateInfo) {
|
|
|
@@ -78,8 +76,6 @@ class SeatController(override val ctx: IRoomContext, serialHandler: Handler) :
|
|
|
TAG_ROOM_SEAT,
|
|
|
"onRoomLeaved, ${flowStateInfo.roomId}, reason:${flowStateInfo.reason}"
|
|
|
)
|
|
|
- liveSeatListJob?.cancel()
|
|
|
- uid2MicSeat.clear()
|
|
|
_micSeatsFlow.update { hashMapOf() }
|
|
|
_selfMicSeatInfo.update { null }
|
|
|
_speakingUsersFlow.update { hashMapOf() }
|
|
|
@@ -89,27 +85,23 @@ class SeatController(override val ctx: IRoomContext, serialHandler: Handler) :
|
|
|
|
|
|
private fun observeSeatList() {
|
|
|
// 监听 seatList 变化,并更新您的麦位 UI
|
|
|
- liveSeatListJob = launch {
|
|
|
- launch {
|
|
|
- liveSeatStore?.liveSeatState?.seatList?.collect { seatInfoList ->
|
|
|
- val roomId = liveSeatRoomId ?: return@collect
|
|
|
- Log.d(
|
|
|
- TAG_ROOM_SEAT,
|
|
|
- "Seat list updated(roomId:$roomId): ${
|
|
|
- seatInfoList.joinToString(separator = ",") {
|
|
|
- "(index:${it.index},uid:${it.userInfo.userID})"
|
|
|
- }
|
|
|
- }"
|
|
|
- )
|
|
|
- handleMicSeatsInfo(
|
|
|
- roomId,
|
|
|
- seatInfoList
|
|
|
- )
|
|
|
- }
|
|
|
- }
|
|
|
- launch {
|
|
|
- liveSeatStore?.liveSeatState?.speakingUsers?.collect { speakingUsers ->
|
|
|
- val roomId = liveSeatRoomId ?: return@collect
|
|
|
+ liveSeatStore?.liveSeatState?.seatList?.onEach { seatInfoList ->
|
|
|
+ val roomId = liveSeatRoomId ?: return@onEach
|
|
|
+ Log.d(
|
|
|
+ TAG_ROOM_SEAT,
|
|
|
+ "Seat list updated(roomId:$roomId): ${
|
|
|
+ seatInfoList.joinToString(separator = ",") {
|
|
|
+ "(index:${it.index},uid:${it.userInfo.userID})"
|
|
|
+ }
|
|
|
+ }"
|
|
|
+ )
|
|
|
+ handleMicSeatsInfo(
|
|
|
+ roomId,
|
|
|
+ seatInfoList
|
|
|
+ )
|
|
|
+ }?.launchIn(this)
|
|
|
+ liveSeatStore?.liveSeatState?.speakingUsers?.onEach { speakingUsers ->
|
|
|
+ val roomId = liveSeatRoomId ?: return@onEach
|
|
|
// Log.d(
|
|
|
// TAG_ROOM_SEAT,
|
|
|
// "Speaking updated(roomId:$roomId): ${
|
|
|
@@ -118,10 +110,8 @@ class SeatController(override val ctx: IRoomContext, serialHandler: Handler) :
|
|
|
// }
|
|
|
// }"
|
|
|
// )
|
|
|
- handleMicSeatSpeaking(speakingUsers)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ handleMicSeatSpeaking(speakingUsers)
|
|
|
+ }?.launchIn(this)
|
|
|
}
|
|
|
|
|
|
override fun observeMicSeatsInfo(scope: CoroutineScope, action: (roomId: String, micSeatsInfo: List<SeatInfo>) -> Unit): Job {
|
|
|
@@ -201,36 +191,23 @@ class SeatController(override val ctx: IRoomContext, serialHandler: Handler) :
|
|
|
}
|
|
|
_selfMicSeatInfo.update { newSelfMicSeatInfo }
|
|
|
_micSeatsFlow.update { newMicSeats }
|
|
|
- uid2MicSeat = newUidMicSeats
|
|
|
if (newSelfMicSeatInfo != null) {
|
|
|
val remoteMute = !newSelfMicSeatInfo.userInfo.allowOpenMicrophone
|
|
|
- val autoMuteMic = roomService.joinController.joinedRoomInfo?.isAutoMuteMic() ?: false
|
|
|
- if (lastSelfMicSeatInfo == null || lastSelfMicSeatInfo.isSeatEmpty()) {
|
|
|
- Log.d(TAG_ROOM_SEAT, "SeatController, newSelfMicSeatInfo, remoteMute:$remoteMute, autoMuteMic:$autoMuteMic")
|
|
|
- //没在麦位
|
|
|
- if (remoteMute) {
|
|
|
- //房主or管理员关麦
|
|
|
- roomService.deviceController.closeLocalMicrophone()
|
|
|
- } else if (autoMuteMic) {
|
|
|
- //用户自己关麦
|
|
|
- roomService.deviceController.closeLocalMicrophone()
|
|
|
- } else {
|
|
|
- //麦位打开
|
|
|
- roomService.deviceController.openLocalMicrophone()
|
|
|
- }
|
|
|
+
|
|
|
+ // 提取出“是否应该被静音”的核心逻辑
|
|
|
+ val shouldMute = if (lastSelfMicSeatInfo == null || lastSelfMicSeatInfo.isSeatEmpty()) {
|
|
|
+ roomService.joinController.joinedRoomInfo?.isAutoMuteMic() ?: false
|
|
|
} else {
|
|
|
- val userMute = roomService.deviceController.isLocalUserMute()
|
|
|
- Log.d(TAG_ROOM_SEAT, "SeatController, newSelfMicSeatInfo, remoteMute:$remoteMute, autoMuteMic:$autoMuteMic, userMute:$userMute")
|
|
|
- if (remoteMute) {
|
|
|
- //房主or管理员关麦
|
|
|
- roomService.deviceController.closeLocalMicrophone()
|
|
|
- } else if (userMute) {
|
|
|
- //用户自己关麦
|
|
|
- roomService.deviceController.closeLocalMicrophone()
|
|
|
- } else {
|
|
|
- //麦位打开
|
|
|
- roomService.deviceController.openLocalMicrophone()
|
|
|
- }
|
|
|
+ roomService.deviceController.isLocalUserMute()
|
|
|
+ }
|
|
|
+
|
|
|
+ Log.d(TAG_ROOM_SEAT, "SeatController, remoteMute:$remoteMute, shouldMute:$shouldMute")
|
|
|
+
|
|
|
+ // 逻辑合并,极为清晰
|
|
|
+ if (remoteMute || shouldMute) {
|
|
|
+ roomService.deviceController.closeLocalMicrophone()
|
|
|
+ } else {
|
|
|
+ roomService.deviceController.openLocalMicrophone()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -251,11 +228,11 @@ class SeatController(override val ctx: IRoomContext, serialHandler: Handler) :
|
|
|
}
|
|
|
|
|
|
override fun isOnMic(uid: String): Boolean {
|
|
|
- return uid2MicSeat.contains(uid)
|
|
|
+ return _micSeatsFlow.value.values.any { it.userInfo.userID == uid }
|
|
|
}
|
|
|
|
|
|
override fun getMicSeatIndex(uid: String): Int? {
|
|
|
- return uid2MicSeat[uid]?.index
|
|
|
+ return _micSeatsFlow.value.values.find { it.userInfo.userID == uid }?.index
|
|
|
}
|
|
|
|
|
|
override fun micOn(micInex: Int, onSuccess: (() -> Unit)?, onFail: ((error: IError) -> Unit)?) {
|
|
|
@@ -350,20 +327,20 @@ class SeatController(override val ctx: IRoomContext, serialHandler: Handler) :
|
|
|
}
|
|
|
|
|
|
override suspend fun getMicSeatsInfo() {
|
|
|
- val roomId = liveSeatRoomId ?: return
|
|
|
- val seatInfoList = liveSeatStore?.liveSeatState?.seatList?.value ?: emptyList()
|
|
|
- Log.d(
|
|
|
- TAG_ROOM_SEAT,
|
|
|
- "Seat list updated(roomId:$roomId): ${
|
|
|
- seatInfoList.joinToString(separator = ",") {
|
|
|
- "(index:${it.index},uid:${it.userInfo.userID})"
|
|
|
- }
|
|
|
- }"
|
|
|
- )
|
|
|
- handleMicSeatsInfo(
|
|
|
- roomId,
|
|
|
- seatInfoList
|
|
|
- )
|
|
|
+// val roomId = liveSeatRoomId ?: return
|
|
|
+// val seatInfoList = liveSeatStore?.liveSeatState?.seatList?.value ?: emptyList()
|
|
|
+// Log.d(
|
|
|
+// TAG_ROOM_SEAT,
|
|
|
+// "Seat list updated(roomId:$roomId): ${
|
|
|
+// seatInfoList.joinToString(separator = ",") {
|
|
|
+// "(index:${it.index},uid:${it.userInfo.userID})"
|
|
|
+// }
|
|
|
+// }"
|
|
|
+// )
|
|
|
+// handleMicSeatsInfo(
|
|
|
+// roomId,
|
|
|
+// seatInfoList
|
|
|
+// )
|
|
|
}
|
|
|
|
|
|
/**
|