55import android .os .Bundle ;
66import android .os .Handler ;
77import android .os .HandlerThread ;
8+ import android .text .TextUtils ;
89import android .util .DisplayMetrics ;
910import android .view .Gravity ;
1011import android .view .View ;
4445
4546import static com .piasy .kmp .webrtc .AndroidPeerConnectionClientFactoryKt .createPeerConnectionClientFactory ;
4647import static com .piasy .kmp .webrtc .AndroidPeerConnectionClientFactoryKt .initializeWebRTC ;
47- import static com .piasy .kmp .webrtc .android .HallActivity .EXTRA_AUDIO_ONLY ;
48- import static com .piasy .kmp .webrtc .android .HallActivity .EXTRA_IS_PUBLISHER ;
49- import static com .piasy .kmp .webrtc .android .HallActivity .EXTRA_RECORD_CALL ;
50- import static com .piasy .kmp .webrtc .android .HallActivity .EXTRA_VIDEO_BITRATE ;
51- import static com .piasy .kmp .webrtc .android .HallActivity .EXTRA_VIDEO_CODEC ;
52- import static com .piasy .kmp .webrtc .android .HallActivity .EXTRA_VIDEO_FPS ;
53- import static com .piasy .kmp .webrtc .android .HallActivity .EXTRA_VIDEO_HEIGHT ;
54- import static com .piasy .kmp .webrtc .android .HallActivity .EXTRA_VIDEO_WIDTH ;
48+ import static com .piasy .kmp .webrtc .android .HallActivity .*;
5549
5650public class CallActivity extends AppCompatActivity implements PeerConnectionClientCallback , AudioMixer .MixerCallback {
5751
@@ -61,6 +55,7 @@ public class CallActivity extends AppCompatActivity implements PeerConnectionCli
6155 private static final boolean PAUSE_STREAMING = true ;
6256
6357 private boolean isPublisher ;
58+ private boolean singlePc ;
6459 private boolean audioOnly ;
6560 private int videoWidth ;
6661 private int videoHeight ;
@@ -87,6 +82,7 @@ public class CallActivity extends AppCompatActivity implements PeerConnectionCli
8782 private EglBase eglBase ;
8883 private PeerConnectionClientFactory pcClientFactory ;
8984 private PeerConnectionClient pcClient ;
85+ private PeerConnectionClient recvPcClient ;
9086 private AudioMixer mixer ;
9187
9288 private static int getSystemUiVisibility () {
@@ -117,6 +113,7 @@ protected void onCreate(Bundle savedInstanceState) {
117113
118114 isPublisher = intent .getBooleanExtra (EXTRA_IS_PUBLISHER , true );
119115 recording = intent .getBooleanExtra (EXTRA_RECORD_CALL , false );
116+ singlePc = intent .getBooleanExtra (EXTRA_SINGLE_PC , false );
120117 audioOnly = intent .getBooleanExtra (EXTRA_AUDIO_ONLY , false );
121118
122119 videoWidth = intent .getIntExtra (EXTRA_VIDEO_WIDTH , 640 );
@@ -300,11 +297,23 @@ public Unit invoke(Integer code, String msg) {
300297
301298 // 5. create PcClient
302299 pcClient = pcClientFactory .createPeerConnectionClient (
303- "test" , PeerConnectionClient .DIR_SEND_RECV , true , videoMaxBitrate , videoFps , this
300+ "test" ,
301+ singlePc ? PeerConnectionClient .DIR_SEND_RECV : PeerConnectionClient .DIR_SEND_ONLY ,
302+ true , videoMaxBitrate , videoFps , this
304303 );
304+ if (!singlePc ) {
305+ recvPcClient = pcClientFactory .createPeerConnectionClient (
306+ "test-recv" ,
307+ PeerConnectionClient .DIR_RECV_ONLY ,
308+ true , videoMaxBitrate , videoFps , this
309+ );
310+ }
305311
306312 // 6. create pc
307313 pcClient .createPeerConnection (Collections .emptyList ());
314+ if (recvPcClient != null ) {
315+ recvPcClient .createPeerConnection (Collections .emptyList ());
316+ }
308317
309318 // 7. create offer
310319 pcClient .createOffer ();
@@ -313,14 +322,28 @@ public Unit invoke(Integer code, String msg) {
313322 @ Override
314323 public void onLocalDescription (@ NotNull String peerUid , @ NotNull SessionDescription sdp ) {
315324 // 8. send offer to remote, get answer from remote, and set answer
316- SessionDescription answer = new SessionDescription (SessionDescription .ANSWER , sdp .getSdpDescription ());
317- pcClient .setRemoteDescription (answer );
325+ if (singlePc || TextUtils .equals (peerUid , "test-recv" )) {
326+ SessionDescription answer = new SessionDescription (SessionDescription .ANSWER , sdp .getSdpDescription ());
327+ pcClient .setRemoteDescription (answer );
328+ } else if (TextUtils .equals (peerUid , "test" )) {
329+ SessionDescription offer = new SessionDescription (SessionDescription .OFFER , sdp .getSdpDescription ());
330+ recvPcClient .setRemoteDescription (offer );
331+ recvPcClient .createAnswer ();
332+ }
333+ }
334+
335+ @ Override
336+ public void onSetRemoteSdpResult (@ NotNull String peerUid , boolean success ) {
318337 }
319338
320339 @ Override
321340 public void onIceCandidate (@ NotNull String peerUid , @ NotNull IceCandidate candidate ) {
322341 // 9. send ice candidate to remote, get ice candidate from remote, add ice candidate
323- pcClient .addIceCandidate (candidate );
342+ if (singlePc || TextUtils .equals (peerUid , "test-recv" )) {
343+ pcClient .addIceCandidate (candidate );
344+ } else if (TextUtils .equals (peerUid , "test" )) {
345+ recvPcClient .addIceCandidate (candidate );
346+ }
324347 }
325348
326349 @ Override
@@ -329,7 +352,11 @@ public void onIceConnected(@NotNull String peerUid) {
329352 @ Override
330353 public void run () {
331354 // 10. on ice connected, add renderer for remote stream
332- pcClient .addRemoteTrackRenderer (remoteRenderer );
355+ if (singlePc ) {
356+ pcClient .addRemoteTrackRenderer (remoteRenderer );
357+ } else {
358+ recvPcClient .addRemoteTrackRenderer (remoteRenderer );
359+ }
333360 }
334361 });
335362 }
@@ -343,6 +370,10 @@ private void disconnect() {
343370 if (pcClientFactory != null ) {
344371 pcClientFactory .stopVideoCapture ();
345372 pcClient .close ();
373+ if (recvPcClient != null ) {
374+ recvPcClient .close ();
375+ recvPcClient = null ;
376+ }
346377 pcClientFactory .destroyPeerConnectionFactory ();
347378 pcClientFactory = null ;
348379 pcClient = null ;
0 commit comments