Skip to content

Commit

Permalink
Create DataChannel by default
Browse files Browse the repository at this point in the history
  • Loading branch information
seaduboi-openai committed Jan 3, 2025
1 parent 87f96f7 commit 16d6803
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
22 changes: 21 additions & 1 deletion src/webrtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ 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 *)"", (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",
Expand Down Expand Up @@ -56,7 +72,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);
Expand All @@ -78,6 +94,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) {
Expand Down

0 comments on commit 16d6803

Please sign in to comment.