Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2015 Savoir-Faire Linux Inc. |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 3 | * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Additional permission under GNU GPL version 3 section 7: |
| 20 | * |
| 21 | * If you modify this program, or any covered work, by linking or |
| 22 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 23 | * modified version of that library), containing parts covered by the |
| 24 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 25 | * grants you additional permission to convey the resulting work. |
| 26 | * Corresponding Source for a non-source form of such a combination |
| 27 | * shall include the source code for the parts of OpenSSL used as well |
| 28 | * as that of the covered work. |
| 29 | */ |
| 30 | |
| 31 | #include "video_widget.h" |
| 32 | |
Julien Grossholtz | 58cfc15 | 2015-10-22 15:43:43 -0400 | [diff] [blame] | 33 | #include <glib/gi18n.h> |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 34 | #include <clutter/clutter.h> |
| 35 | #include <clutter-gtk/clutter-gtk.h> |
| 36 | #include <video/renderer.h> |
Stepan Salenikovich | 033dc83 | 2015-03-23 15:56:47 -0400 | [diff] [blame] | 37 | #include <video/sourcemodel.h> |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 38 | #include <video/devicemodel.h> |
| 39 | #include <QtCore/QUrl> |
| 40 | #include "../defines.h" |
Stepan Salenikovich | 8bc51e5 | 2015-03-21 20:17:29 -0400 | [diff] [blame] | 41 | #include <stdlib.h> |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 42 | #include <atomic> |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame] | 43 | #include <mutex> |
Stepan Salenikovich | 8bc51e5 | 2015-03-21 20:17:29 -0400 | [diff] [blame] | 44 | #include "xrectsel.h" |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 45 | |
Stepan Salenikovich | 9ffad5e | 2015-09-25 13:16:50 -0400 | [diff] [blame] | 46 | static constexpr int VIDEO_LOCAL_SIZE = 150; |
| 47 | static constexpr int VIDEO_LOCAL_OPACITY_DEFAULT = 255; /* out of 255 */ |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 48 | |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 49 | /* check video frame queues at this rate; |
| 50 | * use 30 ms (about 30 fps) since we don't expect to |
| 51 | * receive video frames faster than that */ |
Stepan Salenikovich | 9ffad5e | 2015-09-25 13:16:50 -0400 | [diff] [blame] | 52 | static constexpr int FRAME_RATE_PERIOD = 30; |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 53 | |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 54 | struct _VideoWidgetClass { |
Stepan Salenikovich | 36ef394 | 2015-11-05 18:26:57 -0500 | [diff] [blame] | 55 | GtkClutterEmbedClass parent_class; |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | struct _VideoWidget { |
Stepan Salenikovich | 36ef394 | 2015-11-05 18:26:57 -0500 | [diff] [blame] | 59 | GtkClutterEmbed parent; |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | typedef struct _VideoWidgetPrivate VideoWidgetPrivate; |
| 63 | |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 64 | typedef struct _VideoWidgetRenderer VideoWidgetRenderer; |
| 65 | |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 66 | struct _VideoWidgetPrivate { |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 67 | ClutterActor *video_container; |
| 68 | |
| 69 | /* remote peer data */ |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 70 | VideoWidgetRenderer *remote; |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 71 | |
| 72 | /* local peer data */ |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 73 | VideoWidgetRenderer *local; |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 74 | |
| 75 | guint frame_timeout_source; |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 76 | |
| 77 | /* new renderers should be put into the queue for processing by a g_timeout |
| 78 | * function whose id should be saved into renderer_timeout_source; |
| 79 | * this way when the VideoWidget object is destroyed, we do not try |
| 80 | * to process any new renderers by stoping the g_timeout function. |
| 81 | */ |
| 82 | guint renderer_timeout_source; |
| 83 | GAsyncQueue *new_renderer_queue; |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | struct _VideoWidgetRenderer { |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 87 | VideoRendererType type; |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 88 | ClutterActor *actor; |
Stepan Salenikovich | 223b2fd | 2015-06-22 12:52:21 -0400 | [diff] [blame] | 89 | ClutterAction *drag_action; |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 90 | Video::Renderer *renderer; |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame] | 91 | std::mutex run_mutex; |
| 92 | bool running; |
Stepan Salenikovich | bf118ac | 2015-04-22 14:06:50 -0400 | [diff] [blame] | 93 | |
| 94 | /* show_black_frame is used to request the actor to render a black image; |
Stepan Salenikovich | d6a4ef3 | 2015-11-12 10:59:02 -0500 | [diff] [blame^] | 95 | * this will take over 'running', ie: a black frame will be rendered even if |
| 96 | * the Video::Renderer is not running; |
Stepan Salenikovich | bf118ac | 2015-04-22 14:06:50 -0400 | [diff] [blame] | 97 | * this will be set back to false once the black frame is rendered |
| 98 | */ |
| 99 | std::atomic_bool show_black_frame; |
Stepan Salenikovich | b94873c | 2015-06-02 16:53:18 -0400 | [diff] [blame] | 100 | std::atomic_bool pause_rendering; |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 101 | QMetaObject::Connection render_stop; |
| 102 | QMetaObject::Connection render_start; |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 103 | }; |
| 104 | |
Stepan Salenikovich | 36ef394 | 2015-11-05 18:26:57 -0500 | [diff] [blame] | 105 | G_DEFINE_TYPE_WITH_PRIVATE(VideoWidget, video_widget, GTK_CLUTTER_TYPE_EMBED); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 106 | |
| 107 | #define VIDEO_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), VIDEO_WIDGET_TYPE, VideoWidgetPrivate)) |
| 108 | |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 109 | /* static prototypes */ |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 110 | static gboolean check_frame_queue (VideoWidget *); |
| 111 | static void renderer_stop (VideoWidgetRenderer *); |
| 112 | static void renderer_start (VideoWidgetRenderer *); |
| 113 | static void on_drag_data_received (GtkWidget *, GdkDragContext *, gint, gint, GtkSelectionData *, guint, guint32, gpointer); |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 114 | static gboolean on_button_press_in_screen_event(GtkWidget *, GdkEventButton *, gpointer); |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 115 | static gboolean check_renderer_queue (VideoWidget *); |
| 116 | static void free_video_widget_renderer (VideoWidgetRenderer *); |
| 117 | static void video_widget_add_renderer (VideoWidget *, VideoWidgetRenderer *); |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 118 | |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 119 | /* |
| 120 | * video_widget_dispose() |
| 121 | * |
| 122 | * The dispose function for the video_widget class. |
| 123 | */ |
| 124 | static void |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 125 | video_widget_dispose(GObject *object) |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 126 | { |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 127 | VideoWidget *self = VIDEO_WIDGET(object); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 128 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 129 | |
Stepan Salenikovich | bf74af8 | 2015-03-13 11:35:58 -0400 | [diff] [blame] | 130 | /* dispose may be called multiple times, make sure |
| 131 | * not to call g_source_remove more than once */ |
| 132 | if (priv->frame_timeout_source) { |
| 133 | g_source_remove(priv->frame_timeout_source); |
| 134 | priv->frame_timeout_source = 0; |
| 135 | } |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 136 | |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 137 | if (priv->renderer_timeout_source) { |
| 138 | g_source_remove(priv->renderer_timeout_source); |
| 139 | priv->renderer_timeout_source = 0; |
| 140 | } |
| 141 | |
| 142 | if (priv->new_renderer_queue) { |
| 143 | g_async_queue_unref(priv->new_renderer_queue); |
| 144 | priv->new_renderer_queue = NULL; |
| 145 | } |
| 146 | |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 147 | G_OBJECT_CLASS(video_widget_parent_class)->dispose(object); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
| 151 | /* |
| 152 | * video_widget_finalize() |
| 153 | * |
| 154 | * The finalize function for the video_widget class. |
| 155 | */ |
| 156 | static void |
| 157 | video_widget_finalize(GObject *object) |
| 158 | { |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 159 | VideoWidget *self = VIDEO_WIDGET(object); |
| 160 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 161 | |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 162 | free_video_widget_renderer(priv->local); |
| 163 | free_video_widget_renderer(priv->remote); |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 164 | |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 165 | G_OBJECT_CLASS(video_widget_parent_class)->finalize(object); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /* |
| 170 | * video_widget_class_init() |
| 171 | * |
| 172 | * This function init the video_widget_class. |
| 173 | */ |
| 174 | static void |
| 175 | video_widget_class_init(VideoWidgetClass *klass) |
| 176 | { |
| 177 | GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 178 | |
| 179 | /* override method */ |
| 180 | object_class->dispose = video_widget_dispose; |
| 181 | object_class->finalize = video_widget_finalize; |
| 182 | } |
| 183 | |
Stepan Salenikovich | 223b2fd | 2015-06-22 12:52:21 -0400 | [diff] [blame] | 184 | static void |
| 185 | on_allocation_changed(ClutterActor *video_area, G_GNUC_UNUSED GParamSpec *pspec, VideoWidget *self) |
| 186 | { |
| 187 | g_return_if_fail(IS_VIDEO_WIDGET(self)); |
| 188 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 189 | |
| 190 | auto actor = priv->local->actor; |
| 191 | auto drag_action = priv->local->drag_action; |
| 192 | |
| 193 | ClutterActorBox actor_box; |
| 194 | clutter_actor_get_allocation_box(actor, &actor_box); |
| 195 | gfloat actor_w = clutter_actor_box_get_width(&actor_box); |
| 196 | gfloat actor_h = clutter_actor_box_get_height(&actor_box); |
| 197 | |
| 198 | ClutterActorBox area_box; |
| 199 | clutter_actor_get_allocation_box(video_area, &area_box); |
| 200 | gfloat area_w = clutter_actor_box_get_width(&area_box); |
| 201 | gfloat area_h = clutter_actor_box_get_height(&area_box); |
| 202 | |
| 203 | /* make sure drag area stays within the bounds of the stage */ |
| 204 | ClutterRect *rect = clutter_rect_init ( |
| 205 | clutter_rect_alloc(), |
| 206 | 0, 0, |
| 207 | area_w - actor_w, |
| 208 | area_h - actor_h); |
| 209 | clutter_drag_action_set_drag_area(CLUTTER_DRAG_ACTION(drag_action), rect); |
| 210 | clutter_rect_free(rect); |
| 211 | } |
| 212 | |
| 213 | static void |
| 214 | on_drag_begin(G_GNUC_UNUSED ClutterDragAction *action, |
| 215 | ClutterActor *actor, |
| 216 | G_GNUC_UNUSED gfloat event_x, |
| 217 | G_GNUC_UNUSED gfloat event_y, |
| 218 | G_GNUC_UNUSED ClutterModifierType modifiers, |
| 219 | G_GNUC_UNUSED gpointer user_data) |
| 220 | { |
| 221 | /* clear the align constraint when starting to move the preview, otherwise |
| 222 | * it won't move; save and set its position, to what it was before the |
| 223 | * constraint was cleared, or else it might jump around */ |
| 224 | gfloat actor_x, actor_y; |
| 225 | clutter_actor_get_position(actor, &actor_x, &actor_y); |
| 226 | clutter_actor_clear_constraints(actor); |
| 227 | clutter_actor_set_position(actor, actor_x, actor_y); |
| 228 | } |
| 229 | |
| 230 | static void |
| 231 | on_drag_end(G_GNUC_UNUSED ClutterDragAction *action, |
| 232 | ClutterActor *actor, |
| 233 | G_GNUC_UNUSED gfloat event_x, |
| 234 | G_GNUC_UNUSED gfloat event_y, |
| 235 | G_GNUC_UNUSED ClutterModifierType modifiers, |
| 236 | ClutterActor *video_area) |
| 237 | { |
| 238 | ClutterActorBox area_box; |
| 239 | clutter_actor_get_allocation_box(video_area, &area_box); |
| 240 | gfloat area_w = clutter_actor_box_get_width(&area_box); |
| 241 | gfloat area_h = clutter_actor_box_get_height(&area_box); |
| 242 | |
| 243 | gfloat actor_x, actor_y; |
| 244 | clutter_actor_get_position(actor, &actor_x, &actor_y); |
| 245 | gfloat actor_w, actor_h; |
| 246 | clutter_actor_get_size(actor, &actor_w, &actor_h); |
| 247 | |
| 248 | area_w -= actor_w; |
| 249 | area_h -= actor_h; |
| 250 | |
| 251 | /* add new constraints to make sure the preview stays in about the same location |
| 252 | * relative to the rest of the video when resizing */ |
| 253 | ClutterConstraint *constraint_x = clutter_align_constraint_new(video_area, |
| 254 | CLUTTER_ALIGN_X_AXIS, actor_x/area_w); |
| 255 | clutter_actor_add_constraint(actor, constraint_x); |
| 256 | |
| 257 | ClutterConstraint *constraint_y = clutter_align_constraint_new(video_area, |
| 258 | CLUTTER_ALIGN_Y_AXIS, actor_y/area_h); |
| 259 | clutter_actor_add_constraint(actor, constraint_y); |
| 260 | } |
| 261 | |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * video_widget_init() |
| 265 | * |
| 266 | * This function init the video_widget. |
| 267 | * - init clutter |
| 268 | * - init all the widget members |
| 269 | */ |
| 270 | static void |
| 271 | video_widget_init(VideoWidget *self) |
| 272 | { |
| 273 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 274 | |
Stepan Salenikovich | 36ef394 | 2015-11-05 18:26:57 -0500 | [diff] [blame] | 275 | auto stage = gtk_clutter_embed_get_stage(GTK_CLUTTER_EMBED(self)); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 276 | |
| 277 | /* layout manager is used to arrange children in space, here we ask clutter |
| 278 | * to align children to fill the space when resizing the window */ |
Stepan Salenikovich | 36ef394 | 2015-11-05 18:26:57 -0500 | [diff] [blame] | 279 | clutter_actor_set_layout_manager(stage, |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 280 | clutter_bin_layout_new(CLUTTER_BIN_ALIGNMENT_FILL, CLUTTER_BIN_ALIGNMENT_FILL)); |
| 281 | |
| 282 | /* add a scene container where we can add and remove our actors */ |
| 283 | priv->video_container = clutter_actor_new(); |
| 284 | clutter_actor_set_background_color(priv->video_container, CLUTTER_COLOR_Black); |
Stepan Salenikovich | 36ef394 | 2015-11-05 18:26:57 -0500 | [diff] [blame] | 285 | clutter_actor_add_child(stage, priv->video_container); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 286 | |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 287 | /* init the remote and local structs */ |
| 288 | priv->remote = g_new0(VideoWidgetRenderer, 1); |
| 289 | priv->local = g_new0(VideoWidgetRenderer, 1); |
| 290 | |
| 291 | /* arrange remote actors */ |
| 292 | priv->remote->actor = clutter_actor_new(); |
| 293 | clutter_actor_insert_child_below(priv->video_container, priv->remote->actor, NULL); |
| 294 | /* the remote camera must always fill the container size */ |
| 295 | ClutterConstraint *constraint = clutter_bind_constraint_new(priv->video_container, |
| 296 | CLUTTER_BIND_SIZE, 0); |
| 297 | clutter_actor_add_constraint(priv->remote->actor, constraint); |
| 298 | |
| 299 | /* arrange local actor */ |
| 300 | priv->local->actor = clutter_actor_new(); |
| 301 | clutter_actor_insert_child_above(priv->video_container, priv->local->actor, NULL); |
| 302 | /* set size to square, but it will stay the aspect ratio when the image is rendered */ |
| 303 | clutter_actor_set_size(priv->local->actor, VIDEO_LOCAL_SIZE, VIDEO_LOCAL_SIZE); |
Stepan Salenikovich | 223b2fd | 2015-06-22 12:52:21 -0400 | [diff] [blame] | 304 | /* set position constraint to right cornder; |
| 305 | * this constraint will be removed once the user tries to move the position |
| 306 | * of the action */ |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 307 | constraint = clutter_align_constraint_new(priv->video_container, |
| 308 | CLUTTER_ALIGN_BOTH, 0.99); |
| 309 | clutter_actor_add_constraint(priv->local->actor, constraint); |
| 310 | clutter_actor_set_opacity(priv->local->actor, |
| 311 | VIDEO_LOCAL_OPACITY_DEFAULT); |
| 312 | |
Stepan Salenikovich | 223b2fd | 2015-06-22 12:52:21 -0400 | [diff] [blame] | 313 | /* add ability for actor to be moved */ |
| 314 | clutter_actor_set_reactive(priv->local->actor, TRUE); |
| 315 | priv->local->drag_action = clutter_drag_action_new(); |
| 316 | clutter_actor_add_action(priv->local->actor, priv->local->drag_action); |
| 317 | |
| 318 | g_signal_connect(priv->local->drag_action, "drag-begin", G_CALLBACK(on_drag_begin), NULL); |
Stepan Salenikovich | 36ef394 | 2015-11-05 18:26:57 -0500 | [diff] [blame] | 319 | g_signal_connect_after(priv->local->drag_action, "drag-end", G_CALLBACK(on_drag_end), stage); |
Stepan Salenikovich | 223b2fd | 2015-06-22 12:52:21 -0400 | [diff] [blame] | 320 | |
| 321 | /* make sure the actor stays within the bounds of the stage */ |
Stepan Salenikovich | 36ef394 | 2015-11-05 18:26:57 -0500 | [diff] [blame] | 322 | g_signal_connect(stage, "notify::allocation", G_CALLBACK(on_allocation_changed), self); |
Stepan Salenikovich | 223b2fd | 2015-06-22 12:52:21 -0400 | [diff] [blame] | 323 | |
Stepan Salenikovich | 8934e84 | 2015-04-20 15:16:13 -0400 | [diff] [blame] | 324 | /* Init the timeout source which will check the for new frames. |
| 325 | * The priority must be lower than GTK drawing events |
| 326 | * (G_PRIORITY_HIGH_IDLE + 20) so that this timeout source doesn't choke |
| 327 | * the main loop on slower machines. |
| 328 | */ |
| 329 | priv->frame_timeout_source = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, |
| 330 | FRAME_RATE_PERIOD, |
| 331 | (GSourceFunc)check_frame_queue, |
| 332 | self, |
| 333 | NULL); |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 334 | |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 335 | /* init new renderer queue */ |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 336 | priv->new_renderer_queue = g_async_queue_new_full((GDestroyNotify)free_video_widget_renderer); |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 337 | /* check new render every 30 ms (30ms is "fast enough"); |
| 338 | * we don't use an idle function so it doesn't consume cpu needlessly */ |
| 339 | priv->renderer_timeout_source= g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, |
| 340 | 30, |
| 341 | (GSourceFunc)check_renderer_queue, |
| 342 | self, |
| 343 | NULL); |
| 344 | |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 345 | /* handle button event */ |
| 346 | g_signal_connect(GTK_WIDGET(self), "button-press-event", G_CALLBACK(on_button_press_in_screen_event), NULL); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 347 | |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 348 | /* drag & drop files as video sources */ |
Stepan Salenikovich | cb6aa46 | 2015-03-21 15:13:37 -0400 | [diff] [blame] | 349 | gtk_drag_dest_set(GTK_WIDGET(self), GTK_DEST_DEFAULT_ALL, NULL, 0, (GdkDragAction)(GDK_ACTION_COPY | GDK_ACTION_PRIVATE)); |
| 350 | gtk_drag_dest_add_uri_targets(GTK_WIDGET(self)); |
| 351 | g_signal_connect(GTK_WIDGET(self), "drag-data-received", G_CALLBACK(on_drag_data_received), NULL); |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * on_drag_data_received() |
| 356 | * |
| 357 | * Handle dragged data in the video widget window. |
| 358 | * Dropping an image causes the client to switch the video input to that image. |
| 359 | */ |
| 360 | static void |
| 361 | on_drag_data_received(G_GNUC_UNUSED GtkWidget *self, |
| 362 | G_GNUC_UNUSED GdkDragContext *context, |
| 363 | G_GNUC_UNUSED gint x, |
| 364 | G_GNUC_UNUSED gint y, |
| 365 | GtkSelectionData *selection_data, |
| 366 | G_GNUC_UNUSED guint info, |
| 367 | G_GNUC_UNUSED guint32 time, |
| 368 | G_GNUC_UNUSED gpointer data) |
| 369 | { |
| 370 | gchar **uris = gtk_selection_data_get_uris(selection_data); |
| 371 | |
| 372 | /* only play the first selection */ |
| 373 | if (uris && *uris) |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 374 | Video::SourceModel::instance().setFile(QUrl(*uris)); |
Stepan Salenikovich | cb6aa46 | 2015-03-21 15:13:37 -0400 | [diff] [blame] | 375 | |
| 376 | g_strfreev(uris); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 377 | } |
| 378 | |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 379 | static void |
| 380 | switch_video_input(G_GNUC_UNUSED GtkWidget *widget, Video::Device *device) |
| 381 | { |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 382 | Video::SourceModel::instance().switchTo(device); |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 383 | } |
| 384 | |
Stepan Salenikovich | 8bc51e5 | 2015-03-21 20:17:29 -0400 | [diff] [blame] | 385 | static void |
Stepan Salenikovich | fb5ff0a | 2015-03-29 22:47:47 -0400 | [diff] [blame] | 386 | switch_video_input_screen(G_GNUC_UNUSED GtkWidget *item, G_GNUC_UNUSED gpointer user_data) |
Stepan Salenikovich | 8bc51e5 | 2015-03-21 20:17:29 -0400 | [diff] [blame] | 387 | { |
| 388 | unsigned x, y; |
| 389 | unsigned width, height; |
| 390 | |
| 391 | /* try to get the dispaly or default to 0 */ |
| 392 | QString display_env{getenv("DISPLAY")}; |
| 393 | int display = 0; |
| 394 | |
| 395 | if (!display_env.isEmpty()) { |
| 396 | auto list = display_env.split(":", QString::SkipEmptyParts); |
| 397 | /* should only be one display, so get the first one */ |
| 398 | if (list.size() > 0) { |
| 399 | display = list.at(0).toInt(); |
| 400 | g_debug("sharing screen from DISPLAY %d", display); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | x = y = width = height = 0; |
| 405 | |
| 406 | xrectsel(&x, &y, &width, &height); |
| 407 | |
| 408 | if (!width || !height) { |
| 409 | x = y = 0; |
| 410 | width = gdk_screen_width(); |
| 411 | height = gdk_screen_height(); |
| 412 | } |
| 413 | |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 414 | Video::SourceModel::instance().setDisplay(display, QRect(x,y,width,height)); |
Stepan Salenikovich | 8bc51e5 | 2015-03-21 20:17:29 -0400 | [diff] [blame] | 415 | } |
| 416 | |
Stepan Salenikovich | 763c25a | 2015-03-26 13:51:31 -0400 | [diff] [blame] | 417 | static void |
Stepan Salenikovich | fb5ff0a | 2015-03-29 22:47:47 -0400 | [diff] [blame] | 418 | switch_video_input_file(G_GNUC_UNUSED GtkWidget *item, GtkWidget *parent) |
Stepan Salenikovich | 763c25a | 2015-03-26 13:51:31 -0400 | [diff] [blame] | 419 | { |
| 420 | if (parent && GTK_IS_WIDGET(parent)) { |
| 421 | /* get parent window */ |
| 422 | parent = gtk_widget_get_toplevel(GTK_WIDGET(parent)); |
| 423 | } |
| 424 | |
| 425 | gchar *uri = NULL; |
| 426 | GtkWidget *dialog = gtk_file_chooser_dialog_new( |
| 427 | "Choose File", |
| 428 | GTK_WINDOW(parent), |
| 429 | GTK_FILE_CHOOSER_ACTION_OPEN, |
| 430 | "_Cancel", GTK_RESPONSE_CANCEL, |
| 431 | "_Open", GTK_RESPONSE_ACCEPT, |
| 432 | NULL); |
| 433 | |
| 434 | if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { |
| 435 | uri = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog)); |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 436 | Video::SourceModel::instance().setFile(QUrl(uri)); |
Stepan Salenikovich | 763c25a | 2015-03-26 13:51:31 -0400 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | gtk_widget_destroy(dialog); |
| 440 | |
Stepan Salenikovich | 763c25a | 2015-03-26 13:51:31 -0400 | [diff] [blame] | 441 | g_free(uri); |
| 442 | } |
| 443 | |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 444 | /* |
| 445 | * on_button_press_in_screen_event() |
| 446 | * |
| 447 | * Handle button event in the video screen. |
| 448 | */ |
| 449 | static gboolean |
Stepan Salenikovich | 763c25a | 2015-03-26 13:51:31 -0400 | [diff] [blame] | 450 | on_button_press_in_screen_event(GtkWidget *parent, |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 451 | GdkEventButton *event, |
| 452 | G_GNUC_UNUSED gpointer data) |
| 453 | { |
| 454 | /* check for right click */ |
| 455 | if (event->button != BUTTON_RIGHT_CLICK || event->type != GDK_BUTTON_PRESS) |
| 456 | return FALSE; |
| 457 | |
| 458 | /* create menu with available video sources */ |
| 459 | GtkWidget *menu = gtk_menu_new(); |
| 460 | |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 461 | auto active = Video::SourceModel::instance().activeIndex(); |
Stepan Salenikovich | cb1c295 | 2015-08-13 14:28:20 -0400 | [diff] [blame] | 462 | |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 463 | /* list available devices and check off the active device */ |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 464 | auto device_list = Video::DeviceModel::instance().devices(); |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 465 | |
| 466 | for( auto device: device_list) { |
| 467 | GtkWidget *item = gtk_check_menu_item_new_with_mnemonic(device->name().toLocal8Bit().constData()); |
| 468 | gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 469 | auto device_idx = Video::SourceModel::instance().getDeviceIndex(device); |
Stepan Salenikovich | cb1c295 | 2015-08-13 14:28:20 -0400 | [diff] [blame] | 470 | gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), device_idx == active); |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 471 | g_signal_connect(item, "activate", G_CALLBACK(switch_video_input), device); |
| 472 | } |
| 473 | |
Stepan Salenikovich | bb38263 | 2015-03-26 12:40:46 -0400 | [diff] [blame] | 474 | /* add separator */ |
| 475 | gtk_menu_shell_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new()); |
| 476 | |
Stepan Salenikovich | 8bc51e5 | 2015-03-21 20:17:29 -0400 | [diff] [blame] | 477 | /* add screen area as an input */ |
Julien Grossholtz | 58cfc15 | 2015-10-22 15:43:43 -0400 | [diff] [blame] | 478 | GtkWidget *item = gtk_check_menu_item_new_with_mnemonic(_("Share screen area")); |
Stepan Salenikovich | 8bc51e5 | 2015-03-21 20:17:29 -0400 | [diff] [blame] | 479 | gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
Stepan Salenikovich | cb1c295 | 2015-08-13 14:28:20 -0400 | [diff] [blame] | 480 | gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), Video::SourceModel::ExtendedDeviceList::SCREEN == active); |
Stepan Salenikovich | 8bc51e5 | 2015-03-21 20:17:29 -0400 | [diff] [blame] | 481 | g_signal_connect(item, "activate", G_CALLBACK(switch_video_input_screen), NULL); |
| 482 | |
Stepan Salenikovich | 763c25a | 2015-03-26 13:51:31 -0400 | [diff] [blame] | 483 | /* add file as an input */ |
Julien Grossholtz | 58cfc15 | 2015-10-22 15:43:43 -0400 | [diff] [blame] | 484 | item = gtk_check_menu_item_new_with_mnemonic(_("Share file")); |
Stepan Salenikovich | 763c25a | 2015-03-26 13:51:31 -0400 | [diff] [blame] | 485 | gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
Stepan Salenikovich | cb1c295 | 2015-08-13 14:28:20 -0400 | [diff] [blame] | 486 | gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), Video::SourceModel::ExtendedDeviceList::FILE == active); |
Stepan Salenikovich | 763c25a | 2015-03-26 13:51:31 -0400 | [diff] [blame] | 487 | g_signal_connect(item, "activate", G_CALLBACK(switch_video_input_file), parent); |
| 488 | |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 489 | /* show menu */ |
| 490 | gtk_widget_show_all(menu); |
| 491 | gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, event->button, event->time); |
| 492 | |
| 493 | return TRUE; /* event has been fully handled */ |
| 494 | } |
| 495 | |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 496 | static void |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 497 | clutter_render_image(VideoWidgetRenderer* wg_renderer) |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 498 | { |
Stepan Salenikovich | bf118ac | 2015-04-22 14:06:50 -0400 | [diff] [blame] | 499 | auto actor = wg_renderer->actor; |
| 500 | g_return_if_fail(CLUTTER_IS_ACTOR(actor)); |
| 501 | |
Stepan Salenikovich | b94873c | 2015-06-02 16:53:18 -0400 | [diff] [blame] | 502 | if (wg_renderer->pause_rendering) |
| 503 | return; |
| 504 | |
Stepan Salenikovich | bf118ac | 2015-04-22 14:06:50 -0400 | [diff] [blame] | 505 | if (wg_renderer->show_black_frame) { |
| 506 | /* render a black frame set the bool back to false, this is likely done |
| 507 | * when the renderer is stopped so we ignore whether or not it is running |
| 508 | */ |
Stepan Salenikovich | 6e7b092 | 2015-05-05 17:51:30 -0400 | [diff] [blame] | 509 | if (auto image_old = clutter_actor_get_content(actor)) { |
| 510 | gfloat width; |
| 511 | gfloat height; |
| 512 | if (clutter_content_get_preferred_size(image_old, &width, &height)) { |
| 513 | /* NOTE: this is a workaround for #72531, a crash which occurs |
| 514 | * in cogl < 1.18. We allocate a black frame of the same size |
| 515 | * as the previous image, instead of simply setting an empty or |
| 516 | * a NULL ClutterImage. |
| 517 | */ |
| 518 | auto image_empty = clutter_image_new(); |
| 519 | if (auto empty_data = (guint8 *)g_try_malloc0((gsize)width * height * 4)) { |
| 520 | GError* error = NULL; |
| 521 | clutter_image_set_data( |
| 522 | CLUTTER_IMAGE(image_empty), |
| 523 | empty_data, |
| 524 | COGL_PIXEL_FORMAT_BGRA_8888, |
| 525 | (guint)width, |
| 526 | (guint)height, |
| 527 | (guint)width*4, |
| 528 | &error); |
| 529 | if (error) { |
| 530 | g_warning("error rendering empty image to clutter: %s", error->message); |
| 531 | g_clear_error(&error); |
| 532 | g_object_unref(image_empty); |
| 533 | return; |
| 534 | } |
| 535 | clutter_actor_set_content(actor, image_empty); |
| 536 | g_object_unref(image_empty); |
| 537 | g_free(empty_data); |
| 538 | } else { |
| 539 | clutter_actor_set_content(actor, NULL); |
| 540 | } |
| 541 | } else { |
| 542 | clutter_actor_set_content(actor, NULL); |
| 543 | } |
| 544 | } |
Stepan Salenikovich | bf118ac | 2015-04-22 14:06:50 -0400 | [diff] [blame] | 545 | wg_renderer->show_black_frame = false; |
| 546 | return; |
| 547 | } |
| 548 | |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame] | 549 | ClutterContent *image_new = nullptr; |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 550 | |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame] | 551 | { |
| 552 | /* the following must be done under lock in case a 'stopped' signal is |
| 553 | * received during rendering; otherwise the mem could become invalid */ |
| 554 | std::lock_guard<std::mutex> lock(wg_renderer->run_mutex); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 555 | |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame] | 556 | if (!wg_renderer->running) |
| 557 | return; |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 558 | |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame] | 559 | auto renderer = wg_renderer->renderer; |
| 560 | if (renderer == nullptr) |
| 561 | return; |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 562 | |
Guillaume Roguez | a8860ea | 2015-10-13 18:04:39 -0400 | [diff] [blame] | 563 | auto frame_ptr = renderer->currentFrame(); |
| 564 | auto frame_data = frame_ptr.ptr; |
Stepan Salenikovich | d6a4ef3 | 2015-11-12 10:59:02 -0500 | [diff] [blame^] | 565 | if (!frame_data) |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame] | 566 | return; |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 567 | |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame] | 568 | image_new = clutter_image_new(); |
| 569 | g_return_if_fail(image_new); |
| 570 | |
| 571 | const auto& res = renderer->size(); |
| 572 | const gint BPP = 4; |
| 573 | const gint ROW_STRIDE = BPP * res.width(); |
| 574 | |
| 575 | GError *error = nullptr; |
| 576 | clutter_image_set_data( |
| 577 | CLUTTER_IMAGE(image_new), |
Guillaume Roguez | a8860ea | 2015-10-13 18:04:39 -0400 | [diff] [blame] | 578 | frame_data, |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame] | 579 | COGL_PIXEL_FORMAT_BGRA_8888, |
| 580 | res.width(), |
| 581 | res.height(), |
| 582 | ROW_STRIDE, |
| 583 | &error); |
| 584 | if (error) { |
| 585 | g_warning("error rendering image to clutter: %s", error->message); |
Stepan Salenikovich | 8a287fc | 2015-05-01 16:53:20 -0400 | [diff] [blame] | 586 | g_clear_error(&error); |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame] | 587 | g_object_unref (image_new); |
| 588 | return; |
| 589 | } |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 590 | } |
| 591 | |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 592 | clutter_actor_set_content(actor, image_new); |
Stepan Salenikovich | cd1bf63 | 2015-03-09 16:24:08 -0400 | [diff] [blame] | 593 | g_object_unref (image_new); |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 594 | |
Stepan Salenikovich | cd1bf63 | 2015-03-09 16:24:08 -0400 | [diff] [blame] | 595 | /* note: we must set the content gravity be "resize aspect" after setting the image data to make sure |
| 596 | * that the aspect ratio is correct |
| 597 | */ |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 598 | clutter_actor_set_content_gravity(actor, CLUTTER_CONTENT_GRAVITY_RESIZE_ASPECT); |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 599 | } |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 600 | |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 601 | static gboolean |
| 602 | check_frame_queue(VideoWidget *self) |
| 603 | { |
| 604 | g_return_val_if_fail(IS_VIDEO_WIDGET(self), FALSE); |
| 605 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 606 | |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 607 | /* display renderer's frames */ |
| 608 | clutter_render_image(priv->local); |
| 609 | clutter_render_image(priv->remote); |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 610 | |
| 611 | return TRUE; /* keep going */ |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 612 | } |
| 613 | |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 614 | static void |
| 615 | renderer_stop(VideoWidgetRenderer *renderer) |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 616 | { |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame] | 617 | { |
| 618 | /* must do this under lock, in case the rendering is taking place when |
| 619 | * this signal is received */ |
| 620 | std::lock_guard<std::mutex> lock(renderer->run_mutex); |
| 621 | renderer->running = false; |
| 622 | } |
Stepan Salenikovich | bf118ac | 2015-04-22 14:06:50 -0400 | [diff] [blame] | 623 | /* ask to show a black frame */ |
| 624 | renderer->show_black_frame = true; |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 625 | } |
| 626 | |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 627 | static void |
| 628 | renderer_start(VideoWidgetRenderer *renderer) |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 629 | { |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame] | 630 | { |
| 631 | std::lock_guard<std::mutex> lock(renderer->run_mutex); |
| 632 | renderer->running = true; |
| 633 | } |
Stepan Salenikovich | d6a4ef3 | 2015-11-12 10:59:02 -0500 | [diff] [blame^] | 634 | renderer->show_black_frame = false; |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 635 | } |
| 636 | |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 637 | static void |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 638 | free_video_widget_renderer(VideoWidgetRenderer *renderer) |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 639 | { |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 640 | QObject::disconnect(renderer->render_stop); |
| 641 | QObject::disconnect(renderer->render_start); |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 642 | g_free(renderer); |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 643 | } |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 644 | |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 645 | static void |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 646 | video_widget_add_renderer(VideoWidget *self, VideoWidgetRenderer *new_video_renderer) |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 647 | { |
| 648 | g_return_if_fail(IS_VIDEO_WIDGET(self)); |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 649 | g_return_if_fail(new_video_renderer); |
| 650 | g_return_if_fail(new_video_renderer->renderer); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 651 | |
| 652 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 653 | |
| 654 | /* update the renderer */ |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 655 | switch(new_video_renderer->type) { |
Stepan Salenikovich | c5f0815 | 2015-03-19 00:53:23 -0400 | [diff] [blame] | 656 | case VIDEO_RENDERER_REMOTE: |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 657 | /* swap the remote renderer */ |
| 658 | new_video_renderer->actor = priv->remote->actor; |
| 659 | free_video_widget_renderer(priv->remote); |
| 660 | priv->remote = new_video_renderer; |
| 661 | /* reset the content gravity so that the aspect ratio gets properly |
| 662 | * reset if it chagnes */ |
| 663 | clutter_actor_set_content_gravity(priv->remote->actor, |
| 664 | CLUTTER_CONTENT_GRAVITY_RESIZE_FILL); |
Stepan Salenikovich | c5f0815 | 2015-03-19 00:53:23 -0400 | [diff] [blame] | 665 | break; |
| 666 | case VIDEO_RENDERER_LOCAL: |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 667 | /* swap the remote renderer */ |
| 668 | new_video_renderer->actor = priv->local->actor; |
Stepan Salenikovich | 223b2fd | 2015-06-22 12:52:21 -0400 | [diff] [blame] | 669 | new_video_renderer->drag_action = priv->local->drag_action; |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 670 | free_video_widget_renderer(priv->local); |
| 671 | priv->local = new_video_renderer; |
| 672 | /* reset the content gravity so that the aspect ratio gets properly |
| 673 | * reset if it chagnes */ |
| 674 | clutter_actor_set_content_gravity(priv->local->actor, |
| 675 | CLUTTER_CONTENT_GRAVITY_RESIZE_FILL); |
Stepan Salenikovich | c5f0815 | 2015-03-19 00:53:23 -0400 | [diff] [blame] | 676 | break; |
| 677 | case VIDEO_RENDERER_COUNT: |
| 678 | break; |
| 679 | } |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 680 | } |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 681 | |
| 682 | static gboolean |
| 683 | check_renderer_queue(VideoWidget *self) |
| 684 | { |
| 685 | g_return_val_if_fail(IS_VIDEO_WIDGET(self), G_SOURCE_REMOVE); |
| 686 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 687 | |
| 688 | /* get all the renderers in the queue */ |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 689 | VideoWidgetRenderer *new_video_renderer = (VideoWidgetRenderer *)g_async_queue_try_pop(priv->new_renderer_queue); |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 690 | while (new_video_renderer) { |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 691 | video_widget_add_renderer(self, new_video_renderer); |
| 692 | new_video_renderer = (VideoWidgetRenderer *)g_async_queue_try_pop(priv->new_renderer_queue); |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | return G_SOURCE_CONTINUE; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * video_widget_new() |
| 700 | * |
| 701 | * The function use to create a new video_widget |
| 702 | */ |
| 703 | GtkWidget* |
| 704 | video_widget_new(void) |
| 705 | { |
| 706 | GtkWidget *self = (GtkWidget *)g_object_new(VIDEO_WIDGET_TYPE, NULL); |
| 707 | return self; |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * video_widget_push_new_renderer() |
| 712 | * |
| 713 | * This function is used add a new Video::Renderer to the VideoWidget in a |
| 714 | * thread-safe manner. |
| 715 | */ |
| 716 | void |
| 717 | video_widget_push_new_renderer(VideoWidget *self, Video::Renderer *renderer, VideoRendererType type) |
| 718 | { |
| 719 | g_return_if_fail(IS_VIDEO_WIDGET(self)); |
| 720 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 721 | |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 722 | /* if the renderer is nullptr, there is nothing to be done */ |
| 723 | if (!renderer) return; |
| 724 | |
| 725 | VideoWidgetRenderer *new_video_renderer = g_new0(VideoWidgetRenderer, 1); |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 726 | new_video_renderer->renderer = renderer; |
| 727 | new_video_renderer->type = type; |
| 728 | |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 729 | if (new_video_renderer->renderer->isRendering()) |
| 730 | renderer_start(new_video_renderer); |
| 731 | |
| 732 | new_video_renderer->render_stop = QObject::connect( |
| 733 | new_video_renderer->renderer, |
| 734 | &Video::Renderer::stopped, |
| 735 | [=]() { |
| 736 | renderer_stop(new_video_renderer); |
| 737 | } |
| 738 | ); |
| 739 | |
| 740 | new_video_renderer->render_start = QObject::connect( |
| 741 | new_video_renderer->renderer, |
| 742 | &Video::Renderer::started, |
| 743 | [=]() { |
| 744 | renderer_start(new_video_renderer); |
| 745 | } |
| 746 | ); |
| 747 | |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 748 | g_async_queue_push(priv->new_renderer_queue, new_video_renderer); |
| 749 | } |
Stepan Salenikovich | b94873c | 2015-06-02 16:53:18 -0400 | [diff] [blame] | 750 | |
| 751 | void |
| 752 | video_widget_pause_rendering(VideoWidget *self, gboolean pause) |
| 753 | { |
| 754 | g_return_if_fail(IS_VIDEO_WIDGET(self)); |
| 755 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 756 | |
| 757 | priv->local->pause_rendering = pause; |
| 758 | priv->remote->pause_rendering = pause; |
| 759 | } |