blob: 6b08bab62fee0c257fdb74c935b1b70180164ebb [file] [log] [blame]
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001/*
2 * Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05003 * 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 Grossholtz58cfc152015-10-22 15:43:43 -040033#include <glib/gi18n.h>
Stepan Salenikovich36c025c2015-03-03 19:06:44 -050034#include <clutter/clutter.h>
35#include <clutter-gtk/clutter-gtk.h>
36#include <video/renderer.h>
Stepan Salenikovich033dc832015-03-23 15:56:47 -040037#include <video/sourcemodel.h>
Stepan Salenikovich50c989b2015-03-21 18:32:46 -040038#include <video/devicemodel.h>
39#include <QtCore/QUrl>
40#include "../defines.h"
Stepan Salenikovich8bc51e52015-03-21 20:17:29 -040041#include <stdlib.h>
Guillaume Roguez24834e02015-04-09 12:45:40 -040042#include <atomic>
Stepan Salenikovichaea6c042015-04-29 15:09:16 -040043#include <mutex>
Stepan Salenikovich8bc51e52015-03-21 20:17:29 -040044#include "xrectsel.h"
Stepan Salenikovich36c025c2015-03-03 19:06:44 -050045
Stepan Salenikovich9ffad5e2015-09-25 13:16:50 -040046static constexpr int VIDEO_LOCAL_SIZE = 150;
47static constexpr int VIDEO_LOCAL_OPACITY_DEFAULT = 255; /* out of 255 */
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -040048
Stepan Salenikovich5e6a0b72015-03-12 14:55:22 -040049/* 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 Salenikovich9ffad5e2015-09-25 13:16:50 -040052static constexpr int FRAME_RATE_PERIOD = 30;
Stepan Salenikovich5e6a0b72015-03-12 14:55:22 -040053
Stepan Salenikovich36c025c2015-03-03 19:06:44 -050054struct _VideoWidgetClass {
55 GtkBinClass parent_class;
56};
57
58struct _VideoWidget {
59 GtkBin parent;
60};
61
62typedef struct _VideoWidgetPrivate VideoWidgetPrivate;
63
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -040064typedef struct _VideoWidgetRenderer VideoWidgetRenderer;
65
Stepan Salenikovich36c025c2015-03-03 19:06:44 -050066struct _VideoWidgetPrivate {
67 GtkWidget *clutter_widget;
68 ClutterActor *stage;
69 ClutterActor *video_container;
70
71 /* remote peer data */
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -040072 VideoWidgetRenderer *remote;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -050073
74 /* local peer data */
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -040075 VideoWidgetRenderer *local;
Stepan Salenikovich5e6a0b72015-03-12 14:55:22 -040076
77 guint frame_timeout_source;
Stepan Salenikovich0f693232015-04-22 10:45:08 -040078
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 Salenikovich4ac89f12015-03-10 16:48:47 -040086};
87
88struct _VideoWidgetRenderer {
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -040089 VideoRendererType type;
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -040090 ClutterActor *actor;
Stepan Salenikovich223b2fd2015-06-22 12:52:21 -040091 ClutterAction *drag_action;
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -040092 Video::Renderer *renderer;
Stepan Salenikovichaea6c042015-04-29 15:09:16 -040093 std::mutex run_mutex;
94 bool running;
Guillaume Roguez24834e02015-04-09 12:45:40 -040095 std::atomic_bool dirty;
Stepan Salenikovichbf118ac2015-04-22 14:06:50 -040096
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 Salenikovichb94873c2015-06-02 16:53:18 -0400103 std::atomic_bool pause_rendering;
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400104 QMetaObject::Connection frame_update;
105 QMetaObject::Connection render_stop;
106 QMetaObject::Connection render_start;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500107};
108
109G_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 Salenikovich5e6a0b72015-03-12 14:55:22 -0400113/* static prototypes */
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -0400114static gboolean check_frame_queue (VideoWidget *);
115static void renderer_stop (VideoWidgetRenderer *);
116static void renderer_start (VideoWidgetRenderer *);
117static void on_drag_data_received (GtkWidget *, GdkDragContext *, gint, gint, GtkSelectionData *, guint, guint32, gpointer);
Stepan Salenikovich50c989b2015-03-21 18:32:46 -0400118static gboolean on_button_press_in_screen_event(GtkWidget *, GdkEventButton *, gpointer);
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -0400119static gboolean check_renderer_queue (VideoWidget *);
120static void free_video_widget_renderer (VideoWidgetRenderer *);
121static void video_widget_add_renderer (VideoWidget *, VideoWidgetRenderer *);
Stepan Salenikovich5e6a0b72015-03-12 14:55:22 -0400122
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500123/*
124 * video_widget_dispose()
125 *
126 * The dispose function for the video_widget class.
127 */
128static void
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400129video_widget_dispose(GObject *object)
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500130{
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400131 VideoWidget *self = VIDEO_WIDGET(object);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500132 VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self);
133
Stepan Salenikovichbf74af82015-03-13 11:35:58 -0400134 /* 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 Salenikovich5e6a0b72015-03-12 14:55:22 -0400140
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400141 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 Salenikovich4ac89f12015-03-10 16:48:47 -0400151 G_OBJECT_CLASS(video_widget_parent_class)->dispose(object);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500152}
153
154
155/*
156 * video_widget_finalize()
157 *
158 * The finalize function for the video_widget class.
159 */
160static void
161video_widget_finalize(GObject *object)
162{
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400163 VideoWidget *self = VIDEO_WIDGET(object);
164 VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self);
165
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -0400166 free_video_widget_renderer(priv->local);
167 free_video_widget_renderer(priv->remote);
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400168
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500169 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 */
178static void
179video_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 Salenikovich223b2fd2015-06-22 12:52:21 -0400188static void
189on_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
217static void
218on_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
234static void
235on_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 Salenikovich36c025c2015-03-03 19:06:44 -0500266
267/*
268 * video_widget_init()
269 *
270 * This function init the video_widget.
271 * - init clutter
272 * - init all the widget members
273 */
274static void
275video_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 Salenikovich4ac89f12015-03-10 16:48:47 -0400296 /* 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 Salenikovich223b2fd2015-06-22 12:52:21 -0400313 /* 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 Salenikovich4ac89f12015-03-10 16:48:47 -0400316 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 Salenikovich223b2fd2015-06-22 12:52:21 -0400322 /* 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 Salenikovich8934e842015-04-20 15:16:13 -0400333 /* 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 Salenikovich5e6a0b72015-03-12 14:55:22 -0400343
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400344 /* init new renderer queue */
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -0400345 priv->new_renderer_queue = g_async_queue_new_full((GDestroyNotify)free_video_widget_renderer);
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400346 /* 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 Salenikovich50c989b2015-03-21 18:32:46 -0400354 /* handle button event */
355 g_signal_connect(GTK_WIDGET(self), "button-press-event", G_CALLBACK(on_button_press_in_screen_event), NULL);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500356
Stepan Salenikovich50c989b2015-03-21 18:32:46 -0400357 /* drag & drop files as video sources */
Stepan Salenikovichcb6aa462015-03-21 15:13:37 -0400358 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 */
369static void
370on_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 Roguez5d1514b2015-10-22 15:55:31 -0400383 Video::SourceModel::instance().setFile(QUrl(*uris));
Stepan Salenikovichcb6aa462015-03-21 15:13:37 -0400384
385 g_strfreev(uris);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500386}
387
Stepan Salenikovich50c989b2015-03-21 18:32:46 -0400388static void
389switch_video_input(G_GNUC_UNUSED GtkWidget *widget, Video::Device *device)
390{
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400391 Video::SourceModel::instance().switchTo(device);
Stepan Salenikovich50c989b2015-03-21 18:32:46 -0400392}
393
Stepan Salenikovich8bc51e52015-03-21 20:17:29 -0400394static void
Stepan Salenikovichfb5ff0a2015-03-29 22:47:47 -0400395switch_video_input_screen(G_GNUC_UNUSED GtkWidget *item, G_GNUC_UNUSED gpointer user_data)
Stepan Salenikovich8bc51e52015-03-21 20:17:29 -0400396{
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 Roguez5d1514b2015-10-22 15:55:31 -0400423 Video::SourceModel::instance().setDisplay(display, QRect(x,y,width,height));
Stepan Salenikovich8bc51e52015-03-21 20:17:29 -0400424}
425
Stepan Salenikovich763c25a2015-03-26 13:51:31 -0400426static void
Stepan Salenikovichfb5ff0a2015-03-29 22:47:47 -0400427switch_video_input_file(G_GNUC_UNUSED GtkWidget *item, GtkWidget *parent)
Stepan Salenikovich763c25a2015-03-26 13:51:31 -0400428{
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 Roguez5d1514b2015-10-22 15:55:31 -0400445 Video::SourceModel::instance().setFile(QUrl(uri));
Stepan Salenikovich763c25a2015-03-26 13:51:31 -0400446 }
447
448 gtk_widget_destroy(dialog);
449
Stepan Salenikovich763c25a2015-03-26 13:51:31 -0400450 g_free(uri);
451}
452
Stepan Salenikovich50c989b2015-03-21 18:32:46 -0400453/*
454 * on_button_press_in_screen_event()
455 *
456 * Handle button event in the video screen.
457 */
458static gboolean
Stepan Salenikovich763c25a2015-03-26 13:51:31 -0400459on_button_press_in_screen_event(GtkWidget *parent,
Stepan Salenikovich50c989b2015-03-21 18:32:46 -0400460 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 Roguez5d1514b2015-10-22 15:55:31 -0400470 auto active = Video::SourceModel::instance().activeIndex();
Stepan Salenikovichcb1c2952015-08-13 14:28:20 -0400471
Stepan Salenikovich50c989b2015-03-21 18:32:46 -0400472 /* list available devices and check off the active device */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400473 auto device_list = Video::DeviceModel::instance().devices();
Stepan Salenikovich50c989b2015-03-21 18:32:46 -0400474
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 Roguez5d1514b2015-10-22 15:55:31 -0400478 auto device_idx = Video::SourceModel::instance().getDeviceIndex(device);
Stepan Salenikovichcb1c2952015-08-13 14:28:20 -0400479 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), device_idx == active);
Stepan Salenikovich50c989b2015-03-21 18:32:46 -0400480 g_signal_connect(item, "activate", G_CALLBACK(switch_video_input), device);
481 }
482
Stepan Salenikovichbb382632015-03-26 12:40:46 -0400483 /* add separator */
484 gtk_menu_shell_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
485
Stepan Salenikovich8bc51e52015-03-21 20:17:29 -0400486 /* add screen area as an input */
Julien Grossholtz58cfc152015-10-22 15:43:43 -0400487 GtkWidget *item = gtk_check_menu_item_new_with_mnemonic(_("Share screen area"));
Stepan Salenikovich8bc51e52015-03-21 20:17:29 -0400488 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
Stepan Salenikovichcb1c2952015-08-13 14:28:20 -0400489 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), Video::SourceModel::ExtendedDeviceList::SCREEN == active);
Stepan Salenikovich8bc51e52015-03-21 20:17:29 -0400490 g_signal_connect(item, "activate", G_CALLBACK(switch_video_input_screen), NULL);
491
Stepan Salenikovich763c25a2015-03-26 13:51:31 -0400492 /* add file as an input */
Julien Grossholtz58cfc152015-10-22 15:43:43 -0400493 item = gtk_check_menu_item_new_with_mnemonic(_("Share file"));
Stepan Salenikovich763c25a2015-03-26 13:51:31 -0400494 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
Stepan Salenikovichcb1c2952015-08-13 14:28:20 -0400495 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), Video::SourceModel::ExtendedDeviceList::FILE == active);
Stepan Salenikovich763c25a2015-03-26 13:51:31 -0400496 g_signal_connect(item, "activate", G_CALLBACK(switch_video_input_file), parent);
497
Stepan Salenikovich50c989b2015-03-21 18:32:46 -0400498 /* 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 Salenikovich36c025c2015-03-03 19:06:44 -0500505static void
Guillaume Roguez24834e02015-04-09 12:45:40 -0400506clutter_render_image(VideoWidgetRenderer* wg_renderer)
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500507{
Stepan Salenikovichbf118ac2015-04-22 14:06:50 -0400508 auto actor = wg_renderer->actor;
509 g_return_if_fail(CLUTTER_IS_ACTOR(actor));
510
Stepan Salenikovichb94873c2015-06-02 16:53:18 -0400511 if (wg_renderer->pause_rendering)
512 return;
513
Stepan Salenikovichbf118ac2015-04-22 14:06:50 -0400514 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 Salenikovich6e7b0922015-05-05 17:51:30 -0400518 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 Salenikovichbf118ac2015-04-22 14:06:50 -0400554 wg_renderer->show_black_frame = false;
555 return;
556 }
557
Stepan Salenikovichaea6c042015-04-29 15:09:16 -0400558 ClutterContent *image_new = nullptr;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500559
Stepan Salenikovichaea6c042015-04-29 15:09:16 -0400560 {
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 Salenikovich36c025c2015-03-03 19:06:44 -0500564
Stepan Salenikovichaea6c042015-04-29 15:09:16 -0400565 if (!wg_renderer->running)
566 return;
Guillaume Roguez24834e02015-04-09 12:45:40 -0400567
Stepan Salenikovichaea6c042015-04-29 15:09:16 -0400568 auto renderer = wg_renderer->renderer;
569 if (renderer == nullptr)
570 return;
Guillaume Roguez24834e02015-04-09 12:45:40 -0400571
Guillaume Rogueza8860ea2015-10-13 18:04:39 -0400572 auto frame_ptr = renderer->currentFrame();
573 auto frame_data = frame_ptr.ptr;
574 if (!frame_data or !wg_renderer->dirty)
Stepan Salenikovichaea6c042015-04-29 15:09:16 -0400575 return;
Guillaume Roguez24834e02015-04-09 12:45:40 -0400576
Stepan Salenikovichaea6c042015-04-29 15:09:16 -0400577 wg_renderer->dirty = false;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500578
Stepan Salenikovichaea6c042015-04-29 15:09:16 -0400579 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 Rogueza8860ea2015-10-13 18:04:39 -0400589 frame_data,
Stepan Salenikovichaea6c042015-04-29 15:09:16 -0400590 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 Salenikovich8a287fc2015-05-01 16:53:20 -0400597 g_clear_error(&error);
Stepan Salenikovichaea6c042015-04-29 15:09:16 -0400598 g_object_unref (image_new);
599 return;
600 }
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500601 }
602
Guillaume Roguez24834e02015-04-09 12:45:40 -0400603 clutter_actor_set_content(actor, image_new);
Stepan Salenikovichcd1bf632015-03-09 16:24:08 -0400604 g_object_unref (image_new);
Guillaume Roguez24834e02015-04-09 12:45:40 -0400605
Stepan Salenikovichcd1bf632015-03-09 16:24:08 -0400606 /* 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 Roguez24834e02015-04-09 12:45:40 -0400609 clutter_actor_set_content_gravity(actor, CLUTTER_CONTENT_GRAVITY_RESIZE_ASPECT);
Stepan Salenikovich5e6a0b72015-03-12 14:55:22 -0400610}
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500611
Stepan Salenikovich5e6a0b72015-03-12 14:55:22 -0400612static gboolean
613check_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 Roguez24834e02015-04-09 12:45:40 -0400618 /* display renderer's frames */
619 clutter_render_image(priv->local);
620 clutter_render_image(priv->remote);
Stepan Salenikovich5e6a0b72015-03-12 14:55:22 -0400621
622 return TRUE; /* keep going */
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500623}
624
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400625static void
626renderer_stop(VideoWidgetRenderer *renderer)
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500627{
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400628 QObject::disconnect(renderer->frame_update);
Stepan Salenikovichaea6c042015-04-29 15:09:16 -0400629 {
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 Salenikovichbf118ac2015-04-22 14:06:50 -0400635 /* ask to show a black frame */
636 renderer->show_black_frame = true;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500637}
638
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400639static void
640renderer_start(VideoWidgetRenderer *renderer)
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500641{
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400642 QObject::disconnect(renderer->frame_update);
Stepan Salenikovichaea6c042015-04-29 15:09:16 -0400643 {
644 std::lock_guard<std::mutex> lock(renderer->run_mutex);
645 renderer->running = true;
646 }
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400647 renderer->frame_update = QObject::connect(
648 renderer->renderer,
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500649 &Video::Renderer::frameUpdated,
Guillaume Roguez24834e02015-04-09 12:45:40 -0400650 [renderer]() {
651 // WARNING: this lambda is called in LRC renderer thread,
652 // but check_frame_queue() is in mainloop!
Stepan Salenikovichbf118ac2015-04-22 14:06:50 -0400653
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 Roguez24834e02015-04-09 12:45:40 -0400657 renderer->dirty = true;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500658 }
Guillaume Roguez24834e02015-04-09 12:45:40 -0400659 );
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500660}
661
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400662static void
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -0400663free_video_widget_renderer(VideoWidgetRenderer *renderer)
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400664{
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400665 QObject::disconnect(renderer->frame_update);
666 QObject::disconnect(renderer->render_stop);
667 QObject::disconnect(renderer->render_start);
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -0400668 g_free(renderer);
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400669}
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500670
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400671static void
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -0400672video_widget_add_renderer(VideoWidget *self, VideoWidgetRenderer *new_video_renderer)
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500673{
674 g_return_if_fail(IS_VIDEO_WIDGET(self));
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -0400675 g_return_if_fail(new_video_renderer);
676 g_return_if_fail(new_video_renderer->renderer);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500677
678 VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self);
679
680 /* update the renderer */
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -0400681 switch(new_video_renderer->type) {
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400682 case VIDEO_RENDERER_REMOTE:
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -0400683 /* 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 Salenikovichc5f08152015-03-19 00:53:23 -0400691 break;
692 case VIDEO_RENDERER_LOCAL:
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -0400693 /* swap the remote renderer */
694 new_video_renderer->actor = priv->local->actor;
Stepan Salenikovich223b2fd2015-06-22 12:52:21 -0400695 new_video_renderer->drag_action = priv->local->drag_action;
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -0400696 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 Salenikovichc5f08152015-03-19 00:53:23 -0400702 break;
703 case VIDEO_RENDERER_COUNT:
704 break;
705 }
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500706}
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400707
708static gboolean
709check_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 Salenikovich3bcb0b22015-04-21 18:44:55 -0400715 VideoWidgetRenderer *new_video_renderer = (VideoWidgetRenderer *)g_async_queue_try_pop(priv->new_renderer_queue);
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400716 while (new_video_renderer) {
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -0400717 video_widget_add_renderer(self, new_video_renderer);
718 new_video_renderer = (VideoWidgetRenderer *)g_async_queue_try_pop(priv->new_renderer_queue);
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400719 }
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 */
729GtkWidget*
730video_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 */
742void
743video_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 Salenikovich3bcb0b22015-04-21 18:44:55 -0400748 /* 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 Salenikovich0f693232015-04-22 10:45:08 -0400752 new_video_renderer->renderer = renderer;
753 new_video_renderer->type = type;
754
Stepan Salenikovich3bcb0b22015-04-21 18:44:55 -0400755 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 Salenikovich0f693232015-04-22 10:45:08 -0400774 g_async_queue_push(priv->new_renderer_queue, new_video_renderer);
775}
Stepan Salenikovichb94873c2015-06-02 16:53:18 -0400776
777void
778video_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}