3

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?

Tom
  • 508
  • 4
  • 16

2 Answers2

3

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));
        }
    }
}
Tom
  • 508
  • 4
  • 16
-1

You can using getAudioMedia() API to get AudioMedia, my simple onCallMediaState() in SipCall(subclass of Call class) using C++:

void SipCall::onCallMediaState(OnCallMediaStateParam &prm) {
    this->callInfo = getInfo();
    unsigned media_size = this->callInfo.media.size();
    for (unsigned i = 0; i < media_size; i++) { 
        AudioMedia audioMedia = getAudioMedia(i);
        // do something
        //...
    }
}
levanha
  • 21
  • 1