static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps) { GstWaylandSink *sink = GST_WAYLAND_SINK (bsink); GstBufferPool *newpool, *oldpool; GstVideoInfo info; GstStructure *structure; static GstAllocationParams params = { 0, 0, 0, 15, }; guint size; sink = GST_WAYLAND_SINK (bsink); GST_LOG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps); if (!gst_video_info_from_caps (&info, caps)) goto invalid_format; sink->video_width = info.width; sink->video_height = info.height; size = info.size; /* create a new pool for the new configuration */ newpool = gst_wayland_buffer_pool_new (sink); if (!newpool) { GST_DEBUG_OBJECT (sink, "Failed to create new pool"); return FALSE; } structure = gst_buffer_pool_get_config (newpool); gst_buffer_pool_config_set_params (structure, caps, size, 2, 0); gst_buffer_pool_config_set_allocator (structure, NULL, ¶ms); if (!gst_buffer_pool_set_config (newpool, structure)) goto config_failed; oldpool = sink->pool; sink->pool = newpool; if (oldpool) gst_object_unref (oldpool); return TRUE; invalid_format: { GST_DEBUG_OBJECT (sink, "Could not locate image format from caps %" GST_PTR_FORMAT, caps); return FALSE; } config_failed: { GST_DEBUG_OBJECT (bsink, "failed setting config"); return FALSE; } }
static void gst_wayland_sink_finalize (GObject * object) { GstWaylandSink *sink = GST_WAYLAND_SINK (object); GST_DEBUG_OBJECT (sink, "Finalizing the sink.."); if (sink->window) destroy_window (sink->window); if (sink->display) destroy_display (sink->display); if (sink->pool_lock) { g_mutex_free (sink->pool_lock); sink->pool_lock = NULL; } if (sink->buffer_pool) { gst_wayland_bufferpool_clear (sink); } g_mutex_free (sink->wayland_lock); G_OBJECT_CLASS (parent_class)->finalize (object); }
static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle) { GstWaylandSink *sink = GST_WAYLAND_SINK (overlay); struct wl_surface *surface = (struct wl_surface *) handle; g_return_if_fail (sink != NULL); g_mutex_lock (&sink->render_lock); GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT, (void *) handle); g_clear_object (&sink->window); if (handle) { if (G_LIKELY (gst_wayland_sink_find_display (sink))) { /* we cannot use our own display with an external window handle */ if (G_UNLIKELY (sink->display->own_display)) { GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE, ("Application did not provide a wayland display handle"), ("waylandsink cannot use an externally-supplied surface without " "an externally-supplied display handle. Consider providing a " "display handle from your application with GstContext")); } else { sink->window = gst_wl_window_new_in_surface (sink->display, surface); } } else { GST_ERROR_OBJECT (sink, "Failed to find display handle, " "ignoring window handle"); } } g_mutex_unlock (&sink->render_lock); }
static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps) { GstWaylandSink *sink = GST_WAYLAND_SINK (bsink); const GstStructure *structure; GstCaps *allowed_caps; gboolean ret = TRUE; GST_LOG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps); allowed_caps = gst_pad_get_caps (GST_BASE_SINK_PAD (bsink)); if (!gst_caps_can_intersect (allowed_caps, caps)) return FALSE; structure = gst_caps_get_structure (caps, 0); ret &= gst_structure_get_int (structure, "width", &sink->video_width); ret &= gst_structure_get_int (structure, "height", &sink->video_height); if (!ret) return FALSE; gst_caps_replace (&sink->caps, caps); return TRUE; }
static void gst_wayland_sink_finalize (GObject * object) { GstWaylandSink *sink = GST_WAYLAND_SINK (object); GST_DEBUG_OBJECT (sink, "Finalizing the sink.."); if (sink->last_buffer) gst_buffer_unref (sink->last_buffer); if (sink->display) { /* see comment about this call in gst_wayland_sink_change_state() */ if (sink->pool) { gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL (sink->pool)); } g_object_unref (sink->display); } if (sink->window) g_object_unref (sink->window); if (sink->pool) gst_object_unref (sink->pool); if (sink->display_name) g_free (sink->display_name); g_mutex_clear (&sink->display_lock); g_mutex_clear (&sink->render_lock); G_OBJECT_CLASS (parent_class)->finalize (object); }
static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element, GstStateChange transition) { GstWaylandSink *sink = GST_WAYLAND_SINK (element); GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; switch (transition) { case GST_STATE_CHANGE_NULL_TO_READY: if (!gst_wayland_sink_find_display (sink)) return GST_STATE_CHANGE_FAILURE; break; default: break; } ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); if (ret == GST_STATE_CHANGE_FAILURE) return ret; switch (transition) { case GST_STATE_CHANGE_PAUSED_TO_READY: gst_buffer_replace (&sink->last_buffer, NULL); if (sink->window) { if (gst_wl_window_is_toplevel (sink->window)) { g_clear_object (&sink->window); } else { /* remove buffer from surface, show nothing */ gst_wl_window_render (sink->window, NULL, NULL); } } break; case GST_STATE_CHANGE_READY_TO_NULL: g_mutex_lock (&sink->display_lock); /* If we had a toplevel window, we most likely have our own connection * to the display too, and it is a good idea to disconnect and allow * potentially the application to embed us with GstVideoOverlay * (which requires to re-use the same display connection as the parent * surface). If we didn't have a toplevel window, then the display * connection that we have is definitely shared with the application * and it's better to keep it around (together with the window handle) * to avoid requesting them again from the application if/when we are * restarted (GstVideoOverlay behaves like that in other sinks) */ if (sink->display && !sink->window) { /* -> the window was toplevel */ g_clear_object (&sink->display); } g_mutex_unlock (&sink->display_lock); g_clear_object (&sink->pool); break; default: break; } return ret; }
static void gst_wayland_sink_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { GstWaylandSink *sink = GST_WAYLAND_SINK (object); switch (prop_id) { case PROP_WAYLAND_DISPLAY: sink->display = g_value_get_pointer (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
static GstCaps * gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter) { GstWaylandSink *sink; GstCaps *caps; sink = GST_WAYLAND_SINK (bsink); caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink)); g_mutex_lock (&sink->display_lock); if (sink->display) { GValue list = G_VALUE_INIT; GValue value = G_VALUE_INIT; GArray *formats; gint i; enum wl_shm_format fmt; g_value_init (&list, GST_TYPE_LIST); g_value_init (&value, G_TYPE_STRING); formats = sink->display->shm_formats; for (i = 0; i < formats->len; i++) { fmt = g_array_index (formats, uint32_t, i); g_value_set_string (&value, gst_wl_shm_format_to_string (fmt)); gst_value_list_append_value (&list, &value); } caps = gst_caps_make_writable (caps); gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list); GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps); } g_mutex_unlock (&sink->display_lock); if (filter) { GstCaps *intersection; intersection = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST); gst_caps_unref (caps); caps = intersection; } return caps; }
static void gst_wayland_sink_expose (GstVideoOverlay * overlay) { GstWaylandSink *sink = GST_WAYLAND_SINK (overlay); g_return_if_fail (sink != NULL); GST_DEBUG_OBJECT (sink, "expose"); g_mutex_lock (&sink->render_lock); if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) { GST_DEBUG_OBJECT (sink, "redrawing last buffer"); render_last_buffer (sink); } g_mutex_unlock (&sink->render_lock); }
static GstFlowReturn gst_wayland_sink_buffer_alloc (GstBaseSink * bsink, guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf) { GstWaylandSink *sink = GST_WAYLAND_SINK (bsink); GstWlBuffer *buffer = NULL; GstFlowReturn ret = GST_FLOW_OK; GstStructure *structure = NULL; GstCaps *desired_caps = NULL; GST_LOG_OBJECT (sink, "a buffer of %u bytes was requested with caps " "%" GST_PTR_FORMAT " and offset %" G_GUINT64_FORMAT, size, caps, offset); desired_caps = gst_caps_copy (caps); structure = gst_caps_get_structure (desired_caps, 0); if (gst_structure_get_int (structure, "width", &sink->video_width) && gst_structure_get_int (structure, "height", &sink->video_height)) { sink->bpp = size / sink->video_width / sink->video_height; } g_mutex_lock (sink->pool_lock); while (sink->buffer_pool) { buffer = (GstWlBuffer *) sink->buffer_pool->data; if (buffer) { sink->buffer_pool = g_slist_delete_link (sink->buffer_pool, sink->buffer_pool); } else { break; } } g_mutex_unlock (sink->pool_lock); if (!buffer) buffer = wayland_buffer_create (sink); if (buffer) gst_buffer_set_caps (GST_BUFFER (buffer), caps); *buf = GST_BUFFER (buffer); gst_caps_unref (desired_caps); return ret; }
static void gst_wayland_sink_end_geometry_change (GstWaylandVideo * video) { GstWaylandSink *sink = GST_WAYLAND_SINK (video); g_return_if_fail (sink != NULL); g_mutex_lock (&sink->render_lock); if (!sink->window || !sink->window->area_subsurface) { g_mutex_unlock (&sink->render_lock); GST_INFO_OBJECT (sink, "end_geometry_change called without window, ignoring"); return; } wl_subsurface_set_desync (sink->window->area_subsurface); g_mutex_unlock (&sink->render_lock); }
static void gst_wayland_sink_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { GstWaylandSink *sink = GST_WAYLAND_SINK (object); switch (prop_id) { case PROP_DISPLAY: GST_OBJECT_LOCK (sink); g_value_set_string (value, sink->display_name); GST_OBJECT_UNLOCK (sink); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
static void gst_wayland_sink_set_context (GstElement * element, GstContext * context) { GstWaylandSink *sink = GST_WAYLAND_SINK (element); if (gst_context_has_context_type (context, GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) { g_mutex_lock (&sink->display_lock); if (G_LIKELY (!sink->display)) gst_wayland_sink_set_display_from_context (sink, context); else GST_WARNING_OBJECT (element, "changing display handle is not supported"); g_mutex_unlock (&sink->display_lock); } if (GST_ELEMENT_CLASS (parent_class)->set_context) GST_ELEMENT_CLASS (parent_class)->set_context (element, context); }
static void gst_wayland_sink_finalize (GObject * object) { GstWaylandSink *sink = GST_WAYLAND_SINK (object); GST_DEBUG_OBJECT (sink, "Finalizing the sink.."); if (sink->window) destroy_window (sink->window); if (sink->display) destroy_display (sink->display); if (sink->shm_pool) shm_pool_destroy (sink->shm_pool); g_mutex_clear (&sink->wayland_lock); G_OBJECT_CLASS (parent_class)->finalize (object); }
static GstCaps * gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter) { GstWaylandSink *sink; GstCaps *caps; sink = GST_WAYLAND_SINK (bsink); caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink)); if (filter) { GstCaps *intersection; intersection = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST); gst_caps_unref (caps); caps = intersection; } return caps; }
static gboolean gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query) { GstWaylandSink *sink = GST_WAYLAND_SINK (bsink); GstStructure *config; guint size, min_bufs, max_bufs; config = gst_buffer_pool_get_config (sink->pool); gst_buffer_pool_config_get_params (config, NULL, &size, &min_bufs, &max_bufs); /* we do have a pool for sure (created in set_caps), * so let's propose it anyway, but also propose the allocator on its own */ gst_query_add_allocation_pool (query, sink->pool, size, min_bufs, max_bufs); gst_query_add_allocation_param (query, gst_wl_shm_allocator_get (), NULL); gst_structure_free (config); return TRUE; }
static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay, gint x, gint y, gint w, gint h) { GstWaylandSink *sink = GST_WAYLAND_SINK (overlay); g_return_if_fail (sink != NULL); g_mutex_lock (&sink->render_lock); if (!sink->window) { g_mutex_unlock (&sink->render_lock); GST_WARNING_OBJECT (sink, "set_render_rectangle called without window, ignoring"); return; } GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d", x, y, w, h); gst_wl_window_set_render_rectangle (sink->window, x, y, w, h); g_mutex_unlock (&sink->render_lock); }
static void gst_wayland_sink_finalize (GObject * object) { GstWaylandSink *sink = GST_WAYLAND_SINK (object); GST_DEBUG_OBJECT (sink, "Finalizing the sink.."); if (sink->last_buffer) gst_buffer_unref (sink->last_buffer); if (sink->display) g_object_unref (sink->display); if (sink->window) g_object_unref (sink->window); if (sink->pool) gst_object_unref (sink->pool); g_free (sink->display_name); g_mutex_clear (&sink->display_lock); g_mutex_clear (&sink->render_lock); G_OBJECT_CLASS (parent_class)->finalize (object); }
static GstFlowReturn gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer) { GstWaylandSink *sink = GST_WAYLAND_SINK (bsink); GstBuffer *to_render; GstWlMeta *meta; GstFlowReturn ret = GST_FLOW_OK; g_mutex_lock (&sink->render_lock); GST_LOG_OBJECT (sink, "render buffer %p", buffer); if (G_UNLIKELY (!sink->window)) { /* ask for window handle. Unlock render_lock while doing that because * set_window_handle & friends will lock it in this context */ g_mutex_unlock (&sink->render_lock); gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink)); g_mutex_lock (&sink->render_lock); if (sink->window) { /* inform the window about our caps */ gst_wl_window_set_video_info (sink->window, &sink->video_info); } else { /* if we were not provided a window, create one ourselves */ sink->window = gst_wl_window_new_toplevel (sink->display, &sink->video_info); } sink->video_info_changed = FALSE; } /* drop buffers until we get a frame callback */ if (g_atomic_int_get (&sink->redraw_pending) == TRUE) goto done; if (G_UNLIKELY (sink->video_info_changed)) { gst_wl_window_set_video_info (sink->window, &sink->video_info); sink->video_info_changed = FALSE; } /* now that we have for sure set the video info on the window, it must have * a valid size, otherwise this means that the application has called * set_window_handle() without calling set_render_rectangle(), which is * absolutely necessary for us. */ if (G_UNLIKELY (sink->window->surface_width == 0 || sink->window->surface_height == 0)) goto no_window_size; meta = gst_buffer_get_wl_meta (buffer); if (meta && meta->pool->display == sink->display) { GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer); to_render = buffer; } else { GstMapInfo src; GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer); if (!sink->pool) goto no_pool; if (!gst_buffer_pool_set_active (sink->pool, TRUE)) goto activate_failed; ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL); if (ret != GST_FLOW_OK) goto no_buffer; gst_buffer_map (buffer, &src, GST_MAP_READ); gst_buffer_fill (to_render, 0, src.data, src.size); gst_buffer_unmap (buffer, &src); } gst_buffer_replace (&sink->last_buffer, to_render); render_last_buffer (sink); if (buffer != to_render) gst_buffer_unref (to_render); goto done; no_window_size: { GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, ("Window has no size set"), ("Make sure you set the size after calling set_window_handle")); ret = GST_FLOW_ERROR; goto done; } no_buffer: { GST_WARNING_OBJECT (sink, "could not create image"); goto done; } no_pool: { GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, ("Internal error: can't allocate images"), ("We don't have a bufferpool negotiated")); ret = GST_FLOW_ERROR; goto done; } activate_failed: { GST_ERROR_OBJECT (sink, "failed to activate bufferpool."); ret = GST_FLOW_ERROR; goto done; } done: { g_mutex_unlock (&sink->render_lock); return ret; } }
static GstFlowReturn gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer) { GstWaylandSink *sink = GST_WAYLAND_SINK (bsink); GstBuffer *to_render; GstWlBuffer *wlbuffer; GstFlowReturn ret = GST_FLOW_OK; g_mutex_lock (&sink->render_lock); GST_LOG_OBJECT (sink, "render buffer %p", buffer); if (G_UNLIKELY (!sink->window)) { /* ask for window handle. Unlock render_lock while doing that because * set_window_handle & friends will lock it in this context */ g_mutex_unlock (&sink->render_lock); gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink)); g_mutex_lock (&sink->render_lock); if (!sink->window) { /* if we were not provided a window, create one ourselves */ sink->window = gst_wl_window_new_toplevel (sink->display, &sink->video_info); } } /* drop buffers until we get a frame callback */ if (g_atomic_int_get (&sink->redraw_pending) == TRUE) goto done; /* make sure that the application has called set_render_rectangle() */ if (G_UNLIKELY (sink->window->render_rectangle.w == 0)) goto no_window_size; wlbuffer = gst_buffer_get_wl_buffer (buffer); if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) { GST_LOG_OBJECT (sink, "buffer %p has a wl_buffer from our display, " "writing directly", buffer); to_render = buffer; } else { GstMemory *mem; struct wl_buffer *wbuf = NULL; GST_LOG_OBJECT (sink, "buffer %p does not have a wl_buffer from our " "display, creating it", buffer); mem = gst_buffer_peek_memory (buffer, 0); if (gst_is_wl_shm_memory (mem)) { wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display, &sink->video_info); } if (wbuf) { gst_buffer_add_wl_buffer (buffer, wbuf, sink->display); to_render = buffer; } else { GstMapInfo src; /* we don't know how to create a wl_buffer directly from the provided * memory, so we have to copy the data to a memory that we know how * to handle... */ GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, " "copying to wl_shm memory", buffer); /* sink->pool always exists (created in set_caps), but it may not * be active if upstream is not using it */ if (!gst_buffer_pool_is_active (sink->pool) && !gst_buffer_pool_set_active (sink->pool, TRUE)) goto activate_failed; ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL); if (ret != GST_FLOW_OK) goto no_buffer; /* the first time we acquire a buffer, * we need to attach a wl_buffer on it */ wlbuffer = gst_buffer_get_wl_buffer (to_render); if (G_UNLIKELY (!wlbuffer)) { mem = gst_buffer_peek_memory (to_render, 0); wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display, &sink->video_info); if (G_UNLIKELY (!wbuf)) goto no_wl_buffer; gst_buffer_add_wl_buffer (to_render, wbuf, sink->display); } gst_buffer_map (buffer, &src, GST_MAP_READ); gst_buffer_fill (to_render, 0, src.data, src.size); gst_buffer_unmap (buffer, &src); } } /* drop double rendering */ if (G_UNLIKELY (to_render == sink->last_buffer)) { GST_LOG_OBJECT (sink, "Buffer already being rendered"); goto done; } gst_buffer_replace (&sink->last_buffer, to_render); render_last_buffer (sink); if (buffer != to_render) gst_buffer_unref (to_render); goto done; no_window_size: { GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, ("Window has no size set"), ("Make sure you set the size after calling set_window_handle")); ret = GST_FLOW_ERROR; goto done; } no_buffer: { GST_WARNING_OBJECT (sink, "could not create buffer"); goto done; } no_wl_buffer: { GST_ERROR_OBJECT (sink, "could not create wl_buffer out of wl_shm memory"); ret = GST_FLOW_ERROR; goto done; } activate_failed: { GST_ERROR_OBJECT (sink, "failed to activate bufferpool."); ret = GST_FLOW_ERROR; goto done; } done: { g_mutex_unlock (&sink->render_lock); return ret; } }
static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps) { GstWaylandSink *sink; GstBufferPool *newpool; GstVideoInfo info; enum wl_shm_format format; GArray *formats; gint i; GstStructure *structure; sink = GST_WAYLAND_SINK (bsink); GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps); /* extract info from caps */ if (!gst_video_info_from_caps (&info, caps)) goto invalid_format; format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (&info)); if ((gint) format == -1) goto invalid_format; /* verify we support the requested format */ formats = sink->display->shm_formats; for (i = 0; i < formats->len; i++) { if (g_array_index (formats, uint32_t, i) == format) break; } if (i >= formats->len) goto unsupported_format; /* create a new pool for the new configuration */ newpool = gst_video_buffer_pool_new (); if (!newpool) goto pool_failed; structure = gst_buffer_pool_get_config (newpool); gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0); gst_buffer_pool_config_set_allocator (structure, gst_wl_shm_allocator_get (), NULL); if (!gst_buffer_pool_set_config (newpool, structure)) goto config_failed; /* store the video info */ sink->video_info = info; sink->video_info_changed = TRUE; gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool); gst_object_unref (newpool); return TRUE; invalid_format: { GST_DEBUG_OBJECT (sink, "Could not locate image format from caps %" GST_PTR_FORMAT, caps); return FALSE; } unsupported_format: { GST_DEBUG_OBJECT (sink, "Format %s is not available on the display", gst_wl_shm_format_to_string (format)); return FALSE; } pool_failed: { GST_DEBUG_OBJECT (sink, "Failed to create new pool"); return FALSE; } config_failed: { GST_DEBUG_OBJECT (bsink, "failed setting config"); gst_object_unref (newpool); return FALSE; } }
static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element, GstStateChange transition) { GstWaylandSink *sink = GST_WAYLAND_SINK (element); GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; switch (transition) { case GST_STATE_CHANGE_NULL_TO_READY: if (!gst_wayland_sink_find_display (sink)) return GST_STATE_CHANGE_FAILURE; break; default: break; } ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); if (ret == GST_STATE_CHANGE_FAILURE) return ret; switch (transition) { case GST_STATE_CHANGE_PAUSED_TO_READY: gst_buffer_replace (&sink->last_buffer, NULL); if (sink->window) { if (gst_wl_window_is_toplevel (sink->window)) { g_clear_object (&sink->window); } else { /* remove buffer from surface, show nothing */ wl_surface_attach (sink->window->surface, NULL, 0, 0); wl_surface_damage (sink->window->surface, 0, 0, sink->window->surface_width, sink->window->surface_height); wl_surface_commit (sink->window->surface); wl_display_flush (sink->display->display); } } break; case GST_STATE_CHANGE_READY_TO_NULL: g_mutex_lock (&sink->display_lock); /* If we had a toplevel window, we most likely have our own connection * to the display too, and it is a good idea to disconnect and allow * potentially the application to embed us with GstVideoOverlay * (which requires to re-use the same display connection as the parent * surface). If we didn't have a toplevel window, then the display * connection that we have is definitely shared with the application * and it's better to keep it around (together with the window handle) * to avoid requesting them again from the application if/when we are * restarted (GstVideoOverlay behaves like that in other sinks) */ if (sink->display && !sink->window) { /* -> the window was toplevel */ /* Force all buffers to return to the pool, regardless of * whether the compositor has released them or not. We are * going to kill the display, so we need to return all buffers * to be destroyed before this happens. * Note that this is done here instead of the pool destructor * because the buffers hold a reference to the pool. Also, * the buffers can only be unref'ed from the display's event loop * and the pool holds a reference to the display. If we drop * our references here, when the compositor releases the buffers, * they will be unref'ed from the event loop thread, which will * unref the pool and therefore the display, which will try to * stop the thread from within itself and cause a deadlock. */ if (sink->pool) { gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL (sink->pool)); } g_clear_object (&sink->display); g_clear_object (&sink->pool); } g_mutex_unlock (&sink->display_lock); break; default: break; } return ret; }
static gboolean gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query) { GstWaylandSink *sink = GST_WAYLAND_SINK (bsink); GstBufferPool *pool; GstStructure *config; GstCaps *caps; guint size; gboolean need_pool; gst_query_parse_allocation (query, &caps, &need_pool); if (caps == NULL) goto no_caps; g_mutex_lock (&sink->wayland_lock); if ((pool = sink->pool)) gst_object_ref (pool); g_mutex_unlock (&sink->wayland_lock); if (pool != NULL) { GstCaps *pcaps; /* we had a pool, check caps */ config = gst_buffer_pool_get_config (pool); gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL); if (!gst_caps_is_equal (caps, pcaps)) { /* different caps, we can't use this pool */ gst_object_unref (pool); pool = NULL; } gst_structure_free (config); } if (pool == NULL && need_pool) { GstVideoInfo info; if (!gst_video_info_from_caps (&info, caps)) goto invalid_caps; GST_DEBUG_OBJECT (sink, "create new pool"); pool = gst_wayland_buffer_pool_new (sink); /* the normal size of a frame */ size = info.size; config = gst_buffer_pool_get_config (pool); gst_buffer_pool_config_set_params (config, caps, size, 2, 0); if (!gst_buffer_pool_set_config (pool, config)) goto config_failed; } if (pool) { gst_query_add_allocation_pool (query, pool, size, 2, 0); gst_object_unref (pool); } return TRUE; /* ERRORS */ no_caps: { GST_DEBUG_OBJECT (bsink, "no caps specified"); return FALSE; } invalid_caps: { GST_DEBUG_OBJECT (bsink, "invalid caps specified"); return FALSE; } config_failed: { GST_DEBUG_OBJECT (bsink, "failed setting config"); gst_object_unref (pool); return FALSE; } }
static GstFlowReturn gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer) { GstWaylandSink *sink = GST_WAYLAND_SINK (bsink); GstVideoRectangle src, dst, res; GstBuffer *to_render; GstWlMeta *meta; GstFlowReturn ret; struct window *window; struct display *display; GST_LOG_OBJECT (sink, "render buffer %p", buffer); if (!sink->window) create_window (sink, sink->display, sink->video_width, sink->video_height); window = sink->window; display = sink->display; meta = gst_buffer_get_wl_meta (buffer); if (window->redraw_pending) { wl_display_dispatch (display->display); } if (meta && meta->sink == sink) { GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer); to_render = buffer; } else { GstMapInfo src; GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer); if (!sink->pool) goto no_pool; if (!gst_buffer_pool_set_active (sink->pool, TRUE)) goto activate_failed; ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL); if (ret != GST_FLOW_OK) goto no_buffer; gst_buffer_map (buffer, &src, GST_MAP_READ); gst_buffer_fill (to_render, 0, src.data, src.size); gst_buffer_unmap (buffer, &src); meta = gst_buffer_get_wl_meta (to_render); } src.w = sink->video_width; src.h = sink->video_height; dst.w = sink->window->width; dst.h = sink->window->height; gst_video_sink_center_rect (src, dst, &res, FALSE); wl_surface_attach (sink->window->surface, meta->wbuffer, 0, 0); wl_surface_damage (sink->window->surface, 0, 0, res.w, res.h); window->redraw_pending = TRUE; window->callback = wl_surface_frame (window->surface); wl_callback_add_listener (window->callback, &frame_callback_listener, window); wl_surface_commit (window->surface); wl_display_dispatch (display->display); if (buffer != to_render) gst_buffer_unref (to_render); return GST_FLOW_OK; no_buffer: { GST_WARNING_OBJECT (sink, "could not create image"); return ret; } no_pool: { GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, ("Internal error: can't allocate images"), ("We don't have a bufferpool negotiated")); return GST_FLOW_ERROR; } activate_failed: { GST_ERROR_OBJECT (sink, "failed to activate bufferpool."); ret = GST_FLOW_ERROR; return ret; } }
static GstFlowReturn gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer) { GstWaylandSink *sink = GST_WAYLAND_SINK (bsink); gboolean mem_cpy = TRUE; GstVideoRectangle src, dst, res; GST_LOG_OBJECT (sink, "render buffer %p, data = %p, timestamp = %" GST_TIME_FORMAT, buffer, GST_BUFFER_DATA (buffer), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer))); if (!sink->window) create_window (sink, sink->display, sink->video_width, sink->video_height); if (sink->render_finish) { if (GST_IS_WLBUFFER (buffer)) { GstWlBuffer *tmp_buffer = (GstWlBuffer *) buffer; /* Does it have a waylandbuffer ? */ if (tmp_buffer->wbuffer) { mem_cpy = FALSE; GST_DEBUG_OBJECT (sink, "we have a buffer (%p) we allocated " "ourselves and it has a wayland buffer, no memcpy then", buffer); sink->window->buffer = tmp_buffer->wbuffer; } else { /* No wayland buffer, that's a malloc */ GST_DEBUG_OBJECT (sink, "we have a buffer (%p) we allocated " "ourselves but it does not hold a wayland buffer", buffer); } } else { /* Not our baby! */ GST_DEBUG_OBJECT (sink, "we have a buffer (%p) we did not allocate", buffer); } if (mem_cpy) { GstWlBuffer *wlbuf = wayland_buffer_create (sink); memcpy (GST_BUFFER_DATA (wlbuf), GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer)); sink->window->buffer = wlbuf->wbuffer; } src.w = sink->video_width; src.h = sink->video_height; dst.w = sink->window->width; dst.h = sink->window->height; gst_video_sink_center_rect (src, dst, &res, FALSE); sink->render_finish = FALSE; wl_surface_attach (sink->window->surface, sink->window->buffer, 0, 0); wl_surface_damage (sink->window->surface, 0, 0, res.w, res.h); if (sink->callback) wl_callback_destroy (sink->callback); sink->callback = wl_surface_frame (sink->window->surface); wl_callback_add_listener (sink->callback, &frame_listener, sink); wl_surface_commit (sink->window->surface); } else GST_LOG_OBJECT (sink, "Waiting to get the signal from compositor to render the next frame.."); wl_display_dispatch (sink->display->display); return GST_FLOW_OK; }