mirror of
https://github.com/jambonz/freeswitch-modules.git
synced 2025-12-19 09:07:44 +00:00
add support for deepgram on-prem (#10)
* add option for tls with on prem uri Signed-off-by: Dave Horton <daveh@beachdognet.com> * -s logging --------- Signed-off-by: Dave Horton <daveh@beachdognet.com>
This commit is contained in:
@@ -48,13 +48,15 @@ int AudioPipe::lws_callback(struct lws *wsi,
|
||||
AudioPipe* ap = findPendingConnect(wsi);
|
||||
if (ap) {
|
||||
std::string apiKey = ap->getApiKey();
|
||||
unsigned char **p = (unsigned char **)in, *end = (*p) + len;
|
||||
char b[256];
|
||||
memset(b, 0, sizeof(b));
|
||||
strcpy(b,"Token ");
|
||||
strcpy(b + 6, apiKey.c_str());
|
||||
if (apiKey.length() > 0) {
|
||||
unsigned char **p = (unsigned char **)in, *end = (*p) + len;
|
||||
char b[256];
|
||||
memset(b, 0, sizeof(b));
|
||||
strcpy(b,"Token ");
|
||||
strcpy(b + 6, apiKey.c_str());
|
||||
|
||||
if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_AUTHORIZATION, (unsigned char *)b, strlen(b), p, end)) return -1;
|
||||
if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_AUTHORIZATION, (unsigned char *)b, strlen(b), p, end)) return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -445,11 +447,14 @@ bool AudioPipe::deinitialize() {
|
||||
|
||||
// instance members
|
||||
AudioPipe::AudioPipe(const char* uuid, const char* bugname, const char* host, unsigned int port, const char* path,
|
||||
size_t bufLen, size_t minFreespace, const char* apiKey, notifyHandler_t callback) :
|
||||
size_t bufLen, size_t minFreespace, const char* apiKey, int useTls, notifyHandler_t callback) :
|
||||
m_uuid(uuid), m_host(host), m_port(port), m_path(path), m_finished(false), m_bugname(bugname),
|
||||
m_audio_buffer_min_freespace(minFreespace), m_audio_buffer_max_len(bufLen), m_gracefulShutdown(false),
|
||||
m_audio_buffer_write_offset(LWS_PRE), m_recv_buf(nullptr), m_recv_buf_ptr(nullptr),
|
||||
m_state(LWS_CLIENT_IDLE), m_wsi(nullptr), m_vhd(nullptr), m_apiKey(apiKey), m_callback(callback) {
|
||||
m_audio_buffer_write_offset(LWS_PRE), m_recv_buf(nullptr), m_recv_buf_ptr(nullptr), m_useTls(useTls),
|
||||
m_state(LWS_CLIENT_IDLE), m_wsi(nullptr), m_vhd(nullptr), m_callback(callback) {
|
||||
|
||||
if (apiKey) m_apiKey = apiKey;
|
||||
else m_apiKey = "";
|
||||
|
||||
m_audio_buffer = new uint8_t[m_audio_buffer_max_len];
|
||||
}
|
||||
@@ -474,7 +479,7 @@ bool AudioPipe::connect_client(struct lws_per_vhost_data *vhd) {
|
||||
i.path = m_path.c_str();
|
||||
i.host = i.address;
|
||||
i.origin = i.address;
|
||||
i.ssl_connection = LCCSCF_USE_SSL;
|
||||
if (m_useTls) i.ssl_connection = LCCSCF_USE_SSL;
|
||||
i.pwsi = &(m_wsi);
|
||||
|
||||
m_state = LWS_CLIENT_CONNECTING;
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace deepgram {
|
||||
|
||||
// constructor
|
||||
AudioPipe(const char* uuid, const char* bugname, const char* host, unsigned int port, const char* path,
|
||||
size_t bufLen, size_t minFreespace, const char* apiKey, notifyHandler_t callback);
|
||||
size_t bufLen, size_t minFreespace, const char* apiKey, int useTls, notifyHandler_t callback);
|
||||
~AudioPipe();
|
||||
|
||||
LwsState_t getLwsState(void) { return m_state; }
|
||||
@@ -137,6 +137,7 @@ namespace deepgram {
|
||||
bool m_finished;
|
||||
std::string m_bugname;
|
||||
std::promise<void> m_promise;
|
||||
bool m_useTls;
|
||||
};
|
||||
|
||||
} // namespace deepgram
|
||||
|
||||
@@ -325,6 +325,7 @@ namespace {
|
||||
char* bugname, responseHandler_t responseHandler) {
|
||||
|
||||
int err;
|
||||
int useTls = true;
|
||||
switch_codec_implementation_t read_impl;
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
|
||||
@@ -337,8 +338,32 @@ namespace {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "path: %s\n", path.c_str());
|
||||
|
||||
strncpy(tech_pvt->sessionId, switch_core_session_get_uuid(session), MAX_SESSION_ID);
|
||||
strncpy(tech_pvt->host, "api.deepgram.com", MAX_WS_URL_LEN);
|
||||
tech_pvt->port = 443;
|
||||
|
||||
const char* endpoint = switch_channel_get_variable(channel, "DEEPGRAM_URI");
|
||||
if (endpoint != nullptr) {
|
||||
std::string ep(endpoint);
|
||||
|
||||
useTls = switch_true(switch_channel_get_variable(channel, "DEEPGRAM_USE_TLS"));
|
||||
|
||||
size_t pos = ep.find(':');
|
||||
if (pos != std::string::npos) {
|
||||
std::string host = ep.substr(0, pos);
|
||||
std::string port = ep.substr(pos + 1);
|
||||
strncpy(tech_pvt->host, host.c_str(), MAX_WS_URL_LEN);
|
||||
tech_pvt->port = ::atoi(port.c_str());
|
||||
}
|
||||
else {
|
||||
strncpy(tech_pvt->host, ep.c_str(), MAX_WS_URL_LEN);
|
||||
tech_pvt->port = 443;
|
||||
}
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO,
|
||||
"connecting to deepgram on-prem %s port %d, using tls? (%s)\n", tech_pvt->host, tech_pvt->port, useTls ? "yes" : "no");
|
||||
}
|
||||
else {
|
||||
strncpy(tech_pvt->host, "api.deepgram.com", MAX_WS_URL_LEN);
|
||||
tech_pvt->port = 443;
|
||||
}
|
||||
|
||||
strncpy(tech_pvt->path, path.c_str(), MAX_PATH_LEN);
|
||||
tech_pvt->sampling = desiredSampling;
|
||||
tech_pvt->responseHandler = responseHandler;
|
||||
@@ -351,13 +376,13 @@ namespace {
|
||||
|
||||
const char* apiKey = switch_channel_get_variable(channel, "DEEPGRAM_API_KEY");
|
||||
if (!apiKey && defaultApiKey) apiKey = defaultApiKey;
|
||||
else if (!apiKey) {
|
||||
else if (!apiKey && endpoint == nullptr) {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "no deepgram api key provided\n");
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
deepgram::AudioPipe* ap = new deepgram::AudioPipe(tech_pvt->sessionId, bugname, tech_pvt->host, tech_pvt->port, tech_pvt->path,
|
||||
buflen, read_impl.decoded_bytes_per_packet, apiKey, eventCallback);
|
||||
buflen, read_impl.decoded_bytes_per_packet, apiKey, useTls, eventCallback);
|
||||
if (!ap) {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error allocating AudioPipe\n");
|
||||
return SWITCH_STATUS_FALSE;
|
||||
|
||||
Reference in New Issue
Block a user