Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix unexpected re connection #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(() -> {
Expand Down Expand Up @@ -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"));
}
{
Expand All @@ -595,7 +604,6 @@ public void testSDPObserver() {
}
sdpObserver.onCreateFailure("error");
sdpObserver.onSetFailure("error");

verify(wsHandler, timeout(1000)).disconnect(true);
}

Expand Down Expand Up @@ -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<RtpSender> 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(){
Expand Down
Loading