fix eleevnlabs callsession stuck (#55)

* fix eleevnlabs callsession stuck

Signed-off-by: Hoan HL <quan.luuhoang8@gmail.com>

* fix read sample rate from session is not needed

Signed-off-by: Hoan HL <quan.luuhoang8@gmail.com>

---------

Signed-off-by: Hoan HL <quan.luuhoang8@gmail.com>
This commit is contained in:
Hoan Luu Huu
2024-04-23 20:14:43 +07:00
committed by GitHub
parent bd69d476e7
commit 41aebafd1c
6 changed files with 15 additions and 50 deletions

View File

@@ -135,23 +135,12 @@ extern "C" {
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
if (a->session_id) { if (a->rate != 8000 /*Hz*/) {
int err; int err;
switch_codec_implementation_t read_impl; a->resampler = speex_resampler_init(1, 8000, a->rate, SWITCH_RESAMPLE_QUALITY, &err);
if (0 != err) {
/* lock and unlock session */ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error initializing resampler: %s.\n", speex_resampler_strerror(err));
switch_core_session_t *psession = switch_core_session_locate(a->session_id); return SWITCH_STATUS_FALSE;
switch_core_session_get_read_impl(psession, &read_impl);
switch_core_session_rwunlock(psession);
uint32_t samples_per_second = !strcasecmp(read_impl.iananame, "g722") ? read_impl.actual_samples_per_second : read_impl.samples_per_second;
a->samples_rate = samples_per_second;
if (samples_per_second != 8000 /*Hz*/) {
a->resampler = speex_resampler_init(1, 8000, samples_per_second, SWITCH_RESAMPLE_QUALITY, &err);
if (0 != err) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error initializing resampler: %s.\n", speex_resampler_strerror(err));
return SWITCH_STATUS_FALSE;
}
} }
} }
@@ -276,9 +265,7 @@ extern "C" {
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
// azure returned 8000hz 16 bit data, we have to take enough data based on call sample rate. // azure returned 8000hz 16 bit data, we have to take enough data based on call sample rate.
size_t size = a->samples_rate ? size_t size = std::min((*datalen/(2 * a->rate / 8000)), bufSize);
std::min((*datalen/(2 * a->samples_rate / 8000)), bufSize) :
std::min((*datalen/2), bufSize);
pcm_data.insert(pcm_data.end(), cBuffer->begin(), cBuffer->begin() + size); pcm_data.insert(pcm_data.end(), cBuffer->begin(), cBuffer->begin() + size);
cBuffer->erase(cBuffer->begin(), cBuffer->begin() + size); cBuffer->erase(cBuffer->begin(), cBuffer->begin() + size);
switch_mutex_unlock(a->mutex); switch_mutex_unlock(a->mutex);

View File

@@ -82,7 +82,6 @@ static switch_status_t a_speech_feed_tts(switch_speech_handle_t *sh, char *text,
a->draining = 0; a->draining = 0;
a->reads = 0; a->reads = 0;
a->flushed = 0; a->flushed = 0;
a->samples_rate = 0;
return azure_speech_feed_tts(a, text, flags); return azure_speech_feed_tts(a, text, flags);
} }

View File

@@ -25,7 +25,6 @@ typedef struct azure_data {
int reads; int reads;
int cache_audio; int cache_audio;
int flushed; int flushed;
uint32_t samples_rate;
void *startTime; void *startTime;

View File

@@ -841,7 +841,6 @@ extern "C" {
CURL* easy = createEasyHandle(); CURL* easy = createEasyHandle();
el->conn = (void *) conn ; el->conn = (void *) conn ;
el->sample_rate = 0;
conn->elevenlabs = el; conn->elevenlabs = el;
conn->easy = easy; conn->easy = easy;
conn->global = &global; conn->global = &global;
@@ -852,20 +851,12 @@ extern "C" {
el->circularBuffer = (void *) new CircularBuffer_t(8192); el->circularBuffer = (void *) new CircularBuffer_t(8192);
if (el->session_id) { if (el->rate != 8000 /*Hz*/) {
int err; int err;
switch_codec_implementation_t read_impl; el->resampler = speex_resampler_init(1, 8000, el->rate, SWITCH_RESAMPLE_QUALITY, &err);
switch_core_session_t *psession = switch_core_session_locate(el->session_id); if (0 != err) {
switch_core_session_get_read_impl(psession, &read_impl); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error initializing resampler: %s.\n", speex_resampler_strerror(err));
uint32_t samples_per_second = !strcasecmp(read_impl.iananame, "g722") ? read_impl.actual_samples_per_second : read_impl.samples_per_second; return SWITCH_STATUS_FALSE;
el->sample_rate = samples_per_second;
// elevenlabs output is PCMU 8000
if (samples_per_second != 8000 /*Hz*/) {
el->resampler = speex_resampler_init(1, 8000, samples_per_second, SWITCH_RESAMPLE_QUALITY, &err);
if (0 != err) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error initializing resampler: %s.\n", speex_resampler_strerror(err));
return SWITCH_STATUS_FALSE;
}
} }
} }
@@ -939,9 +930,7 @@ extern "C" {
switch_mutex_unlock(el->mutex); switch_mutex_unlock(el->mutex);
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
size_t size = el->sample_rate ? size_t size = std::min((*datalen/(2 * el->rate / 8000)), cBuffer->size());
std::min((*datalen/(2 * el->sample_rate / 8000)), cBuffer->size()) :
std::min((*datalen/2), cBuffer->size());
pcm_data.insert(pcm_data.end(), cBuffer->begin(), cBuffer->begin() + size); pcm_data.insert(pcm_data.end(), cBuffer->begin(), cBuffer->begin() + size);
cBuffer->erase(cBuffer->begin(), cBuffer->begin() + size); cBuffer->erase(cBuffer->begin(), cBuffer->begin() + size);
switch_mutex_unlock(el->mutex); switch_mutex_unlock(el->mutex);

View File

@@ -31,7 +31,6 @@ struct elevenlabs_data {
char *cache_filename; char *cache_filename;
int rate; int rate;
uint32_t sample_rate;
void *conn; void *conn;
FILE *file; FILE *file;

View File

@@ -838,17 +838,9 @@ extern "C" {
p->circularBuffer = (void *) new CircularBuffer_t(8192); p->circularBuffer = (void *) new CircularBuffer_t(8192);
if (p->session_id) { if (mpg123_param(mh, MPG123_FORCE_RATE, p->rate /*Hz*/, 0) != MPG123_OK) {
int err; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error mpg123_param!\n");
switch_codec_implementation_t read_impl; return SWITCH_STATUS_FALSE;
switch_core_session_t *psession = switch_core_session_locate(p->session_id);
switch_core_session_get_read_impl(psession, &read_impl);
switch_core_session_rwunlock(psession);
uint32_t samples_per_second = !strcasecmp(read_impl.iananame, "g722") ? read_impl.actual_samples_per_second : read_impl.samples_per_second;
if (mpg123_param(mh, MPG123_FORCE_RATE, samples_per_second /*Hz*/, 0) != MPG123_OK) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error mpg123_param!\n");
return SWITCH_STATUS_FALSE;
}
} }
std::ostringstream api_key_stream; std::ostringstream api_key_stream;