datso / react-native-pjsip

A PJSIP module for React Native.
http://datso.github.io/react-native-pjsip
GNU General Public License v3.0
272 stars 229 forks source link

conference call implementation for android #135

Open Daniel231 opened 5 years ago

Daniel231 commented 5 years ago

Hi guys, I'm trying to implement conference call for android. Iv'e looked at this documentation https://www.pjsip.org/docs/book-latest/html/media.html#conference-call , and successfully made a conference call.

the problem is that ive changed something in the code and i cant fix it back.

any help?

my code:

PjSipService.java:

    private void handleCallConference(Intent intent) {
        try {
            int callId = intent.getIntExtra("call_id", -1);
            PjSipCall call = findCall(callId);
            List<AudioMedia> mCallsAudioMedia = new ArrayList<>();
            List<AudDevManager> mCallsMgr = new ArrayList<>();
            for (PjSipCall currCall : mCalls)
            {
                        for (int i = 0; i < currCall.getInfo().getMedia().size(); i++) {
                            currCall.unhold();
                            Media media = currCall.getMedia(i);
                            CallMediaInfo mediaInfo = currCall.getInfo().getMedia().get(i);
                            if (mediaInfo.getType() == pjmedia_type.PJMEDIA_TYPE_AUDIO
                                && media != null
                                && mediaInfo.getStatus() == pjsua_call_media_status.PJSUA_CALL_MEDIA_ACTIVE) {
                                AudioMedia audioMedia = AudioMedia.typecastFromMedia(media);
                                mCallsAudioMedia.add(audioMedia);
                                mCallsMgr.add(currCall.getService().mAccounts.get(0).getService().getAudDevManager()); // the 0 for the test
                            }
                        }
            }
             // for (int j =0; j< mCallsAudioMedia.length(); j++)
            // {
                AudioMedia currAudioMedia = mCallsAudioMedia.get(0); // the 0 and 1 only for testing
                AudioMedia secAudioMedia = mCallsAudioMedia.get(1);
                AudDevManager currMgr = mCallsMgr.get(0);
                AudDevManager secMgr = mCallsMgr.get(1);
                currMgr.getCaptureDevMedia().startTransmit(secAudioMedia);
                secMgr.getCaptureDevMedia().startTransmit(currAudioMedia);
            // }
             mEmitter.fireIntentHandled(intent);
        } catch (Exception e) {
            mEmitter.fireIntentHandled(intent, e);
        }
    }

PjSipModule.java:

 @ReactMethod
    public void conferenceCall(int callId, Callback callback) {
        int callbackId = receiver.register(callback);
        Intent intent = PjActions.createConferenceCallIntent(callbackId, callId, getReactApplicationContext());
        getReactApplicationContext().startService(intent);
    }

Endpoint.js:

    conferenceCall(call) {
        return new Promise((resolve, reject) => {
            NativeModules.PjSipModule.conferenceCall(call.getId(), (successful, data) => {
                if (successful) {
                    resolve(data);
                } else {
                    reject(data);
                }
            });
        });
    }
moisesynfam commented 5 years ago

Did you ever get it back to work ?

Daniel231 commented 5 years ago

Yepp i've successfully managed to fix the problem for all devices types, idk if its best practice but its work :D.

The problem was with the function getMedia in Call.js inside pjsua2 folder, from some reason when i used this method at first time i've always got 0/null answer but when i've put a break point before calling this method i did received a correct value from some reason its seemed like this method acting in async way.

So this the solution iv`e come with is this:

android/src/main/java/org/pjsip/pjsua2/Call.java:

  public synchronized Media getMedia(long med_idx) {
    Media media = null;
    do {
      try {
        wait(100);
      } catch (Exception e) {
        e.printStackTrace();
      }
      long cPtr = pjsua2JNI.Call_getMedia(swigCPtr, this, med_idx);
      media = (cPtr == 0) ? null : new Media(cPtr, false);
    } while (media == null);
    return media;
  }
}

and that it the rest of the code is the same as i wrote above, hope that helps i'm here if there is some issues sorry for the long delay with the answer XD