SkyEngineKit.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.wdkl.skywebrtc;
  2. import android.content.Context;
  3. import android.util.Log;
  4. import com.wdkl.skywebrtc.except.NotInitializedException;
  5. import com.wdkl.skywebrtc.inter.ISkyEvent;
  6. /**
  7. * 主控类
  8. * Created by dds on 2019/8/19.
  9. */
  10. public class SkyEngineKit {
  11. private final static String TAG = "dds_AVEngineKit";
  12. private static SkyEngineKit avEngineKit;
  13. private CallSession mCurrentCallSession;
  14. private ISkyEvent mEvent;
  15. private boolean isAudioOnly = false;
  16. private boolean isOutGoing = false;
  17. public static SkyEngineKit Instance() {
  18. SkyEngineKit var;
  19. if ((var = avEngineKit) != null) {
  20. return var;
  21. } else {
  22. throw new NotInitializedException();
  23. }
  24. }
  25. // 初始化
  26. public static void init(ISkyEvent iSocketEvent) {
  27. if (avEngineKit == null) {
  28. avEngineKit = new SkyEngineKit();
  29. avEngineKit.mEvent = iSocketEvent;
  30. }
  31. }
  32. public void sendRefuseOnPermissionDenied(String room, String inviteId) {
  33. // 未初始化
  34. if (avEngineKit == null) {
  35. Log.e(TAG, "startOutCall error,please init first");
  36. return;
  37. }
  38. if (mCurrentCallSession != null) {
  39. endCall();
  40. } else {
  41. avEngineKit.mEvent.sendRefuse(room, inviteId, EnumType.RefuseType.Hangup.ordinal());
  42. }
  43. }
  44. public void sendDisconnected(String room, String toId, boolean isCrashed) {
  45. // 未初始化
  46. if (avEngineKit == null) {
  47. Log.e(TAG, "startOutCall error,please init first");
  48. return;
  49. }
  50. avEngineKit.mEvent.sendDisConnect(room, toId, isCrashed);
  51. }
  52. // 拨打电话
  53. public boolean startOutCall(Context context, final String room, final String targetId,
  54. final boolean audioOnly) {
  55. // 未初始化
  56. if (avEngineKit == null) {
  57. Log.e(TAG, "startOutCall error,please init first");
  58. return false;
  59. }
  60. // 忙线中
  61. if (mCurrentCallSession != null && mCurrentCallSession.getState() != EnumType.CallState.Idle) {
  62. Log.i(TAG, "startCall error,currentCallSession is exist");
  63. return false;
  64. }
  65. isAudioOnly = audioOnly;
  66. isOutGoing = true;
  67. // 初始化会话
  68. mCurrentCallSession = new CallSession(context, room, audioOnly, mEvent);
  69. mCurrentCallSession.setTargetId(targetId);
  70. mCurrentCallSession.setIsComing(false);
  71. mCurrentCallSession.setCallState(EnumType.CallState.Outgoing);
  72. // 创建房间
  73. mCurrentCallSession.createHome(room, 2);
  74. return true;
  75. }
  76. // 接听电话
  77. public boolean startInCall(Context context, final String room, final String targetId,
  78. final boolean audioOnly) {
  79. if (avEngineKit == null) {
  80. Log.e(TAG, "startInCall error,init is not set");
  81. return false;
  82. }
  83. // 忙线中
  84. if (mCurrentCallSession != null && mCurrentCallSession.getState() != EnumType.CallState.Idle) {
  85. // 发送->忙线中...
  86. Log.i(TAG, "startInCall busy,currentCallSession is exist,start sendBusyRefuse!");
  87. mCurrentCallSession.sendBusyRefuse(room, targetId);
  88. return false;
  89. }
  90. isOutGoing = false;
  91. this.isAudioOnly = audioOnly;
  92. // 初始化会话
  93. mCurrentCallSession = new CallSession(context, room, audioOnly, mEvent);
  94. mCurrentCallSession.setTargetId(targetId);
  95. mCurrentCallSession.setIsComing(true);
  96. mCurrentCallSession.setCallState(EnumType.CallState.Incoming);
  97. // 开始响铃并回复
  98. mCurrentCallSession.shouldStartRing();
  99. mCurrentCallSession.sendRingBack(targetId, room);
  100. return true;
  101. }
  102. // 挂断会话
  103. public void endCall() {
  104. Log.d(TAG, "endCall mCurrentCallSession != null is " + (mCurrentCallSession != null));
  105. if (mCurrentCallSession != null) {
  106. // 停止响铃
  107. mCurrentCallSession.shouldStopRing();
  108. if (mCurrentCallSession.isComing()) {
  109. if (mCurrentCallSession.getState() == EnumType.CallState.Incoming) {
  110. // 接收到邀请,还没同意,发送拒绝
  111. mCurrentCallSession.sendRefuse();
  112. } else {
  113. // 已经接通,挂断电话
  114. mCurrentCallSession.leave();
  115. }
  116. } else {
  117. if (mCurrentCallSession.getState() == EnumType.CallState.Outgoing) {
  118. mCurrentCallSession.sendCancel();
  119. } else {
  120. // 已经接通,挂断电话
  121. mCurrentCallSession.leave();
  122. }
  123. }
  124. mCurrentCallSession.setCallState(EnumType.CallState.Idle);
  125. }
  126. }
  127. // 加入房间
  128. public void joinRoom(Context context, String room) {
  129. if (avEngineKit == null) {
  130. Log.e(TAG, "joinRoom error,init is not set");
  131. return;
  132. }
  133. // 忙线中
  134. if (mCurrentCallSession != null && mCurrentCallSession.getState() != EnumType.CallState.Idle) {
  135. Log.e(TAG, "joinRoom error,currentCallSession is exist");
  136. return;
  137. }
  138. mCurrentCallSession = new CallSession(context, room, false, mEvent);
  139. mCurrentCallSession.setIsComing(true);
  140. mCurrentCallSession.joinHome(room);
  141. }
  142. public void createAndJoinRoom(Context context, String room) {
  143. if (avEngineKit == null) {
  144. Log.e(TAG, "joinRoom error,init is not set");
  145. return;
  146. }
  147. // 忙线中
  148. if (mCurrentCallSession != null && mCurrentCallSession.getState() != EnumType.CallState.Idle) {
  149. Log.e(TAG, "joinRoom error,currentCallSession is exist");
  150. return;
  151. }
  152. mCurrentCallSession = new CallSession(context, room, false, mEvent);
  153. mCurrentCallSession.setIsComing(false);
  154. mCurrentCallSession.createHome(room, 9);
  155. }
  156. // 离开房间
  157. public void leaveRoom() {
  158. if (avEngineKit == null) {
  159. Log.e(TAG, "leaveRoom error,init is not set");
  160. return;
  161. }
  162. if (mCurrentCallSession != null) {
  163. mCurrentCallSession.leave();
  164. mCurrentCallSession.setCallState(EnumType.CallState.Idle);
  165. }
  166. }
  167. public void transferToAudio() {
  168. isAudioOnly = true;
  169. }
  170. public boolean isOutGoing() {
  171. return isOutGoing;
  172. }
  173. public boolean isAudioOnly() {
  174. return isAudioOnly;
  175. }
  176. // 获取对话实例
  177. public CallSession getCurrentSession() {
  178. return this.mCurrentCallSession;
  179. }
  180. }