webrtc/nice: Support domain name as connection-address of ICE candidate
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / ext / qt / qtwindow.cc
1 /*
2  * GStreamer
3  * Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26
27 #include <gst/video/video.h>
28 #include <gst/gl/gstglfuncs.h>
29 #include "qtwindow.h"
30 #include "gstqsgtexture.h"
31 #include "gstqtglutility.h"
32
33 #include <QtCore/QDateTime>
34 #include <QtGui/QGuiApplication>
35 #include <QtQuick/QQuickWindow>
36 #include <QOpenGLFramebufferObject>
37
38 /* compatibility definitions... */
39 #ifndef GL_READ_FRAMEBUFFER
40 #define GL_READ_FRAMEBUFFER 0x8CA8
41 #endif
42 #ifndef GL_DRAW_FRAMEBUFFER
43 #define GL_DRAW_FRAMEBUFFER 0x8CA9
44 #endif
45
46 /**
47  * SECTION:
48  *
49  * #QtGLWindow is an #QQuickWindow that grab QtQuick view to GStreamer OpenGL video buffers.
50  */
51
52 GST_DEBUG_CATEGORY_STATIC (qt_window_debug);
53 #define GST_CAT_DEFAULT qt_window_debug
54
55 struct _QtGLWindowPrivate
56 {
57   GMutex lock;
58   GCond update_cond;
59
60   GstBuffer *buffer;
61   GstCaps *caps;
62   GstVideoInfo v_info;
63
64   gboolean initted;
65   gboolean updated;
66   gboolean quit;
67   gboolean result;
68   gboolean useDefaultFbo;
69
70   GstGLDisplay *display;
71   GstGLContext *other_context;
72   GstGLContext *context;
73
74   GLuint fbo;
75
76   /* frames that qmlview rendered in its gl thread */
77   quint64 frames_rendered;
78   quint64 start;
79   quint64 stop;
80 };
81
82 QtGLWindow::QtGLWindow ( QWindow * parent, QQuickWindow *src ) :
83   QQuickWindow( parent ), source (src)
84 {
85   QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
86   static gsize _debug;
87
88   g_assert (app != NULL);
89
90   if (g_once_init_enter (&_debug)) {
91     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwindow", 0, "Qt GL QuickWindow");
92     g_once_init_leave (&_debug, 1);
93   }
94
95   this->priv = g_new0 (QtGLWindowPrivate, 1);
96
97   g_mutex_init (&this->priv->lock);
98   g_cond_init (&this->priv->update_cond);
99
100   this->priv->display = gst_qt_get_gl_display(FALSE);
101
102   connect (source, SIGNAL(beforeRendering()), this, SLOT(beforeRendering()), Qt::DirectConnection);
103   connect (source, SIGNAL(afterRendering()), this, SLOT(afterRendering()), Qt::DirectConnection);
104   connect (app, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()), Qt::DirectConnection);
105   if (source->isSceneGraphInitialized())
106     source->scheduleRenderJob(new RenderJob(std::bind(&QtGLWindow::onSceneGraphInitialized, this)), QQuickWindow::BeforeSynchronizingStage);
107   else
108     connect (source, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
109
110   connect (source, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
111
112   GST_DEBUG ("%p init Qt Window", this->priv->display);
113 }
114
115 QtGLWindow::~QtGLWindow()
116 {
117   GST_DEBUG ("deinit Qt Window");
118   g_mutex_clear (&this->priv->lock);
119   g_cond_clear (&this->priv->update_cond);
120   if (this->priv->other_context)
121     gst_object_unref(this->priv->other_context);
122   if (this->priv->display)
123     gst_object_unref(this->priv->display);
124   if (this->priv->context)
125     gst_object_unref(this->priv->context);
126   if (this->priv->caps)
127       gst_caps_unref(this->priv->caps);
128   g_free (this->priv);
129   this->priv = NULL;
130 }
131
132 void
133 QtGLWindow::beforeRendering()
134 {
135   unsigned int width, height;
136
137   g_mutex_lock (&this->priv->lock);
138
139   static gsize once = 0;
140   if (g_once_init_enter(&once)) {
141     this->priv->start = QDateTime::currentDateTime().toMSecsSinceEpoch();
142     g_once_init_leave(&once,1);
143   }
144
145   if (!fbo && !this->priv->useDefaultFbo) {
146
147     width = source->width();
148     height = source->height();
149
150     GST_DEBUG ("create new framebuffer object %dX%d", width, height);
151
152     fbo.reset(new QOpenGLFramebufferObject (width, height,
153           QOpenGLFramebufferObject::CombinedDepthStencil, GL_TEXTURE_2D, GL_RGBA));
154
155     source->setRenderTarget(fbo.data());
156   } else if (this->priv->useDefaultFbo) {
157     GST_DEBUG ("use default fbo for render target");
158     fbo.reset(NULL);
159     source->setRenderTarget(NULL);
160   }
161
162   g_mutex_unlock (&this->priv->lock);
163 }
164
165
166 void
167 QtGLWindow::afterRendering()
168 {
169   GstVideoFrame gl_frame;
170   GstVideoInfo *info;
171   GstGLContext *context;
172   gboolean ret;
173   guint width, height;
174   const GstGLFuncs *gl;
175   GLuint dst_tex;
176   GstGLSyncMeta *sync_meta;
177
178   g_mutex_lock (&this->priv->lock);
179
180   this->priv->frames_rendered++;
181
182   if(!this->priv->buffer || this->priv->updated == TRUE) {
183     GST_DEBUG ("skip this frame");
184     g_mutex_unlock (&this->priv->lock);
185     return;
186   }
187
188   GST_DEBUG ("copy buffer %p",this->priv->buffer);
189
190   width = GST_VIDEO_INFO_WIDTH (&this->priv->v_info);
191   height = GST_VIDEO_INFO_HEIGHT (&this->priv->v_info);
192   info = &this->priv->v_info;
193   context = this->priv->other_context;
194
195   gst_gl_context_activate (context, TRUE);
196   gl = context->gl_vtable;
197
198   ret = gst_video_frame_map (&gl_frame, info, this->priv->buffer,
199       (GstMapFlags) (GST_MAP_WRITE | GST_MAP_GL));
200
201   if (!ret) {
202     this->priv->buffer = NULL;
203     GST_ERROR ("Failed to map video frame");
204     goto errors;
205   }
206
207   gl->BindFramebuffer (GL_READ_FRAMEBUFFER, this->source->renderTargetId());
208
209   ret = gst_gl_context_check_framebuffer_status (context, GL_READ_FRAMEBUFFER);
210   if (!ret) {
211     GST_ERROR ("FBO errors");
212     goto errors;
213   }
214
215   dst_tex = *(guint *) gl_frame.data[0];
216   GST_DEBUG ("qml render target id %d, render to tex %d %dX%d", 
217       this->source->renderTargetId(), dst_tex, width,height);
218
219   gl->BindTexture (GL_TEXTURE_2D, dst_tex);
220   if (gl->BlitFramebuffer) {
221     gl->BindFramebuffer (GL_DRAW_FRAMEBUFFER, this->priv->fbo);
222     gl->FramebufferTexture2D (GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
223               GL_TEXTURE_2D, dst_tex, 0);
224
225     ret = gst_gl_context_check_framebuffer_status (context, GL_DRAW_FRAMEBUFFER);
226     if (!ret) {
227       GST_ERROR ("FBO errors");
228       goto errors;
229     }
230     if (this->priv->useDefaultFbo)
231       gl->ReadBuffer (GL_BACK);
232     else
233       gl->ReadBuffer (GL_COLOR_ATTACHMENT0);
234     gl->BlitFramebuffer (0, 0, width, height,
235         0, 0, width, height,
236         GL_COLOR_BUFFER_BIT, GL_LINEAR);
237   } else {
238     gl->CopyTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);
239   }
240
241   gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
242   gst_video_frame_unmap (&gl_frame);
243
244   if (this->priv->context) {
245     sync_meta = gst_buffer_get_gl_sync_meta (this->priv->buffer);
246     if (!sync_meta) {
247       sync_meta = gst_buffer_add_gl_sync_meta (this->priv->context, this->priv->buffer);
248     }
249     gst_gl_sync_meta_set_sync_point (sync_meta, context);
250   }
251
252   GST_DEBUG ("rendering finished");
253
254 done:
255   gst_gl_context_activate (context, FALSE);
256
257   this->priv->result = ret;
258   this->priv->updated = TRUE;
259   g_cond_signal (&this->priv->update_cond);
260   g_mutex_unlock (&this->priv->lock);
261   return;
262
263 errors:
264   gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
265   gst_video_frame_unmap (&gl_frame);
266   goto done;
267 }
268
269 void
270 QtGLWindow::aboutToQuit()
271 {
272   g_mutex_lock (&this->priv->lock);
273
274   this->priv->updated = TRUE;
275   this->priv->quit = TRUE;
276   g_cond_signal (&this->priv->update_cond);
277
278   this->priv->stop = QDateTime::currentDateTime().toMSecsSinceEpoch();
279   qint64 duration = this->priv->stop - this->priv->start;
280   float fps = ((float)this->priv->frames_rendered / duration * 1000);
281
282   GST_DEBUG("about to quit, total refresh frames (%lld) in (%0.3f) seconds, fps: %0.3f",
283       this->priv->frames_rendered, (float)duration / 1000, fps);
284
285   g_mutex_unlock (&this->priv->lock);
286 }
287
288 void
289 QtGLWindow::onSceneGraphInitialized()
290 {
291   GST_DEBUG ("scene graph initialization with Qt GL context %p",
292       this->source->openglContext ());
293
294   this->priv->initted = gst_qt_get_gl_wrapcontext (this->priv->display,
295       &this->priv->other_context, &this->priv->context);
296
297   if (this->priv->initted && this->priv->other_context) {
298     const GstGLFuncs *gl;
299
300     gst_gl_context_activate (this->priv->other_context, TRUE);
301     gl = this->priv->other_context->gl_vtable;
302
303     gl->GenFramebuffers (1, &this->priv->fbo);
304
305     gst_gl_context_activate (this->priv->other_context, FALSE);
306   }
307
308   GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
309       this->priv->other_context);
310 }
311
312 void
313 QtGLWindow::onSceneGraphInvalidated()
314 {
315   GST_DEBUG ("scene graph invalidated");
316
317   if (this->priv->fbo && this->priv->other_context) {
318     const GstGLFuncs *gl;
319
320     gst_gl_context_activate (this->priv->other_context, TRUE);
321     gl = this->priv->other_context->gl_vtable;
322
323     gl->DeleteFramebuffers (1, &this->priv->fbo);
324
325     gst_gl_context_activate (this->priv->other_context, FALSE);
326   }
327 }
328
329 bool
330 QtGLWindow::getGeometry(int * width, int * height)
331 {
332   if (width == NULL || height == NULL)
333     return FALSE;
334
335   double scale = this->source->effectiveDevicePixelRatio();
336   *width = this->source->width() * scale;
337   *height = this->source->height() * scale;
338
339   GST_LOG("Window width %d height %d scale %f", *width, *height,
340       scale);
341
342   return TRUE;
343 }
344
345 GstGLContext *
346 qt_window_get_qt_context (QtGLWindow * qt_window)
347 {
348   g_return_val_if_fail (qt_window != NULL, NULL);
349
350   if (!qt_window->priv->other_context)
351     return NULL;
352
353   return (GstGLContext *) gst_object_ref (qt_window->priv->other_context);
354 }
355
356 GstGLDisplay *
357 qt_window_get_display (QtGLWindow * qt_window)
358 {
359   g_return_val_if_fail (qt_window != NULL, NULL);
360
361   if (!qt_window->priv->display)
362     return NULL;
363
364   return (GstGLDisplay *) gst_object_ref (qt_window->priv->display);
365 }
366
367 GstGLContext *
368 qt_window_get_context (QtGLWindow * qt_window)
369 {
370   g_return_val_if_fail (qt_window != NULL, NULL);
371
372   if (!qt_window->priv->context)
373     return NULL;
374
375   return (GstGLContext *) gst_object_ref (qt_window->priv->context);
376 }
377
378 gboolean
379 qt_window_set_context (QtGLWindow * qt_window, GstGLContext * context)
380 {
381   g_return_val_if_fail (qt_window != NULL, FALSE);
382
383   if (qt_window->priv->context && qt_window->priv->context != context)
384     return FALSE;
385
386   gst_object_replace ((GstObject **) &qt_window->priv->context, (GstObject *) context);
387
388   return TRUE;
389 }
390
391 gboolean
392 qt_window_is_scenegraph_initialized (QtGLWindow * qt_window)
393 {
394   g_return_val_if_fail (qt_window != NULL, FALSE);
395
396   return qt_window->priv->initted;
397 }
398
399 gboolean
400 qt_window_set_caps (QtGLWindow * qt_window, GstCaps * caps)
401 {
402   GstVideoInfo v_info;
403
404   g_return_val_if_fail (qt_window != NULL, FALSE);
405   g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
406   g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
407
408   if (qt_window->priv->caps && gst_caps_is_equal_fixed (qt_window->priv->caps, caps))
409     return TRUE;
410
411   if (!gst_video_info_from_caps (&v_info, caps))
412     return FALSE;
413
414   g_mutex_lock (&qt_window->priv->lock);
415
416   gst_caps_replace (&qt_window->priv->caps, caps);
417
418   qt_window->priv->v_info = v_info;
419
420   g_mutex_unlock (&qt_window->priv->lock);
421
422   return TRUE;
423 }
424
425 gboolean
426 qt_window_set_buffer (QtGLWindow * qt_window, GstBuffer * buffer)
427 {
428   g_return_val_if_fail (qt_window != NULL, FALSE);
429   g_return_val_if_fail (qt_window->priv->initted, FALSE);
430   gboolean ret;
431
432   g_mutex_lock (&qt_window->priv->lock);
433
434   if (qt_window->priv->quit){
435     GST_DEBUG("about to quit, drop this buffer");
436     g_mutex_unlock (&qt_window->priv->lock);
437     return TRUE;
438   }
439
440   qt_window->priv->updated = FALSE;
441   qt_window->priv->buffer = buffer;
442
443   while (!qt_window->priv->updated) 
444     g_cond_wait (&qt_window->priv->update_cond, &qt_window->priv->lock);
445   
446   ret = qt_window->priv->result;
447
448   g_mutex_unlock (&qt_window->priv->lock);
449
450   return ret;
451 }
452
453 void
454 qt_window_use_default_fbo (QtGLWindow * qt_window, gboolean useDefaultFbo)
455 {
456   g_return_if_fail (qt_window != NULL);
457
458   g_mutex_lock (&qt_window->priv->lock);
459
460   GST_DEBUG ("set to use default fbo %d", useDefaultFbo);
461   qt_window->priv->useDefaultFbo = useDefaultFbo;
462
463   g_mutex_unlock (&qt_window->priv->lock);
464 }
465
466 void
467 qt_window_stop(QtGLWindow* qt_window)
468 {
469   g_mutex_lock(&qt_window->priv->lock);
470
471   GST_DEBUG("stop window");
472   qt_window->priv->updated = TRUE;
473   g_cond_signal(&qt_window->priv->update_cond);
474
475   g_mutex_unlock(&qt_window->priv->lock);
476 }