Skip to content

Commit

Permalink
Merge pull request #88 from sepfy/mdns
Browse files Browse the repository at this point in the history
Mdns
  • Loading branch information
sepfy authored Aug 17, 2024
2 parents dfb377d + d972427 commit 5eb3d7e
Show file tree
Hide file tree
Showing 52 changed files with 1,139 additions and 3,317 deletions.
11 changes: 1 addition & 10 deletions .github/workflows/pear-ci.yml → .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: pear-ci
name: build

on: [push]

Expand All @@ -14,16 +14,7 @@ jobs:
- name: install
run: >
sudo apt-get update && sudo apt-get --no-install-recommends -y install
libglib2.0-dev
cmake
ninja-build
libssl-dev
- name: python
run: >
sudo pip3 install meson
- name: deps
run: >
./build-third-party.sh
- name: build
run: >
mkdir cmake ;
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
cmake
dist
45 changes: 43 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ project(peer)

option(ENABLE_TESTS "Enable tests" OFF)

include(ExternalProject)
include(third_party/coreHTTP/httpFilePaths.cmake)
include(third_party/coreMQTT/mqttFilePaths.cmake)

include_directories(dist/include dist/include/cjson)
include_directories(${CMAKE_BINARY_DIR}/dist/include ${CMAKE_BINARY_DIR}/dist/include/cjson)

link_directories(dist/lib)
link_directories(${CMAKE_BINARY_DIR}/dist/lib)

set(DEP_LIBS "srtp2" "usrsctp" "mbedtls" "mbedcrypto" "mbedx509" "cjson")
# Extended debug information (symbols, source code, and macro definitions)
Expand All @@ -24,3 +25,43 @@ if(ENABLE_TESTS)
add_subdirectory(tests)
endif()

ExternalProject_Add(cjson
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/cJSON
CMAKE_ARGS
-DCMAKE_C_FLAGS="-fPIC"
-DBUILD_SHARED_LIBS=off
-DENABLE_CJSON_TEST=off
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/dist
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
)

ExternalProject_Add(mbedtls
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/mbedtls
CMAKE_ARGS
-DCMAKE_C_FLAGS="-fPIC"
-DENABLE_TESTING=off
-DENABLE_PROGRAMS=off
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/dist
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
)
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/third_party/mbedtls/include/mbedtls/mbedtls_config.h INPUT_CONTENT)
string(REPLACE "//#define MBEDTLS_SSL_DTLS_SRTP" "#define MBEDTLS_SSL_DTLS_SRTP" MODIFIED_CONTENT ${INPUT_CONTENT})
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/mbedtls/include/mbedtls/mbedtls_config.h ${MODIFIED_CONTENT})

ExternalProject_Add(srtp2
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libsrtp
CMAKE_ARGS
-DCMAKE_C_FLAGS="-fPIC"
-DTEST_APPS=off
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/dist
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
)

ExternalProject_Add(usrsctp
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/usrsctp
CMAKE_ARGS
-DCMAKE_C_FLAGS="-fPIC"
-Dsctp_build_programs=off
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/dist
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
)
36 changes: 0 additions & 36 deletions build-third-party.sh

This file was deleted.

1 change: 1 addition & 0 deletions examples/esp32/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build
dependencies.lock
managed_components
sdkconfig
sdkconfig.old
4 changes: 1 addition & 3 deletions examples/esp32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ $ source export.sh

### Download
```bash
$ git clone https://github.com/sepfy/libpeer
$ git clone --recursive https://github.com/sepfy/libpeer
$ cd libpeer/examples/esp32
$ idf.py add-dependency "espressif/esp32-camera^2.0.4"
$ idf.py add-dependency "mdns"
$ git clone --recursive https://github.com/sepfy/esp_ports.git components/srtp
```

Expand Down
2 changes: 1 addition & 1 deletion examples/esp32/components/peer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ file(GLOB CODES "${PEER_PROJECT_PATH}/src/*.c")
idf_component_register(
SRCS ${CODES} ${HTTP_SOURCES} ${MQTT_SOURCES} ${MQTT_SERIALIZER_SOURCES}
INCLUDE_DIRS "${PEER_PROJECT_PATH}/src" ${HTTP_INCLUDE_PUBLIC_DIRS} ${MQTT_INCLUDE_PUBLIC_DIRS}
REQUIRES mbedtls srtp json mdns
REQUIRES mbedtls srtp json esp_netif
)

add_definitions("-DESP32 -DHTTP_DO_NOT_USE_CUSTOM_CONFIG -DMQTT_DO_NOT_USE_CUSTOM_CONFIG")
Expand Down
2 changes: 1 addition & 1 deletion examples/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
idf_component_register(SRCS
"app_main.c" "camera.c"
"app_main.c" "camera.c" "audio.c"
INCLUDE_DIRS "."
)

Expand Down
44 changes: 34 additions & 10 deletions examples/esp32/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
static const char *TAG = "webrtc";

static TaskHandle_t xPcTaskHandle = NULL;
static TaskHandle_t xCameraTaskHandle = NULL;
static TaskHandle_t xPsTaskHandle = NULL;
static TaskHandle_t xCameraTaskHandle = NULL;
static TaskHandle_t xAudioTaskHandle = NULL;

