diff --git a/webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/core/WebRTCClient.java b/webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/core/WebRTCClient.java index ee0e9e80..342e3b59 100644 --- a/webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/core/WebRTCClient.java +++ b/webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/core/WebRTCClient.java @@ -523,7 +523,7 @@ public void onCreateSuccess(final SessionDescription desc) { final SessionDescription newDesc = new SessionDescription(desc.type, sdp); PeerInfo peerInfo = getPeerInfoFor(streamId); peerInfo.setLocalDescription(newDesc); - executor.execute(() -> { + handler.postAtFrontOfQueue(() -> { PeerConnection pc = peerInfo.peerConnection; if (pc != null && !isError) { Log.d(TAG, "Set local SDP from " + desc.type); @@ -1987,7 +1987,7 @@ private VideoTrack createVideoTrack(VideoCapturer capturer) { return localVideoTrack; } - private void findVideoSender(String streamId) { + public void findVideoSender(String streamId) { PeerConnection pc = getPeerConnectionFor(streamId); if (pc != null) { for (RtpSender sender : pc.getSenders()) { diff --git a/webrtc-android-framework/src/test/java/io/antmedia/webrtcandroidframework/WebRTCClientTest.java b/webrtc-android-framework/src/test/java/io/antmedia/webrtcandroidframework/WebRTCClientTest.java index 3757b119..bef40ef2 100644 --- a/webrtc-android-framework/src/test/java/io/antmedia/webrtcandroidframework/WebRTCClientTest.java +++ b/webrtc-android-framework/src/test/java/io/antmedia/webrtcandroidframework/WebRTCClientTest.java @@ -61,6 +61,7 @@ import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; @@ -125,7 +126,6 @@ private Handler getMockHandler() { invocation.getArgumentAt(0, Runnable.class).run(); return null; }); - when(handler.postDelayed(any(Runnable.class), anyLong())).thenAnswer((Answer) invocation -> { Long delay = invocation.getArgumentAt(1, Long.class); Thread thread = new Thread(() -> { @@ -570,21 +570,30 @@ public void testPCObserver() { @Test public void testSDPObserver() { doNothing().when(wsHandler).disconnect(anyBoolean()); + Handler handler = getMockHandler(); + webRTCClient.setHandler(handler); + String streamId = "stream1"; + String fakeStreamId = "fakeStreamId"; WebRTCClient.SDPObserver sdpObserver = webRTCClient.getSdpObserver(streamId); assertNotNull(sdpObserver); + WebRTCClient.SDPObserver sdpObserver2 = webRTCClient.getSdpObserver(fakeStreamId); + assertNotNull(sdpObserver2); PeerConnection pc = mock(PeerConnection.class); webRTCClient.addPeerConnection(streamId, pc); + // webRTCClient.addPeerConnection(fakeStreamId, pc); SessionDescription sessionDescription = new SessionDescription(SessionDescription.Type.OFFER, "sdp"); sdpObserver.onCreateSuccess(sessionDescription); verify(pc, timeout(1000)).setLocalDescription(eq(sdpObserver), any()); + verify(pc, timeout(1000)).setLocalDescription(eq(sdpObserver), any()); { webRTCClient.setInitiator(true); sdpObserver.onSetSuccess(); + sdpObserver2.onSetSuccess(); verify(wsHandler, timeout(1000)).sendConfiguration(eq(streamId), any(), eq("offer")); } { @@ -595,7 +604,6 @@ public void testSDPObserver() { } sdpObserver.onCreateFailure("error"); sdpObserver.onSetFailure("error"); - verify(wsHandler, timeout(1000)).disconnect(true); } @@ -1140,6 +1148,78 @@ public void testGetBroadcastObject() { verify(listener, times(1)).onBroadcastObject(broadcast); } + @Test public void switchCameraTest(){ + Mockito.doNothing().when(webRTCClient).initializeRenderers(); + Mockito.doReturn(null).when(webRTCClient).createVideoCapturer(any()); + webRTCClient.initializeVideoCapturer(); + assertEquals(webRTCClient.getConfig().videoSource,IWebRTCClient.StreamSource.FRONT_CAMERA); + webRTCClient.switchCamera(); + assertEquals(webRTCClient.getConfig().videoSource,IWebRTCClient.StreamSource.REAR_CAMERA); + webRTCClient.switchCamera(); + assertEquals(webRTCClient.getConfig().videoSource,IWebRTCClient.StreamSource.FRONT_CAMERA); + + } + + @Test public void testChangeVideoSrc(){ + Mockito.doNothing().when(webRTCClient).initializeRenderers(); + Mockito.doReturn(null).when(webRTCClient).createVideoCapturer(any()); + webRTCClient.initializeVideoCapturer(); + + webRTCClient.changeVideoSource(webRTCClient.getConfig().videoSource); + Mockito.verify(webRTCClient, never()).changeVideoCapturer(any()); + + IWebRTCClient.StreamSource streamSource = IWebRTCClient.StreamSource.REAR_CAMERA; + webRTCClient.changeVideoSource(streamSource); + Mockito.verify(webRTCClient,times(2)).createVideoCapturer(any()); + + assertEquals(webRTCClient.getConfig().videoSource,streamSource); + } + @Test public void testFindVideoSender() throws NoSuchFieldException, IllegalAccessException { + String streamId = "stream1"; + PeerConnection pc = mock(PeerConnection.class); + webRTCClient.addPeerConnection(streamId, pc); + + VideoTrack track = mock(VideoTrack.class); + doReturn("video").when(track).kind(); + RtpSender sender = mock(RtpSender.class); + doReturn(track).when(sender).track(); + + ArrayList senderArray = new ArrayList<>(Arrays.asList(sender)) ; + doReturn(senderArray).when(pc).getSenders(); + + webRTCClient.findVideoSender(streamId); + + Field field = WebRTCClient.class.getDeclaredField("localVideoSender"); + field.setAccessible(true); + RtpSender localVideoSender = (RtpSender) field.get(webRTCClient); + + assertEquals(localVideoSender,sender); + } + @Test + public void testUnexpectedReconnection(){ + String streamId = "stream1"; + final Handler handler = mock(Handler.class); + webRTCClient.setHandler(handler); + when(handler.postAtFrontOfQueue(any(Runnable.class))).thenAnswer((Answer) invocation -> { + return null; + }); + PeerConnection pc = mock(PeerConnection.class); + webRTCClient.addPeerConnection(streamId, pc); + WebRTCClient.PeerInfo peerInfo = new WebRTCClient.PeerInfo(streamId, WebRTCClient.Mode.PUBLISH); + webRTCClient.peers.put(streamId, peerInfo); + peerInfo.peerConnection = pc; + String fakeSdp = ""; + webRTCClient.getSdpObserver(streamId).onCreateSuccess(new SessionDescription(SessionDescription.Type.OFFER, fakeSdp)); + verify(pc, timeout(3000).times(0)).setLocalDescription(any(),any()); + + when(handler.postAtFrontOfQueue(any(Runnable.class))).thenAnswer((Answer) invocation -> { + invocation.getArgumentAt(0, Runnable.class).run(); + return null; + }); + webRTCClient.getSdpObserver(streamId).onCreateSuccess(new SessionDescription(SessionDescription.Type.OFFER, fakeSdp)); + verify(pc, timeout(3000).times(1)).setLocalDescription(any(),any()); + webRTCClient.setHandler(handler); + } @Test public void testToggleAudioOfAllParticipants(){