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 // If this is TRUE all processing should finish ASAP
45 // This is necessary because there could be a race between
46 // unlock() and render(), where unlock() wins, signals the
47 // GCond, then render() tries to render a frame although
48 // everything else isn't running anymore. This will lead
49 // to deadlocks because render() holds the stream lock.
51 // Protected by the buffer mutex
52 Eina_Bool unlocked : 1;
53 Eina_Bool preroll : 1;
54 Eina_Bool update_size : 1;
57 #define _do_init(bla) \
58 GST_DEBUG_CATEGORY_INIT(evas_video_sink_debug, \
63 GST_BOILERPLATE_FULL(EvasVideoSink,
70 static void unlock_buffer_mutex(EvasVideoSinkPrivate* priv);
72 static void evas_video_sink_render_handler(void *data, void *buf, unsigned int len);
75 evas_video_sink_base_init(gpointer g_class)
77 GstElementClass* element_class;
79 element_class = GST_ELEMENT_CLASS(g_class);
80 gst_element_class_add_pad_template(element_class, gst_static_pad_template_get(&sinktemplate));
81 gst_element_class_set_details_simple(element_class, "Evas video sink",
82 "Sink/Video", "Sends video data from a GStreamer pipeline to an Evas object",
83 "Vincent Torri <vtorri@univ-evry.fr>");
87 evas_video_sink_init(EvasVideoSink* sink, EvasVideoSinkClass* klass __UNUSED__)
89 EvasVideoSinkPrivate* priv;
91 sink->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE(sink, EVAS_TYPE_VIDEO_SINK, EvasVideoSinkPrivate);
93 priv->p = ecore_pipe_add(evas_video_sink_render_handler, sink);
96 priv->format = GST_VIDEO_FORMAT_UNKNOWN;
97 priv->data_cond = g_cond_new();
98 priv->buffer_mutex = g_mutex_new();
99 priv->preroll = EINA_FALSE;
100 priv->unlocked = EINA_FALSE;
101 priv->update_size = EINA_TRUE;
105 /**** Object methods ****/
108 evas_video_sink_set_property(GObject * object, guint prop_id,
109 const GValue * value, GParamSpec * pspec)
112 EvasVideoSinkPrivate* priv;
114 sink = EVAS_VIDEO_SINK (object);
118 case PROP_EVAS_OBJECT:
119 g_mutex_lock(priv->buffer_mutex);
120 priv->o = g_value_get_pointer (value);
121 g_mutex_unlock(priv->buffer_mutex);
124 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
130 evas_video_sink_get_property(GObject * object, guint prop_id,
131 GValue * value, GParamSpec * pspec)
134 EvasVideoSinkPrivate* priv;
136 sink = EVAS_VIDEO_SINK (object);
140 case PROP_EVAS_OBJECT:
141 g_mutex_lock(priv->buffer_mutex);
142 g_value_set_pointer (value, priv->o);
143 g_mutex_unlock(priv->buffer_mutex);
146 g_mutex_lock(priv->buffer_mutex);
147 g_value_set_int(value, priv->width);
148 g_mutex_unlock(priv->buffer_mutex);
151 g_mutex_lock(priv->buffer_mutex);
152 g_value_set_int (value, priv->height);
153 g_mutex_unlock(priv->buffer_mutex);
156 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
162 evas_video_sink_dispose(GObject* object)
165 EvasVideoSinkPrivate* priv;
167 sink = EVAS_VIDEO_SINK(object);
170 if (priv->buffer_mutex) {
171 g_mutex_free(priv->buffer_mutex);
172 priv->buffer_mutex = 0;
175 if (priv->data_cond) {
176 g_cond_free(priv->data_cond);
181 ecore_pipe_del(priv->p);
185 G_OBJECT_CLASS(parent_class)->dispose(object);
189 /**** BaseSink methods ****/
191 gboolean evas_video_sink_set_caps(GstBaseSink *bsink, GstCaps *caps)
194 EvasVideoSinkPrivate* priv;
198 sink = EVAS_VIDEO_SINK(bsink);
201 if (G_UNLIKELY(!gst_video_format_parse_caps(caps, &priv->format, &width, &height))) {
205 if ((width != priv->width) || (height != priv->height))
208 priv->height = height;
209 priv->update_size = EINA_TRUE;
213 switch (priv->format)
215 case GST_VIDEO_FORMAT_I420:
216 evas_object_image_size_set(priv->o, priv->width, priv->height);
217 evas_object_image_fill_set(priv->o, 0, 0, priv->width, priv->height);
218 evas_object_image_colorspace_set(priv->o, EVAS_COLORSPACE_YCBCR422P601_PL);
219 evas_object_image_alpha_set(priv->o, 0);
222 case GST_VIDEO_FORMAT_YV12:
223 evas_object_image_size_set(priv->o, priv->width, priv->height);
224 evas_object_image_fill_set(priv->o, 0, 0, priv->width, priv->height);
225 evas_object_image_colorspace_set(priv->o, EVAS_COLORSPACE_YCBCR422P601_PL);
226 evas_object_image_alpha_set(priv->o, 0);
229 case GST_VIDEO_FORMAT_BGR:
232 case GST_VIDEO_FORMAT_BGRx:
235 case GST_VIDEO_FORMAT_BGRA:
239 printf ("unsupported : %d\n", priv->format);
247 evas_video_sink_start(GstBaseSink* base_sink)
249 EvasVideoSinkPrivate* priv;
252 priv = EVAS_VIDEO_SINK(base_sink)->priv;
253 g_mutex_lock(priv->buffer_mutex);
262 priv->unlocked = EINA_FALSE;
265 g_mutex_unlock(priv->buffer_mutex);
270 evas_video_sink_stop(GstBaseSink* base_sink)
272 EvasVideoSinkPrivate* priv = EVAS_VIDEO_SINK(base_sink)->priv;
274 unlock_buffer_mutex(priv);
279 evas_video_sink_unlock(GstBaseSink* object)
283 sink = EVAS_VIDEO_SINK(object);
285 unlock_buffer_mutex(sink->priv);
287 return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock,
292 evas_video_sink_unlock_stop(GstBaseSink* object)
295 EvasVideoSinkPrivate* priv;
297 sink = EVAS_VIDEO_SINK(object);
300 g_mutex_lock(priv->buffer_mutex);
301 priv->unlocked = FALSE;
302 g_mutex_unlock(priv->buffer_mutex);
304 return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock_stop,
309 evas_video_sink_preroll(GstBaseSink* bsink, GstBuffer* buffer)
313 EvasVideoSinkPrivate* priv;
315 sink = EVAS_VIDEO_SINK(bsink);
318 send = gst_buffer_ref(buffer);
320 priv->preroll = EINA_TRUE;
322 ecore_pipe_write(priv->p, &send, sizeof(buffer));
327 evas_video_sink_render(GstBaseSink* bsink, GstBuffer* buffer)
331 EvasVideoSinkPrivate* priv;
334 sink = EVAS_VIDEO_SINK(bsink);
337 g_mutex_lock(priv->buffer_mutex);
339 if (priv->unlocked) {
340 g_mutex_unlock(priv->buffer_mutex);
344 priv->preroll = EINA_FALSE;
346 send = gst_buffer_ref(buffer);
347 ret = ecore_pipe_write(priv->p, &send, sizeof(buffer));
349 return GST_FLOW_ERROR;
351 g_cond_wait(priv->data_cond, priv->buffer_mutex);
352 g_mutex_unlock(priv->buffer_mutex);
357 static void evas_video_sink_render_handler(void *data,
361 Emotion_Gstreamer_Video *ev;
362 Emotion_Video_Stream *vstream;
364 EvasVideoSinkPrivate* priv;
366 unsigned char *evas_data;
367 const guint8 *gst_data;
369 GstFormat fmt = GST_FORMAT_TIME;
372 sink = (EvasVideoSink *)data;
375 buffer = *((GstBuffer **)buf);
380 gst_data = GST_BUFFER_DATA(buffer);
381 if (!gst_data) goto exit_point;
382 if (priv->update_size)
384 evas_object_image_size_set(priv->o, priv->width, priv->height);
385 if (!priv->preroll) priv->update_size = FALSE;
388 evas_data = (unsigned char *)evas_object_image_data_get(priv->o, 1);
390 // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
391 // Here we convert to Evas's BGRA.
392 if (priv->format == GST_VIDEO_FORMAT_BGR) {
393 unsigned char *evas_tmp;
397 evas_tmp = evas_data;
398 /* FIXME: could this be optimized ? */
399 for (x = 0; x < priv->height; x++) {
400 for (y = 0; y < priv->width; y++) {
401 evas_tmp[0] = gst_data[0];
402 evas_tmp[1] = gst_data[1];
403 evas_tmp[2] = gst_data[2];
411 // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
412 // Here we convert to Evas's BGRA.
413 if (priv->format == GST_VIDEO_FORMAT_BGRx) {
414 unsigned char *evas_tmp;
418 evas_tmp = evas_data;
419 /* FIXME: could this be optimized ? */
420 for (x = 0; x < priv->height; x++) {
421 for (y = 0; y < priv->width; y++) {
422 evas_tmp[0] = gst_data[0];
423 evas_tmp[1] = gst_data[1];
424 evas_tmp[2] = gst_data[2];
432 // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
433 // Here we convert to Evas's BGRA.
434 if (priv->format == GST_VIDEO_FORMAT_BGRA) {
435 unsigned char *evas_tmp;
440 evas_tmp = evas_data;
441 /* FIXME: could this be optimized ? */
442 for (x = 0; x < priv->height; x++) {
443 for (y = 0; y < priv->width; y++) {
445 evas_tmp[0] = (gst_data[0] * alpha) / 255;
446 evas_tmp[1] = (gst_data[1] * alpha) / 255;
447 evas_tmp[2] = (gst_data[2] * alpha) / 255;
455 if (priv->format == GST_VIDEO_FORMAT_I420) {
457 unsigned char **rows;
459 evas_object_image_pixels_dirty_set(priv->o, 1);
460 rows = (unsigned char **)evas_data;
462 for (i = 0; i < priv->height; i++)
463 rows[i] = &gst_data[i * priv->width];
465 rows += priv->height;
466 for (i = 0; i < (priv->height / 2); i++)
467 rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
469 rows += priv->height / 2;
470 for (i = 0; i < (priv->height / 2); i++)
471 rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
474 if (priv->format == GST_VIDEO_FORMAT_YV12) {
476 unsigned char **rows;
478 evas_object_image_pixels_dirty_set(priv->o, 1);
480 rows = (unsigned char **)evas_data;
482 for (i = 0; i < priv->height; i++)
483 rows[i] = &gst_data[i * priv->width];
485 rows += priv->height;
486 for (i = 0; i < (priv->height / 2); i++)
487 rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
489 rows += priv->height / 2;
490 for (i = 0; i < (priv->height / 2); i++)
491 rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
494 evas_object_image_data_update_add(priv->o, 0, 0, priv->width, priv->height);
495 evas_object_image_data_set(priv->o, evas_data);
496 evas_object_image_pixels_dirty_set(priv->o, 0);
498 ev = evas_object_data_get(priv->o, "_emotion_gstreamer_video");
499 _emotion_frame_new(ev->obj);
501 vstream = eina_list_nth(ev->video_streams, ev->video_stream_nbr - 1);
503 gst_element_query_position(ev->pipeline, &fmt, &pos);
504 ev->position = (double)pos / (double)GST_SECOND;
506 vstream->width = priv->width;
507 vstream->height = priv->height;
508 ev->ratio = (double) priv->width / (double) priv->height;
510 _emotion_video_pos_update(ev->obj, ev->position, vstream->length_time);
511 _emotion_frame_resize(ev->obj, priv->width, priv->height, ev->ratio);
514 gst_buffer_unref(buffer);
516 if (priv->preroll) return ;
518 g_mutex_lock(priv->buffer_mutex);
520 if (priv->unlocked) {
521 g_mutex_unlock(priv->buffer_mutex);
525 g_cond_signal(priv->data_cond);
526 g_mutex_unlock(priv->buffer_mutex);
530 unlock_buffer_mutex(EvasVideoSinkPrivate* priv)
532 g_mutex_lock(priv->buffer_mutex);
534 priv->unlocked = EINA_TRUE;
535 g_cond_signal(priv->data_cond);
536 g_mutex_unlock(priv->buffer_mutex);
540 marshal_VOID__MINIOBJECT(GClosure * closure, GValue * return_value __UNUSED__,
541 guint n_param_values, const GValue * param_values,
542 gpointer invocation_hint __UNUSED__, gpointer marshal_data)
544 typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1, gpointer data2);
545 marshalfunc_VOID__MINIOBJECT callback;
547 gpointer data1, data2;
549 cc = (GCClosure *) closure;
551 g_return_if_fail(n_param_values == 2);
553 if (G_CCLOSURE_SWAP_DATA(closure)) {
554 data1 = closure->data;
555 data2 = g_value_peek_pointer(param_values + 0);
557 data1 = g_value_peek_pointer(param_values + 0);
558 data2 = closure->data;
560 callback = (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data : cc->callback);
562 callback(data1, gst_value_get_mini_object(param_values + 1), data2);
566 evas_video_sink_class_init(EvasVideoSinkClass* klass)
568 GObjectClass* gobject_class;
569 GstBaseSinkClass* gstbase_sink_class;
571 gobject_class = G_OBJECT_CLASS(klass);
572 gstbase_sink_class = GST_BASE_SINK_CLASS(klass);
574 g_type_class_add_private(klass, sizeof(EvasVideoSinkPrivate));
576 gobject_class->set_property = evas_video_sink_set_property;
577 gobject_class->get_property = evas_video_sink_get_property;
579 g_object_class_install_property (gobject_class, PROP_EVAS_OBJECT,
580 g_param_spec_pointer ("evas-object", "Evas Object",
581 "The Evas object where the display of the video will be done",
584 g_object_class_install_property (gobject_class, PROP_WIDTH,
585 g_param_spec_int ("width", "Width",
586 "The width of the video",
587 0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
589 g_object_class_install_property (gobject_class, PROP_HEIGHT,
590 g_param_spec_int ("height", "Height",
591 "The height of the video",
592 0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
594 gobject_class->dispose = evas_video_sink_dispose;
596 gstbase_sink_class->set_caps = evas_video_sink_set_caps;
597 gstbase_sink_class->stop = evas_video_sink_stop;
598 gstbase_sink_class->start = evas_video_sink_start;
599 gstbase_sink_class->unlock = evas_video_sink_unlock;
600 gstbase_sink_class->unlock_stop = evas_video_sink_unlock_stop;
601 gstbase_sink_class->render = evas_video_sink_render;
602 gstbase_sink_class->preroll = evas_video_sink_preroll;
604 evas_video_sink_signals[REPAINT_REQUESTED] = g_signal_new("repaint-requested",
605 G_TYPE_FROM_CLASS(klass),
606 (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
610 marshal_VOID__MINIOBJECT,
611 G_TYPE_NONE, 1, GST_TYPE_BUFFER);
615 gstreamer_plugin_init (GstPlugin * plugin)
617 return gst_element_register (plugin,
620 EVAS_TYPE_VIDEO_SINK);
624 gstreamer_video_sink_new(Emotion_Gstreamer_Video *ev,
631 GstStateChangeReturn res;
633 obj = _emotion_image_get(o);
636 ERR("Not Evas_Object specified");
640 playbin = gst_element_factory_make("playbin2", "playbin");
643 ERR("Unable to create 'playbin' GstElement.");
647 sink = gst_element_factory_make("emotion-sink", "sink");
650 ERR("Unable to create 'emotion-sink' GstElement.");
654 g_object_set(G_OBJECT(playbin), "video-sink", sink, NULL);
655 g_object_set(G_OBJECT(playbin), "uri", uri, NULL);
656 g_object_set(G_OBJECT(sink), "evas-object", obj, NULL);
658 res = gst_element_set_state(playbin, GST_STATE_PAUSED);
659 if (res == GST_STATE_CHANGE_FAILURE)
661 ERR("Unable to set GST_STATE_PAUSED.");
665 res = gst_element_get_state(playbin, NULL, NULL, GST_CLOCK_TIME_NONE);
666 if (res != GST_STATE_CHANGE_SUCCESS)
668 ERR("Unable to get GST_CLOCK_TIME_NONE.");
672 evas_object_data_set(obj, "_emotion_gstreamer_video", ev);
677 gst_object_unref(playbin);