@@ -1,407 +0,0 @@
package com.yizhuan.xchat_android_core.manager.wj ;
import android.os.Environment ;
import android.os.Handler ;
import android.os.Message ;
import android.util.Log ;
import androidx.annotation.NonNull ;
import androidx.annotation.Nullable ;
import com.netease.nim.uikit.common.util.log.LogUtil ;
import com.wjhd.wy.WJMediaEngine ;
import com.wjhd.wy.audio.AudioEngine ;
import com.wjhd.wy.audio.AudioEngineHandler ;
import com.wjhd.wy.audio.constant.AudioRole ;
import com.wjhd.wy.audio.constant.ErrorCode ;
import com.wjhd.wy.audio.entity.AudioVolumeInfo ;
import com.yizhuan.xchat_android_core.BuildConfig ;
import com.yizhuan.xchat_android_core.manager.AvRoomDataManager ;
import com.yizhuan.xchat_android_core.manager.BaseEngine ;
import com.yizhuan.xchat_android_core.manager.IMNetEaseManager ;
import com.yizhuan.xchat_android_core.manager.RoomEvent ;
import com.yizhuan.xchat_android_core.room.bean.RoomInfo ;
import com.yizhuan.xchat_android_core.room.model.AvRoomModel ;
import com.yizhuan.xchat_android_library.utils.SingleToastUtil ;
import com.yizhuan.xchat_android_library.utils.config.BasicConfig ;
import org.jetbrains.annotations.NotNull ;
import java.io.File ;
import java.lang.ref.WeakReference ;
import java.util.concurrent.atomic.AtomicInteger ;
public class WJAudioEngineManager extends BaseEngine {
public static final String TAG = " WJAudioEngineManager " ;
private String logfileDir ;
@Nullable
private AudioEngine mAudioEngine ;
@NonNull
private final AudioEngineHandler audioEngineHandler = new AudioHandler ( ) ;
private AudioRole curRole ;
private boolean isServerKicked = false ;
private boolean isChaRoomInChannel = false ;
private final AtomicInteger lock = new AtomicInteger ( 0 ) ;
private static final class Helper {
private static final WJAudioEngineManager INSTANCE = new WJAudioEngineManager ( ) ;
}
private WJAudioEngineManager ( ) {
super ( ) ;
}
public static WJAudioEngineManager get ( ) {
return WJAudioEngineManager . Helper . INSTANCE ;
}
private void initRtcEngine ( ) {
WJAudioConstant . LOG_DIR = logfileDir = Environment . getExternalStorageDirectory ( ) . getPath ( ) + File . separator + BasicConfig . INSTANCE . getAppContext ( ) . getPackageName ( ) + File . separator + " log " ;
if ( mAudioEngine = = null ) {
try {
AudioEngine . setRecordingAudioFrameParameters ( 44100 , 2 ) ;
AudioEngine . setLogFileDir ( logfileDir ) ;
AudioEngine . setDebugMode ( BuildConfig . DEBUG ) ; //设置是否调试模式, 底层默认是false
mAudioEngine = WJMediaEngine . createAudioEngine ( BasicConfig . INSTANCE . getAppContext ( ) , WJAudioConstant . APPID , audioEngineHandler ) ;
} catch ( Exception e ) {
throw new RuntimeException ( " NEED TO check sdk init fatal error " + Log . getStackTraceString ( e ) ) ;
}
mAudioEngine . setDefaultAudioRoutetoSpeakerphone ( true ) ;
// 回调谁在说话
mAudioEngine . enableAudioVolumeIndication ( 400 , 0 ) ;
mAudioEngine . setAudioMixingVAD ( true ) ;
mAudioEngine . setEnergyVadFlag ( true ) ;
}
}
private final Handler handler = new RtcEngineHandler ( this ) ;
private static class RtcEngineHandler extends Handler {
private final WeakReference < WJAudioEngineManager > mReference ;
RtcEngineHandler ( WJAudioEngineManager manager ) {
mReference = new WeakReference < > ( manager ) ;
}
@Override
public void handleMessage ( @NotNull Message msg ) {
super . handleMessage ( msg ) ;
WJAudioEngineManager rtcEngineManager = mReference . get ( ) ;
if ( rtcEngineManager = = null ) return ;
if ( msg . what = = WJAudioConstant . MESSAGE_ENTER_CHANNEL_SUCCESS ) {
if ( ! rtcEngineManager . inRoom ) {
//改成无界加入聊天室
IMNetEaseManager . get ( ) . joinAvRoom ( ) ;
rtcEngineManager . inRoom = true ;
}
} else if ( msg . what = = WJAudioConstant . MESSAGE_ENTER_CHANNEL_FAIL ) {
int code = ( int ) msg . obj ;
// 登录失败
LogUtil . e ( TAG , " WJAudio onLoginCompletion: code: " + code ) ;
SingleToastUtil . showToast ( " 进房失败 code: + " + code + " ,请退出房间重新进入 " ) ;
} else if ( msg . what = = WJAudioConstant . MESSAGE_AUDIO_VOLUME_INDICATION ) {
//onAudioVolumeIndication 说话声音音量提示回调
AudioVolumeInfo [ ] speakers = ( AudioVolumeInfo [ ] ) msg . obj ;
RoomInfo roomInfo = AvRoomDataManager . get ( ) . mCurrentRoomInfo ;
if ( roomInfo = = null ) return ;
if ( rtcEngineManager . speakQueueMembersPosition . size ( ) > 0 ) {
rtcEngineManager . speakQueueMembersPosition . clear ( ) ;
}
if ( speakers = = null | | speakers . length = = 0 ) return ;
for ( AudioVolumeInfo speaker : speakers ) {
int micPosition = AvRoomDataManager . get ( ) . getMicPosition ( speaker . getUid ( ) ) ;
if ( micPosition = = Integer . MIN_VALUE ) continue ;
rtcEngineManager . speakQueueMembersPosition . add ( micPosition ) ;
}
IMNetEaseManager . get ( ) . getChatRoomEventObservable ( ) . onNext (
new RoomEvent ( ) . setEvent ( RoomEvent . SPEAK_STATE_CHANGE )
. setMicPositionList ( rtcEngineManager . speakQueueMembersPosition )
) ;
}
}
}
private static class AudioHandler extends AudioEngineHandler {
@Override
public void onError ( int err ) {
if ( err = = ErrorCode . LOGIN_JOIN_CHANNEL_ERROR ) {
WJAudioEngineManager manager = WJAudioEngineManager . get ( ) ;
Message message = manager . handler . obtainMessage ( ) ;
message . what = WJAudioConstant . MESSAGE_ENTER_CHANNEL_FAIL ;
message . obj = err ;
}
SingleToastUtil . showToast ( " RTC error! code: " + err ) ;
}
@Override
public void onJoinChannelSuccess ( long channel , long uid , int elapsed ) {
super . onJoinChannelSuccess ( channel , uid , elapsed ) ;
WJAudioEngineManager manager = WJAudioEngineManager . get ( ) ;
Message message = manager . handler . obtainMessage ( ) ;
message . what = WJAudioConstant . MESSAGE_ENTER_CHANNEL_SUCCESS ;
manager . handler . sendMessage ( message ) ;
manager . isServerKicked = false ;
manager . channelId = channel ;
manager . uid = uid ;
}
@Override
public void onLastmileQuality ( int quality ) {
super . onLastmileQuality ( quality ) ;
if ( 100 > quality & & quality > 50 ) {
IMNetEaseManager . postRoomEvent ( new RoomEvent ( ) . setEvent ( RoomEvent . RTC_ENGINE_NETWORK_BAD ) ) ;
}
}
@Override
public void onConnectionLost ( ) {
super . onConnectionLost ( ) ;
IMNetEaseManager . postRoomEvent ( new RoomEvent ( ) . setEvent ( RoomEvent . RTC_ENGINE_NETWORK_CLOSE ) ) ;
}
@Override
public void onAudioVolumeIndication ( AudioVolumeInfo [ ] speakers ) {
super . onAudioVolumeIndication ( speakers ) ;
WJAudioEngineManager manager = WJAudioEngineManager . get ( ) ;
Message message = manager . handler . obtainMessage ( ) ;
message . what = WJAudioConstant . MESSAGE_AUDIO_VOLUME_INDICATION ;
message . obj = speakers ;
manager . handler . sendMessage ( message ) ;
}
@Override
public void onAudioMixingFinished ( ) {
super . onAudioMixingFinished ( ) ;
IMNetEaseManager . postRoomEvent ( new RoomEvent ( ) . setEvent ( RoomEvent . METHOD_ON_AUDIO_MIXING_FINISHED ) ) ;
}
@Override
public void onClientRoleChanged ( AudioRole oldRole , AudioRole newRole ) {
super . onClientRoleChanged ( oldRole , newRole ) ;
WJAudioEngineManager . get ( ) . curRole = newRole ;
}
@Override
public void onOfflineTimeOut ( long channel , long uid ) {
super . onOfflineTimeOut ( channel , uid ) ;
Log . e ( TAG , " onOfflineTimeOut~~~ " ) ;
WJAudioEngineManager manager = WJAudioEngineManager . get ( ) ;
synchronized ( manager . lock ) {
manager . isServerKicked = true ;
}
if ( manager . isChaRoomInChannel ) {
if ( manager . mAudioEngine = = null ) return ;
manager . mAudioEngine . joinChannel ( manager . channelId , uid , WJAudioConstant . BUSINESS_UID ) ;
manager . mAudioEngine . setRole ( manager . curRole ) ;
}
}
@Override
public void OnMicUniqueIDUpdate ( String micUniqueID ) {
super . OnMicUniqueIDUpdate ( micUniqueID ) ;
Log . e ( TAG , " OnMicUniqueIDUpdate~~~ " ) ;
AvRoomModel . get ( ) . updateMicToken ( micUniqueID ) . subscribe ( ) ;
}
}
@Override
public void resetChannel ( ) {
if ( mAudioEngine ! = null ) {
stopAudioMixing ( ) ;
mAudioEngine . leaveChannel ( ) ;
AudioEngine . destroy ( ) ;
mAudioEngine = null ;
}
isMute = false ;
isRemoteMute = false ;
needRecord = false ;
inRoom = false ;
isAudienceRole = true ;
curRole = AudioRole . NO_ROLE ;
}
@Override
public void reEnterChannel ( long channelId , long uid ) {
resetChannel ( ) ;
enterChannel ( channelId , uid ) ;
}
@Override
public void enterChannel ( long channelId , long uid ) {
initRtcEngine ( ) ;
if ( mAudioEngine = = null ) return ;
mAudioEngine . joinChannel ( channelId , uid , WJAudioConstant . BUSINESS_UID ) ;
}
@Override
public void leaveChannel ( ) {
Log . e ( TAG , " leaveChannel~~~ " ) ;
if ( mAudioEngine ! = null ) {
stopAudioMixing ( ) ;
mAudioEngine . leaveChannel ( ) ;
WJMediaEngine . destroyAudioEngine ( ) ;
}
mAudioEngine = null ;
isMute = false ;
isRemoteMute = false ;
needRecord = false ;
inRoom = false ;
isAudienceRole = false ;
curRole = AudioRole . NO_ROLE ;
}
@Override
public boolean isEnableLoopBack ( ) {
return false ;
}
@Override
public boolean setRole ( int role ) {
if ( role = = ROLE_BROADCASTER ) {
if ( mAudioEngine ! = null ) {
mAudioEngine . setRole ( AudioRole . CLIENT_ROLE_BROADCASTER ) ;
}
} else if ( role = = ROLE_AUDIENCE ) {
if ( mAudioEngine ! = null ) {
mAudioEngine . setRole ( AudioRole . CLIENT_ROLE_AUDIENCE ) ;
}
}
//isAudienceRole 外表用于判断麦显示状态的
isAudienceRole = role = = ROLE_AUDIENCE ;
return true ;
}
@Override
public void setRemoteMute ( boolean mute ) {
if ( mAudioEngine = = null ) return ;
mAudioEngine . muteAllRemoteAudioStreams ( mute ) ;
isRemoteMute = mute ;
}
@Override
public void setMute ( boolean mute ) {
if ( mAudioEngine = = null ) return ;
isMute = mute ;
mAudioEngine . muteLocalAudioStream ( mute ) ;
}
@Override
public void adjustAudioMixingVolume ( int volume ) {
if ( mAudioEngine = = null ) return ;
mAudioEngine . adjustAudioMixingVolume ( volume ) ;
}
@Override
public void adjustRecordingSignalVolume ( int volume ) {
if ( mAudioEngine = = null ) return ;
mAudioEngine . adjustRecordingSignalVolume ( volume ) ;
}
@Override
public int resumeAudioMixing ( ) {
int res = - 1 ;
if ( mAudioEngine ! = null ) {
res = mAudioEngine . resumeAudioMixing ( ) ;
}
if ( res = = 0 ) {
isMusicPlaying = true ;
}
return res ;
}
@Override
public int pauseAudioMixing ( ) {
int res = - 1 ;
if ( mAudioEngine ! = null ) {
res = mAudioEngine . pauseAudioMixing ( ) ;
}
if ( res = = 0 ) {
isMusicPlaying = false ;
}
return res ;
}
@Override
public int startAudioMixing ( String filePath , boolean loopback , int cycle ) {
int res = - 1 ;
if ( mAudioEngine ! = null ) {
res = mAudioEngine . startAudioMixing ( filePath ) ;
}
if ( res = = 0 ) {
isMusicPlaying = true ;
}
return res ;
}
@Override
public int stopAudioMixing ( ) {
int res = - 1 ;
if ( mAudioEngine ! = null ) {
res = mAudioEngine . stopAudioMixing ( ) ;
}
if ( res = = 0 ) {
isMusicPlaying = false ;
}
return res ;
}
@Override
public int getAudioMixingCurrentPosition ( ) {
if ( mAudioEngine = = null ) {
return 0 ;
}
return ( int ) mAudioEngine . getAudioMixingCurrentPosition ( ) ;
}
@Override
public int getAudioMixingDuration ( ) {
if ( mAudioEngine = = null ) {
return 0 ;
}
return ( int ) mAudioEngine . getAudioMixingDuration ( ) ;
}
@Override
public void setRemoteMute ( long uid , boolean mute ) {
}
@Override
public void setChatRoomOnlineStatus ( boolean status ) {
if ( status & & isServerKicked ) {
if ( mAudioEngine = = null ) return ;
mAudioEngine . joinChannel ( channelId , uid , WJAudioConstant . BUSINESS_UID ) ;
mAudioEngine . setRole ( curRole ) ;
synchronized ( lock ) {
isServerKicked = false ;
}
}
synchronized ( lock ) {
isChaRoomInChannel = status ;
}
}
@Override
public void setASMRMode ( boolean enable ) {
if ( mAudioEngine = = null ) {
return ;
}
mAudioEngine . setDenioseLevel ( enable ? 0 : 75 ) ;
mAudioEngine . setAudioEncodeBitRate ( enable ? 192000 : 128000 ) ;
}
}