Skip to content

Commit 2fabe47

Browse files
Create DataChannel by default
1 parent 87f96f7 commit 2fabe47

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ if(NOT DEFINED ENV{OPENAI_API_KEY})
1313
message(FATAL_ERROR "Env variable OPENAI_API_KEY must be set")
1414
endif()
1515

16+
if(ENV{LOG_DATACHANNEL_MESSAGES})
17+
add_compile_definitions(LOG_DATACHANNEL_MESSAGES="1")
18+
endif()
19+
1620
add_compile_definitions(OPENAI_API_KEY="$ENV{OPENAI_API_KEY}")
1721
add_compile_definitions(OPENAI_REALTIMEAPI="https://api.openai.com/v1/realtime?model=gpt-4o-mini-realtime-preview-2024-12-17")
1822

src/webrtc.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ void oai_send_audio_task(void *user_data) {
2525
}
2626
#endif
2727

28+
static void oai_ondatachannel_onmessage_task(char *msg, size_t len,
29+
void *userdata, uint16_t sid) {
30+
#ifdef LOG_DATACHANNEL_MESSAGES
31+
ESP_LOGI(LOG_TAG, "DataChannel Message: %s", msg);
32+
#endif
33+
}
34+
35+
static void oai_ondatachannel_onopen_task(void *userdata) {
36+
if (peer_connection_create_datachannel(peer_connection, DATA_CHANNEL_RELIABLE,
37+
0, 0, (char *)"oai-events",
38+
(char *)"") != -1) {
39+
ESP_LOGI(LOG_TAG, "DataChannel created");
40+
} else {
41+
ESP_LOGE(LOG_TAG, "Failed to create DataChannel");
42+
}
43+
}
44+
2845
static void oai_onconnectionstatechange_task(PeerConnectionState state,
2946
void *user_data) {
3047
ESP_LOGI(LOG_TAG, "PeerConnectionState: %s",
@@ -56,7 +73,7 @@ void oai_webrtc() {
5673
.ice_servers = {},
5774
.audio_codec = CODEC_OPUS,
5875
.video_codec = CODEC_NONE,
59-
.datachannel = DATA_CHANNEL_NONE,
76+
.datachannel = DATA_CHANNEL_STRING,
6077
.onaudiotrack = [](uint8_t *data, size_t size, void *userdata) -> void {
6178
#ifndef LINUX_BUILD
6279
oai_audio_decode(data, size);
@@ -78,6 +95,10 @@ void oai_webrtc() {
7895
peer_connection_oniceconnectionstatechange(peer_connection,
7996
oai_onconnectionstatechange_task);
8097
peer_connection_onicecandidate(peer_connection, oai_on_icecandidate_task);
98+
peer_connection_ondatachannel(peer_connection,
99+
oai_ondatachannel_onmessage_task,
100+
oai_ondatachannel_onopen_task, NULL);
101+
81102
peer_connection_create_offer(peer_connection);
82103

83104
while (1) {

0 commit comments

Comments
 (0)