Skip to content

Commit 153cdc7

Browse files
committed
Fixes #91: Implement incoming webrtc call functionality
1 parent be29c49 commit 153cdc7

File tree

6 files changed

+168
-65
lines changed

6 files changed

+168
-65
lines changed

Examples/restcomm-messenger/app/src/main/java/com/telestax/restcomm_messenger/CallActivity.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public class CallActivity extends Activity implements RCConnectionListener, View
5151
Button btnDecline;
5252
Button btnCancel;
5353

54+
// TODO: remove those when the API is structured properlu
55+
String incomingCallDid = "";
56+
String incomingCallSdp = "";
57+
5458
@Override
5559
protected void onCreate(Bundle savedInstanceState) {
5660
super.onCreate(savedInstanceState);
@@ -96,14 +100,13 @@ protected void onCreate(Bundle savedInstanceState) {
96100
cbMuted.setEnabled(false);
97101

98102
device = RCClient.getInstance().listDevices().get(0);
103+
104+
PreferenceManager.setDefaultValues(this, "preferences.xml", MODE_PRIVATE, R.xml.preferences, false);
105+
prefs = PreferenceManager.getDefaultSharedPreferences(this);
106+
99107
// Get Intent parameters.
100108
final Intent intent = getIntent();
101109
if (intent.getAction() == RCDevice.OUTGOING_CALL) {
102-
PreferenceManager.setDefaultValues(this, "preferences.xml", MODE_PRIVATE, R.xml.preferences, false);
103-
prefs = PreferenceManager.getDefaultSharedPreferences(this);
104-
105-
//prefs = getSharedPreferences("preferences.xml", MODE_PRIVATE);
106-
107110
connectParams.put("username", intent.getStringExtra(RCDevice.EXTRA_DID));
108111
connection = device.connect(connectParams, this, videoView, prefs);
109112

@@ -112,6 +115,19 @@ protected void onCreate(Bundle savedInstanceState) {
112115
return;
113116
}
114117
}
118+
if (intent.getAction() == RCDevice.INCOMING_CALL) {
119+
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
120+
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
121+
ringingPlayer.start();
122+
}
123+
pendingConnection = device.incomingConnection;
124+
pendingConnection.updateListener(this);
125+
pendingConnection.setupWebrtcForIncomingCall(videoView, prefs);
126+
127+
// TODO: remove those
128+
incomingCallDid = intent.getStringExtra(RCDevice.EXTRA_DID);
129+
incomingCallSdp = intent.getStringExtra(RCDevice.EXTRA_SDP);
130+
}
115131
}
116132

