camera.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <stddef.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "esp_log.h"
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "esp_camera.h"
  9. #include "esp_timer.h"
  10. #include "peer_connection.h"
  11. extern PeerConnection* g_pc;
  12. extern int gDataChannelOpened;
  13. extern PeerConnectionState eState;
  14. extern SemaphoreHandle_t xSemaphore;
  15. extern int get_timestamp();
  16. static const char* TAG = "Camera";
  17. #if defined(CONFIG_ESP32S3_XIAO_SENSE)
  18. #define CAM_PIN_PWDN -1
  19. #define CAM_PIN_RESET -1
  20. #define CAM_PIN_XCLK 10
  21. #define CAM_PIN_SIOD 40
  22. #define CAM_PIN_SIOC 39
  23. #define CAM_PIN_D7 48
  24. #define CAM_PIN_D6 11
  25. #define CAM_PIN_D5 12
  26. #define CAM_PIN_D4 14
  27. #define CAM_PIN_D3 16
  28. #define CAM_PIN_D2 18
  29. #define CAM_PIN_D1 17
  30. #define CAM_PIN_D0 15
  31. #define CAM_PIN_VSYNC 38
  32. #define CAM_PIN_HREF 47
  33. #define CAM_PIN_PCLK 13
  34. #elif defined(CONFIG_ESP32_M5STACK_CAMERA_B)
  35. #define CAM_PIN_PWDN -1
  36. #define CAM_PIN_RESET 15
  37. #define CAM_PIN_XCLK 27
  38. #define CAM_PIN_SIOD 22
  39. #define CAM_PIN_SIOC 23
  40. #define CAM_PIN_D7 19
  41. #define CAM_PIN_D6 36
  42. #define CAM_PIN_D5 18
  43. #define CAM_PIN_D4 39
  44. #define CAM_PIN_D3 5
  45. #define CAM_PIN_D2 34
  46. #define CAM_PIN_D1 35
  47. #define CAM_PIN_D0 32
  48. #define CAM_PIN_VSYNC 25
  49. #define CAM_PIN_HREF 26
  50. #define CAM_PIN_PCLK 21
  51. #elif defined(CONFIG_ESP32S3_EYE)
  52. #define CAM_PIN_PWDN -1
  53. #define CAM_PIN_RESET -1
  54. #define CAM_PIN_XCLK 15
  55. #define CAM_PIN_SIOD 4
  56. #define CAM_PIN_SIOC 5
  57. #define CAM_PIN_D7 16
  58. #define CAM_PIN_D6 17
  59. #define CAM_PIN_D5 18
  60. #define CAM_PIN_D4 12
  61. #define CAM_PIN_D3 10
  62. #define CAM_PIN_D2 8
  63. #define CAM_PIN_D1 9
  64. #define CAM_PIN_D0 11
  65. #define CAM_PIN_VSYNC 6
  66. #define CAM_PIN_HREF 7
  67. #define CAM_PIN_PCLK 13
  68. #else // ESP32-EYE
  69. #define CAM_PIN_PWDN -1
  70. #define CAM_PIN_RESET -1
  71. #define CAM_PIN_XCLK 4
  72. #define CAM_PIN_SIOD 18
  73. #define CAM_PIN_SIOC 23
  74. #define CAM_PIN_D7 36
  75. #define CAM_PIN_D6 37
  76. #define CAM_PIN_D5 38
  77. #define CAM_PIN_D4 39
  78. #define CAM_PIN_D3 35
  79. #define CAM_PIN_D2 14
  80. #define CAM_PIN_D1 13
  81. #define CAM_PIN_D0 34
  82. #define CAM_PIN_VSYNC 5
  83. #define CAM_PIN_HREF 27
  84. #define CAM_PIN_PCLK 25
  85. #endif
  86. static camera_config_t camera_config = {
  87. .ledc_timer = LEDC_TIMER_0,
  88. .ledc_channel = LEDC_CHANNEL_0,
  89. .pin_d7 = CAM_PIN_D7,
  90. .pin_d6 = CAM_PIN_D6,
  91. .pin_d5 = CAM_PIN_D5,
  92. .pin_d4 = CAM_PIN_D4,
  93. .pin_d3 = CAM_PIN_D3,
  94. .pin_d2 = CAM_PIN_D2,
  95. .pin_d1 = CAM_PIN_D1,
  96. .pin_d0 = CAM_PIN_D0,
  97. .pin_xclk = CAM_PIN_XCLK,
  98. .pin_pclk = CAM_PIN_PCLK,
  99. .pin_vsync = CAM_PIN_VSYNC,
  100. .pin_href = CAM_PIN_HREF,
  101. .pin_sccb_sda = CAM_PIN_SIOD,
  102. .pin_sccb_scl = CAM_PIN_SIOC,
  103. .pin_pwdn = CAM_PIN_PWDN,
  104. .pin_reset = CAM_PIN_RESET,
  105. .xclk_freq_hz = 20000000,
  106. .pixel_format = PIXFORMAT_JPEG,
  107. .frame_size = FRAMESIZE_VGA,
  108. .jpeg_quality = 10,
  109. .fb_count = 2,
  110. .grab_mode = CAMERA_GRAB_WHEN_EMPTY};
  111. esp_err_t camera_init() {
  112. // initialize the camera
  113. esp_err_t err = esp_camera_init(&camera_config);
  114. if (err != ESP_OK) {
  115. ESP_LOGE(TAG, "Camera Init Failed");
  116. return err;
  117. }
  118. return ESP_OK;
  119. }
  120. void camera_task(void* pvParameters) {
  121. static int fps = 0;
  122. static int64_t last_time;
  123. int64_t curr_time;
  124. camera_fb_t* fb = NULL;
  125. ESP_LOGI(TAG, "Camera Task Started");
  126. last_time = get_timestamp();
  127. for (;;) {
  128. if ((eState == PEER_CONNECTION_COMPLETED) && gDataChannelOpened) {
  129. fb = esp_camera_fb_get();
  130. if (!fb) {
  131. ESP_LOGE(TAG, "Camera capture failed");
  132. }
  133. // ESP_LOGI(TAG, "Camera captured. size=%zu, timestamp=%llu", fb->len, fb->timestamp);
  134. if (xSemaphoreTake(xSemaphore, portMAX_DELAY)) {
  135. peer_connection_datachannel_send(g_pc, (char*)fb->buf, fb->len);
  136. xSemaphoreGive(xSemaphore);
  137. }
  138. fps++;
  139. if ((fps % 100) == 0) {
  140. curr_time = get_timestamp();
  141. ESP_LOGI(TAG, "Camera FPS=%.2f", 1000.0f / (float)(curr_time - last_time) * 100.0f);
  142. last_time = curr_time;
  143. }
  144. esp_camera_fb_return(fb);
  145. }
  146. vTaskDelay(pdMS_TO_TICKS(1000 / 20));
  147. }
  148. }