Skip to content

Commit 1c8444b

Browse files
committed
통화중 webrtc control 가능하도록 처리
1 parent c0568c5 commit 1c8444b

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

app/src/main/java/com/example/webrtc/WebRTCConnectActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class WebRTCConnectActivity : AppCompatActivity() {
6464
viewModel.connect()
6565
}
6666
}
67+
is WebRTCEvent.CloseSession -> finish()
6768
}
6869
}
6970
}

data/src/main/java/com/example/data/client/WebRTCClientImpl.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import com.google.firebase.firestore.FirebaseFirestore
99
import kotlinx.coroutines.CoroutineScope
1010
import kotlinx.coroutines.Dispatchers
1111
import kotlinx.coroutines.flow.MutableSharedFlow
12+
import kotlinx.coroutines.flow.MutableStateFlow
1213
import kotlinx.coroutines.flow.SharedFlow
1314
import kotlinx.coroutines.flow.asSharedFlow
15+
import kotlinx.coroutines.flow.collect
1416
import kotlinx.coroutines.launch
1517
import kotlinx.coroutines.runBlocking
1618
import org.webrtc.*
@@ -47,6 +49,26 @@ internal class WebRTCClientImpl @Inject constructor(
4749

4850
val eventFlow = _eventFlow
4951

52+
private val isMicEnabled = MutableStateFlow(true)
53+
private val isVideoEnabled = MutableStateFlow(true)
54+
55+
56+
init {
57+
collectState()
58+
}
59+
60+
private fun collectState() {
61+
CoroutineScope(Dispatchers.IO).launch {
62+
isMicEnabled.collect{
63+
localAudioTrack?.setEnabled(it)
64+
}
65+
}
66+
CoroutineScope(Dispatchers.IO).launch {
67+
isVideoEnabled.collect{
68+
localVideoTrack?.setEnabled(it)
69+
}
70+
}
71+
}
5072

5173
override fun getVideoCapture(context: Context) =
5274
Camera2Enumerator(context).run {
@@ -188,6 +210,19 @@ internal class WebRTCClientImpl @Inject constructor(
188210
}
189211

190212
override fun getEvent(): SharedFlow<PeerConnectionEvent> = eventFlow.asSharedFlow()
213+
override fun toggleVoice() {
214+
isMicEnabled.value = !isMicEnabled.value
215+
}
216+
217+
override fun toggleVideo() {
218+
isVideoEnabled.value = !isVideoEnabled.value
219+
}
220+
221+
override fun closeSession() {
222+
localAudioTrack?.dispose()
223+
localVideoTrack?.dispose()
224+
peerConnection?.close()
225+
}
191226

192227
override fun answer(roomID: String) =
193228
peerConnection?.answer(roomID)

domain/src/main/java/com/example/domain/client/WebRTCClient.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,10 @@ interface WebRTCClient {
5454

5555
fun getEvent(): SharedFlow<PeerConnectionEvent>
5656

57+
fun toggleVoice()
58+
59+
fun toggleVideo()
60+
61+
fun closeSession()
62+
5763
}

domain/src/main/java/com/example/domain/event/WebRTCEvent.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ import com.example.domain.client.WebRTCClient
55
sealed class WebRTCEvent {
66
data class Initialize(val webRTCClient: WebRTCClient) : WebRTCEvent()
77

8+
object CloseSession : WebRTCEvent()
9+
810
}

presentaion/src/main/java/com/example/presentaion/viewmodel/ConnectionViewModel.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,16 @@ class ConnectionViewModel @Inject constructor(
9292
if (!isJoin.value) webRTCClient.call(roomId.value)
9393
}
9494

95-
fun toggleVoice() {
96-
95+
fun toggleVoice() = viewModelScope.launch {
96+
webRTCClient.toggleVoice()
9797
}
9898

99-
fun toggleVideo() {
100-
99+
fun toggleVideo() = viewModelScope.launch {
100+
webRTCClient.toggleVideo()
101101
}
102102

103-
fun closeSession() {
104-
103+
fun closeSession() = viewModelScope.launch {
104+
webRTCClient.closeSession()
105+
_webRTCEvent.emit(WebRTCEvent.CloseSession)
105106
}
106107
}

0 commit comments

Comments
 (0)