|
| 1 | +# Amazon Kinesis Video Streams WebRTC SDK C - ESP-IDF Guide |
| 2 | + |
| 3 | +This guide provides specific instructions for building and running the Amazon Kinesis Video Streams WebRTC SDK C on ESP32 devices using the ESP-IDF framework. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- ESP-IDF v5.4 or later installed |
| 8 | +- ESP32 development board with 4MB PSRAM (mandatory requirement) |
| 9 | +- AWS Account and credentials |
| 10 | +- Basic familiarity with ESP-IDF development |
| 11 | + |
| 12 | +## Supported Hardware |
| 13 | + |
| 14 | +The SDK has been tested on the following boards: |
| 15 | + |
| 16 | +1. ESP32 (with PSRAM) |
| 17 | +2. ESP32-S3 |
| 18 | +3. ESP32-P4 |
| 19 | + |
| 20 | +### Important Hardware Requirements |
| 21 | + |
| 22 | +- **PSRAM is mandatory**: Internal memory alone is not sufficient to run the WebRTC stack |
| 23 | +- All development boards must have PSRAM capability |
| 24 | +- Choose boards with adequate PSRAM for optimal performance |
| 25 | + |
| 26 | +### Board-Specific Requirements |
| 27 | + |
| 28 | +#### ESP32-P4 Setup |
| 29 | +The ESP32-P4 requires additional components and setup: |
| 30 | + |
| 31 | +1. Required Components (automatically included in P4 builds): |
| 32 | + - esp-hosted |
| 33 | + - esp-remote |
| 34 | + |
| 35 | +2. Network Adapter Setup: |
| 36 | + - Flash the network_adapter example on the ESP32-C6 (part of ESP32-P4 Function EV board) |
| 37 | + - The network_adapter example can be found in the ESP32-C6 SDK |
| 38 | + |
| 39 | +Example build command for ESP32-P4: |
| 40 | +```bash |
| 41 | +idf.py set-target esp32p4 |
| 42 | +idf.py build |
| 43 | +``` |
| 44 | + |
| 45 | +## Build Instructions |
| 46 | + |
| 47 | +### 1. Setting up the Build Environment |
| 48 | + |
| 49 | +First, make sure you have the ESP-IDF environment properly set up: |
| 50 | + |
| 51 | +```bash |
| 52 | +. $IDF_PATH/export.sh # On Linux/macOS |
| 53 | +%IDF_PATH%\export.bat # On Windows |
| 54 | +``` |
| 55 | + |
| 56 | +### 2. Project Configuration |
| 57 | + |
| 58 | +The SDK requires several configurations to be set in the ESP-IDF project: |
| 59 | + |
| 60 | +1. Run menuconfig: |
| 61 | +```bash |
| 62 | +idf.py menuconfig |
| 63 | +``` |
| 64 | + |
| 65 | +2. Important configurations to set: |
| 66 | + - Component config → mbedTLS |
| 67 | + - Enable MBEDTLS_DYNAMIC_BUFFER |
| 68 | + - Set MBEDTLS_SSL_IN_CONTENT_LEN to 16384 |
| 69 | + - Set MBEDTLS_SSL_OUT_CONTENT_LEN to 16384 |
| 70 | + - Component config → ESP HTTPS server |
| 71 | + - Increase Max HTTP Request Header Length to 8192 |
| 72 | + - Component config → FreeRTOS |
| 73 | + - Increase FreeRTOS timer task stack size to 4096 |
| 74 | + |
| 75 | +### 3. Memory Considerations |
| 76 | + |
| 77 | +PSRAM is mandatory for running the WebRTC stack as ESP32's internal RAM is insufficient. Configure your setup properly: |
| 78 | + |
| 79 | +- Enable SPIRAM support in menuconfig (required) |
| 80 | + - Component config → ESP32/ESP32-S3/ESP32-P4 Specific → Support for external SPI RAM |
| 81 | + - Set "SPI RAM config" appropriate for your board |
| 82 | + - Enable "Run memory test on SPI RAM initialization" |
| 83 | + - Set "SPI RAM access method" based on your needs (recommended: "Make RAM allocatable using heap_caps_malloc()") |
| 84 | + |
| 85 | +- Recommended PSRAM settings in menuconfig: |
| 86 | + - Set "Size of per-pointer metadata in bytes" to 8 |
| 87 | + - Enable "Allow external memory as an argument to malloc()" |
| 88 | + |
| 89 | +- Memory allocation strategy: |
| 90 | + - Use PSRAM for large buffers and media frames |
| 91 | + - Reserve internal RAM for critical system operations |
| 92 | + - Monitor memory usage during development |
| 93 | + |
| 94 | +Example of proper PSRAM initialization: |
| 95 | +```c |
| 96 | +#include "esp_psram.h" |
| 97 | + |
| 98 | +void app_main(void) { |
| 99 | + // Initialize PSRAM |
| 100 | + if (!esp_psram_is_initialized()) { |
| 101 | + ESP_LOGE(TAG, "PSRAM initialization failed! WebRTC stack requires PSRAM."); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + size_t psram_size = esp_psram_get_size(); |
| 106 | + ESP_LOGI(TAG, "PSRAM size: %d bytes", psram_size); |
| 107 | + |
| 108 | + // Continue with application initialization |
| 109 | + // ... |
| 110 | +} |
| 111 | +``` |
| 112 | +
|
| 113 | +### 4. Certificate Storage |
| 114 | +
|
| 115 | +For embedded devices, certificates need to be stored in non-volatile storage. The example implementation uses SPIFFS, but NVS can also be used as an alternative: |
| 116 | +
|
| 117 | +1. SPIFFS Storage (Default Implementation): |
| 118 | + ```c |
| 119 | + // Certificate paths in SPIFFS |
| 120 | + setenv("AWS_IOT_CORE_CERT", "/spiffs/certs/certificate.pem", 1); |
| 121 | + setenv("AWS_IOT_CORE_PRIVATE_KEY", "/spiffs/certs/private.key", 1); |
| 122 | + setenv("AWS_KVS_CACERT_PATH", "/spiffs/certs/", 1); |
| 123 | + ``` |
| 124 | + |
| 125 | + To use SPIFFS: |
| 126 | + - Create a `certs` directory in your project's SPIFFS partition |
| 127 | + - Copy your certificates to this directory during flash image creation |
| 128 | + - Ensure SPIFFS is mounted before accessing certificates: |
| 129 | + ```c |
| 130 | + esp_vfs_spiffs_conf_t conf = { |
| 131 | + .base_path = "/spiffs", |
| 132 | + .partition_label = NULL, |
| 133 | + .max_files = 5, |
| 134 | + .format_if_mount_failed = true |
| 135 | + }; |
| 136 | + esp_vfs_spiffs_register(&conf); |
| 137 | + ``` |
| 138 | +
|
| 139 | +2. NVS Storage (Alternative): |
| 140 | + ```c |
| 141 | + #include "nvs_flash.h" |
| 142 | + #include "nvs.h" |
| 143 | +
|
| 144 | + void store_certificates(void) { |
| 145 | + nvs_handle_t nvs_handle; |
| 146 | + nvs_open("storage", NVS_READWRITE, &nvs_handle); |
| 147 | + nvs_set_str(nvs_handle, "cert", certificate_data); |
| 148 | + nvs_set_str(nvs_handle, "key", private_key_data); |
| 149 | + nvs_commit(nvs_handle); |
| 150 | + nvs_close(nvs_handle); |
| 151 | + } |
| 152 | + ``` |
| 153 | + |
| 154 | + Example of loading from NVS: |
| 155 | + ```c |
| 156 | + void load_certificates(char* cert_buffer, size_t max_size) { |
| 157 | + nvs_handle_t nvs_handle; |
| 158 | + size_t required_size; |
| 159 | + nvs_open("storage", NVS_READONLY, &nvs_handle); |
| 160 | + nvs_get_str(nvs_handle, "cert", NULL, &required_size); |
| 161 | + if (required_size <= max_size) { |
| 162 | + nvs_get_str(nvs_handle, "cert", cert_buffer, &required_size); |
| 163 | + } |
| 164 | + nvs_close(nvs_handle); |
| 165 | + } |
| 166 | + ``` |
| 167 | +
|
| 168 | +## IoT Core Setup |
| 169 | +
|
| 170 | +For IoT Core setup and credentials, refer to the [Setup IoT section in main README](./README.md#setup-iot). However, for ESP32 devices: |
| 171 | +
|
| 172 | +1. Store certificates in NVS or secure element |
| 173 | +2. Initialize certificates during boot |
| 174 | +3. Implement secure boot if required |
| 175 | +
|
| 176 | +Example of loading certificates from NVS: |
| 177 | +```c |
| 178 | +void load_certificates(char* cert_buffer, size_t max_size) { |
| 179 | + nvs_handle_t nvs_handle; |
| 180 | + size_t required_size; |
| 181 | + nvs_open("storage", NVS_READONLY, &nvs_handle); |
| 182 | + nvs_get_str(nvs_handle, "cert", NULL, &required_size); |
| 183 | + if (required_size <= max_size) { |
| 184 | + nvs_get_str(nvs_handle, "cert", cert_buffer, &required_size); |
| 185 | + } |
| 186 | + nvs_close(nvs_handle); |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +## Network Configuration |
| 191 | + |
| 192 | +ESP32 requires proper WiFi/Ethernet setup before WebRTC can work: |
| 193 | + |
| 194 | +```c |
| 195 | +void wifi_init_sta(void) { |
| 196 | + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); |
| 197 | + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); |
| 198 | + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
| 199 | + wifi_config_t wifi_config = { |
| 200 | + .sta = { |
| 201 | + .ssid = CONFIG_WIFI_SSID, |
| 202 | + .password = CONFIG_WIFI_PASSWORD, |
| 203 | + }, |
| 204 | + }; |
| 205 | + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); |
| 206 | + ESP_ERROR_CHECK(esp_wifi_start()); |
| 207 | +} |
| 208 | +``` |
| 209 | +
|
| 210 | +## Memory Optimization |
| 211 | +
|
| 212 | +For ESP32 devices, consider these additional memory optimization techniques: |
| 213 | +
|
| 214 | +1. Use PSRAM for large buffers: |
| 215 | +```c |
| 216 | +#ifdef CONFIG_SPIRAM_SUPPORT |
| 217 | + // Allocate in PSRAM |
| 218 | + void* large_buffer = heap_caps_malloc(size, MALLOC_CAP_SPIRAM); |
| 219 | +#else |
| 220 | + // Regular allocation |
| 221 | + void* large_buffer = malloc(size); |
| 222 | +#endif |
| 223 | +``` |
| 224 | + |
| 225 | +2. Adjust WebRTC buffer sizes: |
| 226 | +```c |
| 227 | +// Reduce buffer sizes for ESP32 |
| 228 | +configuration.kvsRtcConfiguration.maximumTransmissionUnit = 1200; |
| 229 | +configuration.kvsRtcConfiguration.maximumNumberOfBuffers = 12; |
| 230 | +``` |
| 231 | + |
| 232 | +## Debugging |
| 233 | + |
| 234 | +ESP32-specific debugging tips: |
| 235 | + |
| 236 | +1. Enable Core Dumps: |
| 237 | +```bash |
| 238 | +idf.py menuconfig |
| 239 | +# Enable saving core dumps |
| 240 | +# Component config → ESP System Settings → Core dump destination → UART |
| 241 | +``` |
| 242 | + |
| 243 | +2. Monitor memory usage: |
| 244 | +```c |
| 245 | +#include "esp_heap_caps.h" |
| 246 | + |
| 247 | +void print_memory_info() { |
| 248 | + printf("Free DRAM: %d bytes\n", heap_caps_get_free_size(MALLOC_CAP_8BIT)); |
| 249 | + printf("Free IRAM: %d bytes\n", heap_caps_get_free_size(MALLOC_CAP_32BIT)); |
| 250 | + #ifdef CONFIG_SPIRAM_SUPPORT |
| 251 | + printf("Free PSRAM: %d bytes\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM)); |
| 252 | + #endif |
| 253 | +} |
| 254 | +``` |
| 255 | + |
| 256 | +## Known Limitations |
| 257 | + |
| 258 | +1. Memory Constraints |
| 259 | + - ESP32 has limited RAM (520KB) |
| 260 | + - Consider using ESP32-WROVER with PSRAM for larger applications |
| 261 | + |
| 262 | +2. CPU Performance |
| 263 | + - WebRTC operations may be CPU intensive |
| 264 | + - Monitor CPU usage and optimize where possible |
| 265 | + |
| 266 | +3. Network Stability |
| 267 | + - WiFi connection stability is crucial |
| 268 | + - Implement proper reconnection handling |
| 269 | + |
| 270 | +## Additional Resources |
| 271 | + |
| 272 | +- [ESP-IDF Programming Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/) |
| 273 | +- [Main KVS WebRTC SDK Documentation](./README.md) |
| 274 | +- [ESP32 Hardware Reference](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/hw-reference/index.html) |
| 275 | + |
| 276 | +## License |
| 277 | + |
| 278 | +This library is licensed under the Apache 2.0 License. |
0 commit comments