Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create DataChannel by default #15

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(DEFINED 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
2 changes: 1 addition & 1 deletion deps/libpeer
Submodule libpeer updated 1 files
+14 −8 src/sctp.c
28 changes: 27 additions & 1 deletion src/webrtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include "main.h"

#define TICK_INTERVAL 15
#define GREETING \
"{\"type\": \"response.create\", \"response\": {\"modalities\": " \
"[\"audio\", \"text\"], \"instructions\": \"Say 'How can I help?.'\"}}"

PeerConnection *peer_connection = NULL;

Expand All @@ -25,6 +28,25 @@ 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");
peer_connection_datachannel_send(peer_connection, (char *)GREETING,
strlen(GREETING));
} 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 +78,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 +100,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
Loading