]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-11677: [mod_video_filter] fix memory leak and support replace read/write video...
authorLiyang <liyang@x-y-t.cn>
Wed, 6 Mar 2019 07:17:05 +0000 (15:17 +0800)
committerAndrey Volk <andywolk@gmail.com>
Mon, 30 Dec 2019 20:31:23 +0000 (00:31 +0400)
https://freeswitch.org/jira/browse/FS-11677

src/mod/applications/mod_video_filter/mod_video_filter.c

index dcb1fc8bcad095d2a20fcb3b6d45a337dc0bbca6..3cd8c84ce9a47c33e927f53a553e05a896020000 100644 (file)
@@ -823,7 +823,7 @@ 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)
+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);
@@ -840,8 +840,10 @@ static switch_status_t video_replace_thread_callback(switch_core_session_t *sess
        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);
@@ -853,21 +855,26 @@ static switch_status_t video_replace_thread_callback(switch_core_session_t *sess
                                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);
                }
        }
@@ -877,6 +884,8 @@ static switch_status_t video_replace_thread_callback(switch_core_session_t *sess
 
 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) {
@@ -896,10 +905,15 @@ static switch_bool_t video_replace_bug_callback(switch_media_bug_t *bug, void *u
                }
                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:
@@ -914,9 +928,14 @@ 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;
+       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")) {
@@ -928,6 +947,29 @@ SWITCH_STANDARD_APP(video_replace_start_function)
                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));
@@ -937,7 +979,7 @@ SWITCH_STANDARD_APP(video_replace_start_function)
 
        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");
@@ -955,7 +997,7 @@ SWITCH_STANDARD_APP(video_replace_start_function)
 }
 
 /* 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;
@@ -965,11 +1007,12 @@ SWITCH_STANDARD_API(video_replace_api_function)
        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)) {
@@ -986,7 +1029,6 @@ SWITCH_STANDARD_API(video_replace_api_function)
 
        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");
@@ -1008,7 +1050,23 @@ SWITCH_STANDARD_API(video_replace_api_function)
 
                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");
@@ -1030,13 +1088,6 @@ SWITCH_STANDARD_API(video_replace_api_function)
                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,
@@ -1083,12 +1134,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);
+                                  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;
 }