5f7312c192e749cfc0a37520a36683094ba15f6c
[profile/ivi/emotion.git] / src / modules / gstreamer / emotion_sink.c
1 #include "emotion_gstreamer.h"
2
3 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE("sink",
4                                                                    GST_PAD_SINK, GST_PAD_ALWAYS,
5                                                                    GST_STATIC_CAPS(GST_VIDEO_CAPS_YUV("{ I420, YV12, YUY2, NV12, ST12, TM12 }") ";"
6                                                                                    GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_BGR ";" GST_VIDEO_CAPS_BGRA));
7
8 GST_DEBUG_CATEGORY_STATIC(evas_video_sink_debug);
9 #define GST_CAT_DEFAULT evas_video_sink_debug
10
11 enum {
12   REPAINT_REQUESTED,
13   LAST_SIGNAL
14 };
15
16 enum {
17   PROP_0,
18   PROP_EVAS_OBJECT,
19   PROP_WIDTH,
20   PROP_HEIGHT,
21   PROP_EV,
22   PROP_LAST
23 };
24
25 static guint evas_video_sink_signals[LAST_SIGNAL] = { 0, };
26
27 #define _do_init(bla)                                   \
28   GST_DEBUG_CATEGORY_INIT(evas_video_sink_debug,        \
29                           "emotion-sink",               \
30                           0,                            \
31                           "emotion video sink")
32
33 GST_BOILERPLATE_FULL(EvasVideoSink,
34                      evas_video_sink,
35                      GstVideoSink,
36                      GST_TYPE_VIDEO_SINK,
37                      _do_init);
38
39
40 static void unlock_buffer_mutex(EvasVideoSinkPrivate* priv);
41 static void evas_video_sink_main_render(void *data);
42 static void evas_video_sink_samsung_main_render(void *data);
43
44 static void
45 evas_video_sink_base_init(gpointer g_class)
46 {
47    GstElementClass* element_class;
48
49    element_class = GST_ELEMENT_CLASS(g_class);
50    gst_element_class_add_pad_template(element_class, gst_static_pad_template_get(&sinktemplate));
51    gst_element_class_set_details_simple(element_class, "Evas video sink",
52                                         "Sink/Video", "Sends video data from a GStreamer pipeline to an Evas object",
53                                         "Vincent Torri <vtorri@univ-evry.fr>");
54 }
55
56 static void
57 evas_video_sink_init(EvasVideoSink* sink, EvasVideoSinkClass* klass __UNUSED__)
58 {
59    EvasVideoSinkPrivate* priv;
60
61    INF("sink init");
62    sink->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE(sink, EVAS_TYPE_VIDEO_SINK, EvasVideoSinkPrivate);
63    priv->o = NULL;
64    priv->width = 0;
65    priv->height = 0;
66    priv->func = NULL;
67    priv->eformat = EVAS_COLORSPACE_ARGB8888;
68    priv->samsung = EINA_FALSE;
69    eina_lock_new(&priv->m);
70    eina_condition_new(&priv->c, &priv->m);
71    priv->unlocked = EINA_FALSE;
72 }
73
74 /**** Object methods ****/
75 static void
76 _cleanup_priv(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
77 {
78    EvasVideoSinkPrivate* priv;
79
80    priv = data;
81
82    eina_lock_take(&priv->m);
83    if (priv->o == obj)
84      priv->o = NULL;
85    eina_lock_release(&priv->m);
86 }
87
88 static void
89 evas_video_sink_set_property(GObject * object, guint prop_id,
90                              const GValue * value, GParamSpec * pspec)
91 {
92    EvasVideoSink* sink;
93    EvasVideoSinkPrivate* priv;
94
95    sink = EVAS_VIDEO_SINK (object);
96    priv = sink->priv;
97
98    switch (prop_id) {
99     case PROP_EVAS_OBJECT:
100        eina_lock_take(&priv->m);
101        evas_object_event_callback_del(priv->o, EVAS_CALLBACK_FREE, _cleanup_priv);
102        priv->o = g_value_get_pointer (value);
103        INF("sink set Evas_Object %p.", priv->o);
104        evas_object_event_callback_add(priv->o, EVAS_CALLBACK_FREE, _cleanup_priv, priv);
105        eina_lock_release(&priv->m);
106        break;
107     case PROP_EV:
108        INF("sink set ev.");
109        eina_lock_take(&priv->m);
110        priv->ev = g_value_get_pointer (value);
111        if (priv->ev)
112          priv->ev->samsung = EINA_TRUE;
113        eina_lock_release(&priv->m);
114        break;
115     default:
116        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
117        ERR("invalid property");
118        break;
119    }
120 }
121
122 static void
123 evas_video_sink_get_property(GObject * object, guint prop_id,
124                              GValue * value, GParamSpec * pspec)
125 {
126    EvasVideoSink* sink;
127    EvasVideoSinkPrivate* priv;
128
129    sink = EVAS_VIDEO_SINK (object);
130    priv = sink->priv;
131
132    switch (prop_id) {
133     case PROP_EVAS_OBJECT:
134        INF("sink get property.");
135        eina_lock_take(&priv->m);
136        g_value_set_pointer(value, priv->o);
137        eina_lock_release(&priv->m);
138        break;
139     case PROP_WIDTH:
140        INF("sink get width.");
141        eina_lock_take(&priv->m);
142        g_value_set_int(value, priv->width);
143        eina_lock_release(&priv->m);
144        break;
145     case PROP_HEIGHT:
146        INF("sink get height.");
147        eina_lock_take(&priv->m);
148        g_value_set_int (value, priv->height);
149        eina_lock_release(&priv->m);
150        break;
151     case PROP_EV:
152        INF("sink get ev.");
153        eina_lock_take(&priv->m);
154        g_value_set_pointer (value, priv->ev);
155        eina_lock_release(&priv->m);
156        break;
157     default:
158        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159        ERR("invalide property");
160        break;
161    }
162 }
163
164 static void
165 evas_video_sink_dispose(GObject* object)
166 {
167    EvasVideoSink* sink;
168    EvasVideoSinkPrivate* priv;
169
170    INF("dispose.");
171
172    sink = EVAS_VIDEO_SINK(object);
173    priv = sink->priv;
174
175    eina_lock_free(&priv->m);
176    eina_condition_free(&priv->c);
177
178    G_OBJECT_CLASS(parent_class)->dispose(object);
179 }
180
181
182 /**** BaseSink methods ****/
183
184 gboolean evas_video_sink_set_caps(GstBaseSink *bsink, GstCaps *caps)
185 {
186    EvasVideoSink* sink;
187    EvasVideoSinkPrivate* priv;
188    GstStructure *structure;
189    GstVideoFormat format;
190    guint32 fourcc;
191    unsigned int i;
192
193    sink = EVAS_VIDEO_SINK(bsink);
194    priv = sink->priv;
195
196    structure = gst_caps_get_structure(caps, 0);
197
198    if (gst_structure_get_int(structure, "width", (int*) &priv->width)
199        && gst_structure_get_int(structure, "height", (int*) &priv->height)
200        && gst_structure_get_fourcc(structure, "format", &fourcc))
201      {
202         priv->source_height = priv->height;
203
204         for (i = 0; colorspace_fourcc_convertion[i].name != NULL; ++i)
205           if (fourcc == colorspace_fourcc_convertion[i].fourcc)
206             {
207                fprintf(stderr, "Found '%s'\n", colorspace_fourcc_convertion[i].name);
208                priv->eformat = colorspace_fourcc_convertion[i].eformat;
209                priv->func = colorspace_fourcc_convertion[i].func;
210                if (colorspace_fourcc_convertion[i].force_height)
211                  {
212                     priv->height = (priv->height >> 1) << 1;
213                  }
214                if (priv->ev)
215                  priv->ev->kill_buffer = EINA_TRUE;
216                return TRUE;
217             }
218
219         if (fourcc == GST_MAKE_FOURCC('S', 'T', '1', '2'))
220           {
221              fprintf(stderr, "Found '%s'\n", "ST12");
222              priv->eformat = EVAS_COLORSPACE_YCBCR420TM12601_PL;
223              priv->samsung = EINA_TRUE;
224              priv->func = NULL;
225              if (priv->ev)
226                {
227                   priv->ev->samsung = EINA_TRUE;
228                   priv->ev->kill_buffer = EINA_TRUE;
229                }
230              return TRUE;
231           }
232      }
233
234    INF("fallback code !");
235    if (!gst_video_format_parse_caps(caps, &format, (int*) &priv->width, (int*) &priv->height))
236      {
237         ERR("Unable to parse caps.");
238         return FALSE;
239      }
240
241    priv->source_height = priv->height;
242
243    for (i = 0; colorspace_format_convertion[i].name != NULL; ++i)
244      if (format == colorspace_format_convertion[i].format)
245        {
246           fprintf(stderr, "Found '%s'\n", colorspace_format_convertion[i].name);
247           priv->eformat = colorspace_format_convertion[i].eformat;
248           priv->func = colorspace_format_convertion[i].func;
249           if (priv->ev)
250             priv->ev->kill_buffer = EINA_FALSE;
251           return TRUE;
252        }
253
254    ERR("unsupported : %d\n", format);
255    return FALSE;
256 }
257
258 static gboolean
259 evas_video_sink_start(GstBaseSink* base_sink)
260 {
261    EvasVideoSinkPrivate* priv;
262    gboolean res = TRUE;
263
264    INF("sink start");
265
266    priv = EVAS_VIDEO_SINK(base_sink)->priv;
267    eina_lock_take(&priv->m);
268    if (!priv->o)
269      res = FALSE;
270    else
271      priv->unlocked = EINA_FALSE;
272    eina_lock_release(&priv->m);
273    return res;
274 }
275
276 static gboolean
277 evas_video_sink_stop(GstBaseSink* base_sink)
278 {
279    EvasVideoSinkPrivate* priv = EVAS_VIDEO_SINK(base_sink)->priv;
280
281    INF("sink stop");
282
283    unlock_buffer_mutex(priv);
284    return TRUE;
285 }
286
287 static gboolean
288 evas_video_sink_unlock(GstBaseSink* object)
289 {
290    EvasVideoSink* sink;
291
292    INF("sink unlock");
293
294    sink = EVAS_VIDEO_SINK(object);
295
296    unlock_buffer_mutex(sink->priv);
297
298    return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock,
299                                        (object), TRUE);
300 }
301
302 static gboolean
303 evas_video_sink_unlock_stop(GstBaseSink* object)
304 {
305    EvasVideoSink* sink;
306    EvasVideoSinkPrivate* priv;
307
308    sink = EVAS_VIDEO_SINK(object);
309    priv = sink->priv;
310
311    INF("sink unlock stop");
312
313    eina_lock_take(&priv->m);
314    priv->unlocked = FALSE;
315    eina_lock_release(&priv->m);
316
317    return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock_stop,
318                                        (object), TRUE);
319 }
320
321 static GstFlowReturn
322 evas_video_sink_preroll(GstBaseSink* bsink, GstBuffer* buffer)
323 {
324    Emotion_Gstreamer_Buffer *send;
325    EvasVideoSinkPrivate *priv;
326    EvasVideoSink *sink;
327
328    INF("sink preroll %p [%i]", GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
329
330    sink = EVAS_VIDEO_SINK(bsink);
331    priv = sink->priv;
332
333    if (GST_BUFFER_SIZE(buffer) <= 0 && !priv->samsung)
334      {
335         WRN("empty buffer");
336         return GST_FLOW_OK;
337      }
338
339    send = emotion_gstreamer_buffer_alloc(priv, buffer, EINA_TRUE);
340
341    if (send)
342      {
343         if (priv->samsung)
344           {
345              if (!priv->func)
346                {
347                   GstStructure *structure;
348                   GstCaps *caps;
349                   gboolean is_multiplane = FALSE;
350
351                   caps = GST_BUFFER_CAPS(buffer);
352                   structure = gst_caps_get_structure (caps, 0);
353                   gst_structure_get_boolean(structure, "multiplane", &is_multiplane);
354                   gst_caps_unref(caps);
355
356                   if (is_multiplane)
357                     priv->func = _evas_video_st12_multiplane;
358                   else
359                     priv->func = _evas_video_st12;
360                }
361
362              ecore_main_loop_thread_safe_call_async(evas_video_sink_samsung_main_render, send);
363           }
364         else
365           ecore_main_loop_thread_safe_call_async(evas_video_sink_main_render, send);
366      }
367
368    return GST_FLOW_OK;
369 }
370
371 static GstFlowReturn
372 evas_video_sink_render(GstBaseSink* bsink, GstBuffer* buffer)
373 {
374    Emotion_Gstreamer_Buffer *send;
375    EvasVideoSinkPrivate *priv;
376    EvasVideoSink *sink;
377
378    INF("sink render %p", buffer);
379
380    sink = EVAS_VIDEO_SINK(bsink);
381    priv = sink->priv;
382
383    eina_lock_take(&priv->m);
384
385    if (priv->unlocked) {
386       ERR("LOCKED");
387       eina_lock_release(&priv->m);
388       return GST_FLOW_OK;
389    }
390
391    send = emotion_gstreamer_buffer_alloc(priv, buffer, EINA_FALSE);
392    if (!send) {
393       eina_lock_release(&priv->m);
394       return GST_FLOW_ERROR;
395    }
396
397    if (priv->samsung)
398      {
399         if (!priv->func)
400           {
401              GstStructure *structure;
402              GstCaps *caps;
403              gboolean is_multiplane = FALSE;
404
405              caps = GST_BUFFER_CAPS(buffer);
406              structure = gst_caps_get_structure (caps, 0);
407              gst_structure_get_boolean(structure, "multiplane", &is_multiplane);
408              gst_caps_unref(caps);
409
410              if (is_multiplane)
411                priv->func = _evas_video_st12_multiplane;
412              else
413                priv->func = _evas_video_st12;
414           }
415
416         ecore_main_loop_thread_safe_call_async(evas_video_sink_samsung_main_render, send);
417      }
418    else
419      ecore_main_loop_thread_safe_call_async(evas_video_sink_main_render, send);
420
421    eina_condition_wait(&priv->c);
422    eina_lock_release(&priv->m);
423
424    return GST_FLOW_OK;
425 }
426
427 static void
428 _update_emotion_fps(Emotion_Gstreamer_Video *ev)
429 {
430    double tim;
431
432    if (!debug_fps) return ;
433
434    tim = ecore_time_get();
435    ev->frames++;
436
437    if (ev->rlapse == 0.0)
438      {
439         ev->rlapse = tim;
440         ev->flapse = ev->frames;
441      }
442    else if ((tim - ev->rlapse) >= 0.5)
443      {
444         printf("FRAME: %i, FPS: %3.1f\n",
445                ev->frames,
446                (ev->frames - ev->flapse) / (tim - ev->rlapse));
447         ev->rlapse = tim;
448         ev->flapse = ev->frames;
449      }
450 }
451
452 static void
453 evas_video_sink_samsung_main_render(void *data)
454 {
455    Emotion_Gstreamer_Buffer *send;
456    Emotion_Video_Stream *vstream;
457    EvasVideoSinkPrivate* priv;
458    GstBuffer* buffer;
459    unsigned char *evas_data;
460    const guint8 *gst_data;
461    GstFormat fmt = GST_FORMAT_TIME;
462    gint64 pos;
463    Eina_Bool preroll;
464    int stride, elevation;
465    Evas_Coord w, h;
466
467    send = data;
468
469    if (!send) goto exit_point;
470
471    priv = send->sink;
472    buffer = send->frame;
473    preroll = send->preroll;
474
475    if (!priv || !priv->o || priv->unlocked)
476      goto exit_point;
477
478    if (send->ev->send)
479      {
480         emotion_gstreamer_buffer_free(send->ev->send);
481         send->ev->send = NULL;
482      }
483
484    if (!send->ev->stream && !send->force)
485      {
486         send->ev->send = send;
487         _emotion_frame_new(send->ev->obj);
488         goto exit_stream;
489      }
490
491    _emotion_gstreamer_video_pipeline_parse(send->ev, EINA_TRUE);
492
493    /* Getting stride to compute the right size and then fill the object properly */
494    /* Y => [0] and UV in [1] */
495    if (priv->func == _evas_video_st12_multiplane)
496      {
497         const GstMultiPlaneImageBuffer *mp_buf = (const GstMultiPlaneImageBuffer *) buffer;
498
499         stride = mp_buf->stride[0];
500         elevation = mp_buf->elevation[0];
501         priv->width = mp_buf->width[0];
502         priv->height = mp_buf->height[0];
503
504         gst_data = (const guint8 *) mp_buf;
505      }
506    else
507      {
508         const SCMN_IMGB *imgb = (const SCMN_IMGB *) GST_BUFFER_MALLOCDATA(buffer);
509
510         stride = imgb->stride[0];
511         elevation = imgb->elevation[0];
512         priv->width = imgb->width[0];
513         priv->height = imgb->height[0];
514
515         gst_data = (const guint8 *) imgb;
516      }
517
518    evas_object_geometry_get(priv->o, NULL, NULL, &w, &h);
519
520    send->ev->fill.width = (double) stride / priv->width;
521    send->ev->fill.height = (double) elevation / priv->height;
522
523    evas_object_image_alpha_set(priv->o, 0);
524    evas_object_image_colorspace_set(priv->o, priv->eformat);
525    evas_object_image_size_set(priv->o, stride, elevation);
526
527    _update_emotion_fps(send->ev);
528
529    evas_data = evas_object_image_data_get(priv->o, 1);
530
531    if (priv->func)
532      priv->func(evas_data, gst_data, stride, elevation, elevation);
533    else
534      WRN("No way to decode %x colorspace !", priv->eformat);
535
536    evas_object_image_data_set(priv->o, evas_data);
537    evas_object_image_data_update_add(priv->o, 0, 0, priv->width, priv->height);
538    evas_object_image_pixels_dirty_set(priv->o, 0);
539
540    if (!preroll && send->ev->play_started)
541      {
542         _emotion_playback_started(send->ev->obj);
543         send->ev->play_started = 0;
544      }
545
546    if (!send->force)
547      {
548         _emotion_frame_new(send->ev->obj);
549      }
550
551    vstream = eina_list_nth(send->ev->video_streams, send->ev->video_stream_nbr - 1);
552
553    gst_element_query_position(send->ev->pipeline, &fmt, &pos);
554    send->ev->position = (double)pos / (double)GST_SECOND;
555
556    if (vstream)
557      {
558         vstream->width = priv->width;
559         vstream->height = priv->height;
560
561         _emotion_video_pos_update(send->ev->obj, send->ev->position, vstream->length_time);
562      }
563
564    send->ev->ratio = (double) priv->width / (double) priv->height;
565    _emotion_frame_refill(send->ev->obj, send->ev->fill.width, send->ev->fill.height);
566    _emotion_frame_resize(send->ev->obj, priv->width, priv->height, send->ev->ratio);
567
568    buffer = gst_buffer_ref(buffer);
569    if (send->ev->last_buffer) gst_buffer_unref(send->ev->last_buffer);
570    send->ev->last_buffer = buffer;
571
572  exit_point:
573    emotion_gstreamer_buffer_free(send);
574
575  exit_stream:
576    if (preroll || !priv->o) return ;
577
578    if (!priv->unlocked)
579      eina_condition_signal(&priv->c);
580 }
581
582 static void
583 evas_video_sink_main_render(void *data)
584 {
585    Emotion_Gstreamer_Buffer *send;
586    Emotion_Gstreamer_Video *ev = NULL;
587    Emotion_Video_Stream *vstream;
588    EvasVideoSinkPrivate* priv;
589    GstBuffer* buffer;
590    unsigned char *evas_data;
591    GstFormat fmt = GST_FORMAT_TIME;
592    gint64 pos;
593    Eina_Bool preroll;
594
595    send = data;
596
597    if (!send) goto exit_point;
598
599    priv = send->sink;
600    buffer = send->frame;
601    preroll = send->preroll;
602    ev = send->ev;
603
604    if (!priv || !priv->o || priv->unlocked)
605      goto exit_point;
606
607    if (ev->send && send != ev->send)
608      {
609         emotion_gstreamer_buffer_free(ev->send);
610         ev->send = NULL;
611      }
612
613    if (!ev->stream && !send->force)
614      {
615         ev->send = send;
616         _emotion_frame_new(ev->obj);
617         evas_object_image_data_update_add(priv->o, 0, 0, priv->width, priv->height);
618         goto exit_stream;
619      }
620
621    _emotion_gstreamer_video_pipeline_parse(ev, EINA_TRUE);
622
623    INF("sink main render [%i, %i] (source height: %i)", priv->width, priv->height, priv->source_height);
624
625    evas_object_image_alpha_set(priv->o, 0);
626    evas_object_image_colorspace_set(priv->o, priv->eformat);
627    evas_object_image_size_set(priv->o, priv->width, priv->height);
628
629    evas_data = evas_object_image_data_get(priv->o, 1);
630
631    if (priv->func)
632      priv->func(evas_data, GST_BUFFER_DATA(buffer), priv->width, priv->source_height, priv->height);
633    else
634      WRN("No way to decode %x colorspace !", priv->eformat);
635
636    evas_object_image_data_set(priv->o, evas_data);
637    evas_object_image_data_update_add(priv->o, 0, 0, priv->width, priv->height);
638    evas_object_image_pixels_dirty_set(priv->o, 0);
639
640    if (!preroll && ev->play_started)
641      {
642         _emotion_playback_started(ev->obj);
643         ev->play_started = 0;
644      }
645
646    if (!send->force)
647      {
648         _emotion_frame_new(ev->obj);
649      }
650
651    gst_element_query_position(ev->pipeline, &fmt, &pos);
652    ev->position = (double)pos / (double)GST_SECOND;
653
654    vstream = eina_list_nth(ev->video_streams, ev->video_stream_nbr - 1);
655
656    if (vstream)
657      {
658        vstream->width = priv->width;
659        vstream->height = priv->height;
660        _emotion_video_pos_update(ev->obj, ev->position, vstream->length_time);
661      }
662
663    ev->ratio = (double) priv->width / (double) priv->height;
664
665    _emotion_frame_resize(ev->obj, priv->width, priv->height, ev->ratio);
666
667    buffer = gst_buffer_ref(buffer);
668    if (ev->last_buffer) gst_buffer_unref(ev->last_buffer);
669    ev->last_buffer = buffer;
670
671  exit_point:
672    emotion_gstreamer_buffer_free(send);
673
674  exit_stream:
675    if (preroll || !priv->o) return ;
676
677    if (!priv->unlocked)
678      eina_condition_signal(&priv->c);
679 }
680
681 static void
682 unlock_buffer_mutex(EvasVideoSinkPrivate* priv)
683 {
684    priv->unlocked = EINA_TRUE;
685
686    eina_condition_signal(&priv->c);
687 }
688
689 static void
690 marshal_VOID__MINIOBJECT(GClosure * closure, GValue * return_value __UNUSED__,
691                          guint n_param_values, const GValue * param_values,
692                          gpointer invocation_hint __UNUSED__, gpointer marshal_data)
693 {
694    typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1, gpointer data2);
695    marshalfunc_VOID__MINIOBJECT callback;
696    GCClosure *cc;
697    gpointer data1, data2;
698
699    cc = (GCClosure *) closure;
700
701    g_return_if_fail(n_param_values == 2);
702
703    if (G_CCLOSURE_SWAP_DATA(closure)) {
704       data1 = closure->data;
705       data2 = g_value_peek_pointer(param_values + 0);
706    } else {
707       data1 = g_value_peek_pointer(param_values + 0);
708       data2 = closure->data;
709    }
710    callback = (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data : cc->callback);
711
712    callback(data1, gst_value_get_mini_object(param_values + 1), data2);
713 }
714
715 static void
716 evas_video_sink_class_init(EvasVideoSinkClass* klass)
717 {
718    GObjectClass* gobject_class;
719    GstBaseSinkClass* gstbase_sink_class;
720
721    gobject_class = G_OBJECT_CLASS(klass);
722    gstbase_sink_class = GST_BASE_SINK_CLASS(klass);
723
724    g_type_class_add_private(klass, sizeof(EvasVideoSinkPrivate));
725
726    gobject_class->set_property = evas_video_sink_set_property;
727    gobject_class->get_property = evas_video_sink_get_property;
728
729    g_object_class_install_property (gobject_class, PROP_EVAS_OBJECT,
730                                     g_param_spec_pointer ("evas-object", "Evas Object",
731                                                           "The Evas object where the display of the video will be done",
732                                                           G_PARAM_READWRITE));
733
734    g_object_class_install_property (gobject_class, PROP_WIDTH,
735                                     g_param_spec_int ("width", "Width",
736                                                       "The width of the video",
737                                                       0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
738
739    g_object_class_install_property (gobject_class, PROP_HEIGHT,
740                                     g_param_spec_int ("height", "Height",
741                                                       "The height of the video",
742                                                       0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
743    g_object_class_install_property (gobject_class, PROP_EV,
744                                     g_param_spec_pointer ("ev", "Emotion_Gstreamer_Video",
745                                                           "THe internal data of the emotion object",
746                                                           G_PARAM_READWRITE));
747
748    gobject_class->dispose = evas_video_sink_dispose;
749
750    gstbase_sink_class->set_caps = evas_video_sink_set_caps;
751    gstbase_sink_class->stop = evas_video_sink_stop;
752    gstbase_sink_class->start = evas_video_sink_start;
753    gstbase_sink_class->unlock = evas_video_sink_unlock;
754    gstbase_sink_class->unlock_stop = evas_video_sink_unlock_stop;
755    gstbase_sink_class->render = evas_video_sink_render;
756    gstbase_sink_class->preroll = evas_video_sink_preroll;
757
758    evas_video_sink_signals[REPAINT_REQUESTED] = g_signal_new("repaint-requested",
759                                                              G_TYPE_FROM_CLASS(klass),
760                                                              (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
761                                                              0,
762                                                              0,
763                                                              0,
764                                                              marshal_VOID__MINIOBJECT,
765                                                              G_TYPE_NONE, 1, GST_TYPE_BUFFER);
766 }
767
768 gboolean
769 gstreamer_plugin_init (GstPlugin * plugin)
770 {
771    return gst_element_register (plugin,
772                                 "emotion-sink",
773                                 GST_RANK_NONE,
774                                 EVAS_TYPE_VIDEO_SINK);
775 }
776
777 static void
778 _emotion_gstreamer_pause(void *data, Ecore_Thread *thread)
779 {
780    Emotion_Gstreamer_Video *ev = data;
781    gboolean res;
782
783    if (ecore_thread_check(thread) || !ev->pipeline) return ;
784
785    gst_element_set_state(ev->pipeline, GST_STATE_PAUSED);
786    res = gst_element_get_state(ev->pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
787    if (res == GST_STATE_CHANGE_NO_PREROLL)
788      {
789         gst_element_set_state(ev->pipeline, GST_STATE_PLAYING);
790         gst_element_get_state(ev->pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
791      }
792 }
793
794 static void
795 _emotion_gstreamer_cancel(void *data, Ecore_Thread *thread)
796 {
797    Emotion_Gstreamer_Video *ev = data;
798
799    ev->threads = eina_list_remove(ev->threads, thread);
800
801    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"));
802
803    if (ev->in == ev->out && ev->delete_me)
804      em_shutdown(ev);
805 }
806
807 static void
808 _emotion_gstreamer_end(void *data, Ecore_Thread *thread)
809 {
810    Emotion_Gstreamer_Video *ev = data;
811
812    ev->threads = eina_list_remove(ev->threads, thread);
813
814    if (ev->play)
815      {
816         gst_element_set_state(ev->pipeline, GST_STATE_PLAYING);
817         ev->play_started = 1;
818      }
819
820    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"));
821
822    if (ev->in == ev->out && ev->delete_me)
823      em_shutdown(ev);
824    else
825      _emotion_gstreamer_video_pipeline_parse(data, EINA_TRUE);
826 }
827
828 static void
829 _video_resize(void *data, Evas_Object *obj __UNUSED__, const Evas_Video_Surface *surface __UNUSED__,
830               Evas_Coord w, Evas_Coord h)
831 {
832    Emotion_Gstreamer_Video *ev = data;
833
834 #ifdef HAVE_ECORE_X
835    ecore_x_window_resize(ev->win, w, h);
836 #endif
837    fprintf(stderr, "resize: %i, %i\n", w, h);
838 }
839
840 static void
841 _video_move(void *data, Evas_Object *obj __UNUSED__, const Evas_Video_Surface *surface __UNUSED__,
842             Evas_Coord x, Evas_Coord y)
843 {
844    Emotion_Gstreamer_Video *ev = data;
845 #ifdef HAVE_ECORE_X
846    unsigned int pos[2];
847
848    fprintf(stderr, "move: %i, %i\n", x, y);
849    pos[0] = x; pos[1] = y;
850    ecore_x_window_prop_card32_set(ev->win, ECORE_X_ATOM_E_VIDEO_POSITION, pos, 2);
851 #endif
852 }
853
854 #if 0
855 /* Much better idea to always feed the XvImageSink and let him handle optimizing the rendering as we do */
856 static void
857 _block_pad_unlink_cb(GstPad *pad, gboolean blocked, gpointer user_data)
858 {
859    if (blocked)
860      {
861         Emotion_Gstreamer_Video *ev = user_data;
862         GstEvent *gev;
863
864         gst_pad_unlink(ev->teepad, ev->xvpad);
865         gev = gst_event_new_eos();
866         gst_pad_send_event(ev->xvpad, gev);
867         gst_pad_set_blocked_async(pad, FALSE, _block_pad_unlink_cb, NULL);
868      }
869 }
870
871 static void
872 _block_pad_link_cb(GstPad *pad, gboolean blocked, gpointer user_data)
873 {
874    if (blocked)
875      {
876         Emotion_Gstreamer_Video *ev = user_data;
877
878         gst_pad_link(ev->teepad, ev->xvpad);
879         if (ev->play)
880           gst_element_set_state(ev->xvsink, GST_STATE_PLAYING);
881         else
882           gst_element_set_state(ev->xvsink, GST_STATE_PAUSED);
883         gst_pad_set_blocked_async(pad, FALSE, _block_pad_link_cb, NULL);
884      }
885 }
886 #endif
887
888 static void
889 _video_show(void *data, Evas_Object *obj __UNUSED__, const Evas_Video_Surface *surface __UNUSED__)
890 {
891    Emotion_Gstreamer_Video *ev = data;
892
893    fprintf(stderr, "show xv\n");
894 #ifdef HAVE_ECORE_X
895    ecore_x_window_show(ev->win);
896 #endif
897    /* gst_pad_set_blocked_async(ev->teepad, TRUE, _block_pad_link_cb, ev); */
898 }
899
900 static void
901 _video_hide(void *data, Evas_Object *obj __UNUSED__, const Evas_Video_Surface *surface __UNUSED__)
902 {
903    Emotion_Gstreamer_Video *ev = data;
904
905    fprintf(stderr, "hide xv\n");
906 #ifdef HAVE_ECORE_X
907    ecore_x_window_hide(ev->win);
908 #endif
909    /* gst_pad_set_blocked_async(ev->teepad, TRUE, _block_pad_unlink_cb, ev); */
910 }
911
912 static void
913 _video_update_pixels(void *data, Evas_Object *obj __UNUSED__, const Evas_Video_Surface *surface __UNUSED__)
914 {
915    Emotion_Gstreamer_Video *ev = data;
916    Emotion_Gstreamer_Buffer *send;
917
918    if (!ev->send) return ;
919
920    send = ev->send;
921    send->force = EINA_TRUE;
922    ev->send = NULL;
923    evas_video_sink_main_render(send);
924 }
925
926 GstElement *
927 gstreamer_video_sink_new(Emotion_Gstreamer_Video *ev,
928                          Evas_Object *o,
929                          const char *uri)
930 {
931    GstElement *playbin;
932    GstElement *bin = NULL;
933    GstElement *esink = NULL;
934    GstElement *xvsink = NULL;
935    GstElement *tee = NULL;
936    GstElement *queue = NULL;
937    Evas_Object *obj;
938    GstPad *pad;
939    GstPad *teepad;
940    int flags;
941    const char *launch;
942 #if defined HAVE_ECORE_X && defined HAVE_XOVERLAY_H
943    const char *engine;
944    Eina_List *engines;
945 #endif
946
947    obj = emotion_object_image_get(o);
948    if (!obj)
949      {
950         ERR("Not Evas_Object specified");
951         return NULL;
952      }
953
954    if (!uri)
955      return NULL;
956
957    launch = emotion_webcam_custom_get(uri);
958    if (launch)
959      {
960         GError *error = NULL;
961
962         playbin = gst_parse_bin_from_description(launch, 1, &error);
963         if (!playbin)
964           {
965              ERR("Unable to setup command : '%s' got error '%s'.", launch, error->message);
966              g_error_free(error);
967              return NULL;
968           }
969         if (error)
970           {
971              WRN("got recoverable error '%s' for command : '%s'.", error->message, launch);
972              g_error_free(error);
973           }
974      }
975    else
976      {
977         playbin = gst_element_factory_make("playbin2", "playbin");
978         if (!playbin)
979           {
980              ERR("Unable to create 'playbin' GstElement.");
981              return NULL;
982           }
983      }
984
985    bin = gst_bin_new(NULL);
986    if (!bin)
987      {
988        ERR("Unable to create GstBin !");
989        goto unref_pipeline;
990      }
991
992    tee = gst_element_factory_make("tee", NULL);
993    if (!tee)
994      {
995        ERR("Unable to create 'tee' GstElement.");
996        goto unref_pipeline;
997      }
998
999 #if defined HAVE_ECORE_X && defined HAVE_XOVERLAY_H
1000    if (window_manager_video)
1001      {
1002        engines = evas_render_method_list();
1003
1004        engine = eina_list_nth(engines, evas_output_method_get(evas_object_evas_get(obj)) - 1);
1005
1006        if (ev->priority && engine && strstr(engine, "_x11") != NULL)
1007          {
1008            Ecore_Evas *ee;
1009            Evas_Coord x, y, w, h;
1010            Ecore_X_Window win;
1011            Ecore_X_Window parent;
1012
1013            evas_object_geometry_get(obj, &x, &y, &w, &h);
1014
1015            ee = ecore_evas_ecore_evas_get(evas_object_evas_get(obj));
1016
1017            if (w < 4) w = 4;
1018            if (h < 2) h = 2;
1019
1020            /* Here we really need to have the help of the window manager, this code will change when we update E17. */
1021            parent = (Ecore_X_Window) ecore_evas_window_get(ee);
1022            fprintf(stderr, "parent: %x\n", parent);
1023
1024            win = ecore_x_window_new(0, x, y, w, h);
1025            fprintf(stderr, "creating window: %x [%i, %i, %i, %i]\n", win, x, y, w, h);
1026            if (win)
1027              {
1028                Ecore_X_Window_State state[] = { ECORE_X_WINDOW_STATE_SKIP_TASKBAR, ECORE_X_WINDOW_STATE_SKIP_PAGER };
1029
1030                ecore_x_netwm_window_state_set(win, state, 2);
1031                ecore_x_window_hide(win);
1032                xvsink = gst_element_factory_make("xvimagesink", NULL);
1033                if (xvsink)
1034                  {
1035                    unsigned int pos[2];
1036
1037 #ifdef HAVE_X_OVERLAY_SET
1038                    gst_x_overlay_set_window_handle(GST_X_OVERLAY(xvsink), win);
1039 #else
1040                    gst_x_overlay_set_xwindow_id(GST_X_OVERLAY(xvsink), win);
1041 #endif
1042                    ev->win = win;
1043
1044                    ecore_x_window_prop_card32_set(win, ECORE_X_ATOM_E_VIDEO_PARENT, &parent, 1);
1045
1046                    pos[0] = x; pos[1] = y;
1047                    ecore_x_window_prop_card32_set(win, ECORE_X_ATOM_E_VIDEO_POSITION, pos, 2);
1048                  }
1049                else
1050                  {
1051                    fprintf(stderr, "destroying win: %x\n", win);
1052                    ecore_x_window_free(win);
1053                  }
1054              }
1055          }
1056        evas_render_method_list_free(engines);
1057      }
1058 #else
1059 # warning "no ecore_x or xoverlay"
1060 #endif
1061
1062    esink = gst_element_factory_make("emotion-sink", "sink");
1063    if (!esink)
1064      {
1065         ERR("Unable to create 'emotion-sink' GstElement.");
1066         goto unref_pipeline;
1067      }
1068
1069    g_object_set(G_OBJECT(esink), "evas-object", obj, NULL);
1070    g_object_set(G_OBJECT(esink), "ev", ev, NULL);
1071
1072    evas_object_image_pixels_get_callback_set(obj, NULL, NULL);
1073
1074    /* We need queue to force each video sink to be in its own thread */
1075    queue = gst_element_factory_make("queue", NULL);
1076    if (!queue)
1077      {
1078         ERR("Unable to create 'queue' GstElement.");
1079         goto unref_pipeline;
1080      }
1081
1082    gst_bin_add_many(GST_BIN(bin), tee, queue, esink, NULL);
1083    gst_element_link_many(queue, esink, NULL);
1084
1085    /* link both sink to GstTee */
1086    pad = gst_element_get_pad(queue, "sink");
1087    teepad = gst_element_get_request_pad(tee, "src%d");
1088    gst_pad_link(teepad, pad);
1089    gst_object_unref(pad);
1090    gst_object_unref(teepad);
1091
1092    if (xvsink)
1093      {
1094         GstElement *fakeeos;
1095
1096         queue = gst_element_factory_make("queue", NULL);
1097         fakeeos = GST_ELEMENT(GST_BIN(g_object_new(GST_TYPE_FAKEEOS_BIN, "name", "eosbin", NULL)));
1098         if (queue && fakeeos)
1099           {
1100              GstPad *queue_pad;
1101
1102              gst_bin_add_many(GST_BIN(bin), fakeeos, NULL);
1103
1104              gst_bin_add_many(GST_BIN(fakeeos), queue, xvsink, NULL);
1105              gst_element_link_many(queue, xvsink, NULL);
1106              queue_pad = gst_element_get_pad(queue, "sink");
1107              gst_element_add_pad(fakeeos, gst_ghost_pad_new("sink", queue_pad));
1108
1109              pad = gst_element_get_pad(fakeeos, "sink");
1110              teepad = gst_element_get_request_pad(tee, "src%d");
1111              gst_pad_link(teepad, pad);
1112
1113              xvsink = fakeeos;
1114
1115              ev->teepad = teepad;
1116              ev->xvpad = pad;
1117           }
1118         else
1119           {
1120              if (fakeeos) gst_object_unref(fakeeos);
1121              if (queue) gst_object_unref(queue);
1122              gst_object_unref(xvsink);
1123              xvsink = NULL;
1124           }
1125      }
1126
1127    teepad = gst_element_get_pad(tee, "sink");
1128    gst_element_add_pad(bin, gst_ghost_pad_new("sink", teepad));
1129    gst_object_unref(teepad);
1130
1131 #define GST_PLAY_FLAG_NATIVE_VIDEO  (1 << 6)
1132 #define GST_PLAY_FLAG_DOWNLOAD      (1 << 7)
1133 #define GST_PLAY_FLAG_AUDIO         (1 << 1)
1134 #define GST_PLAY_FLAG_NATIVE_AUDIO  (1 << 5)
1135
1136    if (launch)
1137      {
1138         g_object_set(G_OBJECT(playbin), "sink", bin, NULL);
1139      }
1140    else
1141      {
1142         g_object_get(G_OBJECT(playbin), "flags", &flags, NULL);
1143         g_object_set(G_OBJECT(playbin), "flags", flags | GST_PLAY_FLAG_NATIVE_VIDEO | GST_PLAY_FLAG_DOWNLOAD | GST_PLAY_FLAG_NATIVE_AUDIO, NULL);
1144         g_object_set(G_OBJECT(playbin), "video-sink", bin, NULL);
1145         g_object_set(G_OBJECT(playbin), "uri", uri, NULL);
1146      }
1147
1148    evas_object_image_pixels_get_callback_set(obj, NULL, NULL);
1149
1150    ev->stream = EINA_TRUE;
1151
1152    if (xvsink)
1153      {
1154         Evas_Video_Surface video;
1155
1156         video.version = EVAS_VIDEO_SURFACE_VERSION;
1157         video.data = ev;
1158         video.parent = NULL;
1159         video.move = _video_move;
1160         video.resize = _video_resize;
1161         video.show = _video_show;
1162         video.hide = _video_hide;
1163         video.update_pixels = _video_update_pixels;
1164
1165         evas_object_image_video_surface_set(obj, &video);
1166         ev->stream = EINA_FALSE;
1167      }
1168
1169    eina_stringshare_replace(&ev->uri, uri);
1170    ev->pipeline = playbin;
1171    ev->sink = bin;
1172    ev->esink = esink;
1173    ev->xvsink = xvsink;
1174    ev->tee = tee;
1175    ev->threads = eina_list_append(ev->threads,
1176                                   ecore_thread_run(_emotion_gstreamer_pause,
1177                                                    _emotion_gstreamer_end,
1178                                                    _emotion_gstreamer_cancel,
1179                                                    ev));
1180
1181    /** NOTE: you need to set: GST_DEBUG_DUMP_DOT_DIR=/tmp EMOTION_ENGINE=gstreamer to save the $EMOTION_GSTREAMER_DOT file in '/tmp' */
1182    /** then call dot -Tpng -oemotion_pipeline.png /tmp/$TIMESTAMP-$EMOTION_GSTREAMER_DOT.dot */
1183    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"));
1184
1185    return playbin;
1186
1187  unref_pipeline:
1188    gst_object_unref(xvsink);
1189    gst_object_unref(esink);
1190    gst_object_unref(tee);
1191    gst_object_unref(bin);
1192    gst_object_unref(playbin);
1193    return NULL;
1194 }