51 lines
1.8 KiB
Kotlin
51 lines
1.8 KiB
Kotlin
package calls.data.remote
|
|
|
|
import android.content.Context
|
|
import org.webrtc.*
|
|
import javax.inject.Inject
|
|
import javax.inject.Singleton
|
|
|
|
@Singleton
|
|
class WebRtcManager @Inject constructor(private val context: Context) {
|
|
private var peerConnection: PeerConnection? = null
|
|
private val factory: PeerConnectionFactory by lazy { createFactory() }
|
|
|
|
// Аудио и видео источники
|
|
private val videoSource by lazy { factory.createVideoSource(false) }
|
|
private val audioSource by lazy { factory.createAudioSource(MediaConstraints()) }
|
|
|
|
private fun createFactory(): PeerConnectionFactory {
|
|
PeerConnectionFactory.initialize(
|
|
PeerConnectionFactory.InitializationOptions.builder(context).createInitializationOptions()
|
|
)
|
|
return PeerConnectionFactory.builder()
|
|
.setVideoEncoderFactory(DefaultVideoEncoderFactory(EglBase.create().eglBaseContext, true, true))
|
|
.setVideoDecoderFactory(DefaultVideoDecoderFactory(EglBase.create().eglBaseContext))
|
|
.createPeerConnectionFactory()
|
|
}
|
|
|
|
fun initializePeerConnection(observer: PeerConnection.Observer) {
|
|
val iceServers = listOf(
|
|
PeerConnection.IceServer.builder("stun:stun.l.google.com:19302").createIceServer()
|
|
)
|
|
peerConnection = factory.createPeerConnection(iceServers, observer)
|
|
}
|
|
|
|
fun createOffer(observer: SdpObserver) {
|
|
peerConnection?.createOffer(observer, MediaConstraints())
|
|
}
|
|
|
|
fun setRemoteDescription(sdp: String, type: SessionDescription.Type, observer: SdpObserver) {
|
|
peerConnection?.setRemoteDescription(observer, SessionDescription(type, sdp))
|
|
}
|
|
|
|
fun addIceCandidate(candidate: IceCandidate) {
|
|
peerConnection?.addIceCandidate(candidate)
|
|
}
|
|
|
|
fun close() {
|
|
peerConnection?.dispose()
|
|
peerConnection = null
|
|
}
|
|
}
|