sip.c 32 KB

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