wxr
2024-08-27 83d5ada866a598ee868d3e23624d104d1027db29
HDLLinphoneSDKDemoNew/HDLLinphoneSDK/src/main/java/com/hdl/hdllinphonesdk/HDLLinphoneKit.java
@@ -1,7 +1,12 @@
package com.hdl.hdllinphonesdk;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.view.TextureView;
import android.widget.Toast;
@@ -63,12 +68,74 @@
    private volatile static HDLLinphoneKit instance;
    private Context viewContext;
    private ServiceWaitThread mServiceWaitThread;
    private String mUsername, mPassword, mServerIP;
    private String mUsername, mPassword, mServerIP,mTransportType;
    //    private TextureView mRenderingView, mPreviewView;
    private boolean isAutoJumpCallView;//是否自动跳转呼叫页面
    private String intercomeType;
    /**
     * 铃声播放器
     */
    private MediaPlayer mediaPlayer;
    /**
     * 未接事件触发时间
     */
    public int missedCallsTime = 30;
    private HDLLinphoneKit() {
    }
    private void InitMediaPlayer(Context context) {
        if (mediaPlayer == null) {
            try {
                mediaPlayer = MediaPlayer.create(context, R.raw.oldphone_mono);
            } catch (Exception ex) {
            }
        }
    }
    public boolean getMediaPlayerPlayingStatus(){
        if (mediaPlayer != null){
            return mediaPlayer.isPlaying();
        }
        return false;
    }
    // 播放铃声
    public void playRingtone() {
        if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
            mediaPlayer.start(); // 开始播放
        }
    }
    /**
     * 是否循环播放铃声
     * @param isLooping
     */
    public void setMediaPlayerLooping(boolean isLooping) {
        mediaPlayer.setLooping(isLooping);
    }
    // 停止铃声
    public void stopRingtone() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop(); // 停止播放
