From d376767837ff3a8062d0c0a9a136ab46d7c3a7ff Mon Sep 17 00:00:00 2001 From: lastpeony Date: Wed, 2 Oct 2024 00:56:07 +0300 Subject: [PATCH 1/4] fix p2p if createLocalVideoTrack false --- .../io/antmedia/webrtcandroidframework/core/WebRTCClient.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 54812e27..6b685ffc 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 @@ -1463,6 +1463,7 @@ public void onConnected(String streamId) { } }); } + streamStoppedByUser = false; } public void onPeerConnectionClosed() { @@ -2050,7 +2051,7 @@ public void createPeerConnectionInternal(String streamId, boolean createLocalTra setWebRTCLogLevel(); - if(createLocalTrack){ + if(createLocalTrack || peer.mode == Mode.P2P){ List mediaStreamLabels = Collections.singletonList("ARDAMS"); try{ From aef95cfd9f660b7cd7756634a6b64932f0513bb1 Mon Sep 17 00:00:00 2001 From: lastpeony Date: Wed, 2 Oct 2024 11:06:06 +0300 Subject: [PATCH 2/4] improve test stability and coverage --- ...ActivityWithDifferentVideoSourcesTest.java | 26 ++++++++++++- .../PeerActivityTest.java | 39 ++++++++++++++++--- .../TestableActivity.java | 6 +-- 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/webrtc-android-sample-app/src/androidTest/java/io/antmedia/webrtc_android_sample_app/ConferenceActivityWithDifferentVideoSourcesTest.java b/webrtc-android-sample-app/src/androidTest/java/io/antmedia/webrtc_android_sample_app/ConferenceActivityWithDifferentVideoSourcesTest.java index 273bf60e..8af7b2b8 100644 --- a/webrtc-android-sample-app/src/androidTest/java/io/antmedia/webrtc_android_sample_app/ConferenceActivityWithDifferentVideoSourcesTest.java +++ b/webrtc-android-sample-app/src/androidTest/java/io/antmedia/webrtc_android_sample_app/ConferenceActivityWithDifferentVideoSourcesTest.java @@ -13,6 +13,7 @@ import android.content.Intent; import android.util.Log; +import android.view.View; import android.widget.TextView; import android.content.Context; @@ -25,6 +26,9 @@ import androidx.test.espresso.IdlingRegistry; import androidx.test.espresso.IdlingResource; +import androidx.test.espresso.UiController; +import androidx.test.espresso.ViewAction; +import androidx.test.espresso.matcher.ViewMatchers; import androidx.test.ext.junit.rules.ActivityScenarioRule; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.rule.GrantPermissionRule; @@ -34,6 +38,7 @@ import androidx.test.uiautomator.Until; +import org.hamcrest.Matcher; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -231,7 +236,7 @@ public void testConferenceSwitchStreamSource() throws InterruptedException { onView(withId(R.id. stats_popup_container)).perform(swipeUp()); - //Thread.sleep(3000); + onView(withId(R.id.stats_popup_container)).perform(waitFor(2000)); onView(withId(R.id.multitrack_stats_popup_close_button)).perform(click()); @@ -281,4 +286,23 @@ public void testConferenceSwitchStreamSource() throws InterruptedException { .check(matches(withText(R.string.disconnected))); } + public static ViewAction waitFor(long millis) { + return new ViewAction() { + @Override + public Matcher getConstraints() { + return ViewMatchers.isDisplayed(); // No constraints, can be used on any view + } + + @Override + public String getDescription() { + return "Wait for " + millis + " milliseconds."; + } + + @Override + public void perform(UiController uiController, View view) { + uiController.loopMainThreadForAtLeast(millis); + } + }; + } + } diff --git a/webrtc-android-sample-app/src/androidTest/java/io/antmedia/webrtc_android_sample_app/PeerActivityTest.java b/webrtc-android-sample-app/src/androidTest/java/io/antmedia/webrtc_android_sample_app/PeerActivityTest.java index 1da5d5d9..b81507ae 100644 --- a/webrtc-android-sample-app/src/androidTest/java/io/antmedia/webrtc_android_sample_app/PeerActivityTest.java +++ b/webrtc-android-sample-app/src/androidTest/java/io/antmedia/webrtc_android_sample_app/PeerActivityTest.java @@ -32,6 +32,7 @@ import androidx.test.rule.GrantPermissionRule; import androidx.test.uiautomator.UiDevice; +import org.apache.commons.lang3.RandomStringUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -54,6 +55,8 @@ public class PeerActivityTest { private IdlingResource mIdlingResource; private String runningTest; + private float videoBytesSent = 0; + private float videoBytesReceived = 0; @Rule public GrantPermissionRule permissionRule @@ -111,7 +114,8 @@ public void testPeerToPeer() throws InterruptedException { onView(withId(R.id.start_streaming_button)).check(matches(withText("Join"))); Espresso.closeSoftKeyboard(); - onView(withId(R.id.stream_id_edittext)).perform(replaceText(PeerActivity.PEER_ROOM_ID_FOR_TEST)); + String randomPeerRoomId = "p2p"+ RandomStringUtils.randomAlphanumeric(6); + onView(withId(R.id.stream_id_edittext)).perform(replaceText(randomPeerRoomId)); onView(withId(R.id.start_streaming_button)).perform(click()); @@ -119,8 +123,7 @@ public void testPeerToPeer() throws InterruptedException { onView(withId(R.id.broadcasting_text_view)) .check(matches(anyOf(withText(R.string.connecting), withText(R.string.live)))); - - RemoteP2PParticipant remoteP2PParticipant = RemoteP2PParticipant.addP2PParticipant(PeerActivity.PEER_ROOM_ID_FOR_TEST, runningTest); + RemoteP2PParticipant remoteP2PParticipant = RemoteP2PParticipant.addP2PParticipant(randomPeerRoomId, runningTest); Thread.sleep(10000); @@ -172,10 +175,11 @@ public void testPeerToPeerReconnection() throws InterruptedException, IOExceptio IdlingRegistry.getInstance().register(mIdlingResource); activity.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); }); + String randomPeerRoomId = "p2p"+ RandomStringUtils.randomAlphanumeric(6); onView(withId(R.id.start_streaming_button)).check(matches(withText("Join"))); Espresso.closeSoftKeyboard(); - onView(withId(R.id.stream_id_edittext)).perform(replaceText(PeerActivity.PEER_ROOM_ID_FOR_TEST)); + onView(withId(R.id.stream_id_edittext)).perform(replaceText(randomPeerRoomId)); onView(withId(R.id.start_streaming_button)).perform(click()); @@ -184,7 +188,7 @@ public void testPeerToPeerReconnection() throws InterruptedException, IOExceptio onView(withId(R.id.broadcasting_text_view)) .check(matches(anyOf(withText(R.string.connecting), withText(R.string.live)))); - RemoteP2PParticipant remoteP2PParticipant = RemoteP2PParticipant.addP2PParticipant(PeerActivity.PEER_ROOM_ID_FOR_TEST, runningTest); + RemoteP2PParticipant remoteP2PParticipant = RemoteP2PParticipant.addP2PParticipant(randomPeerRoomId, runningTest); Thread.sleep(10000); @@ -198,6 +202,15 @@ public void testPeerToPeerReconnection() throws InterruptedException, IOExceptio onView(withId(R.id.multitrack_stats_popup_play_stats_video_track_recyclerview)).inRoot(isDialog()).check(matches(isDisplayed())); + + onView(withId(R.id.multitrack_stats_popup_bytes_sent_video_textview)).check((view, noViewFoundException) -> { + String text = ((TextView) view).getText().toString(); + float value = Float.parseFloat(text); + assertTrue(value > 0f); + videoBytesSent = value; + }); + + onView(withId(R.id.multitrack_stats_popup_play_stats_video_track_recyclerview)) .check((view, noViewFoundException) -> { if (noViewFoundException != null) { @@ -206,8 +219,11 @@ public void testPeerToPeerReconnection() throws InterruptedException, IOExceptio RecyclerView recyclerView = (RecyclerView) view; RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(0); TextView bytesReceivedText = viewHolder.itemView.findViewById(R.id.track_stats_item_bytes_received_textview); + int bytesReceived = Integer.parseInt(( bytesReceivedText).getText().toString()); + assertTrue(bytesReceived > 0); + videoBytesReceived = bytesReceived; }); onView(withId(R.id. stats_popup_container)).perform(swipeUp()); @@ -240,6 +256,16 @@ public void testPeerToPeerReconnection() throws InterruptedException, IOExceptio onView(withId(R.id.multitrack_stats_popup_play_stats_video_track_recyclerview)).inRoot(isDialog()).check(matches(isDisplayed())); + + onView(withId(R.id.multitrack_stats_popup_bytes_sent_video_textview)).check((view, noViewFoundException) -> { + String text = ((TextView) view).getText().toString(); + float value = Float.parseFloat(text); + assertTrue(value > 0f); + assertTrue(value > videoBytesSent); + videoBytesSent = value; + }); + + onView(withId(R.id.multitrack_stats_popup_play_stats_video_track_recyclerview)) .check((view, noViewFoundException) -> { if (noViewFoundException != null) { @@ -248,8 +274,11 @@ public void testPeerToPeerReconnection() throws InterruptedException, IOExceptio RecyclerView recyclerView = (RecyclerView) view; RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(0); TextView bytesReceivedText = viewHolder.itemView.findViewById(R.id.track_stats_item_bytes_received_textview); + int bytesReceived = Integer.parseInt(( bytesReceivedText).getText().toString()); + assertTrue(bytesReceived > 0); + assertTrue(bytesReceived > videoBytesReceived); }); onView(withId(R.id. stats_popup_container)).perform(swipeUp()); diff --git a/webrtc-android-sample-app/src/main/java/io/antmedia/webrtc_android_sample_app/TestableActivity.java b/webrtc-android-sample-app/src/main/java/io/antmedia/webrtc_android_sample_app/TestableActivity.java index c4121b24..c1789d8e 100644 --- a/webrtc-android-sample-app/src/main/java/io/antmedia/webrtc_android_sample_app/TestableActivity.java +++ b/webrtc-android-sample-app/src/main/java/io/antmedia/webrtc_android_sample_app/TestableActivity.java @@ -1,6 +1,5 @@ package io.antmedia.webrtc_android_sample_app; -import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.Window; @@ -12,11 +11,12 @@ import androidx.test.espresso.IdlingResource; import androidx.test.espresso.idling.CountingIdlingResource; +import org.apache.commons.lang3.RandomStringUtils; + public abstract class TestableActivity extends AppCompatActivity { public CountingIdlingResource idlingResource = new CountingIdlingResource("Load", true); protected SharedPreferences sharedPreferences; - public static String CONFERENCE_ROOM_ID_FOR_TEST = "room_12877"; - public static String PEER_ROOM_ID_FOR_TEST = "p2pRoom_12877"; + public static String PEER_ROOM_ID_FOR_TEST = "p2p"+ RandomStringUtils.randomAlphanumeric(3); public void incrementIdle() { idlingResource.increment(); From 72c7c8540deff737c3c43b8c25ac9f3afbfb468d Mon Sep 17 00:00:00 2001 From: lastpeony Date: Tue, 8 Oct 2024 11:27:51 +0300 Subject: [PATCH 3/4] refactor --- .../webrtcandroidframework/core/WebRTCClient.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 6b685ffc..bcd55888 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 @@ -1489,9 +1489,11 @@ public boolean isStreaming(String streamId) { public void onTakeConfiguration(String streamId, SessionDescription sdp) { this.handler.post(() -> { if (sdp.type == SessionDescription.Type.OFFER) { - PeerConnection pc = getPeerConnectionFor(streamId); + PeerInfo peerInfo = getPeerInfoFor(streamId); + PeerConnection pc = peerInfo.peerConnection; if (pc == null) { - createPeerConnection(streamId, false); + boolean createLocalTrack = peerInfo.mode == Mode.P2P; + createPeerConnection(streamId, createLocalTrack); } setRemoteDescription(streamId, sdp); @@ -2051,7 +2053,7 @@ public void createPeerConnectionInternal(String streamId, boolean createLocalTra setWebRTCLogLevel(); - if(createLocalTrack || peer.mode == Mode.P2P){ + if(createLocalTrack){ List mediaStreamLabels = Collections.singletonList("ARDAMS"); try{ From fc1c454ba0aab2253d6a551e8d4bd926a23ff101 Mon Sep 17 00:00:00 2001 From: lastpeony Date: Tue, 8 Oct 2024 11:34:26 +0300 Subject: [PATCH 4/4] small refactor --- .../webrtcandroidframework/core/WebRTCClient.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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 bcd55888..b9ba08e0 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 @@ -1490,10 +1490,12 @@ public void onTakeConfiguration(String streamId, SessionDescription sdp) { this.handler.post(() -> { if (sdp.type == SessionDescription.Type.OFFER) { PeerInfo peerInfo = getPeerInfoFor(streamId); - PeerConnection pc = peerInfo.peerConnection; - if (pc == null) { - boolean createLocalTrack = peerInfo.mode == Mode.P2P; - createPeerConnection(streamId, createLocalTrack); + if(peerInfo != null){ + PeerConnection pc = peerInfo.peerConnection; + if (pc == null) { + boolean createLocalTrack = peerInfo.mode == Mode.P2P; + createPeerConnection(streamId, createLocalTrack); + } } setRemoteDescription(streamId, sdp);