基于SRS服务器实现Android-Web端视频通话(3):Android端向SRS服务器推送WebRTC流

基于SRS服务器实现Android-Web端视频通话(1):SRS服务器启用HTTPS
基于SRS服务器实现Android-Web端视频通话(2):Android端从SRS服务器拉取WebRTC流
基于SRS服务器实现Android-Web端视频通话(3):Android端向SRS服务器推送WebRTC流

实现效果

在这里插入图片描述

引库

implementation 'org.webrtc:google-webrtc:1.0.32006'

其他版本,详见

推流流程

createPeerConnectionFactory -> createPeerConnection(addTransceiver) -> createOffer -> setLocalDescription(OFFER) -> get remote sdp(network requset) -> setRemoteDescription(ANSWER)

代码实现

初始化

//加载并初始化 WebRTC,在创建 PeerConnectionFactory 之前必须至少调用一次
PeerConnectionFactory.initialize(
    PeerConnectionFactory.InitializationOptions
        .builder(applicationContext).createInitializationOptions()
)

private val eglBaseContext = EglBase.create().eglBaseContext

createPeerConnectionFactory

private val peerConnectionFactory: PeerConnectionFactory = createPeerConnectionFactory()

private fun createPeerConnectionFactory(): PeerConnectionFactory {
   
	//先做默认配置,后面可能会遇到坑
    val options = PeerConnectionFactory.Options()
    val encoderFactory = DefaultVideoEncoderFactory(eglBaseContext, true, true)
    val decoderFactory = DefaultVideoDecoderFactory(eglBaseContext)
    return PeerConnectionFactory.builder()
        .setOptions(options)
        .setVideoEncoderFactory(encoderFactory)
        .setVideoDecoderFactory(decoderFactory)
        .createPeerConnectionFactory()
}

createPeerConnection(addTransceiver)

private fun initPeerConnection() {
   
    val createAudioSource = peerConnectionFactory.createAudioSource(createAudioConstraints())
    val audioTrack =
        peerConnectionFactory.createAudioTrack("local_audio_track", createAudioSource)

    cameraVideoCapturer = createVideoCapture(this)
    cameraVideoCapturer?.let {
    capture ->
        val videoSource = peerConnectionFactory.createVideoSource(capture.isScreencast)
        videoTrack =
            peerConnectionFactory.createVideoTrack("local_video_track", videoSource).apply {
   
            	//显示到本地画面上
                addSink(mBinding.svr)
            }
        surfaceTextureHelper =
            SurfaceTextureHelper.create("surface_texture_thread", eglBaseContext)
        capture.initialize(surfaceTextureHelper, this, videoSource.capturerObserver)
        //开始捕帧,宽、高、帧率。
        capture.startCapture(640, 480, 20)
    }

    val rtcConfig = PeerConnection.RTCConfiguration(emptyList())
    /*
 	<p>For users who wish to send multiple audio/video streams and need to stay interoperable with legacy WebRTC implementations, specify PLAN_B.
 	<p>For users who wish to send multiple audio/video streams and/or wish to use the new RtpTransceiver API, specify UNIFIED_PLAN.
 	*/
	//使用PeerConnection.SdpSemantics.UNIFIED_PLAN
    rtcConfig.sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN

    peerConnection = peerConnectionFactory.createPeerConnection(
        rtcConfig,
        PeerConnectionObserver
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冬季穿短裤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值