sip.c 31 KB

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