4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Sangchul Lee <sc11.lee@samsung.com>
8 * This library is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the
10 * Free Software Foundation; either version 2.1 of the License, or (at your option)
13 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 * License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software Foundation, Inc., 51
20 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * SECTION:element-evasimagesink
26 * Gstreamer Evas Video Sink - draw video on the given Evas Image Object
33 #include <sys/types.h>
35 #include <gst/video/video.h>
36 #include <gst/video/gstvideosink.h>
41 #include "gstevasimagesink.h"
43 #define CAP_WIDTH "width"
44 #define CAP_HEIGHT "height"
46 GST_DEBUG_CATEGORY_STATIC (gst_evas_image_sink_debug);
47 #define GST_CAT_DEFAULT gst_evas_image_sink_debug
69 #define GL_X11_ENGINE "gl_x11"
70 #define DO_RENDER_FROM_FIMC 1
71 #define SIZE_FOR_UPDATE_VISIBILITY sizeof(gchar)
73 #define EVASIMAGESINK_SET_EVAS_OBJECT_EVENT_CALLBACK( x_evas_image_object, x_usr_data ) \
76 if (x_evas_image_object) { \
77 evas_object_event_callback_add (x_evas_image_object, EVAS_CALLBACK_DEL, evas_callback_del_event, x_usr_data); \
78 evas_object_event_callback_add (x_evas_image_object, EVAS_CALLBACK_RESIZE, evas_callback_resize_event, x_usr_data); \
82 #define EVASIMAGESINK_UNSET_EVAS_OBJECT_EVENT_CALLBACK( x_evas_image_object ) \
85 if (x_evas_image_object) { \
86 evas_object_event_callback_del (x_evas_image_object, EVAS_CALLBACK_DEL, evas_callback_del_event); \
87 evas_object_event_callback_del (x_evas_image_object, EVAS_CALLBACK_RESIZE, evas_callback_resize_event); \
91 GMutex *instance_lock;
92 guint instance_lock_count;
94 static inline gboolean
95 is_evas_image_object (Evas_Object *obj)
101 type = evas_object_type_get (obj);
105 if (strcmp (type, "image") == 0) {
111 /* the capabilities of the inputs.
115 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
118 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx));
120 GST_BOILERPLATE (GstEvasImageSink, gst_evas_image_sink, GstVideoSink, GST_TYPE_VIDEO_SINK);
122 static void gst_evas_image_sink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
123 static void gst_evas_image_sink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
124 static gboolean gst_evas_image_sink_set_caps (GstBaseSink *base_sink, GstCaps *caps);
125 static GstFlowReturn gst_evas_image_sink_show_frame (GstVideoSink *video_sink, GstBuffer *buf);
126 static gboolean gst_evas_image_sink_event (GstBaseSink *sink, GstEvent *event);
127 static GstStateChangeReturn gst_evas_image_sink_change_state (GstElement *element, GstStateChange transition);
128 static void evas_callback_del_event (void *data, Evas *e, Evas_Object *obj, void *event_info);
129 static void evas_callback_resize_event (void *data, Evas *e, Evas_Object *obj, void *event_info);
132 gst_evas_image_sink_base_init (gpointer gclass)
134 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
136 gst_element_class_set_details_simple (element_class,
139 "Video sink element for evas image object",
140 "Samsung Electronics <www.samsung.com>");
142 gst_element_class_add_pad_template (element_class,
143 gst_static_pad_template_get (&sink_factory));
147 gst_evas_image_sink_class_init (GstEvasImageSinkClass *klass)
149 GObjectClass *gobject_class;
150 GstBaseSinkClass *gstbasesink_class;
151 GstVideoSinkClass *gstvideosink_class;
152 GstElementClass *gstelement_class;
154 gobject_class = (GObjectClass *) klass;
155 gstbasesink_class = GST_BASE_SINK_CLASS (klass);
156 gstvideosink_class = GST_VIDEO_SINK_CLASS (klass);
157 gstelement_class = (GstElementClass *) klass;
159 gobject_class->set_property = gst_evas_image_sink_set_property;
160 gobject_class->get_property = gst_evas_image_sink_get_property;
162 g_object_class_install_property (gobject_class, PROP_EVAS_OBJECT,
163 g_param_spec_pointer ("evas-object", "Destination Evas Object", "Destination evas image object", G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
164 g_object_class_install_property (gobject_class, PROP_EVAS_OBJECT_SHOW,
165 g_param_spec_boolean ("visible", "Show Evas Object", "When disabled, evas object does not show", TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167 gstvideosink_class->show_frame = GST_DEBUG_FUNCPTR (gst_evas_image_sink_show_frame);
168 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_evas_image_sink_set_caps);
169 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_evas_image_sink_event);
170 gstelement_class->change_state = GST_DEBUG_FUNCPTR(gst_evas_image_sink_change_state);
174 gst_evas_image_sink_fini (gpointer data, GObject *obj)
176 GST_DEBUG ("[ENTER]");
178 GstEvasImageSink *esink = GST_EVASIMAGESINK (obj);
183 gst_buffer_unref (esink->oldbuf);
187 evas_object_image_data_set(esink->eo, NULL);
190 g_mutex_lock (instance_lock);
191 instance_lock_count--;
192 g_mutex_unlock (instance_lock);
193 if (instance_lock_count == 0) {
194 g_mutex_free (instance_lock);
195 instance_lock = NULL;
198 GST_DEBUG ("[LEAVE]");
202 evas_image_sink_cb_pipe (void *data, void *buffer, unsigned int nbyte)
205 GstEvasImageSink *esink = data;
208 GST_DEBUG ("[ENTER]");
210 if (!esink || !esink->eo) {
213 if (nbyte == SIZE_FOR_UPDATE_VISIBILITY) {
214 if(!esink->object_show) {
215 evas_object_hide(esink->eo);
216 GST_INFO ("object hide..");
218 evas_object_show(esink->eo);
219 GST_INFO ("object show..");
221 GST_DEBUG ("[LEAVE]");
224 if (!buffer || nbyte != sizeof (GstBuffer *)) {
228 memcpy (&buf, buffer, sizeof (GstBuffer *));
230 GST_ERROR ("There is no buffer");
233 if (esink->present_data_addr == -1) {
234 /* if present_data_addr is -1, we don't use this member variable */
235 } else if (esink->present_data_addr != DO_RENDER_FROM_FIMC) {
236 GST_WARNING ("skip rendering this buffer, present_data_addr:%d, DO_RENDER_FROM_FIMC:%d", esink->present_data_addr, DO_RENDER_FROM_FIMC);
240 MMTA_ACUM_ITEM_BEGIN("eavsimagesink _cb_pipe total", FALSE);
242 if ( !esink->is_evas_object_size_set && esink->w > 0 && esink->h > 0) {
243 evas_object_image_size_set (esink->eo, esink->w, esink->h);
244 GST_DEBUG("evas_object_image_size_set(), width(%d),height(%d)",esink->w,esink->h);
245 esink->is_evas_object_size_set = TRUE;
247 if (esink->gl_zerocopy) {
248 img_data = evas_object_image_data_get (esink->eo, EINA_TRUE);
249 if (!img_data || !GST_BUFFER_DATA(buf)) {
250 GST_WARNING ("Cannot get image data from evas object or cannot get gstbuffer data");
251 evas_object_image_data_set(esink->eo, img_data);
253 GST_DEBUG ("img_data(%x), GST_BUFFER_DATA(buf):%x, esink->w(%d),esink->h(%d), esink->eo(%x)",img_data,GST_BUFFER_DATA(buf),esink->w,esink->h,esink->eo);
254 __ta__("evasimagesink memcpy in _cb_pipe", memcpy (img_data, GST_BUFFER_DATA (buf), esink->w * esink->h * COLOR_DEPTH););
255 evas_object_image_pixels_dirty_set (esink->eo, 1);
256 evas_object_image_data_set(esink->eo, img_data);
258 gst_buffer_unref (buf);
260 GST_DEBUG ("GST_BUFFER_DATA(buf):%x, esink->eo(%x)",GST_BUFFER_DATA(buf),esink->eo);
261 evas_object_image_data_set (esink->eo, GST_BUFFER_DATA (buf));
262 evas_object_image_pixels_dirty_set (esink->eo, 1);
264 gst_buffer_unref(esink->oldbuf);
269 MMTA_ACUM_ITEM_END("eavsimagesink _cb_pipe total", FALSE);
271 GST_DEBUG ("[LEAVE]");
275 gst_evas_image_sink_init (GstEvasImageSink *esink, GstEvasImageSinkClass *gclass)
277 GST_DEBUG ("[ENTER]");
281 esink->object_show = FALSE;
282 esink->update_visibility = UPDATE_FALSE;
283 esink->gl_zerocopy = FALSE;
284 esink->is_evas_object_size_set = FALSE;
285 esink->present_data_addr = -1;
288 instance_lock = g_mutex_new();
290 g_mutex_lock (instance_lock);
291 instance_lock_count++;
292 g_mutex_unlock (instance_lock);
294 g_object_weak_ref (G_OBJECT (esink), gst_evas_image_sink_fini, NULL);
296 GST_DEBUG ("[LEAVE]");
300 evas_callback_del_event (void *data, Evas *e, Evas_Object *obj, void *event_info)
302 GST_DEBUG ("[ENTER]");
304 GstEvasImageSink *esink = data;
309 EVASIMAGESINK_UNSET_EVAS_OBJECT_EVENT_CALLBACK (esink->eo);
311 gst_buffer_unref (esink->oldbuf);
312 esink->oldbuf = NULL;
316 evas_object_image_data_set(esink->eo, NULL);
320 GST_DEBUG ("[LEAVE]");
324 evas_callback_resize_event (void *data, Evas *e, Evas_Object *obj, void *event_info)
329 GST_DEBUG ("[ENTER]");
331 GstEvasImageSink *esink = data;
336 evas_object_geometry_get(esink->eo, NULL, NULL, &w, &h);
338 GST_WARNING ("evas object size (w:%d,h:%d) was not set",w,h);
340 evas_object_image_fill_set(esink->eo, 0, 0, w, h);
341 GST_DEBUG ("evas object fill set (w:%d,h:%d)",w,h);
344 GST_DEBUG ("[LEAVE]");
348 evas_image_sink_get_size_from_caps (GstCaps *caps, int *w, int *h)
354 if (!caps || !w || !h) {
357 s = gst_caps_get_structure (caps, 0);
362 r = gst_structure_get_int (s, CAP_WIDTH, &width);
367 r = gst_structure_get_int (s, CAP_HEIGHT, &height);
378 is_zerocopy_supported (Evas *e)
380 Eina_List *engines, *l;
389 engines = evas_render_method_list ();
394 cur_id = evas_output_method_get (e);
396 EINA_LIST_FOREACH (engines, l, name) {
397 id = evas_render_method_lookup (name);
398 if (name && id == cur_id) {
399 if (!strcmp (name, GL_X11_ENGINE)) {
409 evas_image_sink_event_parse_data (GstEvasImageSink *esink, GstEvent *event)
411 const GstStructure *st;
412 guint st_data_addr = 0;
413 gint st_data_width = 0;
414 gint st_data_height = 0;
416 g_return_val_if_fail (event != NULL, FALSE);
417 g_return_val_if_fail (esink != NULL, FALSE);
419 if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM_OOB) {
420 GST_WARNING ("it's not a custom downstream oob event");
423 st = gst_event_get_structure (event);
424 if (st == NULL || !gst_structure_has_name (st, "GstStructureForCustomEvent")) {
425 GST_WARNING ("structure in a given event is not proper");
428 if (!gst_structure_get_uint (st, "data-addr", &st_data_addr)) {
429 GST_WARNING ("parsing data-addr failed");
432 esink->present_data_addr = st_data_addr;
438 gst_evas_image_sink_event (GstBaseSink *sink, GstEvent *event)
440 GstEvasImageSink *esink = GST_EVASIMAGESINK (sink);
444 switch (GST_EVENT_TYPE (event)) {
445 case GST_EVENT_FLUSH_START:
446 GST_DEBUG ("GST_EVENT_FLUSH_START");
448 case GST_EVENT_FLUSH_STOP:
449 GST_DEBUG ("GST_EVENT_FLUSH_STOP");
452 GST_DEBUG ("GST_EVENT_EOS");
454 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
455 if(!evas_image_sink_event_parse_data(esink, event)) {
456 GST_DEBUG ("GST_EVENT_CUSTOM_DOWNSTREAM_OOB, present_data_addr:%x",esink->present_data_addr);
458 GST_ERROR ("evas_image_sink_event_parse_data() failed");
464 if (GST_BASE_SINK_CLASS (parent_class)->event) {
465 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
471 static GstStateChangeReturn
472 gst_evas_image_sink_change_state (GstElement *element, GstStateChange transition)
474 GstStateChangeReturn ret_state = GST_STATE_CHANGE_SUCCESS;
475 GstEvasImageSink *esink = NULL;
476 esink = GST_EVASIMAGESINK(element);
480 GST_ERROR("can not get evasimagesink from element");
481 return GST_STATE_CHANGE_FAILURE;
484 switch (transition) {
485 case GST_STATE_CHANGE_NULL_TO_READY:
486 GST_INFO ("*** STATE_CHANGE_NULL_TO_READY ***");
488 case GST_STATE_CHANGE_READY_TO_PAUSED:
489 GST_INFO ("*** STATE_CHANGE_READY_TO_PAUSED ***");
491 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
492 GST_INFO ("*** STATE_CHANGE_PAUSED_TO_PLAYING ***");
498 ret_state = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
500 switch (transition) {
501 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
502 GST_INFO ("*** STATE_CHANGE_PLAYING_TO_PAUSED ***");
504 case GST_STATE_CHANGE_PAUSED_TO_READY:
505 GST_INFO ("*** STATE_CHANGE_PAUSED_TO_READY ***");
507 case GST_STATE_CHANGE_READY_TO_NULL:
508 GST_INFO ("*** STATE_CHANGE_READY_TO_NULL ***");
509 EVASIMAGESINK_UNSET_EVAS_OBJECT_EVENT_CALLBACK (esink->eo);
511 ecore_pipe_del (esink->epipe);
523 gst_evas_image_sink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
525 GstEvasImageSink *esink = GST_EVASIMAGESINK (object);
528 g_mutex_lock (instance_lock);
531 case PROP_EVAS_OBJECT:
532 eo = g_value_get_pointer (value);
533 if (is_evas_image_object (eo)) {
534 if (eo != esink->eo) {
537 /* delete evas object callbacks registrated on a previous evas image object */
538 EVASIMAGESINK_UNSET_EVAS_OBJECT_EVENT_CALLBACK (esink->eo);
540 /* add evas object callbacks on a new evas image object */
541 EVASIMAGESINK_SET_EVAS_OBJECT_EVENT_CALLBACK (esink->eo, esink);
543 esink->gl_zerocopy = is_zerocopy_supported (evas_object_evas_get (eo));
544 if (esink->gl_zerocopy) {
545 evas_object_image_content_hint_set (esink->eo, EVAS_IMAGE_CONTENT_HINT_DYNAMIC);
546 GST_DEBUG("Enable gl zerocopy");
548 GST_DEBUG("Evas Image Object(%x) is set",esink->eo);
549 esink->is_evas_object_size_set = FALSE;
551 esink->epipe = ecore_pipe_add (evas_image_sink_cb_pipe, esink);
553 GST_ERROR ("ecore-pipe create failed");
559 GST_ERROR ("Cannot set evas-object property: value is not an evas image object");
563 case PROP_EVAS_OBJECT_SHOW:
566 esink->object_show = g_value_get_boolean (value);
567 if( !is_evas_image_object(esink->eo) ) {
568 GST_WARNING ("Cannot apply visible(show-object) property: cannot get an evas object\n");
571 esink->update_visibility = UPDATE_TRUE;
573 esink->epipe = ecore_pipe_add (evas_image_sink_cb_pipe, esink);
575 GST_ERROR ("ecore-pipe create failed");
579 r = ecore_pipe_write (esink->epipe, &esink->update_visibility, SIZE_FOR_UPDATE_VISIBILITY);
580 if (r == EINA_FALSE) {
581 GST_WARNING ("Failed to ecore_pipe_write() for updating visibility)\n");
586 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
590 g_mutex_unlock (instance_lock);
594 gst_evas_image_sink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
596 GstEvasImageSink *esink = GST_EVASIMAGESINK (object);
599 case PROP_EVAS_OBJECT:
600 g_value_set_pointer (value, esink->eo);
602 case PROP_EVAS_OBJECT_SHOW:
603 g_value_set_boolean (value, esink->object_show);
606 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
612 gst_evas_image_sink_set_caps (GstBaseSink *base_sink, GstCaps *caps)
616 GstEvasImageSink *esink = GST_EVASIMAGESINK (base_sink);
618 esink->is_evas_object_size_set = FALSE;
619 r = evas_image_sink_get_size_from_caps (caps, &w, &h);
623 GST_DEBUG ("set size w(%d), h(%d)", w, h);
629 gst_evas_image_sink_show_frame (GstVideoSink *video_sink, GstBuffer *buf)
631 GstEvasImageSink *esink = GST_EVASIMAGESINK (video_sink);
634 g_mutex_lock (instance_lock);
635 if (esink->present_data_addr == -1) {
636 /* if present_data_addr is -1, we don't use this member variable */
637 } else if (esink->present_data_addr != DO_RENDER_FROM_FIMC) {
638 GST_WARNING ("skip rendering this buffer, present_data_addr:%d, DO_RENDER_FROM_FIMC:%d", esink->present_data_addr, DO_RENDER_FROM_FIMC);
639 g_mutex_unlock (instance_lock);
643 esink->epipe = ecore_pipe_add (evas_image_sink_cb_pipe, esink);
645 GST_ERROR ("ecore-pipe create failed");
646 g_mutex_unlock (instance_lock);
647 return GST_FLOW_ERROR;
651 gst_buffer_ref (buf);
652 __ta__("evasimagesink ecore_pipe_write", r = ecore_pipe_write (esink->epipe, &buf, sizeof (GstBuffer *)););
653 if (r == EINA_FALSE) {
654 gst_buffer_unref (buf);
656 GST_DEBUG ("ecore_pipe_write() was called with GST_BUFFER_DATA(buf):%x", GST_BUFFER_DATA(buf));
658 g_mutex_unlock (instance_lock);
663 evas_image_sink_init (GstPlugin *evasimagesink)
665 GST_DEBUG_CATEGORY_INIT (gst_evas_image_sink_debug, "evasimagesink", 0, "Evas image object based videosink");
667 return gst_element_register (evasimagesink, "evasimagesink", GST_RANK_NONE, GST_TYPE_EVASIMAGESINK);
671 #define PACKAGE "gstevasimagesink-plugin-package"
678 "Evas image object based videosink",
679 evas_image_sink_init,
682 "Samsung Electronics Co",
683 "http://www.samsung.com/"