mirror of
https://github.com/jambonz/freeswitch-modules.git
synced 2026-01-25 02:08:27 +00:00
Feat/mod dub v2 (#22)
* support tts elevenlabs to mod_dub Signed-off-by: Quan HL <quan.luuhoang8@gmail.com> * wip Signed-off-by: Quan HL <quan.luuhoang8@gmail.com> * wip * wip * fix aws race condition when 2 start transcribes are sent at the same instant * wip * wip * wip * allow queue play on track * wip Signed-off-by: Quan HL <quan.luuhoang8@gmail.com> * revert change for aws transcribe Signed-off-by: Quan HL <quan.luuhoang8@gmail.com> * fix type * wip * wip * rename parameters * rename paramters * wip * wip * wip * wip * wip * bug: there exists scenarios where callback is not defined * wip * wip * revert unintended changes to mod_google_transcribe * fix bugs w/ streaming tts simplified arg parsing to use freeswitch conventions --------- Signed-off-by: Quan HL <quan.luuhoang8@gmail.com> Co-authored-by: Quan HL <quan.luuhoang8@gmail.com>
This commit is contained in:
1 parent
4e57f73c7e
commit
9cdc5fdfca
23 files changed
+1907
-1612
No files matched your search
+31
-83
@@ -8,6 +8,7 @@
|
||||
#include <switch.h>
|
||||
#include <switch_curl.h>
|
||||
#include "dub_glue.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Prototypes */
|
||||
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_dub_shutdown);
|
||||
@@ -88,7 +89,6 @@ static switch_status_t dub_add_track(switch_core_session_t *session, char* track
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
switch_media_bug_t *bug = NULL;
|
||||
struct cap_cb *cb = NULL;
|
||||
int offset = 0;
|
||||
int samples_per_second;
|
||||
|
||||
switch_codec_implementation_t write_impl = { 0 };
|
||||
@@ -102,36 +102,32 @@ static switch_status_t dub_add_track(switch_core_session_t *session, char* track
|
||||
cb =(struct cap_cb *) switch_core_session_alloc(session, sizeof(struct cap_cb));
|
||||
memset(cb, 0, sizeof(struct cap_cb));
|
||||
switch_mutex_init(&cb->mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
|
||||
offset = 0;
|
||||
}
|
||||
else {
|
||||
/* retrieve the bug and search for an empty track */
|
||||
cb = (struct cap_cb *) switch_core_media_bug_get_user_data(bug);
|
||||
while (offset < MAX_DUB_TRACKS && cb->tracks[offset].state != DUB_TRACK_STATE_INACTIVE) {
|
||||
offset++;
|
||||
}
|
||||
if (offset == MAX_DUB_TRACKS) {
|
||||
/* all tracks are in use */
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dub_add_track: no available tracks\n");
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
switch_core_session_get_write_impl(session, &write_impl);
|
||||
samples_per_second = !strcasecmp(write_impl.iananame, "g722") ? write_impl.actual_samples_per_second : write_impl.samples_per_second;
|
||||
|
||||
init_dub_track(&cb->tracks[offset], trackName, samples_per_second);
|
||||
switch_mutex_lock(cb->mutex);
|
||||
if (add_track(cb, trackName, samples_per_second) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_mutex_unlock(cb->mutex);
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dub_add_track: error adding track %s\n", trackName);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
if (!bug) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "dub_add_track: adding bug for track %s\n", trackName);
|
||||
if (switch_core_media_bug_add(session, MY_BUG_NAME, NULL, capture_callback, (void *) cb, 0, SMBF_WRITE_REPLACE, &bug) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_mutex_unlock(cb->mutex);
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dub_add_track: error adding bug!\n");
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
switch_channel_set_private(channel, MY_BUG_NAME, bug);
|
||||
}
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "dub_add_track: added track %s at offset %d\n", trackName, offset);
|
||||
switch_mutex_unlock(cb->mutex);
|
||||
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
@@ -140,39 +136,17 @@ static switch_status_t dub_remove_track(switch_core_session_t *session, char* tr
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
switch_media_bug_t *bug = (switch_media_bug_t*) switch_channel_get_private(channel, MY_BUG_NAME);
|
||||
switch_status_t status = SWITCH_STATUS_FALSE;
|
||||
dub_track_t *track = NULL;
|
||||
|
||||
if (bug) {
|
||||
struct cap_cb *cb =(struct cap_cb *) switch_core_media_bug_get_user_data(bug);
|
||||
|
||||
switch_mutex_lock(cb->mutex);
|
||||
for (int i = 0; i < MAX_DUB_TRACKS && track == NULL; i++) {
|
||||
if (cb->tracks[i].state != DUB_TRACK_STATE_INACTIVE && strcmp(cb->tracks[i].trackName, trackName) == 0) {
|
||||
track = &cb->tracks[i];
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "dub_remove_track: removing track %s at offset %d\n", trackName, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (track) {
|
||||
int count = 0;
|
||||
|
||||
remove_dub_track(track);
|
||||
|
||||
/* check if this is the last bug */
|
||||
for (int i = 0; i < MAX_DUB_TRACKS; i++) {
|
||||
if (cb->tracks[i].state != DUB_TRACK_STATE_INACTIVE) count++;
|
||||
}
|
||||
|
||||
if (count == 0) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "dub_remove_track: removing bug after removing last track\n");
|
||||
dub_session_cleanup(session, 0, bug);
|
||||
}
|
||||
status = SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
else {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dub_remove_track: track %s not found\n", trackName);
|
||||
}
|
||||
status = remove_dub_track(cb, trackName);
|
||||
switch_mutex_unlock(cb->mutex);
|
||||
if (status != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dub_remove_track: error removing track %s\n", trackName);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
}
|
||||
else {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dub_remove_track: bug not found\n");
|
||||
@@ -185,24 +159,14 @@ static switch_status_t dub_silence_track(switch_core_session_t *session, char* t
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
switch_media_bug_t *bug = (switch_media_bug_t*) switch_channel_get_private(channel, MY_BUG_NAME);
|
||||
switch_status_t status = SWITCH_STATUS_FALSE;
|
||||
dub_track_t *track = NULL;
|
||||
|
||||
if (bug) {
|
||||
struct cap_cb *cb =(struct cap_cb *) switch_core_media_bug_get_user_data(bug);
|
||||
switch_mutex_lock(cb->mutex);
|
||||
for (int i = 0; i < MAX_DUB_TRACKS; i++) {
|
||||
if (cb->tracks[i].state != DUB_TRACK_STATE_INACTIVE && strcmp(cb->tracks[i].trackName, trackName) == 0) {
|
||||
track = &cb->tracks[i];
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "dub_silence_track: silencing track %s at offset %d\n", trackName, i);
|
||||
break;
|
||||
}
|
||||
status = silence_dub_track(cb, trackName);
|
||||
if (status != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dub_silence_track: error silencing track %s\n", trackName);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
if (track) {
|
||||
silence_dub_track(track);
|
||||
status = SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
switch_mutex_unlock(cb->mutex);
|
||||
}
|
||||
else {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dub_silence_track: bug not found\n");
|
||||
@@ -215,24 +179,14 @@ static switch_status_t dub_play_on_track(switch_core_session_t *session, char* t
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
switch_media_bug_t *bug = (switch_media_bug_t*) switch_channel_get_private(channel, MY_BUG_NAME);
|
||||
switch_status_t status = SWITCH_STATUS_FALSE;
|
||||
dub_track_t *track = NULL;
|
||||
|
||||
if (bug) {
|
||||
struct cap_cb *cb =(struct cap_cb *) switch_core_media_bug_get_user_data(bug);
|
||||
switch_mutex_lock(cb->mutex);
|
||||
for (int i = 0; i < MAX_DUB_TRACKS; i++) {
|
||||
if (cb->tracks[i].state != DUB_TRACK_STATE_INACTIVE && strcmp(cb->tracks[i].trackName, trackName) == 0) {
|
||||
track = &cb->tracks[i];
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
|
||||
"dub_play_on_track: playing %s on track %s at offset %d with gain %d\n", url, trackName, i, gain);
|
||||
break;
|
||||
}
|
||||
status = play_dub_track(cb, trackName, url, loop, gain);
|
||||
if (status != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dub_play_on_track: error playing %s on track %s\n", url, trackName);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
if (track) {
|
||||
status = play_dub_track(track, cb->mutex, url, loop, gain);
|
||||
}
|
||||
switch_mutex_unlock(cb->mutex);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
@@ -241,37 +195,30 @@ static switch_status_t dub_say_on_track(switch_core_session_t *session, char* tr
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
switch_media_bug_t *bug = (switch_media_bug_t*) switch_channel_get_private(channel, MY_BUG_NAME);
|
||||
switch_status_t status = SWITCH_STATUS_FALSE;
|
||||
dub_track_t *track = NULL;
|
||||
|
||||
if (bug) {
|
||||
struct cap_cb *cb =(struct cap_cb *) switch_core_media_bug_get_user_data(bug);
|
||||
switch_mutex_lock(cb->mutex);
|
||||
for (int i = 0; i < MAX_DUB_TRACKS; i++) {
|
||||
if (cb->tracks[i].state != DUB_TRACK_STATE_INACTIVE && strcmp(cb->tracks[i].trackName, trackName) == 0) {
|
||||
track = &cb->tracks[i];
|
||||
break;
|
||||
}
|
||||
status = say_dub_track(cb, trackName, text, gain);
|
||||
if (status != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dub_say_on_track: error saying %s on track %s\n", text, trackName);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
if (track) {
|
||||
status = say_dub_track(track, cb->mutex, text, gain);
|
||||
}
|
||||
switch_mutex_unlock(cb->mutex);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
#define DUB_API_SYNTAX "<uuid> [addTrack|removeTrack|silenceTrack|playOnTrack|sayOnTrack|setGain] track [url|text|gain] [gain] [loop]"
|
||||
#define MAX_PARAMS 6
|
||||
SWITCH_STANDARD_API(dub_function)
|
||||
{
|
||||
char *mycmd = NULL, *argv[6] = { 0 };
|
||||
char *mycmd = NULL, *argv[MAX_PARAMS] = { 0 };
|
||||
int argc = 0;
|
||||
int error_written = 0;
|
||||
switch_status_t status = SWITCH_STATUS_FALSE;
|
||||
|
||||
if (!zstr(cmd) && (mycmd = strdup(cmd))) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "dub_function: %s\n", mycmd);
|
||||
argc = switch_separate_string(mycmd, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
|
||||
argc = switch_separate_string(mycmd, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
|
||||
}
|
||||
|
||||
if (zstr(cmd) || argc < 3 || zstr(argv[0]) || zstr(argv[1]) || zstr(argv[2])) {
|
||||
@@ -302,6 +249,7 @@ SWITCH_STANDARD_API(dub_function)
|
||||
}
|
||||
else if (0 == strcmp(action, "playOnTrack")) {
|
||||
if (argc < 4) {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "playOnTrack requires a url\n");
|
||||
stream->write_function(stream, "-USAGE: %s\n", DUB_API_SYNTAX);
|
||||
error_written = 1;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user