Kamal
Kamal

Reputation: 45

How to set MediaConstraints in peerConnection.CreateAnswer() for Video Calling using WebRTC Jar for Android native

I referred and mixed code from internet, everything is working fine but when i'm passing new MediaContraints() in pc.createAnswer(), pc.setLocalDescription() method was not called. But when i'm passing constraints like "offerToRecieveAudio" and "video" is true, giving error like invalid Constrains.

Could anyone help me out.

Thanks

 private void createAnswer()
{
    Log.e(TAG,"Create Answer...............................");


    peerConnection.createAnswer(new CustomSdpObserver("create Answer")
    {
        @Override
        public void onCreateSuccess(SessionDescription sessionDescription)
        {
            super.onCreateSuccess(sessionDescription);

            Log.e(TAG,"onCreateAnswer...............................");

            peerConnection.setLocalDescription(new CustomSdpObserver("create Answer Local Description")
            {
                @Override
                public void onCreateSuccess(SessionDescription sessionDescription)
                {
                    super.onCreateSuccess(sessionDescription);

                    try {
                        JSONObject json = new JSONObject();
                        JSONObject jsonObject = new JSONObject();



                        JSONObject sessDescObj = new JSONObject();
                        sessDescObj.put("sdp",sessionDescription.description);
                        sessDescObj.put("type","ANSWER");
                        json.put("type", "rtc");
                        json.put("dest", CustomWebSocketListener.fromUser);
                        jsonObject.put("sdp", sessDescObj);



                        json.put("data",jsonObject);
                        Log.d(TAG,"Answer Success " + json.toString());

                        ws1.send(json.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, sessionDescription);
        }

        @Override
        public void onCreateFailure(String s)
        {
            super.onCreateFailure(s);

            Log.e(TAG,"onCreateFailure Answer " + s);
        }

        @Override
        public void onSetSuccess()
        {
            super.onSetSuccess();

            Log.e(TAG,"onSetSuccess Answer");
        }

        @Override
        public void onSetFailure(String s)
        {
            super.onSetFailure(s);

            Log.e(TAG,"onSetFailure Answer" + s);
        }
    }, new MediaConstraints());
}

Upvotes: 0

Views: 2291

Answers (2)

Ananth
Ananth

Reputation: 2735

In Kotlin you can take help from the below example:

private fun PeerConnection.answer(sdpObserver: SdpObserver) {
    val constraints = MediaConstraints().apply {
        mandatory.add(MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true"))
    }

    createAnswer(object : SdpObserver by sdpObserver {
        override fun onCreateSuccess(p0: SessionDescription?) {
            setLocalDescription(object : SdpObserver {
                override fun onSetFailure(p0: String?) {
                    Log.i("RTCClient", "answer onSetFailure")
                }

                override fun onSetSuccess() {
                    Log.i("RTCClient", "answer onSetSuccess")
                }

                override fun onCreateSuccess(p0: SessionDescription?) {
                    Log.i("RTCClient", "answer onCreateSuccess")
                }

                override fun onCreateFailure(p0: String?) {
                    Log.i("RTCClient", "answer onCreateFailure")
                }
            }, p0)
            sdpObserver.onCreateSuccess(p0)
        }
    }, constraints)
}

Above snippet is taken from here

For more help and examples, you can go here.

Upvotes: 0

Kamal
Kamal

Reputation: 45

I fixed this issue by myself and i was sending wrong data format to the server that is why i was unable to answer the call in pc.createAnswer().

correct data format is

{"type":"rtc","dest":"z","data":{"sdp":{"sdp":"v=0\r\no=- 524024530293356509 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=group:BUNDLE audio video\r\na=msid-semantic: WMS ARDAMS\r\nm=audio 9 UDP\/TLS\/RTP\/SAVPF 111 103 104 9 102 0 8 106 105 13 110 112 113 126\r\nc=IN IP4 0.0.0.0\r\na=rtcp:9 IN IP4 0.0.0.0\r\na=ice-ufrag:klaX\r\na=ice-pwd:XMJFsHZ4YD0RzSKEkaHOPY9p\r\na=ice-options:renomination\r\na=fingerprint:sha-256 FA:BC:AC:9C:8C:FD:27:8A:24:C3:7E:14:06:14:1F:2D:1E:F9:EC:4F:6E:B7:AD:AE:A4:FE:34:D7:84:9B:32:7E\r\na=setup:active\r\na=mid:audio\r\na=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\na=sendrecv\r\na=rtcp-mux\r\na=rtpmap:111 opus\/48000\/2\r\na=rtcp-fb:111 transport-cc\r\na=fmtp:111 minptime=10;useinbandfec=1\r\na=rtpmap:103 ISAC\/16000\r\na=rtpmap:104 ISAC\/32000\r\na=rtpmap:9 G722\/8000\r\na=rtpmap:102 ILBC\/8000\r\na=rtpmap:0 PCMU\/8000\r\na=rtpmap:8 PCMA\/8000\r\na=rtpmap:106 CN\/32000\r\na=rtpmap:105 CN\/16000\r\na=rtpmap:13 CN\/8000\r\na=rtpmap:110 telephone-event\/48000\r\na=rtpmap:112 telephone-event\/32000\r\na=rtpmap:113 telephone-event\/16000\r\na=rtpmap:126 telephone-event\/8000\r\na=ssrc:257339793 cname:PWk1dSqFR0EwmC9i\r\na=ssrc:257339793 msid:ARDAMS ARDAMSa0\r\na=ssrc:257339793 mslabel:ARDAMS\r\na=ssrc:257339793 label:ARDAMSa0\r\nm=video 9 UDP\/TLS\/RTP\/SAVPF 96 98 100 127 97 99 101\r\nc=IN IP4 0.0.0.0\r\na=rtcp:9 IN IP4 0.0.0.0\r\na=ice-ufrag:klaX\r\na=ice-pwd:XMJFsHZ4YD0RzSKEkaHOPY9p\r\na=ice-options:renomination\r\na=fingerprint:sha-256 FA:BC:AC:9C:8C:FD:27:8A:24:C3:7E:14:06:14:1F:2D:1E:F9:EC:4F:6E:B7:AD:AE:A4:FE:34:D7:84:9B:32:7E\r\na=setup:active\r\na=mid:video\r\na=extmap:2 urn:ietf:params:rtp-hdrext:toffset\r\na=extmap:3 http:\/\/www.webrtc.org\/experiments\/rtp-hdrext\/abs-send-time\r\na=extmap:4 urn:3gpp:video-orientation\r\na=extmap:5 http:\/\/www.ietf.org\/id\/draft-holmer-rmcat-transport-wide-cc-extensions-01\r\na=extmap:6 http:\/\/www.webrtc.org\/experiments\/rtp-hdrext\/playout-delay\r\na=sendrecv\r\na=rtcp-mux\r\na=rtcp-rsize\r\na=rtpmap:96 VP8\/90000\r\na=rtcp-fb:96 ccm fir\r\na=rtcp-fb:96 nack\r\na=rtcp-fb:96 nack pli\r\na=rtcp-fb:96 goog-remb\r\na=rtcp-fb:96 transport-cc\r\na=rtpmap:98 VP9\/90000\r\na=rtcp-fb:98 ccm fir\r\na=rtcp-fb:98 nack\r\na=rtcp-fb:98 nack pli\r\na=rtcp-fb:98 goog-remb\r\na=rtcp-fb:98 transport-cc\r\na=rtpmap:100 red\/90000\r\na=rtpmap:127 ulpfec\/90000\r\na=rtpmap:97 rtx\/90000\r\na=fmtp:97 apt=96\r\na=rtpmap:99 rtx\/90000\r\na=fmtp:99 apt=98\r\na=rtpmap:101 rtx\/90000\r\na=fmtp:101 apt=100\r\na=ssrc-group:FID 706483969 3503188891\r\na=ssrc:706483969 cname:PWk1dSqFR0EwmC9i\r\na=ssrc:706483969 msid:ARDAMS ARDAMSv0\r\na=ssrc:706483969 mslabel:ARDAMS\r\na=ssrc:706483969 label:ARDAMSv0\r\na=ssrc:3503188891 cname:PWk1dSqFR0EwmC9i\r\na=ssrc:3503188891 msid:ARDAMS ARDAMSv0\r\na=ssrc:3503188891 mslabel:ARDAMS\r\na=ssrc:3503188891 label:ARDAMSv0\r\n","type":"answer"}}}

java code

peerConnection.createAnswer(new SimpleSdpObserver() {

        @Override
        public void onCreateSuccess(SessionDescription sessionDescription) {
            peerConnection.setLocalDescription(new SimpleSdpObserver(), sessionDescription);
            //JSONObject message = new JSONObject();
            JSONObject json = new JSONObject();
            JSONObject jsonObject = new JSONObject();
            try {
                json.put("type", "rtc");
                json.put("dest", CustomWebSocketListener.fromUser);
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("sdp", sessionDescription.description);
                jsonObject1.put("type", "answer");
                jsonObject.put("sdp", jsonObject1);//sessionDescription.description
                json.put("data", jsonObject);

                sendMessage(json);


            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

u can also look at this can't add Remote session description in webrtc android client

From Github https://github.com/IhorKlimov/Android-WebRtc/blob/master/app/src/main/java/com/myhexaville/androidwebrtc/tutorial/CompleteActivity.java

Upvotes: 0

Related Questions