1414
1515import androidx .annotation .NonNull ;
1616import androidx .annotation .Nullable ;
17+ import androidx .appcompat .widget .AppCompatTextView ;
1718
1819import com .yanzhenjie .permission .AndPermission ;
1920import com .yanzhenjie .permission .runtime .Permission ;
2021
22+ import java .util .HashMap ;
23+ import java .util .Map ;
24+ import java .util .concurrent .ConcurrentHashMap ;
25+
2126import io .agora .api .example .MainApplication ;
2227import io .agora .api .example .R ;
2328import io .agora .api .example .annotation .Example ;
2429import io .agora .api .example .common .BaseFragment ;
30+ import io .agora .api .example .common .model .StatisticsInfo ;
2531import io .agora .api .example .utils .CommonUtil ;
2632import io .agora .rtc .Constants ;
2733import io .agora .rtc .IRtcEngineEventHandler ;
@@ -48,12 +54,15 @@ public class JoinChannelVideo extends BaseFragment implements View.OnClickListen
4854{
4955 private static final String TAG = JoinChannelVideo .class .getSimpleName ();
5056
51- private FrameLayout fl_local , fl_remote ;
57+ private FrameLayout fl_local , fl_remote , fl_remote_2 , fl_remote_3 , fl_remote_4 , fl_remote_5 ;
5258 private Button join ;
5359 private EditText et_channel ;
54- private RtcEngine engine ;
60+ private io . agora . rtc . RtcEngine engine ;
5561 private int myUid ;
5662 private boolean joined = false ;
63+ private Map <Integer , ViewGroup > remoteViews = new ConcurrentHashMap <Integer , ViewGroup >();
64+ private AppCompatTextView localStats , remoteStats ;
65+ private StatisticsInfo statisticsInfo ;
5766
5867 @ Nullable
5968 @ Override
@@ -70,8 +79,25 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
7079 join = view .findViewById (R .id .btn_join );
7180 et_channel = view .findViewById (R .id .et_channel );
7281 view .findViewById (R .id .btn_join ).setOnClickListener (this );
73- fl_local = view .findViewById (R .id .fl_local );
74- fl_remote = view .findViewById (R .id .fl_remote );
82+ fl_local = view .findViewById (R .id .fl_local_video );
83+ fl_remote = view .findViewById (R .id .fl_remote_video );
84+ fl_remote_2 = view .findViewById (R .id .fl_remote2 );
85+ fl_remote_3 = view .findViewById (R .id .fl_remote3 );
86+ fl_remote_4 = view .findViewById (R .id .fl_remote4 );
87+ fl_remote_5 = view .findViewById (R .id .fl_remote5 );
88+ localStats = view .findViewById (R .id .local_stats );
89+ localStats .bringToFront ();
90+ remoteStats = view .findViewById (R .id .remote_stats );
91+ remoteStats .bringToFront ();
92+ statisticsInfo = new StatisticsInfo ();
93+ }
94+
95+ private void updateLocalStats (){
96+ localStats .setText (statisticsInfo .getLocalVideoStats ());
97+ }
98+
99+ private void updateRemoteStats (){
100+ remoteStats .setText (statisticsInfo .getRemoteVideoStats ());
75101 }
76102
77103 @ Override
@@ -391,23 +417,25 @@ public void onUserJoined(int uid, int elapsed)
391417 if (context == null ) {
392418 return ;
393419 }
394- handler . post (() ->
395- {
396- /**Display remote video stream*/
397- SurfaceView surfaceView = null ;
398- if ( fl_remote . getChildCount () > 0 )
420+ if ( remoteViews . containsKey ( uid )){
421+ return ;
422+ }
423+ else {
424+ handler . post (() ->
399425 {
400- fl_remote .removeAllViews ();
401- }
402- // Create render view by RtcEngine
403- surfaceView = RtcEngine .CreateRendererView (context );
404- surfaceView .setZOrderMediaOverlay (true );
405- // Add to the remote container
406- fl_remote .addView (surfaceView , new FrameLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
407-
408- // Setup remote video to render
409- engine .setupRemoteVideo (new VideoCanvas (surfaceView , RENDER_MODE_HIDDEN , uid ));
410- });
426+ /**Display remote video stream*/
427+ SurfaceView surfaceView = null ;
428+ // Create render view by RtcEngine
429+ surfaceView = RtcEngine .CreateRendererView (context );
430+ surfaceView .setZOrderMediaOverlay (true );
431+ ViewGroup view = getAvailableView ();
432+ remoteViews .put (uid , view );
433+ // Add to the remote container
434+ view .addView (surfaceView , new FrameLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
435+ // Setup remote video to render
436+ engine .setupRemoteVideo (new VideoCanvas (surfaceView , RENDER_MODE_HIDDEN , uid ));
437+ });
438+ }
411439 }
412440
413441 /**Occurs when a remote user (Communication)/host (Live Broadcast) leaves the channel.
@@ -432,8 +460,60 @@ public void run() {
432460 Note: The video will stay at its last frame, to completely remove it you will need to
433461 remove the SurfaceView from its parent*/
434462 engine .setupRemoteVideo (new VideoCanvas (null , RENDER_MODE_HIDDEN , uid ));
463+ remoteViews .get (uid ).removeAllViews ();
464+ remoteViews .remove (uid );
435465 }
436466 });
437467 }
468+
469+ @ Override
470+ public void onRemoteAudioStats (io .agora .rtc .IRtcEngineEventHandler .RemoteAudioStats remoteAudioStats ) {
471+ statisticsInfo .setRemoteAudioStats (remoteAudioStats );
472+ updateRemoteStats ();
473+ }
474+
475+ @ Override
476+ public void onLocalAudioStats (io .agora .rtc .IRtcEngineEventHandler .LocalAudioStats localAudioStats ) {
477+ statisticsInfo .setLocalAudioStats (localAudioStats );
478+ updateLocalStats ();
479+ }
480+
481+ @ Override
482+ public void onRemoteVideoStats (io .agora .rtc .IRtcEngineEventHandler .RemoteVideoStats remoteVideoStats ) {
483+ statisticsInfo .setRemoteVideoStats (remoteVideoStats );
484+ updateRemoteStats ();
485+ }
486+
487+ @ Override
488+ public void onLocalVideoStats (io .agora .rtc .IRtcEngineEventHandler .LocalVideoStats localVideoStats ) {
489+ statisticsInfo .setLocalVideoStats (localVideoStats );
490+ updateLocalStats ();
491+ }
492+
493+ @ Override
494+ public void onRtcStats (io .agora .rtc .IRtcEngineEventHandler .RtcStats rtcStats ) {
495+ statisticsInfo .setRtcStats (rtcStats );
496+ }
438497 };
498+
499+ private ViewGroup getAvailableView () {
500+ if (fl_remote .getChildCount () == 0 ){
501+ return fl_remote ;
502+ }
503+ else if (fl_remote_2 .getChildCount () == 0 ){
504+ return fl_remote_2 ;
505+ }
506+ else if (fl_remote_3 .getChildCount () == 0 ){
507+ return fl_remote_3 ;
508+ }
509+ else if (fl_remote_4 .getChildCount () == 0 ){
510+ return fl_remote_4 ;
511+ }
512+ else if (fl_remote_5 .getChildCount () == 0 ){
513+ return fl_remote_5 ;
514+ }
515+ else {
516+ return fl_remote ;
517+ }
518+ }
439519}
0 commit comments