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_BGRx ";" GST_VIDEO_CAPS_BGR ";" GST_VIDEO_CAPS_BGRA ";" GST_VIDEO_CAPS_YUV("{I420,YV12}")));
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 GstVideoFormat format;
44 GstBuffer *last_buffer; /* We need to keep a copy of the last inserted buffer as evas doesn't copy YUV data around */
46 // If this is TRUE all processing should finish ASAP
47 // This is necessary because there could be a race between
48 // unlock() and render(), where unlock() wins, signals the
49 // GCond, then render() tries to render a frame although
50 // everything else isn't running anymore. This will lead
51 // to deadlocks because render() holds the stream lock.
53 // Protected by the buffer mutex
54 Eina_Bool unlocked : 1;
55 Eina_Bool preroll : 1;
56 Eina_Bool update_size : 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;
93 sink->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE(sink, EVAS_TYPE_VIDEO_SINK, EvasVideoSinkPrivate);
95 priv->p = ecore_pipe_add(evas_video_sink_render_handler, sink);
96 priv->last_buffer = NULL;
99 priv->format = GST_VIDEO_FORMAT_UNKNOWN;
100 priv->data_cond = g_cond_new();
101 priv->buffer_mutex = g_mutex_new();
102 priv->preroll = EINA_FALSE;
103 priv->unlocked = EINA_FALSE;
104 priv->update_size = EINA_TRUE;
108 /**** Object methods ****/
111 evas_video_sink_set_property(GObject * object, guint prop_id,
112 const GValue * value, GParamSpec * pspec)
115 EvasVideoSinkPrivate* priv;
117 sink = EVAS_VIDEO_SINK (object);
121 case PROP_EVAS_OBJECT:
122 g_mutex_lock(priv->buffer_mutex);
123 priv->o = g_value_get_pointer (value);
124 g_mutex_unlock(priv->buffer_mutex);
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
133 evas_video_sink_get_property(GObject * object, guint prop_id,
134 GValue * value, GParamSpec * pspec)
137 EvasVideoSinkPrivate* priv;
139 sink = EVAS_VIDEO_SINK (object);
143 case PROP_EVAS_OBJECT:
144 g_mutex_lock(priv->buffer_mutex);
145 g_value_set_pointer (value, priv->o);
146 g_mutex_unlock(priv->buffer_mutex);
149 g_mutex_lock(priv->buffer_mutex);
150 g_value_set_int(value, priv->width);
151 g_mutex_unlock(priv->buffer_mutex);
154 g_mutex_lock(priv->buffer_mutex);
155 g_value_set_int (value, priv->height);
156 g_mutex_unlock(priv->buffer_mutex);
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 evas_video_sink_dispose(GObject* object)
168 EvasVideoSinkPrivate* priv;
170 sink = EVAS_VIDEO_SINK(object);
173 if (priv->buffer_mutex) {
174 g_mutex_free(priv->buffer_mutex);
175 priv->buffer_mutex = 0;
178 if (priv->data_cond) {
179 g_cond_free(priv->data_cond);
184 ecore_pipe_del(priv->p);
188 if (priv->last_buffer) {
189 gst_buffer_unref(priv->last_buffer);
190 priv->last_buffer = NULL;
193 G_OBJECT_CLASS(parent_class)->dispose(object);
197 /**** BaseSink methods ****/
199 gboolean evas_video_sink_set_caps(GstBaseSink *bsink, GstCaps *caps)
202 EvasVideoSinkPrivate* priv;
206 sink = EVAS_VIDEO_SINK(bsink);
209 if (G_UNLIKELY(!gst_video_format_parse_caps(caps, &priv->format, &width, &height))) {
213 if ((width != priv->width) || (height != priv->height))
216 priv->height = height;
217 priv->update_size = EINA_TRUE;
221 switch (priv->format)
223 case GST_VIDEO_FORMAT_I420:
224 evas_object_image_size_set(priv->o, priv->width, priv->height);
225 evas_object_image_fill_set(priv->o, 0, 0, priv->width, priv->height);
226 evas_object_image_colorspace_set(priv->o, EVAS_COLORSPACE_YCBCR422P601_PL);
227 evas_object_image_alpha_set(priv->o, 0);
230 case GST_VIDEO_FORMAT_YV12:
231 evas_object_image_size_set(priv->o, priv->width, priv->height);
232 evas_object_image_fill_set(priv->o, 0, 0, priv->width, priv->height);
233 evas_object_image_colorspace_set(priv->o, EVAS_COLORSPACE_YCBCR422P601_PL);
234 evas_object_image_alpha_set(priv->o, 0);
237 case GST_VIDEO_FORMAT_BGR:
240 case GST_VIDEO_FORMAT_BGRx:
243 case GST_VIDEO_FORMAT_BGRA:
247 printf ("unsupported : %d\n", priv->format);
255 evas_video_sink_start(GstBaseSink* base_sink)
257 EvasVideoSinkPrivate* priv;
260 priv = EVAS_VIDEO_SINK(base_sink)->priv;
261 g_mutex_lock(priv->buffer_mutex);
270 priv->unlocked = EINA_FALSE;
273 g_mutex_unlock(priv->buffer_mutex);
278 evas_video_sink_stop(GstBaseSink* base_sink)
280 EvasVideoSinkPrivate* priv = EVAS_VIDEO_SINK(base_sink)->priv;
282 unlock_buffer_mutex(priv);
287 evas_video_sink_unlock(GstBaseSink* object)
291 sink = EVAS_VIDEO_SINK(object);
293 unlock_buffer_mutex(sink->priv);
295 return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock,
300 evas_video_sink_unlock_stop(GstBaseSink* object)
303 EvasVideoSinkPrivate* priv;
305 sink = EVAS_VIDEO_SINK(object);
308 g_mutex_lock(priv->buffer_mutex);
309 priv->unlocked = FALSE;
310 g_mutex_unlock(priv->buffer_mutex);
312 return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock_stop,
317 evas_video_sink_preroll(GstBaseSink* bsink, GstBuffer* buffer)
321 EvasVideoSinkPrivate* priv;
323 sink = EVAS_VIDEO_SINK(bsink);
326 send = gst_buffer_ref(buffer);
328 priv->preroll = EINA_TRUE;
330 ecore_pipe_write(priv->p, &send, sizeof(buffer));
335 evas_video_sink_render(GstBaseSink* bsink, GstBuffer* buffer)
339 EvasVideoSinkPrivate* priv;
342 sink = EVAS_VIDEO_SINK(bsink);
345 g_mutex_lock(priv->buffer_mutex);
347 if (priv->unlocked) {
348 g_mutex_unlock(priv->buffer_mutex);
352 priv->preroll = EINA_FALSE;
354 send = gst_buffer_ref(buffer);
355 ret = ecore_pipe_write(priv->p, &send, sizeof(buffer));
357 return GST_FLOW_ERROR;
359 g_cond_wait(priv->data_cond, priv->buffer_mutex);
360 g_mutex_unlock(priv->buffer_mutex);
365 static void evas_video_sink_render_handler(void *data,
369 Emotion_Gstreamer_Video *ev;
370 Emotion_Video_Stream *vstream;
372 EvasVideoSinkPrivate* priv;
374 unsigned char *evas_data;
375 const guint8 *gst_data;
377 GstFormat fmt = GST_FORMAT_TIME;
380 sink = (EvasVideoSink *)data;
383 buffer = *((GstBuffer **)buf);
388 gst_data = GST_BUFFER_DATA(buffer);
389 if (!gst_data) goto exit_point;
390 if (priv->update_size)
392 evas_object_image_size_set(priv->o, priv->width, priv->height);
393 if (!priv->preroll) priv->update_size = FALSE;
396 evas_data = (unsigned char *)evas_object_image_data_get(priv->o, 1);
398 // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
399 // Here we convert to Evas's BGRA.
400 if (priv->format == GST_VIDEO_FORMAT_BGR) {
401 unsigned char *evas_tmp;
405 evas_tmp = evas_data;
406 /* FIXME: could this be optimized ? */
407 for (x = 0; x < priv->height; x++) {
408 for (y = 0; y < priv->width; y++) {
409 evas_tmp[0] = gst_data[0];
410 evas_tmp[1] = gst_data[1];
411 evas_tmp[2] = gst_data[2];
419 // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
420 // Here we convert to Evas's BGRA.
421 if (priv->format == GST_VIDEO_FORMAT_BGRx) {
422 unsigned char *evas_tmp;
426 evas_tmp = evas_data;
427 /* FIXME: could this be optimized ? */
428 for (x = 0; x < priv->height; x++) {
429 for (y = 0; y < priv->width; y++) {
430 evas_tmp[0] = gst_data[0];
431 evas_tmp[1] = gst_data[1];
432 evas_tmp[2] = gst_data[2];
440 // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
441 // Here we convert to Evas's BGRA.
442 if (priv->format == GST_VIDEO_FORMAT_BGRA) {
443 unsigned char *evas_tmp;
448 evas_tmp = evas_data;
449 /* FIXME: could this be optimized ? */
450 for (x = 0; x < priv->height; x++) {
451 for (y = 0; y < priv->width; y++) {
453 evas_tmp[0] = (gst_data[0] * alpha) / 255;
454 evas_tmp[1] = (gst_data[1] * alpha) / 255;
455 evas_tmp[2] = (gst_data[2] * alpha) / 255;
463 if (priv->format == GST_VIDEO_FORMAT_I420) {
465 unsigned char **rows;
467 evas_object_image_pixels_dirty_set(priv->o, 1);
468 rows = (unsigned char **)evas_data;
470 for (i = 0; i < priv->height; i++)
471 rows[i] = &gst_data[i * priv->width];
473 rows += priv->height;
474 for (i = 0; i < (priv->height / 2); i++)
475 rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
477 rows += priv->height / 2;
478 for (i = 0; i < (priv->height / 2); i++)
479 rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
482 if (priv->format == GST_VIDEO_FORMAT_YV12) {
484 unsigned char **rows;
486 evas_object_image_pixels_dirty_set(priv->o, 1);
488 rows = (unsigned char **)evas_data;
490 for (i = 0; i < priv->height; i++)
491 rows[i] = &gst_data[i * priv->width];
493 rows += priv->height;
494 for (i = 0; i < (priv->height / 2); i++)
495 rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
497 rows += priv->height / 2;
498 for (i = 0; i < (priv->height / 2); i++)
499 rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
502 evas_object_image_data_update_add(priv->o, 0, 0, priv->width, priv->height);
503 evas_object_image_data_set(priv->o, evas_data);
504 evas_object_image_pixels_dirty_set(priv->o, 0);
506 ev = evas_object_data_get(priv->o, "_emotion_gstreamer_video");
507 _emotion_frame_new(ev->obj);
509 vstream = eina_list_nth(ev->video_streams, ev->video_stream_nbr - 1);
511 gst_element_query_position(ev->pipeline, &fmt, &pos);
512 ev->position = (double)pos / (double)GST_SECOND;
514 vstream->width = priv->width;
515 vstream->height = priv->height;
516 ev->ratio = (double) priv->width / (double) priv->height;
518 _emotion_video_pos_update(ev->obj, ev->position, vstream->length_time);
519 _emotion_frame_resize(ev->obj, priv->width, priv->height, ev->ratio);
522 if (priv->last_buffer) gst_buffer_unref(priv->last_buffer);
523 priv->last_buffer = buffer;
525 if (priv->preroll) return ;
527 g_mutex_lock(priv->buffer_mutex);
529 if (priv->unlocked) {
530 g_mutex_unlock(priv->buffer_mutex);
534 g_cond_signal(priv->data_cond);
535 g_mutex_unlock(priv->buffer_mutex);
539 unlock_buffer_mutex(EvasVideoSinkPrivate* priv)
541 g_mutex_lock(priv->buffer_mutex);
543 priv->unlocked = EINA_TRUE;
544 g_cond_signal(priv->data_cond);
545 g_mutex_unlock(priv->buffer_mutex);
549 marshal_VOID__MINIOBJECT(GClosure * closure, GValue * return_value __UNUSED__,
550 guint n_param_values, const GValue * param_values,
551 gpointer invocation_hint __UNUSED__, gpointer marshal_data)
553 typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1, gpointer data2);
554 marshalfunc_VOID__MINIOBJECT callback;
556 gpointer data1, data2;
558 cc = (GCClosure *) closure;
560 g_return_if_fail(n_param_values == 2);
562 if (G_CCLOSURE_SWAP_DATA(closure)) {
563 data1 = closure->data;
564 data2 = g_value_peek_pointer(param_values + 0);
566 data1 = g_value_peek_pointer(param_values + 0);
567 data2 = closure->data;
569 callback = (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data : cc->callback);
571 callback(data1, gst_value_get_mini_object(param_values + 1), data2);
575 evas_video_sink_class_init(EvasVideoSinkClass* klass)
577 GObjectClass* gobject_class;
578 GstBaseSinkClass* gstbase_sink_class;
580 gobject_class = G_OBJECT_CLASS(klass);
581 gstbase_sink_class = GST_BASE_SINK_CLASS(klass);
583 g_type_class_add_private(klass, sizeof(EvasVideoSinkPrivate));
585 gobject_class->set_property = evas_video_sink_set_property;
586 gobject_class->get_property = evas_video_sink_get_property;
588 g_object_class_install_property (gobject_class, PROP_EVAS_OBJECT,
589 g_param_spec_pointer ("evas-object", "Evas Object",
590 "The Evas object where the display of the video will be done",
593 g_object_class_install_property (gobject_class, PROP_WIDTH,
594 g_param_spec_int ("width", "Width",
595 "The width of the video",
596 0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
598 g_object_class_install_property (gobject_class, PROP_HEIGHT,
599 g_param_spec_int ("height", "Height",
600 "The height of the video",
601 0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
603 gobject_class->dispose = evas_video_sink_dispose;
605 gstbase_sink_class->set_caps = evas_video_sink_set_caps;
606 gstbase_sink_class->stop = evas_video_sink_stop;
607 gstbase_sink_class->start = evas_video_sink_start;
608 gstbase_sink_class->unlock = evas_video_sink_unlock;
609 gstbase_sink_class->unlock_stop = evas_video_sink_unlock_stop;
610 gstbase_sink_class->render = evas_video_sink_render;
611 gstbase_sink_class->preroll = evas_video_sink_preroll;
613 evas_video_sink_signals[REPAINT_REQUESTED] = g_signal_new("repaint-requested",
614 G_TYPE_FROM_CLASS(klass),
615 (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
619 marshal_VOID__MINIOBJECT,
620 G_TYPE_NONE, 1, GST_TYPE_BUFFER);
624 gstreamer_plugin_init (GstPlugin * plugin)
626 return gst_element_register (plugin,
629 EVAS_TYPE_VIDEO_SINK);
633 gstreamer_video_sink_new(Emotion_Gstreamer_Video *ev,
640 GstStateChangeReturn res;
642 obj = _emotion_image_get(o);
645 ERR("Not Evas_Object specified");
649 playbin = gst_element_factory_make("playbin2", "playbin");
652 ERR("Unable to create 'playbin' GstElement.");
656 sink = gst_element_factory_make("emotion-sink", "sink");
659 ERR("Unable to create 'emotion-sink' GstElement.");
663 g_object_set(G_OBJECT(playbin), "video-sink", sink, NULL);
664 g_object_set(G_OBJECT(playbin), "uri", uri, NULL);
665 g_object_set(G_OBJECT(sink), "evas-object", obj, NULL);
667 res = gst_element_set_state(playbin, GST_STATE_PAUSED);
668 if (res == GST_STATE_CHANGE_FAILURE)
670 ERR("Unable to set GST_STATE_PAUSED.");
674 res = gst_element_get_state(playbin, NULL, NULL, GST_CLOCK_TIME_NONE);
675 if (res != GST_STATE_CHANGE_SUCCESS)
677 ERR("Unable to get GST_CLOCK_TIME_NONE.");
681 evas_object_data_set(obj, "_emotion_gstreamer_video", ev);
683 /** NOTE: you need to set: GST_DEBUG_DUMP_DOT_DIR=/tmp EMOTION_ENGINE=gstreamer to save the $EMOTION_GSTREAMER_DOT file in '/tmp' */
684 /** then call dot -Tpng -oemotion_pipeline.png /tmp/$TIMESTAMP-$EMOTION_GSTREAMER_DOT.dot */
685 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"));
690 gst_object_unref(playbin);