sip.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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 "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "esp_wifi.h"
  9. #include "esp_event.h"
  10. #include "esp_log.h"
  11. #include "nvs_flash.h"
  12. #include "lwip/sockets.h"
  13. #include <stdbool.h>
  14. #include "esp_rom_md5.h"
  15. #include "bsp_board.h"
  16. #include "tca9555_driver.h"
  17. #define ESP32_IP "192.168.68.16"
  18. #define RTP_PORT 4000
  19. #define WIFI_SSID "TOCHTECH"
  20. #define WIFI_PASS "Smarturns2017"
  21. #define ASTERISK_IP "192.168.68.90"
  22. #define SIP_PORT 5060
  23. #define SIP_LOCAL_IP "0.0.0.0"
  24. #define SIP_LOCAL_PORT 5062
  25. #define SIP_USER "1001"
  26. #define SIP_PASSWORD "secret123"
  27. #define SAMPLES_PER_PACKET 160 // 20 ms
  28. #define MIC_FRAME_SAMPLES 320
  29. #define AEC_MAX_FRAME_SAMPLES 512
  30. #define FAR_END_FIFO_SAMPLES 2048
  31. #define AEC_REFERENCE_DELAY_MS 20
  32. #define AEC_REFERENCE_DELAY_SAMPLES ((16000 * AEC_REFERENCE_DELAY_MS) / 1000)
  33. static int32_t mic_buffer[320];
  34. static int16_t mic_pcm16[SAMPLES_PER_PACKET];
  35. static uint8_t rx_packet[172];
  36. static const char *TAG = "SIP";
  37. volatile bool wifi_ready = false;
  38. static int s_rtp_sock = -1;
  39. static struct sockaddr_in s_rtp_peer = {0};
  40. static uint16_t s_rtp_peer_port = 0;
  41. static volatile bool call_active = false;
  42. static volatile bool s_audio_hw_ready = false;
  43. static volatile bool rtp_tasks_running = false;
  44. static volatile bool rtp_tx_done = true;
  45. static int16_t far_end_fifo[FAR_END_FIFO_SAMPLES];
  46. static size_t far_end_read = 0;
  47. static size_t far_end_write = 0;
  48. static portMUX_TYPE far_end_mux = portMUX_INITIALIZER_UNLOCKED;
  49. static int16_t aec_mic_pending[AEC_MAX_FRAME_SAMPLES];
  50. static int16_t aec_ref_pending[AEC_MAX_FRAME_SAMPLES];
  51. static size_t aec_pending_samples = 0;
  52. static int16_t aec_output_fifo[FAR_END_FIFO_SAMPLES];
  53. static size_t aec_output_samples = 0;
  54. static void rtp_tx_task(void *pvParameters);
  55. static void rtp_rx_task(void *pvParameters);
  56. static void far_end_push(const int16_t *samples, size_t count)
  57. {
  58. portENTER_CRITICAL(&far_end_mux);
  59. for (size_t i = 0; i < count; ++i) {
  60. size_t next = (far_end_write + 1) % FAR_END_FIFO_SAMPLES;
  61. if (next == far_end_read) {
  62. far_end_read = (far_end_read + 1) % FAR_END_FIFO_SAMPLES;
  63. }
  64. far_end_fifo[far_end_write] = samples[i];
  65. far_end_write = next;
  66. }
  67. portEXIT_CRITICAL(&far_end_mux);
  68. }
  69. static void far_end_pop(int16_t *samples, size_t count)
  70. {
  71. portENTER_CRITICAL(&far_end_mux);
  72. for (size_t i = 0; i < count; ++i) {
  73. if (far_end_read == far_end_write) {
  74. samples[i] = 0;
  75. } else {
  76. samples[i] = far_end_fifo[far_end_read];
  77. far_end_read = (far_end_read + 1) % FAR_END_FIFO_SAMPLES;
  78. }
  79. }
  80. portEXIT_CRITICAL(&far_end_mux);
  81. }
  82. static bool aec_process_capture(const int16_t *near_end,
  83. const int16_t *far_end,
  84. size_t sample_count)
  85. {
  86. int frame_size = bsp_aec_get_frame_size();
  87. if (frame_size <= 0 || frame_size > AEC_MAX_FRAME_SAMPLES) {
  88. return false;
  89. }
  90. for (size_t i = 0; i < sample_count; ++i) {
  91. if (aec_pending_samples == AEC_MAX_FRAME_SAMPLES) {
  92. return false;
  93. }
  94. aec_mic_pending[aec_pending_samples] = near_end[i];
  95. aec_ref_pending[aec_pending_samples] = far_end[i];
  96. ++aec_pending_samples;
  97. if (aec_pending_samples == (size_t)frame_size) {
  98. int16_t processed[AEC_MAX_FRAME_SAMPLES];
  99. if (bsp_aec_process_frame(aec_mic_pending,
  100. aec_ref_pending,
  101. processed) != ESP_OK) {
  102. return false;
  103. }
  104. for (int sample = 0; sample < frame_size; sample += 2) {
  105. if (aec_output_samples < FAR_END_FIFO_SAMPLES) {
  106. aec_output_fifo[aec_output_samples++] = processed[sample];
  107. }
  108. }
  109. aec_pending_samples = 0;
  110. }
  111. }
  112. return true;
  113. }
  114. static bool aec_output_pop(int16_t *samples, size_t count)
  115. {
  116. if (aec_output_samples < count) {
  117. return false;
  118. }
  119. memcpy(samples, aec_output_fifo, count * sizeof(int16_t));
  120. memmove(aec_output_fifo,
  121. aec_output_fifo + count,
  122. (aec_output_samples - count) * sizeof(int16_t));
  123. aec_output_samples -= count;
  124. return true;
  125. }
  126. static void aec_stream_reset(void)
  127. {
  128. portENTER_CRITICAL(&far_end_mux);
  129. far_end_read = 0;
  130. far_end_write = 0;
  131. portEXIT_CRITICAL(&far_end_mux);
  132. aec_pending_samples = 0;
  133. aec_output_samples = 0;
  134. int16_t reference_delay[AEC_REFERENCE_DELAY_SAMPLES] = {0};
  135. far_end_push(reference_delay, AEC_REFERENCE_DELAY_SAMPLES);
  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 uint16_t seq = 0;
  425. static uint32_t timestamp = 0;
  426. if(s_rtp_peer_port == 0)
  427. {
  428. ESP_LOGW(TAG,
  429. "No RTP peer port set!");
  430. return;
  431. }
  432. uint8_t payload[SAMPLES_PER_PACKET];
  433. // Read one 20 ms stereo capture block (320 mono samples at 16 kHz).
  434. if (!microphone_read((int16_t *)mic_buffer, sizeof(mic_buffer)))
  435. {
  436. return;
  437. }
  438. int16_t *mic16 = (int16_t *)mic_buffer;
  439. int16_t near_end[MIC_FRAME_SAMPLES];
  440. int16_t far_end[MIC_FRAME_SAMPLES];
  441. for (int i = 0; i < MIC_FRAME_SAMPLES; ++i) {
  442. int32_t left = mic16[i * 2];
  443. int32_t right = mic16[i * 2 + 1];
  444. near_end[i] = (int16_t)((left + right) / 2);
  445. }
  446. far_end_pop(far_end, MIC_FRAME_SAMPLES);
  447. if (!aec_process_capture(near_end, far_end, MIC_FRAME_SAMPLES) ||
  448. !aec_output_pop(mic_pcm16, SAMPLES_PER_PACKET)) {
  449. return;
  450. }
  451. // PCM16 -> PCMU (G711 u-law)
  452. for(int i = 0; i < SAMPLES_PER_PACKET; i++)
  453. {
  454. payload[i] = linear2ulaw(mic_pcm16[i]);
  455. }
  456. uint8_t packet[12 + SAMPLES_PER_PACKET];
  457. memset(packet,0,sizeof(packet));
  458. packet[0] = 0x80; // RTP version
  459. packet[1] = 0x00; // PCMU
  460. packet[2] = seq >> 8;
  461. packet[3] = seq & 0xff;
  462. packet[4] = timestamp >> 24;
  463. packet[5] = timestamp >> 16;
  464. packet[6] = timestamp >> 8;
  465. packet[7] = timestamp;
  466. uint32_t ssrc = 0x12345678;
  467. packet[8] = (ssrc >> 24) & 0xff;
  468. packet[9] = (ssrc >> 16) & 0xff;
  469. packet[10] = (ssrc >> 8) & 0xff;
  470. packet[11] = ssrc & 0xff;
  471. memcpy(packet+12,
  472. payload,
  473. SAMPLES_PER_PACKET);
  474. struct sockaddr_in peer = s_rtp_peer;
  475. peer.sin_port = htons(s_rtp_peer_port);
  476. esp_netif_ip_info_t ip;
  477. esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
  478. esp_netif_get_ip_info(netif, &ip);
  479. int ret =
  480. sendto(
  481. s_rtp_sock,
  482. packet,
  483. sizeof(packet),
  484. 0,
  485. (struct sockaddr *)&peer,
  486. sizeof(peer));
  487. if(ret < 0)
  488. {
  489. ESP_LOGE(TAG,
  490. "RTP send failed errno=%d",
  491. errno);
  492. }
  493. seq++;
  494. timestamp += SAMPLES_PER_PACKET;
  495. }
  496. static void rtp_tx_task(void *pvParameters)
  497. {
  498. ESP_LOGI(TAG, "RTP TX started");
  499. TickType_t last_wake = xTaskGetTickCount();
  500. while(call_active)
  501. {
  502. sip_send_rtp_packet();
  503. vTaskDelayUntil(
  504. &last_wake,
  505. pdMS_TO_TICKS(20)
  506. );
  507. }
  508. ESP_LOGI(TAG, "RTP TX stopped");
  509. rtp_tx_done = true;
  510. vTaskDelete(NULL);
  511. }
  512. static void rtp_rx_task(void *pvParameters)
  513. {
  514. ESP_LOGI(TAG, "RTP RX started");
  515. while (call_active)
  516. {
  517. struct sockaddr_in src;
  518. socklen_t len = sizeof(src);
  519. int n = recvfrom(
  520. s_rtp_sock,
  521. rx_packet,
  522. sizeof(rx_packet),
  523. 0,
  524. (struct sockaddr *)&src,
  525. &len);//
  526. if (n < 0)
  527. {
  528. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  529. continue;
  530. }
  531. ESP_LOGE(TAG, "recvfrom failed errno=%d", errno);
  532. call_active = false;
  533. break;
  534. }
  535. if (s_rtp_peer_port != 0 &&
  536. (src.sin_port != htons(s_rtp_peer_port) ||
  537. src.sin_addr.s_addr != s_rtp_peer.sin_addr.s_addr))
  538. {
  539. ESP_LOGW(TAG,
  540. "Ignoring RTP from unexpected peer %s:%d",
  541. inet_ntoa(src.sin_addr),
  542. ntohs(src.sin_port));
  543. continue;
  544. }
  545. /* Ignore packets that are clearly looped back to the local device. */
  546. esp_netif_ip_info_t local_ip;
  547. esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
  548. esp_netif_get_ip_info(netif, &local_ip);
  549. if (src.sin_addr.s_addr == local_ip.ip.addr)
  550. {
  551. ESP_LOGW(TAG, "Ignoring looped RTP packet from local IP");
  552. continue;
  553. }
  554. if (n <= 12)
  555. {
  556. ESP_LOGW(TAG, "Packet too small");
  557. continue;
  558. }
  559. int payload_size = n - 12;
  560. if (payload_size > SAMPLES_PER_PACKET)
  561. {
  562. payload_size = SAMPLES_PER_PACKET;
  563. }
  564. int16_t play_buf[640];
  565. int16_t far_end_ref[320];
  566. for (int i = 0; i < payload_size; i++)
  567. {
  568. int16_t s1 = ulaw2linear(rx_packet[12+i]);
  569. int16_t s2 = s1;
  570. if (i < payload_size - 1)
  571. {
  572. int16_t next = ulaw2linear(rx_packet[12+i+1]);
  573. s2 = (s1 + next) / 2;
  574. }
  575. // sample 1
  576. play_buf[4*i + 0] = s1; // Left
  577. play_buf[4*i + 1] = s1; // Right
  578. // interpolated sample
  579. play_buf[4*i + 2] = s2; // Left
  580. play_buf[4*i + 3] = s2; // Right
  581. far_end_ref[2 * i] = s1;
  582. far_end_ref[2 * i + 1] = s2;
  583. }
  584. // The reference must be the mono 16 kHz signal sent to the speaker,
  585. // before it is duplicated into the codec's stereo output.
  586. far_end_push(far_end_ref, payload_size * 2);
  587. esp_audio_play(
  588. play_buf,
  589. payload_size * 4 * sizeof(int16_t),
  590. portMAX_DELAY);
  591. }
  592. while (!rtp_tx_done) {
  593. vTaskDelay(pdMS_TO_TICKS(10));
  594. }
  595. if (s_rtp_sock >= 0) {
  596. close(s_rtp_sock);
  597. s_rtp_sock = -1;
  598. }
  599. rtp_tasks_running = false;
  600. ESP_LOGI(TAG, "RTP RX stopped");
  601. vTaskDelete(NULL);
  602. }
  603. /* ================= SIP TASK ================= */
  604. void sip_register_task(void *pvParameters)
  605. {
  606. struct sockaddr_in server = {0};
  607. server.sin_family = AF_INET;
  608. server.sin_port = htons(SIP_PORT);
  609. server.sin_addr.s_addr = inet_addr(ASTERISK_IP);
  610. int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  611. struct sockaddr_in local_addr = {0};
  612. local_addr.sin_family = AF_INET;
  613. local_addr.sin_port = htons(SIP_LOCAL_PORT);
  614. local_addr.sin_addr.s_addr = INADDR_ANY;
  615. if (bind(sock,
  616. (struct sockaddr *)&local_addr,
  617. sizeof(local_addr)) < 0) {
  618. ESP_LOGE(TAG, "Failed to bind SIP port %d", SIP_LOCAL_PORT);
  619. close(sock);
  620. vTaskDelete(NULL);
  621. return;
  622. }
  623. ESP_LOGI(TAG, "SIP listening on UDP %d", SIP_LOCAL_PORT);
  624. if (sock < 0) {
  625. ESP_LOGE(TAG, "Socket failed");
  626. vTaskDelete(NULL);
  627. return;
  628. }
  629. struct timeval timeout = {
  630. .tv_sec = 300,
  631. .tv_usec = 0
  632. };
  633. setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
  634. /* ================= FIRST REGISTER ================= */
  635. char register_msg[512];
  636. snprintf(register_msg, sizeof(register_msg),
  637. "REGISTER sip:%s SIP/2.0\r\n"
  638. "Via: SIP/2.0/UDP %s:%d\r\n"
  639. "From: <sip:%s@%s>;tag=1234\r\n"
  640. "To: <sip:%s@%s>\r\n"
  641. "Call-ID: esp32-0001\r\n"
  642. "CSeq: 1 REGISTER\r\n"
  643. "Contact: <sip:%s@%s:%d>\r\n"
  644. "Expires: 300\r\n"
  645. "Content-Length: 0\r\n"
  646. "\r\n",
  647. ASTERISK_IP,
  648. SIP_LOCAL_IP,
  649. SIP_LOCAL_PORT,
  650. SIP_USER,
  651. ASTERISK_IP,
  652. SIP_USER,
  653. ASTERISK_IP,
  654. SIP_USER,
  655. SIP_LOCAL_IP,
  656. SIP_LOCAL_PORT);
  657. ESP_LOGI(TAG, "Sending REGISTER...");
  658. sendto(sock, register_msg, strlen(register_msg), 0,
  659. (struct sockaddr*)&server, sizeof(server));
  660. /* ================= RECEIVE 401 / 200 ================= */
  661. char response[1024];
  662. socklen_t addr_len = sizeof(server);
  663. bool registered = false;
  664. while (!registered) {
  665. int len = recvfrom(sock, response, sizeof(response) - 1, 0,
  666. (struct sockaddr *)&server, &addr_len);
  667. if (len <= 0) {
  668. ESP_LOGE(TAG, "No response");
  669. close(sock);
  670. vTaskDelete(NULL);
  671. return;
  672. }
  673. response[len] = '\0';
  674. ESP_LOGI(TAG, "Received SIP response (%d bytes)", len);
  675. if (strstr(response, "SIP/2.0 401")) {
  676. char sip_nonce[256] = {0};
  677. char sip_realm[128] = {0};
  678. char *n = strstr(response, "nonce=\"");
  679. if (n) {
  680. n += 7;
  681. char *end = strchr(n, '"');
  682. if (end) {
  683. size_t nlen = end - n;
  684. if (nlen < sizeof(sip_nonce)) {
  685. strncpy(sip_nonce, n, nlen);
  686. sip_nonce[nlen] = '\0';
  687. }
  688. }
  689. }
  690. char *r = strstr(response, "realm=\"");
  691. if (r) {
  692. r += 7;
  693. char *rend = strchr(r, '"');
  694. if (rend) {
  695. size_t rlen = rend - r;
  696. if (rlen < sizeof(sip_realm)) {
  697. strncpy(sip_realm, r, rlen);
  698. sip_realm[rlen] = '\0';
  699. }
  700. }
  701. }
  702. if (sip_realm[0] == '\0' || sip_nonce[0] == '\0') {
  703. ESP_LOGE(TAG, "Missing realm or nonce in challenge");
  704. close(sock);
  705. vTaskDelete(NULL);
  706. return;
  707. }
  708. ESP_LOGI(TAG, "Nonce: %s", sip_nonce);
  709. ESP_LOGI(TAG, "Realm: %s", sip_realm);
  710. char response_hash[64];
  711. char sip_uri[64];
  712. snprintf(sip_uri, sizeof(sip_uri), "sip:%s", ASTERISK_IP);
  713. sip_compute_response(
  714. SIP_USER,
  715. sip_realm,
  716. SIP_PASSWORD,
  717. sip_nonce,
  718. sip_uri,
  719. response_hash
  720. );
  721. char auth[1024];
  722. snprintf(auth, sizeof(auth),
  723. "REGISTER sip:%s SIP/2.0\r\n"
  724. "Via: SIP/2.0/UDP %s:%d\r\n"
  725. "From: <sip:%s@%s>;tag=1234\r\n"
  726. "To: <sip:%s@%s>\r\n"
  727. "Call-ID: esp32-0001\r\n"
  728. "CSeq: 2 REGISTER\r\n"
  729. "Contact: <sip:%s@%s:%d>\r\n"
  730. "Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"sip:%s\", response=\"%s\"\r\n"
  731. "Expires: 300\r\n"
  732. "Content-Length: 0\r\n"
  733. "\r\n",
  734. ASTERISK_IP,
  735. SIP_LOCAL_IP,
  736. SIP_LOCAL_PORT,
  737. SIP_USER,
  738. ASTERISK_IP,
  739. SIP_USER,
  740. ASTERISK_IP,
  741. SIP_USER,
  742. SIP_LOCAL_IP,
  743. SIP_LOCAL_PORT,
  744. SIP_USER,
  745. sip_realm,
  746. sip_nonce,
  747. ASTERISK_IP,
  748. response_hash);
  749. ESP_LOGI(TAG, "Sending AUTH REGISTER...");
  750. sendto(sock, auth, strlen(auth), 0,
  751. (struct sockaddr *)&server, sizeof(server));
  752. }
  753. else if (strstr(response, "SIP/2.0 200")) {
  754. registered = true;
  755. }
  756. else if (strstr(response, "SIP/2.0 486")) {
  757. ESP_LOGW(TAG, "Server returned 486 Busy");
  758. }
  759. else {
  760. ESP_LOGI(TAG, "Ignoring SIP response");
  761. }
  762. }
  763. if (!registered) {
  764. ESP_LOGW(TAG, "Registration was not completed");
  765. close(sock);
  766. vTaskDelete(NULL);
  767. return;
  768. }
  769. ESP_LOGI(TAG, "🎉 SIP REGISTER SUCCESS");
  770. ESP_LOGI(TAG, "stack remaining: %u",
  771. uxTaskGetStackHighWaterMark(NULL));
  772. /* =====================================================
  773. * KEEP SOCKET OPEN AND WAIT FOR SIP REQUESTS
  774. * ===================================================== */
  775. ESP_LOGI(TAG, "Waiting for incoming SIP requests...");
  776. while (1) {
  777. char rx_buf[2048];
  778. int rx_len = recvfrom(sock,
  779. rx_buf,
  780. sizeof(rx_buf) - 1,
  781. 0,
  782. (struct sockaddr *)&server,
  783. &addr_len);
  784. if (rx_len <= 0) {
  785. continue;
  786. }
  787. rx_buf[rx_len] = '\0';
  788. ESP_LOGI(TAG, "\n===== RAW INVITE =====\n%s\n=======================\n", rx_buf);
  789. ESP_LOGI(TAG, "Received SIP packet (%d bytes)", rx_len);
  790. if (strncmp(rx_buf, "REGISTER ", 9) == 0) {
  791. ESP_LOGI(TAG, "Received REGISTER; sending 200 OK");
  792. sip_send_200_ok(sock, &server, rx_buf, NULL, 0);
  793. continue;
  794. }
  795. else if (strncmp(rx_buf, "INVITE ", 7) == 0) {
  796. ESP_LOGI(TAG, "📞 Incoming INVITE");
  797. /* ================= EXTRACT SIP HEADERS ================= */
  798. char via[512] = {0};
  799. char from[256] = {0};
  800. char to[256] = {0};
  801. char callid[256] = {0};
  802. char cseq[128] = {0};
  803. char *header = NULL;
  804. header = strstr(rx_buf, "Via:");
  805. if (header) {
  806. sscanf(header, "Via: %511[^\r\n]", via);
  807. }
  808. header = strstr(rx_buf, "From:");
  809. if (header) {
  810. sscanf(header, "From: %255[^\r\n]", from);
  811. }
  812. header = strstr(rx_buf, "To:");
  813. if (header) {
  814. sscanf(header, "To: %255[^\r\n]", to);
  815. }
  816. header = strstr(rx_buf, "Call-ID:");
  817. if (header) {
  818. sscanf(header, "Call-ID: %255[^\r\n]", callid);
  819. }
  820. header = strstr(rx_buf, "CSeq:");
  821. if (header) {
  822. sscanf(header, "CSeq: %127[^\r\n]", cseq);
  823. }
  824. /* ================= SEND 100 TRYING ================= */
  825. char trying[4096];
  826. snprintf(trying, sizeof(trying),
  827. "SIP/2.0 100 Trying\r\n"
  828. "Via: %s\r\n"
  829. "From: %s\r\n"
  830. "To: %s\r\n"
  831. "Call-ID: %s\r\n"
  832. "CSeq: %s\r\n"
  833. "Content-Length: 0\r\n"
  834. "\r\n",
  835. via,
  836. from,
  837. to,
  838. callid,
  839. cseq);
  840. sendto(sock,
  841. trying,
  842. strlen(trying),
  843. 0,
  844. (struct sockaddr *)&server,
  845. sizeof(server));
  846. ESP_LOGI(TAG, "100 Trying sent");
  847. ESP_LOGI(TAG, "stack remaining: %u",
  848. uxTaskGetStackHighWaterMark(NULL));
  849. char ok[4096];
  850. char sdp[1024];
  851. sip_build_sdp(sdp, sizeof(sdp));
  852. if (sip_extract_rtp_port(rx_buf, &s_rtp_peer_port)) {
  853. s_rtp_peer.sin_family = AF_INET;
  854. s_rtp_peer.sin_port = htons(s_rtp_peer_port);
  855. s_rtp_peer.sin_addr.s_addr = inet_addr(ASTERISK_IP);
  856. ESP_LOGI(TAG,
  857. "RTP peer port=%d",
  858. s_rtp_peer_port);
  859. }
  860. snprintf(ok, sizeof(ok),
  861. "SIP/2.0 200 OK\r\n"
  862. "Via: %s\r\n"
  863. "From: %s\r\n"
  864. "To: %s;esp32\r\n"
  865. "Call-ID: %s\r\n"
  866. "CSeq: %s\r\n"
  867. "Contact: <sip:%s@%s:%d>\r\n"
  868. "Content-Type: application/sdp\r\n"
  869. "Content-Length: %d\r\n"
  870. "\r\n"
  871. "%s",
  872. via,
  873. from,
  874. to,
  875. callid,
  876. cseq,
  877. SIP_USER,
  878. ESP32_IP,
  879. SIP_LOCAL_PORT,
  880. strlen(sdp),
  881. sdp);
  882. ESP_LOGI(TAG, "\n========== 200 OK ==========\n%s\n============================\n", ok);
  883. sendto(sock,
  884. ok,
  885. strlen(ok),
  886. 0,
  887. (struct sockaddr *)&server,
  888. sizeof(server));
  889. ESP_LOGI(TAG, "200 OK sent");
  890. }
  891. else if (strncmp(rx_buf, "OPTIONS ", 8) == 0) {
  892. ESP_LOGI(TAG, "Received OPTIONS");
  893. }
  894. else if (strncmp(rx_buf, "ACK ", 4) == 0)
  895. {
  896. ESP_LOGI(TAG, "Received ACK");
  897. if (!call_active && !rtp_tasks_running)
  898. {
  899. if (!rtp_socket_open()) {
  900. ESP_LOGE(TAG, "Cannot start RTP call without a socket");
  901. continue;
  902. }
  903. if (bsp_aec_reset() != ESP_OK) {
  904. ESP_LOGE(TAG, "Cannot reset AEC for this call");
  905. close(s_rtp_sock);
  906. s_rtp_sock = -1;
  907. continue;
  908. }
  909. aec_stream_reset();
  910. ESP_LOGI(TAG, "AEC reference delay: %d ms", AEC_REFERENCE_DELAY_MS);
  911. call_active = true;
  912. rtp_tasks_running = true;
  913. rtp_tx_done = false;
  914. BaseType_t rx_ret = xTaskCreate(
  915. rtp_rx_task,
  916. "rtp_rx",
  917. 16384,
  918. NULL,
  919. 5,
  920. NULL);
  921. if (rx_ret != pdPASS) {
  922. ESP_LOGE(TAG, "Failed to create RTP tasks");
  923. call_active = false;
  924. rtp_tasks_running = false;
  925. close(s_rtp_sock);
  926. s_rtp_sock = -1;
  927. rtp_tx_done = true;
  928. } else {
  929. BaseType_t tx_ret = xTaskCreate(
  930. rtp_tx_task,
  931. "rtp_tx",
  932. 16384,
  933. NULL,
  934. 5,
  935. NULL);
  936. if (tx_ret != pdPASS) {
  937. ESP_LOGE(TAG, "Failed to create RTP TX task");
  938. call_active = false;
  939. rtp_tx_done = true;
  940. }
  941. }
  942. }
  943. else if (!call_active && rtp_tasks_running) {
  944. ESP_LOGW(TAG, "Previous RTP call is still shutting down");
  945. }
  946. }
  947. else if (strncmp(rx_buf, "BYE ", 4) == 0)
  948. {
  949. ESP_LOGI(TAG, "Received BYE");
  950. call_active = false;
  951. sip_send_200_ok(sock, &server, rx_buf, NULL, 0);
  952. }
  953. else {
  954. ESP_LOGI(TAG, "Unknown SIP message");
  955. }
  956. }
  957. close(sock);
  958. vTaskDelete(NULL);
  959. }
  960. esp_err_t sip_media_init(void)
  961. {
  962. if (s_audio_hw_ready) {
  963. return ESP_OK;
  964. }
  965. esp_err_t ret = esp_board_init(16000, 2, 32);
  966. if (ret != ESP_OK) {
  967. ESP_LOGE(TAG, "esp_board_init failed: %s", esp_err_to_name(ret));
  968. return ret;
  969. }
  970. tca9555_driver_init();
  971. s_audio_hw_ready = true;
  972. ESP_LOGI(TAG, "Audio board initialized for live RTP media");
  973. return ESP_OK;
  974. }