emotion: handle evas NV12 and MT12 colorspace format.
[framework/uifw/emotion.git] / src / modules / gstreamer / emotion_sink.c
index 65b8138..b21f95b 100644 (file)
@@ -1,16 +1,11 @@
-#include <glib.h>
-#include <gst/gst.h>
-#include <gst/video/video.h>
-#include <gst/video/gstvideosink.h>
-
 #include <Ecore.h>
 
 #include "emotion_gstreamer.h"
 
 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE("sink",
                                                                    GST_PAD_SINK, GST_PAD_ALWAYS,
-                                                                   GST_STATIC_CAPS(GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_BGR ";" GST_VIDEO_CAPS_BGRA ";" GST_VIDEO_CAPS_YUV("{I420,YV12}")));
-
+                                                                   GST_STATIC_CAPS(GST_VIDEO_CAPS_YUV("{ I420, YV12, YUY2, NV12, ST12, TM12 }") ";"
+                                                                                   GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_BGR ";" GST_VIDEO_CAPS_BGRA));
 
 GST_DEBUG_CATEGORY_STATIC(evas_video_sink_debug);
 #define GST_CAT_DEFAULT evas_video_sink_debug
@@ -25,35 +20,12 @@ enum {
   PROP_EVAS_OBJECT,
   PROP_WIDTH,
   PROP_HEIGHT,
-  PROP_LAST,
+  PROP_EV,
+  PROP_LAST
 };
 
 static guint evas_video_sink_signals[LAST_SIGNAL] = { 0, };
 
