Skip to content

Commit

Permalink
Retain call start timestamp per peer to prevent race conditions.
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-signal committed Sep 24, 2020
1 parent f9a9ee6 commit 4cd433b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ public final class RemotePeer implements Remote, Parcelable
@NonNull private final RecipientId recipientId;
@NonNull private CallState callState;
@NonNull private CallId callId;
private long callStartTimestamp;

public RemotePeer(@NonNull RecipientId recipientId) {
this.recipientId = recipientId;
this.callState = CallState.IDLE;
this.callId = new CallId(-1L);
this.recipientId = recipientId;
this.callState = CallState.IDLE;
this.callId = new CallId(-1L);
this.callStartTimestamp = 0;
}

private RemotePeer(@NonNull Parcel in) {
this.recipientId = RecipientId.CREATOR.createFromParcel(in);
this.callState = CallState.values()[in.readInt()];
this.callId = new CallId(in.readLong());
this.recipientId = RecipientId.CREATOR.createFromParcel(in);
this.callState = CallState.values()[in.readInt()];
this.callId = new CallId(in.readLong());
this.callStartTimestamp = in.readLong();
}

public @NonNull CallId getCallId() {
Expand All @@ -44,6 +47,14 @@ public void setCallId(@NonNull CallId callId) {
this.callId = callId;
}

public void setCallStartTimestamp(long callStartTimestamp) {
this.callStartTimestamp = callStartTimestamp;
}

public long getCallStartTimestamp() {
return callStartTimestamp;
}

public @NonNull CallState getState() {
return callState;
}
Expand Down Expand Up @@ -135,6 +146,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
recipientId.writeToParcel(dest, flags);
dest.writeInt(callState.ordinal());
dest.writeLong(callId.longValue());
dest.writeLong(callStartTimestamp);
}

public static final Creator<RemotePeer> CREATOR = new Creator<RemotePeer>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ public class WebRtcCallService extends Service implements CallManager.Observer,
@Nullable private RemotePeer busyPeer;
@Nullable private RemotePeer preJoinPeer;
@Nullable private SparseArray<RemotePeer> peerMap;
private long callStartTimestamp;

@Nullable private EglBase eglBase;
@Nullable private BroadcastVideoSink localSink;
Expand Down Expand Up @@ -436,7 +435,7 @@ private void handleReceivedOffer(Intent intent) {
}

peerMap.append(remotePeer.hashCode(), remotePeer);
callStartTimestamp = serverReceivedTimestamp;
remotePeer.setCallStartTimestamp(serverReceivedTimestamp);
Log.i(TAG, "add remotePeer callId: " + remotePeer.getCallId() + " key: " + remotePeer.hashCode());

isRemoteVideoOffer = offerType == OfferMessage.Type.VIDEO_CALL;
Expand Down Expand Up @@ -524,7 +523,7 @@ private void handleOutgoingCall(Intent intent) {
EventBus.getDefault().removeStickyEvent(WebRtcViewModel.class);

peerMap.append(remotePeer.hashCode(), remotePeer);
callStartTimestamp = System.currentTimeMillis();
remotePeer.setCallStartTimestamp(System.currentTimeMillis());
Log.i(TAG, "add remotePeer callId: " + remotePeer.getCallId() + " key: " + remotePeer.hashCode());

initializeVideo();
Expand Down Expand Up @@ -1168,7 +1167,7 @@ private void handleReceivedOfferExpired(Intent intent) {

Log.i(TAG, "handleReceivedOfferExpired(): call_id: " + remotePeer.getCallId());

insertMissedCall(remotePeer, true, callStartTimestamp);
insertMissedCall(remotePeer, true, remotePeer.getCallStartTimestamp());

terminate(remotePeer);
}
Expand Down Expand Up @@ -1197,7 +1196,7 @@ private void handleReceivedOfferWhileActive(Intent intent) {
stopForeground(true);
}

insertMissedCall(remotePeer, true, callStartTimestamp);
insertMissedCall(remotePeer, true, remotePeer.getCallStartTimestamp());

terminate(remotePeer);
}
Expand All @@ -1218,7 +1217,7 @@ private void handleEndedRemoteHangup(Intent intent) {

boolean incomingBeforeAccept = remotePeer.getState() == CallState.ANSWERING || remotePeer.getState() == CallState.LOCAL_RINGING;
if (incomingBeforeAccept) {
insertMissedCall(remotePeer, true, callStartTimestamp);
insertMissedCall(remotePeer, true, remotePeer.getCallStartTimestamp());
}

terminate(remotePeer);
Expand Down Expand Up @@ -1305,7 +1304,7 @@ private void handleEndedRemoteGlare(Intent intent) {

boolean incomingBeforeAccept = remotePeer.getState() == CallState.ANSWERING || remotePeer.getState() == CallState.LOCAL_RINGING;
if (incomingBeforeAccept) {
insertMissedCall(remotePeer, true, callStartTimestamp);
insertMissedCall(remotePeer, true, remotePeer.getCallStartTimestamp());
}

terminate(remotePeer);
Expand All @@ -1321,7 +1320,7 @@ private void handleEndedFailure(Intent intent) {
}

if (remotePeer.getState() == CallState.ANSWERING || remotePeer.getState() == CallState.LOCAL_RINGING) {
insertMissedCall(remotePeer, true, callStartTimestamp);
insertMissedCall(remotePeer, true, remotePeer.getCallStartTimestamp());
}

terminate(remotePeer);
Expand Down

0 comments on commit 4cd433b

Please sign in to comment.