Skip to content

Commit 16d6803

Browse files
Create DataChannel by default
1 parent 87f96f7 commit 16d6803

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

CMakeLists.txt

+4
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

+21-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ 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 *)"", (char *)"") != -1) {
38+
ESP_LOGI(LOG_TAG, "DataChannel created");
39+
} else {
40+
ESP_LOGE(LOG_TAG, "Failed to create DataChannel");
41+
}
42+
}
43+
2844
static void oai_onconnectionstatechange_task(PeerConnectionState state,
2945
void *user_data) {
3046
ESP_LOGI(LOG_TAG, "PeerConnectionState: %s",
@@ -56,7 +72,7 @@ void oai_webrtc() {
5672
.ice_servers = {},
5773
.audio_codec = CODEC_OPUS,
5874
.video_codec = CODEC_NONE,
59-
.datachannel = DATA_CHANNEL_NONE,
75+
.datachannel = DATA_CHANNEL_STRING,
6076
.onaudiotrack = [](uint8_t *data, size_t size, void *userdata) -> void {
6177
#ifndef LINUX_BUILD
6278
oai_audio_decode(data, size);
@@ -78,6 +94,10 @@ void oai_webrtc() {
7894
peer_connection_oniceconnectionstatechange(peer_connection,
7995
oai_onconnectionstatechange_task);
8096
peer_connection_onicecandidate(peer_connection, oai_on_icecandidate_task);
97+
peer_connection_ondatachannel(peer_connection,
98+
oai_ondatachannel_onmessage_task,
99+
oai_ondatachannel_onopen_task, NULL);
100+
81101
peer_connection_create_offer(peer_connection);
82102

83103
while (1) {

0 commit comments

Comments
 (0)