13 #include <glib-object.h>
14 #include <gst/video/gstvideosink.h>
15 #include <gst/video/video.h>
19 # include <Ecore_Evas.h>
20 # ifdef HAVE_XOVERLAY_H
21 # include <gst/interfaces/xoverlay.h>
26 #include "emotion_private.h"
27 #include "emotion_gstreamer.h"
29 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE("sink",
30 GST_PAD_SINK, GST_PAD_ALWAYS,
31 GST_STATIC_CAPS(GST_VIDEO_CAPS_YUV("{ I420, YV12, YUY2, NV12, ST12, TM12 }") ";"
32 GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_BGR ";" GST_VIDEO_CAPS_BGRA));
34 GST_DEBUG_CATEGORY_STATIC(evas_video_sink_debug);
35 #define GST_CAT_DEFAULT evas_video_sink_debug
51 static guint evas_video_sink_signals[LAST_SIGNAL] = { 0, };
53 #define _do_init(bla) \
54 GST_DEBUG_CATEGORY_INIT(evas_video_sink_debug, \
59 GST_BOILERPLATE_FULL(EvasVideoSink,
66 static void unlock_buffer_mutex(EvasVideoSinkPrivate* priv);
67 static void evas_video_sink_main_render(void *data);
68 static void evas_video_sink_samsung_main_render(void *data);
71 evas_video_sink_base_init(gpointer g_class)
73 GstElementClass* element_class;
75 element_class = GST_ELEMENT_CLASS(g_class);
76 gst_element_class_add_pad_template(element_class, gst_static_pad_template_get(&sinktemplate));
77 gst_element_class_set_details_simple(element_class, "Evas video sink",
78 "Sink/Video", "Sends video data from a GStreamer pipeline to an Evas object",
79 "Vincent Torri <vtorri@univ-evry.fr>");
83 evas_video_sink_init(EvasVideoSink* sink, EvasVideoSinkClass* klass __UNUSED__)
85 EvasVideoSinkPrivate* priv;
88 sink->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE(sink, EVAS_TYPE_VIDEO_SINK, EvasVideoSinkPrivate);
93 priv->eformat = EVAS_COLORSPACE_ARGB8888;
94 priv->samsung = EINA_FALSE;
95 eina_lock_new(&priv->m);
96 eina_condition_new(&priv->c, &priv->m);
97 priv->unlocked = EINA_FALSE;
100 /**** Object methods ****/
102 _cleanup_priv(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
104 EvasVideoSinkPrivate* priv;
108 eina_lock_take(&priv->m);
111 eina_lock_release(&priv->m);
115 evas_video_sink_set_property(GObject * object, guint prop_id,
116 const GValue * value, GParamSpec * pspec)
119 EvasVideoSinkPrivate* priv;
121 sink = EVAS_VIDEO_SINK (object);
125 case PROP_EVAS_OBJECT:
126 eina_lock_take(&priv->m);
127 evas_object_event_callback_del(priv->o, EVAS_CALLBACK_FREE, _cleanup_priv);
128 priv->o = g_value_get_pointer (value);
129 INF("sink set Evas_Object %p.", priv->o);
130 evas_object_event_callback_add(priv->o, EVAS_CALLBACK_FREE, _cleanup_priv, priv);
131 eina_lock_release(&priv->m);
135 eina_lock_take(&priv->m);
136 priv->ev = g_value_get_pointer (value);
138 priv->ev->samsung = EINA_TRUE;
139 eina_lock_release(&priv->m);
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143 ERR("invalid property");
149 evas_video_sink_get_property(GObject * object, guint prop_id,
150 GValue * value, GParamSpec * pspec)
153 EvasVideoSinkPrivate* priv;
155 sink = EVAS_VIDEO_SINK (object);
159 case PROP_EVAS_OBJECT:
160 INF("sink get property.");
161 eina_lock_take(&priv->m);
162 g_value_set_pointer(value, priv->o);
163 eina_lock_release(&priv->m);
166 INF("sink get width.");
167 eina_lock_take(&priv->m);
168 g_value_set_int(value, priv->width);
169 eina_lock_release(&priv->m);
172 INF("sink get height.");
173 eina_lock_take(&priv->m);
174 g_value_set_int (value, priv->height);
175 eina_lock_release(&priv->m);
179 eina_lock_take(&priv->m);
180 g_value_set_pointer (value, priv->ev);
181 eina_lock_release(&priv->m);
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185 ERR("invalide property");
191 evas_video_sink_dispose(GObject* object)
194 EvasVideoSinkPrivate* priv;
198 sink = EVAS_VIDEO_SINK(object);
201 eina_lock_free(&priv->m);
202 eina_condition_free(&priv->c);
204 G_OBJECT_CLASS(parent_class)->dispose(object);
208 /**** BaseSink methods ****/
210 gboolean evas_video_sink_set_caps(GstBaseSink *bsink, GstCaps *caps)
213 EvasVideoSinkPrivate* priv;
214 GstStructure *structure;
215 GstVideoFormat format;
219 sink = EVAS_VIDEO_SINK(bsink);
222 structure = gst_caps_get_structure(caps, 0);
224 if (gst_structure_get_int(structure, "width", (int*) &priv->width)
225 && gst_structure_get_int(structure, "height", (int*) &priv->height)
226 && gst_structure_get_fourcc(structure, "format", &fourcc))
228 priv->source_height = priv->height;
230 for (i = 0; colorspace_fourcc_convertion[i].name != NULL; ++i)
231 if (fourcc == colorspace_fourcc_convertion[i].fourcc)
233 fprintf(stderr, "Found '%s'\n", colorspace_fourcc_convertion[i].name);
234 priv->eformat = colorspace_fourcc_convertion[i].eformat;
235 priv->func = colorspace_fourcc_convertion[i].func;
236 if (colorspace_fourcc_convertion[i].force_height)
238 priv->height = (priv->height >> 1) << 1;
241 priv->ev->kill_buffer = EINA_TRUE;
245 if (fourcc == GST_MAKE_FOURCC('S', 'T', '1', '2'))
247 fprintf(stderr, "Found '%s'\n", "ST12");
248 priv->eformat = EVAS_COLORSPACE_YCBCR420TM12601_PL;
249 priv->samsung = EINA_TRUE;
253 priv->ev->samsung = EINA_TRUE;
254 priv->ev->kill_buffer = EINA_TRUE;
260 INF("fallback code !");
261 if (!gst_video_format_parse_caps(caps, &format, (int*) &priv->width, (int*) &priv->height))
263 ERR("Unable to parse caps.");
267 priv->source_height = priv->height;
269 for (i = 0; colorspace_format_convertion[i].name != NULL; ++i)
270 if (format == colorspace_format_convertion[i].format)
272 fprintf(stderr, "Found '%s'\n", colorspace_format_convertion[i].name);
273 priv->eformat = colorspace_format_convertion[i].eformat;
274 priv->func = colorspace_format_convertion[i].func;
276 priv->ev->kill_buffer = EINA_FALSE;
280 ERR("unsupported : %d\n", format);
285 evas_video_sink_start(GstBaseSink* base_sink)
287 EvasVideoSinkPrivate* priv;
292 priv = EVAS_VIDEO_SINK(base_sink)->priv;
293 eina_lock_take(&priv->m);
297 priv->unlocked = EINA_FALSE;
298 eina_lock_release(&priv->m);
303 evas_video_sink_stop(GstBaseSink* base_sink)
305 EvasVideoSinkPrivate* priv = EVAS_VIDEO_SINK(base_sink)->priv;
309 unlock_buffer_mutex(priv);
314 evas_video_sink_unlock(GstBaseSink* object)
320 sink = EVAS_VIDEO_SINK(object);
322 unlock_buffer_mutex(sink->priv);
324 return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock,
329 evas_video_sink_unlock_stop(GstBaseSink* object)
332 EvasVideoSinkPrivate* priv;
334 sink = EVAS_VIDEO_SINK(object);
337 INF("sink unlock stop");
339 eina_lock_take(&priv->m);
340 priv->unlocked = FALSE;
341 eina_lock_release(&priv->m);
343 return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock_stop,
348 evas_video_sink_preroll(GstBaseSink* bsink, GstBuffer* buffer)
350 Emotion_Gstreamer_Buffer *send;
351 EvasVideoSinkPrivate *priv;
354 INF("sink preroll %p [%i]", GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
356 sink = EVAS_VIDEO_SINK(bsink);
359 if (GST_BUFFER_SIZE(buffer) <= 0 && !priv->samsung)
365 send = emotion_gstreamer_buffer_alloc(priv, buffer, EINA_TRUE);
373 GstStructure *structure;
375 gboolean is_multiplane = FALSE;
377 caps = GST_BUFFER_CAPS(buffer);
378 structure = gst_caps_get_structure (caps, 0);
379 gst_structure_get_boolean(structure, "multiplane", &is_multiplane);
380 gst_caps_unref(caps);
383 priv->func = _evas_video_st12_multiplane;
385 priv->func = _evas_video_st12;
388 ecore_main_loop_thread_safe_call_async(evas_video_sink_samsung_main_render, send);
391 ecore_main_loop_thread_safe_call_async(evas_video_sink_main_render, send);
398 evas_video_sink_render(GstBaseSink* bsink, GstBuffer* buffer)
400 Emotion_Gstreamer_Buffer *send;
401 EvasVideoSinkPrivate *priv;
404 INF("sink render %p", buffer);
406 sink = EVAS_VIDEO_SINK(bsink);
409 eina_lock_take(&priv->m);
411 if (priv->unlocked) {
413 eina_lock_release(&priv->m);
417 send = emotion_gstreamer_buffer_alloc(priv, buffer, EINA_FALSE);
419 eina_lock_release(&priv->m);
420 return GST_FLOW_ERROR;
427 GstStructure *structure;
429 gboolean is_multiplane = FALSE;
431 caps = GST_BUFFER_CAPS(buffer);
432 structure = gst_caps_get_structure (caps, 0);
433 gst_structure_get_boolean(structure, "multiplane", &is_multiplane);
434 gst_caps_unref(caps);
437 priv->func = _evas_video_st12_multiplane;
439 priv->func = _evas_video_st12;
442 ecore_main_loop_thread_safe_call_async(evas_video_sink_samsung_main_render, send);
445 ecore_main_loop_thread_safe_call_async(evas_video_sink_main_render, send);
447 eina_condition_wait(&priv->c);
448 eina_lock_release(&priv->m);
454 _update_emotion_fps(Emotion_Gstreamer_Video *ev)
458 if (!debug_fps) return ;
460 tim = ecore_time_get();
463 if (ev->rlapse == 0.0)
466 ev->flapse = ev->frames;
468 else if ((tim - ev->rlapse) >= 0.5)
470 printf("FRAME: %i, FPS: %3.1f\n",
472 (ev->frames - ev->flapse) / (tim - ev->rlapse));
474 ev->flapse = ev->frames;
479 evas_video_sink_samsung_main_render(void *data)
481 Emotion_Gstreamer_Buffer *send;
482 Emotion_Video_Stream *vstream;
483 EvasVideoSinkPrivate *priv = NULL;
485 unsigned char *evas_data;
486 const guint8 *gst_data;
487 GstFormat fmt = GST_FORMAT_TIME;
489 Eina_Bool preroll = EINA_FALSE;
490 int stride, elevation;
495 if (!send) goto exit_point;
498 buffer = send->frame;
499 preroll = send->preroll;
501 /* frame after cleanup */
502 if (!preroll && !send->ev->last_buffer)
508 if (!priv || !priv->o || priv->unlocked)
513 emotion_gstreamer_buffer_free(send->ev->send);
514 send->ev->send = NULL;
517 if (!send->ev->stream && !send->force)
519 send->ev->send = send;
520 _emotion_frame_new(send->ev->obj);
524 _emotion_gstreamer_video_pipeline_parse(send->ev, EINA_TRUE);
526 /* Getting stride to compute the right size and then fill the object properly */
527 /* Y => [0] and UV in [1] */
528 if (priv->func == _evas_video_st12_multiplane)
530 const GstMultiPlaneImageBuffer *mp_buf = (const GstMultiPlaneImageBuffer *) buffer;
532 stride = mp_buf->stride[0];
533 elevation = mp_buf->elevation[0];
534 priv->width = mp_buf->width[0];
535 priv->height = mp_buf->height[0];
537 gst_data = (const guint8 *) mp_buf;
541 const SCMN_IMGB *imgb = (const SCMN_IMGB *) GST_BUFFER_MALLOCDATA(buffer);
543 stride = imgb->stride[0];
544 elevation = imgb->elevation[0];
545 priv->width = imgb->width[0];
546 priv->height = imgb->height[0];
548 gst_data = (const guint8 *) imgb;
551 evas_object_geometry_get(priv->o, NULL, NULL, &w, &h);
553 send->ev->fill.width = (double) stride / priv->width;
554 send->ev->fill.height = (double) elevation / priv->height;
556 evas_object_image_alpha_set(priv->o, 0);
557 evas_object_image_colorspace_set(priv->o, priv->eformat);
558 evas_object_image_size_set(priv->o, stride, elevation);
560 _update_emotion_fps(send->ev);
562 evas_data = evas_object_image_data_get(priv->o, 1);
565 priv->func(evas_data, gst_data, stride, elevation, elevation);
567 WRN("No way to decode %x colorspace !", priv->eformat);
569 evas_object_image_data_set(priv->o, evas_data);
570 evas_object_image_data_update_add(priv->o, 0, 0, priv->width, priv->height);
571 evas_object_image_pixels_dirty_set(priv->o, 0);
573 if (!preroll && send->ev->play_started)
575 _emotion_playback_started(send->ev->obj);
576 send->ev->play_started = 0;
581 _emotion_frame_new(send->ev->obj);
584 vstream = eina_list_nth(send->ev->video_streams, send->ev->video_stream_nbr - 1);
586 gst_element_query_position(send->ev->pipeline, &fmt, &pos);
587 send->ev->position = (double)pos / (double)GST_SECOND;
591 vstream->width = priv->width;
592 vstream->height = priv->height;
594 _emotion_video_pos_update(send->ev->obj, send->ev->position, vstream->length_time);
597 send->ev->ratio = (double) priv->width / (double) priv->height;
598 _emotion_frame_refill(send->ev->obj, send->ev->fill.width, send->ev->fill.height);
599 _emotion_frame_resize(send->ev->obj, priv->width, priv->height, send->ev->ratio);
601 buffer = gst_buffer_ref(buffer);
602 if (send->ev->last_buffer) gst_buffer_unref(send->ev->last_buffer);
603 send->ev->last_buffer = buffer;
606 emotion_gstreamer_buffer_free(send);
611 if (preroll || !priv->o) return;
614 eina_condition_signal(&priv->c);
619 evas_video_sink_main_render(void *data)
621 Emotion_Gstreamer_Buffer *send;
622 Emotion_Gstreamer_Video *ev = NULL;
623 Emotion_Video_Stream *vstream;
624 EvasVideoSinkPrivate *priv = NULL;
626 unsigned char *evas_data;
627 GstFormat fmt = GST_FORMAT_TIME;
629 Eina_Bool preroll = EINA_FALSE;
633 if (!send) goto exit_point;
636 buffer = send->frame;
637 preroll = send->preroll;
640 /* frame after cleanup */
641 if (!preroll && !ev->last_buffer)
647 if (!priv || !priv->o || priv->unlocked)
650 if (ev->send && send != ev->send)
652 emotion_gstreamer_buffer_free(ev->send);
656 if (!ev->stream && !send->force)
659 _emotion_frame_new(ev->obj);
660 evas_object_image_data_update_add(priv->o, 0, 0, priv->width, priv->height);
664 _emotion_gstreamer_video_pipeline_parse(ev, EINA_TRUE);
666 INF("sink main render [%i, %i] (source height: %i)", priv->width, priv->height, priv->source_height);
668 evas_object_image_alpha_set(priv->o, 0);
669 evas_object_image_colorspace_set(priv->o, priv->eformat);
670 evas_object_image_size_set(priv->o, priv->width, priv->height);
672 evas_data = evas_object_image_data_get(priv->o, 1);
675 priv->func(evas_data, GST_BUFFER_DATA(buffer), priv->width, priv->source_height, priv->height);
677 WRN("No way to decode %x colorspace !", priv->eformat);
679 evas_object_image_data_set(priv->o, evas_data);
680 evas_object_image_data_update_add(priv->o, 0, 0, priv->width, priv->height);
681 evas_object_image_pixels_dirty_set(priv->o, 0);
683 _update_emotion_fps(ev);
685 if (!preroll && ev->play_started)
687 _emotion_playback_started(ev->obj);
688 ev->play_started = 0;
693 _emotion_frame_new(ev->obj);
696 gst_element_query_position(ev->pipeline, &fmt, &pos);
697 ev->position = (double)pos / (double)GST_SECOND;
699 vstream = eina_list_nth(ev->video_streams, ev->video_stream_nbr - 1);
703 vstream->width = priv->width;
704 vstream->height = priv->height;
705 _emotion_video_pos_update(ev->obj, ev->position, vstream->length_time);
708 ev->ratio = (double) priv->width / (double) priv->height;
710 _emotion_frame_resize(ev->obj, priv->width, priv->height, ev->ratio);
712 buffer = gst_buffer_ref(buffer);
713 if (ev->last_buffer) gst_buffer_unref(ev->last_buffer);
714 ev->last_buffer = buffer;
717 emotion_gstreamer_buffer_free(send);
722 if (preroll || !priv->o) return;
725 eina_condition_signal(&priv->c);
730 unlock_buffer_mutex(EvasVideoSinkPrivate* priv)
732 priv->unlocked = EINA_TRUE;
734 eina_condition_signal(&priv->c);
738 marshal_VOID__MINIOBJECT(GClosure * closure, GValue * return_value __UNUSED__,
739 guint n_param_values, const GValue * param_values,
740 gpointer invocation_hint __UNUSED__, gpointer marshal_data)
742 typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1, gpointer data2);
743 marshalfunc_VOID__MINIOBJECT callback;
745 gpointer data1, data2;
747 cc = (GCClosure *) closure;
749 g_return_if_fail(n_param_values == 2);
751 if (G_CCLOSURE_SWAP_DATA(closure)) {
752 data1 = closure->data;
753 data2 = g_value_peek_pointer(param_values + 0);
755 data1 = g_value_peek_pointer(param_values + 0);
756 data2 = closure->data;
758 callback = (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data : cc->callback);
760 callback(data1, gst_value_get_mini_object(param_values + 1), data2);
764 evas_video_sink_class_init(EvasVideoSinkClass* klass)
766 GObjectClass* gobject_class;
767 GstBaseSinkClass* gstbase_sink_class;
769 gobject_class = G_OBJECT_CLASS(klass);
770 gstbase_sink_class = GST_BASE_SINK_CLASS(klass);
772 g_type_class_add_private(klass, sizeof(EvasVideoSinkPrivate));
774 gobject_class->set_property = evas_video_sink_set_property;
775 gobject_class->get_property = evas_video_sink_get_property;
777 g_object_class_install_property (gobject_class, PROP_EVAS_OBJECT,
778 g_param_spec_pointer ("evas-object", "Evas Object",
779 "The Evas object where the display of the video will be done",
782 g_object_class_install_property (gobject_class, PROP_WIDTH,
783 g_param_spec_int ("width", "Width",
784 "The width of the video",
785 0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
787 g_object_class_install_property (gobject_class, PROP_HEIGHT,
788 g_param_spec_int ("height", "Height",
789 "The height of the video",
790 0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
791 g_object_class_install_property (gobject_class, PROP_EV,
792 g_param_spec_pointer ("ev", "Emotion_Gstreamer_Video",
793 "THe internal data of the emotion object",
796 gobject_class->dispose = evas_video_sink_dispose;
798 gstbase_sink_class->set_caps = evas_video_sink_set_caps;
799 gstbase_sink_class->stop = evas_video_sink_stop;
800 gstbase_sink_class->start = evas_video_sink_start;
801 gstbase_sink_class->unlock = evas_video_sink_unlock;
802 gstbase_sink_class->unlock_stop = evas_video_sink_unlock_stop;
803 gstbase_sink_class->render = evas_video_sink_render;
804 gstbase_sink_class->preroll = evas_video_sink_preroll;
806 evas_video_sink_signals[REPAINT_REQUESTED] = g_signal_new("repaint-requested",
807 G_TYPE_FROM_CLASS(klass),
808 (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
812 marshal_VOID__MINIOBJECT,
813 G_TYPE_NONE, 1, GST_TYPE_BUFFER);
817 gstreamer_plugin_init (GstPlugin * plugin)
819 return gst_element_register (plugin,
822 EVAS_TYPE_VIDEO_SINK);
826 _emotion_gstreamer_pause(void *data, Ecore_Thread *thread)
828 Emotion_Gstreamer_Video *ev = data;
831 if (ecore_thread_check(thread) || !ev->pipeline) return ;
833 gst_element_set_state(ev->pipeline, GST_STATE_PAUSED);
834 res = gst_element_get_state(ev->pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
835 if (res == GST_STATE_CHANGE_NO_PREROLL)
837 gst_element_set_state(ev->pipeline, GST_STATE_PLAYING);
838 gst_element_get_state(ev->pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
843 _emotion_gstreamer_cancel(void *data, Ecore_Thread *thread)
845 Emotion_Gstreamer_Video *ev = data;
847 ev->threads = eina_list_remove(ev->threads, thread);
849 if (getenv("EMOTION_GSTREAMER_DOT")) GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(ev->pipeline), GST_DEBUG_GRAPH_SHOW_ALL, getenv("EMOTION_GSTREAMER_DOT"));
851 if (ev->in == ev->out && ev->delete_me)
856 _emotion_gstreamer_end(void *data, Ecore_Thread *thread)
858 Emotion_Gstreamer_Video *ev = data;
860 ev->threads = eina_list_remove(ev->threads, thread);
864 gst_element_set_state(ev->pipeline, GST_STATE_PLAYING);
865 ev->play_started = 1;
868 if (getenv("EMOTION_GSTREAMER_DOT")) GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(ev->pipeline), GST_DEBUG_GRAPH_SHOW_ALL, getenv("EMOTION_GSTREAMER_DOT"));
870 if (ev->in == ev->out && ev->delete_me)
873 _emotion_gstreamer_video_pipeline_parse(data, EINA_TRUE);
877 _video_resize(void *data, Evas_Object *obj __UNUSED__, const Evas_Video_Surface *surface __UNUSED__,
878 Evas_Coord w, Evas_Coord h)
881 Emotion_Gstreamer_Video *ev = data;
883 ecore_x_window_resize(ev->win, w, h);
884 fprintf(stderr, "resize: %i, %i\n", w, h);
889 _video_move(void *data, Evas_Object *obj __UNUSED__, const Evas_Video_Surface *surface __UNUSED__,
890 Evas_Coord x, Evas_Coord y)
893 Emotion_Gstreamer_Video *ev = data;
896 fprintf(stderr, "move: %i, %i\n", x, y);
897 pos[0] = x; pos[1] = y;
898 ecore_x_window_prop_card32_set(ev->win, ECORE_X_ATOM_E_VIDEO_POSITION, pos, 2);
903 /* Much better idea to always feed the XvImageSink and let him handle optimizing the rendering as we do */
905 _block_pad_unlink_cb(GstPad *pad, gboolean blocked, gpointer user_data)
909 Emotion_Gstreamer_Video *ev = user_data;
912 gst_pad_unlink(ev->xvteepad, ev->xvpad);
913 gev = gst_event_new_eos();
914 gst_pad_send_event(ev->xvpad, gev);
915 gst_pad_set_blocked_async(pad, FALSE, _block_pad_unlink_cb, NULL);
920 _block_pad_link_cb(GstPad *pad, gboolean blocked, gpointer user_data)
924 Emotion_Gstreamer_Video *ev = user_data;
926 gst_pad_link(ev->xvteepad, ev->xvpad);
928 gst_element_set_state(ev->xvsink, GST_STATE_PLAYING);
930 gst_element_set_state(ev->xvsink, GST_STATE_PAUSED);
931 gst_pad_set_blocked_async(pad, FALSE, _block_pad_link_cb, NULL);
937 _video_show(void *data, Evas_Object *obj __UNUSED__, const Evas_Video_Surface *surface __UNUSED__)
940 Emotion_Gstreamer_Video *ev = data;
942 fprintf(stderr, "show xv\n");
943 ecore_x_window_show(ev->win);
945 /* gst_pad_set_blocked_async(ev->xvteepad, TRUE, _block_pad_link_cb, ev); */
949 _video_hide(void *data, Evas_Object *obj __UNUSED__, const Evas_Video_Surface *surface __UNUSED__)
952 Emotion_Gstreamer_Video *ev = data;
954 fprintf(stderr, "hide xv\n");
955 ecore_x_window_hide(ev->win);
957 /* gst_pad_set_blocked_async(ev->xvteepad, TRUE, _block_pad_unlink_cb, ev); */
961 _video_update_pixels(void *data, Evas_Object *obj __UNUSED__, const Evas_Video_Surface *surface __UNUSED__)
963 Emotion_Gstreamer_Video *ev = data;
964 Emotion_Gstreamer_Buffer *send;
966 if (!ev->send) return ;
969 send->force = EINA_TRUE;
971 evas_video_sink_main_render(send);
975 _image_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
977 Emotion_Gstreamer_Video *ev = data;
978 Evas_Coord width, height;
979 int image_area, src_area;
982 GstElementFactory *cfactory = NULL;
983 GstElement *convert = NULL, *filter = NULL, *queue = NULL;
984 GstPad *pad = NULL, *teepad = NULL;
985 GstCaps *caps = NULL;
986 Eina_List *l, *engines;
987 const char *ename, *engine = NULL;
989 evas_object_geometry_get(obj, NULL, NULL, &width, &height);
990 image_area = width * height;
991 src_area = ev->src_width * ev->src_height;
992 ratio = (double)image_area / (double)src_area;
994 // when an image is much smaller than original video size,
995 // add fimcconvert element to the pipeline
996 if (ratio < 0.8 && ev->stream && !ev->convert)
998 cfactory = gst_element_factory_find("fimcconvert");
999 if (!cfactory) return;
1001 convert = gst_element_factory_create(cfactory, NULL);
1002 if (!convert) return;
1004 // add capsfilter to limit size and formats based on the backend
1005 filter = gst_element_factory_make("capsfilter", "fimccapsfilter");
1008 gst_object_unref(convert);
1012 engines = evas_render_method_list();
1013 EINA_LIST_FOREACH(engines, l, ename)
1015 if (evas_render_method_lookup(ename) ==
1016 evas_output_method_get(evas_object_evas_get(obj)))
1023 if (strstr(engine, "software") != NULL)
1025 caps = gst_caps_new_simple("video/x-raw-rgb",
1026 "width", G_TYPE_INT, width,
1027 "height", G_TYPE_INT, height,
1030 else if (strstr(engine, "gl") != NULL)
1032 caps = gst_caps_new_simple("video/x-raw-yuv",
1033 "width", G_TYPE_INT, width,
1034 "height", G_TYPE_INT, height,
1037 g_object_set(G_OBJECT(filter), "caps", caps, NULL);
1038 gst_caps_unref(caps);
1040 // add new elements to the pipeline
1041 queue = gst_bin_get_by_name(GST_BIN(ev->sink), "equeue");
1042 gst_element_unlink(ev->tee, queue);
1043 gst_element_release_request_pad(ev->tee, ev->eteepad);
1044 gst_object_unref(ev->eteepad);
1046 gst_bin_add_many(GST_BIN(ev->sink), convert, filter, NULL);
1047 gst_element_link_many(ev->tee, convert, filter, queue, NULL);
1049 pad = gst_element_get_pad(convert, "sink");
1050 teepad = gst_element_get_request_pad(ev->tee, "src%d");
1051 gst_pad_link(teepad, pad);
1052 gst_object_unref(pad);
1054 gst_element_sync_state_with_parent(convert);
1055 gst_element_sync_state_with_parent(filter);
1057 ev->eteepad = teepad;
1058 ev->convert = convert;
1059 evas_render_method_list_free(engines);
1061 INF("add fimcconvert element. video size: %dx%d. emotion object size: %dx%d",
1062 ev->src_width, ev->src_height, width, height);
1064 // set size again to the capsfilter when the image is resized
1065 else if (ev->convert)
1067 filter = gst_bin_get_by_name(GST_BIN(ev->sink), "fimccapsfilter");
1069 engines = evas_render_method_list();
1070 EINA_LIST_FOREACH(engines, l, ename)
1072 if (evas_render_method_lookup(ename) ==
1073 evas_output_method_get(evas_object_evas_get(obj)))
1080 if (strstr(engine, "software") != NULL)
1082 caps = gst_caps_new_simple("video/x-raw-rgb",
1083 "width", G_TYPE_INT, width,
1084 "height", G_TYPE_INT, height,
1087 else if (strstr(engine, "gl") != NULL)
1089 caps = gst_caps_new_simple("video/x-raw-yuv",
1090 "width", G_TYPE_INT, width,
1091 "height", G_TYPE_INT, height,
1095 g_object_set(G_OBJECT(filter), "caps", caps, NULL);
1096 gst_caps_unref(caps);
1097 evas_render_method_list_free(engines);
1099 INF("set capsfilter size again:. video size: %dx%d. emotion object size: %dx%d",
1100 ev->src_width, ev->src_height, width, height);
1105 gstreamer_video_sink_new(Emotion_Gstreamer_Video *ev,
1109 GstElement *playbin;
1110 GstElement *bin = NULL;
1111 GstElement *esink = NULL;
1112 GstElement *xvsink = NULL;
1113 GstElement *tee = NULL;
1114 GstElement *queue = NULL;
1120 #if defined HAVE_ECORE_X && defined HAVE_XOVERLAY_H
1121 const char *engine = NULL;
1125 obj = emotion_object_image_get(o);
1128 ERR("Not Evas_Object specified");
1135 launch = emotion_webcam_custom_get(uri);
1138 GError *error = NULL;
1140 playbin = gst_parse_bin_from_description(launch, 1, &error);
1143 ERR("Unable to setup command : '%s' got error '%s'.", launch, error->message);
1144 g_error_free(error);
1149 WRN("got recoverable error '%s' for command : '%s'.", error->message, launch);
1150 g_error_free(error);
1155 playbin = gst_element_factory_make("playbin2", "playbin");
1158 ERR("Unable to create 'playbin' GstElement.");
1163 bin = gst_bin_new(NULL);
1166 ERR("Unable to create GstBin !");
1167 goto unref_pipeline;
1170 tee = gst_element_factory_make("tee", NULL);
1173 ERR("Unable to create 'tee' GstElement.");
1174 goto unref_pipeline;
1177 #if defined HAVE_ECORE_X && defined HAVE_XOVERLAY_H
1178 if (window_manager_video)
1183 engines = evas_render_method_list();
1185 EINA_LIST_FOREACH(engines, l, ename)
1187 if (evas_render_method_lookup(ename) ==
1188 evas_output_method_get(evas_object_evas_get(obj)))
1195 if (ev->priority && engine && strstr(engine, "_x11") != NULL)
1198 Evas_Coord x, y, w, h;
1200 Ecore_X_Window parent;
1202 evas_object_geometry_get(obj, &x, &y, &w, &h);
1204 ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1209 /* Here we really need to have the help of the window manager, this code will change when we update E17. */
1210 parent = (Ecore_X_Window) ecore_evas_window_get(ee);
1211 fprintf(stderr, "parent: %x\n", parent);
1213 win = ecore_x_window_new(0, x, y, w, h);
1214 fprintf(stderr, "creating window: %x [%i, %i, %i, %i]\n", win, x, y, w, h);
1217 Ecore_X_Window_State state[] = { ECORE_X_WINDOW_STATE_SKIP_TASKBAR, ECORE_X_WINDOW_STATE_SKIP_PAGER };
1219 ecore_x_netwm_window_state_set(win, state, 2);
1220 ecore_x_window_hide(win);
1221 xvsink = gst_element_factory_make("xvimagesink", NULL);
1224 unsigned int pos[2];
1226 #ifdef HAVE_X_OVERLAY_SET
1227 gst_x_overlay_set_window_handle(GST_X_OVERLAY(xvsink), win);
1229 gst_x_overlay_set_xwindow_id(GST_X_OVERLAY(xvsink), win);
1233 ecore_x_window_prop_card32_set(win, ECORE_X_ATOM_E_VIDEO_PARENT, &parent, 1);
1235 pos[0] = x; pos[1] = y;
1236 ecore_x_window_prop_card32_set(win, ECORE_X_ATOM_E_VIDEO_POSITION, pos, 2);
1240 fprintf(stderr, "destroying win: %x\n", win);
1241 ecore_x_window_free(win);
1245 evas_render_method_list_free(engines);
1248 # warning "missing: ecore_x OR xoverlay"
1251 esink = gst_element_factory_make("emotion-sink", "sink");
1254 ERR("Unable to create 'emotion-sink' GstElement.");
1255 goto unref_pipeline;
1258 g_object_set(G_OBJECT(esink), "evas-object", obj, NULL);
1259 g_object_set(G_OBJECT(esink), "ev", ev, NULL);
1261 evas_object_image_pixels_get_callback_set(obj, NULL, NULL);
1262 evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _image_resize, ev);
1264 /* We need queue to force each video sink to be in its own thread */
1265 queue = gst_element_factory_make("queue", "equeue");
1268 ERR("Unable to create 'queue' GstElement.");
1269 goto unref_pipeline;
1272 gst_bin_add_many(GST_BIN(bin), tee, queue, esink, NULL);
1273 gst_element_link_many(queue, esink, NULL);
1275 /* link both sink to GstTee */
1276 pad = gst_element_get_pad(queue, "sink");
1277 teepad = gst_element_get_request_pad(tee, "src%d");
1278 gst_pad_link(teepad, pad);
1279 gst_object_unref(pad);
1281 ev->eteepad = teepad;
1285 GstElement *fakeeos;
1287 queue = gst_element_factory_make("queue", "xvqueue");
1288 fakeeos = GST_ELEMENT(GST_BIN(g_object_new(GST_TYPE_FAKEEOS_BIN, "name", "eosbin", NULL)));
1289 if (queue && fakeeos)
1293 gst_bin_add_many(GST_BIN(bin), fakeeos, NULL);
1295 gst_bin_add_many(GST_BIN(fakeeos), queue, xvsink, NULL);
1296 gst_element_link_many(queue, xvsink, NULL);
1297 queue_pad = gst_element_get_pad(queue, "sink");
1298 gst_element_add_pad(fakeeos, gst_ghost_pad_new("sink", queue_pad));
1300 pad = gst_element_get_pad(fakeeos, "sink");
1301 teepad = gst_element_get_request_pad(tee, "src%d");
1302 gst_pad_link(teepad, pad);
1306 ev->xvteepad = teepad;
1311 if (fakeeos) gst_object_unref(fakeeos);
1312 if (queue) gst_object_unref(queue);
1313 gst_object_unref(xvsink);
1318 teepad = gst_element_get_pad(tee, "sink");
1319 gst_element_add_pad(bin, gst_ghost_pad_new("sink", teepad));
1320 gst_object_unref(teepad);
1322 #define GST_PLAY_FLAG_NATIVE_VIDEO (1 << 6)
1323 #define GST_PLAY_FLAG_DOWNLOAD (1 << 7)
1324 #define GST_PLAY_FLAG_AUDIO (1 << 1)
1325 #define GST_PLAY_FLAG_NATIVE_AUDIO (1 << 5)
1329 g_object_set(G_OBJECT(playbin), "sink", bin, NULL);
1333 g_object_get(G_OBJECT(playbin), "flags", &flags, NULL);
1334 g_object_set(G_OBJECT(playbin), "flags", flags | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_DOWNLOAD | GST_PLAY_FLAG_NATIVE_AUDIO, NULL);
1335 g_object_set(G_OBJECT(playbin), "video-sink", bin, NULL);
1336 g_object_set(G_OBJECT(playbin), "uri", uri, NULL);
1339 evas_object_image_pixels_get_callback_set(obj, NULL, NULL);
1341 ev->stream = EINA_TRUE;
1345 Evas_Video_Surface video;
1347 video.version = EVAS_VIDEO_SURFACE_VERSION;
1349 video.parent = NULL;
1350 video.move = _video_move;
1351 video.resize = _video_resize;
1352 video.show = _video_show;
1353 video.hide = _video_hide;
1354 video.update_pixels = _video_update_pixels;
1356 evas_object_image_video_surface_set(obj, &video);
1357 ev->stream = EINA_FALSE;
1360 eina_stringshare_replace(&ev->uri, uri);
1361 ev->pipeline = playbin;
1364 ev->xvsink = xvsink;
1366 ev->threads = eina_list_append(ev->threads,
1367 ecore_thread_run(_emotion_gstreamer_pause,
1368 _emotion_gstreamer_end,
1369 _emotion_gstreamer_cancel,
1372 /** NOTE: you need to set: GST_DEBUG_DUMP_DOT_DIR=/tmp EMOTION_ENGINE=gstreamer to save the $EMOTION_GSTREAMER_DOT file in '/tmp' */
1373 /** then call dot -Tpng -oemotion_pipeline.png /tmp/$TIMESTAMP-$EMOTION_GSTREAMER_DOT.dot */
1374 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"));
1379 gst_object_unref(xvsink);
1380 gst_object_unref(esink);
1381 gst_object_unref(tee);
1382 gst_object_unref(bin);
1383 gst_object_unref(playbin);