瀏覽代碼

修改直播远端用户推流回调逻辑

Pq 3 年之前
父節點
當前提交
283d888955

+ 54 - 5
student/src/main/java/com/cooleshow/student/presenter/live/LiveRoomPresenter.java

@@ -6,9 +6,12 @@ import android.util.Log;
 import com.cooleshow.base.data.net.BaseResponse;
 import com.cooleshow.base.presenter.BasePresenter;
 import com.cooleshow.base.rx.BaseObserver;
+import com.cooleshow.base.utils.LogUtils;
 import com.cooleshow.base.utils.RequestBodyUtil;
 import com.cooleshow.student.api.APIService;
 import com.cooleshow.student.bean.FriendInfoBean;
+import com.daya.live_teaching.common.ErrorCode;
+import com.daya.live_teaching.common.ResultCallback;
 import com.rong.io.live.bean.ImUserState;
 import com.cooleshow.student.bean.LiveRoomInfoBean;
 import com.cooleshow.student.contract.LiveRoomContract;
@@ -86,8 +89,22 @@ public class LiveRoomPresenter extends BasePresenter<LiveRoomContract.view> impl
         @Override
         public void onRemoteUserPublishResource(RCRTCRemoteUser rcrtcRemoteUser, List<RCRTCInputStream> list) {
             Log.i("pq", "onRemoteUserPublishResource:");
-            if (getView() != null) {
-                getView().onRemoteUserPublishResource(rcrtcRemoteUser.getUserId());
+            if (currentRoomInfo != null && TextUtils.equals(currentRoomInfo.speakerId, rcrtcRemoteUser.getUserId())) {
+                subscribeAVStream();
+            } else {
+                subscribeTargetStream(rcrtcRemoteUser.getUserId(), list, new ResultCallback<String>() {
+                    @Override
+                    public void onSuccess(String s) {
+                        if (getView() != null) {
+                            getView().onRemoteUserPublishResource(rcrtcRemoteUser.getUserId());
+                        }
+                    }
+
+                    @Override
+                    public void onFail(int errorCode, String errorStr) {
+                        LogUtils.i("pq","subscribeTargetStream fail:errorCode:"+errorCode+"errorStr:"+errorStr);
+                    }
+                });
             }
         }
 
@@ -175,7 +192,7 @@ public class LiveRoomPresenter extends BasePresenter<LiveRoomContract.view> impl
         if (currentConnectionStatus == RongIMClient.ConnectionStatusListener.ConnectionStatus.CONNECTED) {
             Log.i("pq", "LiveRoomActivity init getRoomInfo");
             getRoomInfo(roomId);
-        }else {
+        } else {
             connectIM();
         }
 
@@ -453,10 +470,10 @@ public class LiveRoomPresenter extends BasePresenter<LiveRoomContract.view> impl
     private void joinRoom(String roomId, boolean isCreate) {
         //如果是观众就直接加入房间
         Log.i("pq", "joinRoom");
-        LiveEventHelper.getInstance().joinRoom(roomId,RCRTCLiveRole.AUDIENCE,new ClickCallback<Boolean>() {
+        LiveEventHelper.getInstance().joinRoom(roomId, RCRTCLiveRole.AUDIENCE, new ClickCallback<Boolean>() {
             @Override
             public void onResult(Boolean result, String msg) {
-                Log.i("pq", "joinRoom onResult:" + result+"->"+msg);
+                Log.i("pq", "joinRoom onResult:" + result + "->" + msg);
                 if (result) {
                     setCurrentRoom(currentRoomInfo, isCreate);
                     refreshMusicView(true);
@@ -982,6 +999,38 @@ public class LiveRoomPresenter extends BasePresenter<LiveRoomContract.view> impl
         }
     }
 
+    /**
+     * 订阅指定的流
+     *
+     * @param userId
+     * @param list
+     * @param callback
+     */
+    public void subscribeTargetStream(final String userId, List<RCRTCInputStream> list, final ResultCallback<String> callback) {
+        RCRTCRoom rcrtcRoom = LiveEventHelper.getInstance().getRtcRoom();
+        if (rcrtcRoom == null) {
+            Log.i("pq", "unSubscribeStream failed , rcrtcRoom is null");
+            if (callback != null) {
+                callback.onFail(ErrorCode.RTC_ERROR.getCode(), null);
+            }
+            return;
+        }
+        rcrtcRoom.getLocalUser().subscribeStreams(list, new IRCRTCResultCallback() {
+            @Override
+            public void onSuccess() {
+                if (callback != null) {
+                    callback.onSuccess("");
+                }
+            }
+
+            @Override
+            public void onFailed(RTCErrorCode errorCode) {
+                if (callback != null) {
+                    callback.onFail(errorCode.getValue(), errorCode.getReason());
+                }
+            }
+        });
+    }
 
     /**
      * 主动订阅远端用户发布的流

+ 1 - 7
student/src/main/java/com/cooleshow/student/ui/live/LiveRoomActivity.java

@@ -1177,13 +1177,7 @@ public class LiveRoomActivity extends BaseMVPActivity<ActivityLiveroomLayoutBind
     public void onRemoteUserPublishResource(String remoteUserId) {
         if (!TextUtils.isEmpty(remoteUserId)) {
             Log.i("pq", "收到onRemoteUserPublishResource:" + remoteUserId);
-            if (mRoomInfoBean != null && TextUtils.equals(mRoomInfoBean.speakerId, remoteUserId)) {
-                if (presenter != null) {
-                    presenter.subscribeAVStream();
-                }
-            } else {
-                refreshAudio(null, null);
-            }
+            refreshAudio(null, null);
         }
     }
 

+ 19 - 3
teacher/src/main/java/com/cooleshow/teacher/presenter/live/LiveRoomPresenter.java

@@ -6,6 +6,7 @@ import android.util.Log;
 import com.cooleshow.base.data.net.BaseResponse;
 import com.cooleshow.base.presenter.BasePresenter;
 import com.cooleshow.base.rx.BaseObserver;
+import com.cooleshow.base.utils.LogUtils;
 import com.cooleshow.base.utils.RequestBodyUtil;
 import com.cooleshow.base.utils.Utils;
 import com.cooleshow.teacher.api.APIService;
@@ -86,7 +87,19 @@ public class LiveRoomPresenter extends BasePresenter<LiveRoomContract.LiveRoomVi
         @Override
         public void onRemoteUserPublishResource(RCRTCRemoteUser rcrtcRemoteUser, List<RCRTCInputStream> list) {
             Log.i("pq", "onRemoteUserPublishResource");
-            subscribeTargetStream(rcrtcRemoteUser.getUserId(), list, null);
+            subscribeTargetStream(rcrtcRemoteUser.getUserId(), list, new ResultCallback<String>() {
+                @Override
+                public void onSuccess(String s) {
+                    if (getView() != null) {
+                        getView().onRemoteUserPublishResource(rcrtcRemoteUser.getUserId());
+                    }
+                }
+
+                @Override
+                public void onFail(int errorCode, String errorStr) {
+                    LogUtils.i("pq", "subscribeTargetStream fail:errorCode:" + errorCode + "errorStr:" + errorStr);
+                }
+            });
         }
 
         @Override
@@ -843,13 +856,16 @@ public class LiveRoomPresenter extends BasePresenter<LiveRoomContract.LiveRoomVi
         rcrtcRoom.getLocalUser().subscribeStreams(list, new IRCRTCResultCallback() {
             @Override
             public void onSuccess() {
-                if (getView() != null) {
-                    getView().onRemoteUserPublishResource(userId);
+                if (callback != null) {
+                    callback.onSuccess("");
                 }
             }
 
             @Override
             public void onFailed(RTCErrorCode errorCode) {
+                if (callback != null) {
+                    callback.onFail(errorCode.getValue(), errorCode.getReason());
+                }
             }
         });
     }