--- gstreamer-vaapi-1.12.1/gst/vaapi/gstvaapipluginutil.c	2017-06-20 14:40:00.000000000 +0800
+++ gstreamer-vaapi-1.12.1-hack/gst/vaapi/gstvaapipluginutil.c	2017-07-04 11:49:02.206600462 +0800
@@ -54,6 +54,7 @@
   GstVaapiDisplayType type;
   GstVaapiDisplayCreateFunc create_display;
   GstVaapiDisplayCreateFromHandleFunc create_display_from_handle;
+  const gchar *gst_gl_window;
 } DisplayMap;
 
 /* *INDENT-OFF* */
@@ -63,21 +64,24 @@
    GST_VAAPI_DISPLAY_TYPE_WAYLAND,
    gst_vaapi_display_wayland_new,
    (GstVaapiDisplayCreateFromHandleFunc)
-   gst_vaapi_display_wayland_new_with_display},
+   gst_vaapi_display_wayland_new_with_display,
+   "wayland"},
 #endif
 #if USE_GLX
   {"glx",
    GST_VAAPI_DISPLAY_TYPE_GLX,
    gst_vaapi_display_glx_new,
    (GstVaapiDisplayCreateFromHandleFunc)
-   gst_vaapi_display_glx_new_with_display},
+   gst_vaapi_display_glx_new_with_display,
+   "x11"},
 #endif
 #if USE_X11
   {"x11",
    GST_VAAPI_DISPLAY_TYPE_X11,
    gst_vaapi_display_x11_new,
    (GstVaapiDisplayCreateFromHandleFunc)
-   gst_vaapi_display_x11_new_with_display},
+   gst_vaapi_display_x11_new_with_display,
+   "x11"},
 #endif
 #if USE_DRM
   {"drm",
@@ -103,6 +107,28 @@
     if (display || display_type != GST_VAAPI_DISPLAY_TYPE_ANY)
       break;
   }
+
+  if (display && m->gst_gl_window) {
+    /* The opengl plugin likes to guess at what display type to use and
+     * checks for X11 before Wayland. We on the other hand check for Wayland
+     * before X11 (see above). So if you're running Xwayland (like in Gnome
+     * Shell) then you will find we have chosen "wayland" (above) but the
+     * opengl plugin will choose "x11", and will fail to render video from
+     * vaapi-wayland (LP: #1698287). So we use the only means available to
+     * force the opengl plugin to make the right decision... */
+    const gchar *preexisting = g_getenv("GST_GL_WINDOW");
+    const gchar *required = m->gst_gl_window;
+    if (preexisting) {
+      if (!g_strstr_len (preexisting, strlen(required), required)) {
+        GST_ERROR ("Environment contains GST_GL_WINDOW=%s but needs to be "
+                   "unset or set to \"%s\" for vaapi to render!",
+                   preexisting, required);
+      }
+    } else {
+      g_setenv ("GST_GL_WINDOW", required, 1);
+    }
+  }
+
   return display;
 }
 
