return SWITCH_STATUS_SUCCESS;
}
-static switch_status_t video_replace_thread_callback(switch_core_session_t *session, switch_frame_t *frame, void *user_data)
+static switch_status_t video_replace_thread_callback(switch_core_session_t *session, switch_frame_t *frame, void *user_data, switch_abc_type_t type)
{
video_replace_context_t *context = (video_replace_context_t *)user_data;
switch_channel_t *channel = switch_core_session_get_channel(session);
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;
+ if (type == SWITCH_ABC_TYPE_READ_VIDEO_PING || (context->vfh.params && switch_true(switch_event_get_header(context->vfh.params, "scale")))) {
+ 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);
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 (switch_core_file_seek(&context->vfh, &pos, 0, SEEK_SET) == SWITCH_STATUS_SUCCESS) 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 (file_frame.img) {
+ switch_img_free(&(context->rp_img));
+ context->rp_img = file_frame.img;
}
if (context->rp_img) {
+ if (context->rp_img->d_w != frame->img->d_w || context->rp_img->d_h != frame->img->d_h ) {
+ frame->img = NULL;
+ }
+
switch_img_copy(context->rp_img, &frame->img);
}
}
static switch_bool_t video_replace_bug_callback(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type)
{
+ switch_core_session_t *session = switch_core_media_bug_get_session(bug);
+ switch_channel_t *channel = switch_core_session_get_channel(session);
video_replace_context_t *context = (video_replace_context_t *)user_data;
switch (type) {
}
break;
case SWITCH_ABC_TYPE_READ_VIDEO_PING:
- case SWITCH_ABC_TYPE_VIDEO_PATCH:
+ case SWITCH_ABC_TYPE_WRITE_VIDEO_PING:
{
- switch_frame_t *frame = switch_core_media_bug_get_video_ping_frame(bug);
- video_replace_thread_callback(context->session, frame, context);
+ if (switch_test_flag(&context->vfh, SWITCH_FILE_OPEN)) {
+ switch_frame_t *frame = switch_core_media_bug_get_video_ping_frame(bug);
+ video_replace_thread_callback(context->session, frame, context, type);
+ } else {
+ switch_channel_set_private(channel, "_video_replace_bug_", NULL);
+ return SWITCH_FALSE;
+ }
}
break;
default:
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;
+ switch_media_bug_flag_t flags = 0;
const char *function = "video_replace";
video_replace_context_t *context;
+ char *lbuf;
+ int argc = 0;
+ char *argv[2] = { 0 };
+ char *direction = NULL;
+ char *file = NULL;
if ((bug = (switch_media_bug_t *) switch_channel_get_private(channel, "_video_replace_bug_"))) {
if (!zstr(data) && !strcasecmp(data, "stop")) {
return;
}
+ if (data && (lbuf = switch_core_session_strdup(session, data))
+ && (argc = switch_separate_string(lbuf, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) > 0) {
+
+ if (argc > 1) {
+ direction = argv[0];
+ file = argv[1];
+ } else {
+ direction = "write";
+ file = lbuf;
+ }
+
+ if (!strcasecmp(direction, "read")) {
+ flags = SMBF_READ_VIDEO_PING;
+ } else if (!strcasecmp(direction, "write")) {
+ flags = SMBF_WRITE_VIDEO_PING;
+ } else {
+ switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "invalid replace direction!\n");
+ return;
+ }
+ } else {
+ 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_thread_rwlock_rdlock(MODULE_INTERFACE->rwlock);
- if (switch_core_file_open(&context->vfh, data, 1, 8000,
+ 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(session)) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error opening video file\n");
}
/* API Interface Function */
-#define VIDEO_REPLACE_API_SYNTAX "<uuid> [start|stop] <file>"
+#define VIDEO_REPLACE_API_SYNTAX "<uuid> <stop|start> [read|write] <file>"
SWITCH_STANDARD_API(video_replace_api_function)
{
switch_core_session_t *rsession = NULL;
video_replace_context_t *context;
char *mycmd = NULL;
int argc = 0;
- char *argv[3] = { 0 };
+ char *argv[4] = { 0 };
char *uuid = NULL;
char *action = NULL;
char *file = NULL;
- switch_media_bug_flag_t flags = SMBF_READ_VIDEO_PING | SMBF_READ_VIDEO_PATCH;
+ char *direction = NULL;
+ switch_media_bug_flag_t flags = 0;
const char *function = "video_replace";
if (zstr(cmd)) {
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;
} else if (!strcasecmp(action, "start")) {
- if (zstr(file)) goto usage;
+ if (argc == 3) {
+ direction = "write";
+ file = argv[2];
+ } else {
+ direction = argv[2];
+ file = argv[3];
+ }
+
+ if (zstr(direction) || zstr(file)) goto usage;
+
+ if (!strcasecmp(direction, "read")) {
+ flags = SMBF_READ_VIDEO_PING;
+ } else if (!strcasecmp(direction, "write")) {
+ flags = SMBF_WRITE_VIDEO_PING;
+ } else {
+ goto usage;
+ }
if (bug) {
stream->write_function(stream, "-ERR alreday start\n");
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,
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);
+ video_replace_start_function, "[read|write] <file> | stop", SAF_NONE);
- SWITCH_ADD_API(api_interface, "video_replace", "video_replace", video_replace_api_function, VIDEO_REPLACE_API_SYNTAX);
+ SWITCH_ADD_API(api_interface, "uuid_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");
+ switch_console_set_complete("add uuid_video_replace ::console::list_uuid start ::[read:write");
+ switch_console_set_complete("add uuid_video_replace ::console::list_uuid stop");
return SWITCH_STATUS_SUCCESS;
}