package com.xscm.moduleutil.rtc; import static com.xscm.moduleutil.rtc.RtcConstants.SCENARIOSTYPE_GAME; import android.app.Application; import android.text.TextUtils; import com.xscm.moduleutil.bean.room.Config; import com.xscm.moduleutil.interfaces.SoundLevelUpdateListener; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import io.agora.rtc2.Constants; import io.agora.rtc2.RtcEngine; /** * @author xf */ public class RtcManager implements Rtc { private final static String TAG = "RTC"; // private String agoraAppId = CommonAppContext.getInstance().getCurrentEnvironment().getSwSdkAppId(); private Application mApplication; private static RtcManager instance; private static RtcEventListener mRtcEventListener; private int mRtcType = -1; private static RtcEngine mRtcEngine; private int mScenariosType = -1; private Config mConfig; String mRoomId; String mUserId; String mUserName; String mToken; String mStreamID; private boolean isGame;//是否游戏语音 private boolean muteMic = true;//麦克风状态,默认关闭 private String audioUrl; private List soundLevelUpdateListeners = new CopyOnWriteArrayList<>(); public RtcManager(Application application) { this.mApplication = application; } public void addRtcEventListener(RtcEventListener rtcEventListener) { mRtcEventListener = rtcEventListener; } public static RtcManager instance(Application application) { if (instance == null) { synchronized (RtcManager.class) { if (instance == null) { instance = new RtcManager(application); } } } return instance; } public static RtcManager getInstance() { return instance; } @Override public void init(int rtcType, int scenariosType, Config config) { this.mRtcType = rtcType; if (rtcType == RtcConstants.RtcType_AGORA) { initAgora(scenariosType); } mScenariosType = scenariosType; mConfig = config; } @Override public void loginRoom(String roomId, String userId, String userName, String token) { if (mRtcType == RtcConstants.RtcType_AGORA) { if (mRtcEngine != null) { //mRtcEngine.joinChannel(token, roomId, "Extra Optional Data", Integer.parseInt(userId)); mRtcEngine.joinChannel(token, roomId, "OpenVCall", Integer.parseInt(userId)); } } mRoomId = roomId; mUserName = userName; mUserId = userId; mToken = token; } @Override public void leaveChannel(String roomId) { downWheat(); if (mRtcType == RtcConstants.RtcType_AGORA) { if (mRtcEngine != null) { mRtcEngine.leaveChannel(); } } mRoomId = null; setGame(false); } @Override public void enableHeadphoneMonitor(boolean b) { if (mRtcType == RtcConstants.RtcType_AGORA) { if (mRtcEngine != null) { mRtcEngine.enableInEarMonitoring(b); } } } @Override public void setHeadphoneMonitorVolume(int volume) { if (mRtcType == RtcConstants.RtcType_AGORA) { if (mRtcEngine != null) { mRtcEngine.setInEarMonitoringVolume(volume); } } } @Override public void setAudioMixingVolume(int volume) { if (mRtcType == RtcConstants.RtcType_AGORA) { if (mRtcEngine != null) { mRtcEngine.adjustAudioMixingVolume(volume); } } } @Override public void startAudioMixing(String url) { this.audioUrl = url; if (mRtcType == RtcConstants.RtcType_AGORA) { if (mRtcEngine != null) { mRtcEngine.startAudioMixing(url, false, 1, 1); } } } @Override public void stopAudioMixing() { this.audioUrl = null; if (mRtcType == RtcConstants.RtcType_AGORA) { if (mRtcEngine != null) { mRtcEngine.stopAudioMixing(); } } } @Override public void pauseAudioMixing() { if (mRtcType == RtcConstants.RtcType_AGORA) { if (mRtcEngine != null) { mRtcEngine.pauseAudioMixing(); } } } @Override public void resumeAudioMixing() { if (mRtcType == RtcConstants.RtcType_AGORA) { if (mRtcEngine != null) { mRtcEngine.resumeAudioMixing(); } } } @Override public void applyWheat(String streamID) { mStreamID = streamID; if (mRtcType == RtcConstants.RtcType_AGORA) { if (mRtcEngine != null) { mRtcEngine.setClientRole(Constants.CLIENT_ROLE_BROADCASTER); mRtcEngine.enableLocalAudio(true); mRtcEngine.muteLocalAudioStream(muteMic); mRtcEngine.enableAudioVolumeIndication(1000, 3,true);//监听远端说话 } } } @Override public void downWheat() { if (mRtcType == RtcConstants.RtcType_AGORA) { if (mRtcEngine != null) { mRtcEngine.setClientRole(Constants.CLIENT_ROLE_AUDIENCE); mRtcEngine.enableLocalAudio(false); mRtcEngine.muteLocalAudioStream(true); } } mStreamID = ""; } public void startPublishingStream() { if (!TextUtils.isEmpty(mStreamID)) { } } public void stopPublishingStream() { } @Override public void muteLocalAudioStream(boolean b) { this.muteMic = b; if (mRtcType == RtcConstants.RtcType_AGORA) { if (mRtcEngine != null) { mRtcEngine.muteLocalAudioStream(b); } } } public void muteMicWithNoNetwork(boolean b) { if (mRtcType == RtcConstants.RtcType_AGORA) { if (mRtcEngine != null) { mRtcEngine.muteLocalAudioStream(b); } } } @Override public void muteSpeaker(boolean b) { if (mRtcType == RtcConstants.RtcType_AGORA) { if (mRtcEngine != null) { mRtcEngine.muteAllRemoteAudioStreams(b); } } } private void initAgora(int scenariosType) { try { mRtcEngine = RtcEngine.create(mApplication, "4a521d6f1c6343998b1c8fd425dea02a", new MyIRtcEngineEventHandler(mRtcEventListener,soundLevelUpdateListeners)); } catch (Exception e) { e.printStackTrace(); } switch (scenariosType) { case RtcConstants.SCENARIOSTYPE_SING: mRtcEngine.setAudioProfile(Constants.AUDIO_PROFILE_MUSIC_HIGH_QUALITY_STEREO, Constants.AUDIO_SCENARIO_GAME_STREAMING); break; default: mRtcEngine.setAudioProfile(Constants.AUDIO_PROFILE_MUSIC_STANDARD_STEREO, Constants.AUDIO_SCENARIO_GAME_STREAMING); break; } mRtcEngine.enableLocalAudio(false); mRtcEngine.muteLocalAudioStream(true); } public void destroy(final RtcDestroyCallback callback) { if (mRtcEngine != null) { RtcEngine.destroy(); mRtcEngine = null; if (callback != null) { callback.onDestroySuccess(); } } if (mRtcEngine == null ) { if (callback != null) { callback.onDestroySuccess(); } } } /** * 停止/开启声浪检测 * * @param atWheat 停止 true 开启 */ public void setSoundLevelMonitor(boolean atWheat) { } /** * 上传即构日志 */ public void uploadLog() { } public void removeRtcEventListener() { mRtcEventListener = null; } /** * 退出房閒 */ public void leaveChannel() { if (isGame && mRoomId != null) { leaveChannel(mRoomId); setAudioUrl(null); } } public void loginRoom(final int rtcType, final int scenariosType, final Config config, final String roomId, final String userId, final String userName, final String token, RtcDestroyCallback rtcDestroyCallback) { if (mRoomId != null && !mRoomId.equals(roomId)) { destroyAndLogin(rtcType, scenariosType, config, roomId, userId, userName, token, rtcDestroyCallback); return; } if (rtcType == mRtcType) {//sdk没切换 if (rtcType == RtcConstants.RtcType_AGORA) { if (scenariosType == mScenariosType && mRtcEngine != null) {//场景没切换 //mRtcEngine.joinChannel(token, roomId, "Extra Optional Data", Integer.parseInt(userId)); mRtcEngine.joinChannel(token, roomId, "OpenVCall", Integer.parseInt(userId)); //加入房间先下麦操作 处理进入房间就开始音频推流问题 downWheat(); if (rtcDestroyCallback != null) { rtcDestroyCallback.onDestroySuccess(); } } else {//场景切换了 destroyAndLogin(rtcType, scenariosType, config, roomId, userId, userName, token, rtcDestroyCallback); return; } } } else { destroyAndLogin(rtcType, scenariosType, config, roomId, userId, userName, token, rtcDestroyCallback); return; } mRtcType = rtcType; mScenariosType = scenariosType; mConfig = config; mRoomId = roomId; mUserId = userId; mUserName = userName; mToken = token; } /** * 销毁并加入房间 * * @param rtcType * @param scenariosType * @param config * @param roomId * @param userId * @param userName * @param token * @param rtcDestroyCallback */ public void destroyAndLogin(final int rtcType, final int scenariosType, final Config config, final String roomId, final String userId, final String userName, final String token, final RtcDestroyCallback rtcDestroyCallback) { destroy(new RtcDestroyCallback() { @Override public void onDestroySuccess() { init(rtcType, scenariosType, config); loginRoom(roomId, userId, userName, token); if (rtcDestroyCallback != null) { rtcDestroyCallback.onDestroySuccess(); } } }); } public void setGame(boolean game) { isGame = game; } /** * 登录H5小游戏 * * @param roomId * @param userId * @param userName * @param token */ public void loginRoomGame(String roomId, String userId, String userName, String token) { if (mRtcEngine != null ) { loginRoom(roomId, userId, userName, token); } else { destroyAndLogin(RtcConstants.RtcType_CURR, SCENARIOSTYPE_GAME, null, roomId, userId, userName, token, null); } setGame(true); } /** * 恢复麦克风状态 */ public void resumeMic() { muteLocalAudioStream(muteMic); } public void resumeAudio() { if (!TextUtils.isEmpty(audioUrl)) { startAudioMixing(audioUrl); } } public void setAudioUrl(String audioUrl) { this.audioUrl = audioUrl; } public boolean isMuteMic() { return muteMic; } public void addSoundLevelListener(SoundLevelUpdateListener listener) { if (soundLevelUpdateListeners != null) { soundLevelUpdateListeners.add(listener); } } public void removeSoundLevelListener(SoundLevelUpdateListener listener) { if (soundLevelUpdateListeners != null) { soundLevelUpdateListeners.remove(listener); } } }