Skip to content

Commit 49a2ab0

Browse files
committed
Added WebRTC split functionality
- Added streaming_only and signaling_only examples - streaming_only can be flashed on P4 and signaling only on C6 - The message exchange between the two happens over webrtc_bridge
1 parent 08051fb commit 49a2ab0

29 files changed

+1834
-59
lines changed

esp_port/components/esp_webrtc_utils/src/esp_work_queue.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
#include <esp_work_queue.h>
1414

1515
#define ESP_WORKQ_SIZE 32
16-
#define ESP_WORKQ_TASK_STACK (60 * 1014)
1716
#define ESP_WORKQ_TASK_PRIO (8)
17+
#if ENABLE_SIGNALLING_ONLY
18+
#define ESP_WORKQ_TASK_STACK (16 * 1014)
19+
#else
20+
#define ESP_WORKQ_TASK_STACK (24 * 1014)
21+
#endif
1822

1923
static const char *TAG = "esp_work_queue";
2024

@@ -129,19 +133,13 @@ esp_err_t esp_work_queue_start(void)
129133
}
130134

131135
StaticTask_t *task_buffer = heap_caps_calloc(1, sizeof(StaticTask_t), MALLOC_CAP_INTERNAL);
132-
// void *task_stack = heap_caps_calloc_prefer(1, ESP_WORKQ_TASK_STACK, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
133-
void *task_stack = heap_caps_malloc(ESP_WORKQ_TASK_STACK, MALLOC_CAP_INTERNAL);
136+
void *task_stack = heap_caps_malloc_prefer(ESP_WORKQ_TASK_STACK, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL);
134137
assert(task_buffer && task_stack);
135138

136139
/* the task never exits, so do not bother to free the buffers */
137140
xTaskCreateStatic(&esp_work_queue_task, "esp_workq_task", ESP_WORKQ_TASK_STACK,
138141
NULL, ESP_WORKQ_TASK_PRIO, task_stack, task_buffer);
139142

140-
// if (xTaskCreate(&esp_work_queue_task, "rmaker_queue_task", ESP_WORKQ_TASK_STACK,
141-
// NULL, ESP_WORKQ_TASK_PRIO, NULL) != pdPASS) {
142-
// ESP_LOGE(TAG, "Couldn't create RainMaker work queue task");
143-
// return ESP_FAIL;
144-
// }
145143
queue_state = WORK_QUEUE_STATE_RUNNING;
146144
return ESP_OK;
147145
}

