Skip to content

Commit

Permalink
compatiable with esp32 lwip stack
Browse files Browse the repository at this point in the history
  • Loading branch information
sepfy committed Oct 10, 2024
1 parent adad862 commit 6892a46
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 53 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if (DEFINED ENV{IDF_PATH})
INCLUDE_DIRS "./src" ${HTTP_INCLUDE_PUBLIC_DIRS} ${MQTT_INCLUDE_PUBLIC_DIRS}
REQUIRES mbedtls srtp json esp_netif
)
add_definitions("-DESP32 -DHTTP_DO_NOT_USE_CUSTOM_CONFIG -DMQTT_DO_NOT_USE_CUSTOM_CONFIG")
add_definitions("-DCONFIG_USE_LWIP=1" "-DCONFIG_USE_USRSCTP=0" "-DCONFIG_AUDIO_BUFFER_SIZE=8096" "-DCONFIG_DATA_BUFFER_SIZE=102400" "-DHTTP_DO_NOT_USE_CUSTOM_CONFIG" "-DMQTT_DO_NOT_USE_CUSTOM_CONFIG")
return()
endif()

Expand Down
2 changes: 0 additions & 2 deletions examples/esp32/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ void app_main(void) {
peer_signaling_set_config(&service_config);
peer_signaling_join_channel();

peer_signaling_join_channel(deviceid, g_pc);

#if defined(CONFIG_ESP32S3_XIAO_SENSE)
StackType_t* stack_memory = (StackType_t*)heap_caps_malloc(8192 * sizeof(StackType_t), MALLOC_CAP_SPIRAM);
StaticTask_t task_buffer;
Expand Down
4 changes: 3 additions & 1 deletion examples/esp32/main/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## IDF Component Manager Manifest File
dependencies:
sepfy/libpeer: ">=0.0.2"
sepfy/libpeer:
path: ../../../
version: ">=0.0.2"
espressif/esp_audio_codec: "^2.0.0"
espressif/esp32-camera: "^2.0.4"
protocol_examples_common:
Expand Down
6 changes: 0 additions & 6 deletions examples/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
# This file was generated using idf.py save-defconfig. It can be edited manually.
# Espressif IoT Development Framework (ESP-IDF) 5.2.2 Project Minimal Configuration
#
CONFIG_IDF_TARGET="esp32s3"
CONFIG_APP_RETRIEVE_LEN_ELF_SHA=16
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_ESP32S3_XIAO_SENSE=y
CONFIG_EXAMPLE_WIFI_SSID="myssid"
CONFIG_EXAMPLE_WIFI_PASSWORD="mypassword"
CONFIG_EXAMPLE_CONNECT_IPV6=n
CONFIG_ESP_PHY_REDUCE_TX_POWER=y
CONFIG_SPIRAM=y
Expand All @@ -17,9 +13,7 @@ CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2048
CONFIG_ESP_MAIN_TASK_STACK_SIZE=4096
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1=n
CONFIG_ESP_IPC_TASK_STACK_SIZE=2048
CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM=16
CONFIG_ESP_WIFI_STATIC_TX_BUFFER_NUM=32
CONFIG_ESP_WIFI_CACHE_TX_BUFFER_NUM=64
CONFIG_LWIP_IPV6_AUTOCONFIG=y
CONFIG_LWIP_IPV6_DHCP6=y
CONFIG_LWIP_TCP_SND_BUF_DEFAULT=5744
Expand Down
8 changes: 7 additions & 1 deletion src/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ static int agent_socket_recv(Agent* agent, Address* addr, uint8_t* buf, int len)
int maxfd = -1;
fd_set rfds;
struct timeval tv;
int addr_type[] = { AF_INET,
#if CONFIG_IPV6
AF_INET6,
#endif
};

tv.tv_sec = 0;
tv.tv_usec = AGENT_POLL_TIMEOUT * 1000;
FD_ZERO(&rfds);

for (i = 0; i < 2; i++) {
for (i = 0; i < sizeof(addr_type) / sizeof(addr_type[0]); i++) {
if (agent->udp_sockets[i].fd > maxfd) {
maxfd = agent->udp_sockets[i].fd;
}
Expand Down
6 changes: 0 additions & 6 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@
#define CONFIG_DATA_BUFFER_SIZE (SCTP_MTU * 128)
#endif

#ifdef ESP32
#define CONFIG_VIDEO_BUFFER_SIZE (CONFIG_MTU * 64)
#define CONFIG_AUDIO_BUFFER_SIZE (CONFIG_MTU * 64)
#define CONFIG_DATA_BUFFER_SIZE (SCTP_MTU * 128)
#endif

#define AUDIO_LATENCY 20 // ms
#define KEEPALIVE_CONNCHECK 10000
#define CONFIG_IPV6 0
Expand Down
4 changes: 3 additions & 1 deletion src/dtls_srtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ static int dtls_srtp_selfsign_cert(DtlsSrtp* dtls_srtp) {
return ret;
}

#if CONFIG_MBEDTLS_DEBUG
static void dtls_srtp_debug(void* ctx, int level, const char* file, int line, const char* str) {
LOGD("%s:%04d: %s", file, line, str);
}
#endif

int dtls_srtp_init(DtlsSrtp* dtls_srtp, DtlsSrtpRole role, void* user_data) {
static const mbedtls_ssl_srtp_profile default_profiles[] = {
Expand Down Expand Up @@ -339,7 +341,7 @@ static void dtls_srtp_key_derivation_cb(void* context,
memcpy(master_secret, ms, sizeof(master_secret));
return dtls_srtp_key_derivation(dtls_srtp, master_secret, randbytes, tls_prf_type);
#else
mencpy(master_secret, secret, sizeof(master_secret));
memcpy(master_secret, secret, sizeof(master_secret));
dtls_srtp_key_derivation(dtls_srtp, master_secret, randbytes, tls_prf_type);
#endif
}
Expand Down
27 changes: 24 additions & 3 deletions src/peer_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,17 @@ PeerConnection* peer_connection_create(PeerConfiguration* config) {
memset(&pc->sctp, 0, sizeof(pc->sctp));

if (pc->config.datachannel) {
#if (CONFIG_DATA_BUFFER_SIZE) > 0
LOGI("Datachannel allocates heap size: %d", CONFIG_DATA_BUFFER_SIZE);
pc->data_rb = buffer_new(CONFIG_DATA_BUFFER_SIZE);
#endif
}

if (pc->config.audio_codec) {
#if (CONFIG_AUDIO_BUFFER_SIZE) > 0
LOGI("Audio allocates heap size: %d", CONFIG_AUDIO_BUFFER_SIZE);
pc->audio_rb = buffer_new(CONFIG_AUDIO_BUFFER_SIZE);
#endif

rtp_encoder_init(&pc->artp_encoder, pc->config.audio_codec,
peer_connection_outgoing_rtp_packet, (void*)pc);
Expand All @@ -188,9 +192,10 @@ PeerConnection* peer_connection_create(PeerConfiguration* config) {
}

if (pc->config.video_codec) {
#if (CONFIG_VIDEO_BUFFER_SIZE) > 0
LOGI("Video allocates heap size: %d", CONFIG_VIDEO_BUFFER_SIZE);
pc->video_rb = buffer_new(CONFIG_VIDEO_BUFFER_SIZE);

#endif
rtp_encoder_init(&pc->vrtp_encoder, pc->config.video_codec,
peer_connection_outgoing_rtp_packet, (void*)pc);

Expand Down Expand Up @@ -222,17 +227,23 @@ int peer_connection_send_audio(PeerConnection* pc, const uint8_t* buf, size_t le
// LOGE("dtls_srtp not connected");
return -1;
}

#if (CONFIG_AUDIO_BUFFER_SIZE) > 0
return buffer_push_tail(pc->audio_rb, buf, len);
#else
return rtp_encoder_encode(&pc->artp_encoder, data, bytes);
#endif
}

int peer_connection_send_video(PeerConnection* pc, const uint8_t* buf, size_t len) {
if (pc->state != PEER_CONNECTION_COMPLETED) {
// LOGE("dtls_srtp not connected");
return -1;
}

#if (CONFIG_VIDEO_BUFFER_SIZE) > 0
return buffer_push_tail(pc->video_rb, buf, len);
#else
return rtp_encoder_encode(&pc->vrtp_encoder, data, bytes);
#endif
}

int peer_connection_datachannel_send(PeerConnection* pc, char* message, size_t len) {
Expand All @@ -245,10 +256,14 @@ int peer_connection_datachannel_send_sid(PeerConnection* pc, char* message, size
return -1;
}

#if (CONFIG_DATA_BUFFER_SIZE) > 0
return buffer_push_tail(pc->data_rb, (uint8_t*)message, len);
#else
if (pc->config.datachannel == DATA_CHANNEL_STRING)
return sctp_outgoing_data(&pc->sctp, message, len, PPID_STRING, sid);
else
return sctp_outgoing_data(&pc->sctp, message, len, PPID_BINARY, sid);
#endif
}

static char* peer_connection_dtls_role_setup_value(DtlsSrtpRole d) {
Expand Down Expand Up @@ -378,18 +393,23 @@ int peer_connection_loop(PeerConnection* pc) {
break;
case PEER_CONNECTION_COMPLETED:

#if (CONFIG_VIDEO_BUFFER_SIZE) > 0
data = buffer_peak_head(pc->video_rb, &bytes);
if (data) {
rtp_encoder_encode(&pc->vrtp_encoder, data, bytes);
buffer_pop_head(pc->video_rb);
}
#endif

#if (CONFIG_AUDIO_BUFFER_SIZE) > 0
data = buffer_peak_head(pc->audio_rb, &bytes);
if (data) {
rtp_encoder_encode(&pc->artp_encoder, data, bytes);
buffer_pop_head(pc->audio_rb);
}
#endif

#if (CONFIG_DATA_BUFFER_SIZE) > 0
data = buffer_peak_head(pc->data_rb, &bytes);
if (data) {
if (pc->config.datachannel == DATA_CHANNEL_STRING)
Expand All @@ -398,6 +418,7 @@ int peer_connection_loop(PeerConnection* pc) {
sctp_outgoing_data(&pc->sctp, (char*)data, bytes, PPID_BINARY, 0);
buffer_pop_head(pc->data_rb);
}
#endif

if ((pc->agent_ret = agent_recv(&pc->agent, pc->agent_buf, sizeof(pc->agent_buf))) > 0) {
LOGD("agent_recv %d", pc->agent_ret);
Expand Down
52 changes: 22 additions & 30 deletions src/ports.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include "lwip/netdb.h"
#include "lwip/netif.h"
#include "lwip/sys.h"
#elif ESP32
#include <esp_netif.h>
#else
#include <ifaddrs.h>
#include <net/if.h>
Expand All @@ -28,36 +26,30 @@ int ports_get_host_addr(Address* addr, const char* iface_prefix) {

#if CONFIG_USE_LWIP
struct netif* netif;
ip_addr_t ip_addr;

int i;
for (netif = netif_list; netif != NULL; netif = netif->next) {
ip_addr = netif->ip_addr;
printf("Interface %s IP Address: %s\n", netif->name, ipaddr_ntoa(&ip_addr));
memcpy(&addr->sin.sin_addr, &ip_addr.u_addr.ip4, 4);
ret = 1;
}
#elif ESP32
esp_netif_t* netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
esp_netif_ip_info_t ip_info;
esp_ip6_addr_t ip6_info;

switch (addr->family) {
case AF_INET6:
if (esp_netif_get_ip6_global(netif, &ip6_info) == ESP_OK) {
memcpy(&addr->sin6.sin6_addr, &ip6_info.addr, 16);
ret = 1;
} else if (esp_netif_get_ip6_linklocal(netif, &ip6_info) == ESP_OK) {
memcpy(&addr->sin6.sin6_addr, &ip6_info.addr, 16);
ret = 1;
}
break;
case AF_INET:
default:
if (esp_netif_get_ip_info(netif, &ip_info) == ESP_OK) {
memcpy(&addr->sin.sin_addr, &ip_info.ip.addr, 4);
ret = 1;
}
switch (addr->family) {
case AF_INET6:
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (!ip6_addr_isany(netif_ip6_addr(netif, i))) {
memcpy(&addr->sin6.sin6_addr, netif_ip6_addr(netif, i), 16);
ret = 1;
break;
}
}
break;
case AF_INET:
default:
if (!ip_addr_isany(&netif->ip_addr)) {
memcpy(&addr->sin.sin_addr, &netif->ip_addr.u_addr.ip4, 4);
ret = 1;
}
break;
}

if (ret) {
break;
}
}
#else

Expand Down
8 changes: 6 additions & 2 deletions src/ssl_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ static int ssl_transport_mbedtls_recv_timeout(void* ctx, unsigned char* buf, siz
if (ret < 0) {
return -1;
} else if (ret == 0) {
return MBEDTLS_ERR_SSL_TIMEOUT;
// timeout
} else {
if (FD_ISSET(((TcpSocket*)ctx)->fd, &read_fds)) {
ret = tcp_socket_recv((TcpSocket*)ctx, buf, len);
}
}

return tcp_socket_recv((TcpSocket*)ctx, buf, len);
return ret;
}

static int ssl_transport_mbedlts_send(void* ctx, const uint8_t* buf, size_t len) {
Expand Down

0 comments on commit 6892a46

Please sign in to comment.