Skip to content

Commit

Permalink
TEL-6181:replace pointer to sessions with their uuids in vid_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
izzytelnyx committed Nov 5, 2024
1 parent 7d00934 commit c92591d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/include/switch_core_media.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ SWITCH_DECLARE(switch_bool_t) switch_core_media_codec_get_cap(switch_core_sessio


SWITCH_DECLARE(switch_timer_t *) switch_core_media_get_timer(switch_core_session_t *session, switch_media_type_t mtype);
SWITCH_DECLARE(void) switch_core_media_start_engine_function(switch_core_session_t *session, switch_media_type_t type, switch_engine_function_t engine_function, void *user_data);
SWITCH_DECLARE(void) switch_core_media_start_engine_function(const char *uuid, switch_media_type_t type, switch_engine_function_t engine_function, void *user_data);
SWITCH_DECLARE(void) switch_core_media_end_engine_function(switch_core_session_t *session, switch_media_type_t type);
SWITCH_DECLARE(switch_status_t) switch_core_session_start_video_thread(switch_core_session_t *session);
SWITCH_DECLARE(int) switch_core_media_check_engine_function(switch_core_session_t *session, switch_media_type_t type);
Expand Down
4 changes: 2 additions & 2 deletions src/mod/applications/mod_fsv/mod_fsv.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ SWITCH_STANDARD_APP(record_fsv_function)
switch_mutex_init(&mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
eh.mutex = mutex;
eh.fd = fd;
switch_core_media_start_engine_function(session, SWITCH_MEDIA_TYPE_VIDEO, record_video_thread, &eh);
switch_core_media_start_engine_function(switch_core_session_get_uuid(session), SWITCH_MEDIA_TYPE_VIDEO, record_video_thread, &eh);
switch_core_session_request_video_refresh(session);
}

Expand Down Expand Up @@ -761,7 +761,7 @@ SWITCH_STANDARD_APP(decode_video_function)

switch_channel_set_flag_recursive(channel, CF_VIDEO_DECODED_READ);

switch_core_media_start_engine_function(session, SWITCH_MEDIA_TYPE_VIDEO, decode_video_thread, &max_pictures);
switch_core_media_start_engine_function(switch_core_session_get_uuid(session), SWITCH_MEDIA_TYPE_VIDEO, decode_video_thread, &max_pictures);

switch_ivr_play_file(session, NULL, moh, NULL);

Expand Down
9 changes: 8 additions & 1 deletion src/switch_core_media.c
Original file line number Diff line number Diff line change
Expand Up @@ -8830,12 +8830,18 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_start_video_thread(switch_co
return SWITCH_STATUS_SUCCESS;
}

SWITCH_DECLARE(void) switch_core_media_start_engine_function(switch_core_session_t *session, switch_media_type_t type, switch_engine_function_t engine_function, void *user_data)
SWITCH_DECLARE(void) switch_core_media_start_engine_function(const char *uuid, switch_media_type_t type, switch_engine_function_t engine_function, void *user_data)
{
switch_core_session_t *session = switch_core_session_locate(uuid);
switch_media_handle_t *smh;
switch_rtp_engine_t *engine;

if (!session) {
return;
}

if (!(smh = session->media_handle)) {
switch_core_session_rwunlock(session);
return;
}

Expand All @@ -8856,6 +8862,7 @@ SWITCH_DECLARE(void) switch_core_media_start_engine_function(switch_core_session
switch_core_session_video_reset(session);
}
switch_mutex_unlock(smh->control_mutex);
switch_core_session_rwunlock(session);
}

SWITCH_DECLARE(int) switch_core_media_check_engine_function(switch_core_session_t *session, switch_media_type_t type)
Expand Down
97 changes: 63 additions & 34 deletions src/switch_ivr_bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ static void cleanup_proxy_mode_b(switch_core_session_t *session);
/*********************************************************************************/

struct vid_helper {
switch_core_session_t *session_a;
switch_core_session_t *session_b;
char session_a_uuid[SWITCH_UUID_FORMATTED_LENGTH + 1];
char session_b_uuid[SWITCH_UUID_FORMATTED_LENGTH + 1];
int up;
};

Expand All @@ -52,24 +52,34 @@ static void text_bridge_thread(switch_core_session_t *session, void *obj)
struct vid_helper *vh = obj;
switch_status_t status;
switch_frame_t *read_frame = 0;
switch_channel_t *channel = switch_core_session_get_channel(vh->session_a);
switch_channel_t *b_channel = switch_core_session_get_channel(vh->session_b);
switch_channel_t *channel;
switch_channel_t *b_channel;
switch_buffer_t *text_buffer = NULL;
switch_size_t text_framesize = 1024, inuse = 0;
unsigned char *text_framedata = NULL;
switch_frame_t frame = { 0 };
switch_core_session_t * session_a, *session_b;

if ((session_a = switch_core_session_locate(vh->session_a_uuid))) {
channel = switch_core_session_get_channel(session_a);
}

if ((session_b = switch_core_session_locate(vh->session_b_uuid))) {
b_channel = switch_core_session_get_channel(session_b);
}

switch_buffer_create_dynamic(&text_buffer, 512, 1024, 0);
switch_zmalloc(text_framedata, 1024);
text_framesize = 1024;

vh->up = 1;


while (switch_channel_up_nosig(channel) && switch_channel_up_nosig(b_channel) && vh->up == 1) {
status = switch_core_session_read_text_frame(vh->session_a, &read_frame, SWITCH_IO_FLAG_NONE, 0);
status = switch_core_session_read_text_frame(session_a, &read_frame, SWITCH_IO_FLAG_NONE, 0);

if (!SWITCH_READ_ACCEPTABLE(status)) {
switch_core_session_write_text_frame(vh->session_a, NULL, 0, 0);
switch_core_session_write_text_frame(session_a, NULL, 0, 0);
continue;
}

Expand Down Expand Up @@ -127,17 +137,25 @@ static void text_bridge_thread(switch_core_session_t *session, void *obj)
}

if (!switch_test_flag(read_frame, SFF_CNG)) {
switch_status_t tstatus = switch_core_session_write_text_frame(vh->session_b, read_frame, 0, 0);
switch_status_t tstatus = switch_core_session_write_text_frame(session_b, read_frame, 0, 0);

if (tstatus != SWITCH_STATUS_SUCCESS) {
switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
}
}
switch_core_session_write_text_frame(vh->session_a, NULL, 0, 0);
switch_core_session_write_text_frame(session_a, NULL, 0, 0);
}

