From 2fabe473286111411b0257ba77f508b42fd63964 Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Fri, 3 Jan 2025 15:20:19 -0500 Subject: [PATCH] Create DataChannel by default --- CMakeLists.txt | 4 ++++ src/webrtc.cpp | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4db8827..7ef22a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,10 @@ if(NOT DEFINED ENV{OPENAI_API_KEY}) message(FATAL_ERROR "Env variable OPENAI_API_KEY must be set") endif() +if(ENV{LOG_DATACHANNEL_MESSAGES}) + add_compile_definitions(LOG_DATACHANNEL_MESSAGES="1") +endif() + add_compile_definitions(OPENAI_API_KEY="$ENV{OPENAI_API_KEY}") add_compile_definitions(OPENAI_REALTIMEAPI="https://api.openai.com/v1/realtime?model=gpt-4o-mini-realtime-preview-2024-12-17") diff --git a/src/webrtc.cpp b/src/webrtc.cpp index e41b284..b8bc3b7 100644 --- a/src/webrtc.cpp +++ b/src/webrtc.cpp @@ -25,6 +25,23 @@ void oai_send_audio_task(void *user_data) { } #endif +static void oai_ondatachannel_onmessage_task(char *msg, size_t len, + void *userdata, uint16_t sid) { +#ifdef LOG_DATACHANNEL_MESSAGES + ESP_LOGI(LOG_TAG, "DataChannel Message: %s", msg); +#endif +} + +static void oai_ondatachannel_onopen_task(void *userdata) { + if (peer_connection_create_datachannel(peer_connection, DATA_CHANNEL_RELIABLE, + 0, 0, (char *)"oai-events", + (char *)"") != -1) { + ESP_LOGI(LOG_TAG, "DataChannel created"); + } else { + ESP_LOGE(LOG_TAG, "Failed to create DataChannel"); + } +} + static void oai_onconnectionstatechange_task(PeerConnectionState state, void *user_data) { ESP_LOGI(LOG_TAG, "PeerConnectionState: %s", @@ -56,7 +73,7 @@ void oai_webrtc() { .ice_servers = {}, .audio_codec = CODEC_OPUS, .video_codec = CODEC_NONE, - .datachannel = DATA_CHANNEL_NONE, + .datachannel = DATA_CHANNEL_STRING, .onaudiotrack = [](uint8_t *data, size_t size, void *userdata) -> void { #ifndef LINUX_BUILD oai_audio_decode(data, size); @@ -78,6 +95,10 @@ void oai_webrtc() { peer_connection_oniceconnectionstatechange(peer_connection, oai_onconnectionstatechange_task); peer_connection_onicecandidate(peer_connection, oai_on_icecandidate_task); + peer_connection_ondatachannel(peer_connection, + oai_ondatachannel_onmessage_task, + oai_ondatachannel_onopen_task, NULL); + peer_connection_create_offer(peer_connection); while (1) {