Skip to content

Commit e52c8fa

Browse files
committed
webrtc message utils
- Refactored webrtc_message re-assembly logic to message utils - Any componenent can include message_utils.h to leverage the same - Use message_utils in webrtc_bridge and register for network_coprocessor messages
1 parent 9533399 commit e52c8fa

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

esp_port/components/esp_webrtc_utils/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set(srcs "src/esp_cli.c" "src/wifi_cli.c" "src/webrtc_mem_utils.c"
2-
"src/esp_work_queue.c" "src/esp_webrtc_time.c" "src/flash_wrapper.c")
2+
"src/esp_work_queue.c" "src/esp_webrtc_time.c" "src/flash_wrapper.c"
3+
"src/message_utils.c")
34

45
set(requires esp_wifi console mqtt esp_timer esp_common freertos spiffs)
56

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include <stdlib.h>
10+
#include "esp_err.h"
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
/**
17+
* @brief Structure to hold received message data
18+
*/
19+
typedef struct received_msg {
20+
char *buf; /*!< Buffer to store message data */
21+
int capacity; /*!< Total capacity of the buffer */
22+
int data_size; /*!< Current size of data in the buffer */
23+
} received_msg_t;
24+
25+
/**
26+
* @brief Creates a buffer for received messages
27+
*
28+
* @param capacity Size of the buffer to create
29+
* @return Pointer to the created buffer structure or NULL if failed
30+
*/
31+
received_msg_t *esp_webrtc_create_buffer_for_msg(int capacity);
32+
33+
/**
34+
* @brief Appends message data to an existing buffer
35+
*
36+
* @param dst_msg Destination message buffer
37+
* @param data_ptr Pointer to the data to append
38+
* @param data_len Length of the data to append
39+
* @param is_fin Flag indicating if this is the final fragment
40+
* @return ESP_OK if message is complete, ESP_ERR_NOT_FINISHED if more fragments expected, ESP_FAIL on error
41+
*/
42+
esp_err_t esp_webrtc_append_msg_to_existing(received_msg_t *dst_msg, void *data_ptr, int data_len, bool is_fin);
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <string.h>
8+
#include "esp_log.h"
9+
#include "esp_heap_caps.h"
10+
#include "message_utils.h"
11+
12+
static const char *TAG = "message_utils";
13+
14+
received_msg_t *esp_webrtc_create_buffer_for_msg(int capacity)
15+
{
16+
ESP_LOGI(TAG, "Creating buffer of capacity %d", capacity);
17+
received_msg_t *msg = (received_msg_t *) calloc(1, sizeof(received_msg_t));
18+
if (!msg) {
19+
ESP_LOGE(TAG, "Failed allocation of message structure");
20+
return NULL;
21+
}
22+
23+
msg->buf = heap_caps_malloc_prefer(capacity, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL);
24+
if (!msg->buf) {
25+
ESP_LOGE(TAG, "Failed allocation of buffer size %d", capacity);
26+
free(msg);
27+
return NULL;
28+
}
29+
msg->capacity = capacity;
30+
msg->data_size = 0;
31+
return msg;
32+
}
33+
34+
esp_err_t esp_webrtc_append_msg_to_existing(received_msg_t *dst_msg, void *data_ptr, int data_len, bool is_fin)
35+
{
36+
ESP_LOGI(TAG, "Appending message of size %d at %d location, is_fin: %d", data_len, dst_msg->data_size, is_fin);
37+
38+
if (!is_fin) {
39+
if (dst_msg->data_size + data_len > dst_msg->capacity) {
40+
ESP_LOGE(TAG, "Cannot fit all the data in %d size buffer. current_filled: %d, incoming_data: %d",
41+
dst_msg->capacity, dst_msg->data_size, data_len);
42+
dst_msg->data_size = dst_msg->capacity + 1; /* Overflow the limit forcefully */
43+
return ESP_FAIL;
44+
} else { /* Gather fragmented message */
45+
memcpy(dst_msg->buf + dst_msg->data_size, data_ptr, data_len);
46+
dst_msg->data_size += data_len;
47+
return ESP_ERR_NOT_FINISHED;
48+
}
49+
} else if (dst_msg->data_size == 0) { /* non-fragmented message */
50+
memcpy(dst_msg->buf + dst_msg->data_size, data_ptr, data_len);
51+
dst_msg->data_size += data_len;
52+
return ESP_OK;
53+
} else { /* final part of the fragment */
54+
if (dst_msg->data_size + data_len <= dst_msg->capacity) { /* valid fragmented gathered message */
55+
/* Copy last piece */
56+
memcpy(dst_msg->buf + dst_msg->data_size, data_ptr, data_len);
57+
dst_msg->data_size += data_len;
58+
/* Process now... */
59+
return ESP_OK;
60+
} else {
61+
ESP_LOGW(TAG, "Discarding the message which could not be fit");
62+
dst_msg->data_size = 0;
63+
return ESP_FAIL;
64+
}
65+
}
66+
return ESP_FAIL;
67+
}

0 commit comments

Comments
 (0)