FS-11677: [mod_video_filter] support api/app to replace the write video of session with a bug
https://freeswitch.org/jira/browse/FS-11677
This commit is contained in:
parent
fa18a3d0d6
commit
9847a8915f
|
@ -70,6 +70,12 @@ typedef struct chromakey_child_context_s {
|
|||
} chromakey_child_context_t;
|
||||
|
||||
|
||||
typedef struct video_replace_context_s {
|
||||
switch_image_t *rp_img;
|
||||
switch_file_handle_t vfh;
|
||||
switch_core_session_t *session;
|
||||
} video_replace_context_t;
|
||||
|
||||
|
||||
static void init_context(chromakey_context_t *context)
|
||||
{
|
||||
|
@ -817,6 +823,245 @@ SWITCH_STANDARD_API(chromakey_api_function)
|
|||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
static switch_status_t video_replace_thread_callback(switch_core_session_t *session, switch_frame_t *frame, void *user_data)
|
||||
{
|
||||
video_replace_context_t *context = (video_replace_context_t *)user_data;
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
switch_frame_t file_frame = { 0 };
|
||||
|
||||
if (!switch_channel_ready(channel)) {
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
if (!frame->img) {
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
if (switch_test_flag(&context->vfh, SWITCH_FILE_OPEN)) {
|
||||
switch_status_t status = SWITCH_STATUS_FALSE;
|
||||
|
||||
context->vfh.mm.scale_w = frame->img->d_w;
|
||||
context->vfh.mm.scale_h = frame->img->d_h;
|
||||
|
||||
status = switch_core_file_read_video(&context->vfh, &file_frame, SVR_FLUSH);
|
||||
switch_core_file_command(&context->vfh, SCFC_FLUSH_AUDIO);
|
||||
|
||||
if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) {
|
||||
int close = 1;
|
||||
|
||||
if (context->vfh.params) {
|
||||
const char *loopstr = switch_event_get_header(context->vfh.params, "loop");
|
||||
if (switch_true(loopstr)) {
|
||||
uint32_t pos = 0;
|
||||
switch_core_file_seek(&context->vfh, &pos, 0, SEEK_SET);
|
||||
close = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (close) {
|
||||
switch_core_file_close(&context->vfh);
|
||||
}
|
||||
} else if (file_frame.img) {
|
||||
switch_img_free(&context->rp_img);
|
||||
switch_img_copy(file_frame.img, &context->rp_img);
|
||||
switch_img_free(&(file_frame.img));
|
||||
}
|
||||
|
||||
if (context->rp_img) {
|
||||
switch_img_copy(context->rp_img, &frame->img);
|
||||
}
|
||||
}
|
||||
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
static switch_bool_t video_replace_bug_callback(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type)
|
||||
{
|
||||
video_replace_context_t *context = (video_replace_context_t *)user_data;
|
||||
|
||||
switch (type) {
|
||||
case SWITCH_ABC_TYPE_INIT:
|
||||
{
|
||||
}
|
||||
break;
|
||||
case SWITCH_ABC_TYPE_CLOSE:
|
||||
{
|
||||
switch_thread_rwlock_unlock(MODULE_INTERFACE->rwlock);
|
||||
switch_img_free(&context->rp_img);
|
||||
|
||||
if (switch_test_flag(&context->vfh, SWITCH_FILE_OPEN)) {
|
||||
switch_core_file_close(&context->vfh);
|
||||
memset(&context->vfh, 0, sizeof(context->vfh));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SWITCH_ABC_TYPE_READ_VIDEO_PING:
|
||||
case SWITCH_ABC_TYPE_VIDEO_PATCH:
|
||||
{
|
||||
switch_frame_t *frame = switch_core_media_bug_get_video_ping_frame(bug);
|
||||
video_replace_thread_callback(context->session, frame, context);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return SWITCH_TRUE;
|
||||
}
|
||||
|
||||
SWITCH_STANDARD_APP(video_replace_start_function)
|
||||
{
|
||||
switch_media_bug_t *bug;
|
||||
switch_status_t status;
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
switch_media_bug_flag_t flags = SMBF_READ_VIDEO_PING|SMBF_READ_VIDEO_PATCH;
|
||||
const char *function = "video_replace";
|
||||
video_replace_context_t *context;
|
||||
|
||||
if ((bug = (switch_media_bug_t *) switch_channel_get_private(channel, "_video_replace_bug_"))) {
|
||||
if (!zstr(data) && !strcasecmp(data, "stop")) {
|
||||
switch_channel_set_private(channel, "_video_replace_bug_", NULL);
|
||||
switch_core_media_bug_remove(session, &bug);
|
||||
} else {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "alreday start!\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
switch_channel_wait_for_flag(channel, CF_VIDEO_READY, SWITCH_TRUE, 10000, NULL);
|
||||
|
||||
context = (video_replace_context_t *) switch_core_session_alloc(session, sizeof(*context));
|
||||
switch_assert(context != NULL);
|
||||
memset(context, 0, sizeof(*context));
|
||||
context->session = session;
|
||||
|
||||
switch_thread_rwlock_rdlock(MODULE_INTERFACE->rwlock);
|
||||
|
||||
if (switch_core_file_open(&context->vfh, data, 1, 8000,
|
||||
SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT | SWITCH_FILE_FLAG_VIDEO,
|
||||
switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error opening video file\n");
|
||||
switch_thread_rwlock_unlock(MODULE_INTERFACE->rwlock);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((status = switch_core_media_bug_add(session, function, NULL, video_replace_bug_callback, context, 0, flags, &bug)) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Failure!\n");
|
||||
switch_thread_rwlock_unlock(MODULE_INTERFACE->rwlock);
|
||||
return;
|
||||
}
|
||||
|
||||
switch_channel_set_private(channel, "_video_replace_bug_", bug);
|
||||
}
|
||||
|
||||
/* API Interface Function */
|
||||
#define VIDEO_REPLACE_API_SYNTAX "<uuid> [start|stop] <file>"
|
||||
SWITCH_STANDARD_API(video_replace_api_function)
|
||||
{
|
||||
switch_core_session_t *rsession = NULL;
|
||||
switch_channel_t *channel = NULL;
|
||||
switch_media_bug_t *bug;
|
||||
switch_status_t status;
|
||||
video_replace_context_t *context;
|
||||
char *mycmd = NULL;
|
||||
int argc = 0;
|
||||
char *argv[3] = { 0 };
|
||||
char *uuid = NULL;
|
||||
char *action = NULL;
|
||||
char *file = NULL;
|
||||
switch_media_bug_flag_t flags = SMBF_READ_VIDEO_PING | SMBF_READ_VIDEO_PATCH;
|
||||
const char *function = "video_replace";
|
||||
|
||||
if (zstr(cmd)) {
|
||||
goto usage;
|
||||
}
|
||||
|
||||
if (!(mycmd = strdup(cmd))) {
|
||||
goto usage;
|
||||
}
|
||||
|
||||
if ((argc = switch_separate_string(mycmd, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) < 2) {
|
||||
goto usage;
|
||||
}
|
||||
|
||||
uuid = argv[0];
|
||||
action = argv[1];
|
||||
file = argv[2];
|
||||
|
||||
if (!(rsession = switch_core_session_locate(uuid))) {
|
||||
stream->write_function(stream, "-ERR Cannot locate session!\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
channel = switch_core_session_get_channel(rsession);
|
||||
|
||||
bug = (switch_media_bug_t *) switch_channel_get_private(channel, "_video_replace_bug_");
|
||||
|
||||
if (!strcasecmp(action, "stop")) {
|
||||
if (bug) {
|
||||
switch_channel_set_private(channel, "_video_replace_bug_", NULL);
|
||||
switch_core_media_bug_remove(rsession, &bug);
|
||||
stream->write_function(stream, "+OK Success\n");
|
||||
} else {
|
||||
stream->write_function(stream, "-ERR not start\n");
|
||||
}
|
||||
|
||||
goto done;
|
||||
} else if (!strcasecmp(action, "start")) {
|
||||
if (zstr(file)) goto usage;
|
||||
|
||||
if (bug) {
|
||||
stream->write_function(stream, "-ERR alreday start\n");
|
||||
goto done;
|
||||
}
|
||||
} else {
|
||||
goto usage;
|
||||
}
|
||||
|
||||
context = (video_replace_context_t *) switch_core_session_alloc(rsession, sizeof(*context));
|
||||
switch_assert(context != NULL);
|
||||
context->session = rsession;
|
||||
|
||||
switch_thread_rwlock_rdlock(MODULE_INTERFACE->rwlock);
|
||||
|
||||
if (switch_core_file_open(&context->vfh, file, 1, 8000,
|
||||
SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT | SWITCH_FILE_FLAG_VIDEO,
|
||||
switch_core_session_get_pool(rsession)) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error opening video file\n");
|
||||
switch_thread_rwlock_unlock(MODULE_INTERFACE->rwlock);
|
||||
goto done;
|
||||
} else {
|
||||
switch_vid_params_t vp = { 0 };
|
||||
|
||||
switch_core_media_get_vid_params(context->session, &vp);
|
||||
context->vfh.mm.scale_w = vp.width;
|
||||
context->vfh.mm.scale_h = vp.height;
|
||||
context->vfh.mm.fps = vp.fps;
|
||||
}
|
||||
|
||||
if ((status = switch_core_media_bug_add(rsession, function, NULL,
|
||||
video_replace_bug_callback, context, 0, flags, &bug)) != SWITCH_STATUS_SUCCESS) {
|
||||
stream->write_function(stream, "-ERR Failure!\n");
|
||||
switch_thread_rwlock_unlock(MODULE_INTERFACE->rwlock);
|
||||
goto done;
|
||||
} else {
|
||||
switch_channel_set_private(channel, "_video_replace_bug_", bug);
|
||||
stream->write_function(stream, "+OK Success\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
usage:
|
||||
stream->write_function(stream, "-USAGE: %s\n", VIDEO_REPLACE_API_SYNTAX);
|
||||
|
||||
done:
|
||||
if (rsession) {
|
||||
switch_core_session_rwunlock(rsession);
|
||||
}
|
||||
|
||||
switch_safe_free(mycmd);
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_video_filter_shutdown)
|
||||
{
|
||||
|
@ -837,7 +1082,13 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_video_filter_load)
|
|||
|
||||
SWITCH_ADD_API(api_interface, "chromakey", "chromakey", chromakey_api_function, CHROMAKEY_API_SYNTAX);
|
||||
|
||||
SWITCH_ADD_APP(app_interface, "video_replace", "video_replace", "video replace bug",
|
||||
video_replace_start_function, "<file>", SAF_NONE);
|
||||
|
||||
SWITCH_ADD_API(api_interface, "video_replace", "video_replace", video_replace_api_function, VIDEO_REPLACE_API_SYNTAX);
|
||||
|
||||
switch_console_set_complete("add chromakey ::console::list_uuid ::[start:stop");
|
||||
switch_console_set_complete("add video_replace ::console::list_uuid ::[start:stop");
|
||||
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue