update 随机PK RTC控制器新增超时管理

This commit is contained in:
zlzw 2022-12-10 15:01:24 +08:00
parent ae020b105a
commit e0287899f8

View File

@ -1,20 +1,32 @@
package com.yunbao.common.manager; package com.yunbao.common.manager;
import static cn.rongcloud.rtc.base.RTCErrorCode.JOIN_CHAT_ROOM_TIMEOUT;
import com.yunbao.common.utils.ToastUtil; import com.yunbao.common.utils.ToastUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import cn.rongcloud.rtc.api.RCRTCRemoteUser;
import cn.rongcloud.rtc.api.RCRTCRoom; import cn.rongcloud.rtc.api.RCRTCRoom;
import cn.rongcloud.rtc.api.callback.IRCRTCResultCallback; import cn.rongcloud.rtc.api.callback.IRCRTCResultCallback;
import cn.rongcloud.rtc.api.callback.IRCRTCRoomEventsListener;
import cn.rongcloud.rtc.api.stream.RCRTCInputStream;
import cn.rongcloud.rtc.base.RTCErrorCode; import cn.rongcloud.rtc.base.RTCErrorCode;
/** /**
* RTC管理类负责管理申请同意拒绝PK * RTC管理类负责管理申请同意拒绝PK
*/ */
public class IMRTCManager { public class IMRTCManager {
private static IMRTCManager manager; private static IMRTCManager manager;
private RCRTCRoom rtcRoom; private RCRTCRoom rtcRoom;
private List<String> requestUid;
private IMRTCManager() { private IMRTCManager() {
requestUid = new ArrayList<>();
} }
public static IMRTCManager getInstance() { public static IMRTCManager getInstance() {
@ -30,9 +42,10 @@ public class IMRTCManager {
/** /**
* 响应PK请求 * 响应PK请求
* @param liveUid 对方房间号 *
* @param agree 是否同意 * @param liveUid 对方房间号
* @param extra 扩展参数 * @param agree 是否同意
* @param extra 扩展参数
* @param callback 回调 * @param callback 回调
*/ */
public void responseJoinOtherRoom(String liveUid, boolean agree, String extra, IRCRTCResultCallback callback) { public void responseJoinOtherRoom(String liveUid, boolean agree, String extra, IRCRTCResultCallback callback) {
@ -56,13 +69,18 @@ public class IMRTCManager {
/** /**
* 申请PK * 申请PK
* @param liveUid 对方房间号 *
* @param liveUid 对方房间号
* @param inviterAutoMix 是否将邀请者音视频资源发送到被邀请人房间中合流 * @param inviterAutoMix 是否将邀请者音视频资源发送到被邀请人房间中合流
* @param extra 扩展参数 * @param extra 扩展参数
* @param callback 回调 * @param callback 回调
*/ */
public void requestJoinOtherRoom(String liveUid,boolean inviterAutoMix,String extra,IRCRTCResultCallback callback){ public void requestJoinOtherRoom(String liveUid, boolean inviterAutoMix, String extra, IRCRTCResultCallback callback) {
if(rtcRoom!=null){ if (requestUid.contains(liveUid)) {
callback.onFailed(RTCErrorCode.RongRTCCodeJoinRepeatedRoom);
return;
}
if (rtcRoom != null) {
/* /*
inviteeRoomId - 被邀请者所在房间 id inviteeRoomId - 被邀请者所在房间 id
inviteeUserId - 被邀请用户 id inviteeUserId - 被邀请用户 id
@ -74,6 +92,86 @@ public class IMRTCManager {
extra - 扩展字段默认为空 extra - 扩展字段默认为空
*/ */
rtcRoom.getLocalUser().requestJoinOtherRoom(liveUid, liveUid, inviterAutoMix, extra, new IRCRTCResultCallback() { rtcRoom.getLocalUser().requestJoinOtherRoom(liveUid, liveUid, inviterAutoMix, extra, new IRCRTCResultCallback() {
@Override
public void onSuccess() {
callback.onSuccess();
ToastUtil.show("发起邀请成功");
requestUid.add(liveUid);
startRequestTimeoutTask(liveUid);
}
@Override
public void onFailed(RTCErrorCode errorCode) {
ToastUtil.show("邀请失败 " + errorCode.getValue());
callback.onFailed(errorCode);
}
});
} else {
callback.onFailed(RTCErrorCode.RongRTCCodeIMError);
}
}
public void registerRoomListener(IRCRTCRoomEventsListener listener) {
if (rtcRoom != null) {
rtcRoom.registerRoomListener(new IRCRTCRoomEventsListener() {
@Override
public void onRemoteUserPublishResource(RCRTCRemoteUser remoteUser, List<RCRTCInputStream> streams) {
listener.onRemoteUserPublishResource(remoteUser, streams);
}
@Override
public void onRemoteUserMuteAudio(RCRTCRemoteUser remoteUser, RCRTCInputStream stream, boolean mute) {
listener.onRemoteUserMuteAudio(remoteUser, stream, mute);
}
@Override
public void onRemoteUserMuteVideo(RCRTCRemoteUser remoteUser, RCRTCInputStream stream, boolean mute) {
listener.onRemoteUserMuteVideo(remoteUser, stream, mute);
}
@Override
public void onRemoteUserUnpublishResource(RCRTCRemoteUser remoteUser, List<RCRTCInputStream> streams) {
listener.onRemoteUserUnpublishResource(remoteUser, streams);
}
@Override
public void onUserJoined(RCRTCRemoteUser remoteUser) {
listener.onUserJoined(remoteUser);
}
@Override
public void onUserLeft(RCRTCRemoteUser remoteUser) {
listener.onUserLeft(remoteUser);
}
@Override
public void onUserOffline(RCRTCRemoteUser remoteUser) {
listener.onUserOffline(remoteUser);
}
@Override
public void onPublishLiveStreams(List<RCRTCInputStream> streams) {
listener.onPublishLiveStreams(streams);
}
@Override
public void onUnpublishLiveStreams(List<RCRTCInputStream> streams) {
listener.onUnpublishLiveStreams(streams);
}
});
}
}
/**
* 取消邀请
*
* @param liveUid 房间号
* @param extra 扩展参数
* @param callback 回调
*/
public void cancelRequestJoinOtherRoom(String liveUid, String extra, IRCRTCResultCallback callback) {
if (rtcRoom != null) {
rtcRoom.getLocalUser().cancelRequestJoinOtherRoom(liveUid, liveUid, extra, new IRCRTCResultCallback() {
@Override @Override
public void onSuccess() { public void onSuccess() {
callback.onSuccess(); callback.onSuccess();
@ -84,8 +182,40 @@ public class IMRTCManager {
callback.onFailed(errorCode); callback.onFailed(errorCode);
} }
}); });
}else{ } else {
callback.onFailed(RTCErrorCode.RongRTCCodeIMError); callback.onFailed(RTCErrorCode.RongRTCCodeIMError);
} }
} }
public void callPkSuccess(String liveUid) {
requestUid.remove(liveUid);
}
private void startRequestTimeoutTask(String liveUid) {
new Timer().schedule(new TimerTask() {
int waitTime = 15;
@Override
public void run() {
if (!requestUid.contains(liveUid)) {
cancel();
}
if (waitTime-- == 0) {
//callback.onFailed(JOIN_CHAT_ROOM_TIMEOUT);
cancelRequestJoinOtherRoom(liveUid, "extra", new IRCRTCResultCallback() {
@Override
public void onSuccess() {
requestUid.remove(liveUid);
}
@Override
public void onFailed(RTCErrorCode errorCode) {
}
});
cancel();
}
}
}, 1000, 1000);
}
} }