esp_port/components/esp_webrtc_utils/src/flash_wrapper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ esp_err_t flash_wrapper_init(void)
141141
BaseType_t res = xTaskCreate(
142142
flash_op_task,
143143
"flash_op_task",
144-
8192,
144+
4096,
145145
NULL,
146146
5,
147147
&flash_task_handle

esp_port/components/kvs_webrtc/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ if(CONFIG_USE_ESP_WEBSOCKET_CLIENT)
102102
target_compile_definitions(${COMPONENT_LIB} PUBLIC -DUSE_ESP_WEBSOCKET -DESP_PLATFORM)
103103
endif()
104104

105-
option(ENABLE_KVS_THREADPOOL "Enable support for KVS thread pool in signaling" ON)
105+
# option(ENABLE_KVS_THREADPOOL "Enable support for KVS thread pool in signaling" OFF)
106106
set(KVS_PLAT_ESP_FREERTOS ON CACHE BOOL "Build for ESP FreeRTOS")
107107
add_definitions(-DKVS_PLAT_ESP_FREERTOS)
108-
if (ENABLE_KVS_THREADPOOL)
109-
add_definitions(-DENABLE_KVS_THREADPOOL)
110-
endif()
108+
# if (ENABLE_KVS_THREADPOOL)
109+
# add_definitions(-DENABLE_KVS_THREADPOOL)
110+
# endif()
111111

112112
# Check for ifaddrs.h and poll.h
113113
include(CheckIncludeFiles)

esp_port/components/webrtc_bridge/idf_component.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ dependencies:
55
esp_webrtc_utils:
66
path: ../esp_webrtc_utils
77
version: "*"
8-
# espressif/esp_hosted:
9-
# override_path: ../esp_hosted
10-
# version: "*"
8+
9+
espressif/esp_hosted:
10+
override_path: ../esp_hosted
11+
version: "*"
12+
rules:
13+
- if: "target not in [linux]"
14+
15+
network_coprocessor:
16+
path: ../network_coprocessor
17+
version: "*"
18+
rules:
19+
- if: target in [esp32c6, esp32c5]

esp_port/components/webrtc_bridge/src/webrtc_bridge.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,24 +277,16 @@ void webrtc_bridge_send_message(const char *data, int len)
277277
#ifndef CONFIG_ESP_WEBRTC_BRIDGE_HOSTED
278278
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
279279
{
280-
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id);
281280
esp_mqtt_event_handle_t event = event_data;
282281
esp_mqtt_client_handle_t client = event->client;
283282
int msg_id;
284-
285-
switch ((esp_mqtt_event_id_t)event_id) {
283+
// your_context_t *context = event->context;
284+
switch (event->event_id) {
286285
case MQTT_EVENT_CONNECTED:
286+
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
287287
msg_id = esp_mqtt_client_subscribe(client, FROM_TOPIC, 0);
288288
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
289289

290-
291-
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
292-
// msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0);
293-
// ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
294-
295-
// msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
296-
// ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
297-
298290
// msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
299291
// ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
300292

@@ -320,7 +312,11 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
320312
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
321313
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
322314
printf("DATA=%.*s\r\n", event->data_len, event->data);
323-
on_webrtc_bridge_msg_received(event->data, event->data_len);
315+
if (message_handler) {
316+
message_handler(event->data, event->data_len);
317+
} else {
318+
on_webrtc_bridge_msg_received((void *) event->data, event->data_len);
319+
}
324320
break;
325321
case MQTT_EVENT_ERROR:
326322
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");

esp_port/examples/app_common/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ if (ENABLE_RAINMAKER)
2020
add_definitions(-DENABLE_RAINMAKER)
2121
endif ()
2222

23+
if (ENABLE_DATA_CHANNEL)
24+
add_definitions(-DENABLE_DATA_CHANNEL)
25+
endif()
26+
2327
# Create a SPIFFS image from the contents of the 'spiffs_image' directory
2428
# that fits the partition named 'storage'. FLASH_IN_PROJECT indicates that
2529
# the generated image should be flashed when the entire project is flashed to

esp_port/examples/app_common/idf_component.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ dependencies:
1111
version: "*"
1212
public: true
1313

14-
# espressif/esp_hosted:
15-
# override_path: ../../components/esp_hosted
16-
# version: "*"
14+
credential:
15+
path: ../../components/credential
16+
version: "*"
1717

1818
webrtc_bridge:
1919
path: ../../components/webrtc_bridge
2020
version: "*"
21+
22+
signaling_serializer:
23+
path: ../../components/signaling_serializer
24+
version: "*"

esp_port/examples/app_common/include/sample_config.h

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern "C" {
2121
#define DEFAULT_AUDIO_OPUS_CHANNELS 2
2222
#define DEFAULT_AUDIO_OPUS_SAMPLE_RATE_HZ 48000
2323
#define DEFAULT_AUDIO_OPUS_BITS_PER_SAMPLE 16
24-
#define DEFAULT_MAX_CONCURRENT_STREAMING_SESSION 10
24+
#define DEFAULT_MAX_CONCURRENT_STREAMING_SESSION 2
2525

2626
#define AUDIO_CODEC_NAME_ALAW "alaw"
2727
#define AUDIO_CODEC_NAME_MULAW "mulaw"
@@ -167,6 +167,10 @@ typedef struct {
167167
RtcOnDataChannel onDataChannel;
168168
SignalingClientMetrics signalingClientMetrics;
169169

170+
// Callbacks for signaling messages
171+
VOID (*onAnswer)(UINT64, PSignalingMessage);
172+
VOID (*onIceCandidate)(UINT64, PSignalingMessage);
173+
170174
PStackQueue pPendingSignalingMessageForRemoteClient;
171175
PHashTable pRtcPeerConnectionForRemoteClient;
172176

@@ -272,7 +276,6 @@ STATUS createSampleConfiguration(PCHAR, SIGNALING_CHANNEL_ROLE_TYPE, BOOL, BOOL,
272276
STATUS freeSampleConfiguration(PSampleConfiguration*);
273277
STATUS signalingClientStateChanged(UINT64, SIGNALING_CLIENT_STATE);
274278
STATUS signalingMessageReceived(UINT64, PReceivedSignalingMessage);
275-
STATUS handleAnswer(PSampleConfiguration, PSampleStreamingSession, PSignalingMessage);
276279
STATUS handleOffer(PSampleConfiguration, PSampleStreamingSession, PSignalingMessage);
277280
STATUS handleRemoteCandidate(PSampleStreamingSession, PSignalingMessage);
278281
STATUS initializePeerConnection(PSampleConfiguration, PRtcPeerConnection*);
@@ -283,6 +286,28 @@ STATUS streamingSessionOnShutdown(PSampleStreamingSession, UINT64, StreamSession
283286
STATUS sendSignalingMessage(PSampleStreamingSession, PSignalingMessage);
284287
STATUS respondWithAnswer(PSampleStreamingSession);
285288
STATUS resetSampleConfigurationState(PSampleConfiguration);
289+
290+
#ifdef DYNAMIC_SIGNALING_PAYLOAD
291+
/**
292+
* @brief Allocate memory for the payload of a SignalingMessage
293+
*
294+
* @param[in,out] pSignalingMessage The signaling message for which to allocate payload
295+
* @param[in] size Size in bytes to allocate
296+
*
297+
* @return STATUS code of the execution
298+
*/
299+
STATUS allocateSignalingMessagePayload(PSignalingMessage pSignalingMessage, UINT32 size);
300+
301+
/**
302+
* @brief Free the dynamically allocated payload of a SignalingMessage
303+
*
304+
* @param[in,out] pSignalingMessage The signaling message whose payload should be freed
305+
*
306+
* @return STATUS code of the execution
307+
*/
308+
STATUS freeSignalingMessagePayload(PSignalingMessage pSignalingMessage);
309+
#endif
310+
286311
VOID sampleVideoFrameHandler(UINT64, PFrame);
287312
VOID sampleAudioFrameHandler(UINT64, PFrame);
288313
VOID sampleFrameHandler(UINT64, PFrame);
@@ -306,4 +331,4 @@ UINT32 setLogLevel();
306331
#ifdef __cplusplus
307332
}
308333
#endif
309-
#endif /* __KINESIS_VIDEO_SAMPLE_INCLUDE__ */
334+
#endif /* __KINESIS_VIDEO_SAMPLE_INCLUDE__ */

0 commit comments

Comments
 (0)