Search code examples
androidwebrtc

cannot resolve symbol 'RTCPeerConnection()'


I try to solve this issue Why the function "onaddstream" is never called?

But it's look like that:

'rtcpeerconnection()' doesn't exist in the last version of webrtc : "org.webrtc:google-webrtc:1.0.28513"

My onAddStream has a callback in old version. But not anymore

Please help me

Edit after answer of @philip

I can't override peerObserver, android studio say:

Expression peerObserver of type 'peerConnection.Observer' cannot be invoked as a function. The function 'invoke()' is not found.

 override fun runnerConnect(peerObserver: PeerConnection.Observer) {
        Log.d("WebRTCClientCaster", "runnerConnect sdp ")
        //   mCurrentPeerConnection = mPeerConnectionFactory!!.createPeerConnection(IceServer.instance.listServers(), peerObserver)
        val rtcConfig: PeerConnection.RTCConfiguration = PeerConnection.RTCConfiguration(IceServer.instance.listServers())
        rtcConfig.sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN
        mCurrentPeerConnection = mPeerConnectionFactory!!.createPeerConnection(rtcConfig, peerObserver)
    }

    override fun runnerConnect(sdpObserver: SdpObserver) {
        Log.d("WebRTCClientCaster", "runnerConnect peer")
       // mCurrentPeerConnection!!.addStream(mLocalMS)

        val mVideoTrack =
            mPeerConnectionFactory?.createVideoTrack(WebRTCConstant.VIDEO_ID, mVideoSource)
        mCurrentPeerConnection?.addTransceiver(mVideoTrack)
        val mAudioTrack =
            mPeerConnectionFactory?.createAudioTrack(WebRTCConstant.AUDIO_ID, mAudioSource)
        mCurrentPeerConnection?.addTransceiver(mAudioTrack)
        mCurrentPeerConnection!!.createOffer(sdpObserver, sdpConstraints)
    }

The part where I create factory and source :

private fun setConfigurationOfCamera() {

        Log.d("webRTCClientCaster", "setConfigurationOfCamera")
        val options = PeerConnectionFactory.Options()
        val defaultVideoEncoderFactory = DefaultVideoEncoderFactory(
            this.mEglBase,
            true,
            true
        )
        val defaultVideoDecoderFactory = DefaultVideoDecoderFactory(this.mEglBase)
        mPeerConnectionFactory = PeerConnectionFactory.builder()
            .setOptions(options)
            .setVideoEncoderFactory(defaultVideoEncoderFactory)
            .setVideoDecoderFactory(defaultVideoDecoderFactory)
            .createPeerConnectionFactory()

        mLocalMS = mPeerConnectionFactory!!.createLocalMediaStream(WebRTCConstant.STREAM_LABEL)

        if (mPeerConnectionParameters.isVideoCallEnabled) {

            val surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", this.mEglBase)
            videoCapturerAndroid = Capturer.getInstance().video
            mVideoSource = mPeerConnectionFactory?.createVideoSource(videoCapturerAndroid!!.isScreencast)

            videoCapturerAndroid?.initialize(surfaceTextureHelper, MyApp.getContext(), mVideoSource?.capturerObserver)
            Capturer.getInstance().startCapture(
                mPeerConnectionParameters.videoWidth,
                mPeerConnectionParameters.videoHeight,
                mPeerConnectionParameters.videoFps
            )
            // Add track to video capture with device
            mLocalMS.addTrack(createVideoTrack(WebRTCConstant.VIDEO_ID, mVideoSource!!))
        }

        mAudioSource = mPeerConnectionFactory!!.createAudioSource(audioConstraints)
        mLocalMS.addTrack(createAudioTrack(mAudioSource!!))

        listener.onConfigurationReady()
    }

The comment it what I replace, and with this code I Have a sigabrt who say :

# Fatal error in: ../../../../usr/local/google/home/sakal/code/webrtc-aar-release/src/pc/peer_connection.cc, line 1240
    # last system error: 0
    # Check failed: !IsUnifiedPlan()
    # AddStream is not available with Unified Plan SdpSemantics. Please use AddTrack instead.
2019-09-23 12:53:07.223 4804-6057/com.Dazzl.debug A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 6057 (signaling_threa)

Solution

  • Try doing this. Note that onTrack is implemented in the PeerConnectionObserver (pcObserver), not the the peerConnection itself:

        PeerConnection.RTCConfiguration rtcConfig =
                new PeerConnection.RTCConfiguration(mIceServers);
    rtcConfig.sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN;
    //Unified Plan is the new standard for SDP semantics and is needed to use transceivers
    
    peerConnection = factory.createPeerConnection(rtcConfig, new PcObserver("localPeerCreation")
            {
                @Override
                public void onTrack(RtpTransceiver transceiver) {
                    showToast(getString(R.string.onReceivedTrackToastMessage));
                    super.onTrack(transceiver);
                    gotRemoteTrack(transceiver);
                }
            });
    
    //Somewhere else in your code, where you create the local video track...
    localVideoTrack = factory.createVideoTrack(VIDEO_TRACK_ID, videoSource);
    
    peerConnection.addTransceiver(localVideoTrack);
    peerConnection.addTransceiver(localAudioTrack);
    

    //In a separate class create a PeerConnectionObserver that inherits from PeerConnection.Observer...

      public class PcObserver implements PeerConnection.Observer {
        private String logTag;
    
        CustomPeerConnectionObserver(String logTag) {
            this.logTag = logTag
        }
    
        @Override
        public void onSignalingChange(PeerConnection.SignalingState signalingState) {
            Log.d(logTag, "onSignalingChange() called with: signalingState = [" + signalingState + "]");
        }
     //....
    }