From 7c94fc168012021a8eaadd95f07c000b672514fe Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Sat, 13 Jun 2015 02:57:50 -0500 Subject: [PATCH] play video files for hold_music squashme --- src/include/switch_core_media.h | 1 + src/include/switch_types.h | 2 + src/switch_core_media.c | 53 ++++++++++++++++++++++++-- src/switch_ivr_bridge.c | 66 +++++++++++++++++++++++++++++++-- 4 files changed, 116 insertions(+), 6 deletions(-) diff --git a/src/include/switch_core_media.h b/src/include/switch_core_media.h index 09958e7e45..2d42f05720 100644 --- a/src/include/switch_core_media.h +++ b/src/include/switch_core_media.h @@ -331,6 +331,7 @@ SWITCH_DECLARE(void) switch_core_session_stop_media(switch_core_session_t *sessi SWITCH_DECLARE(switch_media_flow_t) switch_core_session_media_flow(switch_core_session_t *session, switch_media_type_t type); SWITCH_DECLARE(switch_status_t) switch_core_media_get_vid_params(switch_core_session_t *session, switch_vid_params_t *vid_params); SWITCH_DECLARE(switch_status_t) switch_core_media_set_video_file(switch_core_session_t *session, switch_file_handle_t *fh, switch_rw_t rw); +SWITCH_DECLARE(switch_file_handle_t *) switch_core_media_get_video_file(switch_core_session_t *session, switch_rw_t rw); SWITCH_DECLARE(switch_bool_t) switch_core_session_in_video_thread(switch_core_session_t *session); SWITCH_DECLARE(switch_bool_t) switch_core_media_check_dtls(switch_core_session_t *session, switch_media_type_t type); diff --git a/src/include/switch_types.h b/src/include/switch_types.h index b170414344..c435106c38 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -1486,6 +1486,8 @@ typedef enum { CF_VIDEO_ONLY, CF_VIDEO_READY, CF_VIDEO_MIRROR_INPUT, + CF_VIDEO_READ_FILE_ATTACHED, + CF_VIDEO_WRITE_FILE_ATTACHED, /* WARNING: DO NOT ADD ANY FLAGS BELOW THIS LINE */ /* IF YOU ADD NEW ONES CHECK IF THEY SHOULD PERSIST OR ZERO THEM IN switch_core_session.c switch_core_session_request_xml() */ CF_FLAG_MAX diff --git a/src/switch_core_media.c b/src/switch_core_media.c index 1432d30c8e..deba971311 100644 --- a/src/switch_core_media.c +++ b/src/switch_core_media.c @@ -4763,6 +4763,39 @@ SWITCH_DECLARE(int) switch_core_media_toggle_hold(switch_core_session_t *session return changed; } + +SWITCH_DECLARE(switch_file_handle_t *) switch_core_media_get_video_file(switch_core_session_t *session, switch_rw_t rw) +{ + switch_media_handle_t *smh; + switch_rtp_engine_t *v_engine; + switch_file_handle_t *fh; + + switch_assert(session); + + if (!switch_channel_test_flag(session->channel, CF_VIDEO)) { + return NULL; + } + + if (!(smh = session->media_handle)) { + return NULL; + } + + v_engine = &smh->engines[SWITCH_MEDIA_TYPE_VIDEO]; + + switch_mutex_lock(v_engine->mh.file_mutex); + + if (rw == SWITCH_RW_READ) { + fh = smh->video_read_fh; + } else { + fh = smh->video_write_fh; + } + + switch_mutex_unlock(v_engine->mh.file_mutex); + + return fh; +} + + SWITCH_DECLARE(switch_status_t) switch_core_media_set_video_file(switch_core_session_t *session, switch_file_handle_t *fh, switch_rw_t rw) { switch_media_handle_t *smh; @@ -4789,14 +4822,28 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_set_video_file(switch_core_ses switch_mutex_lock(v_engine->mh.file_mutex); if (rw == SWITCH_RW_READ) { - smh->video_read_fh = fh; + if (fh) { switch_channel_set_flag_recursive(session->channel, CF_VIDEO_DECODED_READ); - } else { + switch_channel_set_flag(session->channel, CF_VIDEO_READ_FILE_ATTACHED); + } else if (smh->video_read_fh) { switch_channel_clear_flag_recursive(session->channel, CF_VIDEO_DECODED_READ); switch_core_session_video_reset(session); } + + if (!fh) { + switch_channel_clear_flag(session->channel, CF_VIDEO_READ_FILE_ATTACHED); + } + + smh->video_read_fh = fh; + } else { + if (fh) { + switch_channel_set_flag(session->channel, CF_VIDEO_WRITE_FILE_ATTACHED); + } else { + switch_channel_clear_flag(session->channel, CF_VIDEO_WRITE_FILE_ATTACHED); + } + switch_core_media_gen_key_frame(session); smh->video_write_fh = fh; } @@ -4941,7 +4988,7 @@ static void *SWITCH_THREAD_FUNC video_helper_thread(switch_thread_t *thread, voi vloops++; if (!buf) { - int buflen = SWITCH_RECOMMENDED_BUFFER_SIZE * 4; + int buflen = SWITCH_RTP_MAX_BUF_LEN; buf = switch_core_session_alloc(session, buflen); fr.packet = buf; fr.packetlen = buflen; diff --git a/src/switch_ivr_bridge.c b/src/switch_ivr_bridge.c index 129ae72e12..a6d086ec68 100644 --- a/src/switch_ivr_bridge.c +++ b/src/switch_ivr_bridge.c @@ -53,7 +53,9 @@ static void video_bridge_thread(switch_core_session_t *session, void *obj) switch_channel_t *b_channel = switch_core_session_get_channel(vh->session_b); switch_status_t status; switch_frame_t *read_frame = 0; - int set_decoded_read = 0; + int set_decoded_read = 0, refresh_timer = 0, playing_file = 0; + switch_frame_t fr = { 0 }; + unsigned char *buf = NULL; vh->up = 1; @@ -63,10 +65,13 @@ static void video_bridge_thread(switch_core_session_t *session, void *obj) switch_core_session_request_video_refresh(vh->session_a); switch_core_session_request_video_refresh(vh->session_b); + refresh_timer = 5; + while (switch_channel_up_nosig(channel) && switch_channel_up_nosig(b_channel) && vh->up == 1) { if (switch_channel_media_up(channel)) { switch_codec_t *a_codec = switch_core_session_get_video_read_codec(vh->session_a); switch_codec_t *b_codec = switch_core_session_get_video_write_codec(vh->session_b); + switch_file_handle_t *fh; switch_assert(a_codec); switch_assert(b_codec); @@ -76,6 +81,7 @@ static void video_bridge_thread(switch_core_session_t *session, void *obj) if (set_decoded_read) { switch_channel_clear_flag_recursive(channel, CF_VIDEO_DECODED_READ); set_decoded_read = 0; + refresh_timer = 5; } } } else { @@ -83,18 +89,72 @@ static void video_bridge_thread(switch_core_session_t *session, void *obj) switch_channel_test_flag(b_channel, CF_VIDEO_DECODED_READ)) { switch_channel_set_flag_recursive(channel, CF_VIDEO_DECODED_READ); set_decoded_read = 1; + refresh_timer = 5; } } - status = switch_core_session_read_video_frame(vh->session_a, &read_frame, SWITCH_IO_FLAG_NONE, 0); + + if ((fh = switch_core_media_get_video_file(session, SWITCH_RW_WRITE)) && switch_test_flag(fh, SWITCH_FILE_OPEN)) { + switch_status_t wstatus; + + if (!playing_file) { + playing_file = 1; + } + + if (!buf) { + int buflen = SWITCH_RTP_MAX_BUF_LEN; + + buf = switch_core_session_alloc(session, buflen); + fr.packet = buf; + fr.packetlen = buflen; + fr.data = buf + 12; + fr.buflen = buflen - 12; + switch_core_media_gen_key_frame(session); + } + + wstatus = switch_core_file_read_video(fh, &fr, SVR_BLOCK); + + if (wstatus == SWITCH_STATUS_SUCCESS) { + switch_core_session_write_video_frame(session, &fr, SWITCH_IO_FLAG_NONE, 0); + switch_img_free(&fr.img); + } else if (wstatus != SWITCH_STATUS_BREAK) { + switch_core_media_set_video_file(session, NULL, SWITCH_RW_WRITE); + } + + continue; + } + + if (playing_file) { + playing_file = 0; + switch_core_session_request_video_refresh(vh->session_a); + switch_core_session_request_video_refresh(vh->session_b); + switch_channel_video_sync(channel); + refresh_timer = 5; + } + + if (refresh_timer) { + if (--refresh_timer == 0) { + switch_core_session_request_video_refresh(vh->session_a); + switch_core_session_request_video_refresh(vh->session_b); + } + } + + + status = switch_core_session_read_video_frame(vh->session_a, &read_frame, SWITCH_IO_FLAG_NONE, 0); + if (!SWITCH_READ_ACCEPTABLE(status)) { switch_cond_next(); continue; } + + + if (switch_test_flag(read_frame, SFF_CNG)) { + continue; + } } - if (switch_test_flag(read_frame, SFF_CNG)) { + if (switch_channel_test_flag(channel, CF_LEG_HOLDING) || switch_channel_test_flag(b_channel, CF_VIDEO_READ_FILE_ATTACHED)) { continue; }