utilities.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * CallingAPI.cpp
  3. *
  4. * Created on: 2021年12月10日
  5. * Author: Admin
  6. */
  7. #include "utilities.h"
  8. #include "utils/Log.h"
  9. #include "string.h"
  10. #include "sip_config.h"
  11. #include "storage/StoragePreferences.h"
  12. #include "sip_config.h"
  13. #include <entry/EasyUIContext.h>
  14. #include <mutex>
  15. #include "base/strings.hpp"
  16. #include "base/base.hpp"
  17. #include "utilities.h"
  18. #include "base/time.hpp"
  19. #include "base/strings.hpp"
  20. #include "base/prefs.hpp"
  21. #include "base/json_object.h"
  22. #include "edge/ip_table.h"
  23. #include "core/jhws.h"
  24. #include "manager/ConfigManager.h"
  25. #include "security/SecurityManager.h"
  26. #include "zkaudio.h"
  27. #include "voip/telephone_audio_initializer.h"
  28. /**
  29. * 仅用于测试,默认不发送视频
  30. */
  31. void TelephoneAutoTransmitVideo(bool transmit);
  32. std::string GetDeviceUid();
  33. static std::mutex telephone_mutex;
  34. static voip::Telephone* telephone = NULL;
  35. voip::Telephone* GetTelephone() {
  36. std::lock_guard<std::mutex> lock(telephone_mutex);
  37. if (telephone != NULL) {
  38. return telephone;
  39. }
  40. voip::Configuration conf;
  41. #if 0
  42. std::string password = StoragePreferences::getString(SIP_REG_PASSWORD,
  43. SIP_REG_PASSWORD_DEFAULT);
  44. conf.id_uri = "sip:RM-1-1-1-1-1-1@jhws.top;transport=tcp";
  45. // conf.id_uri = "sip:RM-414-1-1-1-1-7@test.jhws.top";
  46. conf.server_uri = "sip:test.jhws.top";
  47. // conf.door_uri = "sip:OD-414-1-1-1-0-2@jhws.top";
  48. conf.user_name = "RM-1-1-1-1-1-1";
  49. conf.password = password;
  50. conf.port = 58583;
  51. #else
  52. std::string domain = StoragePreferences::getString(SIP_REG_DOMAIN,
  53. SIP_REG_DOMAIN_DEFAULT);
  54. std::string number = StoragePreferences::getString(SIP_REG_ACCOUNT,
  55. SIP_REG_ACCOUNT_DEFAULT);
  56. std::string password = StoragePreferences::getString(SIP_REG_PASSWORD,
  57. SIP_REG_PASSWORD_DEFAULT);
  58. int port = StoragePreferences::getInt(SIP_REG_PORT, SIP_REG_PORT_DEFAULT);
  59. // conf.id_uri = URI(number);
  60. conf.id_uri = URI(number, domain);
  61. conf.server_uri = base::format("sip:%s:%d", domain.c_str(), port);
  62. // conf.server_uri = "sip:new-sip.jhws.top:58583";
  63. conf.user_name = number;
  64. conf.password = password;
  65. conf.enable_ice = StoragePreferences::getBool(SIP_ENABLE_ICE, false);
  66. std::string stun_server =
  67. base::trim(StoragePreferences::getString(SIP_STUN_TURN_SERVER, ""));
  68. conf.stun_server = stun_server;
  69. conf.turn_server = stun_server;
  70. // conf.user_agent = "JHCloud-linux-i1.1.0";
  71. // conf.headers["SID"] = GetDeviceUid();
  72. // conf.headers["CID"] = "jhylf14988-com.jhws";
  73. // conf.headers["JWT"] = StoragePreferences::getString(SIP_JHWS_TOKEN, "");
  74. // LOGD("deviceuid: %s", GetDeviceUid().c_str());
  75. // LOGD("id_url: %s", conf.id_uri.c_str());
  76. // LOGD("server_url: %s", conf.server_uri.c_str());
  77. // LOGD("user_name: %s", conf.user_name.c_str());
  78. // LOGD("password: %s", conf.password.c_str());
  79. // LOGD("user_agent: %s", conf.user_agent.c_str());
  80. #endif
  81. bool enable_video = StoragePreferences::getBool(SIP_ENABLE_VIDEO, true);
  82. conf.auto_transmit_video_to_remote = enable_video;
  83. //单实例
  84. telephone = new voip::Telephone(conf);
  85. #if 0
  86. //设置消回音、增益等参数
  87. base::AcousticEchoCancellater::Configuration aec_conf;
  88. aec_conf.sample_rate = 8000; //固定8K
  89. aec_conf.nearend_channel = 1;
  90. aec_conf.farend_channel = 1;
  91. aec_conf.comfort_noise_enable = 0;
  92. base::AudioProcessChain::Configuration apc_conf;
  93. apc_conf.number_of_channels = 1;
  94. apc_conf.sample_rate = 8000; //固定8K
  95. apc_conf.anr.enable = 1; //降低噪音
  96. // apc_conf.eq.enable = 1;
  97. apc_conf.agc.enable = 1;
  98. for (int i = 0; i < array_length(apc_conf.eq.gain_db); ++i) {
  99. //调节EQ,使发送给对方的声音音量更大
  100. // apc_conf.eq.gain_db[i] = 10;
  101. }
  102. zk_audio_input_set_volume_native(0, 0);
  103. zk_audio_input_set_volume_native(1, 12);
  104. zk_audio_output_set_volume_native(15);
  105. // telephone->SetInputAecApcConfig(aec_conf, apc_conf);
  106. #endif
  107. #if 1
  108. const int kSampleRate = 8000;//固定8K
  109. //设置消回音等参数
  110. base::AcousticEchoCancellater::Configuration aec_conf;
  111. aec_conf.sample_rate = kSampleRate; //固定8K
  112. aec_conf.nearend_channel = 1;
  113. aec_conf.farend_channel = 1;
  114. {
  115. aec_conf.comfort_noise_enable = 0;
  116. unsigned int suppression_mode_intensity[7] = { 5, 5, 5, 5, 4, 4, 4 };
  117. memcpy((void*)aec_conf.suppression_mode_intensity, (void*)suppression_mode_intensity,
  118. (int) sizeof(suppression_mode_intensity));
  119. }
  120. //进一步处理录音数据
  121. base::AudioProcessChain::Configuration apc_conf;
  122. apc_conf.number_of_channels = 1;
  123. apc_conf.sample_rate = kSampleRate;
  124. {
  125. apc_conf.anr.enable = 1; //降低噪音
  126. for (int i = 0; i < array_length(apc_conf.anr.intensity); ++i) {
  127. apc_conf.anr.intensity[i] = 5;
  128. }
  129. }
  130. {
  131. apc_conf.eq.enable = 0;//增大音量
  132. for(int i = 0; i < array_length(apc_conf.eq.gain_db); ++i) {
  133. apc_conf.eq.gain_db[i] = 8;
  134. }
  135. }
  136. {
  137. apc_conf.agc.enable = 0;
  138. short def_compression_ratio_input[7] = { -65, -55, -48, -25, -18, -12, 0 };
  139. short def_compression_ratio_output[7] = { -65, -50, -30, -15, -8, -2, 0 };
  140. memcpy(apc_conf.agc.compression_ratio_input, def_compression_ratio_input,
  141. (int) sizeof(def_compression_ratio_input));
  142. memcpy(apc_conf.agc.compression_ratio_output, def_compression_ratio_output,
  143. (int) sizeof(def_compression_ratio_output));
  144. }
  145. telephone->SetInputPcmGainSize(10.0);
  146. telephone->SetInputAecApcConfig(aec_conf, apc_conf);
  147. #else
  148. //也可以从配置文件中设置 Aec、 Apc、 输入输出音量
  149. const std::string filename =
  150. CONFIGMANAGER->getResFilePath("telephone_audio_config.json");
  151. TelephoneAudioInitializer::Setup(telephone, filename);
  152. #endif
  153. return telephone;
  154. }
  155. void ReleaseTelephone() {
  156. std::lock_guard<std::mutex> lock(telephone_mutex);
  157. if (telephone != NULL) {
  158. deleteptr(&telephone);
  159. }
  160. }
  161. std::string GetVersion(){
  162. return "1.0.0";
  163. }
  164. int GetVersionNo() {
  165. return 3;
  166. }
  167. std::string GetDeviceUid() {
  168. unsigned char devID[16];
  169. int ret = SECURITYMANAGER->getDevID(devID);
  170. if (ret <= 0) {
  171. return "";
  172. }
  173. std::string str;
  174. for (int i = 0; i < ret; ++i) {
  175. str += base::format("%02X", devID[i]);
  176. }
  177. return str;
  178. }