Skip to content

[mosq]: Add support for esp-peer in brokerless example #840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions .github/workflows/mosq__build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ jobs:
strategy:
matrix:
idf_ver: ["latest", "release-v5.5", "release-v5.4", "release-v5.3", "release-v5.2", "release-v5.1"]
example: ["broker", "serverless_mqtt"]
exclude:
- idf_ver: "release-v5.1"
example: "serverless_mqtt" # serverless_mqtt is not supported due to esp-peer

runs-on: ubuntu-22.04
container: espressif/idf:${{ matrix.idf_ver }}
env:
TEST_DIR: components/mosquitto/examples
TEST_DIR: components/mosquitto/examples/${{ matrix.example }}
TARGET_TEST: broker
TARGET_TEST_DIR: build_esp32_default
steps:
Expand All @@ -31,14 +36,17 @@ jobs:
. ${IDF_PATH}/export.sh
pip install idf-component-manager idf-build-apps --upgrade
python ci/build_apps.py -c ${TEST_DIR} -m components/mosquitto/.build-test-rules.yml
# upload only the target test artifacts
cd ${TEST_DIR}/${TARGET_TEST}
${GITHUB_WORKSPACE}/ci/clean_build_artifacts.sh `pwd`/${TARGET_TEST_DIR}
zip -qur artifacts.zip ${TARGET_TEST_DIR}
if [ "${{ matrix.example }}" == "${TARGET_TEST}" ]; then
# upload only the target test artifacts
cd ${TEST_DIR}
${GITHUB_WORKSPACE}/ci/clean_build_artifacts.sh `pwd`/${TARGET_TEST_DIR}
zip -qur artifacts.zip ${TARGET_TEST_DIR}
fi
- uses: actions/upload-artifact@v4
if: ${{ matrix.example == 'broker' }}
with:
name: mosq_target_esp32_${{ matrix.idf_ver }}
path: ${{ env.TEST_DIR }}/${{ env.TARGET_TEST }}/artifacts.zip
path: ${{ env.TEST_DIR }}/artifacts.zip
if-no-files-found: error

test_mosq:
Expand Down
15 changes: 15 additions & 0 deletions components/mosquitto/examples/serverless_mqtt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,19 @@
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)

# Setup ESP-PEER from GitHub repo (but it's supported only on certain targets)
set(ESP_PEER_COMPATIBLE_TARGETS "esp32s2" "esp32s3" "esp32p4" "esp32")
if(IDF_TARGET IN_LIST ESP_PEER_COMPATIBLE_TARGETS)
execute_process(COMMAND ${CMAKE_BINARY_DIR}/../esp_peer_setup/install.sh
${CMAKE_BINARY_DIR}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
RESULT_VARIABLE script_result)

if(script_result)
message(FATAL_ERROR "Script esp_peer_setup.sh failed with exit code ${script_result}")
endif()
list(APPEND EXTRA_COMPONENT_DIRS "${CMAKE_BINARY_DIR}/esp-peer/components/")
endif()

project(serverless_mqtt)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
From a2d51a026449cf1ba2cb53996ce1cdf1f6f078d2 Mon Sep 17 00:00:00 2001
From: David Cermak <cermak@espressif.com>
Date: Wed, 9 Jul 2025 12:40:36 +0200
Subject: [PATCH] fix(htt): Add default event to http-client handler

