Skip to content

Commit ead838a

Browse files
committed
ESP port: Added needed components and examples
- Cleanly added the support under esp_port directory - Added ESP specific components, submodules and an webrtc_classic example - Added `app_common` needed as a common functionality for webrtc example/s. - Added network_adapter example: Needed when we want to use the webrtc_classic on P4+C6 board
1 parent b99e978 commit ead838a

File tree

260 files changed

+51966
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

260 files changed

+51966
-4
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ tags
1212
CMakeLists.txt.user
1313
*.vscode
1414
*.swp
15+
16+
sdkconfig
17+
sdkconfig.old
18+
dependencies.lock

.gitmodules

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@
44
[submodule "open-source/amazon-kinesis-video-streams-producer-c"]
55
path = open-source/amazon-kinesis-video-streams-producer-c
66
url = https://github.com/awslabs/amazon-kinesis-video-streams-producer-c.git
7+
[submodule "esp_port/components/esp_usrsctp/usrsctp"]
8+
path = esp_port/components/esp_usrsctp/usrsctp
9+
url = https://github.com/vikramdattu/usrsctp.git
10+
[submodule "esp_port/components/libsrtp2/libsrtp"]
11+
path = esp_port/components/libsrtp2/libsrtp
12+
url = https://github.com/cisco/libsrtp.git
13+
[submodule "esp_port/components/libwebsockets/libwebsockets"]
14+
path = esp_port/components/libwebsockets/libwebsockets
15+
url = https://github.com/warmcat/libwebsockets.git

README_ESP.md

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
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.

esp_port/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sdkconfig
2+
sdkconfig.old
3+
dependencies.lock
4+
managed_components
5+
build/
6+
*.key
7+
*.pem

esp_port/README.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
1-
# Port for Espressif Chipsets
1+
# ESP-IDF Port of Amazon Kinesis Video Streams WebRTC SDK
22

3-
This directory maintains Espressif Chipsets support via ESP-IDF framework.
3+
This directory contains the ESP-IDF port of the Amazon Kinesis Video Streams WebRTC SDK. The port is organized as a collection of ESP-IDF components that can be used in ESP-IDF projects.
44

55
## Directory Structure
66

7-
- `components/`: Contains Espressif-specific dependency code and components required for the port
8-
- `examples/`: Contains example applications ready to be built using the ESP-IDF build system
7+
```
8+
esp_port/
9+
├── components/ # ESP-IDF components
10+
│ ├── api_call/ # API call handling component
11+
│ ├── credential/ # Credential management
12+
│ ├── esp_hosted/ # ESP hosted functionality
13+
│ ├── esp_usrsctp/ # SCTP protocol implementation for ESP
14+
│ ├── esp_webrtc_utils/ # WebRTC utilities for ESP
15+
│ ├── esp_wifi_remote/ # Remote WiFi functionality
16+
│ ├── kvs_utils/ # KVS utility functions
17+
│ ├── kvs_webrtc/ # Main KVS WebRTC component
18+
│ ├── libllhttp/ # HTTP parsing library
19+
│ ├── libsrtp2/ # SRTP (Secure Real-time Transport Protocol) library
20+
│ ├── libwebsockets/ # WebSocket library
21+
│ ├── network_coprocessor/ # Network coprocessor support
22+
│ `── state_machine/ # State machine implementation
23+
└── examples/ # Example applications
24+
```
25+
26+
## Components
27+
28+
### libsrtp2
29+
The `libsrtp2` component is a port of the Cisco libSRTP library, which provides implementations of the Secure Real-time Transport Protocol (SRTP) and the Secure Real-time Transport Control Protocol (SRTCP). This component is essential for secure media transport in WebRTC applications.
30+
31+
### Other Components
32+
- `kvs_webrtc`: Main component implementing the KVS WebRTC functionality
33+
- `esp_webrtc_utils`: Helper utilities for WebRTC implementation
34+
- `esp_usrsctp`: User-land SCTP implementation for data channel support, specifically modified for Espressif
35+
- `libwebsockets`: WebSocket implementation for signaling
36+
- Additional components provide supporting functionality for the WebRTC stack
37+
38+
## Configuration
39+
40+
Each component may have its own configuration options that can be set through `menuconfig`. Refer to the individual component documentation for specific configuration options.
41+
42+
## Notes
43+
44+
- The port uses mbedTLS for cryptographic operations
45+
- Some components have specific ESP-IDF version requirements
46+
- Check the examples directory for implementation references

esp_port/components/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
This directory contains ESP-IDF components that are required for the Amazon KVS WebRTC SDK port to ESP32 devices. These components provide the necessary functionality for WebRTC communication, media streaming, and device-specific features.
44

55
These components can be used in ESP-IDF projects by adding them as dependencies in the project's `idf_component.yml` file.
6+
7+
Some components here are port from [Amazon Kinesis Video Streams PIC](https://github.com/awslabs/amazon-kinesis-video-streams-pic)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
idf_component_register(SRCS "http_helper.c"
2+
"http_api.c"
3+
"http_api_rsp.c"
4+
"netio.c"
5+
INCLUDE_DIRS "."
6+
REQUIRES kvs_utils mbedtls credential esp_http_client)

0 commit comments

Comments
 (0)