sip.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. // in Asterisk pjsip.conf, under 1001, set direct_media=no
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <math.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "esp_wifi.h"
  10. #include "esp_event.h"
  11. #include "esp_log.h"
  12. #include "nvs_flash.h"
  13. #include "lwip/sockets.h"
  14. #include <stdbool.h>
  15. #include "esp_rom_md5.h"
  16. #include "bsp_board.h"
  17. #include "tca9555_driver.h"
  18. /*
  19. #include "esp_audio_enc_default.h"
  20. #include "esp_audio_enc.h"
  21. #include "esp_audio_dec_default.h"
  22. #include "esp_audio_dec.h"
  23. #include "esp_g711_enc.h"
  24. #include "esp_g711_dec.h"*/
  25. #define ESP32_IP "192.168.68.67"
  26. #define RTP_PORT 4000
  27. #define WIFI_SSID "TOCHTECH"
  28. #define WIFI_PASS "Smarturns2017"
  29. #define ASTERISK_IP "192.168.68.118"
  30. #define SIP_PORT 5060
  31. #define SIP_LOCAL_IP "0.0.0.0"
  32. #define SIP_LOCAL_PORT 5062
  33. #define SIP_USER "1001"
  34. #define SIP_PASSWORD "secret123"
  35. #define SAMPLE_RATE 8000
  36. #define SAMPLES_PER_PACKET 160 // 20 ms
  37. #define TONE_FREQ 1000
  38. #define MIC_FRAME_SAMPLES 320
  39. #define AEC_MAX_FRAME_SAMPLES 512
  40. #define FAR_END_FIFO_SAMPLES 2048
  41. static int32_t mic_buffer[320];
  42. static int16_t mic_pcm16[SAMPLES_PER_PACKET];
  43. static uint8_t payload[SAMPLES_PER_PACKET];
  44. static uint8_t rx_packet[172];
  45. static int16_t speaker_pcm[160];
  46. static const char *TAG = "SIP";
  47. volatile bool wifi_ready = false;
  48. static int s_rtp_sock = -1;
  49. static struct sockaddr_in s_rtp_peer = {0};
  50. static uint16_t s_rtp_peer_port = 0;
  51. static volatile bool call_active = false;
  52. static volatile bool s_audio_hw_ready = false;
  53. static volatile bool rtp_tasks_running = false;
  54. static volatile bool rtp_tx_done = true;
  55. static int16_t far_end_fifo[FAR_END_FIFO_SAMPLES];
  56. static size_t far_end_read = 0;
  57. static size_t far_end_write = 0;
  58. static portMUX_TYPE far_end_mux = portMUX_INITIALIZER_UNLOCKED;
  59. static int16_t aec_mic_pending[AEC_MAX_FRAME_SAMPLES];
  60. static int16_t aec_ref_pending[AEC_MAX_FRAME_SAMPLES];
  61. static size_t aec_pending_samples = 0;
  62. static int16_t aec_output_fifo[FAR_END_FIFO_SAMPLES];
  63. static size_t aec_output_samples = 0;
  64. esp_err_t esp_get_feed_data(
  65. bool is_get_raw_channel,
  66. int16_t *buffer,
  67. int buffer_len);
  68. esp_err_t esp_audio_play(const int16_t *data,
  69. int length,
  70. uint32_t ticks_to_wait);
  71. esp_err_t sip_media_init(void);
  72. static void rtp_tx_task(void *pvParameters);
  73. static void rtp_rx_task(void *pvParameters);
  74. static void far_end_push(const int16_t *samples, size_t count)
  75. {
  76. portENTER_CRITICAL(&far_end_mux);
  77. for (size_t i = 0; i < count; ++i) {
  78. size_t next = (far_end_write + 1) % FAR_END_FIFO_SAMPLES;
  79. if (next == far_end_read) {
  80. far_end_read = (far_end_read + 1) % FAR_END_FIFO_SAMPLES;
  81. }
  82. far_end_fifo[far_end_write] = samples[i];
  83. far_end_write = next;
  84. }
  85. portEXIT_CRITICAL(&far_end_mux);
  86. }
  87. static void far_end_pop(int16_t *samples, size_t count)
  88. {
  89. portENTER_CRITICAL(&far_end_mux);
  90. for (size_t i = 0; i < count; ++i) {
  91. if (far_end_read == far_end_write) {
  92. samples[i] = 0;
  93. } else {
  94. samples[i] = far_end_fifo[far_end_read];
  95. far_end_read = (far_end_read + 1) % FAR_END_FIFO_SAMPLES;
  96. }
  97. }
  98. portEXIT_CRITICAL(&far_end_mux);
  99. }
  100. static bool aec_process_capture(const int16_t *near_end,
  101. const int16_t *far_end,
  102. size_t sample_count)
  103. {
  104. int frame_size = bsp_aec_get_frame_size();
  105. if (frame_size <= 0 || frame_size > AEC_MAX_FRAME_SAMPLES) {
  106. return false;
  107. }
  108. for (size_t i = 0; i < sample_count; ++i) {
  109. if (aec_pending_samples == AEC_MAX_FRAME_SAMPLES) {
  110. return false;
  111. }
  112. aec_mic_pending[aec_pending_samples] = near_end[i];
  113. aec_ref_pending[aec_pending_samples] = far_end[i];
  114. ++aec_pending_samples;
  115. if (aec_pending_samples == (size_t)frame_size) {
  116. int16_t processed[AEC_MAX_FRAME_SAMPLES];
  117. if (bsp_aec_process_frame(aec_mic_pending,
  118. aec_ref_pending,
  119. processed) != ESP_OK) {
  120. return false;
  121. }
  122. /* AEC runs at 16 kHz. The negotiated PCMU stream is 8 kHz,
  123. * so retain every second cleaned sample for RTP encoding. */
  124. for (int sample = 0; sample < frame_size; sample += 2) {
  125. if (aec_output_samples < FAR_END_FIFO_SAMPLES) {
  126. aec_output_fifo[aec_output_samples++] = processed[sample];
  127. }
  128. }
  129. aec_pending_samples = 0;
  130. }
  131. }
  132. return true;
  133. }
  134. static bool aec_output_pop(int16_t *samples, size_t count)
  135. {
  136. if (aec_output_samples < count) {
  137. return false;
  138. }
  139. memcpy(samples, aec_output_fifo, count * sizeof(int16_t));
  140. memmove(aec_output_fifo,
  141. aec_output_fifo + count,
  142. (aec_output_samples - count) * sizeof(int16_t));
  143. aec_output_samples -= count;
  144. return true;
  145. }
  146. static bool rtp_socket_open(void)
  147. {
  148. if (s_rtp_sock >= 0) {
  149. return true;
  150. }
  151. s_rtp_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  152. if (s_rtp_sock < 0) {
  153. ESP_LOGE(TAG, "RTP socket failed errno=%d", errno);
  154. return false;
  155. }
  156. struct sockaddr_in local_rtp = {0};
  157. local_rtp.sin_family = AF_INET;
  158. local_rtp.sin_port = htons(RTP_PORT);
  159. local_rtp.sin_addr.s_addr = INADDR_ANY;
  160. if (bind(s_rtp_sock, (struct sockaddr *)&local_rtp, sizeof(local_rtp)) < 0) {
  161. ESP_LOGE(TAG, "RTP bind failed errno=%d", errno);
  162. close(s_rtp_sock);
  163. s_rtp_sock = -1;
  164. return false;
  165. }
  166. struct timeval timeout = {
  167. .tv_sec = 0,
  168. .tv_usec = 100000
  169. };
  170. if (setsockopt(s_rtp_sock, SOL_SOCKET, SO_RCVTIMEO,
  171. &timeout, sizeof(timeout)) < 0) {
  172. ESP_LOGE(TAG, "RTP receive timeout failed errno=%d", errno);
  173. close(s_rtp_sock);
  174. s_rtp_sock = -1;
  175. return false;
  176. }
  177. ESP_LOGI(TAG, "RTP socket %d listening on port %d",
  178. s_rtp_sock, RTP_PORT);
  179. return true;
  180. }
  181. /* ================= WIFI ================= */
  182. static void wifi_event_handler(void *arg, esp_event_base_t event_base,
  183. int32_t event_id, void *event_data)
  184. {
  185. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  186. esp_wifi_connect();
  187. }
  188. else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  189. ESP_LOGI(TAG, "WiFi disconnected, retrying...");
  190. wifi_ready = false;
  191. esp_wifi_connect();
  192. }
  193. else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  194. ESP_LOGI(TAG, "WiFi connected!");
  195. wifi_ready = true;
  196. }
  197. esp_netif_ip_info_t ip;
  198. esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip);
  199. ESP_LOGI(TAG, "ESP32 IP: " IPSTR, IP2STR(&ip.ip));
  200. }
  201. void wifi_init(void)
  202. {
  203. nvs_flash_init();
  204. esp_netif_init();
  205. esp_event_loop_create_default();
  206. esp_netif_create_default_wifi_sta();
  207. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  208. esp_wifi_init(&cfg);
  209. esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL);
  210. esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL);
  211. wifi_config_t wifi_config = {
  212. .sta = {
  213. .ssid = WIFI_SSID,
  214. .password = WIFI_PASS,
  215. },
  216. };
  217. esp_wifi_set_mode(WIFI_MODE_STA);
  218. esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
  219. esp_wifi_start();
  220. }
  221. /* ================= MD5 HELPERS ================= */
  222. static void md5_calc(const unsigned char *input, size_t len, unsigned char output[16])
  223. {
  224. md5_context_t ctx;
  225. esp_rom_md5_init(&ctx);
  226. esp_rom_md5_update(&ctx, input, len);
  227. esp_rom_md5_final(output, &ctx);
  228. }
  229. static void md5_to_hex(unsigned char *md5, char *out)
  230. {
  231. for (int i = 0; i < 16; i++) {
  232. sprintf(out + i * 2, "%02x", md5[i]);
  233. }
  234. }
  235. /* ================= SIP DIGEST ================= */
  236. void sip_compute_response(
  237. const char *username,
  238. const char *realm,
  239. const char *password,
  240. const char *nonce,
  241. const char *uri,
  242. char *out_response)
  243. {
  244. unsigned char ha1_md5[16], ha2_md5[16], final_md5[16];
  245. char ha1_hex[64], ha2_hex[64], final_str[256];
  246. char ha1[128], ha2[128];
  247. snprintf(ha1, sizeof(ha1), "%s:%s:%s", username, realm, password);
  248. md5_calc((unsigned char*)ha1, strlen(ha1), ha1_md5);
  249. md5_to_hex(ha1_md5, ha1_hex);
  250. snprintf(ha2, sizeof(ha2), "REGISTER:%s", uri);
  251. md5_calc((unsigned char*)ha2, strlen(ha2), ha2_md5);
  252. md5_to_hex(ha2_md5, ha2_hex);
  253. snprintf(final_str, sizeof(final_str),
  254. "%s:%s:%s", ha1_hex, nonce, ha2_hex);
  255. md5_calc((unsigned char*)final_str, strlen(final_str), final_md5);
  256. md5_to_hex(final_md5, out_response);
  257. }
  258. /* ================= SIP HELPERS ================= */
  259. static bool sip_extract_header(const char *msg, const char *header, char *out, size_t out_len)
  260. {
  261. const char *start = strstr(msg, header);
  262. if (!start) {
  263. return false;
  264. }
  265. start += strlen(header);
  266. while (*start == ' ' || *start == '\t') {
  267. start++;
  268. }
  269. const char *end = start;
  270. while (*end != '\0' && *end != '\r' && *end != '\n') {
  271. end++;
  272. }
  273. size_t len = (size_t)(end - start);
  274. if (len >= out_len) {
  275. len = out_len - 1;
  276. }
  277. memcpy(out, start, len);
  278. out[len] = '\0';
  279. return true;
  280. }
  281. static void sip_send_200_ok(int sock, const struct sockaddr_in *remote, const char *sip_msg,
  282. const char *sdp_body, size_t sdp_len)
  283. {
  284. char via[512] = {0};
  285. char from[256] = {0};
  286. char to[256] = {0};
  287. char callid[256] = {0};
  288. char cseq[128] = {0};
  289. char response[4096];
  290. if (!sip_extract_header(sip_msg, "Via:", via, sizeof(via))) {
  291. snprintf(via, sizeof(via), "SIP/2.0/UDP %s:%d", SIP_LOCAL_IP, SIP_LOCAL_PORT);
  292. }
  293. if (!sip_extract_header(sip_msg, "From:", from, sizeof(from))) {
  294. snprintf(from, sizeof(from), "<sip:%s@%s>", SIP_USER, ASTERISK_IP);
  295. }
  296. if (!sip_extract_header(sip_msg, "To:", to, sizeof(to))) {
  297. snprintf(to, sizeof(to), "<sip:%s@%s>", SIP_USER, ASTERISK_IP);
  298. }
  299. if (!sip_extract_header(sip_msg, "Call-ID:", callid, sizeof(callid))) {
  300. snprintf(callid, sizeof(callid), "esp32-call");
  301. }
  302. if (!sip_extract_header(sip_msg, "CSeq:", cseq, sizeof(cseq))) {
  303. snprintf(cseq, sizeof(cseq), "1 REGISTER");
  304. }
  305. int body_len = sdp_body ? (int)sdp_len : 0;
  306. int written = snprintf(response, sizeof(response),
  307. "SIP/2.0 200 OK\r\n"
  308. "Via: %s\r\n"
  309. "From: %s\r\n"
  310. "To: %s;tag=esp32\r\n"
  311. "Call-ID: %s\r\n"
  312. "CSeq: %s\r\n"
  313. "Contact: <sip:%s@%s:%d>\r\n"
  314. "%s"
  315. "Content-Length: %d\r\n"
  316. "\r\n"
  317. "%s",
  318. via,
  319. from,
  320. to,
  321. callid,
  322. cseq,
  323. SIP_USER,
  324. ESP32_IP,
  325. SIP_LOCAL_PORT,
  326. sdp_body ? "Content-Type: application/sdp\r\n" : "",
  327. body_len,
  328. sdp_body ? sdp_body : "");
  329. if (written < 0 || written >= (int)sizeof(response)) {
  330. ESP_LOGW(TAG, "SIP 200 OK response too large");
  331. return;
  332. }
  333. sendto(sock, response, (size_t)written, 0,
  334. (struct sockaddr *)remote, sizeof(*remote));
  335. }
  336. static void sip_build_sdp(char *out, size_t out_len)
  337. {
  338. snprintf(out, out_len,
  339. "v=0\r\n"
  340. "o=ESP32 1234 1234 IN IP4 %s\r\n"
  341. "s=ESP32 SIP Call\r\n"
  342. "c=IN IP4 %s\r\n"
  343. "t=0 0\r\n"
  344. "m=audio %d RTP/AVP 0\r\n"
  345. "a=rtpmap:0 PCMU/8000\r\n"
  346. "a=ptime:20\r\n"
  347. "a=sendrecv\r\n",
  348. ESP32_IP,
  349. ESP32_IP,
  350. RTP_PORT);
  351. }
  352. static bool sip_extract_rtp_port(const char *msg, uint16_t *out_port)
  353. {
  354. const char *m = strstr(msg, "m=audio ");
  355. if (!m) {
  356. return false;
  357. }
  358. m += strlen("m=audio ");
  359. char port_buf[16] = {0};
  360. size_t i = 0;
  361. while (*m && *m != ' ' && *m != '\r' && *m != '\n' && i < sizeof(port_buf) - 1) {
  362. port_buf[i++] = *m++;
  363. }
  364. port_buf[i] = '\0';
  365. *out_port = (uint16_t)atoi(port_buf);
  366. return *out_port != 0;
  367. }
  368. /* ================= RTP TX ================= */
  369. static bool microphone_read(int16_t *samples, size_t count)
  370. {
  371. // ESP_LOGI(TAG,
  372. // "mic ptr=%p samples=%d bytes=%d",
  373. // samples,
  374. // count,
  375. // count * sizeof(int16_t));
  376. esp_err_t ret = esp_get_feed_data(false, samples, count);
  377. if(ret != ESP_OK)
  378. {
  379. ESP_LOGE(TAG,
  380. "esp_get_feed_data failed: %s",
  381. esp_err_to_name(ret));
  382. return false;
  383. }
  384. return true;
  385. }
  386. static uint8_t linear2ulaw(int16_t pcm)
  387. {
  388. const int16_t BIAS = 0x84;
  389. const int16_t CLIP = 32635;
  390. uint8_t mask;
  391. uint8_t seg;
  392. uint8_t uval;
  393. int16_t sample = pcm;
  394. if (sample < 0)
  395. {
  396. sample = -sample;
  397. mask = 0x7F;
  398. }
  399. else
  400. {
  401. mask = 0xFF;
  402. }
  403. if (sample > CLIP)
  404. sample = CLIP;
  405. sample += BIAS;
  406. if (sample <= 0xFF)
  407. seg = 0;
  408. else if (sample <= 0x1FF)
  409. seg = 1;
  410. else if (sample <= 0x3FF)
  411. seg = 2;
  412. else if (sample <= 0x7FF)
  413. seg = 3;
  414. else if (sample <= 0xFFF)
  415. seg = 4;
  416. else if (sample <= 0x1FFF)
  417. seg = 5;
  418. else if (sample <= 0x3FFF)
  419. seg = 6;
  420. else
  421. seg = 7;
  422. uval = (seg << 4) |
  423. ((sample >> (seg + 3)) & 0x0F);
  424. return uval ^ mask;
  425. }
  426. static int16_t ulaw2linear(uint8_t u_val)
  427. {
  428. u_val = ~u_val;
  429. int t = ((u_val & 0x0F) << 3) + 0x84;
  430. t <<= ((unsigned)u_val & 0x70) >> 4;
  431. if (u_val & 0x80)
  432. return 0x84 - t;
  433. else
  434. return t - 0x84;
  435. }
  436. static void sip_send_rtp_packet(void)
  437. {
  438. static uint32_t call_count = 0;
  439. /*
  440. ESP_LOGI(TAG,
  441. "sip_send_rtp_packet() #%lu",
  442. (unsigned long)call_count++);*/
  443. static uint16_t seq = 0;
  444. static uint32_t timestamp = 0;
  445. if(s_rtp_peer_port == 0)
  446. {
  447. ESP_LOGW(TAG,"No RTP peer");
  448. return;
  449. }
  450. if(s_rtp_peer_port == 0)
  451. {
  452. ESP_LOGW(TAG,
  453. "No RTP peer port set!");
  454. return;
  455. }
  456. uint8_t payload[SAMPLES_PER_PACKET];
  457. // Read one 20 ms stereo capture block (320 mono samples at 16 kHz).
  458. if (!microphone_read((int16_t *)mic_buffer, sizeof(mic_buffer)))
  459. {
  460. return;
  461. }
  462. int16_t *mic16 = (int16_t *)mic_buffer;
  463. int16_t near_end[MIC_FRAME_SAMPLES];
  464. int16_t far_end[MIC_FRAME_SAMPLES];
  465. for (int i = 0; i < MIC_FRAME_SAMPLES; ++i) {
  466. int32_t left = mic16[i * 2];
  467. int32_t right = mic16[i * 2 + 1];
  468. near_end[i] = (int16_t)((left + right) / 2);
  469. }
  470. far_end_pop(far_end, MIC_FRAME_SAMPLES);
  471. if (!aec_process_capture(near_end, far_end, MIC_FRAME_SAMPLES) ||
  472. !aec_output_pop(mic_pcm16, SAMPLES_PER_PACKET)) {
  473. return;
  474. }
  475. // Debug microphone level
  476. int max = 0;
  477. for(int i = 0; i < SAMPLES_PER_PACKET; i++)
  478. {
  479. int value = abs(mic_pcm16[i]);
  480. if(value > max)
  481. max = value;
  482. }
  483. /*
  484. ESP_LOGI("MIC", "max=%d", max);*/
  485. // PCM16 -> PCMU (G711 u-law)
  486. for(int i = 0; i < SAMPLES_PER_PACKET; i++)
  487. {
  488. payload[i] = linear2ulaw(mic_pcm16[i]);
  489. }
  490. //testing multiple packets
  491. /*
  492. ESP_LOGI("MIC",
  493. "%6d %6d %6d %6d %6d %6d %6d %6d "
  494. "%6d %6d %6d %6d %6d %6d %6d %6d "
  495. "%6d %6d %6d %6d",
  496. mic_pcm16[0], mic_pcm16[1], mic_pcm16[2], mic_pcm16[3],
  497. mic_pcm16[4], mic_pcm16[5], mic_pcm16[6], mic_pcm16[7],
  498. mic_pcm16[8], mic_pcm16[9], mic_pcm16[10], mic_pcm16[11],
  499. mic_pcm16[12], mic_pcm16[13], mic_pcm16[14], mic_pcm16[15],
  500. mic_pcm16[16], mic_pcm16[17], mic_pcm16[18], mic_pcm16[19]);
  501. */
  502. uint8_t packet[12 + SAMPLES_PER_PACKET];
  503. memset(packet,0,sizeof(packet));
  504. packet[0] = 0x80; // RTP version
  505. packet[1] = 0x00; // PCMU
  506. packet[2] = seq >> 8;
  507. packet[3] = seq & 0xff;
  508. packet[4] = timestamp >> 24;
  509. packet[5] = timestamp >> 16;
  510. packet[6] = timestamp >> 8;
  511. packet[7] = timestamp;
  512. uint32_t ssrc = 0x12345678;
  513. packet[8] = (ssrc >> 24) & 0xff;
  514. packet[9] = (ssrc >> 16) & 0xff;
  515. packet[10] = (ssrc >> 8) & 0xff;
  516. packet[11] = ssrc & 0xff;
  517. memcpy(packet+12,
  518. payload,
  519. SAMPLES_PER_PACKET);
  520. struct sockaddr_in peer = s_rtp_peer;
  521. peer.sin_port = htons(s_rtp_peer_port);
  522. /*ESP_LOGI(TAG,
  523. "RTP destination IP: %s",
  524. inet_ntoa(peer.sin_addr));
  525. ESP_LOGI(TAG,
  526. "Sending RTP to %s:%d",
  527. inet_ntoa(peer.sin_addr),
  528. ntohs(peer.sin_port));*/
  529. esp_netif_ip_info_t ip;
  530. esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
  531. esp_netif_get_ip_info(netif, &ip);
  532. /*ESP_LOGI(TAG, "Current IP: " IPSTR, IP2STR(&ip.ip));*/
  533. int ret =
  534. sendto(
  535. s_rtp_sock,
  536. packet,
  537. sizeof(packet),
  538. 0,
  539. (struct sockaddr *)&peer,
  540. sizeof(peer));
  541. if(ret < 0)
  542. {
  543. ESP_LOGE(TAG,
  544. "RTP send failed errno=%d",
  545. errno);
  546. }
  547. else
  548. {
  549. /* ESP_LOGI(TAG,
  550. "RTP sent bytes=%d seq=%d",
  551. ret,
  552. seq);*/
  553. }
  554. /*if(ret > 0)
  555. {
  556. ESP_LOGI(TAG,
  557. "RTP sent seq=%d",
  558. seq);
  559. }*/
  560. seq++;
  561. timestamp += SAMPLES_PER_PACKET;
  562. }
  563. static void rtp_tx_task(void *pvParameters)
  564. {
  565. ESP_LOGI(TAG, "RTP TX started");
  566. TickType_t last_wake = xTaskGetTickCount();
  567. while(call_active)
  568. {/*ESP_LOGI(TAG, "RTP loop");*/
  569. sip_send_rtp_packet();
  570. vTaskDelayUntil(
  571. &last_wake,
  572. pdMS_TO_TICKS(20)
  573. );
  574. }
  575. ESP_LOGI(TAG, "RTP TX stopped");
  576. rtp_tx_done = true;
  577. vTaskDelete(NULL);
  578. }
  579. static void rtp_rx_task(void *pvParameters)
  580. {
  581. ESP_LOGI(TAG, "RTP RX started");
  582. while (call_active)
  583. {
  584. struct sockaddr_in src;
  585. socklen_t len = sizeof(src);
  586. int n = recvfrom(
  587. s_rtp_sock,
  588. rx_packet,
  589. sizeof(rx_packet),
  590. 0,
  591. (struct sockaddr *)&src,
  592. &len);//
  593. if (n < 0)
  594. {
  595. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  596. continue;
  597. }
  598. ESP_LOGE(TAG, "recvfrom failed errno=%d", errno);
  599. call_active = false;
  600. break;
  601. }
  602. if (s_rtp_peer_port != 0 &&
  603. (src.sin_port != htons(s_rtp_peer_port) ||
  604. src.sin_addr.s_addr != s_rtp_peer.sin_addr.s_addr))
  605. {
  606. ESP_LOGW(TAG,
  607. "Ignoring RTP from unexpected peer %s:%d",
  608. inet_ntoa(src.sin_addr),
  609. ntohs(src.sin_port));
  610. continue;
  611. }
  612. /* Ignore packets that are clearly looped back to the local device. */
  613. esp_netif_ip_info_t local_ip;
  614. esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
  615. esp_netif_get_ip_info(netif, &local_ip);
  616. if (src.sin_addr.s_addr == local_ip.ip.addr)
  617. {
  618. ESP_LOGW(TAG, "Ignoring looped RTP packet from local IP");
  619. continue;
  620. }
  621. // ESP_LOGI(TAG, "Received RTP packet (%d bytes)", n);
  622. if (n <= 12)
  623. {
  624. ESP_LOGW(TAG, "Packet too small");
  625. continue;
  626. }
  627. int payload_size = n - 12;
  628. if (payload_size > SAMPLES_PER_PACKET)
  629. {
  630. payload_size = SAMPLES_PER_PACKET;
  631. }
  632. int16_t play_buf[640];
  633. int16_t far_end_ref[320];
  634. for (int i = 0; i < payload_size; i++)
  635. {
  636. int16_t s1 = ulaw2linear(rx_packet[12+i]);
  637. int16_t s2 = s1;
  638. if (i < payload_size - 1)
  639. {
  640. int16_t next = ulaw2linear(rx_packet[12+i+1]);
  641. s2 = (s1 + next) / 2;
  642. }
  643. // sample 1
  644. play_buf[4*i + 0] = s1; // Left
  645. play_buf[4*i + 1] = s1; // Right
  646. // interpolated sample
  647. play_buf[4*i + 2] = s2; // Left
  648. play_buf[4*i + 3] = s2; // Right
  649. far_end_ref[2 * i] = s1;
  650. far_end_ref[2 * i + 1] = s2;
  651. }
  652. // The reference must be the mono 16 kHz signal sent to the speaker,
  653. // before it is duplicated into the codec's stereo output.
  654. far_end_push(far_end_ref, payload_size * 2);
  655. esp_err_t ret = esp_audio_play(
  656. play_buf,
  657. payload_size * 4 * sizeof(int16_t),
  658. portMAX_DELAY);
  659. // ESP_LOGI(TAG,
  660. // "esp_audio_play() = %s",
  661. // esp_err_to_name(ret));
  662. // ESP_LOGI(TAG, "esp_audio_play() returned %s",
  663. // esp_err_to_name(ret));
  664. }
  665. while (!rtp_tx_done) {
  666. vTaskDelay(pdMS_TO_TICKS(10));
  667. }
  668. if (s_rtp_sock >= 0) {
  669. close(s_rtp_sock);
  670. s_rtp_sock = -1;
  671. }
  672. rtp_tasks_running = false;
  673. ESP_LOGI(TAG, "RTP RX stopped");
  674. vTaskDelete(NULL);
  675. }
  676. /* ================= SIP TASK ================= */
  677. void sip_register_task(void *pvParameters)
  678. {
  679. struct sockaddr_in server = {0};
  680. server.sin_family = AF_INET;
  681. server.sin_port = htons(SIP_PORT);
  682. server.sin_addr.s_addr = inet_addr(ASTERISK_IP);
  683. int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  684. struct sockaddr_in local_addr = {0};
  685. local_addr.sin_family = AF_INET;
  686. local_addr.sin_port = htons(SIP_LOCAL_PORT);
  687. local_addr.sin_addr.s_addr = INADDR_ANY;
  688. if (bind(sock,
  689. (struct sockaddr *)&local_addr,
  690. sizeof(local_addr)) < 0) {
  691. ESP_LOGE(TAG, "Failed to bind SIP port %d", SIP_LOCAL_PORT);
  692. close(sock);
  693. vTaskDelete(NULL);
  694. return;
  695. }
  696. ESP_LOGI(TAG, "SIP listening on UDP %d", SIP_LOCAL_PORT);
  697. if (sock < 0) {
  698. ESP_LOGE(TAG, "Socket failed");
  699. vTaskDelete(NULL);
  700. return;
  701. }
  702. struct timeval timeout = {
  703. .tv_sec = 300,
  704. .tv_usec = 0
  705. };
  706. setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
  707. /* ================= FIRST REGISTER ================= */
  708. char register_msg[512];
  709. snprintf(register_msg, sizeof(register_msg),
  710. "REGISTER sip:%s SIP/2.0\r\n"
  711. "Via: SIP/2.0/UDP %s:%d\r\n"
  712. "From: <sip:%s@%s>;tag=1234\r\n"
  713. "To: <sip:%s@%s>\r\n"
  714. "Call-ID: esp32-0001\r\n"
  715. "CSeq: 1 REGISTER\r\n"
  716. "Contact: <sip:%s@%s:%d>\r\n"
  717. "Expires: 300\r\n"
  718. "Content-Length: 0\r\n"
  719. "\r\n",
  720. ASTERISK_IP,
  721. SIP_LOCAL_IP,
  722. SIP_LOCAL_PORT,
  723. SIP_USER,
  724. ASTERISK_IP,
  725. SIP_USER,
  726. ASTERISK_IP,
  727. SIP_USER,
  728. SIP_LOCAL_IP,
  729. SIP_LOCAL_PORT);
  730. ESP_LOGI(TAG, "Sending REGISTER...");
  731. sendto(sock, register_msg, strlen(register_msg), 0,
  732. (struct sockaddr*)&server, sizeof(server));
  733. /* ================= RECEIVE 401 / 200 ================= */
  734. char response[1024];
  735. socklen_t addr_len = sizeof(server);
  736. bool registered = false;
  737. while (!registered) {
  738. int len = recvfrom(sock, response, sizeof(response) - 1, 0,
  739. (struct sockaddr *)&server, &addr_len);
  740. if (len <= 0) {
  741. ESP_LOGE(TAG, "No response");
  742. close(sock);
  743. vTaskDelete(NULL);
  744. return;
  745. }
  746. response[len] = '\0';
  747. // printf("SIP RESPONSE:\n%s\n", response);
  748. ESP_LOGI(TAG, "Received SIP response (%d bytes)", len);
  749. if (strstr(response, "SIP/2.0 401")) {
  750. char sip_nonce[256] = {0};
  751. char sip_realm[128] = {0};
  752. char *n = strstr(response, "nonce=\"");
  753. if (n) {
  754. n += 7;
  755. char *end = strchr(n, '"');
  756. if (end) {
  757. size_t nlen = end - n;
  758. if (nlen < sizeof(sip_nonce)) {
  759. strncpy(sip_nonce, n, nlen);
  760. sip_nonce[nlen] = '\0';
  761. }
  762. }
  763. }
  764. char *r = strstr(response, "realm=\"");
  765. if (r) {
  766. r += 7;
  767. char *rend = strchr(r, '"');
  768. if (rend) {
  769. size_t rlen = rend - r;
  770. if (rlen < sizeof(sip_realm)) {
  771. strncpy(sip_realm, r, rlen);
  772. sip_realm[rlen] = '\0';
  773. }
  774. }
  775. }
  776. if (sip_realm[0] == '\0' || sip_nonce[0] == '\0') {
  777. ESP_LOGE(TAG, "Missing realm or nonce in challenge");
  778. close(sock);
  779. vTaskDelete(NULL);
  780. return;
  781. }
  782. ESP_LOGI(TAG, "Nonce: %s", sip_nonce);
  783. ESP_LOGI(TAG, "Realm: %s", sip_realm);
  784. char response_hash[64];
  785. char sip_uri[64];
  786. snprintf(sip_uri, sizeof(sip_uri), "sip:%s", ASTERISK_IP);
  787. sip_compute_response(
  788. SIP_USER,
  789. sip_realm,
  790. SIP_PASSWORD,
  791. sip_nonce,
  792. sip_uri,
  793. response_hash
  794. );
  795. char auth[1024];
  796. snprintf(auth, sizeof(auth),
  797. "REGISTER sip:%s SIP/2.0\r\n"
  798. "Via: SIP/2.0/UDP %s:%d\r\n"
  799. "From: <sip:%s@%s>;tag=1234\r\n"
  800. "To: <sip:%s@%s>\r\n"
  801. "Call-ID: esp32-0001\r\n"
  802. "CSeq: 2 REGISTER\r\n"
  803. "Contact: <sip:%s@%s:%d>\r\n"
  804. "Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"sip:%s\", response=\"%s\"\r\n"
  805. "Expires: 300\r\n"
  806. "Content-Length: 0\r\n"
  807. "\r\n",
  808. ASTERISK_IP,
  809. SIP_LOCAL_IP,
  810. SIP_LOCAL_PORT,
  811. SIP_USER,
  812. ASTERISK_IP,
  813. SIP_USER,
  814. ASTERISK_IP,
  815. SIP_USER,
  816. SIP_LOCAL_IP,
  817. SIP_LOCAL_PORT,
  818. SIP_USER,
  819. sip_realm,
  820. sip_nonce,
  821. ASTERISK_IP,
  822. response_hash);
  823. ESP_LOGI(TAG, "Sending AUTH REGISTER...");
  824. sendto(sock, auth, strlen(auth), 0,
  825. (struct sockaddr *)&server, sizeof(server));
  826. }
  827. else if (strstr(response, "SIP/2.0 200")) {
  828. registered = true;
  829. }
  830. else if (strstr(response, "SIP/2.0 486")) {
  831. ESP_LOGW(TAG, "Server returned 486 Busy");
  832. }
  833. else {
  834. ESP_LOGI(TAG, "Ignoring SIP response");
  835. }
  836. }
  837. if (!registered) {
  838. ESP_LOGW(TAG, "Registration was not completed");
  839. close(sock);
  840. vTaskDelete(NULL);
  841. return;
  842. }
  843. ESP_LOGI(TAG, "🎉 SIP REGISTER SUCCESS");
  844. ESP_LOGI(TAG, "stack remaining: %u",
  845. uxTaskGetStackHighWaterMark(NULL));
  846. /* =====================================================
  847. * KEEP SOCKET OPEN AND WAIT FOR SIP REQUESTS
  848. * ===================================================== */
  849. ESP_LOGI(TAG, "Waiting for incoming SIP requests...");
  850. while (1) {
  851. char rx_buf[2048];
  852. int rx_len = recvfrom(sock,
  853. rx_buf,
  854. sizeof(rx_buf) - 1,
  855. 0,
  856. (struct sockaddr *)&server,
  857. &addr_len);
  858. if (rx_len <= 0) {
  859. continue;
  860. }
  861. rx_buf[rx_len] = '\0';
  862. ESP_LOGI(TAG, "\n===== RAW INVITE =====\n%s\n=======================\n", rx_buf);
  863. // printf("\n========================================\n");
  864. // printf("SIP MESSAGE RECEIVED:\n");
  865. // printf("%s\n", rx_buf);
  866. ESP_LOGI(TAG, "Received SIP packet (%d bytes)", rx_len);
  867. // printf("========================================\n");
  868. if (strncmp(rx_buf, "REGISTER ", 9) == 0) {
  869. ESP_LOGI(TAG, "Received REGISTER; sending 200 OK");
  870. sip_send_200_ok(sock, &server, rx_buf, NULL, 0);
  871. continue;
  872. }
  873. else if (strncmp(rx_buf, "INVITE ", 7) == 0) {
  874. ESP_LOGI(TAG, "📞 Incoming INVITE");
  875. char *test = strstr(rx_buf, "m=audio");
  876. if(test)
  877. {
  878. ESP_LOGI(TAG, "Found m=audio SDP:");
  879. ESP_LOGI(TAG, "%s", test);
  880. }
  881. else
  882. {
  883. ESP_LOGE(TAG, "No m=audio found");
  884. }
  885. /* ================= EXTRACT SIP HEADERS ================= */
  886. char via[512] = {0};
  887. char from[256] = {0};
  888. char to[256] = {0};
  889. char callid[256] = {0};
  890. char cseq[128] = {0};
  891. char *header = NULL;
  892. header = strstr(rx_buf, "Via:");
  893. if (header) {
  894. sscanf(header, "Via: %511[^\r\n]", via);
  895. }
  896. header = strstr(rx_buf, "From:");
  897. if (header) {
  898. sscanf(header, "From: %255[^\r\n]", from);
  899. }
  900. header = strstr(rx_buf, "To:");
  901. if (header) {
  902. sscanf(header, "To: %255[^\r\n]", to);
  903. }
  904. header = strstr(rx_buf, "Call-ID:");
  905. if (header) {
  906. sscanf(header, "Call-ID: %255[^\r\n]", callid);
  907. }
  908. header = strstr(rx_buf, "CSeq:");
  909. if (header) {
  910. sscanf(header, "CSeq: %127[^\r\n]", cseq);
  911. }
  912. /* ================= SEND 100 TRYING ================= */
  913. char trying[4096];
  914. snprintf(trying, sizeof(trying),
  915. "SIP/2.0 100 Trying\r\n"
  916. "Via: %s\r\n"
  917. "From: %s\r\n"
  918. "To: %s\r\n"
  919. "Call-ID: %s\r\n"
  920. "CSeq: %s\r\n"
  921. "Content-Length: 0\r\n"
  922. "\r\n",
  923. via,
  924. from,
  925. to,
  926. callid,
  927. cseq);
  928. sendto(sock,
  929. trying,
  930. strlen(trying),
  931. 0,
  932. (struct sockaddr *)&server,
  933. sizeof(server));
  934. ESP_LOGI(TAG, "100 Trying sent");
  935. ESP_LOGI(TAG, "stack remaining: %u",
  936. uxTaskGetStackHighWaterMark(NULL));
  937. char ok[4096];
  938. char sdp[1024];
  939. sip_build_sdp(sdp, sizeof(sdp));
  940. if (sip_extract_rtp_port(rx_buf, &s_rtp_peer_port)) {
  941. s_rtp_peer.sin_family = AF_INET;
  942. s_rtp_peer.sin_port = htons(s_rtp_peer_port);
  943. s_rtp_peer.sin_addr.s_addr = inet_addr(ASTERISK_IP);
  944. ESP_LOGI(TAG,
  945. "RTP peer port=%d",
  946. s_rtp_peer_port);
  947. }
  948. snprintf(ok, sizeof(ok),
  949. "SIP/2.0 200 OK\r\n"
  950. "Via: %s\r\n"
  951. "From: %s\r\n"
  952. "To: %s;esp32\r\n"
  953. "Call-ID: %s\r\n"
  954. "CSeq: %s\r\n"
  955. "Contact: <sip:%s@%s:%d>\r\n"
  956. "Content-Type: application/sdp\r\n"
  957. "Content-Length: %d\r\n"
  958. "\r\n"
  959. "%s",
  960. via,
  961. from,
  962. to,
  963. callid,
  964. cseq,
  965. SIP_USER,
  966. ESP32_IP,
  967. SIP_LOCAL_PORT,
  968. strlen(sdp),
  969. sdp);
  970. ESP_LOGI(TAG, "\n========== 200 OK ==========\n%s\n============================\n", ok);
  971. sendto(sock,
  972. ok,
  973. strlen(ok),
  974. 0,
  975. (struct sockaddr *)&server,
  976. sizeof(server));
  977. ESP_LOGI(TAG, "200 OK sent");
  978. }
  979. else if (strncmp(rx_buf, "OPTIONS ", 8) == 0) {
  980. ESP_LOGI(TAG, "Received OPTIONS");
  981. }
  982. else if (strncmp(rx_buf, "ACK ", 4) == 0)
  983. {
  984. ESP_LOGI(TAG, "Received ACK");
  985. uint8_t test_values[] = {0xFF, 0x7F, 0x00, 0x80};
  986. for(int i = 0; i < 4; i++)
  987. {
  988. ESP_LOGI(TAG,
  989. "ulaw %02X -> %d",
  990. test_values[i],
  991. ulaw2linear(test_values[i]));
  992. }
  993. if (!call_active && !rtp_tasks_running)
  994. {
  995. if (!rtp_socket_open()) {
  996. ESP_LOGE(TAG, "Cannot start RTP call without a socket");
  997. continue;
  998. }
  999. call_active = true;
  1000. rtp_tasks_running = true;
  1001. rtp_tx_done = false;
  1002. BaseType_t rx_ret = xTaskCreate(
  1003. rtp_rx_task,
  1004. "rtp_rx",
  1005. 16384,
  1006. NULL,
  1007. 5,
  1008. NULL);
  1009. if (rx_ret != pdPASS) {
  1010. ESP_LOGE(TAG, "Failed to create RTP tasks");
  1011. call_active = false;
  1012. rtp_tasks_running = false;
  1013. close(s_rtp_sock);
  1014. s_rtp_sock = -1;
  1015. rtp_tx_done = true;
  1016. } else {
  1017. BaseType_t tx_ret = xTaskCreate(
  1018. rtp_tx_task,
  1019. "rtp_tx",
  1020. 16384,
  1021. NULL,
  1022. 5,
  1023. NULL);
  1024. if (tx_ret != pdPASS) {
  1025. ESP_LOGE(TAG, "Failed to create RTP TX task");
  1026. call_active = false;
  1027. rtp_tx_done = true;
  1028. }
  1029. }
  1030. //temp comment out
  1031. //ESP_LOGI(TAG, "rtp task create result=%d", ret);
  1032. }
  1033. else if (!call_active && rtp_tasks_running) {
  1034. ESP_LOGW(TAG, "Previous RTP call is still shutting down");
  1035. }
  1036. }
  1037. else if (strncmp(rx_buf, "BYE ", 4) == 0)
  1038. {
  1039. ESP_LOGI(TAG, "Received BYE");
  1040. call_active = false;
  1041. sip_send_200_ok(sock, &server, rx_buf, NULL, 0);
  1042. }
  1043. else {
  1044. ESP_LOGI(TAG, "Unknown SIP message");
  1045. }
  1046. }
  1047. close(sock);
  1048. vTaskDelete(NULL);
  1049. }
  1050. esp_err_t sip_media_init(void)
  1051. {
  1052. if (s_audio_hw_ready) {
  1053. return ESP_OK;
  1054. }
  1055. esp_err_t ret = esp_board_init(16000, 2, 32);
  1056. if (ret != ESP_OK) {
  1057. ESP_LOGE(TAG, "esp_board_init failed: %s", esp_err_to_name(ret));
  1058. return ret;
  1059. }
  1060. tca9555_driver_init();
  1061. /*if (!sip_open_audio_media_codecs()) {
  1062. return ESP_FAIL;
  1063. }*/
  1064. s_audio_hw_ready = true;
  1065. ESP_LOGI(TAG, "Audio board initialized for live RTP media");
  1066. return ESP_OK;
  1067. }