---
components/esp_webrtc/impl/apprtc_signal/https_client.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/components/esp_webrtc/impl/apprtc_signal/https_client.c b/components/esp_webrtc/impl/apprtc_signal/https_client.c
index b7ee6fc..aca4a4e 100644
--- a/components/esp_webrtc/impl/apprtc_signal/https_client.c
+++ b/components/esp_webrtc/impl/apprtc_signal/https_client.c
@@ -48,6 +48,8 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
{
http_info_t *info = evt->user_data;
switch (evt->event_id) {
+ default:
+ break;
case HTTP_EVENT_ERROR:
ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
break;
--
2.25.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
From cdc43a56f5ea1ab1935f55f47f8644f5dd30825e Mon Sep 17 00:00:00 2001
From: David Cermak <cermak@espressif.com>
Date: Thu, 10 Jul 2025 11:09:57 +0200
Subject: [PATCH] fix(media_lib): Remove deprecated freeRTOS header

---
components/media_lib_sal/port/media_lib_os_freertos.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/components/media_lib_sal/port/media_lib_os_freertos.c b/components/media_lib_sal/port/media_lib_os_freertos.c
index d248d59..aea0527 100644
--- a/components/media_lib_sal/port/media_lib_os_freertos.c
+++ b/components/media_lib_sal/port/media_lib_os_freertos.c
@@ -40,8 +40,12 @@
#include "esp_idf_version.h"

#if CONFIG_FREERTOS_ENABLE_TASK_SNAPSHOT
+#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0))
+#include "esp_private/freertos_debug.h"
+#else
#include "freertos/task_snapshot.h"
#endif
+#endif

#ifdef __XTENSA__
#include "esp_debug_helpers.h"
--
2.43.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash
set -e
echo "bin_dir: $1"

bin_dir="$1"
THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ESP_PEER_VERSION="9512eef258a45aafcaaa309b1a67505c8b500363"

ESP_PEER_URL="https://github.com/espressif/esp-webrtc-solution/archive/${ESP_PEER_VERSION}.zip"
ESP_PEER_DIR="${bin_dir}/esp-peer"
ZIP_PATH="${bin_dir}/esp-peer.zip"
EXTRACTED_DIR="${ESP_PEER_DIR}/esp-webrtc-solution-${ESP_PEER_VERSION}"
COMPONENTS_SRC="${EXTRACTED_DIR}/components"
COMPONENTS_DST="${ESP_PEER_DIR}/components"
PATCH_FILE_1="${THIS_DIR}/Add-default-event-to-http-client-handler.patch"
PATCH_FILE_2="${THIS_DIR}/Remove-deprecated-freeRTOS-header.patch"
PATCH_FILE_3="${THIS_DIR}/libpeer-Add-direct-dependency-to-libsrtp.patch"

# Download if not exists
if [ ! -d "$EXTRACTED_DIR" ]; then
echo "Downloading esp-peer ${ESP_PEER_VERSION}..."
wget -O "$ZIP_PATH" "$ESP_PEER_URL"
unzip -o "$ZIP_PATH" -d "$ESP_PEER_DIR"
patch -p1 -d "$EXTRACTED_DIR" < "$PATCH_FILE_1"
patch -p1 -d "$EXTRACTED_DIR" < "$PATCH_FILE_2"
patch -p1 -d "$EXTRACTED_DIR" < "$PATCH_FILE_3"
mv ${EXTRACTED_DIR}/components ${ESP_PEER_DIR}
mv ${ESP_PEER_DIR}/components/esp_webrtc/impl/peer_default ${ESP_PEER_DIR}/components
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
From 695e057000698f4897b6c5802851499842e2fe31 Mon Sep 17 00:00:00 2001
From: David Cermak <cermak@espressif.com>
Date: Fri, 11 Jul 2025 16:59:21 +0200
Subject: [PATCH] fix(libpeer): Add direct dependency to libsrtp

---
components/esp_webrtc/impl/peer_default/CMakeLists.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/components/esp_webrtc/impl/peer_default/CMakeLists.txt b/components/esp_webrtc/impl/peer_default/CMakeLists.txt
index 2af35cf..3fb4615 100644
--- a/components/esp_webrtc/impl/peer_default/CMakeLists.txt
+++ b/components/esp_webrtc/impl/peer_default/CMakeLists.txt
@@ -2,6 +2,6 @@ idf_component_register(INCLUDE_DIRS ./include)

get_filename_component(BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR} NAME)
add_prebuilt_library(${BASE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/libs/${IDF_TARGET}/libpeer_default.a"
- PRIV_REQUIRES ${BASE_DIR} esp_timer)
+ PRIV_REQUIRES ${BASE_DIR} esp_timer espressif__esp_libsrtp)
target_link_libraries(${COMPONENT_LIB} INTERFACE "-L ${CMAKE_CURRENT_SOURCE_DIR}/libs/${IDF_TARGET}")
target_link_libraries(${COMPONENT_LIB} INTERFACE peer_default)
--
2.43.0
10 changes: 10 additions & 0 deletions components/mosquitto/examples/serverless_mqtt/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
if(CONFIG_EXAMPLE_PEER_LIB_ESP_PEER)
set(PEER_BACKEND_SRC "peer_impl_webrtc.c")
else()
set(PEER_BACKEND_SRC "peer_impl_juice.c")
endif()

idf_component_register(SRCS "serverless_mqtt.c"
"wifi_connect.c"
"${PEER_BACKEND_SRC}"
INCLUDE_DIRS "."
REQUIRES libjuice nvs_flash mqtt json esp_wifi)
if(CONFIG_EXAMPLE_PEER_LIB_ESP_PEER)
idf_component_optional_requires(PUBLIC media_lib_sal esp_webrtc peer_default)
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,55 @@ menu "Example Configuration"
WiFi station password for the example to use.
endmenu

choice EXAMPLE_PEER_LIB
prompt "Choose peer library"
default EXAMPLE_PEER_LIB_LIBJUICE
help
Choose the peer library to use for WebRTC communication.
libjuice: Use libjuice library for ICE/STUN/TURN (Performs manual signalling)
esp_peer: Use ESP-IDF specific peer library

config EXAMPLE_PEER_LIB_ESP_PEER
bool "esp_peer"

config EXAMPLE_PEER_LIB_LIBJUICE
bool "libjuice"
endchoice

config EXAMPLE_WEBRTC_URL
string "WebRTC server URL"
depends on EXAMPLE_PEER_LIB_ESP_PEER
default "https://webrtc.espressif.com/join/"
help
URL of WebRTC remote endpoint.

config EXAMPLE_WEBRTC_ROOM_ID
string "WebRTC room ID"
depends on EXAMPLE_PEER_LIB_ESP_PEER
default "12345"
help
Room ID for WebRTC synchronisation.
Could be a random number, but the same for both peers.


config EXAMPLE_MQTT_BROKER_URI
string "MQTT Broker URL"
depends on EXAMPLE_PEER_LIB_LIBJUICE
default "mqtt://mqtt.eclipseprojects.io"
help
URL of the mqtt broker use for synchronisation and exchanging
ICE connect info (description and candidates).

config EXAMPLE_MQTT_SYNC_TOPIC
string "MQTT topic for synchronisation"
depends on EXAMPLE_PEER_LIB_LIBJUICE
default "/topic/serverless_mqtt"
help
MQTT topic used fo synchronisation.

config EXAMPLE_STUN_SERVER
string "Hostname of STUN server"
depends on EXAMPLE_PEER_LIB_LIBJUICE
default "stun.l.google.com"
help
STUN server hostname.
Expand All @@ -67,6 +101,7 @@ menu "Example Configuration"

choice EXAMPLE_SERVERLESS_ROLE
prompt "Choose your role"
depends on EXAMPLE_PEER_LIB_LIBJUICE
default EXAMPLE_SERVERLESS_ROLE_PEER1
help
Choose either peer1 or peer2.
Expand Down
18 changes: 18 additions & 0 deletions components/mosquitto/examples/serverless_mqtt/main/peer_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/

#include <stdio.h>
#include "esp_random.h"
#include "esp_sleep.h"
#include "mosq_broker.h"

typedef void (*on_peer_recv_t)(const char *data, size_t size);

esp_err_t peer_init(on_peer_recv_t cb);

void peer_get_buffer(char ** buffer, size_t *buffer_len);

void peer_send(char* data, size_t size);
Loading