Android WebRTC无法在不同的网络上运行 - 无视频

问题描述 投票:0回答:1

我正在尝试通过webrtc将视频从Raspberry Pi流式传输到Android设备。我使用firebase(firestore)作为信令。我可以在连接到相同的wifi时运行设置,但是当使用不同的网络时它会失败。

设备 - RPI

客户端1)Web客户端(托管在firebase上)2)Android App

在设备和客户端之间的同一网络(wifi)上,两个客户端都能够播放视频和音频。

但是当设备和客户端位于不同的网络上时,Web客户端可以显示视频,但Android App无法显示视频。

信号正常工作,设备,摄像头和麦克风启动,冰候选人成功交换。我还在android上添加了远程流(onAddStream调用)。但是没有播放视频和音频。

Android PeerConnectionClient

    class PeerConnectionClient(private val activity: MainActivity, private val fSignalling: FSignalling) {

    internal var isVideoRunning = false

    private val rootEglBase by lazy {
        EglBase.create()
    }

    private val peerConnectionFactory: PeerConnectionFactory by lazy {
        val initializationOptions = PeerConnectionFactory.InitializationOptions.builder(activity).createInitializationOptions()
        PeerConnectionFactory.initialize(initializationOptions)

        val options = PeerConnectionFactory.Options()
        val defaultVideoEncoderFactory = DefaultVideoEncoderFactory(rootEglBase.eglBaseContext, true, true)
        val defaultVideoDecoderFactory = DefaultVideoDecoderFactory(rootEglBase.eglBaseContext)
        PeerConnectionFactory.builder()
                .setOptions(options)
                .setVideoEncoderFactory(defaultVideoEncoderFactory)
                .setVideoDecoderFactory(defaultVideoDecoderFactory)
                .createPeerConnectionFactory()
    }

    private val iceServersList = mutableListOf("stun:stun.l.google.com:19302")

    private var sdpConstraints: MediaConstraints? = null
    private var localAudioTrack: AudioTrack? = null

    private var localPeer: PeerConnection? = null

    private var gotUserMedia: Boolean = false
    private var peerIceServers: MutableList<PeerConnection.IceServer> = ArrayList()

    init {
        peerIceServers.add(PeerConnection.IceServer.builder(iceServersList).createIceServer())

        // activity.surface_view.release()
        activity.surface_view.init(rootEglBase.eglBaseContext, null)
        activity.surface_view.setZOrderMediaOverlay(true)

        createPeer()
    }

    private fun createPeer() {
        sdpConstraints = MediaConstraints()


        val audioconstraints = MediaConstraints()
        val audioSource = peerConnectionFactory.createAudioSource(audioconstraints)
        localAudioTrack = peerConnectionFactory.createAudioTrack("101", audioSource)
        gotUserMedia = true

        activity.runOnUiThread {
            if (localAudioTrack != null) {
                createPeerConnection()
                // doCall()
            }
        }

    }

    /**
     * Creating the local peerconnection instance
     */
    private fun createPeerConnection() {
        val constraints = MediaConstraints()
        constraints.mandatory.add(MediaConstraints.KeyValuePair("offerToReceiveAudio", "true"))
        constraints.mandatory.add(MediaConstraints.KeyValuePair("offerToReceiveVideo", "true"))
        constraints.optional.add(MediaConstraints.KeyValuePair("DtlsSrtpKeyAgreement", "true"))

        val rtcConfig = PeerConnection.RTCConfiguration(peerIceServers)
        // TCP candidates are only useful when connecting to a server that supports
        // ICE-TCP.
        rtcConfig.enableDtlsSrtp = true
        rtcConfig.enableRtpDataChannel = true
        // rtcConfig.tcpCandidatePolicy = PeerConnection.TcpCandidatePolicy.DISABLED
        // rtcConfig.bundlePolicy = PeerConnection.BundlePolicy.MAXBUNDLE
        // rtcConfig.rtcpMuxPolicy = PeerConnection.RtcpMuxPolicy.REQUIRE
        // rtcConfig.continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy.GATHER_CONTINUALLY
        // Use ECDSA encryption.
        // rtcConfig.keyType = PeerConnection.KeyType.ECDSA
        localPeer = peerConnectionFactory.createPeerConnection(rtcConfig, constraints, object : PeerObserver {
            override fun onIceCandidate(p0: IceCandidate) {
                super.onIceCandidate(p0)
                onIceCandidateReceived(p0)
            }

            override fun onAddStream(p0: MediaStream) {
                activity.showToast("Received Remote stream")
                super.onAddStream(p0)
                gotRemoteStream(p0)
            }

        })

        addStreamToLocalPeer()
    }

    /**
     * Adding the stream to the localpeer
     */
    private fun addStreamToLocalPeer() {
        //creating local mediastream
        val stream = peerConnectionFactory.createLocalMediaStream("102")
        stream.addTrack(localAudioTrack)
        localPeer!!.addStream(stream)
    }

    /**
     * This method is called when the app is initiator - We generate the offer and send it over through socket
     * to remote peer
     */
    /*private fun doCall() {
        localPeer!!.createOffer(object : mySdpObserver {
            override fun onCreateSuccess(p0: SessionDescription) {
                super.onCreateSuccess(p0)
                localPeer!!.setLocalDescription(object: mySdpObserver {}, p0)
                Log.d("onCreateSuccess", "SignallingClient emit ")
            }
        }, sdpConstraints)
    }*/

    private fun onIceCandidateReceived(iceCandidate: IceCandidate) {
        //we have received ice candidate. We can set it to the other peer.
        if (localPeer == null) {
            return
        }

        val message = JSONObject()
        message.put("type", "candidate")
        message.put("label", iceCandidate.sdpMLineIndex)
        message.put("id", iceCandidate.sdpMid)
        message.put("candidate", iceCandidate.serverUrl)

        fSignalling.doSignalingSend(message.toString())
    }

    private fun gotRemoteStream(stream: MediaStream) {
        isVideoRunning = true
        //we have remote video stream. add to the renderer.
        val videoTrack = stream.videoTracks[0]
        videoTrack.setEnabled(true)
        activity.runOnUiThread {
            try {
                // val remoteRenderer = VideoRenderer(surface_view)
                activity.surface_view.visibility = View.VISIBLE
                // videoTrack.addRenderer(remoteRenderer)
                videoTrack.addSink(activity.surface_view)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    fun onReceivePeerMessage(data: JSONObject) {
        if (data.getString("type") == "offer") {
            // val sdpReturned = SdpUtils.forceChosenVideoCodec(data.getString("sdp"), "H264")
            val sdpReturned = data.getString("sdp")
            // data.remove("sdp")
            // data.put("sdp", sdpReturned)

            val sessionDescription = SessionDescription(SessionDescription.Type.OFFER, sdpReturned)

            localPeer?.setRemoteDescription(object: mySdpObserver { }, sessionDescription)

            localPeer?.createAnswer(object : mySdpObserver {
                override fun onCreateSuccess(p0: SessionDescription) {
                    super.onCreateSuccess(p0)
                    localPeer!!.setLocalDescription( object : mySdpObserver {}, p0)

                    val description = JSONObject()
                    description.put("type", p0.type.canonicalForm())
                    description.put("sdp", p0.description)

                    [email protected](description.toString())
                }

                override fun onCreateFailure(p0: String) {
                    super.onCreateFailure(p0)
                    activity.showToast("Failed to create answer")
                }

            }, MediaConstraints())

        } else if (data.getString("type") == "candidate") {
            val iceCandidates = IceCandidate(data.getString("id"), data.getInt("label"), data.getString("candidate"))
            localPeer?.addIceCandidate(iceCandidates)
        }
    }

    internal fun close() {
        isVideoRunning = false
        localPeer?.close()
        localPeer = null
    }
 }

我的印象是,如果Web客户端能够在不同的网络(移动热点)上显示视频,那么Web客户端使用的同一个互联网上的Android客户端也应该能够显示视频。这是错的吗?为什么android不会显示视频(onAddStream被调用)

是否需要使用Turn服务器?我的假设再次是if web客户端工作,所以应该是android。我在RPI上使用的服务不支持转向服务器。

附加信息:设备是双重的ISP(我猜)(但由于网络客户端可以连接,我猜不会是一个问题)。

android video webrtc stun turn
1个回答
0
投票

我找到了解决问题的方法

我在用

private fun onIceCandidateReceived(iceCandidate: IceCandidate) {
    //we have received ice candidate. We can set it to the other peer.
    if (localPeer == null) {
        return
    }

    val message = JSONObject()
    message.put("type", "candidate")
    message.put("label", iceCandidate.sdpMLineIndex)
    message.put("id", iceCandidate.sdpMid)
    message.put("candidate", iceCandidate.serverUrl)

    fSignalling.doSignalingSend(message.toString())
}

而是被要求使用

message.put("candidate", iceCandidate.sdp) // iceCandidate.serverUrl)
© www.soinside.com 2019 - 2024. All rights reserved.