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_colorspace_set(priv->o, EVAS_COLORSPACE_YCBCR422P601_PL);
226 evas_object_image_alpha_set(priv->o, 0);
229 case GST_VIDEO_FORMAT_YV12:
230 evas_object_image_size_set(priv->o, priv->width, priv->height);
231 evas_object_image_colorspace_set(priv->o, EVAS_COLORSPACE_YCBCR422P601_PL);
232 evas_object_image_alpha_set(priv->o, 0);
235 case GST_VIDEO_FORMAT_BGR:
238 case GST_VIDEO_FORMAT_BGRx:
241 case GST_VIDEO_FORMAT_BGRA:
245 printf ("unsupported : %d\n", priv->format);
253 evas_video_sink_start(GstBaseSink* base_sink)
255 EvasVideoSinkPrivate* priv;
258 priv = EVAS_VIDEO_SINK(base_sink)->priv;
259 g_mutex_lock(priv->buffer_mutex);
268 priv->unlocked = EINA_FALSE;
271 g_mutex_unlock(priv->buffer_mutex);
276 evas_video_sink_stop(GstBaseSink* base_sink)
278 EvasVideoSinkPrivate* priv = EVAS_VIDEO_SINK(base_sink)->priv;
280 unlock_buffer_mutex(priv);
285 evas_video_sink_unlock(GstBaseSink* object)
289 sink = EVAS_VIDEO_SINK(object);
291 unlock_buffer_mutex(sink->priv);
293 return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock,
298 evas_video_sink_unlock_stop(GstBaseSink* object)
301 EvasVideoSinkPrivate* priv;
303 sink = EVAS_VIDEO_SINK(object);
306 g_mutex_lock(priv->buffer_mutex);
307 priv->unlocked = FALSE;
308 g_mutex_unlock(priv->buffer_mutex);
310 return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock_stop,
315 evas_video_sink_preroll(GstBaseSink* bsink, GstBuffer* buffer)
319 EvasVideoSinkPrivate* priv;
321 sink = EVAS_VIDEO_SINK(bsink);
324 send = gst_buffer_ref(buffer);
326 priv->preroll = EINA_TRUE;
328 ecore_pipe_write(priv->p, &send, sizeof(buffer));
333 evas_video_sink_render(GstBaseSink* bsink, GstBuffer* buffer)
337 EvasVideoSinkPrivate* priv;
340 sink = EVAS_VIDEO_SINK(bsink);
343 g_mutex_lock(priv->buffer_mutex);
345 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;
389 if (priv->update_size)
391 evas_object_image_size_set(priv->o, priv->width, priv->height);
392 if (!priv->preroll) priv->update_size = FALSE;
395 // This prevent a race condition when data are still in the pipe
396 // but the buffer size as changed because of a request from
397 // emotion smart (like on a file set).
398 evas_object_image_size_get(priv->o, &w, &h);
399 if (w != priv->width || h != priv->height)
402 ev = evas_object_data_get(priv->o, "_emotion_gstreamer_video");
403 if (!ev) goto exit_point;
405 evas_data = (unsigned char *)evas_object_image_data_get(priv->o, 1);
407 // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
408 // Here we convert to Evas's BGRA.
409 if (priv->format == GST_VIDEO_FORMAT_BGR) {
410 unsigned char *evas_tmp;
414 evas_tmp = evas_data;
415 /* FIXME: could this be optimized ? */
416 for (x = 0; x < priv->height; x++) {
417 for (y = 0; y < priv->width; y++) {
418 evas_tmp[0] = gst_data[0];
419 evas_tmp[1] = gst_data[1];
420 evas_tmp[2] = gst_data[2];
428 // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
429 // Here we convert to Evas's BGRA.
430 if (priv->format == GST_VIDEO_FORMAT_BGRx) {
431 unsigned char *evas_tmp;
435 evas_tmp = evas_data;
436 /* FIXME: could this be optimized ? */
437 for (x = 0; x < priv->height; x++) {
438 for (y = 0; y < priv->width; y++) {
439 evas_tmp[0] = gst_data[0];
440 evas_tmp[1] = gst_data[1];
441 evas_tmp[2] = gst_data[2];
449 // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
450 // Here we convert to Evas's BGRA.
451 if (priv->format == GST_VIDEO_FORMAT_BGRA) {
452 unsigned char *evas_tmp;
457 evas_tmp = evas_data;
458 /* FIXME: could this be optimized ? */
459 for (x = 0; x < priv->height; x++) {
460 for (y = 0; y < priv->width; y++) {
462 evas_tmp[0] = (gst_data[0] * alpha) / 255;
463 evas_tmp[1] = (gst_data[1] * alpha) / 255;
464 evas_tmp[2] = (gst_data[2] * alpha) / 255;
472 if (priv->format == GST_VIDEO_FORMAT_I420) {
474 unsigned char **rows;
476 evas_object_image_pixels_dirty_set(priv->o, 1);
477 rows = (unsigned char **)evas_data;
479 for (i = 0; i < priv->height; i++)
480 rows[i] = &gst_data[i * priv->width];
482 rows += priv->height;
483 for (i = 0; i < (priv->height / 2); i++)
484 rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
486 rows += priv->height / 2;
487 for (i = 0; i < (priv->height / 2); i++)
488 rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
491 if (priv->format == GST_VIDEO_FORMAT_YV12) {
493 unsigned char **rows;
495 evas_object_image_pixels_dirty_set(priv->o, 1);
497 rows = (unsigned char **)evas_data;
499 for (i = 0; i < priv->height; i++)
500 rows[i] = &gst_data[i * priv->width];
502 rows += priv->height;
503 for (i = 0; i < (priv->height / 2); i++)
504 rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
506 rows += priv->height / 2;
507 for (i = 0; i < (priv->height / 2); i++)
508 rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
511 evas_object_image_data_update_add(priv->o, 0, 0, priv->width, priv->height);
512 evas_object_image_data_set(priv->o, evas_data);
513 evas_object_image_pixels_dirty_set(priv->o, 0);
515 _emotion_frame_new(ev->obj);
517 vstream = eina_list_nth(ev->video_streams, ev->video_stream_nbr - 1);
519 gst_element_query_position(ev->pipeline, &fmt, &pos);
520 ev->position = (double)pos / (double)GST_SECOND;
522 vstream->width = priv->width;
523 vstream->height = priv->height;
524 ev->ratio = (double) priv->width / (double) priv->height;
526 _emotion_video_pos_update(ev->obj, ev->position, vstream->length_time);
527 _emotion_frame_resize(ev->obj, priv->width, priv->height, ev->ratio);
530 if (priv->last_buffer) gst_buffer_unref(priv->last_buffer);
531 priv->last_buffer = buffer;
533 if (priv->preroll) return ;
535 g_mutex_lock(priv->buffer_mutex);
537 if (priv->unlocked) {
538 g_mutex_unlock(priv->buffer_mutex);
542 g_cond_signal(priv->data_cond);
543 g_mutex_unlock(priv->buffer_mutex);
547 unlock_buffer_mutex(EvasVideoSinkPrivate* priv)
549 g_mutex_lock(priv->buffer_mutex);
551 priv->unlocked = EINA_TRUE;
552 g_cond_signal(priv->data_cond);
553 g_mutex_unlock(priv->buffer_mutex);
557 marshal_VOID__MINIOBJECT(GClosure * closure, GValue * return_value __UNUSED__,
558 guint n_param_values, const GValue * param_values,
559 gpointer invocation_hint __UNUSED__, gpointer marshal_data)
561 typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1, gpointer data2);
562 marshalfunc_VOID__MINIOBJECT callback;
564 gpointer data1, data2;
566 cc = (GCClosure *) closure;
568 g_return_if_fail(n_param_values == 2);
570 if (G_CCLOSURE_SWAP_DATA(closure)) {
571 data1 = closure->data;
572 data2 = g_value_peek_pointer(param_values + 0);
574 data1 = g_value_peek_pointer(param_values + 0);
575 data2 = closure->data;
577 callback = (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data : cc->callback);
579 callback(data1, gst_value_get_mini_object(param_values + 1), data2);
583 evas_video_sink_class_init(EvasVideoSinkClass* klass)
585 GObjectClass* gobject_class;
586 GstBaseSinkClass* gstbase_sink_class;
588 gobject_class = G_OBJECT_CLASS(klass);
589 gstbase_sink_class = GST_BASE_SINK_CLASS(klass);
591 g_type_class_add_private(klass, sizeof(EvasVideoSinkPrivate));
593 gobject_class->set_property = evas_video_sink_set_property;
594 gobject_class->get_property = evas_video_sink_get_property;
596 g_object_class_install_property (gobject_class, PROP_EVAS_OBJECT,
597 g_param_spec_pointer ("evas-object", "Evas Object",
598 "The Evas object where the display of the video will be done",
601 g_object_class_install_property (gobject_class, PROP_WIDTH,
602 g_param_spec_int ("width", "Width",
603 "The width of the video",
604 0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
606 g_object_class_install_property (gobject_class, PROP_HEIGHT,
607 g_param_spec_int ("height", "Height",
608 "The height of the video",
609 0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
611 gobject_class->dispose = evas_video_sink_dispose;
613 gstbase_sink_class->set_caps = evas_video_sink_set_caps;
614 gstbase_sink_class->stop = evas_video_sink_stop;
615 gstbase_sink_class->start = evas_video_sink_start;
616 gstbase_sink_class->unlock = evas_video_sink_unlock;
617 gstbase_sink_class->unlock_stop = evas_video_sink_unlock_stop;
618 gstbase_sink_class->render = evas_video_sink_render;
619 gstbase_sink_class->preroll = evas_video_sink_preroll;
621 evas_video_sink_signals[REPAINT_REQUESTED] = g_signal_new("repaint-requested",
622 G_TYPE_FROM_CLASS(klass),
623 (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
627 marshal_VOID__MINIOBJECT,
628 G_TYPE_NONE, 1, GST_TYPE_BUFFER);
632 gstreamer_plugin_init (GstPlugin * plugin)
634 return gst_element_register (plugin,
637 EVAS_TYPE_VIDEO_SINK);
641 gstreamer_video_sink_new(Emotion_Gstreamer_Video *ev,
648 GstStateChangeReturn res;
650 obj = _emotion_image_get(o);
653 ERR("Not Evas_Object specified");
657 playbin = gst_element_factory_make("playbin2", "playbin");
660 ERR("Unable to create 'playbin' GstElement.");
664 sink = gst_element_factory_make("emotion-sink", "sink");
667 ERR("Unable to create 'emotion-sink' GstElement.");
671 g_object_set(G_OBJECT(playbin), "video-sink", sink, NULL);
672 g_object_set(G_OBJECT(playbin), "uri", uri, NULL);
673 g_object_set(G_OBJECT(sink), "evas-object", obj, NULL);
675 res = gst_element_set_state(playbin, GST_STATE_PAUSED);
676 if (res == GST_STATE_CHANGE_FAILURE)
678 ERR("Unable to set GST_STATE_PAUSED.");
682 res = gst_element_get_state(playbin, NULL, NULL, GST_CLOCK_TIME_NONE);
683 if (res != GST_STATE_CHANGE_SUCCESS)
685 ERR("Unable to get GST_CLOCK_TIME_NONE.");
689 evas_object_data_set(obj, "_emotion_gstreamer_video", ev);
691 /** NOTE: you need to set: GST_DEBUG_DUMP_DOT_DIR=/tmp EMOTION_ENGINE=gstreamer to save the $EMOTION_GSTREAMER_DOT file in '/tmp' */
692 /** then call dot -Tpng -oemotion_pipeline.png /tmp/$TIMESTAMP-$EMOTION_GSTREAMER_DOT.dot */
693 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"));
698 gst_object_unref(playbin);