telephone.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * telephone.h
  3. *
  4. * Created on: 2021年12月13日
  5. * Author: zkswe
  6. */
  7. #ifndef JNI_VOIP_TELEPHONE_H_
  8. #define JNI_VOIP_TELEPHONE_H_
  9. #include <string>
  10. #include <map>
  11. #include <functional>
  12. #include "acoustic_echo_cancellater.h"
  13. #include "audio_process_chain.h"
  14. namespace voip {
  15. enum State {
  16. STATE_CALL_CALLING, //正在拨号
  17. STATE_CALL_EARLY, //正在响铃
  18. STATE_CALL_INCOMING, //来电
  19. STATE_CALL_CONNECTING,//正在连接
  20. STATE_CALL_CONFIRMED, //接通
  21. STATE_CALL_DISCONNECTED, //结束
  22. };
  23. enum SIPStatusCode {
  24. SIP_STATUS_CODE_RINGING = 180,
  25. SIP_STATUS_CODE_PROGRESS = 183,
  26. SIP_STATUS_CODE_OK = 200,
  27. SIP_STATUS_CODE_TEMPORARILY_UNAVAILABLE = 480,
  28. SIP_STATUS_CODE_BUSY_HERE = 486,
  29. SIP_STATUS_CODE_DECLINE = 603,
  30. };
  31. enum SIPTranstorpType {
  32. SIP_TRANSPORT_UDP = 1,
  33. SIP_TRANSPORT_TCP,
  34. };
  35. struct RtcpStreamStat {
  36. /**
  37. * 上次更新时间
  38. */
  39. int64_t last_updated_at = 0;
  40. /**
  41. * 接收的包总数
  42. */
  43. int64_t number_of_packets = 0;
  44. };
  45. struct RtcpStat {
  46. RtcpStreamStat rx;
  47. };
  48. struct MediaStreamStat {
  49. RtcpStat audio;
  50. RtcpStat video;
  51. };
  52. enum DtmfMethod {
  53. /**
  54. * Send DTMF using RFC2833.
  55. */
  56. DTMF_METHOD_RFC2833,
  57. /**
  58. * Send DTMF using SIP INFO.
  59. */
  60. DTMF_METHOD_SIP_INFO
  61. };
  62. enum RingtonePolicy {
  63. /**
  64. * 默认的铃声策略,自动响铃
  65. */
  66. RINGTONE_POLICY_DEFAULT,
  67. /**
  68. * 没有任何铃声
  69. */
  70. RINGTONE_POLICY_NONE = 100,
  71. };
  72. struct CallInfo {
  73. /**
  74. * Call ID
  75. * 小于0无效
  76. */
  77. int id;
  78. /**
  79. * 当前状态
  80. */
  81. State state;
  82. /**
  83. * 对方地址
  84. */
  85. std::string remote_uri;
  86. /**
  87. * 对方联系人
  88. */
  89. std::string remote_contact;
  90. /**
  91. * 已通话时长,未接通时为0
  92. * 单位 毫秒
  93. */
  94. int duration;
  95. CallInfo();
  96. };
  97. typedef std::map<std::string, std::string> HeaderList;
  98. struct CallSetting {
  99. /**
  100. * 自定义 Contact display-name字段
  101. */
  102. std::string display_name;
  103. /**
  104. * 是否启用视频
  105. * 默认true
  106. */
  107. bool enable_video;
  108. /**
  109. * 自定义 Header
  110. */
  111. HeaderList headers;
  112. CallSetting();
  113. };
  114. struct Configuration {
  115. /**
  116. * sip:name@domain
  117. */
  118. std::string id_uri;
  119. /**
  120. * sip:domain:port
  121. */
  122. std::string server_uri;
  123. std::string user_name;
  124. std::string password;
  125. std::string user_agent;
  126. /**
  127. * 注册时自定义的Header
  128. */
  129. HeaderList headers;
  130. /**
  131. * 本地账号使用的端口号
  132. * 默认 5060
  133. */
  134. int local_port;
  135. /**
  136. * 是否启用本地账号
  137. * true 启用; false 关闭
  138. * 默认启用
  139. */
  140. bool enable_local_account;
  141. /**
  142. * 传输协议类型
  143. */
  144. SIPTranstorpType transtorp_type;
  145. /**
  146. * 通话时,自动发送演示视频画面到对方
  147. * 默认 false
  148. */
  149. bool auto_transmit_video_to_remote;
  150. /**
  151. * 启用ICE
  152. */
  153. bool enable_ice;
  154. std::string turn_server;
  155. std::string stun_server;
  156. std::string outbound_proxy;
  157. Configuration();
  158. };
  159. class Telephone {
  160. public:
  161. Telephone(const Configuration& conf);
  162. virtual ~Telephone();
  163. /**
  164. * 拨号
  165. */
  166. int MakeCall(const std::string& uri);
  167. /**
  168. * 拨号
  169. */
  170. int MakeCall(const std::string& uri, const CallSetting& setting);
  171. /**
  172. * 接听
  173. */
  174. int Answer();
  175. /**
  176. * 接听
  177. *
  178. * status_code (100~699)
  179. */
  180. int Answer(int call_id, int status_code);
  181. /**
  182. * 挂断
  183. */
  184. int Hangup();
  185. /**
  186. * 挂断
  187. *
  188. * status_code (100~699)
  189. */
  190. int Hangup(int call_id, int status_code);
  191. /**
  192. * 获得通话信息
  193. */
  194. CallInfo GetCallInfo();
  195. /**
  196. * 获得指定通话信息
  197. */
  198. CallInfo GetCallInfo(int call_id);
  199. /**
  200. * 获得当前电话数量
  201. * @return
  202. */
  203. int GetCallCount();
  204. typedef void (*CallStateListener)(Telephone*, int call_id, State state);
  205. /**
  206. * 监听电话状态
  207. */
  208. void AddCallStateListener(CallStateListener listener);
  209. void RemoveCallStateListener(CallStateListener listener);
  210. typedef void (*RegisteredStateListener)(Telephone* telephone, int code);
  211. void AddRegisteredStateListener(RegisteredStateListener listener);
  212. void RemoveRegisteredStateListener(RegisteredStateListener listener);
  213. /**
  214. * 获取SIP注册状态码
  215. * @return
  216. */
  217. int GetRegistrationStatusCode();
  218. /**
  219. * 设置视频显示位置 (暂未实现)
  220. * 默认全屏显示
  221. * 若要显示视频,需要创建一个充满屏幕的视频控件,可参考call.ftu的配置
  222. */
  223. void SetVideoRect(int x, int y, int w, int h);
  224. /**
  225. * 发送即时消息,忽略对方应答
  226. * @param to
  227. * @param target_uri
  228. * @param text
  229. * @return 如果target_uri为空,则发送到to。正常发送返回0
  230. */
  231. int SendInstantMessage(const std::string& to,
  232. const std::string& target_uri, const std::string& text);
  233. /**
  234. * 发送即时消息,并等待对方应答
  235. * @param to
  236. * @param target_uri 如果target_uri为空,则发送到to
  237. * @param content 发送的内容
  238. * @param timeout 等待响应的毫秒数,最大30000ms
  239. * @return 状态码, 常规情况下,成功应答返回200,未收到应答返回0或者408,
  240. * 其他值的含义参考sip协议
  241. */
  242. int SendInstantMessageAndWait(const std::string& to,
  243. const std::string& target_uri, const std::string& content, int timeout);
  244. /**
  245. * 使用 SIP INFO 方式 发送 DTMF
  246. * @param ch
  247. * @return
  248. */
  249. int SendDtmf(const std::string& ch);
  250. /**
  251. * 使用指定方式发送DTMF
  252. * @param method 发送DTMF的方式
  253. * @param ch 发送内容
  254. * @return 0 成功
  255. */
  256. int SendDtmf(DtmfMethod method, const std::string& ch);
  257. int SendMessageRequest(const std::string& from,
  258. const std::string& to,
  259. const std::string& text);
  260. /**
  261. * 设置铃声策略
  262. * @param policy
  263. */
  264. void SetRingtonePolicy(RingtonePolicy policy);
  265. /**
  266. * 获取媒体流状态
  267. * @param call_id
  268. * @param stat
  269. * @return
  270. */
  271. int GetMediaStreamStat(int call_id, MediaStreamStat* stat);
  272. /**
  273. * 设置录音的消回音、音频处理链路参数
  274. * 通话时设置无效,设置成功后,下次通话生效
  275. * @param conf
  276. */
  277. int SetInputAecApcConfig(
  278. const base::AcousticEchoCancellater::Configuration& aec,
  279. const base::AudioProcessChain::Configuration& apc);
  280. /**
  281. * 设置经过aec、apc处理后的声音的增益
  282. * @param size 增益倍数1~10
  283. */
  284. int SetInputPcmGainSize(float size);
  285. private:
  286. class Impl;
  287. Impl* impl_;
  288. };
  289. } /* namespace voip */
  290. #endif /* JNI_VOIP_TELEPHONE_H_ */