Skip to content

Commit 3544453

Browse files
committed
Add multiple data channel support
1 parent 119d7e8 commit 3544453

File tree

20 files changed

+595
-43
lines changed

20 files changed

+595
-43
lines changed

components/esp_capture/src/impl/capture_video_src/capture_video_v4l2_src.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
#define TAG "V4L2_SRC"
4141

42+
#define MAX_BUFS (4)
4243
#define MAX_SUPPORT_FORMATS_NUM (4)
4344
#define FMT_STR(fmt) ((uint8_t *)&fmt)[0], ((uint8_t *)&fmt)[1], ((uint8_t *)&fmt)[2], ((uint8_t *)&fmt)[3]
4445

@@ -49,9 +50,9 @@ typedef struct {
4950
esp_capture_codec_type_t support_formats[MAX_SUPPORT_FORMATS_NUM];
5051
uint8_t format_count;
5152
int fd;
52-
uint8_t *fb_buffer[2];
53-
struct v4l2_buffer v4l2_buf[2];
54-
bool fb_used[2];
53+
uint8_t *fb_buffer[MAX_BUFS];
54+
struct v4l2_buffer v4l2_buf[MAX_BUFS];
55+
bool fb_used[MAX_BUFS];
5556
bool nego_ok;
5657
bool started;
5758
} v4l2_src_t;
@@ -289,10 +290,8 @@ static int v4l2_close(esp_capture_video_src_if_t *src)
289290
return -1;
290291
}
291292
for (int i = 0; i < v4l2->buf_count; i++) {
292-
struct v4l2_buffer *buf = &v4l2->v4l2_buf[i];
293293
if (v4l2->fb_used[i]) {
294294
v4l2->fb_used[i] = 0;
295-
ioctl(v4l2->fd, VIDIOC_QBUF, buf);
296295
}
297296
}
298297
if (v4l2->fd > 0) {
@@ -320,8 +319,7 @@ esp_capture_video_src_if_t *esp_capture_new_video_v4l2_src(esp_capture_video_v4l
320319
v4l2->base.stop = v4l2_stop;
321320
v4l2->base.close = v4l2_close;
322321
strncpy(v4l2->dev_name, cfg->dev_name, sizeof(v4l2->dev_name));
323-
// TODO limit to 2
324-
v4l2->buf_count = cfg->buf_count > 2 ? 2 : cfg->buf_count;
322+
v4l2->buf_count = (cfg->buf_count > MAX_BUFS ? MAX_BUFS : cfg->buf_count);
325323
return &v4l2->base;
326324
}
327325
#endif
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Changelog
2+
3+
## v1.1.0
4+
5+
### Features
6+
7+
- Export buffer configuration for RTP and data channel
8+
- Added support for multiple data channel
9+
- Added notify for data channel open, close event
10+
11+
### Bug Fixes
12+
13+
- Fixed keep alive check not take effect if peer closed unexpectly
14+
15+
16+
## v1.0.0
17+
18+
- Initial version of `peer_default`
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version: 0.0.1
1+
version: 1.1.0

components/esp_webrtc/impl/peer_default/include/esp_peer_default.h

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,54 @@ extern "C" {
3131
#endif
3232

3333
/**
34-
* @brief Peer default configuration
34+
* @brief Peer default data channel configuration
3535
*/
3636
typedef struct {
37-
uint16_t agent_recv_timeout; /*!< Agent receive timeout setting*/
38-
} esp_peer_default_cfg_t;
37+
uint16_t cache_timeout; /*!< Data channel frame keep timeout (unit ms) default: 5000ms if set to 0 */
38+
uint32_t send_cache_size; /*!< Cache size for outgoing data channel packets (unit Bytes)
39+
default: 100kB if set to 0 */
40+
uint32_t recv_cache_size; /*!< Cache size for incoming data channel packets (unit Bytes)
41+
default: 100kB if set to 0 */
42+
} esp_peer_default_data_ch_cfg_t;
43+
44+
/**
45+
* @brief Peer default jitter buffer configuration
46+
*/
47+
typedef struct {
48+
uint16_t cache_timeout; /*!< Maximum timeout to keep the received RTP packet (unit ms) default: 100ms if set to 0 */
49+
uint16_t resend_delay; /*!< Not resend until resend delay reached (unit ms) default: 20ms if set to 0*/
50+
uint32_t cache_size; /*!< Cache size for incoming data channel frame (unit Bytes)
51+
For audio jitter buffer default: 100kB if set to 0
52+
For video jitter buffer default: 400kB if set to 0 */
53+
} esp_peer_default_jitter_cfg_t;
54+
55+
/**
56+
* @brief Peer default RTP send and receive configuration
57+
*
58+
* @note Insufficient RTP resources can disable features like packet retransmission and loss reporting
59+
* Without these features, packet loss may result in audio glitches and mosaic-like video artifacts
60+
*/
61+
typedef struct {
62+
esp_peer_default_jitter_cfg_t audio_recv_jitter; /*!< Audio jitter buffer configuration */
63+
esp_peer_default_jitter_cfg_t video_recv_jitter; /*!< Video jitter buffer configuration */
64+
uint32_t send_pool_size; /*!< Send pool size for outgoing RTP packets (unit Bytes)
65+
default: 400kB if set to 0 */
66+
uint32_t send_queue_num; /*!< Maximum queue number to hold outgoing RTP packet metadata info
67+
default: 256 */
68+
uint16_t max_resend_count; /*!< Maximum resend count for one RTP packet
69+
default: 3 times */
70+
} esp_peer_default_rtp_cfg_t;
3971

72+
/**
73+
* @brief Peer default configuration (optional)
74+
*/
75+
typedef struct {
76+
uint16_t agent_recv_timeout; /*!< ICE agent receive timeout setting (unit ms)
77+
default: 100ms if set to 0
78+
Some STUN/TURN server reply message slow increase this value */
79+
esp_peer_default_data_ch_cfg_t data_ch_cfg; /*!< Configuration of data channel */
80+
esp_peer_default_rtp_cfg_t rtp_cfg; /*!< Configuration of RTP buffer */
81+
} esp_peer_default_cfg_t;
4082

4183
#ifdef __cplusplus
4284
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

components/esp_webrtc/include/esp_peer.h

Lines changed: 107 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,18 @@ extern "C" {
3434
* @brief Peer state
3535
*/
3636
typedef enum {
37-
ESP_PEER_STATE_CLOSED = 0, /*!< Closed */
38-
ESP_PEER_STATE_DISCONNECTED = 1, /*!< Disconnected */
39-
ESP_PEER_STATE_NEW_CONNECTION = 2, /*!< New connection comming */
40-
ESP_PEER_STATE_PAIRING = 3, /*!< Under candidate pairing */
41-
ESP_PEER_STATE_PAIRED = 4, /*!< Candidate pairing success */
42-
ESP_PEER_STATE_CONNECTING = 5, /*!< Building connection with peer */
43-
ESP_PEER_STATE_CONNECTED = 6, /*!< Connected with peer */
44-
ESP_PEER_STATE_CONNECT_FAILED = 7, /*!< Connect failed */
45-
ESP_PEER_STATE_DATA_CHANNEL_OPENED = 8, /*!< Data channel is opened */
46-
ESP_PEER_STATE_DATA_CHANNEL_CLOSED = 9, /*!< Data channel is closed */
37+
ESP_PEER_STATE_CLOSED = 0, /*!< Closed */
38+
ESP_PEER_STATE_DISCONNECTED = 1, /*!< Disconnected */
39+
ESP_PEER_STATE_NEW_CONNECTION = 2, /*!< New connection comming */
40+
ESP_PEER_STATE_PAIRING = 3, /*!< Under candidate pairing */
41+
ESP_PEER_STATE_PAIRED = 4, /*!< Candidate pairing success */
42+
ESP_PEER_STATE_CONNECTING = 5, /*!< Building connection with peer */
43+
ESP_PEER_STATE_CONNECTED = 6, /*!< Connected with peer */
44+
ESP_PEER_STATE_CONNECT_FAILED = 7, /*!< Connect failed */
45+
ESP_PEER_STATE_DATA_CHANNEL_CONNECTED = 8, /*!< Data channel is connected */
46+
ESP_PEER_STATE_DATA_CHANNEL_OPENED = 9, /*!< Data channel is opened */
47+
ESP_PEER_STATE_DATA_CHANNEL_CLOSED = 10, /*!< Data channel is closed */
48+
ESP_PEER_STATE_DATA_CHANNEL_DISCONNECTED = 11, /*!< Data channel is disconencted */
4749
} esp_peer_state_t;
4850

4951
/**
@@ -134,9 +136,10 @@ typedef struct {
134136
* @brief Data frame information
135137
*/
136138
typedef struct {
137-
esp_peer_data_channel_type_t type; /*!< Data channel type */
138-
uint8_t *data; /*!< Pointer to data to be sent through data channel */
139-
int size; /*!< Data size */
139+
esp_peer_data_channel_type_t type; /*!< Data channel type */
140+
uint16_t stream_id; /*!< Data channel stream ID */
141+
uint8_t *data; /*!< Pointer to data to be sent through data channel */
142+
int size; /*!< Data size */
140143
} esp_peer_data_frame_t;
141144

142145
/**
@@ -157,6 +160,21 @@ typedef struct {
157160
int size; /*!< Message data size */
158161
} esp_peer_msg_t;
159162

163+
/**
164+
* @brief Peer data channel configuration for create
165+
*/
166+
typedef struct {
167+
char *label; /*!< Data channel label */
168+
} esp_peer_data_channel_cfg_t;
169+
170+
/**
171+
* @brief Peer data channel information
172+
*/
173+
typedef struct {
174+
const char *label; /*!< Data channel label */
175+
uint16_t stream_id; /*!< Chunk stream id for this channel */
176+
} esp_peer_data_channel_info_t;
177+
160178
/**
161179
* @brief Peer handle
162180
*/
@@ -174,7 +192,12 @@ typedef struct {
174192
esp_peer_video_stream_info_t video_info; /*!< Video stream information */
175193
esp_peer_media_dir_t audio_dir; /*!< Audio transmission direction */
176194
esp_peer_media_dir_t video_dir; /*!< Video transmission direction */
195+
bool no_auto_reconnect; /*!< Disable auto reconnect if connected fail */
177196
bool enable_data_channel; /*!< Enable data channel */
197+
bool manual_ch_create; /*!< Manual create data channel
198+
When SCTP role is client, it will try to send DCEP automatically
199+
To disable this behavior can create data channel manually and set this flag
200+
*/
178201
void *extra_cfg; /*!< Extra configuration */
179202
int extra_size; /*!< Extra configuration size */
180203
void *ctx; /*!< User context */
@@ -227,13 +250,29 @@ typedef struct {
227250
*/
228251
int (*on_video_data)(esp_peer_video_frame_t* frame, void* ctx);
229252

253+
/**
254+
* @brief Peer data channel opened event callback
255+
* @param[in] ch Data channel information
256+
* @param[in] ctx User context
257+
* @return Status code indicating success or failure.
258+
*/
259+
int (*on_channel_open)(esp_peer_data_channel_info_t *ch, void *ctx);
260+
230261
/**
231262
* @brief Peer data frame callback
232263
* @param[in] frame Data frame information
233264
* @param[in] ctx User context
234265
* @return Status code indicating success or failure.
235266
*/
236-
int (*on_data)(esp_peer_data_frame_t* frame, void* ctx);
267+
int (*on_data)(esp_peer_data_frame_t *frame, void *ctx);
268+
269+
/**
270+
* @brief Peer data channel closed event callback
271+
* @param[in] ch Data channel information
272+
* @param[in] ctx User context
273+
* @return Status code indicating success or failure.
274+
*/
275+
int (*on_channel_close)(esp_peer_data_channel_info_t *ch, void *ctx);
237276
} esp_peer_cfg_t;
238277

239278
/**
@@ -296,6 +335,22 @@ typedef struct {
296335
*/
297336
int (*send_data)(esp_peer_handle_t peer, esp_peer_data_frame_t* frame);
298337

338+
/**
339+
* @brief Manually create data chanel for peer
340+
* @param[in] peer Peer handle
341+
* @param[in] ch_cfg Data channel configuration
342+
* @return Status code indicating success or failure.
343+
*/
344+
int (*create_data_channel)(esp_peer_handle_t peer, esp_peer_data_channel_cfg_t *ch_cfg);
345+
346+
/**
347+
* @brief Close data chanel for peer
348+
* @param[in] peer Peer handle
349+
* @param[in] label Data channel label
350+
* @return Status code indicating success or failure.
351+
*/
352+
int (*close_data_channel)(esp_peer_handle_t peer, const char *label);
353+
299354
/**
300355
* @brief Peer main loop
301356
* @note Peer connection need handle peer status change, receive stream data in this loop
@@ -338,7 +393,7 @@ typedef struct {
338393
* - ESP_PEER_ERR_INVALID_ARG Invalid argument
339394
* - ESP_PEER_ERR_NOT_SUPPORT Not support
340395
*/
341-
int esp_peer_open(esp_peer_cfg_t *cfg, const esp_peer_ops_t *ops, esp_peer_handle_t *handle);
396+
int esp_peer_open(esp_peer_cfg_t *cfg, const esp_peer_ops_t *ops, esp_peer_handle_t *peer);
342397

343398
/**
344399
* @brief Create new conenction
@@ -353,22 +408,53 @@ int esp_peer_open(esp_peer_cfg_t *cfg, const esp_peer_ops_t *ops, esp_peer_handl
353408
* - ESP_PEER_ERR_INVALID_ARG Invalid argument
354409
* - ESP_PEER_ERR_NOT_SUPPORT Not support
355410
*/
356-
int esp_peer_new_connection(esp_peer_handle_t handle);
411+
int esp_peer_new_connection(esp_peer_handle_t peer);
412+
413+
/**
414+
* @brief Manually create data channel
415+
*
416+
* @note It will send DCEP event to peer until data channel created
417+
*
418+
* @param[in] peer Peer handle
419+
* @param[in] ch_cfg Configuration for data channel creation
420+
*
421+
* @return
422+
* - ESP_PEER_ERR_NONE Open data channel success
423+
* - ESP_PEER_ERR_INVALID_ARG Invalid argument
424+
* - ESP_PEER_ERR_NOT_SUPPORT Not support
425+
*/
426+
int esp_peer_create_data_channel(esp_peer_handle_t peer, esp_peer_data_channel_cfg_t *ch_cfg);
427+
428+
/**
429+
* @brief Manually close data channel by label
430+
*
431+
* @param[in] peer Peer handle
432+
* @param[in] label Channel label
433+
*
434+
* @return
435+
* - ESP_PEER_ERR_NONE Close data channel success
436+
* - ESP_PEER_ERR_INVALID_ARG Invalid argument
437+
* - ESP_PEER_ERR_NOT_SUPPORT Not support
438+
*/
439+
int esp_peer_close_data_channel(esp_peer_handle_t peer, const char *label);
357440

358441
/**
359442
* @brief Update ICE server information
360443
*
361444
* @note After new connection is created, It will try gather ICE candidate from ICE servers.
362445
* And report local SDP to let user send to signaling server.
363446
*
364-
* @param[in] peer Peer handle
447+
* @param[in] peer Peer handle
448+
* @param[in] role Peer roles of controlling or controlled
449+
* @param[in] server Pointer to array of ICE server configuration
450+
* @param[in] server_num ICE server number
365451
*
366452
* @return
367453
* - ESP_PEER_ERR_NONE Open peer connection success
368454
* - ESP_PEER_ERR_INVALID_ARG Invalid argument
369455
* - ESP_PEER_ERR_NOT_SUPPORT Not support
370456
*/
371-
int esp_peer_update_ice_info(esp_peer_handle_t handle, esp_peer_role_t role, esp_peer_ice_server_cfg_t* server, int server_num);
457+
int esp_peer_update_ice_info(esp_peer_handle_t peer, esp_peer_role_t role, esp_peer_ice_server_cfg_t* server, int server_num);
372458

373459
/**
374460
* @brief Send message to peer
@@ -453,7 +539,7 @@ int esp_peer_main_loop(esp_peer_handle_t peer);
453539
* - ESP_PEER_ERR_INVALID_ARG Invalid argument
454540
* - ESP_PEER_ERR_NOT_SUPPORT Not support
455541
*/
456-
int esp_peer_disconnect(esp_peer_handle_t handle);
542+
int esp_peer_disconnect(esp_peer_handle_t peer);
457543

458544
/**
459545
* @brief Query of peer connection
@@ -466,7 +552,7 @@ int esp_peer_disconnect(esp_peer_handle_t handle);
466552
* - ESP_PEER_ERR_NONE Open peer connection success
467553
* - ESP_PEER_ERR_INVALID_ARG Invalid argument
468554
*/
469-
int esp_peer_query(esp_peer_handle_t handle);
555+
int esp_peer_query(esp_peer_handle_t peer);
470556

471557
/**
472558
* @brief Close peer connection
@@ -480,7 +566,7 @@ int esp_peer_query(esp_peer_handle_t handle);
480566
* - ESP_PEER_ERR_INVALID_ARG Invalid argument
481567
* - ESP_PEER_ERR_NOT_SUPPORT Not support
482568
*/
483-
int esp_peer_close(esp_peer_handle_t handle);
569+
int esp_peer_close(esp_peer_handle_t peer);
484570

485571
#ifdef __cplusplus
486572
}

0 commit comments

Comments
 (0)