sip.c 30 KB

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