//            mediaPlayer.reset(); // 重置 MediaPlayer
//            mediaPlayer.release(); // 释放资源
//            mediaPlayer = null;
        }
    }
    public void releaseRingtone(){
        if (mediaPlayer != null ) {
            mediaPlayer.release(); // 释放资源
            mediaPlayer = null;
        }
    }
    /**
@@ -110,6 +177,7 @@
        viewContext = context.getApplicationContext();
        if (!HDLLinphoneService.isReady()) {
            CheckIfNeedstartService();
            InitMediaPlayer(context);
        } else {
        }
    }
@@ -144,7 +212,7 @@
            @Override
            public void incomingCall(Call linphoneCall) {
                super.incomingCall(linphoneCall);
                Log.d("HDLLinphoneKit","------incomingCall------");
                Log.d("HDLLinphoneKit", "------incomingCall------");
                if (linphoneCall != null) {
                    String callUsername = linphoneCall.getRemoteAddress().getUsername();
                    HDLLog.I("来电号码:" + callUsername);
@@ -163,21 +231,21 @@
            @Override
            public void outgoingInit() {
                super.outgoingInit();
                Log.d("HDLLinphoneKit","------outgoingInit------");
                Log.d("HDLLinphoneKit", "------outgoingInit------");
            }
            @Override
            public void callConnected() {
                super.callConnected();
                // 视频通话默认免提,语音通话默认非免提
                Log.d("HDLLinphoneKit","------callConnected------");
                Log.d("HDLLinphoneKit", "------callConnected------");
                toggleSpeaker(true);
            }
            @Override
            public void callEnd() {
                super.callEnd();
                Log.d("HDLLinphoneKit","------callEnd------");
                Log.d("HDLLinphoneKit", "------callEnd------");
//                context.sendBroadcast(new Intent(HDLLinphoneIntercomActivity.RECEIVE_FINISH_VIDEO_ACTIVITY));
            }
        });
@@ -196,6 +264,24 @@
        }
    }
    private HDLLinphoneService mService;
    private final ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder ibinder) {
            HDLLinphoneService.LinphoneBinder binder = (HDLLinphoneService.LinphoneBinder) ibinder;
            mService = binder.getService();
            //初始化
            mService.initCore();
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }
    };
    /**
     * 开启服务
     *
@@ -206,12 +292,8 @@
            if (!HDLLinphoneService.isReady()) {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setClass(context, HDLLinphoneService.class);
                context.startService(intent);
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                    context.startForegroundService(intent);//解决android8.0以上无法启动服务的问题
//                } else {
//                    context.startService(intent);
//                }
                context.bindService(intent, connection, Context.BIND_AUTO_CREATE);
            }
        } catch (Exception e) {
            e.printStackTrace();
@@ -251,7 +333,22 @@
        mUsername = username;
        mPassword = password;
        mServerIP = serverIP;
        intercomeType = inter_type;
        mTransportType = "UDP";
        login(inter_type);
    }
    public void setAccountAndLogin(String username, String password, String serverIP, String inter_type, String transportType) {
        mUsername = username;
        mPassword = password;
        mServerIP = serverIP;
        mTransportType = transportType;
        intercomeType = inter_type;
        login(inter_type);
    }
    public String getIntercomeType() {
        return intercomeType;
    }
    /**
@@ -352,16 +449,18 @@
     * 登录 SIP 服务器
     */
    private void loginToServer(String inter_type) {
        Log.e("==========", "loginToServer: ");
        try {
            if (mUsername == null || mPassword == null || mServerIP == null) {
                throw new RuntimeException("The sip account is not configured.");
            }
            if (inter_type.equals(INTER_PHONE_TYPE_FREEVIEW)) {
                // 全视通可视对讲注册
                freeViewRegisterUserAuth(mUsername, mPassword, mServerIP);
                freeViewRegisterUserAuth(mUsername, mPassword, mServerIP,mTransportType);
            } else {
                // 其他可视对讲注册
                registerUserAuth(mUsername, mPassword, mServerIP);
                registerUserAuth(mUsername, mPassword, mServerIP,mTransportType);
            }
        } catch (CoreException e) {
            e.printStackTrace();
@@ -376,16 +475,29 @@
     * @param host
     * @throws CoreException
     */
    public void registerUserAuth(String name, String password, String host) throws CoreException {
    public void registerUserAuth(String name, String password, String host,String transportType) throws CoreException {
        Core mLinphoneCore = HDLLinphoneService.getCore();
        if (mLinphoneCore == null) return;
        AuthInfo authInfo = Factory.instance().createAuthInfo(name, null, password, null, null, host);
        AuthInfo authInfo = Factory.instance().createAuthInfo(name, name, password, null, null, host);
        AccountCreator mAccountCreator = mLinphoneCore.createAccountCreator(null);
        mAccountCreator.setUsername(name);
        mAccountCreator.setDomain(host);
        mAccountCreator.setPassword(password);
        mAccountCreator.setTransport(TransportType.Udp);
        switch (transportType.toUpperCase()){
            case "UDP":
                mAccountCreator.setTransport(TransportType.Udp);
                Log.i("linphone", "setting : TransportType.Udp");
                break;
            case "TCP":
                mAccountCreator.setTransport(TransportType.Tcp);
                Log.i("linphone", "setting : TransportType.Tcp");
                break;
            default:
                mAccountCreator.setTransport(TransportType.Udp);
                Log.i("linphone", "default : TransportType.Udp");
                break;
        }
        ProxyConfig prxCfg = mAccountCreator.createProxyConfig();
        prxCfg.enableQualityReporting(false);
@@ -402,7 +514,7 @@
     *
     * @throws CoreException
     */
    public void freeViewRegisterUserAuth(String userName, String password, String domain) throws CoreException {
    public void freeViewRegisterUserAuth(String userName, String password, String domain,String transportType) throws CoreException {
        Core mLinphoneCore = HDLLinphoneService.getCore();
        if (mLinphoneCore == null) return;
@@ -412,13 +524,28 @@
        mAccountCreator.setUsername(userName);
        mAccountCreator.setDomain("yun");
        mAccountCreator.setPassword(password);
        mAccountCreator.setTransport(TransportType.Udp);
        switch (transportType.toUpperCase()){
            case "UDP":
                mAccountCreator.setTransport(TransportType.Udp);
                Log.i("linphone", "setting : TransportType.Udp");
                break;
            case "TCP":
                mAccountCreator.setTransport(TransportType.Tcp);
                Log.i("linphone", "setting : TransportType.Tcp");
                break;
            default:
                mAccountCreator.setTransport(TransportType.Udp);
                Log.i("linphone", "default : TransportType.Udp");
                break;
        }
        ProxyConfig prxCfg = mAccountCreator.createProxyConfig();
        prxCfg.enableQualityReporting(false);
        prxCfg.setQualityReportingCollector(null);
        prxCfg.setQualityReportingInterval(0);
        prxCfg.enableRegister(true);
        //全视通需要设置固定代理服务器ip
        prxCfg.setServerAddr("sip:139.159.157.75:46000;transport=udp");
        mLinphoneCore.addProxyConfig(prxCfg);//添加代理配置。如果启用了注册,这将开始在代理上注册。
@@ -488,7 +615,7 @@
            params.enableLowBandwidth(false);
            params.setAudioBandwidthLimit(0); // disable limitation
            currentCall.acceptWithParams(params);
            Log.d("HDLLinphoneKit","---acceptCall= ");
            Log.d("HDLLinphoneKit", "---acceptCall= ");
        } catch (Exception e) {
            e.printStackTrace();
@@ -552,7 +679,7 @@
     * 设置外放设备
     */
    private void routeAudioToSpeakerHelper(boolean speakerOn) {
        Log.d("HDLLinphoneKit","设置外放设备[Audio Manager] Routing audio to " + (speakerOn ? "speaker" : "earpiece"));
        Log.d("HDLLinphoneKit", "设置外放设备[Audio Manager] Routing audio to " + (speakerOn ? "speaker" : "earpiece"));
        try {
            if (HDLLinphoneService.getCore() == null) return;
            if (HDLLinphoneService.getCore().getCallsNb() == 0) return;
@@ -561,14 +688,14 @@
            if (currentCall == null) return;
            for (AudioDevice audioDevice : HDLLinphoneService.getCore().getAudioDevices()) {
                Log.d("HDLLinphoneKit","audioDevice.getType()= " + audioDevice.getType());
                Log.d("HDLLinphoneKit", "audioDevice.getType()= " + audioDevice.getType());
            }
            for (AudioDevice audioDevice : HDLLinphoneService.getCore().getAudioDevices()) {
                Log.d("HDLLinphoneKit","设置外放设备speakerOn=" + speakerOn + " audioDevice.getType()=" + audioDevice.getType());
                Log.d("HDLLinphoneKit", "设置外放设备speakerOn=" + speakerOn + " audioDevice.getType()=" + audioDevice.getType());
                if (speakerOn && audioDevice.getType() == AudioDevice.Type.Speaker) {
                    currentCall.setOutputAudioDevice(audioDevice);
                    Log.d("HDLLinphoneKit","AudioDevice.Type.Speaker");
                    Log.d("HDLLinphoneKit", "AudioDevice.Type.Speaker");
                    return;
                } else if (!speakerOn && audioDevice.getType() == AudioDevice.Type.Earpiece) {
                    currentCall.setOutputAudioDevice(audioDevice);