117133
// UI Events
@@ -131,7 +147,8 @@ public void onClick(View view) {
131147
}
132148
} else if (view.getId() == R.id.button_answer) {
133149
if (pendingConnection != null) {
134-
pendingConnection.accept();
150+
//pendingConnection.accept();
151+
pendingConnection.answerCall(incomingCallDid, incomingCallSdp);
135152
connection = this.pendingConnection;
136153
ringingPlayer.pause();
137154
// Abandon audio focus when playback complete

restcomm.android.client.sdk/src/main/java/org/mobicents/restcomm/android/client/sdk/RCConnection.java

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ public RCConnection(RCConnectionListener connectionListener)
182182
this.listener = connectionListener;
183183
}
184184

185+
// could not use the previous constructor with connectionListener = null, hence created this:
186+
public RCConnection()
187+
{
188+
this.listener = null;
189+
}
190+
191+
public void updateListener(RCConnectionListener listener)
192+
{
193+
this.listener = listener;
194+
}
195+
185196
// 'Copy' constructor
186197
public RCConnection(RCConnection connection)
187198
{
@@ -202,12 +213,32 @@ public void setupWebrtcAndCall(GLSurfaceView videoView, SharedPreferences prefs,
202213
iceServers.add(new PeerConnection.IceServer("stun:stun.l.google.com:19302", "", ""));
203214
this.signalingParameters = new SignalingParameters(iceServers, true, "", sipUri, "", null, null);
204215

205-
startCall();
216+
startCall(this.signalingParameters);
217+
}
218+
219+
public void setupWebrtcForIncomingCall(GLSurfaceView videoView, SharedPreferences prefs)
220+
{
221+
initializeWebrtc(videoView, prefs);
222+
}
223+
224+
// TODO: remove this when ready
225+
public void answerCall(String sipUri, String sdp)
226+
{
227+
228+
LinkedList<PeerConnection.IceServer> iceServers = new LinkedList<>();
229+
iceServers.add(new PeerConnection.IceServer("stun:stun.l.google.com:19302", "", ""));
230+
this.signalingParameters = new SignalingParameters(iceServers, false, "", sipUri, "", null, null);
231+
SignalingParameters params = SignalingParameters.extractCandidates(new SessionDescription(SessionDescription.Type.OFFER, sdp));
232+
this.signalingParameters.offerSdp = params.offerSdp;
233+
this.signalingParameters.iceCandidates = params.iceCandidates;
234+
235+
startCall(this.signalingParameters);
206236
}
207237

208238
// initialize webrtc facilities for the call
209239
private void initializeWebrtc(GLSurfaceView videoView, SharedPreferences prefs)
210240
{
241+
Log.e(TAG, "@@@@@ initializeWebrtc ");
211242
Context context = RCClient.getInstance().context;
212243
keyprefVideoCallEnabled = context.getString(R.string.pref_videocall_key);
213244
keyprefResolution = context.getString(R.string.pref_resolution_key);
@@ -231,6 +262,7 @@ private void initializeWebrtc(GLSurfaceView videoView, SharedPreferences prefs)
231262

232263
public void setupWebrtc(GLSurfaceView videoView, SharedPreferences prefs)
233264
{
265+
Log.e(TAG, "@@@@@ setupWebrtc");
234266
Context context = RCClient.getInstance().context;
235267
//Thread.setDefaultUncaughtExceptionHandler(
236268
// new UnhandledExceptionHandler(this));
@@ -393,8 +425,9 @@ public void run() {
393425
*/
394426
}
395427

396-
private void startCall()
428+
private void startCall(SignalingParameters signalingParameters)
397429
{
430+
Log.e(TAG, "@@@@@ startCall");
398431
callStartedTimeMs = System.currentTimeMillis();
399432

400433
// Start room connection.
@@ -423,6 +456,7 @@ public void run() {
423456

424457
// Disconnect from remote resources, dispose of local resources, and exit.
425458
public void disconnectWebrtc() {
459+
Log.e(TAG, "@@@@@ disconnectWebrtc");
426460
activityRunning = false;
427461
/* Signaling is already disconnected
428462
if (appRtcClient != null) {
@@ -462,6 +496,7 @@ private void createPeerConnectionFactory() {
462496
Runnable myRunnable = new Runnable() {
463497
@Override
464498
public void run() {
499+
Log.e(TAG, "@@@@@ createPeerConnectionFactory");
465500
if (peerConnectionClient == null) {
466501
final long delta = System.currentTimeMillis() - callStartedTimeMs;
467502
Log.d(TAG, "Creating peer connection factory, delay=" + delta + "ms");
@@ -502,6 +537,7 @@ public void onLocalDescription(final SessionDescription sdp) {
502537
Runnable myRunnable = new Runnable() {
503538
@Override
504539
public void run() {
540+
Log.e(TAG, "@@@@@ onLocalDescription");
505541
if (signalingParameters != null && !signalingParameters.sipUrl.isEmpty()) {
506542
logAndToast("Sending " + sdp.type + ", delay=" + delta + "ms");
507543
if (signalingParameters.initiator) {
@@ -510,6 +546,13 @@ public void run() {
510546
//appRtcClient.sendOfferSdp(sdp);
511547
} else {
512548
//appRtcClient.sendAnswerSdp(sdp);
549+
connection.signalingParameters.answerSdp = sdp;
550+
// for an incoming call we have already stored the offer candidates there, now
551+
// we are done with those and need to come up with answer candidates
552+
// TODO: this might prove dangerous as the signalingParms struct used to be all const,
553+
// but I changed it since with JAIN sip signalling where various parts are picked up
554+
// at different points in time
555+
connection.signalingParameters.iceCandidates.clear();
513556
}
514557
}
515558
}
@@ -524,7 +567,7 @@ public void onIceCandidate(final IceCandidate candidate) {
524567
Runnable myRunnable = new Runnable() {
525568
@Override
526569
public void run() {
527-
Log.e(TAG, "onIceCandidate:");
570+
Log.e(TAG, "@@@@@ onIceCandidate:");
528571
connection.signalingParameters.addIceCandidate(candidate);
529572
/*
530573
if (appRtcClient != null) {
@@ -544,9 +587,17 @@ public void onIceGatheringComplete()
544587
Runnable myRunnable = new Runnable() {
545588
@Override
546589
public void run() {
547-
Log.e(TAG, "onIceGatheringComplete");
548-
// we have gathered all candidates and SDP. Combine then in SIP SDP and send over to JAIN SIP
549-
DeviceImpl.GetInstance().CallWebrtc(signalingParameters.sipUrl, connection.signalingParameters.generateSipSdp());
590+
Log.e(TAG, "@@@@@ onIceGatheringComplete");
591+
if (signalingParameters.initiator) {
592+
// we have gathered all candidates and SDP. Combine then in SIP SDP and send over to JAIN SIP
593+
DeviceImpl.GetInstance().CallWebrtc(signalingParameters.sipUrl,
594+
connection.signalingParameters.generateSipSdp(connection.signalingParameters.offerSdp,
595+
connection.signalingParameters.iceCandidates));
596+
}
597+
else {
598+
DeviceImpl.GetInstance().AcceptWebrtc(connection.signalingParameters.generateSipSdp(connection.signalingParameters.answerSdp,
599+
connection.signalingParameters.iceCandidates));
600+
}
550601
}
551602
};
552603
mainHandler.post(myRunnable);
@@ -560,6 +611,7 @@ public void onIceConnected() {
560611
Runnable myRunnable = new Runnable() {
561612
@Override
562613
public void run() {
614+
Log.e(TAG, "@@@@@ onIceConnected");
563615
logAndToast("ICE connected, delay=" + delta + "ms");
564616
iceConnected = true;
565617
//callConnected();
@@ -574,6 +626,7 @@ public void onIceDisconnected() {
574626
Runnable myRunnable = new Runnable() {
575627
@Override
576628
public void run() {
629+
Log.e(TAG, "@@@@@ onIceDisconnected");
577630
logAndToast("ICE disconnected");
578631
iceConnected = false;
579632
disconnect();
@@ -584,6 +637,7 @@ public void run() {
584637

585638
@Override
586639
public void onPeerConnectionClosed() {
640+
Log.e(TAG, "@@@@@ onPeerConnectionClosed");
587641
}
588642

589643
@Override
@@ -624,6 +678,7 @@ public void run() {
624678
}
625679

626680
private void onConnectedToRoomInternal(final SignalingParameters params) {
681+
Log.e(TAG, "@@@@@ onConnectedToRoomInternal");
627682
final long delta = System.currentTimeMillis() - callStartedTimeMs;
628683

629684
signalingParameters = params;
@@ -668,6 +723,7 @@ public void onRemoteDescription(final SessionDescription sdp) {
668723
Runnable myRunnable = new Runnable() {
669724
@Override
670725
public void run() {
726+
Log.e(TAG, "@@@@@ onRemoteDescription");
671727
if (peerConnectionClient == null) {
672728
Log.e(TAG, "Received remote SDP for non-initilized peer connection.");
673729
return;
@@ -690,6 +746,7 @@ public void run() {
690746

691747
//@Override
692748
public void onRemoteIceCandidates(final List<IceCandidate> candidates) {
749+
Log.e(TAG, "@@@@@ onRemoteIceCandidates");
693750
// no need to run it in UI thread it is already there due to onRemoteDescription
694751
if (peerConnectionClient == null) {
695752
Log.e(TAG,
@@ -770,6 +827,14 @@ public void accept()
770827
}
771828
}
772829

830+
private void acceptWebrtc(final String sdp)
831+
{
832+
if (haveConnectivity()) {
833+
DeviceImpl.GetInstance().AcceptWebrtc(sdp);
834+
this.state = state.CONNECTED;
835+
}
836+
}
837+
773838
/**
774839
* Ignore incoming connection
775840
*/
@@ -873,9 +938,10 @@ public void onSipUAConnected(SipEvent event)
873938
this.state = ConnectionState.CONNECTED;
874939
final RCConnection finalConnection = new RCConnection(this);
875940

876-
// notify RCDevice (this is temporary)
877-
//RCDevice device = RCClient.getInstance().listDevices().get(0);
878-
onRemoteDescription(event.sdp);
941+
// we want to notify webrtc onRemoteDescription *only* on an outgoing call
942+
if (!this.isIncoming()) {
943+
onRemoteDescription(event.sdp);
944+
}
879945

880946
// Important: need to fire the event in UI context cause currently we 're in JAIN SIP thread
881947
Handler mainHandler = new Handler(RCClient.getInstance().context.getMainLooper());

restcomm.android.client.sdk/src/main/java/org/mobicents/restcomm/android/client/sdk/RCDevice.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,16 @@ public enum DeviceCapability {
106106
CLIENT_NAME,
107107
}
108108

109+
private static final String TAG = "RCDevice";
109110
public static String OUTGOING_CALL = "ACTION_OUTGOING_CALL";
110111
public static String INCOMING_CALL = "ACTION_INCOMING_CALL";
111112
public static String INCOMING_MESSAGE = "ACTION_INCOMING_MESSAGE";
112113
public static String EXTRA_DID = "com.telestax.restcomm_messenger.DID";
113-
public static String EXTRA_DEVICE = "com.telestax.restcomm.android.client.sdk.extra-device";
114-
public static String EXTRA_CONNECTION = "com.telestax.restcomm.android.client.sdk.extra-connection";
114+
public static String EXTRA_SDP = "com.telestax.restcomm_messenger.SDP";
115+
//public static String EXTRA_DEVICE = "com.telestax.restcomm.android.client.sdk.extra-device";
116+
//public static String EXTRA_CONNECTION = "com.telestax.restcomm.android.client.sdk.extra-connection";
115117
PendingIntent pendingIntent;
116-
private static final String TAG = "RCDevice";
118+
public RCConnection incomingConnection;
117119

118120
/*
119121
// #webrtc
@@ -458,14 +460,15 @@ public void updateParams(HashMap<String, String> params) {
458460

459461
// SipUA listeners
460462
public void onSipUAConnectionArrived(SipEvent event) {
461-
RCConnectionListener connectionListener = (RCConnectionListener) this.listener;
462-
RCConnection connection = new RCConnection(connectionListener);
463-
connection.incoming = true;
464-
connection.state = RCConnection.ConnectionState.CONNECTING;
465-
DeviceImpl.GetInstance().sipuaConnectionListener = connection;
463+
//RCConnectionListener connectionListener = (RCConnectionListener) this.listener;
464+
incomingConnection = new RCConnection();
465+
incomingConnection.incoming = true;
466+
incomingConnection.state = RCConnection.ConnectionState.CONNECTING;
467+
DeviceImpl.GetInstance().sipuaConnectionListener = incomingConnection;
466468

467469
// Important: need to fire the event in UI context cause currently we 're in JAIN SIP thread
468470
final String from = event.from;
471+
final String sdp = event.sdp;
469472
Handler mainHandler = new Handler(RCClient.getInstance().context.getMainLooper());
470473
Runnable myRunnable = new Runnable() {
471474
@Override
@@ -474,9 +477,8 @@ public void run() {
474477
try {
475478
Intent dataIntent = new Intent();
476479
dataIntent.setAction(INCOMING_CALL);
477-
478480
dataIntent.putExtra(RCDevice.EXTRA_DID, from);
479-
481+
dataIntent.putExtra(RCDevice.EXTRA_SDP, sdp);
480482
pendingIntent.send(RCClient.getInstance().context, 0, dataIntent);
481483

482484
} catch (PendingIntent.CanceledException e) {

restcomm.android.client.sdk/src/main/java/org/mobicents/restcomm/android/client/sdk/SignalingParameters.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public class SignalingParameters {
2222
public final String sipUrl;
2323
public final String wssPostUrl;
2424
public SessionDescription offerSdp;
25+
public SessionDescription answerSdp;
2526
public List<IceCandidate> iceCandidates;
27+
//public List<IceCandidate> answerIceCandidates;
2628

2729
public SignalingParameters(
2830
List<PeerConnection.IceServer> iceServers,
@@ -35,7 +37,9 @@ public SignalingParameters(
3537
this.sipUrl = sipUrl;
3638
this.wssPostUrl = wssPostUrl;
3739
this.offerSdp = offerSdp;
40+
this.answerSdp = null;
3841
this.iceCandidates = iceCandidates;
42+
//this.answerIceCandidates = null;
3943
}
4044
public SignalingParameters() {
4145
this.iceServers = null;
@@ -44,11 +48,13 @@ public SignalingParameters() {
4448
this.sipUrl = "";
4549
this.wssPostUrl = "";
4650
this.offerSdp = null;
51+
this.answerSdp = null;
4752
this.iceCandidates = null;
53+
//this.answerIceCandidates = null;
4854
}
4955

5056
// combines offerSdp with iceCandidates and comes up with the full SDP
51-
public String generateSipSdp() {
57+
public String generateSipSdp(SessionDescription offerSdp, List<IceCandidate> iceCandidates) {
5258
// concatenate all candidates in one String
5359
String candidates = "";
5460
for (IceCandidate candidate : iceCandidates) {
@@ -81,7 +87,7 @@ public static SignalingParameters extractCandidates(SessionDescription sdp) {
8187
}
8288

8389
// remove candidates from SDP
84-
params.offerSdp = new SessionDescription(sdp.type, sdp.description.replaceAll("a=candidate.*?\\r\\n", ""));
90+
params.offerSdp = new SessionDescription(sdp.type, sdp.description.replaceAll("a=candidate.*?\\r\\n", ""));
8591

8692
return params;
8793
}

sipua/src/main/java/org/mobicents/restcomm/android/sipua/impl/DeviceImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void onSipMessage(final SipEvent sipEventObject) {
9696
}
9797
} else if (sipEventObject.type == SipEventType.LOCAL_RINGING) {
9898
if (this.sipuaDeviceListener != null) {
99-
this.sipuaDeviceListener.onSipUAConnectionArrived(null);
99+
this.sipuaDeviceListener.onSipUAConnectionArrived(sipEventObject);
100100
}
101101
}
102102
}
@@ -123,6 +123,10 @@ public void Accept() {
123123
sipManager.AcceptCall(soundManager.setupAudioStream());
124124
}
125125

126+
public void AcceptWebrtc(final String sdp) {
127+
sipManager.AcceptCallWebrtc(sdp);
128+
}
129+
126130
@Override
127131
public void Reject() {
128132
sipManager.RejectCall();

0 commit comments

Comments
 (0)