Skip to content

Commit 7102bdc

Browse files
committed
Make peer_default as seperate component v1.2.1
1 parent 7c670a5 commit 7102bdc

39 files changed

+1351
-261
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Push components to https://components.espressif.com
2+
on:
3+
push:
4+
branches:
5+
- main
6+
jobs:
7+
upload_components:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4
11+
with:
12+
submodules: "recursive"
13+
- name: Upload components to the component registry
14+
uses: espressif/upload-components-ci-action@v2
15+
with:
16+
components: |
17+
esp_peer:./components/esp_peer
18+
namespace: "espressif"
19+
api_token: ${{ secrets.IDF_COMPONENT_API_TOKEN }}

.gitlab/ci/build_webrtc_solutions.yml

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,39 @@
1616
artifacts:
1717
expire_in: 4 days
1818
paths:
19-
- ${CI_PROJECT_DIR}/solutions/$CI_BUILD_FOLDER/build*/size.json
20-
- ${CI_PROJECT_DIR}/solutions/$CI_BUILD_FOLDER/build*/build_log.txt
21-
- ${CI_PROJECT_DIR}/solutions/$CI_BUILD_FOLDER/build*/*.bin
22-
- ${CI_PROJECT_DIR}/solutions/$CI_BUILD_FOLDER/build*/*.elf
23-
- ${CI_PROJECT_DIR}/solutions/$CI_BUILD_FOLDER/build*/flasher_args.json
24-
- ${CI_PROJECT_DIR}/solutions/$CI_BUILD_FOLDER/build*/flash_project_args
25-
- ${CI_PROJECT_DIR}/solutions/$CI_BUILD_FOLDER/build*/config/sdkconfig.json
26-
- ${CI_PROJECT_DIR}/solutions/$CI_BUILD_FOLDER/build*/sdkconfig
27-
- ${CI_PROJECT_DIR}/solutions/$CI_BUILD_FOLDER/build*/bootloader/*.bin
28-
- ${CI_PROJECT_DIR}/solutions/$CI_BUILD_FOLDER/build*/partition_table/*.bin
19+
- ${CI_PROJECT_DIR}/$CI_BUILD_FOLDER/build*/size.json
20+
- ${CI_PROJECT_DIR}/$CI_BUILD_FOLDER/build*/build_log.txt
21+
- ${CI_PROJECT_DIR}/$CI_BUILD_FOLDER/build*/*.bin
22+
- ${CI_PROJECT_DIR}/$CI_BUILD_FOLDER/build*/*.elf
23+
- ${CI_PROJECT_DIR}/$CI_BUILD_FOLDER/build*/flasher_args.json
24+
- ${CI_PROJECT_DIR}/$CI_BUILD_FOLDER/build*/flash_project_args
25+
- ${CI_PROJECT_DIR}/$CI_BUILD_FOLDER/build*/config/sdkconfig.json
26+
- ${CI_PROJECT_DIR}/$CI_BUILD_FOLDER/build*/sdkconfig
27+
- ${CI_PROJECT_DIR}/$CI_BUILD_FOLDER/build*/bootloader/*.bin
28+
- ${CI_PROJECT_DIR}/$CI_BUILD_FOLDER/build*/partition_table/*.bin
2929
script:
3030
- export OPENAI_API_KEY=FAKE_KEY_FOR_BUILD_ONLY
3131
- cd $IDF_PATH
32-
- perl ${CI_PROJECT_DIR}/tools/build_apps.pl ${CI_PROJECT_DIR}/solutions/$CI_BUILD_FOLDER $IDF_TARGET
32+
- perl ${CI_PROJECT_DIR}/tools/build_apps.pl ${CI_PROJECT_DIR}/$CI_BUILD_FOLDER $IDF_TARGET
3333

3434
build_matrix:
3535
extends: .build_solutions
3636
parallel:
3737
matrix:
38-
- CI_BUILD_FOLDER: ["doorbell_demo"]
38+
- CI_BUILD_FOLDER: ["solutions/doorbell_demo"]
3939
IDF_TARGET: ["esp32p4", "esp32s3"]
40-
- CI_BUILD_FOLDER: ["openai_demo"]
40+
- CI_BUILD_FOLDER: ["solutions/openai_demo"]
4141
IDF_TARGET: ["esp32s3"]
42-
- CI_BUILD_FOLDER: ["peer_demo"]
42+
- CI_BUILD_FOLDER: ["solutions/peer_demo"]
4343
IDF_TARGET: ["esp32", "esp32s2", "esp32s3"]
44-
- CI_BUILD_FOLDER: ["videocall_demo"]
44+
- CI_BUILD_FOLDER: ["solutions/videocall_demo"]
4545
IDF_TARGET: ["esp32p4"]
46-
- CI_BUILD_FOLDER: ["whip_demo"]
46+
- CI_BUILD_FOLDER: ["solutions/whip_demo"]
4747
IDF_TARGET: ["esp32p4"]
48-
- CI_BUILD_FOLDER: ["doorbell_local"]
48+
- CI_BUILD_FOLDER: ["solutions/doorbell_local"]
4949
IDF_TARGET: ["esp32p4"]
50+
- CI_BUILD_FOLDER: ["components/esp_peer/examples/peer_demo"]
51+
IDF_TARGET: ["esp32p4", "esp32s3"]
5052

5153
stages:
5254
- build

components/esp_webrtc/impl/peer_default/CHANGELOG.md renamed to components/esp_peer/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## v1.2.1
4+
5+
### Features
6+
7+
- Make `esp_peer` as separate module for ESP Component Registry
8+
- Allow RTP rolling buffer allocated on RAM
9+
10+
### Bug Fixes
11+
12+
- Fixed handshake may error if agent receive handshake message during connectivity check
13+
314
## v1.2.0
415

516
### Features

components/esp_peer/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
idf_component_register(INCLUDE_DIRS ./include SRC_DIRS "src")
2+
3+
get_filename_component(BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR} NAME)
4+
add_prebuilt_library(${BASE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/libs/${IDF_TARGET}/libpeer_default.a"
5+
PRIV_REQUIRES ${BASE_DIR} esp_timer mbedtls)
6+
target_link_libraries(${COMPONENT_LIB} PRIVATE "-L ${CMAKE_CURRENT_SOURCE_DIR}/libs/${IDF_TARGET}")
7+
target_link_libraries(${COMPONENT_LIB} PRIVATE esp_peer)

components/esp_peer/LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Espressif Modified MIT License
2+
3+
Copyright (c) 2025 Espressif Systems (Shanghai) CO., LTD
4+
5+
Permission is hereby granted for use EXCLUSIVELY with Espressif Systems products.
6+
This includes the right to use, copy, modify, merge, publish, distribute, and sublicense
7+
the Software, subject to the following conditions:
8+
9+
1. This Software MUST BE USED IN CONJUNCTION WITH ESPRESSIF SYSTEMS PRODUCTS.
10+
2. The above copyright notice and this permission notice shall be included in all copies
11+
or substantial portions of the Software.
12+
3. Redistribution of the Software in source or binary form FOR USE WITH NON-ESPRESSIF PRODUCTS
13+
is strictly prohibited.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17+
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
18+
FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
DEALINGS IN THE SOFTWARE.
21+
22+
SPDX-License-Identifier: LicenseRef-Espressif-Modified-MIT

components/esp_peer/README.md

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
# ESP Peer – WebRTC PeerConnection Component
2+
3+
`esp_peer` is a full-featured WebRTC PeerConnection implementation optimized for Espressif platforms. It enables peer-to-peer communication with audio, video, and data channels, adhering to the standard WebRTC protocol stack.
4+
5+
---
6+
7+
## 🔍 Overview
8+
9+
Derived from [libpeer](https://github.com/sepfy/libpeer.git), `esp_peer` adds platform-specific enhancements and optimizations for the ESP32 series. It provides a production-grade WebRTC stack supporting:
10+
11+
- **ICE** – Interactive Connectivity Establishment
12+
- **DTLS** – Datagram Transport Layer Security
13+
- **SCTP** – Stream Control Transmission Protocol
14+
- **RTP/SRTP** – Real-time Transport Protocol with secure variants
15+
16+
---
17+
18+
## ✨ Key Features
19+
20+
### ✅ WebRTC Protocol Stack
21+
22+
- **TURN Support**: Implements [RFC5766](https://datatracker.ietf.org/doc/html/rfc5766) and [RFC8656](https://datatracker.ietf.org/doc/html/rfc8656)
23+
- **Dual ICE Roles**: Operates as *Controlling* or *Controlled*
24+
- **Optimized ICE Pairing**: Fast and efficient candidate selection
25+
- **RTP Reliability**: Implements NACK, jitter buffering, and retransmission
26+
- **Robust SCTP**: Supports multi-streaming, fragmentation, and SACK
27+
- **Threaded Operation**: Non-blocking send/receive with dedicated tasks
28+
29+
### 🎹 Media Support
30+
31+
- **Audio Codecs**: G711A (PCMA), G711U (PCMU), OPUS
32+
- **Video Codecs**: H.264, MJPEG
33+
- **Data Channels**: Reliable/unreliable; ordered/unordered modes
34+
- **Flexible Media Modes**: Send-only, receive-only, or full-duplex
35+
36+
### 🧹 Modular & Configurable
37+
38+
- **Pluggable Interfaces**: Clean abstraction via `esp_peer_ops_t`
39+
- **Default Implementation**: Ready-to-use via `esp_peer_get_default_impl()`
40+
- **Resource Control**: Tune memory usage, timeouts, buffer sizes
41+
42+
---
43+
44+
## 🚀 Getting Started
45+
46+
### 1️⃣ Peer Configuration
47+
48+
```c
49+
esp_peer_cfg_t cfg = {
50+
.role = ESP_PEER_ROLE_CONTROLLING,
51+
.ice_trans_policy = ESP_PEER_ICE_TRANS_POLICY_ALL,
52+
.audio_info = {
53+
.codec = ESP_PEER_AUDIO_CODEC_OPUS,
54+
.sample_rate = 48000,
55+
.channel = 1
56+
},
57+
.video_info = {
58+
.codec = ESP_PEER_VIDEO_CODEC_H264,
59+
.width = 640,
60+
.height = 480,
61+
.fps = 30
62+
},
63+
.audio_dir = ESP_PEER_MEDIA_DIR_SEND_RECV,
64+
.video_dir = ESP_PEER_MEDIA_DIR_SEND_RECV,
65+
.enable_data_channel = true,
66+
.ctx = user_context,
67+
.on_state = state_callback,
68+
.on_msg = message_callback,
69+
.on_audio_data = audio_callback,
70+
.on_video_data = video_callback,
71+
.on_data = data_callback
72+
};
73+
```
74+
75+
---
76+
77+
### 2️⃣ Migration: Browser WebRTC → ESP Peer
78+
79+
Easily migrate from browser-style WebRTC code to the embedded `esp_peer` API. Refer to [`peer_demo.c`](examples/peer_demo/main/peer_demo.c) for a complete walkthrough.
80+
81+
#### 🔄 API Mapping
82+
83+
| Browser WebRTC | ESP Peer | Notes |
84+
| -------------------------------------------------- | ----------------------------------- | --------------------------------- |
85+
| `new RTCPeerConnection()` | `esp_peer_open()` | Configuration-based instantiation |
86+
| `pc.onicecandidate` | `.on_msg` callback | Candidate/SDP exchange |
87+
| `pc.onconnectionstatechange` | `.on_state` callback | Connection lifecycle |
88+
| `pc.ontrack` | `.on_audio_data` / `.on_video_data` | Media reception |
89+
| `pc.ondatachannel` | `.on_data` callback | Data channel messages |
90+
| `createOffer()` / `createAnswer()` | `esp_peer_new_connection()` | Auto-generates SDP |
91+
| `setLocalDescription()` / `setRemoteDescription()` | `esp_peer_send_msg()` | Exchange SDP manually |
92+
| `dataChannel.send()` | `esp_peer_send_data()` | Send data via SCTP |
93+
| Manual event loop | `esp_peer_main_loop()` | Handled in background task |
94+
95+
---
96+
97+
### 3️⃣ Data Channel Usage
98+
99+
#### ✔️ Auto Creation
100+
101+
If `.enable_data_channel = true`:
102+
103+
- **Client Role**: Creates the default channel automatically
104+
- **Server Role**: Waits for peer to create and pair the channel
105+
106+
#### 🛠 Manual Creation
107+
108+
For advanced use cases (e.g., multiple or custom-configured channels):
109+
110+
```c
111+
esp_peer_cfg_t cfg = {
112+
.enable_data_channel = true,
113+
.manual_ch_create = true,
114+
// ...
115+
};
116+
esp_peer_open(&cfg, esp_peer_get_default_impl(), &peer);
117+
118+
// After ESP_PEER_STATE_DATA_CHANNEL_CONNECTED
119+
esp_peer_data_channel_cfg_t ch_cfg = {
120+
.type = ESP_PEER_DATA_CHANNEL_RELIABLE,
121+
.ordered = true,
122+
.label = "my_channel"
123+
};
124+
esp_peer_create_data_channel(peer, &ch_cfg);
125+
126+
// Once open:
127+
esp_peer_data_frame_t data_frame = {
128+
.type = ESP_PEER_DATA_CHANNEL_STRING,
129+
.stream_id = 0,
130+
.data = (uint8_t*)"Hello WebRTC!",
131+
.size = 13
132+
};
133+
esp_peer_send_data(peer, &data_frame);
134+
```
135+
136+
## 4️⃣ Signaling Explanation
137+
138+
WebRTC requires a signaling mechanism to exchange SDP and ICE candidates between peers. This module **does not provide a built-in signaling transport**.
139+
140+
You can refer to the complete [esp-webrtc-solution](https://github.com/espressif/esp-webrtc-solution) which includes practical signaling implementations such as handy OpenAI, WHIP, and AppRTC, or you may develop a custom signaling system tailored to your application.
141+
142+
---
143+
144+
## ⚙️ Fine-Tuning (Optional)
145+
146+
Tune for memory or performance trade-offs using `esp_peer_default_cfg_t`:
147+
148+
```c
149+
esp_peer_default_cfg_t default_cfg = {
150+
.agent_recv_timeout = 100,
151+
.data_ch_cfg = {
152+
.cache_timeout = 5000,
153+
.send_cache_size = 102400,
154+
.recv_cache_size = 102400,
155+
},
156+
.rtp_cfg = {
157+
.audio_recv_jitter = {
158+
.cache_timeout = 100,
159+
.resend_delay = 20,
160+
.cache_size = 102400,
161+
},
162+
.video_recv_jitter = {
163+
.cache_timeout = 100,
164+
.resend_delay = 20,
165+
.cache_size = 409600,
166+
},
167+
.send_pool_size = 409600,
168+
.send_queue_num = 256,
169+
.max_resend_count = 3
170+
}
171+
};
172+
173+
cfg.extra_cfg = &default_cfg;
174+
cfg.extra_size = sizeof(default_cfg);
175+
esp_peer_open(&cfg, esp_peer_get_default_impl(), &peer);
176+
```
177+
178+
---
179+
180+
## 📉 Minimum Resource Requirements
181+
182+
`esp_peer` is highly configurable to support low-memory environments. Even on platforms **without PSRAM**, a minimal setup uses **< 60 KB** RAM.
183+
184+
See the [`peer_demo`](examples/peer_demo) example for how two peers can run concurrently on an **ESP32-S3** without external memory.
185+
186+
---
187+
188+
## 📦 Dependencies
189+
190+
### Libraries
191+
192+
- **libSRTP** – Secure RTP (bundled)
193+
- **mbedTLS** – Required for DTLS (bundled with ESP-IDF)
194+
195+
### ESP-IDF Settings
196+
197+
Ensure these config options are enabled:
198+
199+
```ini
200+
CONFIG_MBEDTLS_SSL_PROTO_DTLS=y
201+
CONFIG_MBEDTLS_SSL_DTLS_SRTP=y
202+
```
203+
204+
---
205+
206+
## 🔀 PeerConnection State Machine
207+
208+
```mermaid
209+
stateDiagram-v2
210+
direction LR
211+
[*] --> NEW_CONNECTION
212+
NEW_CONNECTION --> PAIRING
213+
PAIRING --> PAIRED
214+
PAIRED --> CONNECTING
215+
CONNECTING --> CONNECTED
216+
CONNECTED --> DATA_CHANNEL_CONNECTED
217+
DATA_CHANNEL_CONNECTED --> DATA_CHANNEL_OPENED
218+
DATA_CHANNEL_OPENED --> DISCONNECTED
219+
DISCONNECTED --> CLOSED
220+
CLOSED --> [*]
221+
```
222+
223+
---
224+
225+
## 🧠 Performance Tips
226+
227+
- **Tune Buffer Sizes**: Trade latency for memory based on use case
228+
- **Adjust Timeouts**: Adapt to high-latency or lossy networks
229+
- **Use Dedicated Task**: Run `esp_peer_main_loop()` in its own thread
230+
- **Profile Resource Usage**: Monitor heap and stack for optimization
231+
232+
---
233+
234+
## 📬 Contact & Support
235+
236+
This component is part of the [esp-webrtc-solution](https://github.com/espressif/esp-webrtc-solution), offering complete WebRTC capabilities including media stream (esp_capture, av_render), varieties of signaling (OpenAI, WHIP, AppRTC, etc.) and many practical examples.
237+
238+
🔧 Found a bug? Have a suggestion?\
239+
Open an issue on GitHub: [esp-webrtc-solution/issues](https://github.com/espressif/esp-webrtc-solution/issues)
240+
241+
We’re here to help!
242+
243+
---
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The following lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
7+
project(peer_demo)

0 commit comments

Comments
 (0)