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 |
| 46 | #define VIDEO_LOCAL_OPACITY_DEFAULT 150 /* out of 255 */ |
| 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; |
| 90 | Video::Renderer *renderer; |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame^] | 91 | std::mutex run_mutex; |
| 92 | bool running; |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 93 | std::atomic_bool dirty; |
Stepan Salenikovich | bf118ac | 2015-04-22 14:06:50 -0400 | [diff] [blame] | 94 | |
| 95 | /* show_black_frame is used to request the actor to render a black image; |
| 96 | * this will take over 'running' and 'dirty', ie: a black frame will be |
| 97 | * rendered even if the Video::Renderer is not running, or has a frame available. |
| 98 | * this will be set back to false once the black frame is rendered |
| 99 | */ |
| 100 | std::atomic_bool show_black_frame; |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 101 | QMetaObject::Connection frame_update; |
| 102 | QMetaObject::Connection render_stop; |
| 103 | QMetaObject::Connection render_start; |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | G_DEFINE_TYPE_WITH_PRIVATE(VideoWidget, video_widget, GTK_TYPE_BIN); |
| 107 | |
| 108 | #define VIDEO_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), VIDEO_WIDGET_TYPE, VideoWidgetPrivate)) |
| 109 | |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 110 | /* static prototypes */ |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 111 | static gboolean check_frame_queue (VideoWidget *); |
| 112 | static void renderer_stop (VideoWidgetRenderer *); |
| 113 | static void renderer_start (VideoWidgetRenderer *); |
| 114 | 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] | 115 | static gboolean on_button_press_in_screen_event(GtkWidget *, GdkEventButton *, gpointer); |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 116 | static gboolean check_renderer_queue (VideoWidget *); |
| 117 | static void free_video_widget_renderer (VideoWidgetRenderer *); |
| 118 | static void video_widget_add_renderer (VideoWidget *, VideoWidgetRenderer *); |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 119 | |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 120 | /* |
| 121 | * video_widget_dispose() |
| 122 | * |
| 123 | * The dispose function for the video_widget class. |
| 124 | */ |
| 125 | static void |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 126 | video_widget_dispose(GObject *object) |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 127 | { |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 128 | VideoWidget *self = VIDEO_WIDGET(object); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 129 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 130 | |
Stepan Salenikovich | bf74af8 | 2015-03-13 11:35:58 -0400 | [diff] [blame] | 131 | /* dispose may be called multiple times, make sure |
| 132 | * not to call g_source_remove more than once */ |
| 133 | if (priv->frame_timeout_source) { |
| 134 | g_source_remove(priv->frame_timeout_source); |
| 135 | priv->frame_timeout_source = 0; |
| 136 | } |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 137 | |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 138 | if (priv->renderer_timeout_source) { |
| 139 | g_source_remove(priv->renderer_timeout_source); |
| 140 | priv->renderer_timeout_source = 0; |
| 141 | } |
| 142 | |
| 143 | if (priv->new_renderer_queue) { |
| 144 | g_async_queue_unref(priv->new_renderer_queue); |
| 145 | priv->new_renderer_queue = NULL; |
| 146 | } |
| 147 | |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 148 | G_OBJECT_CLASS(video_widget_parent_class)->dispose(object); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | |
| 152 | /* |
| 153 | * video_widget_finalize() |
| 154 | * |
| 155 | * The finalize function for the video_widget class. |
| 156 | */ |
| 157 | static void |
| 158 | video_widget_finalize(GObject *object) |
| 159 | { |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 160 | VideoWidget *self = VIDEO_WIDGET(object); |
| 161 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 162 | |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 163 | free_video_widget_renderer(priv->local); |
| 164 | free_video_widget_renderer(priv->remote); |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 165 | |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 166 | G_OBJECT_CLASS(video_widget_parent_class)->finalize(object); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /* |
| 171 | * video_widget_class_init() |
| 172 | * |
| 173 | * This function init the video_widget_class. |
| 174 | */ |
| 175 | static void |
| 176 | video_widget_class_init(VideoWidgetClass *klass) |
| 177 | { |
| 178 | GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 179 | |
| 180 | /* override method */ |
| 181 | object_class->dispose = video_widget_dispose; |
| 182 | object_class->finalize = video_widget_finalize; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /* |
| 187 | * video_widget_init() |
| 188 | * |
| 189 | * This function init the video_widget. |
| 190 | * - init clutter |
| 191 | * - init all the widget members |
| 192 | */ |
| 193 | static void |
| 194 | video_widget_init(VideoWidget *self) |
| 195 | { |
| 196 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 197 | |
| 198 | /* init clutter widget */ |
| 199 | priv->clutter_widget = gtk_clutter_embed_new(); |
| 200 | /* add it to the video_widget */ |
| 201 | gtk_container_add(GTK_CONTAINER(self), priv->clutter_widget); |
| 202 | /* get the stage */ |
| 203 | priv->stage = gtk_clutter_embed_get_stage(GTK_CLUTTER_EMBED(priv->clutter_widget)); |
| 204 | |
| 205 | /* layout manager is used to arrange children in space, here we ask clutter |
| 206 | * to align children to fill the space when resizing the window */ |
| 207 | clutter_actor_set_layout_manager(priv->stage, |
| 208 | clutter_bin_layout_new(CLUTTER_BIN_ALIGNMENT_FILL, CLUTTER_BIN_ALIGNMENT_FILL)); |
| 209 | |
| 210 | /* add a scene container where we can add and remove our actors */ |
| 211 | priv->video_container = clutter_actor_new(); |
| 212 | clutter_actor_set_background_color(priv->video_container, CLUTTER_COLOR_Black); |
| 213 | clutter_actor_add_child(priv->stage, priv->video_container); |
| 214 | |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 215 | /* init the remote and local structs */ |
| 216 | priv->remote = g_new0(VideoWidgetRenderer, 1); |
| 217 | priv->local = g_new0(VideoWidgetRenderer, 1); |
| 218 | |
| 219 | /* arrange remote actors */ |
| 220 | priv->remote->actor = clutter_actor_new(); |
| 221 | clutter_actor_insert_child_below(priv->video_container, priv->remote->actor, NULL); |
| 222 | /* the remote camera must always fill the container size */ |
| 223 | ClutterConstraint *constraint = clutter_bind_constraint_new(priv->video_container, |
| 224 | CLUTTER_BIND_SIZE, 0); |
| 225 | clutter_actor_add_constraint(priv->remote->actor, constraint); |
| 226 | |
| 227 | /* arrange local actor */ |
| 228 | priv->local->actor = clutter_actor_new(); |
| 229 | clutter_actor_insert_child_above(priv->video_container, priv->local->actor, NULL); |
| 230 | /* set size to square, but it will stay the aspect ratio when the image is rendered */ |
| 231 | clutter_actor_set_size(priv->local->actor, VIDEO_LOCAL_SIZE, VIDEO_LOCAL_SIZE); |
| 232 | /* set position constraint to right cornder */ |
| 233 | constraint = clutter_align_constraint_new(priv->video_container, |
| 234 | CLUTTER_ALIGN_BOTH, 0.99); |
| 235 | clutter_actor_add_constraint(priv->local->actor, constraint); |
| 236 | clutter_actor_set_opacity(priv->local->actor, |
| 237 | VIDEO_LOCAL_OPACITY_DEFAULT); |
| 238 | |
Stepan Salenikovich | 8934e84 | 2015-04-20 15:16:13 -0400 | [diff] [blame] | 239 | /* Init the timeout source which will check the for new frames. |
| 240 | * The priority must be lower than GTK drawing events |
| 241 | * (G_PRIORITY_HIGH_IDLE + 20) so that this timeout source doesn't choke |
| 242 | * the main loop on slower machines. |
| 243 | */ |
| 244 | priv->frame_timeout_source = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, |
| 245 | FRAME_RATE_PERIOD, |
| 246 | (GSourceFunc)check_frame_queue, |
| 247 | self, |
| 248 | NULL); |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 249 | |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 250 | /* init new renderer queue */ |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 251 | 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] | 252 | /* check new render every 30 ms (30ms is "fast enough"); |
| 253 | * we don't use an idle function so it doesn't consume cpu needlessly */ |
| 254 | priv->renderer_timeout_source= g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, |
| 255 | 30, |
| 256 | (GSourceFunc)check_renderer_queue, |
| 257 | self, |
| 258 | NULL); |
| 259 | |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 260 | /* handle button event */ |
| 261 | 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] | 262 | |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 263 | /* drag & drop files as video sources */ |
Stepan Salenikovich | cb6aa46 | 2015-03-21 15:13:37 -0400 | [diff] [blame] | 264 | gtk_drag_dest_set(GTK_WIDGET(self), GTK_DEST_DEFAULT_ALL, NULL, 0, (GdkDragAction)(GDK_ACTION_COPY | GDK_ACTION_PRIVATE)); |
| 265 | gtk_drag_dest_add_uri_targets(GTK_WIDGET(self)); |
| 266 | g_signal_connect(GTK_WIDGET(self), "drag-data-received", G_CALLBACK(on_drag_data_received), NULL); |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * on_drag_data_received() |
| 271 | * |
| 272 | * Handle dragged data in the video widget window. |
| 273 | * Dropping an image causes the client to switch the video input to that image. |
| 274 | */ |
| 275 | static void |
| 276 | on_drag_data_received(G_GNUC_UNUSED GtkWidget *self, |
| 277 | G_GNUC_UNUSED GdkDragContext *context, |
| 278 | G_GNUC_UNUSED gint x, |
| 279 | G_GNUC_UNUSED gint y, |
| 280 | GtkSelectionData *selection_data, |
| 281 | G_GNUC_UNUSED guint info, |
| 282 | G_GNUC_UNUSED guint32 time, |
| 283 | G_GNUC_UNUSED gpointer data) |
| 284 | { |
| 285 | gchar **uris = gtk_selection_data_get_uris(selection_data); |
| 286 | |
| 287 | /* only play the first selection */ |
| 288 | if (uris && *uris) |
Stepan Salenikovich | 033dc83 | 2015-03-23 15:56:47 -0400 | [diff] [blame] | 289 | Video::SourceModel::instance()->setFile(QUrl(*uris)); |
Stepan Salenikovich | cb6aa46 | 2015-03-21 15:13:37 -0400 | [diff] [blame] | 290 | |
| 291 | g_strfreev(uris); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 292 | } |
| 293 | |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 294 | static void |
| 295 | switch_video_input(G_GNUC_UNUSED GtkWidget *widget, Video::Device *device) |
| 296 | { |
Stepan Salenikovich | bb38263 | 2015-03-26 12:40:46 -0400 | [diff] [blame] | 297 | Video::DeviceModel::instance()->setActive(device); |
Stepan Salenikovich | 033dc83 | 2015-03-23 15:56:47 -0400 | [diff] [blame] | 298 | Video::SourceModel::instance()->switchTo(device); |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 299 | } |
| 300 | |
Stepan Salenikovich | 8bc51e5 | 2015-03-21 20:17:29 -0400 | [diff] [blame] | 301 | static void |
Stepan Salenikovich | fb5ff0a | 2015-03-29 22:47:47 -0400 | [diff] [blame] | 302 | 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] | 303 | { |
| 304 | unsigned x, y; |
| 305 | unsigned width, height; |
| 306 | |
| 307 | /* try to get the dispaly or default to 0 */ |
| 308 | QString display_env{getenv("DISPLAY")}; |
| 309 | int display = 0; |
| 310 | |
| 311 | if (!display_env.isEmpty()) { |
| 312 | auto list = display_env.split(":", QString::SkipEmptyParts); |
| 313 | /* should only be one display, so get the first one */ |
| 314 | if (list.size() > 0) { |
| 315 | display = list.at(0).toInt(); |
| 316 | g_debug("sharing screen from DISPLAY %d", display); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | x = y = width = height = 0; |
| 321 | |
| 322 | xrectsel(&x, &y, &width, &height); |
| 323 | |
| 324 | if (!width || !height) { |
| 325 | x = y = 0; |
| 326 | width = gdk_screen_width(); |
| 327 | height = gdk_screen_height(); |
| 328 | } |
| 329 | |
Stepan Salenikovich | 033dc83 | 2015-03-23 15:56:47 -0400 | [diff] [blame] | 330 | Video::SourceModel::instance()->setDisplay(display, QRect(x,y,width,height)); |
Stepan Salenikovich | 8bc51e5 | 2015-03-21 20:17:29 -0400 | [diff] [blame] | 331 | } |
| 332 | |
Stepan Salenikovich | 763c25a | 2015-03-26 13:51:31 -0400 | [diff] [blame] | 333 | static void |
Stepan Salenikovich | fb5ff0a | 2015-03-29 22:47:47 -0400 | [diff] [blame] | 334 | switch_video_input_file(G_GNUC_UNUSED GtkWidget *item, GtkWidget *parent) |
Stepan Salenikovich | 763c25a | 2015-03-26 13:51:31 -0400 | [diff] [blame] | 335 | { |
| 336 | if (parent && GTK_IS_WIDGET(parent)) { |
| 337 | /* get parent window */ |
| 338 | parent = gtk_widget_get_toplevel(GTK_WIDGET(parent)); |
| 339 | } |
| 340 | |
| 341 | gchar *uri = NULL; |
| 342 | GtkWidget *dialog = gtk_file_chooser_dialog_new( |
| 343 | "Choose File", |
| 344 | GTK_WINDOW(parent), |
| 345 | GTK_FILE_CHOOSER_ACTION_OPEN, |
| 346 | "_Cancel", GTK_RESPONSE_CANCEL, |
| 347 | "_Open", GTK_RESPONSE_ACCEPT, |
| 348 | NULL); |
| 349 | |
| 350 | if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { |
| 351 | uri = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog)); |
| 352 | } |
| 353 | |
| 354 | gtk_widget_destroy(dialog); |
| 355 | |
| 356 | Video::SourceModel::instance()->setFile(QUrl(uri)); |
| 357 | |
| 358 | g_free(uri); |
| 359 | } |
| 360 | |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 361 | /* |
| 362 | * on_button_press_in_screen_event() |
| 363 | * |
| 364 | * Handle button event in the video screen. |
| 365 | */ |
| 366 | static gboolean |
Stepan Salenikovich | 763c25a | 2015-03-26 13:51:31 -0400 | [diff] [blame] | 367 | on_button_press_in_screen_event(GtkWidget *parent, |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 368 | GdkEventButton *event, |
| 369 | G_GNUC_UNUSED gpointer data) |
| 370 | { |
| 371 | /* check for right click */ |
| 372 | if (event->button != BUTTON_RIGHT_CLICK || event->type != GDK_BUTTON_PRESS) |
| 373 | return FALSE; |
| 374 | |
| 375 | /* create menu with available video sources */ |
| 376 | GtkWidget *menu = gtk_menu_new(); |
| 377 | |
| 378 | /* list available devices and check off the active device */ |
| 379 | auto device_list = Video::DeviceModel::instance()->devices(); |
| 380 | |
| 381 | for( auto device: device_list) { |
| 382 | GtkWidget *item = gtk_check_menu_item_new_with_mnemonic(device->name().toLocal8Bit().constData()); |
| 383 | gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
| 384 | gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), device->isActive()); |
| 385 | g_signal_connect(item, "activate", G_CALLBACK(switch_video_input), device); |
| 386 | } |
| 387 | |
Stepan Salenikovich | bb38263 | 2015-03-26 12:40:46 -0400 | [diff] [blame] | 388 | /* add separator */ |
| 389 | gtk_menu_shell_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new()); |
| 390 | |
Stepan Salenikovich | 8bc51e5 | 2015-03-21 20:17:29 -0400 | [diff] [blame] | 391 | /* add screen area as an input */ |
| 392 | GtkWidget *item = gtk_check_menu_item_new_with_mnemonic("Share screen area"); |
| 393 | gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
| 394 | g_signal_connect(item, "activate", G_CALLBACK(switch_video_input_screen), NULL); |
| 395 | |
Stepan Salenikovich | 763c25a | 2015-03-26 13:51:31 -0400 | [diff] [blame] | 396 | /* add file as an input */ |
| 397 | item = gtk_check_menu_item_new_with_mnemonic("Share file"); |
| 398 | gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
| 399 | g_signal_connect(item, "activate", G_CALLBACK(switch_video_input_file), parent); |
| 400 | |
Stepan Salenikovich | 50c989b | 2015-03-21 18:32:46 -0400 | [diff] [blame] | 401 | /* show menu */ |
| 402 | gtk_widget_show_all(menu); |
| 403 | gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, event->button, event->time); |
| 404 | |
| 405 | return TRUE; /* event has been fully handled */ |
| 406 | } |
| 407 | |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 408 | static void |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 409 | clutter_render_image(VideoWidgetRenderer* wg_renderer) |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 410 | { |
Stepan Salenikovich | bf118ac | 2015-04-22 14:06:50 -0400 | [diff] [blame] | 411 | auto actor = wg_renderer->actor; |
| 412 | g_return_if_fail(CLUTTER_IS_ACTOR(actor)); |
| 413 | |
| 414 | if (wg_renderer->show_black_frame) { |
| 415 | /* render a black frame set the bool back to false, this is likely done |
| 416 | * when the renderer is stopped so we ignore whether or not it is running |
| 417 | */ |
| 418 | auto image_empty = clutter_image_new(); |
| 419 | clutter_actor_set_content(actor, image_empty); |
| 420 | g_object_unref (image_empty); |
| 421 | wg_renderer->show_black_frame = false; |
| 422 | return; |
| 423 | } |
| 424 | |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame^] | 425 | ClutterContent *image_new = nullptr; |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 426 | |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame^] | 427 | { |
| 428 | /* the following must be done under lock in case a 'stopped' signal is |
| 429 | * received during rendering; otherwise the mem could become invalid */ |
| 430 | std::lock_guard<std::mutex> lock(wg_renderer->run_mutex); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 431 | |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame^] | 432 | if (!wg_renderer->running) |
| 433 | return; |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 434 | |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame^] | 435 | auto renderer = wg_renderer->renderer; |
| 436 | if (renderer == nullptr) |
| 437 | return; |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 438 | |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame^] | 439 | const auto frameData = (const guint8*)renderer->currentFrame().constData(); |
| 440 | if (!frameData or !wg_renderer->dirty) |
| 441 | return; |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 442 | |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame^] | 443 | wg_renderer->dirty = false; |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 444 | |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame^] | 445 | image_new = clutter_image_new(); |
| 446 | g_return_if_fail(image_new); |
| 447 | |
| 448 | const auto& res = renderer->size(); |
| 449 | const gint BPP = 4; |
| 450 | const gint ROW_STRIDE = BPP * res.width(); |
| 451 | |
| 452 | GError *error = nullptr; |
| 453 | clutter_image_set_data( |
| 454 | CLUTTER_IMAGE(image_new), |
| 455 | frameData, |
| 456 | COGL_PIXEL_FORMAT_BGRA_8888, |
| 457 | res.width(), |
| 458 | res.height(), |
| 459 | ROW_STRIDE, |
| 460 | &error); |
| 461 | if (error) { |
| 462 | g_warning("error rendering image to clutter: %s", error->message); |
| 463 | g_error_free(error); |
| 464 | g_object_unref (image_new); |
| 465 | return; |
| 466 | } |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 467 | } |
| 468 | |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 469 | clutter_actor_set_content(actor, image_new); |
Stepan Salenikovich | cd1bf63 | 2015-03-09 16:24:08 -0400 | [diff] [blame] | 470 | g_object_unref (image_new); |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 471 | |
Stepan Salenikovich | cd1bf63 | 2015-03-09 16:24:08 -0400 | [diff] [blame] | 472 | /* note: we must set the content gravity be "resize aspect" after setting the image data to make sure |
| 473 | * that the aspect ratio is correct |
| 474 | */ |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 475 | clutter_actor_set_content_gravity(actor, CLUTTER_CONTENT_GRAVITY_RESIZE_ASPECT); |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 476 | } |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 477 | |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 478 | static gboolean |
| 479 | check_frame_queue(VideoWidget *self) |
| 480 | { |
| 481 | g_return_val_if_fail(IS_VIDEO_WIDGET(self), FALSE); |
| 482 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 483 | |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 484 | /* display renderer's frames */ |
| 485 | clutter_render_image(priv->local); |
| 486 | clutter_render_image(priv->remote); |
Stepan Salenikovich | 5e6a0b7 | 2015-03-12 14:55:22 -0400 | [diff] [blame] | 487 | |
| 488 | return TRUE; /* keep going */ |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 489 | } |
| 490 | |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 491 | static void |
| 492 | renderer_stop(VideoWidgetRenderer *renderer) |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 493 | { |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 494 | QObject::disconnect(renderer->frame_update); |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame^] | 495 | { |
| 496 | /* must do this under lock, in case the rendering is taking place when |
| 497 | * this signal is received */ |
| 498 | std::lock_guard<std::mutex> lock(renderer->run_mutex); |
| 499 | renderer->running = false; |
| 500 | } |
Stepan Salenikovich | bf118ac | 2015-04-22 14:06:50 -0400 | [diff] [blame] | 501 | /* ask to show a black frame */ |
| 502 | renderer->show_black_frame = true; |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 503 | } |
| 504 | |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 505 | static void |
| 506 | renderer_start(VideoWidgetRenderer *renderer) |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 507 | { |
Stepan Salenikovich | c5f0815 | 2015-03-19 00:53:23 -0400 | [diff] [blame] | 508 | QObject::disconnect(renderer->frame_update); |
Stepan Salenikovich | aea6c04 | 2015-04-29 15:09:16 -0400 | [diff] [blame^] | 509 | { |
| 510 | std::lock_guard<std::mutex> lock(renderer->run_mutex); |
| 511 | renderer->running = true; |
| 512 | } |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 513 | renderer->frame_update = QObject::connect( |
| 514 | renderer->renderer, |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 515 | &Video::Renderer::frameUpdated, |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 516 | [renderer]() { |
| 517 | // WARNING: this lambda is called in LRC renderer thread, |
| 518 | // but check_frame_queue() is in mainloop! |
Stepan Salenikovich | bf118ac | 2015-04-22 14:06:50 -0400 | [diff] [blame] | 519 | |
| 520 | /* make sure show_black_frame is false since it will take priority |
| 521 | * over a new frame from the Video::Renderer */ |
| 522 | renderer->show_black_frame = false; |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 523 | renderer->dirty = true; |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 524 | } |
Guillaume Roguez | 24834e0 | 2015-04-09 12:45:40 -0400 | [diff] [blame] | 525 | ); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 526 | } |
| 527 | |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 528 | static void |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 529 | free_video_widget_renderer(VideoWidgetRenderer *renderer) |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 530 | { |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 531 | QObject::disconnect(renderer->frame_update); |
| 532 | QObject::disconnect(renderer->render_stop); |
| 533 | QObject::disconnect(renderer->render_start); |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 534 | g_free(renderer); |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 535 | } |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 536 | |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 537 | static void |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 538 | video_widget_add_renderer(VideoWidget *self, VideoWidgetRenderer *new_video_renderer) |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 539 | { |
| 540 | g_return_if_fail(IS_VIDEO_WIDGET(self)); |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 541 | g_return_if_fail(new_video_renderer); |
| 542 | g_return_if_fail(new_video_renderer->renderer); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 543 | |
| 544 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 545 | |
| 546 | /* update the renderer */ |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 547 | switch(new_video_renderer->type) { |
Stepan Salenikovich | c5f0815 | 2015-03-19 00:53:23 -0400 | [diff] [blame] | 548 | case VIDEO_RENDERER_REMOTE: |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 549 | /* swap the remote renderer */ |
| 550 | new_video_renderer->actor = priv->remote->actor; |
| 551 | free_video_widget_renderer(priv->remote); |
| 552 | priv->remote = new_video_renderer; |
| 553 | /* reset the content gravity so that the aspect ratio gets properly |
| 554 | * reset if it chagnes */ |
| 555 | clutter_actor_set_content_gravity(priv->remote->actor, |
| 556 | CLUTTER_CONTENT_GRAVITY_RESIZE_FILL); |
Stepan Salenikovich | c5f0815 | 2015-03-19 00:53:23 -0400 | [diff] [blame] | 557 | break; |
| 558 | case VIDEO_RENDERER_LOCAL: |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 559 | /* swap the remote renderer */ |
| 560 | new_video_renderer->actor = priv->local->actor; |
| 561 | free_video_widget_renderer(priv->local); |
| 562 | priv->local = new_video_renderer; |
| 563 | /* reset the content gravity so that the aspect ratio gets properly |
| 564 | * reset if it chagnes */ |
| 565 | clutter_actor_set_content_gravity(priv->local->actor, |
| 566 | CLUTTER_CONTENT_GRAVITY_RESIZE_FILL); |
Stepan Salenikovich | c5f0815 | 2015-03-19 00:53:23 -0400 | [diff] [blame] | 567 | break; |
| 568 | case VIDEO_RENDERER_COUNT: |
| 569 | break; |
| 570 | } |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 571 | } |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 572 | |
| 573 | static gboolean |
| 574 | check_renderer_queue(VideoWidget *self) |
| 575 | { |
| 576 | g_return_val_if_fail(IS_VIDEO_WIDGET(self), G_SOURCE_REMOVE); |
| 577 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 578 | |
| 579 | /* get all the renderers in the queue */ |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 580 | 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] | 581 | while (new_video_renderer) { |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 582 | video_widget_add_renderer(self, new_video_renderer); |
| 583 | 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] | 584 | } |
| 585 | |
| 586 | return G_SOURCE_CONTINUE; |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * video_widget_new() |
| 591 | * |
| 592 | * The function use to create a new video_widget |
| 593 | */ |
| 594 | GtkWidget* |
| 595 | video_widget_new(void) |
| 596 | { |
| 597 | GtkWidget *self = (GtkWidget *)g_object_new(VIDEO_WIDGET_TYPE, NULL); |
| 598 | return self; |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * video_widget_push_new_renderer() |
| 603 | * |
| 604 | * This function is used add a new Video::Renderer to the VideoWidget in a |
| 605 | * thread-safe manner. |
| 606 | */ |
| 607 | void |
| 608 | video_widget_push_new_renderer(VideoWidget *self, Video::Renderer *renderer, VideoRendererType type) |
| 609 | { |
| 610 | g_return_if_fail(IS_VIDEO_WIDGET(self)); |
| 611 | VideoWidgetPrivate *priv = VIDEO_WIDGET_GET_PRIVATE(self); |
| 612 | |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 613 | /* if the renderer is nullptr, there is nothing to be done */ |
| 614 | if (!renderer) return; |
| 615 | |
| 616 | VideoWidgetRenderer *new_video_renderer = g_new0(VideoWidgetRenderer, 1); |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 617 | new_video_renderer->renderer = renderer; |
| 618 | new_video_renderer->type = type; |
| 619 | |
Stepan Salenikovich | 3bcb0b2 | 2015-04-21 18:44:55 -0400 | [diff] [blame] | 620 | if (new_video_renderer->renderer->isRendering()) |
| 621 | renderer_start(new_video_renderer); |
| 622 | |
| 623 | new_video_renderer->render_stop = QObject::connect( |
| 624 | new_video_renderer->renderer, |
| 625 | &Video::Renderer::stopped, |
| 626 | [=]() { |
| 627 | renderer_stop(new_video_renderer); |
| 628 | } |
| 629 | ); |
| 630 | |
| 631 | new_video_renderer->render_start = QObject::connect( |
| 632 | new_video_renderer->renderer, |
| 633 | &Video::Renderer::started, |
| 634 | [=]() { |
| 635 | renderer_start(new_video_renderer); |
| 636 | } |
| 637 | ); |
| 638 | |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 639 | g_async_queue_push(priv->new_renderer_queue, new_video_renderer); |
| 640 | } |