android: do NOT mix addTrack() and addTransceiver() APIs

The PeerConnection API went through a transition from Track-based to
Transceiver-based API. Nowadays, it's a very common mistake to mix them
inconsistently, especially caused by Google- or StackOverflow-based
development, because examples online tend to be old and BAD.

Corresponds to equivalent code in OpenVidu-Browser:
https://github.com/OpenVidu/openvidu/blob/v2.20.0/openvidu-browser/src/OpenViduInternal/WebRtcPeer/WebRtcPeer.ts#L187-L248
This commit is contained in:
Juan Navarro 2021-11-05 15:45:22 +01:00
parent 8bed5d36c5
commit 0f64ad7a62
2 changed files with 15 additions and 16 deletions

View File

@ -90,6 +90,15 @@ public class Session {
}
});
if (localParticipant.getAudioTrack() != null) {
peerConnection.addTransceiver(localParticipant.getAudioTrack(),
new RtpTransceiver.RtpTransceiverInit(RtpTransceiver.RtpTransceiverDirection.SEND_ONLY));
}
if (localParticipant.getVideoTrack() != null) {
peerConnection.addTransceiver(localParticipant.getVideoTrack(),
new RtpTransceiver.RtpTransceiverInit(RtpTransceiver.RtpTransceiverDirection.SEND_ONLY));
}
return peerConnection;
}
@ -134,13 +143,10 @@ public class Session {
}
});
peerConnection.addTrack(localParticipant.getAudioTrack());//Add audio track to create transReceiver
peerConnection.addTrack(localParticipant.getVideoTrack());//Add video track to create transReceiver
for (RtpTransceiver transceiver : peerConnection.getTransceivers()) {
//We set both audio and video in receive only mode
transceiver.setDirection(RtpTransceiver.RtpTransceiverDirection.RECV_ONLY);
}
peerConnection.addTransceiver(MediaStreamTrack.MediaType.MEDIA_TYPE_AUDIO,
new RtpTransceiver.RtpTransceiverInit(RtpTransceiver.RtpTransceiverDirection.RECV_ONLY));
peerConnection.addTransceiver(MediaStreamTrack.MediaType.MEDIA_TYPE_VIDEO,
new RtpTransceiver.RtpTransceiverInit(RtpTransceiver.RtpTransceiverDirection.RECV_ONLY));
this.remoteParticipants.get(connectionId).setPeerConnection(peerConnection);
}

View File

@ -126,18 +126,11 @@ public class CustomWebSocket extends AsyncTask<SessionActivity, Void, Void> impl
PeerConnection localPeerConnection = session.createLocalPeerConnection();
localPeerConnection.addTrack(localParticipant.getAudioTrack());
localPeerConnection.addTrack(localParticipant.getVideoTrack());
for (RtpTransceiver transceiver : localPeerConnection.getTransceivers()) {
transceiver.setDirection(RtpTransceiver.RtpTransceiverDirection.SEND_ONLY);
}
localParticipant.setPeerConnection(localPeerConnection);
MediaConstraints sdpConstraints = new MediaConstraints();
sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("offerToReceiveAudio", "true"));
sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("offerToReceiveVideo", "true"));
sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("offerToReceiveAudio", "false"));
sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("offerToReceiveVideo", "false"));
session.createOfferForPublishing(sdpConstraints);
if (result.getJSONArray(JsonConstants.VALUE).length() > 0) {