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