vh->up = 0;

if (session_a) {
switch_core_session_rwunlock(session_a);
}

if (session_b) {
switch_core_session_rwunlock(session_b);
}

switch_buffer_destroy(&text_buffer);
switch_safe_free(text_framedata);
}
Expand All @@ -148,35 +166,44 @@ static void text_bridge_thread(switch_core_session_t *session, void *obj)
static void video_bridge_thread(switch_core_session_t *session, void *obj)
{
struct vid_helper *vh = obj;
switch_channel_t *channel = switch_core_session_get_channel(vh->session_a);
switch_channel_t *b_channel = switch_core_session_get_channel(vh->session_b);
switch_channel_t *channel;
switch_channel_t *b_channel;
switch_status_t status;
switch_frame_t *read_frame = 0;
int set_decoded_read = 0, refresh_timer = 0;
int refresh_cnt = 300;
int pass_val = 0, last_pass_val = 0;
switch_core_session_t * session_a, *session_b;

if ((session_a = switch_core_session_locate(vh->session_a_uuid))) {
channel = switch_core_session_get_channel(session_a);
}

if ((session_b = switch_core_session_locate(vh->session_b_uuid))) {
b_channel = switch_core_session_get_channel(session_b);
}

vh->up = 1;

if (switch_core_session_read_lock(vh->session_a) != SWITCH_STATUS_SUCCESS) {
if (switch_core_session_read_lock(session_a) != SWITCH_STATUS_SUCCESS) {
vh->up = 0;
return;
}

if (switch_core_session_read_lock(vh->session_b) != SWITCH_STATUS_SUCCESS) {
if (switch_core_session_read_lock(session_b) != SWITCH_STATUS_SUCCESS) {
vh->up = 0;
switch_core_session_rwunlock(vh->session_a);
switch_core_session_rwunlock(session_a);
return;
}

switch_core_session_request_video_refresh(vh->session_a);
switch_core_session_request_video_refresh(vh->session_b);
switch_core_session_request_video_refresh(session_a);
switch_core_session_request_video_refresh(session_b);

refresh_timer = refresh_cnt;

while (switch_channel_up_nosig(channel) && switch_channel_up_nosig(b_channel) && vh->up == 1) {
if (switch_channel_media_up(channel)) {
if (switch_core_session_transcoding(vh->session_a, vh->session_b, SWITCH_MEDIA_TYPE_VIDEO)) {
if (switch_core_session_transcoding(session_a, session_b, SWITCH_MEDIA_TYPE_VIDEO)) {
pass_val = 1;
} else {
pass_val = 2;
Expand All @@ -196,8 +223,8 @@ static void video_bridge_thread(switch_core_session_t *session, void *obj)
switch_codec_implementation_t session_a_codec_implementation;
switch_codec_implementation_t session_b_codec_implementation;

switch_core_session_get_video_read_impl(vh->session_a, &session_a_codec_implementation);
switch_core_session_get_video_write_impl(vh->session_b, &session_b_codec_implementation);
switch_core_session_get_video_read_impl(session_a, &session_a_codec_implementation);
switch_core_session_get_video_write_impl(session_b, &session_b_codec_implementation);

if (switch_channel_test_flag(channel, CF_VIDEO_DECODED_READ)) {
if (session_a_codec_implementation.impl_id == session_b_codec_implementation.impl_id && !switch_channel_test_flag(b_channel, CF_VIDEO_DECODED_READ)) {
Expand All @@ -219,12 +246,12 @@ static void video_bridge_thread(switch_core_session_t *session, void *obj)

if (refresh_timer) {
if (refresh_timer > 0 && (refresh_timer % 100) == 0) {
switch_core_session_request_video_refresh(vh->session_b);
switch_core_session_request_video_refresh(session_b);
}
refresh_timer--;
}

status = switch_core_session_read_video_frame(vh->session_a, &read_frame, SWITCH_IO_FLAG_NONE, 0);
status = switch_core_session_read_video_frame(session_a, &read_frame, SWITCH_IO_FLAG_NONE, 0);

if (!SWITCH_READ_ACCEPTABLE(status)) {
switch_cond_next();
Expand All @@ -239,7 +266,7 @@ static void video_bridge_thread(switch_core_session_t *session, void *obj)
}

if (switch_channel_media_up(b_channel)) {
if (switch_core_session_write_video_frame(vh->session_b, read_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
if (switch_core_session_write_video_frame(session_b, read_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
switch_cond_next();
continue;
}
Expand All @@ -250,24 +277,24 @@ static void video_bridge_thread(switch_core_session_t *session, void *obj)
switch_channel_clear_flag_recursive(channel, CF_VIDEO_DECODED_READ);
}

switch_core_session_kill_channel(vh->session_b, SWITCH_SIG_BREAK);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(vh->session_a), SWITCH_LOG_DEBUG, "%s video thread ended.\n", switch_channel_get_name(channel));
switch_core_session_kill_channel(session_b, SWITCH_SIG_BREAK);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session_a), SWITCH_LOG_DEBUG, "%s video thread ended.\n", switch_channel_get_name(channel));

switch_core_session_request_video_refresh(vh->session_a);
switch_core_session_request_video_refresh(vh->session_b);
switch_core_session_request_video_refresh(session_a);
switch_core_session_request_video_refresh(session_b);

switch_core_session_passthru(vh->session_a, SWITCH_MEDIA_TYPE_VIDEO, SWITCH_FALSE);
switch_core_session_passthru(session_a, SWITCH_MEDIA_TYPE_VIDEO, SWITCH_FALSE);

switch_core_session_rwunlock(vh->session_a);
switch_core_session_rwunlock(vh->session_b);
switch_core_session_rwunlock(session_a);
switch_core_session_rwunlock(session_b);

vh->up = 0;
return;
}

static void launch_video(struct vid_helper *vh)
{
switch_core_media_start_engine_function(vh->session_a, SWITCH_MEDIA_TYPE_VIDEO, video_bridge_thread, vh);
switch_core_media_start_engine_function(vh->session_a_uuid, SWITCH_MEDIA_TYPE_VIDEO, video_bridge_thread, vh);
}
#endif

Expand Down Expand Up @@ -699,17 +726,17 @@ static void *audio_bridge_thread(switch_thread_t *thread, void *obj)
if (switch_channel_test_flag(chan_a, CF_HAS_TEXT) && switch_channel_test_flag(chan_b, CF_HAS_TEXT) && !txt_launch) {
txt_launch++;

th.session_a = session_a;
th.session_b = session_b;
switch_core_media_start_engine_function(th.session_a, SWITCH_MEDIA_TYPE_TEXT, text_bridge_thread, &th);
strcpy(th.session_a_uuid, switch_core_session_get_uuid(session_a));
strcpy(th.session_b_uuid, switch_core_session_get_uuid(session_b));
switch_core_media_start_engine_function(th.session_a_uuid, SWITCH_MEDIA_TYPE_TEXT, text_bridge_thread, &th);

}

#ifdef SWITCH_VIDEO_IN_THREADS
if (switch_channel_test_flag(chan_a, CF_VIDEO) && switch_channel_test_flag(chan_b, CF_VIDEO) && !vid_launch) {
vid_launch++;
vh.session_a = session_a;
vh.session_b = session_b;
strcpy(vh.session_a_uuid, switch_core_session_get_uuid(session_a));
strcpy(vh.session_b_uuid, switch_core_session_get_uuid(session_b));
switch_channel_clear_flag(chan_a, CF_VIDEO_BLANK);
switch_channel_clear_flag(chan_b, CF_VIDEO_BLANK);
launch_video(&vh);
Expand Down Expand Up @@ -2029,10 +2056,12 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_multi_threaded_bridge(switch_core_ses
}

if ((cause = switch_channel_get_cause(caller_channel))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "IZZYLOG12 set hangupcause to %s\n", switch_channel_cause2str(cause));
switch_channel_set_variable(peer_channel, SWITCH_BRIDGE_HANGUP_CAUSE_VARIABLE, switch_channel_cause2str(cause));
}

if ((cause = switch_channel_get_cause(peer_channel))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "IZZYLOG12* set hangupcause to %s\n", switch_channel_cause2str(cause));
switch_channel_set_variable(caller_channel, SWITCH_BRIDGE_HANGUP_CAUSE_VARIABLE, switch_channel_cause2str(cause));
}

Expand Down

0 comments on commit c92591d

Please sign in to comment.