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