Browse Source

修改为无sip服务器呼叫,去掉sip状态

weizhengliang 1 year ago
parent
commit
7d7b023df0

+ 9 - 9
build.gradle

@@ -65,9 +65,9 @@ buildscript {
             /**
              * 依赖仓储
              */
-            maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
-            maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
-            maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
+            maven { url 'https://maven.aliyun.com/nexus/content/repositories/google' }
+            maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
+            maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
 
             //jcenter()
             mavenCentral()
@@ -100,9 +100,9 @@ buildscript {
     }
 
     repositories {
-        maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
-        maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
-        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
+        maven { url 'https://maven.aliyun.com/nexus/content/repositories/google' }
+        maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
+        maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
 
         jcenter()
         mavenCentral()
@@ -121,9 +121,9 @@ buildscript {
  */
 allprojects {
     repositories {
-        maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
-        maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
-        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
+        maven { url 'https://maven.aliyun.com/nexus/content/repositories/google' }
+        maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
+        maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
 
         jcenter()
         mavenCentral()

+ 29 - 8
callingbed/src/main/java/com/wdkl/app/ncs/callingbed/activity/CallingbedActivity.kt

@@ -68,6 +68,7 @@ import org.greenrobot.eventbus.EventBus
 import org.greenrobot.eventbus.Subscribe
 import org.greenrobot.eventbus.ThreadMode
 import org.linphone.core.AccountCreator
+import org.linphone.core.Factory
 import org.linphone.core.RegistrationState
 import org.linphone.core.TransportType
 import serialporttest.utils.SerialPortUtil
@@ -160,6 +161,7 @@ class CallingbedActivity :BaseActivity<CallingbedActivityPresenter, CallingbedMa
 
         //获取mac地址
         Constant.LOCAL_MAC = NetHelper.getInstance().macAddress
+        Constant.LOCAL_IP = NetHelper.getInstance().localIP
 
         //注册广播
         regReceiver()
@@ -547,7 +549,7 @@ class CallingbedActivity :BaseActivity<CallingbedActivityPresenter, CallingbedMa
 
         //配置sip账户
         if (WdklSipService.getCore() != null) {
-            mAccountCreator = WdklSipService.getCore().createAccountCreator(null)
+            /*mAccountCreator = WdklSipService.getCore().createAccountCreator(null)
             WdklSipService.getCore().clearAllAuthInfo()
             WdklSipService.getCore().clearProxyConfig()
             // 以下三项必须
@@ -561,10 +563,29 @@ class CallingbedActivity :BaseActivity<CallingbedActivityPresenter, CallingbedMa
 
                 // 这里会自动创建代理配置、认证信息到 SIP核心
                 val cfg = mAccountCreator!!.createProxyConfig()
-                cfg.expires = 12000
-                Log.e(TAG, "sip login time: " + cfg.expires)
+                cfg.expires = 300
                 // 确保新创建的是最新
                 WdklSipService.getCore().defaultProxyConfig = cfg
+            }*/
+
+
+            WdklSipService.getCore().clearAllAuthInfo()
+            WdklSipService.getCore().clearProxyConfig()
+            //无服务器时端口必须,固定5060,否则拨打IP时,也需加上端口号。如:sip:192.168.1.100:6666
+            val transports = Factory.instance().createTransports()
+            transports.setUdpPort(5060)
+            transports.setTcpPort(5060)
+            WdklSipService.getCore().setTransports(transports)
+
+            if (!TextUtils.isEmpty(Constant.SIP_ID) && !TextUtils.isEmpty(Constant.LOCAL_IP)) {
+                val accountCreator = WdklSipService.getCore().createAccountCreator(null)
+                accountCreator.setUsername(Constant.SIP_ID)
+                accountCreator.setDomain(Constant.LOCAL_IP)
+                accountCreator.setTransport(TransportType.Udp)
+                val cfg = accountCreator.createProxyConfig()
+                WdklSipService.getCore().setDefaultProxyConfig(cfg)
+            } else {
+                showMessage("没有IP地址或没有SIP ID")
             }
         }
     }
@@ -1096,11 +1117,11 @@ class CallingbedActivity :BaseActivity<CallingbedActivityPresenter, CallingbedMa
                     updateStatus(status)
                 }*/
 
-                if (messageEvent.message is RegistrationState) {
+                /*if (messageEvent.message is RegistrationState) {
                     val state = messageEvent.message as RegistrationState
                     Log.d("callingbed", "sip register state: $state")
                     updateSipState(state)
-                }
+                }*/
             }
 
             //网络ping状态
@@ -1303,7 +1324,7 @@ class CallingbedActivity :BaseActivity<CallingbedActivityPresenter, CallingbedMa
          */
         if (Build.MODEL.equals("rk3128") || "WDFJ-I".equals(Build.MODEL)) {
             var count = SettingConfig.getNetErrResetCount(this)
-            if (!Constant.TCP_CONNECTED && TextUtils.isEmpty(NetHelper.getInstance().localIP)) {
+            if (!Constant.TCP_CONNECTED && TextUtils.isEmpty(Constant.LOCAL_IP)) {
                 netErrCount++
                 Log.e(TAG, "network error!!! ==> netErrCount: $netErrCount, count: $count")
             } else {
@@ -1341,10 +1362,10 @@ class CallingbedActivity :BaseActivity<CallingbedActivityPresenter, CallingbedMa
     private fun updateTcpState() {
         if (Constant.TCP_CONNECTED) {
             view_title_layout_iv_tcp.setImageResource(R.mipmap.ic_tcp_success)
-            //view_title_layout_tv_point.setBackgroundResource(R.color.green)
+            view_title_layout_tv_point.setBackgroundResource(R.color.green)
         } else {
             view_title_layout_iv_tcp.setImageResource(R.mipmap.ic_tcp_fail)
-            //view_title_layout_tv_point.setBackgroundResource(R.color.red_color)
+            view_title_layout_tv_point.setBackgroundResource(R.color.red_color)
         }
     }
 

+ 2 - 4
callingbed/src/main/java/com/wdkl/app/ncs/callingbed/fragment/QrCodeFragment.kt

@@ -70,8 +70,6 @@ class QrCodeFragment : BaseFragment<QrCodeFragmentPresenter, QrcodeViewBinding>(
             }
         }.start()
 
-        val macAddr = NetHelper.getInstance().macAddress
-        val ipAddr = NetHelper.getInstance().localIP
         if (Constant.DEVICE_STATUS == 0) {
             tv_device_id.text = "设备ID: " + Constant.DEVICE_ID + " - 未启用"
         } else if (Constant.DEVICE_STATUS == 1) {
@@ -81,8 +79,8 @@ class QrCodeFragment : BaseFragment<QrCodeFragmentPresenter, QrcodeViewBinding>(
         }
 
         val serverIp =  UrlManager.build().base.substringAfterLast("//").substringBefore(":")
-        tv_local_ip.text = "本机IP: " + ipAddr + " - " + serverIp
-        tv_local_mac.text = "本机MAC: " + macAddr
+        tv_local_ip.text = "本机IP: " + Constant.LOCAL_IP + " - " + serverIp
+        tv_local_mac.text = "本机MAC: " + Constant.LOCAL_MAC
         tv_app_version.text = "版本信息: V" + BuildConfig.VERSION_NAME
         tv_mcu_version.text = "MCU版本: " + Constant.MCU_VERSION_NUMBER
     }

+ 24 - 36
callingbed/src/main/java/com/wdkl/app/ncs/callingbed/fragment/SipCallFragment.kt

@@ -44,34 +44,6 @@ class SipCallFragment: BaseCallFragment() {
 
     private var callSuccess: Boolean = false
 
-
-    // 配置通话状态监听
-    private val coreListener = object : CoreListenerStub() {
-        override fun onCallStateChanged(
-            core: Core,
-            call: Call,
-            state: Call.State,
-            message: String
-        ) {
-            Log.e(TAG, "received call state: $state, ${call.remoteAddress.displayName}, $message")
-            if (state == Call.State.IncomingReceived || state == Call.State.IncomingEarlyMedia) {
-                //来电时将自动接听
-                if (sipCore != null) {
-                    val params = sipCore!!.createCallParams(call)
-                    params.enableVideo(false)
-                    call.acceptWithParams(params)
-                } else {
-                    VoiceUtil.handoffAudioCall(Constant.DEVICE_ID, fromId, Constant.interactionId)
-                    callEnd()
-                }
-            } else if (state == Call.State.End || state == Call.State.Released) {
-                callEnd()
-            } else if (state == Call.State.Connected) {
-                showCalling()
-            }
-        }
-    }
-
     override fun getLayId(): Int {
         return R.layout.sip_voice_call_layout
     }
@@ -86,9 +58,6 @@ class SipCallFragment: BaseCallFragment() {
         }
 
         sipCore = WdklSipService.getCore()
-        if (sipCore != null) {
-            sipCore!!.addListener(coreListener)
-        }
 
         when (callState) {
             0 -> {
@@ -163,9 +132,6 @@ class SipCallFragment: BaseCallFragment() {
         if (sip_voice_call_timer != null) {
             sip_voice_call_timer.stop()
         }
-        if (sipCore != null) {
-            sipCore!!.removeListener(coreListener)
-        }
         RingPlayHelper.stopRingTone()
     }
 
@@ -268,7 +234,7 @@ class SipCallFragment: BaseCallFragment() {
             if (call == null) {
                 call = sipCore!!.calls[0]
             }
-            call!!.terminate()
+            call?.terminate()
         }
     }
 
@@ -300,7 +266,7 @@ class SipCallFragment: BaseCallFragment() {
                                 Constant.interactionId = curInteractionVO.id
                                 fromId = curTcpModel.fromId
                                 acceptCall()
-                                if (sipCore == null || TextUtils.isEmpty(curInteractionVO.toSipId)) {
+                                /*if (sipCore == null || TextUtils.isEmpty(curInteractionVO.toSipId)) {
                                     //通话失败,重置并返回主界面
                                     showMessage("Core或toSipId为空!")
                                     Constant.CALL_STATE = Constant.CALL_STANDBY
@@ -314,6 +280,17 @@ class SipCallFragment: BaseCallFragment() {
                                         sipCore!!.inviteAddressWithParams(addressToCall, params)
                                         Log.d(TAG, "invite target device: " + addressToCall.asString())
                                     }
+                                }*/
+
+                                //无服务器呼叫
+                                val addressToCall = sipCore?.interpretUrl(curInteractionVO.toSipId + "@" + curInteractionVO.toEthIp)
+                                val params = sipCore?.createCallParams(null)
+                                //视频开关
+                                params?.enableVideo(false)
+                                if (addressToCall != null) {
+                                    Log.e(TAG, "send invite address: " + addressToCall.asString())
+                                    showMessage("呼叫: " + addressToCall.asString())
+                                    sipCore?.inviteAddressWithParams(addressToCall, params!!)
                                 }
                             } else if (curTcpModel.getAction() == TcpAction.VoiceAction.REJECT) {
                                 //我方呼出,对方拒绝
@@ -385,6 +362,17 @@ class SipCallFragment: BaseCallFragment() {
                     }
                 }
             }
+
+            Constant.EVENT_END_CALL -> {
+                Log.d(TAG, ">>>>>>>>>>>>>> EVENT_END_CALL")
+                Constant.CALL_STATE = Constant.CALL_STANDBY
+                VoiceUtil.handoffAudioCall(Constant.DEVICE_ID, fromId, Constant.interactionId)
+                callEnd()
+            }
+
+            Constant.SIP_CONNECTED -> {
+                showCalling()
+            }
         }
     }
 

+ 20 - 0
callingbed/src/main/java/com/wdkl/app/ncs/callingbed/sip/WdklSipService.java

@@ -11,6 +11,7 @@ import android.support.annotation.Nullable;
 import android.widget.Toast;
 
 import com.wdkl.app.ncs.callingbed.R;
+import com.wdkl.ncs.android.lib.base.BaseApplication;
 import com.wdkl.ncs.android.middleware.common.Constant;
 import com.wdkl.ncs.android.middleware.common.MessageEvent;
 
@@ -84,6 +85,25 @@ public class WdklSipService extends Service {
         //主监听器,根据事件调用界面
         mCoreListener = new CoreListenerStub() {
             @Override
+            public void onCallStateChanged(Core core, Call call, Call.State state, String message) {
+                Toast.makeText(WdklSipService.this, message, Toast.LENGTH_SHORT).show();
+                android.util.Log.d("sipCall", ">>>>>>>>>>>> call state: " + state + ", " + call.getRemoteAddress().asString());
+
+                if (state == Call.State.IncomingReceived || state == Call.State.IncomingEarlyMedia) {
+                    //来电时将自动接听
+                    CallParams params = getCore().createCallParams(call);
+                    if (params != null) {
+                        params.enableVideo(false);
+                    }
+                    call.acceptWithParams(params);
+                } else if (state == Call.State.Connected) {
+                    EventBus.getDefault().post(new MessageEvent(state, Constant.SIP_CONNECTED));
+                } else if (state == Call.State.End || state == Call.State.Released){
+                    EventBus.getDefault().post(new MessageEvent(state, Constant.EVENT_END_CALL));
+                }
+            }
+
+            @Override
             public void onRegistrationStateChanged(Core core, ProxyConfig cfg, RegistrationState state, String message) {
                 EventBus.getDefault().post(new MessageEvent(state, Constant.EVENT_SIP_REGISTER_STATUS));
             }

+ 5 - 0
middleware/src/main/code/com/wdkl/ncs/android/middleware/common/Constant.java

@@ -56,6 +56,7 @@ public class Constant {
 
     //MAC地址
     public static String LOCAL_MAC = "";
+    public static String LOCAL_IP = "";
 
     //设备ID
     public static Integer DEVICE_ID;
@@ -189,4 +190,8 @@ public class Constant {
     public static final int EVENT_RESTART_APP = 0x12;
 
     public static final int EVENT_UPDATE_FEES = 0x13;
+
+    public static final int EVENT_END_CALL = 0x14;
+
+    public static final int SIP_CONNECTED = 0x15;
 }

+ 1 - 1
middleware/src/main/code/com/wdkl/ncs/android/middleware/tcp/TcpClientHandler.java

@@ -44,7 +44,7 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<String> {
         retryTimes = 0;
         Constant.TCP_CONNECTED = true;
         EventBus.getDefault().post(new MessageEvent(1, Constant.EVENT_TCP_STATE));
-        TcpModel tcpModel = DeviceUtil.deviceConnect(Constant.LOCAL_MAC);
+        TcpModel tcpModel = DeviceUtil.deviceConnect(Constant.LOCAL_MAC, Constant.LOCAL_IP);
         TcpClient.getInstance().sendMsg(tcpModel.toJson());
     }
 

+ 2 - 1
middleware/src/main/code/com/wdkl/ncs/android/middleware/tcp/channel/DeviceUtil.java

@@ -9,7 +9,7 @@ import com.wdkl.ncs.android.middleware.tcp.enums.TcpAction;
 import com.wdkl.ncs.android.middleware.tcp.enums.TcpType;
 
 public class DeviceUtil {
-    public static TcpModel deviceConnect(String mac){
+    public static TcpModel deviceConnect(String mac, String ip){
         TcpModel tcpModel = new TcpModel();
         DeviceConnectDTO connectDTO = new DeviceConnectDTO();
         connectDTO.setIdentification(mac);
@@ -17,6 +17,7 @@ public class DeviceUtil {
         connectDTO.setSoftware_version("V"+ BuildConfig.VERSION_NAME);
         connectDTO.setModel(Build.MODEL);
         connectDTO.setCode(Build.SERIAL);
+        connectDTO.setClientIp(ip);
         tcpModel.setType(TcpType.DEVICE);
         tcpModel.setAction(TcpAction.DeviceAction.CONNECT);
         tcpModel.setData(connectDTO);

+ 11 - 0
middleware/src/main/code/com/wdkl/ncs/android/middleware/tcp/dto/DeviceConnectDTO.java

@@ -31,6 +31,9 @@ public class DeviceConnectDTO implements Serializable {
      */
     private String code;
 
+    /** 客户端Ip **/
+    private String clientIp;
+
     public String getIdentification() {
         return identification;
     }
@@ -70,4 +73,12 @@ public class DeviceConnectDTO implements Serializable {
     public void setCode(String code) {
         this.code = code;
     }
+
+    public String getClientIp() {
+        return clientIp;
+    }
+
+    public void setClientIp(String clientIp) {
+        this.clientIp = clientIp;
+    }
 }