-struct _EvasVideoSinkPrivate {
-   Evas_Object *o;
-   Ecore_Pipe *p;
-
-   int width;
-   int height;
-   GstVideoFormat format;
-
-   GMutex* buffer_mutex;
-   GCond* data_cond;
-
-   // If this is TRUE all processing should finish ASAP
-   // This is necessary because there could be a race between
-   // unlock() and render(), where unlock() wins, signals the
-   // GCond, then render() tries to render a frame although
-   // everything else isn't running anymore. This will lead
-   // to deadlocks because render() holds the stream lock.
-   //
-   // Protected by the buffer mutex
-   Eina_Bool unlocked : 1;
-   Eina_Bool preroll : 1;
-   Eina_Bool update_size : 1;
-};
-
 #define _do_init(bla)                                   \
   GST_DEBUG_CATEGORY_INIT(evas_video_sink_debug,        \
                           "emotion-sink",              \
@@ -69,7 +41,7 @@ GST_BOILERPLATE_FULL(EvasVideoSink,
 
 static void unlock_buffer_mutex(EvasVideoSinkPrivate* priv);
 
-static void evas_video_sink_render_handler(void *data, void *buf, unsigned int len);
+static void evas_video_sink_main_render(void *data);
 
 static void
 evas_video_sink_base_init(gpointer g_class)
@@ -88,21 +60,33 @@ evas_video_sink_init(EvasVideoSink* sink, EvasVideoSinkClass* klass __UNUSED__)
 {
    EvasVideoSinkPrivate* priv;
 
+   INF("sink init");
    sink->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE(sink, EVAS_TYPE_VIDEO_SINK, EvasVideoSinkPrivate);
    priv->o = NULL;
-   priv->p = ecore_pipe_add(evas_video_sink_render_handler, sink);
+   priv->last_buffer = NULL;
    priv->width = 0;
    priv->height = 0;
-   priv->format = GST_VIDEO_FORMAT_UNKNOWN;
-   priv->data_cond = g_cond_new();
-   priv->buffer_mutex = g_mutex_new();
-   priv->preroll = EINA_FALSE;
+   priv->gformat = GST_VIDEO_FORMAT_UNKNOWN;
+   priv->eformat = EVAS_COLORSPACE_ARGB8888;
+   eina_lock_new(&priv->m);
+   eina_condition_new(&priv->c, &priv->m);
    priv->unlocked = EINA_FALSE;
-   priv->update_size = EINA_TRUE;
 }
 
 
 /**** Object methods ****/
+static void
+_cleanup_priv(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
+{
+   EvasVideoSinkPrivate* priv;
+
+   priv = data;
+
+   eina_lock_take(&priv->m);
+   if (priv->o == obj)
+     priv->o = NULL;
+   eina_lock_release(&priv->m);
+}
 
 static void
 evas_video_sink_set_property(GObject * object, guint prop_id,
@@ -116,12 +100,20 @@ evas_video_sink_set_property(GObject * object, guint prop_id,
 
    switch (prop_id) {
     case PROP_EVAS_OBJECT:
-       g_mutex_lock(priv->buffer_mutex);
+       eina_lock_take(&priv->m);
+       evas_object_event_callback_del(priv->o, EVAS_CALLBACK_FREE, _cleanup_priv);
        priv->o = g_value_get_pointer (value);
-       g_mutex_unlock(priv->buffer_mutex);
+       evas_object_event_callback_add(priv->o, EVAS_CALLBACK_FREE, _cleanup_priv, priv);
+       eina_lock_release(&priv->m);
+       break;
+    case PROP_EV:
+       eina_lock_take(&priv->m);
+       priv->ev = g_value_get_pointer (value);
+       eina_lock_release(&priv->m);
        break;
     default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+       ERR("invalid property");
        break;
    }
 }
@@ -138,22 +130,28 @@ evas_video_sink_get_property(GObject * object, guint prop_id,
 
    switch (prop_id) {
     case PROP_EVAS_OBJECT:
-       g_mutex_lock(priv->buffer_mutex);
+       eina_lock_take(&priv->m);
        g_value_set_pointer (value, priv->o);
-       g_mutex_unlock(priv->buffer_mutex);
+       eina_lock_release(&priv->m);
        break;
     case PROP_WIDTH:
-       g_mutex_lock(priv->buffer_mutex);
+       eina_lock_take(&priv->m);
        g_value_set_int(value, priv->width);
-       g_mutex_unlock(priv->buffer_mutex);
+       eina_lock_release(&priv->m);
        break;
     case PROP_HEIGHT:
-       g_mutex_lock(priv->buffer_mutex);
+       eina_lock_take(&priv->m);
        g_value_set_int (value, priv->height);
-       g_mutex_unlock(priv->buffer_mutex);
+       eina_lock_release(&priv->m);
+       break;
+    case PROP_EV:
+       eina_lock_take(&priv->m);
+       g_value_set_pointer (value, priv->ev);
+       eina_lock_release(&priv->m);
        break;
     default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+       ERR("invalide property");
        break;
    }
 }
@@ -167,19 +165,12 @@ evas_video_sink_dispose(GObject* object)
    sink = EVAS_VIDEO_SINK(object);
    priv = sink->priv;
 
-   if (priv->buffer_mutex) {
-      g_mutex_free(priv->buffer_mutex);
-      priv->buffer_mutex = 0;
-   }
-
-   if (priv->data_cond) {
-      g_cond_free(priv->data_cond);
-      priv->data_cond = 0;
-   }
+   eina_lock_free(&priv->m);
+   eina_condition_free(&priv->c);
 
-   if (priv->p) {
-      ecore_pipe_del(priv->p);
-      priv->p = NULL;
+   if (priv->last_buffer) {
+      gst_buffer_unref(priv->last_buffer);
+      priv->last_buffer = NULL;
    }
 
    G_OBJECT_CLASS(parent_class)->dispose(object);
@@ -192,53 +183,75 @@ gboolean evas_video_sink_set_caps(GstBaseSink *bsink, GstCaps *caps)
 {
    EvasVideoSink* sink;
    EvasVideoSinkPrivate* priv;
+   GstStructure *structure;
+   GstVideoFormat format;
+   guint32 fourcc;
    int width;
    int height;
 
    sink = EVAS_VIDEO_SINK(bsink);
    priv = sink->priv;
 
-   if (G_UNLIKELY(!gst_video_format_parse_caps(caps, &priv->format, &width, &height))) {
-      return FALSE;
-   }
-
-   if ((width != priv->width) || (height != priv->height))
+   if (!gst_video_format_parse_caps(caps, &format, &width, &height))
      {
-        priv->width = width;
-        priv->height = height;
-        priv->update_size = EINA_TRUE;
+        ERR("Unable to parse caps.");
+        return FALSE;
      }
 
-   printf("format :");
-   switch (priv->format)
+   priv->width = width;
+   priv->height = height;
+
+   structure = gst_caps_get_structure(caps, 0);
+
+   if (gst_structure_get_fourcc(structure, "format", &fourcc))
      {
-      case GST_VIDEO_FORMAT_I420:
-         evas_object_image_size_set(priv->o, priv->width, priv->height);
-         evas_object_image_fill_set(priv->o, 0, 0, priv->width, priv->height);
-         evas_object_image_colorspace_set(priv->o, EVAS_COLORSPACE_YCBCR422P601_PL);
-         evas_object_image_alpha_set(priv->o, 0);
-         printf ("I420\n");
-         break;
-      case GST_VIDEO_FORMAT_YV12:
-         evas_object_image_size_set(priv->o, priv->width, priv->height);
-         evas_object_image_fill_set(priv->o, 0, 0, priv->width, priv->height);
-         evas_object_image_colorspace_set(priv->o, EVAS_COLORSPACE_YCBCR422P601_PL);
-         evas_object_image_alpha_set(priv->o, 0);
-         printf ("YV12\n");
-         break;
-      case GST_VIDEO_FORMAT_BGR:
-         printf ("BGR\n");
-         break;
-      case GST_VIDEO_FORMAT_BGRx:
-         printf ("BGRx\n");
-         break;
-      case GST_VIDEO_FORMAT_BGRA:
-         printf ("BGRA\n");
-         break;
-      default:
-         printf ("unsupported : %d\n", priv->format);
-         return FALSE;
+        switch (fourcc)
+          {
+           case GST_MAKE_FOURCC('I', '4', '2', '0'):
+              priv->eformat = EVAS_COLORSPACE_YCBCR422P601_PL;
+              printf ("I420\n");
+              break;
+           case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
+              priv->eformat = EVAS_COLORSPACE_YCBCR422P601_PL;
+              printf ("YV12\n");
+              break;
+           case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
+              priv->eformat = EVAS_COLORSPACE_YCBCR422601_PL;
+              printf("YUY2\n");
+              break;
+           case GST_MAKE_FOURCC('N', 'V', '1', '2'):
+              priv->eformat = EVAS_COLORSPACE_YCBCR420NV12601_PL;
+              printf("NV12\n");
+              break;
+           case GST_MAKE_FOURCC('S', 'T', '1', '2'):
+           case GST_MAKE_FOURCC('T', 'M', '1', '2'):
+              priv->eformat = EVAS_COLORSPACE_YCBCR420TM12601_PL;
+              printf("ST12\n");
+              break;
+           default:
+              goto test_format;
+          }
      }
+   else
+     {
+     test_format:
+        switch (format)
+          {
+           case GST_VIDEO_FORMAT_BGR: priv->eformat = EVAS_COLORSPACE_ARGB8888;
+              printf ("BGR\n");
+              break;
+           case GST_VIDEO_FORMAT_BGRx: priv->eformat = EVAS_COLORSPACE_ARGB8888;
+              printf ("BGRx\n");
+              break;
+           case GST_VIDEO_FORMAT_BGRA: priv->eformat = EVAS_COLORSPACE_ARGB8888;
+              printf ("BGRA\n");
+              break;
+           default:
+              ERR("unsupported : %d\n", format);
+              return FALSE;
+          }
+     }
+   priv->gformat = format;
 
    return TRUE;
 }
@@ -250,19 +263,12 @@ evas_video_sink_start(GstBaseSink* base_sink)
    gboolean res = TRUE;
 
    priv = EVAS_VIDEO_SINK(base_sink)->priv;
-   g_mutex_lock(priv->buffer_mutex);
+   eina_lock_take(&priv->m);
    if (!priv->o)
      res = FALSE;
    else
-     {
-        if (!priv->p)
-          res = FALSE;
-        else
-          {
-             priv->unlocked = EINA_FALSE;
-          }
-     }
-   g_mutex_unlock(priv->buffer_mutex);
+     priv->unlocked = EINA_FALSE;
+   eina_lock_release(&priv->m);
    return res;
 }
 
@@ -297,9 +303,9 @@ evas_video_sink_unlock_stop(GstBaseSink* object)
    sink = EVAS_VIDEO_SINK(object);
    priv = sink->priv;
 
-   g_mutex_lock(priv->buffer_mutex);
+   eina_lock_take(&priv->m);
    priv->unlocked = FALSE;
-   g_mutex_unlock(priv->buffer_mutex);
+   eina_lock_release(&priv->m);
 
    return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock_stop,
                                        (object), TRUE);
@@ -308,194 +314,278 @@ evas_video_sink_unlock_stop(GstBaseSink* object)
 static GstFlowReturn
 evas_video_sink_preroll(GstBaseSink* bsink, GstBuffer* buffer)
 {
-   GstBuffer *send;
-   EvasVideoSink* sink;
-   EvasVideoSinkPrivate* priv;
+   Emotion_Gstreamer_Buffer *send;
+   EvasVideoSinkPrivate *priv;
+   EvasVideoSink *sink;
 
    sink = EVAS_VIDEO_SINK(bsink);
    priv = sink->priv;
 
-   send = gst_buffer_ref(buffer);
+   send = emotion_gstreamer_buffer_alloc(priv, buffer, EINA_TRUE);
 
-   priv->preroll = EINA_TRUE;
+   if (send)
+     ecore_main_loop_thread_safe_call_async(evas_video_sink_main_render, send);
 
-   ecore_pipe_write(priv->p, &send, sizeof(buffer));
    return GST_FLOW_OK;
 }
 
 static GstFlowReturn
 evas_video_sink_render(GstBaseSink* bsink, GstBuffer* buffer)
 {
-   GstBuffer *send;
-   EvasVideoSink* sink;
-   EvasVideoSinkPrivate* priv;
-   Eina_Bool ret;
+   Emotion_Gstreamer_Buffer *send;
+   EvasVideoSinkPrivate *priv;
+   EvasVideoSink *sink;
 
    sink = EVAS_VIDEO_SINK(bsink);
    priv = sink->priv;
 
-   g_mutex_lock(priv->buffer_mutex);
+   eina_lock_take(&priv->m);
 
    if (priv->unlocked) {
-      g_mutex_unlock(priv->buffer_mutex);
+      ERR("LOCKED");
+      eina_lock_release(&priv->m);
       return GST_FLOW_OK;
    }
 
-   priv->preroll = EINA_FALSE;
+   send = emotion_gstreamer_buffer_alloc(priv, buffer, EINA_FALSE);
+   if (!send) {
+      eina_lock_release(&priv->m);
+      return GST_FLOW_ERROR;
+   }
 
-   send = gst_buffer_ref(buffer);
-   ret = ecore_pipe_write(priv->p, &send, sizeof(buffer));
-   if (!ret)
-     return GST_FLOW_ERROR;
+   ecore_main_loop_thread_safe_call_async(evas_video_sink_main_render, send);
 
-   g_cond_wait(priv->data_cond, priv->buffer_mutex);
-   g_mutex_unlock(priv->buffer_mutex);
+   eina_condition_wait(&priv->c);
+   eina_lock_release(&priv->m);
 
    return GST_FLOW_OK;
 }
 
-static void evas_video_sink_render_handler(void *data,
-                                           void *buf,
-                                           unsigned int len)
+static void
+evas_video_sink_main_render(void *data)
 {
-   Emotion_Gstreamer_Video *ev;
+   Emotion_Gstreamer_Buffer *send;
+   Emotion_Gstreamer_Video *ev = NULL;
    Emotion_Video_Stream *vstream;
-   EvasVideoSink* sink;
    EvasVideoSinkPrivate* priv;
    GstBuffer* buffer;
    unsigned char *evas_data;
    const guint8 *gst_data;
-   GstQuery *query;
    GstFormat fmt = GST_FORMAT_TIME;
+   Evas_Coord w, h;
    gint64 pos;
+   Eina_Bool preroll;
 
-   sink = (EvasVideoSink *)data;
-   priv = sink->priv;
+   send = data;
 
-   buffer = *((GstBuffer **)buf);
+   priv = send->sink;
+   if (!priv) goto exit_point;
+   if (!priv->o) goto exit_point;
 
-   if (priv->unlocked)
-     goto exit_point;
+   buffer = send->frame;
+   preroll = send->preroll;
+
+   if (priv->unlocked) goto exit_point;
 
    gst_data = GST_BUFFER_DATA(buffer);
    if (!gst_data) goto exit_point;
-   if (priv->update_size)
-     {
-        evas_object_image_size_set(priv->o, priv->width, priv->height);
-        if (!priv->preroll) priv->update_size = FALSE;
-     }
+
+   ev = send->ev;
+   if (!ev) goto exit_point;
+
+   _emotion_gstreamer_video_pipeline_parse(ev, EINA_TRUE);
+
+   // This prevent a race condition when data are still in the pipe
+   // but the buffer size as changed because of a request from
+   // emotion smart (like on a file set).
+   evas_object_image_size_get(priv->o, &w, &h);
+   if (w != priv->width || h != priv->height)
+     goto exit_point;
+
+   evas_object_image_size_set(priv->o, priv->width, priv->height);
+   evas_object_image_alpha_set(priv->o, 0);
+   evas_object_image_colorspace_set(priv->o, priv->eformat);
 
    evas_data = (unsigned char *)evas_object_image_data_get(priv->o, 1);
 
    // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
    // Here we convert to Evas's BGRA.
-   if (priv->format == GST_VIDEO_FORMAT_BGR) {
-      unsigned char *evas_tmp;
-      int x;
-      int y;
-
-      evas_tmp = evas_data;
-      /* FIXME: could this be optimized ? */
-      for (x = 0; x < priv->height; x++) {
-         for (y = 0; y < priv->width; y++) {
-            evas_tmp[0] = gst_data[0];
-            evas_tmp[1] = gst_data[1];
-            evas_tmp[2] = gst_data[2];
-            evas_tmp[3] = 255;
-            gst_data += 3;
-            evas_tmp += 4;
-         }
-      }
-   }
+   switch (priv->gformat)
+     {
+      case GST_VIDEO_FORMAT_BGR:
+        {
+           unsigned char *evas_tmp;
+           int x;
+           int y;
+
+           evas_tmp = evas_data;
+           /* FIXME: could this be optimized ? */
+           for (x = 0; x < priv->height; x++) {
+              for (y = 0; y < priv->width; y++) {
+                 evas_tmp[0] = gst_data[0];
+                 evas_tmp[1] = gst_data[1];
+                 evas_tmp[2] = gst_data[2];
+                 evas_tmp[3] = 255;
+                 gst_data += 3;
+                 evas_tmp += 4;
+              }
+           }
+           break;
+        }
+
+        // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
+        // Here we convert to Evas's BGRA.
+      case GST_VIDEO_FORMAT_BGRx:
+        {
+           unsigned char *evas_tmp;
+           int x;
+           int y;
+
+           evas_tmp = evas_data;
+           /* FIXME: could this be optimized ? */
+           for (x = 0; x < priv->height; x++) {
+              for (y = 0; y < priv->width; y++) {
+                 evas_tmp[0] = gst_data[0];
+                 evas_tmp[1] = gst_data[1];
+                 evas_tmp[2] = gst_data[2];
+                 evas_tmp[3] = 255;
+                 gst_data += 4;
+                 evas_tmp += 4;
+              }
+           }
+           break;
+        }
+
+        // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
+        // Here we convert to Evas's BGRA.
+      case GST_VIDEO_FORMAT_BGRA:
+        {
+           unsigned char *evas_tmp;
+           int x;
+           int y;
+           unsigned char alpha;
+
+           evas_tmp = evas_data;
+           /* FIXME: could this be optimized ? */
+           for (x = 0; x < priv->height; x++) {
+              for (y = 0; y < priv->width; y++) {
+                 alpha = gst_data[3];
+                 evas_tmp[0] = (gst_data[0] * alpha) / 255;
+                 evas_tmp[1] = (gst_data[1] * alpha) / 255;
+                 evas_tmp[2] = (gst_data[2] * alpha) / 255;
+                 evas_tmp[3] = alpha;
+                 gst_data += 4;
+                 evas_tmp += 4;
+              }
+           }
+           break;
+        }
 
-   // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
-   // Here we convert to Evas's BGRA.
-   if (priv->format == GST_VIDEO_FORMAT_BGRx) {
-      unsigned char *evas_tmp;
-      int x;
-      int y;
-
-      evas_tmp = evas_data;
-      /* FIXME: could this be optimized ? */
-      for (x = 0; x < priv->height; x++) {
-         for (y = 0; y < priv->width; y++) {
-            evas_tmp[0] = gst_data[0];
-            evas_tmp[1] = gst_data[1];
-            evas_tmp[2] = gst_data[2];
-            evas_tmp[3] = 255;
-            gst_data += 4;
-            evas_tmp += 4;
-         }
-      }
-   }
+      case GST_VIDEO_FORMAT_I420:
+        {
+           int i;
+           const unsigned char **rows;
 
-   // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
-   // Here we convert to Evas's BGRA.
-   if (priv->format == GST_VIDEO_FORMAT_BGRA) {
-      unsigned char *evas_tmp;
-      int x;
-      int y;
-      unsigned char alpha;
-
-      evas_tmp = evas_data;
-      /* FIXME: could this be optimized ? */
-      for (x = 0; x < priv->height; x++) {
-         for (y = 0; y < priv->width; y++) {
-            alpha = gst_data[3];
-            evas_tmp[0] = (gst_data[0] * alpha) / 255;
-            evas_tmp[1] = (gst_data[1] * alpha) / 255;
-            evas_tmp[2] = (gst_data[2] * alpha) / 255;
-            evas_tmp[3] = alpha;
-            gst_data += 4;
-            evas_tmp += 4;
-         }
-      }
-   }
+           evas_object_image_pixels_dirty_set(priv->o, 1);
+           rows = (const unsigned char **)evas_data;
 
-   if (priv->format == GST_VIDEO_FORMAT_I420) {
-      int i;
-      unsigned char **rows;
+           for (i = 0; i < priv->height; i++)
+             rows[i] = &gst_data[i * priv->width];
 
-      evas_object_image_pixels_dirty_set(priv->o, 1);
-      rows = (unsigned char **)evas_data;
+           rows += priv->height;
+           for (i = 0; i < (priv->height / 2); i++)
+             rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
 
-      for (i = 0; i < priv->height; i++)
-        rows[i] = &gst_data[i * priv->width];
+           rows += priv->height / 2;
+           for (i = 0; i < (priv->height / 2); i++)
+             rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
+           break;
+        }
 
-      rows += priv->height;
-      for (i = 0; i < (priv->height / 2); i++)
-        rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
+      case GST_VIDEO_FORMAT_YV12:
+        {
+           int i;
+           const unsigned char **rows;
 
-      rows += priv->height / 2;
-      for (i = 0; i < (priv->height / 2); i++)
-        rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
-   }
+           evas_object_image_pixels_dirty_set(priv->o, 1);
 
-   if (priv->format == GST_VIDEO_FORMAT_YV12) {
-      int i;
-      unsigned char **rows;
+           rows = (const unsigned char **)evas_data;
 
-      evas_object_image_pixels_dirty_set(priv->o, 1);
+           for (i = 0; i < priv->height; i++)
+             rows[i] = &gst_data[i * priv->width];
 
-      rows = (unsigned char **)evas_data;
+           rows += priv->height;
+           for (i = 0; i < (priv->height / 2); i++)
+             rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
 
-      for (i = 0; i < priv->height; i++)
-        rows[i] = &gst_data[i * priv->width];
+           rows += priv->height / 2;
+           for (i = 0; i < (priv->height / 2); i++)
+             rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
+           break;
+        }
 
-      rows += priv->height;
-      for (i = 0; i < (priv->height / 2); i++)
-        rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
+      case GST_VIDEO_FORMAT_YUY2:
+        {
+           int i;
+           const unsigned char **rows;
 
-      rows += priv->height / 2;
-      for (i = 0; i < (priv->height / 2); i++)
-        rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
-   }
+           evas_object_image_pixels_dirty_set(priv->o, 1);
+
+           rows = (const unsigned char **)evas_data;
+
+           for (i = 0; i < priv->height; i++)
+             rows[i] = &gst_data[i * priv->width * 2];
+           break;
+        }
+
+      default:
+
+         switch (priv->eformat)
+           {
+            case EVAS_COLORSPACE_YCBCR420NV12601_PL:
+             {
+                 int i;
+                 const unsigned char **rows;
+
+                 evas_object_image_pixels_dirty_set(priv->o, 1);
+
+                 rows = (const unsigned char **)evas_data;
+
+                 for (i = 0; i < priv->height; i++)
+                   rows[i] = &gst_data[i * priv->width];
+
+                 rows += priv->height;
+                 for (i = 0; i < (priv->height / 2); i++)
+                   rows[i] = &gst_data[priv->height * priv->width + i * priv->width];
+                 break;
+             }
+            case EVAS_COLORSPACE_YCBCR420NV12T601_PL:
+              {
+                 int i;
+                 const unsigned char **rows;
+
+                 evas_object_image_pixels_dirty_set(priv->o, 1);
+
+                 rows = (const unsigned char **)evas_data;
+
+                 for (i = 0; i < (priv->height / 32) / 2; i++)
+                   rows[i] = &gst_data[i * priv->width * 2 * 32];
+
+                 if ((priv->height / 32) % 2)
+                   rows[i] = &gst_data[i * priv->width * 2 * 32];
+
+                 rows += priv->height;
+                 for (i = 0; i < ((priv->height / 2) / 32) / 2; ++i)
+                   rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2) * 2 * 16];
+                 break;
+              }
+           }
+     }
 
    evas_object_image_data_update_add(priv->o, 0, 0, priv->width, priv->height);
    evas_object_image_data_set(priv->o, evas_data);
    evas_object_image_pixels_dirty_set(priv->o, 0);
 
-   ev = evas_object_data_get(priv->o, "_emotion_gstreamer_video");
    _emotion_frame_new(ev->obj);
 
    vstream = eina_list_nth(ev->video_streams, ev->video_stream_nbr - 1);
@@ -510,30 +600,29 @@ static void evas_video_sink_render_handler(void *data,
    _emotion_video_pos_update(ev->obj, ev->position, vstream->length_time);
    _emotion_frame_resize(ev->obj, priv->width, priv->height, ev->ratio);
 
- exit_point:
-   gst_buffer_unref(buffer);
+   if (priv->last_buffer) gst_buffer_unref(priv->last_buffer);
+   priv->last_buffer = gst_buffer_ref(buffer);
 
-   if (priv->preroll) return ;
+ exit_point:
+   emotion_gstreamer_buffer_free(send);
 
-   g_mutex_lock(priv->buffer_mutex);
+   if (preroll || !priv->o || !ev) return ;
 
-   if (priv->unlocked) {
-      g_mutex_unlock(priv->buffer_mutex);
-      return;
-   }
+   eina_lock_take(&priv->m);
+   if (!priv->unlocked)
+     eina_condition_signal(&priv->c);
 
-   g_cond_signal(priv->data_cond);
-   g_mutex_unlock(priv->buffer_mutex);
+   eina_lock_release(&priv->m);
 }
 
 static void
 unlock_buffer_mutex(EvasVideoSinkPrivate* priv)
 {
-   g_mutex_lock(priv->buffer_mutex);
-
+   eina_lock_take(&priv->m);
    priv->unlocked = EINA_TRUE;
-   g_cond_signal(priv->data_cond);
-   g_mutex_unlock(priv->buffer_mutex);
+
+   eina_condition_signal(&priv->c);
+   eina_lock_release(&priv->m);
 }
 
 static void
@@ -590,6 +679,10 @@ evas_video_sink_class_init(EvasVideoSinkClass* klass)
                                     g_param_spec_int ("height", "Height",
                                                       "The height of the video",
                                                       0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+   g_object_class_install_property (gobject_class, PROP_EV,
+                                    g_param_spec_pointer ("ev", "Emotion_Gstreamer_Video",
+                                                          "THe internal data of the emotion object",
+                                                          G_PARAM_READWRITE));
 
    gobject_class->dispose = evas_video_sink_dispose;
 
@@ -620,6 +713,46 @@ gstreamer_plugin_init (GstPlugin * plugin)
                                 EVAS_TYPE_VIDEO_SINK);
 }
 
+static void
+_emotion_gstreamer_pause(void *data, Ecore_Thread *thread)
+{
+   Emotion_Gstreamer_Video *ev = data;
+
+   if (ecore_thread_check(thread) || !ev->pipeline) return ;
+
+   gst_element_set_state(ev->pipeline, GST_STATE_PAUSED);
+}
+
+static void
+_emotion_gstreamer_cancel(void *data, Ecore_Thread *thread)
+{
+   Emotion_Gstreamer_Video *ev = data;
+
+   ev->threads = eina_list_remove(ev->threads, thread);
+
+   if (ev->in == ev->out && ev->threads == NULL && ev->delete_me)
+     em_shutdown(ev);
+}
+
+static void
+_emotion_gstreamer_end(void *data, Ecore_Thread *thread)
+{
+   Emotion_Gstreamer_Video *ev = data;
+
+   ev->threads = eina_list_remove(ev->threads, thread);
+
+   if (ev->play)
+     {
+        gst_element_set_state(ev->pipeline, GST_STATE_PLAYING);
+        ev->play_started = 1;
+     }
+
+   if (ev->in == ev->out && ev->threads == NULL && ev->delete_me)
+     em_shutdown(ev);
+   else
+     _emotion_gstreamer_video_pipeline_parse(data, EINA_TRUE);
+}
+
 GstElement *
 gstreamer_video_sink_new(Emotion_Gstreamer_Video *ev,
                         Evas_Object *o,
@@ -628,9 +761,8 @@ gstreamer_video_sink_new(Emotion_Gstreamer_Video *ev,
    GstElement *playbin;
    GstElement *sink;
    Evas_Object *obj;
-   GstStateChangeReturn res;
 
-   obj = _emotion_image_get(o);
+   obj = emotion_object_image_get(o);
    if (!obj)
      {
         ERR("Not Evas_Object specified");
@@ -651,25 +783,23 @@ gstreamer_video_sink_new(Emotion_Gstreamer_Video *ev,
         goto unref_pipeline;
      }
 
-   g_object_set(G_OBJECT(playbin), "video-sink", sink, NULL);
-   g_object_set(G_OBJECT(playbin), "uri", uri, NULL);
    g_object_set(G_OBJECT(sink), "evas-object", obj, NULL);
+   g_object_set(G_OBJECT(sink), "ev", ev, NULL);
 
-   res = gst_element_set_state(playbin, GST_STATE_PAUSED);
-   if (res == GST_STATE_CHANGE_FAILURE)
-     {
-        ERR("Unable to set GST_STATE_PAUSED.");
-        goto unref_pipeline;
-     }
-
-   res = gst_element_get_state(playbin, NULL, NULL, GST_CLOCK_TIME_NONE);
-   if (res != GST_STATE_CHANGE_SUCCESS)
-     {
-        ERR("Unable to get GST_CLOCK_TIME_NONE.");
-        goto unref_pipeline;
-     }
+   g_object_set(G_OBJECT(playbin), "video-sink", sink, NULL);
+   g_object_set(G_OBJECT(playbin), "uri", uri, NULL);
 
-   evas_object_data_set(obj, "_emotion_gstreamer_video", ev);
+   ev->pipeline = playbin;
+   ev->sink = sink;
+   ev->threads = eina_list_append(ev->threads,
+                                  ecore_thread_run(_emotion_gstreamer_pause,
+                                                   _emotion_gstreamer_end,
+                                                   _emotion_gstreamer_cancel,
+                                                   ev));
+
+   /** NOTE: you need to set: GST_DEBUG_DUMP_DOT_DIR=/tmp EMOTION_ENGINE=gstreamer to save the $EMOTION_GSTREAMER_DOT file in '/tmp' */
+   /** then call dot -Tpng -oemotion_pipeline.png /tmp/$TIMESTAMP-$EMOTION_GSTREAMER_DOT.dot */
+   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"));
 
    return playbin;