From 4bcd06c235c5e74184a73dba52eeda1481f1e6e9 Mon Sep 17 00:00:00 2001 From: Juan Navarro Date: Wed, 18 May 2022 13:12:57 +0200 Subject: [PATCH] android: Only publish/receive video if setting SDP doesn't fail A bad SDP will be rejected by the WebRTC implementation, so no further publish/subscribe operations should be done on failure. This expands on the idea of https://github.com/OpenVidu/openvidu-tutorials/pull/174 and also simplifies implementations of CustomSdpObserver: there is no need to inherit its methods just for logging, as CustomSdpObserver already logs everything on its default implementation. --- .../openvidu_android/openvidu/Session.java | 56 ++++++------------- .../websocket/CustomWebSocket.java | 22 ++++---- 2 files changed, 26 insertions(+), 52 deletions(-) diff --git a/openvidu-android/app/src/main/java/io/openvidu/openvidu_android/openvidu/Session.java b/openvidu-android/app/src/main/java/io/openvidu/openvidu_android/openvidu/Session.java index 329fa79f..e20d31ed 100644 --- a/openvidu-android/app/src/main/java/io/openvidu/openvidu_android/openvidu/Session.java +++ b/openvidu-android/app/src/main/java/io/openvidu/openvidu_android/openvidu/Session.java @@ -185,58 +185,34 @@ public class Session { public void createOfferForPublishing(MediaConstraints constraints) { localParticipant.getPeerConnection().createOffer(new CustomSdpObserver("createOffer") { @Override - public void onCreateSuccess(SessionDescription sessionDescription) { - super.onCreateSuccess(sessionDescription); - Log.i("createOffer SUCCESS", sessionDescription.toString()); - - localParticipant.getPeerConnection().setLocalDescription( - new CustomSdpObserver("createOffer_setLocalDescription") { - @Override - public void onSetSuccess() { - super.onSetSuccess(); - websocket.publishVideo(sessionDescription); - } - - @Override - public void onSetFailure(String s) { - super.onCreateFailure(s); - Log.e("setLocalDescription ERROR", s); - } - }, - sessionDescription); + public void onCreateSuccess(SessionDescription sdp) { + super.onCreateSuccess(sdp); + Log.i("createOffer SUCCESS", sdp.toString()); + localParticipant.getPeerConnection().setLocalDescription(new CustomSdpObserver("createOffer_setLocalDescription") { + @Override + public void onSetSuccess() { + super.onSetSuccess(); + websocket.publishVideo(sdp); + } + }, sdp); } - - @Override - public void onCreateFailure(String s) { - Log.e("createOffer ERROR", s); - } - }, constraints); } public void createAnswerForSubscribing(RemoteParticipant remoteParticipant, String streamId, MediaConstraints constraints) { remoteParticipant.getPeerConnection().createAnswer(new CustomSdpObserver("createAnswerSubscribing") { @Override - public void onCreateSuccess(SessionDescription sessionDescription) { - super.onCreateSuccess(sessionDescription); - Log.i("createAnswer SUCCESS", sessionDescription.toString()); + public void onCreateSuccess(SessionDescription sdp) { + super.onCreateSuccess(sdp); + Log.i("createAnswer SUCCESS", sdp.toString()); remoteParticipant.getPeerConnection().setLocalDescription(new CustomSdpObserver("createAnswerSubscribing_setLocalDescription") { @Override public void onSetSuccess() { - websocket.receiveVideoFrom(sessionDescription, remoteParticipant, streamId); + super.onSetSuccess(); + websocket.receiveVideoFrom(sdp, remoteParticipant, streamId); } - @Override - public void onSetFailure(String s) { - Log.e("setRemoteDescription ER", s); - } - }, sessionDescription); + }, sdp); } - - @Override - public void onCreateFailure(String s) { - Log.e("createAnswer ERROR", s); - } - }, constraints); } diff --git a/openvidu-android/app/src/main/java/io/openvidu/openvidu_android/websocket/CustomWebSocket.java b/openvidu-android/app/src/main/java/io/openvidu/openvidu_android/websocket/CustomWebSocket.java index c3040685..1adb6188 100644 --- a/openvidu-android/app/src/main/java/io/openvidu/openvidu_android/websocket/CustomWebSocket.java +++ b/openvidu-android/app/src/main/java/io/openvidu/openvidu_android/websocket/CustomWebSocket.java @@ -174,12 +174,9 @@ public class CustomWebSocket extends AsyncTask impl remoteParticipant.getPeerConnection().setRemoteDescription(new CustomSdpObserver("prepareReceiveVideoFrom_setRemoteDescription") { @Override public void onSetSuccess() { + super.onSetSuccess(); subscriptionInitiatedFromServer(remoteParticipant, streamId); } - @Override - public void onSetFailure(String s) { - Log.i("setRemoteDescription ER", s); - } }, remoteSdpOffer); } else if (this.IDS_RECEIVEVIDEO.containsKey(rpcId)) { // Response to receiveVideoFrom @@ -402,14 +399,15 @@ public class CustomWebSocket extends AsyncTask impl remoteParticipant.getPeerConnection().createOffer(new CustomSdpObserver("remote offer sdp") { @Override - public void onCreateSuccess(SessionDescription sessionDescription) { - super.onCreateSuccess(sessionDescription); - remoteParticipant.getPeerConnection().setLocalDescription(new CustomSdpObserver("remoteSetLocalDesc"), sessionDescription); - receiveVideoFrom(sessionDescription, remoteParticipant, streamId); - } - @Override - public void onCreateFailure(String s) { - Log.e("createOffer error", s); + public void onCreateSuccess(SessionDescription sdp) { + super.onCreateSuccess(sdp); + remoteParticipant.getPeerConnection().setLocalDescription(new CustomSdpObserver("remoteSetLocalDesc") { + @Override + public void onSetSuccess() { + super.onSetSuccess(); + receiveVideoFrom(sdp, remoteParticipant, streamId); + } + }, sdp); } }, sdpConstraints); }