Search code examples
androidpjsippjsua2voip-android

Android pjsip: First Incoming Video call gives CallmediaInfo status PJSUA_CALL_MEDIA_NONE


I am using Pjsip(v2.6) for VoIP Audio and Video call.

I am facing issue in first Incoming Video Call. It always fails when I register a user and make a video call.

When I was debugging my code, I found that I am receiving CallMediaInfo status as PJSUA_CALL_MEDIA_NONE. When I make a second attempt, I received it as PJSUA_CALL_MEDIA_ACTIVE and preview gets displayed.

Here is my code for makeCall:

fun makeCall(id: String, isVideo: Boolean) {
        video = isVideo
        val buddyUri = "sip:$CALLEE@$URI"

        val call = MyCall(account, -1)
        val prm = CallOpParam(true)
        val callSettings = prm.opt
        if (isVideo) {
            callSettings.videoCount = 1
            callSettings.audioCount = 1
        } else {
            callSettings.audioCount = 1
            callSettings.videoCount = 0
        }

        try {
            call.makeCall(buddyUri, prm)
        } catch (e: Exception) {
            call.delete()
        }
        currentCall = call
    }

Here is my code for which executes Incoming_Call notification:

if (m?.what == Constants.MSGTYPE.INCOMING_CALL) {

            /* Incoming call */
            val call = m.obj as MyCall
            val prm = CallOpParam()

            /* Only one call at anytime */
            if (currentCall != null) {
                call.delete()
                return true
            }

            /* Answer with ringing */
            prm.statusCode = pjsip_status_code.PJSIP_SC_RINGING
            try {
                val callSetting = prm.opt
                callSetting?.audioCount = 1
                if (video) {
                    callSetting?.videoCount = 1
                } else callSetting?.videoCount = 0
                call.answer(prm)
            } catch (e: Exception) {
                println(e)
            }

            currentCall = call
            if (call.info.remOfferer && call.info.remVideoCount == 1L) {
                // start Video Activity
            } else {
                // start Audio Activity
            }
        }

When I accept the call, I received onCallMediaState callback:

override fun onCallMediaState(prm: OnCallMediaStateParam) {
        val ci: CallInfo
        try {
            ci = info
        } catch (e: Exception) {
            return
        }

        val cmiv = ci.media

        for (i in 0 until cmiv!!.size()) {
            val cmi = cmiv.get(i.toInt())
            if (cmi.type == pjmedia_type.PJMEDIA_TYPE_AUDIO && (cmi.status == pjsua_call_media_status.PJSUA_CALL_MEDIA_ACTIVE || cmi.status == pjsua_call_media_status.PJSUA_CALL_MEDIA_REMOTE_HOLD)) {
                LogUtils.e("AANAL onCallMediaState  Audio")
                handleAudioMedia(getMedia(i))
            } else if (cmi.type == pjmedia_type.PJMEDIA_TYPE_VIDEO &&
//                    cmi.status == pjsua_call_media_status.PJSUA_CALL_MEDIA_ACTIVE &&
                    cmi.videoIncomingWindowId != pjsua2.INVALID_ID) {
                LogUtils.e("AANAL onCallMediaState  Video")
                handleVideoMedia(cmi)
            }
        }

        MyApp.observer.notifyCallMediaState(this)
    }

Can anyone help me with this?

Thank you in advance!


Solution

  • I found my own answer. It was because of incorrect videoCount I was setting at time of answer call.

    Make sure that you are setting videoCount = 1 when you call call.answer method in Incoming Call callback.

    eg.

         if (m?.what == Constants.MSGTYPE.INCOMING_CALL) {
            ...
            try {
                            val callSetting = prm.opt
                            callSetting?.audioCount = 1
                            // I have corrected this videoCount
                            if (call.info.remOfferer && call.info.remVideoCount == 1L) { 
                                callSetting?.videoCount = 1
                            } else callSetting?.videoCount = 0
                            call.answer(prm)
                        } catch (e: Exception) {
                            println(e)
                        }
    
            ...
            }