3 #include <gst/video/video.h>
4 #include <gst/video/gstvideosink.h>
8 #include "emotion_gstreamer.h"
10 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE("sink",
11 GST_PAD_SINK, GST_PAD_ALWAYS,
12 GST_STATIC_CAPS(GST_VIDEO_CAPS_YUV("{ I420, YV12, YUY2 }") ";"
13 GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_BGR ";" GST_VIDEO_CAPS_BGRA));
15 GST_DEBUG_CATEGORY_STATIC(evas_video_sink_debug);
16 #define GST_CAT_DEFAULT evas_video_sink_debug
31 static guint evas_video_sink_signals[LAST_SIGNAL] = { 0, };
33 struct _EvasVideoSinkPrivate {
39 Evas_Colorspace eformat;
40 GstVideoFormat gformat;
45 GstBuffer *last_buffer; /* We need to keep a copy of the last inserted buffer as evas doesn't copy YUV data around */
47 // If this is TRUE all processing should finish ASAP
48 // This is necessary because there could be a race between
49 // unlock() and render(), where unlock() wins, signals the
50 // GCond, then render() tries to render a frame although
51 // everything else isn't running anymore. This will lead
52 // to deadlocks because render() holds the stream lock.
54 // Protected by the buffer mutex
55 Eina_Bool unlocked : 1;
56 Eina_Bool preroll : 1;
59 #define _do_init(bla) \
60 GST_DEBUG_CATEGORY_INIT(evas_video_sink_debug, \
65 GST_BOILERPLATE_FULL(EvasVideoSink,
72 static void unlock_buffer_mutex(EvasVideoSinkPrivate* priv);
74 static void evas_video_sink_render_handler(void *data, void *buf, unsigned int len);
77 evas_video_sink_base_init(gpointer g_class)
79 GstElementClass* element_class;
81 element_class = GST_ELEMENT_CLASS(g_class);
82 gst_element_class_add_pad_template(element_class, gst_static_pad_template_get(&sinktemplate));
83 gst_element_class_set_details_simple(element_class, "Evas video sink",
84 "Sink/Video", "Sends video data from a GStreamer pipeline to an Evas object",
85 "Vincent Torri <vtorri@univ-evry.fr>");
89 evas_video_sink_init(EvasVideoSink* sink, EvasVideoSinkClass* klass __UNUSED__)
91 EvasVideoSinkPrivate* priv;
94 sink->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE(sink, EVAS_TYPE_VIDEO_SINK, EvasVideoSinkPrivate);
96 priv->p = ecore_pipe_add(evas_video_sink_render_handler, sink);
97 priv->last_buffer = NULL;
100 priv->gformat = GST_VIDEO_FORMAT_UNKNOWN;
101 priv->eformat = EVAS_COLORSPACE_ARGB8888;
102 priv->data_cond = g_cond_new();
103 priv->buffer_mutex = g_mutex_new();
104 priv->preroll = EINA_FALSE;
105 priv->unlocked = EINA_FALSE;
109 /**** Object methods ****/
112 evas_video_sink_set_property(GObject * object, guint prop_id,
113 const GValue * value, GParamSpec * pspec)
116 EvasVideoSinkPrivate* priv;
118 sink = EVAS_VIDEO_SINK (object);
122 case PROP_EVAS_OBJECT:
123 g_mutex_lock(priv->buffer_mutex);
124 priv->o = g_value_get_pointer (value);
125 g_mutex_unlock(priv->buffer_mutex);
128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129 ERR("invalid property");
135 evas_video_sink_get_property(GObject * object, guint prop_id,
136 GValue * value, GParamSpec * pspec)
139 EvasVideoSinkPrivate* priv;
141 sink = EVAS_VIDEO_SINK (object);
145 case PROP_EVAS_OBJECT:
146 g_mutex_lock(priv->buffer_mutex);
147 g_value_set_pointer (value, priv->o);
148 g_mutex_unlock(priv->buffer_mutex);
151 g_mutex_lock(priv->buffer_mutex);
152 g_value_set_int(value, priv->width);
153 g_mutex_unlock(priv->buffer_mutex);
156 g_mutex_lock(priv->buffer_mutex);
157 g_value_set_int (value, priv->height);
158 g_mutex_unlock(priv->buffer_mutex);
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
162 ERR("invalide property");
168 evas_video_sink_dispose(GObject* object)
171 EvasVideoSinkPrivate* priv;
173 sink = EVAS_VIDEO_SINK(object);
176 if (priv->buffer_mutex) {
177 g_mutex_free(priv->buffer_mutex);
178 priv->buffer_mutex = 0;
181 if (priv->data_cond) {
182 g_cond_free(priv->data_cond);
187 ecore_pipe_del(priv->p);
191 if (priv->last_buffer) {
192 gst_buffer_unref(priv->last_buffer);
193 priv->last_buffer = NULL;
196 G_OBJECT_CLASS(parent_class)->dispose(object);
200 /**** BaseSink methods ****/
202 gboolean evas_video_sink_set_caps(GstBaseSink *bsink, GstCaps *caps)
205 EvasVideoSinkPrivate* priv;
206 GstVideoFormat format;
210 sink = EVAS_VIDEO_SINK(bsink);
213 if (G_UNLIKELY(!gst_video_format_parse_caps(caps, &format, &width, &height))) {
214 ERR("Unable to parse caps.");
219 priv->height = height;
221 printf("%p format :", priv->o);
224 case GST_VIDEO_FORMAT_I420: priv->eformat = EVAS_COLORSPACE_YCBCR422P601_PL;
227 case GST_VIDEO_FORMAT_YV12: priv->eformat = EVAS_COLORSPACE_YCBCR422P601_PL;
230 case GST_VIDEO_FORMAT_YUY2: priv->eformat = EVAS_COLORSPACE_YCBCR422601_PL;
233 case GST_VIDEO_FORMAT_BGR: priv->eformat = EVAS_COLORSPACE_ARGB8888;
236 case GST_VIDEO_FORMAT_BGRx: priv->eformat = EVAS_COLORSPACE_ARGB8888;
239 case GST_VIDEO_FORMAT_BGRA: priv->eformat = EVAS_COLORSPACE_ARGB8888;
243 ERR("unsupported : %d\n", format);
246 priv->gformat = format;
252 evas_video_sink_start(GstBaseSink* base_sink)
254 EvasVideoSinkPrivate* priv;
257 priv = EVAS_VIDEO_SINK(base_sink)->priv;
258 g_mutex_lock(priv->buffer_mutex);
267 priv->unlocked = EINA_FALSE;
270 g_mutex_unlock(priv->buffer_mutex);
275 evas_video_sink_stop(GstBaseSink* base_sink)
277 EvasVideoSinkPrivate* priv = EVAS_VIDEO_SINK(base_sink)->priv;
279 unlock_buffer_mutex(priv);
284 evas_video_sink_unlock(GstBaseSink* object)
288 sink = EVAS_VIDEO_SINK(object);
290 unlock_buffer_mutex(sink->priv);
292 return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock,
297 evas_video_sink_unlock_stop(GstBaseSink* object)
300 EvasVideoSinkPrivate* priv;
302 sink = EVAS_VIDEO_SINK(object);
305 g_mutex_lock(priv->buffer_mutex);
306 priv->unlocked = FALSE;
307 g_mutex_unlock(priv->buffer_mutex);
309 return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock_stop,
314 evas_video_sink_preroll(GstBaseSink* bsink, GstBuffer* buffer)
318 EvasVideoSinkPrivate* priv;
320 sink = EVAS_VIDEO_SINK(bsink);
323 send = gst_buffer_ref(buffer);
325 priv->preroll = EINA_TRUE;
327 ecore_pipe_write(priv->p, &send, sizeof(buffer));
332 evas_video_sink_render(GstBaseSink* bsink, GstBuffer* buffer)
336 EvasVideoSinkPrivate* priv;
339 sink = EVAS_VIDEO_SINK(bsink);
342 g_mutex_lock(priv->buffer_mutex);
344 if (priv->unlocked) {
346 g_mutex_unlock(priv->buffer_mutex);
350 priv->preroll = EINA_FALSE;
352 send = gst_buffer_ref(buffer);
353 ret = ecore_pipe_write(priv->p, &send, sizeof(buffer));
355 return GST_FLOW_ERROR;
357 g_cond_wait(priv->data_cond, priv->buffer_mutex);
358 g_mutex_unlock(priv->buffer_mutex);
363 static void evas_video_sink_render_handler(void *data,
367 Emotion_Gstreamer_Video *ev;
368 Emotion_Video_Stream *vstream;
370 EvasVideoSinkPrivate* priv;
372 unsigned char *evas_data;
373 const guint8 *gst_data;
375 GstFormat fmt = GST_FORMAT_TIME;
379 sink = (EvasVideoSink *)data;
382 buffer = *((GstBuffer **)buf);
387 gst_data = GST_BUFFER_DATA(buffer);
388 if (!gst_data) goto exit_point;
390 // This prevent a race condition when data are still in the pipe
391 // but the buffer size as changed because of a request from
392 // emotion smart (like on a file set).
393 evas_object_image_size_get(priv->o, &w, &h);
394 if (w != priv->width || h != priv->height)
397 ev = evas_object_data_get(priv->o, "_emotion_gstreamer_video");
398 if (!ev) goto exit_point;
400 evas_object_image_size_set(priv->o, priv->width, priv->height);
401 evas_object_image_alpha_set(priv->o, 0);
402 evas_object_image_colorspace_set(priv->o, priv->eformat);
404 evas_data = (unsigned char *)evas_object_image_data_get(priv->o, 1);
406 // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
407 // Here we convert to Evas's BGRA.
408 switch (priv->gformat)
410 case GST_VIDEO_FORMAT_BGR:
412 unsigned char *evas_tmp;
416 evas_tmp = evas_data;
417 /* FIXME: could this be optimized ? */
418 for (x = 0; x < priv->height; x++) {
419 for (y = 0; y < priv->width; y++) {
420 evas_tmp[0] = gst_data[0];
421 evas_tmp[1] = gst_data[1];
422 evas_tmp[2] = gst_data[2];
431 // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
432 // Here we convert to Evas's BGRA.
433 case GST_VIDEO_FORMAT_BGRx:
435 unsigned char *evas_tmp;
439 evas_tmp = evas_data;
440 /* FIXME: could this be optimized ? */
441 for (x = 0; x < priv->height; x++) {
442 for (y = 0; y < priv->width; y++) {
443 evas_tmp[0] = gst_data[0];
444 evas_tmp[1] = gst_data[1];
445 evas_tmp[2] = gst_data[2];
454 // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
455 // Here we convert to Evas's BGRA.
456 case GST_VIDEO_FORMAT_BGRA:
458 unsigned char *evas_tmp;
463 evas_tmp = evas_data;
464 /* FIXME: could this be optimized ? */
465 for (x = 0; x < priv->height; x++) {
466 for (y = 0; y < priv->width; y++) {
468 evas_tmp[0] = (gst_data[0] * alpha) / 255;
469 evas_tmp[1] = (gst_data[1] * alpha) / 255;
470 evas_tmp[2] = (gst_data[2] * alpha) / 255;
479 case GST_VIDEO_FORMAT_I420:
482 const unsigned char **rows;
484 evas_object_image_pixels_dirty_set(priv->o, 1);
485 rows = (const unsigned char **)evas_data;
487 for (i = 0; i < priv->height; i++)
488 rows[i] = &gst_data[i * priv->width];
490 rows += priv->height;
491 for (i = 0; i < (priv->height / 2); i++)
492 rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
494 rows += priv->height / 2;
495 for (i = 0; i < (priv->height / 2); i++)
496 rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
500 case GST_VIDEO_FORMAT_YV12:
503 const unsigned char **rows;
505 evas_object_image_pixels_dirty_set(priv->o, 1);
507 rows = (const unsigned char **)evas_data;
509 for (i = 0; i < priv->height; i++)
510 rows[i] = &gst_data[i * priv->width];
512 rows += priv->height;
513 for (i = 0; i < (priv->height / 2); i++)
514 rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
516 rows += priv->height / 2;
517 for (i = 0; i < (priv->height / 2); i++)
518 rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
522 case GST_VIDEO_FORMAT_YUY2:
525 const unsigned char **rows;
527 evas_object_image_pixels_dirty_set(priv->o, 1);
529 rows = (const unsigned char **)evas_data;
531 for (i = 0; i < priv->height; i++)
532 rows[i] = &gst_data[i * priv->width * 2];
537 evas_object_image_data_update_add(priv->o, 0, 0, priv->width, priv->height);
538 evas_object_image_data_set(priv->o, evas_data);
539 evas_object_image_pixels_dirty_set(priv->o, 0);
541 _emotion_frame_new(ev->obj);
543 vstream = eina_list_nth(ev->video_streams, ev->video_stream_nbr - 1);
545 gst_element_query_position(ev->pipeline, &fmt, &pos);
546 ev->position = (double)pos / (double)GST_SECOND;
548 vstream->width = priv->width;
549 vstream->height = priv->height;
550 ev->ratio = (double) priv->width / (double) priv->height;
552 _emotion_video_pos_update(ev->obj, ev->position, vstream->length_time);
553 _emotion_frame_resize(ev->obj, priv->width, priv->height, ev->ratio);
556 if (priv->last_buffer) gst_buffer_unref(priv->last_buffer);
557 priv->last_buffer = buffer;
559 if (priv->preroll) return ;
561 g_mutex_lock(priv->buffer_mutex);
563 if (priv->unlocked) {
564 g_mutex_unlock(priv->buffer_mutex);
568 g_cond_signal(priv->data_cond);
569 g_mutex_unlock(priv->buffer_mutex);
573 unlock_buffer_mutex(EvasVideoSinkPrivate* priv)
575 g_mutex_lock(priv->buffer_mutex);
577 priv->unlocked = EINA_TRUE;
578 g_cond_signal(priv->data_cond);
579 g_mutex_unlock(priv->buffer_mutex);
583 marshal_VOID__MINIOBJECT(GClosure * closure, GValue * return_value __UNUSED__,
584 guint n_param_values, const GValue * param_values,
585 gpointer invocation_hint __UNUSED__, gpointer marshal_data)
587 typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1, gpointer data2);
588 marshalfunc_VOID__MINIOBJECT callback;
590 gpointer data1, data2;
592 cc = (GCClosure *) closure;
594 g_return_if_fail(n_param_values == 2);
596 if (G_CCLOSURE_SWAP_DATA(closure)) {
597 data1 = closure->data;
598 data2 = g_value_peek_pointer(param_values + 0);
600 data1 = g_value_peek_pointer(param_values + 0);
601 data2 = closure->data;
603 callback = (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data : cc->callback);
605 callback(data1, gst_value_get_mini_object(param_values + 1), data2);
609 evas_video_sink_class_init(EvasVideoSinkClass* klass)
611 GObjectClass* gobject_class;
612 GstBaseSinkClass* gstbase_sink_class;
614 gobject_class = G_OBJECT_CLASS(klass);
615 gstbase_sink_class = GST_BASE_SINK_CLASS(klass);
617 g_type_class_add_private(klass, sizeof(EvasVideoSinkPrivate));
619 gobject_class->set_property = evas_video_sink_set_property;
620 gobject_class->get_property = evas_video_sink_get_property;
622 g_object_class_install_property (gobject_class, PROP_EVAS_OBJECT,
623 g_param_spec_pointer ("evas-object", "Evas Object",
624 "The Evas object where the display of the video will be done",
627 g_object_class_install_property (gobject_class, PROP_WIDTH,
628 g_param_spec_int ("width", "Width",
629 "The width of the video",
630 0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
632 g_object_class_install_property (gobject_class, PROP_HEIGHT,
633 g_param_spec_int ("height", "Height",
634 "The height of the video",
635 0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
637 gobject_class->dispose = evas_video_sink_dispose;
639 gstbase_sink_class->set_caps = evas_video_sink_set_caps;
640 gstbase_sink_class->stop = evas_video_sink_stop;
641 gstbase_sink_class->start = evas_video_sink_start;
642 gstbase_sink_class->unlock = evas_video_sink_unlock;
643 gstbase_sink_class->unlock_stop = evas_video_sink_unlock_stop;
644 gstbase_sink_class->render = evas_video_sink_render;
645 gstbase_sink_class->preroll = evas_video_sink_preroll;
647 evas_video_sink_signals[REPAINT_REQUESTED] = g_signal_new("repaint-requested",
648 G_TYPE_FROM_CLASS(klass),
649 (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
653 marshal_VOID__MINIOBJECT,
654 G_TYPE_NONE, 1, GST_TYPE_BUFFER);
658 gstreamer_plugin_init (GstPlugin * plugin)
660 return gst_element_register (plugin,
663 EVAS_TYPE_VIDEO_SINK);
667 gstreamer_video_sink_new(Emotion_Gstreamer_Video *ev,
674 GstStateChangeReturn res;
677 obj = _emotion_image_get(o);
680 ERR("Not Evas_Object specified");
684 start = ecore_time_get();
685 playbin = gst_element_factory_make("playbin2", "playbin");
688 ERR("Unable to create 'playbin' GstElement.");
691 end = ecore_time_get();
692 DBG("Playbin2: %f", end - start);
694 start = ecore_time_get();
695 sink = gst_element_factory_make("emotion-sink", "sink");
698 ERR("Unable to create 'emotion-sink' GstElement.");
702 g_object_set(G_OBJECT(playbin), "video-sink", sink, NULL);
703 g_object_set(G_OBJECT(playbin), "uri", uri, NULL);
704 g_object_set(G_OBJECT(sink), "evas-object", obj, NULL);
706 end = ecore_time_get();
708 DBG("emotion-sink: %f", end - start);
710 start = ecore_time_get();
711 /* res = gst_element_set_state(playbin, GST_STATE_PLAYING); */
712 res = gst_element_set_state(playbin, GST_STATE_PAUSED);
713 if (res == GST_STATE_CHANGE_FAILURE)
715 ERR("Unable to set GST_STATE_PAUSED.");
718 end = ecore_time_get();
719 DBG("Pause pipeline: %f", end - start);
721 start = ecore_time_get();
722 res = gst_element_get_state(playbin, NULL, NULL, GST_CLOCK_TIME_NONE);
723 if (res != GST_STATE_CHANGE_SUCCESS)
725 /** NOTE: you need to set: GST_DEBUG_DUMP_DOT_DIR=/tmp EMOTION_ENGINE=gstreamer to save the $EMOTION_GSTREAMER_DOT file in '/tmp' */
726 /** then call dot -Tpng -oemotion_pipeline.png /tmp/$TIMESTAMP-$EMOTION_GSTREAMER_DOT.dot */
727 if (getenv("EMOTION_GSTREAMER_DOT")) GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(playbin), GST_DEBUG_GRAPH_SHOW_ALL, getenv("EMOTION_GSTREAMER_DOT"));
729 ERR("Unable to get GST_CLOCK_TIME_NONE.");
732 end = ecore_time_get();
733 DBG("No time: %f", end - start);
735 /** NOTE: you need to set: GST_DEBUG_DUMP_DOT_DIR=/tmp EMOTION_ENGINE=gstreamer to save the $EMOTION_GSTREAMER_DOT file in '/tmp' */
736 /** then call dot -Tpng -oemotion_pipeline.png /tmp/$TIMESTAMP-$EMOTION_GSTREAMER_DOT.dot */
737 if (getenv("EMOTION_GSTREAMER_DOT")) GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(playbin), GST_DEBUG_GRAPH_SHOW_ALL, getenv("EMOTION_GSTREAMER_DOT"));
739 evas_object_data_set(obj, "_emotion_gstreamer_video", ev);
744 gst_object_unref(playbin);