extern esp_err_t camera_init();
extern esp_err_t audio_init();
extern void camera_task(void *pvParameters);
extern void wifi_init_sta();
extern void audio_task(void *pvParameters);

SemaphoreHandle_t xSemaphore = NULL;

PeerConnection *g_pc;
PeerConnectionState eState = PEER_CONNECTION_CLOSED;
Expand All @@ -50,9 +54,9 @@ static void oniceconnectionstatechange(PeerConnectionState state, void *user_dat
}
}

static void onmessasge(char *msg, size_t len, void *userdata) {
static void onmessage(char *msg, size_t len, void *userdata, uint16_t sid) {

ESP_LOGI(TAG, "Datachannel message: %.*s, size", len, msg);
ESP_LOGI(TAG, "Datachannel message: %.*s", len, msg);
}

void onopen(void *userdata) {
Expand Down Expand Up @@ -84,7 +88,10 @@ void peer_connection_task(void *arg) {

for(;;) {

peer_connection_loop(g_pc);
if (xSemaphoreTake(xSemaphore, portMAX_DELAY)) {
peer_connection_loop(g_pc);
xSemaphoreGive(xSemaphore);
}

vTaskDelay(pdMS_TO_TICKS(1));
}
Expand All @@ -99,6 +106,7 @@ void app_main(void) {
.ice_servers = {
{ .urls = "stun:stun.l.google.com:19302" }
},
.audio_codec = CODEC_PCMA,
.datachannel = DATA_CHANNEL_BINARY,
};

Expand All @@ -117,29 +125,45 @@ void app_main(void) {
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(mdns_init());
ESP_ERROR_CHECK(example_connect());

if (esp_read_mac(mac, ESP_MAC_WIFI_STA) == ESP_OK) {
sprintf(deviceid, "esp32-%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
ESP_LOGI(TAG, "Device ID: %s", deviceid);
}

xSemaphore = xSemaphoreCreateMutex();

peer_init();

camera_init();

#if defined(CONFIG_ESP32S3_XIAO_SENSE)
audio_init();
#endif

g_pc = peer_connection_create(&config);
peer_connection_oniceconnectionstatechange(g_pc, oniceconnectionstatechange);
peer_connection_ondatachannel(g_pc, onmessasge, onopen, onclose);
peer_connection_ondatachannel(g_pc, onmessage, onopen, onclose);

ServiceConfiguration service_config = SERVICE_CONFIG_DEFAULT();
service_config.client_id = deviceid;
service_config.pc = g_pc;
service_config.mqtt_url = "broker.emqx.io";
peer_signaling_set_config(&service_config);
peer_signaling_join_channel();

peer_signaling_join_channel(deviceid, g_pc);

xTaskCreatePinnedToCore(camera_task, "camera", 4096, NULL, 6, &xCameraTaskHandle, 0);
#if defined(CONFIG_ESP32S3_XIAO_SENSE)
xTaskCreatePinnedToCore(audio_task, "audio", 8192, NULL, 7, &xAudioTaskHandle, 0);
#endif

xTaskCreatePinnedToCore(camera_task, "camera", 4096, NULL, 8, &xCameraTaskHandle, 1);

xTaskCreatePinnedToCore(peer_connection_task, "peer_connection", 8192, NULL, 10, &xPcTaskHandle, 1);
xTaskCreatePinnedToCore(peer_connection_task, "peer_connection", 8192, NULL, 5, &xPcTaskHandle, 1);

xTaskCreatePinnedToCore(peer_signaling_task, "peer_signaling", 8192, NULL, 10, &xPsTaskHandle, 0);
xTaskCreatePinnedToCore(peer_signaling_task, "peer_signaling", 8192, NULL, 6, &xPsTaskHandle, 1);

ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
ESP_LOGI(TAG, "open https://sepfy.github.io/webrtc?deviceId=%s", deviceid);
Expand Down
68 changes: 36 additions & 32 deletions examples/esp32s3/main/audio.c → examples/esp32/main/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include "esp_log.h"
#include "driver/i2s_pdm.h"

#include "esp_audio_enc_default.h"
#include "esp_audio_enc_reg.h"
#include "esp_g711_enc.h"
#include "esp_audio_enc.h"
#include "esp_audio_enc_def.h"
#include "esp_audio_def.h"
#include "esp_opus_enc.h"

#include "peer_connection.h"

Expand All @@ -24,51 +24,55 @@ i2s_chan_handle_t rx_handle = NULL;
esp_audio_enc_handle_t enc_handle = NULL;
esp_audio_enc_in_frame_t aenc_in_frame = { 0 };
esp_audio_enc_out_frame_t aenc_out_frame = { 0 };
esp_g711_enc_config_t g711_cfg;
esp_audio_enc_config_t enc_cfg;

esp_err_t audio_codec_init() {

uint8_t *inbuf = NULL;
uint8_t *outbuf = NULL;
int aenc_in_frame_size = 0;
int aenc_out_frame_size = 0;
uint8_t *read_buf = NULL;
uint8_t *write_buf = NULL;
int read_size = 0;
int out_size = 0;

esp_audio_err_t ret = ESP_AUDIO_ERR_OK;

esp_opus_enc_config_t config = ESP_OPUS_ENC_CONFIG_DEFAULT();
config.sample_rate = ESP_AUDIO_SAMPLE_RATE_8K;
config.channel = ESP_AUDIO_MONO;
config.bitrate = 18000;
config.frame_duration = ESP_OPUS_ENC_FRAME_DURATION_40_MS;
esp_audio_enc_register_default();

ret = esp_opus_enc_open(&config, sizeof(esp_opus_enc_config_t), &enc_handle);
g711_cfg.sample_rate = ESP_AUDIO_SAMPLE_RATE_8K;
g711_cfg.channel = ESP_AUDIO_MONO;
g711_cfg.bits_per_sample = ESP_AUDIO_BIT16;

enc_cfg.type = ESP_AUDIO_TYPE_G711A;
enc_cfg.cfg = &g711_cfg;
enc_cfg.cfg_sz = sizeof(g711_cfg);

ret = esp_audio_enc_open(&enc_cfg, &enc_handle);
if (ret != ESP_AUDIO_ERR_OK) {
ESP_LOGE(TAG, "audio encoder open failed");
return ESP_FAIL;
}

ret = esp_opus_enc_get_frame_size(enc_handle, &aenc_in_frame_size, &aenc_out_frame_size);
if (ret != ESP_AUDIO_ERR_OK) {
ESP_LOGE(TAG, "audio encoder get frame size failed");
return ESP_FAIL;
int frame_size = (g711_cfg.bits_per_sample * g711_cfg.channel) >> 3;
// Get frame_size
esp_audio_enc_get_frame_size(enc_handle, &read_size, &out_size);
ESP_LOGI(TAG, "audio codec init. frame size: %d, read size: %d, out size: %d", frame_size, read_size, out_size);
// 8000HZ duration 20ms
if (frame_size == read_size) {
read_size *= 8000 / 1000 * 20;
out_size *= 8000 / 1000 * 20;
}

inbuf = calloc(1, aenc_in_frame_size*2);
if (!inbuf) {
ESP_LOGE(TAG, "inbuf malloc failed");
read_buf = malloc(read_size);
write_buf = malloc(out_size);
if (read_buf == NULL || write_buf == NULL) {
return ESP_FAIL;
}

outbuf = calloc(1, aenc_out_frame_size);
if (!outbuf) {
ESP_LOGE(TAG, "outbuf malloc failed");
return ESP_FAIL;
}
aenc_in_frame.buffer = read_buf;
aenc_in_frame.len = read_size;
aenc_out_frame.buffer = write_buf;
aenc_out_frame.len = out_size;

aenc_in_frame.buffer = inbuf;
aenc_in_frame.len = aenc_in_frame_size;
aenc_out_frame.buffer = outbuf;
aenc_out_frame.len = aenc_out_frame_size;
ESP_LOGI(TAG, "audio codec init done. in_frame_size=%d, out_frame_size=%d", aenc_in_frame_size, aenc_out_frame_size);
ESP_LOGI(TAG, "audio codec init done. in buffer size: %d, out buffer size: %d", read_size, out_size);
return 0;
}

Expand Down Expand Up @@ -130,7 +134,7 @@ void audio_task(void *arg) {

if (ret == aenc_in_frame.len) {

if (esp_opus_enc_process(enc_handle, &aenc_in_frame, &aenc_out_frame) == ESP_AUDIO_ERR_OK) {
if (esp_audio_enc_process(enc_handle, &aenc_in_frame, &aenc_out_frame) == ESP_AUDIO_ERR_OK) {

peer_connection_send_audio(g_pc, aenc_out_frame.buffer, aenc_out_frame.encoded_bytes);

Expand Down
9 changes: 6 additions & 3 deletions examples/esp32/main/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
extern PeerConnection *g_pc;
extern int gDataChannelOpened;
extern PeerConnectionState eState;
extern SemaphoreHandle_t xSemaphore;
extern int get_timestamp();
static const char *TAG = "Camera";

Expand Down Expand Up @@ -134,7 +135,10 @@ void camera_task(void *pvParameters) {
}

//ESP_LOGI(TAG, "Camera captured. size=%zu, timestamp=%llu", fb->len, fb->timestamp);
peer_connection_datachannel_send(g_pc, (char*)fb->buf, fb->len);
if (xSemaphoreTake(xSemaphore, portMAX_DELAY)) {
peer_connection_datachannel_send(g_pc, (char*)fb->buf, fb->len);
xSemaphoreGive(xSemaphore);
}

fps++;

Expand All @@ -148,8 +152,7 @@ void camera_task(void *pvParameters) {
esp_camera_fb_return(fb);
}

// 10 FPS
vTaskDelay(pdMS_TO_TICKS(1000/10));
vTaskDelay(pdMS_TO_TICKS(1000/20));
}

}
Expand Down
Loading

0 comments on commit 5eb3d7e

Please sign in to comment.