Search code examples
javasippjsippjsua2

PJSUA2 Java - How to get ongoing Call's AudioMedia


I'm using the Java library for PJSUA / PJSIP and i'm trying to get the AudioMedia for an answered call but its not working. I've followed the C++ documentation (no Java doc for answering calls) which has led me to the following implementation:

public void onCallMediaState(OnCallMediaStateParam param) {
    CallInfo ci = this.getInfo();

    for(int i = 0; i < ci.getMedia().size(); i++) {
        if(ci.getMedia().get(i).getType() == pjmedia_type.PJMEDIA_TYPE_AUDIO) {

            AudioMedia aum = (AudioMedia) this.getMedia(i);
        }
    }
}

The first part works, it finds a media in the call info with type PJMEDIA_TYPE_AUDIO, and if i check the type of this.getMedia(i) that is also PJMEDIA_TYPE_AUDIO. However when I try to cast it to type AudioMedia it fails to cast.

I assume the rest of SIP setup is working, as when I call the number, pjsua reports the incoming call and answers it, I'm just unable to get the AudioMedia to send/receive audio.

The documentation is for C++ but thus far it has been exactly the same for Java except for this part, Reference here. What am I doing wrong?


Solution

  • Found it!

    AudioMedia has a static method typecastFromMedia(Media media) for casting to AudioMedia. I assume this is because the casting has to occur in the underlying C++ implementation so you can't just do a high-level java cast.

    Working example:

    public void onCallMediaState(OnCallMediaStateParam param) {
        CallInfo ci = this.getInfo();
    
        for(int i = 0; i < ci.getMedia().size(); i++) {
            if(ci.getMedia().get(i).getType() == pjmedia_type.PJMEDIA_TYPE_AUDIO) {
    
                AudioMedia aum = AudioMedia.typecastFromMedia(this.getMedia(i));
            }
        }
    }