app_main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <stddef.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/param.h>
  6. #include <sys/time.h>
  7. #include "esp_event.h"
  8. #include "esp_log.h"
  9. #include "esp_mac.h"
  10. #include "esp_netif.h"
  11. #include "esp_ota_ops.h"
  12. #include "esp_partition.h"
  13. #include "esp_system.h"
  14. #include "esp_tls.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "nvs_flash.h"
  17. #include "protocol_examples_common.h"
  18. #include "peer.h"
  19. static const char* TAG = "webrtc";
  20. static TaskHandle_t xPcTaskHandle = NULL;
  21. static TaskHandle_t xCameraTaskHandle = NULL;
  22. static TaskHandle_t xAudioTaskHandle = NULL;
  23. extern esp_err_t camera_init();
  24. extern esp_err_t audio_init();
  25. extern void camera_task(void* pvParameters);
  26. extern void audio_task(void* pvParameters);
  27. SemaphoreHandle_t xSemaphore = NULL;
  28. PeerConnection* g_pc;
  29. PeerConnectionState eState = PEER_CONNECTION_CLOSED;
  30. int gDataChannelOpened = 0;
  31. int64_t get_timestamp() {
  32. struct timeval tv;
  33. gettimeofday(&tv, NULL);
  34. return (tv.tv_sec * 1000LL + (tv.tv_usec / 1000LL));
  35. }
  36. static void oniceconnectionstatechange(PeerConnectionState state, void* user_data) {
  37. ESP_LOGI(TAG, "PeerConnectionState: %d", state);
  38. eState = state;
  39. // not support datachannel close event
  40. if (eState != PEER_CONNECTION_COMPLETED) {
  41. gDataChannelOpened = 0;
  42. }
  43. }
  44. static void onmessage(char* msg, size_t len, void* userdata, uint16_t sid) {
  45. ESP_LOGI(TAG, "Datachannel message: %.*s", len, msg);
  46. }
  47. void onopen(void* userdata) {
  48. ESP_LOGI(TAG, "Datachannel opened");
  49. gDataChannelOpened = 1;
  50. }
  51. static void onclose(void* userdata) {
  52. }
  53. void peer_connection_task(void* arg) {
  54. ESP_LOGI(TAG, "peer_connection_task started");
  55. for (;;) {
  56. if (xSemaphoreTake(xSemaphore, portMAX_DELAY)) {
  57. peer_connection_loop(g_pc);
  58. xSemaphoreGive(xSemaphore);
  59. }
  60. vTaskDelay(pdMS_TO_TICKS(1));
  61. }
  62. }
  63. void app_main(void) {
  64. PeerConfiguration config = {
  65. .ice_servers = {
  66. {.urls = "stun:stun.l.google.com:19302"}},
  67. .audio_codec = CODEC_PCMA,
  68. .datachannel = DATA_CHANNEL_BINARY,
  69. };
  70. ESP_LOGI(TAG, "[APP] Startup..");
  71. ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
  72. ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
  73. esp_log_level_set("*", ESP_LOG_INFO);
  74. esp_log_level_set("esp-tls", ESP_LOG_VERBOSE);
  75. esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
  76. esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE);
  77. esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE);
  78. esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);
  79. esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE);
  80. ESP_ERROR_CHECK(nvs_flash_init());
  81. ESP_ERROR_CHECK(esp_netif_init());
  82. ESP_ERROR_CHECK(esp_event_loop_create_default());
  83. ESP_ERROR_CHECK(example_connect());
  84. xSemaphore = xSemaphoreCreateMutex();
  85. peer_init();
  86. camera_init();
  87. #if defined(CONFIG_ESP32S3_XIAO_SENSE)
  88. audio_init();
  89. #endif
  90. g_pc = peer_connection_create(&config);
  91. peer_connection_oniceconnectionstatechange(g_pc, oniceconnectionstatechange);
  92. peer_connection_ondatachannel(g_pc, onmessage, onopen, onclose);
  93. peer_signaling_connect(CONFIG_SIGNALING_URL, CONFIG_SIGNALING_TOKEN, g_pc);
  94. #if defined(CONFIG_ESP32S3_XIAO_SENSE)
  95. StackType_t* stack_memory = (StackType_t*)heap_caps_malloc(8192 * sizeof(StackType_t), MALLOC_CAP_SPIRAM);
  96. StaticTask_t task_buffer;
  97. if (stack_memory) {
  98. xAudioTaskHandle = xTaskCreateStaticPinnedToCore(audio_task, "audio", 8192, NULL, 7, stack_memory, &task_buffer, 0);
  99. }
  100. #endif
  101. xTaskCreatePinnedToCore(camera_task, "camera", 4096, NULL, 8, &xCameraTaskHandle, 1);
  102. xTaskCreatePinnedToCore(peer_connection_task, "peer_connection", 8192, NULL, 5, &xPcTaskHandle, 1);
  103. ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
  104. printf("============= Configuration =============\n");
  105. printf(" %-5s : %s\n", "URL", CONFIG_SIGNALING_URL);
  106. printf(" %-5s : %s\n", "Token", CONFIG_SIGNALING_TOKEN);
  107. printf("=========================================\n");
  108. while (1) {
  109. peer_signaling_loop();
  110. vTaskDelay(pdMS_TO_TICKS(10));
  111. }
  112. }