emotion: advertise only on frame size change.
[profile/ivi/emotion.git] / src / modules / gstreamer / emotion_sink.c
1 #include <glib.h>
2 #include <gst/gst.h>
3 #include <gst/video/video.h>
4 #include <gst/video/gstvideosink.h>
5
6 #include <Ecore.h>
7
8 #include "emotion_gstreamer.h"
9
10 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE("sink",
11                                                                    GST_PAD_SINK, GST_PAD_ALWAYS,
12                                                                    GST_STATIC_CAPS(GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_BGR ";" GST_VIDEO_CAPS_BGRA ";" GST_VIDEO_CAPS_YUV("{I420,YV12}")));
13
14
15 GST_DEBUG_CATEGORY_STATIC(evas_video_sink_debug);
16 #define GST_CAT_DEFAULT evas_video_sink_debug
17
18 enum {
19   REPAINT_REQUESTED,
20   LAST_SIGNAL
21 };
22
23 enum {
24   PROP_0,
25   PROP_EVAS_OBJECT,
26   PROP_WIDTH,
27   PROP_HEIGHT,
28   PROP_LAST,
29 };
30
31 static guint evas_video_sink_signals[LAST_SIGNAL] = { 0, };
32
33 struct _EvasVideoSinkPrivate {
34    Evas_Object *o;
35    Ecore_Pipe *p;
36
37    int width;
38    int height;
39    GstVideoFormat format;
40
41    GMutex* buffer_mutex;
42    GCond* data_cond;
43
44    // If this is TRUE all processing should finish ASAP
45    // This is necessary because there could be a race between
46    // unlock() and render(), where unlock() wins, signals the
47    // GCond, then render() tries to render a frame although
48    // everything else isn't running anymore. This will lead
49    // to deadlocks because render() holds the stream lock.
50    //
51    // Protected by the buffer mutex
52    Eina_Bool unlocked : 1;
53    Eina_Bool preroll : 1;
54    Eina_Bool update_size : 1;
55 };
56
57 #define _do_init(bla)                                   \
58   GST_DEBUG_CATEGORY_INIT(evas_video_sink_debug,        \
59                           "emotion-sink",               \
60                           0,                            \
61                           "emotion video sink")
62
63 GST_BOILERPLATE_FULL(EvasVideoSink,
64                      evas_video_sink,
65                      GstVideoSink,
66                      GST_TYPE_VIDEO_SINK,
67                      _do_init);
68
69
70 static void unlock_buffer_mutex(EvasVideoSinkPrivate* priv);
71
72 static void evas_video_sink_render_handler(void *data, void *buf, unsigned int len);
73
74 static void
75 evas_video_sink_base_init(gpointer g_class)
76 {
77    GstElementClass* element_class;
78
79    element_class = GST_ELEMENT_CLASS(g_class);
80    gst_element_class_add_pad_template(element_class, gst_static_pad_template_get(&sinktemplate));
81    gst_element_class_set_details_simple(element_class, "Evas video sink",
82                                         "Sink/Video", "Sends video data from a GStreamer pipeline to an Evas object",
83                                         "Vincent Torri <vtorri@univ-evry.fr>");
84 }
85
86 static void
87 evas_video_sink_init(EvasVideoSink* sink, EvasVideoSinkClass* klass __UNUSED__)
88 {
89    EvasVideoSinkPrivate* priv;
90
91    sink->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE(sink, EVAS_TYPE_VIDEO_SINK, EvasVideoSinkPrivate);
92    priv->o = NULL;
93    priv->p = ecore_pipe_add(evas_video_sink_render_handler, sink);
94    priv->width = 0;
95    priv->height = 0;
96    priv->format = GST_VIDEO_FORMAT_UNKNOWN;
97    priv->data_cond = g_cond_new();
98    priv->buffer_mutex = g_mutex_new();
99    priv->preroll = EINA_FALSE;
100    priv->unlocked = EINA_FALSE;
101    priv->update_size = EINA_TRUE;
102 }
103
104
105 /**** Object methods ****/
106
107 static void
108 evas_video_sink_set_property(GObject * object, guint prop_id,
109                              const GValue * value, GParamSpec * pspec)
110 {
111    EvasVideoSink* sink;
112    EvasVideoSinkPrivate* priv;
113
114    sink = EVAS_VIDEO_SINK (object);
115    priv = sink->priv;
116
117    switch (prop_id) {
118     case PROP_EVAS_OBJECT:
119        g_mutex_lock(priv->buffer_mutex);
120        priv->o = g_value_get_pointer (value);
121        g_mutex_unlock(priv->buffer_mutex);
122        break;
123     default:
124        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
125        break;
126    }
127 }
128
129 static void
130 evas_video_sink_get_property(GObject * object, guint prop_id,
131                              GValue * value, GParamSpec * pspec)
132 {
133    EvasVideoSink* sink;
134    EvasVideoSinkPrivate* priv;
135
136    sink = EVAS_VIDEO_SINK (object);
137    priv = sink->priv;
138
139    switch (prop_id) {
140     case PROP_EVAS_OBJECT:
141        g_mutex_lock(priv->buffer_mutex);
142        g_value_set_pointer (value, priv->o);
143        g_mutex_unlock(priv->buffer_mutex);
144        break;
145     case PROP_WIDTH:
146        g_mutex_lock(priv->buffer_mutex);
147        g_value_set_int(value, priv->width);
148        g_mutex_unlock(priv->buffer_mutex);
149        break;
150     case PROP_HEIGHT:
151        g_mutex_lock(priv->buffer_mutex);
152        g_value_set_int (value, priv->height);
153        g_mutex_unlock(priv->buffer_mutex);
154        break;
155     default:
156        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157        break;
158    }
159 }
160
161 static void
162 evas_video_sink_dispose(GObject* object)
163 {
164    EvasVideoSink* sink;
165    EvasVideoSinkPrivate* priv;
166
167    sink = EVAS_VIDEO_SINK(object);
168    priv = sink->priv;
169
170    if (priv->buffer_mutex) {
171       g_mutex_free(priv->buffer_mutex);
172       priv->buffer_mutex = 0;
173    }
174
175    if (priv->data_cond) {
176       g_cond_free(priv->data_cond);
177       priv->data_cond = 0;
178    }
179
180    if (priv->p) {
181       ecore_pipe_del(priv->p);
182       priv->p = NULL;
183    }
184
185    G_OBJECT_CLASS(parent_class)->dispose(object);
186 }
187
188
189 /**** BaseSink methods ****/
190
191 gboolean evas_video_sink_set_caps(GstBaseSink *bsink, GstCaps *caps)
192 {
193    EvasVideoSink* sink;
194    EvasVideoSinkPrivate* priv;
195    int width;
196    int height;
197
198    sink = EVAS_VIDEO_SINK(bsink);
199    priv = sink->priv;
200
201    if (G_UNLIKELY(!gst_video_format_parse_caps(caps, &priv->format, &width, &height))) {
202       return FALSE;
203    }
204
205    if ((width != priv->width) || (height != priv->height))
206      {
207         priv->width = width;
208         priv->height = height;
209         priv->update_size = EINA_TRUE;
210      }
211
212    printf("format :");
213    switch (priv->format)
214      {
215       case GST_VIDEO_FORMAT_I420:
216          evas_object_image_size_set(priv->o, priv->width, priv->height);
217          evas_object_image_fill_set(priv->o, 0, 0, priv->width, priv->height);
218          evas_object_image_colorspace_set(priv->o, EVAS_COLORSPACE_YCBCR422P601_PL);
219          evas_object_image_alpha_set(priv->o, 0);
220          printf ("I420\n");
221          break;
222       case GST_VIDEO_FORMAT_YV12:
223          evas_object_image_size_set(priv->o, priv->width, priv->height);
224          evas_object_image_fill_set(priv->o, 0, 0, priv->width, priv->height);
225          evas_object_image_colorspace_set(priv->o, EVAS_COLORSPACE_YCBCR422P601_PL);
226          evas_object_image_alpha_set(priv->o, 0);
227          printf ("YV12\n");
228          break;
229       case GST_VIDEO_FORMAT_BGR:
230          printf ("BGR\n");
231          break;
232       case GST_VIDEO_FORMAT_BGRx:
233          printf ("BGRx\n");
234          break;
235       case GST_VIDEO_FORMAT_BGRA:
236          printf ("BGRA\n");
237          break;
238       default:
239          printf ("unsupported : %d\n", priv->format);
240          return FALSE;
241      }
242
243    return TRUE;
244 }
245
246 static gboolean
247 evas_video_sink_start(GstBaseSink* base_sink)
248 {
249    EvasVideoSinkPrivate* priv;
250    gboolean res = TRUE;
251
252    priv = EVAS_VIDEO_SINK(base_sink)->priv;
253    g_mutex_lock(priv->buffer_mutex);
254    if (!priv->o)
255      res = FALSE;
256    else
257      {
258         if (!priv->p)
259           res = FALSE;
260         else
261           {
262              priv->unlocked = EINA_FALSE;
263           }
264      }
265    g_mutex_unlock(priv->buffer_mutex);
266    return res;
267 }
268
269 static gboolean
270 evas_video_sink_stop(GstBaseSink* base_sink)
271 {
272    EvasVideoSinkPrivate* priv = EVAS_VIDEO_SINK(base_sink)->priv;
273
274    unlock_buffer_mutex(priv);
275    return TRUE;
276 }
277
278 static gboolean
279 evas_video_sink_unlock(GstBaseSink* object)
280 {
281    EvasVideoSink* sink;
282
283    sink = EVAS_VIDEO_SINK(object);
284
285    unlock_buffer_mutex(sink->priv);
286
287    return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock,
288                                        (object), TRUE);
289 }
290
291 static gboolean
292 evas_video_sink_unlock_stop(GstBaseSink* object)
293 {
294    EvasVideoSink* sink;
295    EvasVideoSinkPrivate* priv;
296
297    sink = EVAS_VIDEO_SINK(object);
298    priv = sink->priv;
299
300    g_mutex_lock(priv->buffer_mutex);
301    priv->unlocked = FALSE;
302    g_mutex_unlock(priv->buffer_mutex);
303
304    return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock_stop,
305                                        (object), TRUE);
306 }
307
308 static GstFlowReturn
309 evas_video_sink_preroll(GstBaseSink* bsink, GstBuffer* buffer)
310 {
311    GstBuffer *send;
312    EvasVideoSink* sink;
313    EvasVideoSinkPrivate* priv;
314
315    sink = EVAS_VIDEO_SINK(bsink);
316    priv = sink->priv;
317
318    send = gst_buffer_ref(buffer);
319
320    priv->preroll = EINA_TRUE;
321
322    ecore_pipe_write(priv->p, &send, sizeof(buffer));
323    return GST_FLOW_OK;
324 }
325
326 static GstFlowReturn
327 evas_video_sink_render(GstBaseSink* bsink, GstBuffer* buffer)
328 {
329    GstBuffer *send;
330    EvasVideoSink* sink;
331    EvasVideoSinkPrivate* priv;
332    Eina_Bool ret;
333
334    sink = EVAS_VIDEO_SINK(bsink);
335    priv = sink->priv;
336
337    g_mutex_lock(priv->buffer_mutex);
338
339    if (priv->unlocked) {
340       g_mutex_unlock(priv->buffer_mutex);
341       return GST_FLOW_OK;
342    }
343
344    priv->preroll = EINA_FALSE;
345
346    send = gst_buffer_ref(buffer);
347    ret = ecore_pipe_write(priv->p, &send, sizeof(buffer));
348    if (!ret)
349      return GST_FLOW_ERROR;
350
351    g_cond_wait(priv->data_cond, priv->buffer_mutex);
352    g_mutex_unlock(priv->buffer_mutex);
353
354    return GST_FLOW_OK;
355 }
356
357 static void evas_video_sink_render_handler(void *data,
358                                            void *buf,
359                                            unsigned int len)
360 {
361    Emotion_Gstreamer_Video *ev;
362    Emotion_Video_Stream *vstream;
363    EvasVideoSink* sink;
364    EvasVideoSinkPrivate* priv;
365    GstBuffer* buffer;
366    unsigned char *evas_data;
367    const guint8 *gst_data;
368    GstQuery *query;
369    GstFormat fmt = GST_FORMAT_TIME;
370    gint64 pos;
371
372    sink = (EvasVideoSink *)data;
373    priv = sink->priv;
374
375    buffer = *((GstBuffer **)buf);
376
377    if (priv->unlocked)
378      goto exit_point;
379
380    gst_data = GST_BUFFER_DATA(buffer);
381    if (!gst_data) goto exit_point;
382    if (priv->update_size)
383      {
384         evas_object_image_size_set(priv->o, priv->width, priv->height);
385         if (!priv->preroll) priv->update_size = FALSE;
386      }
387
388    evas_data = (unsigned char *)evas_object_image_data_get(priv->o, 1);
389
390    // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
391    // Here we convert to Evas's BGRA.
392    if (priv->format == GST_VIDEO_FORMAT_BGR) {
393       unsigned char *evas_tmp;
394       int x;
395       int y;
396
397       evas_tmp = evas_data;
398       /* FIXME: could this be optimized ? */
399       for (x = 0; x < priv->height; x++) {
400          for (y = 0; y < priv->width; y++) {
401             evas_tmp[0] = gst_data[0];
402             evas_tmp[1] = gst_data[1];
403             evas_tmp[2] = gst_data[2];
404             evas_tmp[3] = 255;
405             gst_data += 3;
406             evas_tmp += 4;
407          }
408       }
409    }
410
411    // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
412    // Here we convert to Evas's BGRA.
413    if (priv->format == GST_VIDEO_FORMAT_BGRx) {
414       unsigned char *evas_tmp;
415       int x;
416       int y;
417
418       evas_tmp = evas_data;
419       /* FIXME: could this be optimized ? */
420       for (x = 0; x < priv->height; x++) {
421          for (y = 0; y < priv->width; y++) {
422             evas_tmp[0] = gst_data[0];
423             evas_tmp[1] = gst_data[1];
424             evas_tmp[2] = gst_data[2];
425             evas_tmp[3] = 255;
426             gst_data += 4;
427             evas_tmp += 4;
428          }
429       }
430    }
431
432    // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
433    // Here we convert to Evas's BGRA.
434    if (priv->format == GST_VIDEO_FORMAT_BGRA) {
435       unsigned char *evas_tmp;
436       int x;
437       int y;
438       unsigned char alpha;
439
440       evas_tmp = evas_data;
441       /* FIXME: could this be optimized ? */
442       for (x = 0; x < priv->height; x++) {
443          for (y = 0; y < priv->width; y++) {
444             alpha = gst_data[3];
445             evas_tmp[0] = (gst_data[0] * alpha) / 255;
446             evas_tmp[1] = (gst_data[1] * alpha) / 255;
447             evas_tmp[2] = (gst_data[2] * alpha) / 255;
448             evas_tmp[3] = alpha;
449             gst_data += 4;
450             evas_tmp += 4;
451          }
452       }
453    }
454
455    if (priv->format == GST_VIDEO_FORMAT_I420) {
456       int i;
457       unsigned char **rows;
458
459       evas_object_image_pixels_dirty_set(priv->o, 1);
460       rows = (unsigned char **)evas_data;
461
462       for (i = 0; i < priv->height; i++)
463         rows[i] = &gst_data[i * priv->width];
464
465       rows += priv->height;
466       for (i = 0; i < (priv->height / 2); i++)
467         rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
468
469       rows += priv->height / 2;
470       for (i = 0; i < (priv->height / 2); i++)
471         rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
472    }
473
474    if (priv->format == GST_VIDEO_FORMAT_YV12) {
475       int i;
476       unsigned char **rows;
477
478       evas_object_image_pixels_dirty_set(priv->o, 1);
479
480       rows = (unsigned char **)evas_data;
481
482       for (i = 0; i < priv->height; i++)
483         rows[i] = &gst_data[i * priv->width];
484
485       rows += priv->height;
486       for (i = 0; i < (priv->height / 2); i++)
487         rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
488
489       rows += priv->height / 2;
490       for (i = 0; i < (priv->height / 2); i++)
491         rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
492    }
493
494    evas_object_image_data_update_add(priv->o, 0, 0, priv->width, priv->height);
495    evas_object_image_data_set(priv->o, evas_data);
496    evas_object_image_pixels_dirty_set(priv->o, 0);
497
498    ev = evas_object_data_get(priv->o, "_emotion_gstreamer_video");
499    _emotion_frame_new(ev->obj);
500
501    vstream = eina_list_nth(ev->video_streams, ev->video_stream_nbr - 1);
502
503    gst_element_query_position(ev->pipeline, &fmt, &pos);
504    ev->position = (double)pos / (double)GST_SECOND;
505
506    vstream->width = priv->width;
507    vstream->height = priv->height;
508    ev->ratio = (double) priv->width / (double) priv->height;
509
510    _emotion_video_pos_update(ev->obj, ev->position, vstream->length_time);
511    _emotion_frame_resize(ev->obj, priv->width, priv->height, ev->ratio);
512
513  exit_point:
514    gst_buffer_unref(buffer);
515
516    if (priv->preroll) return ;
517
518    g_mutex_lock(priv->buffer_mutex);
519
520    if (priv->unlocked) {
521       g_mutex_unlock(priv->buffer_mutex);
522       return;
523    }
524
525    g_cond_signal(priv->data_cond);
526    g_mutex_unlock(priv->buffer_mutex);
527 }
528
529 static void
530 unlock_buffer_mutex(EvasVideoSinkPrivate* priv)
531 {
532    g_mutex_lock(priv->buffer_mutex);
533
534    priv->unlocked = EINA_TRUE;
535    g_cond_signal(priv->data_cond);
536    g_mutex_unlock(priv->buffer_mutex);
537 }
538
539 static void
540 marshal_VOID__MINIOBJECT(GClosure * closure, GValue * return_value __UNUSED__,
541                          guint n_param_values, const GValue * param_values,
542                          gpointer invocation_hint __UNUSED__, gpointer marshal_data)
543 {
544    typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1, gpointer data2);
545    marshalfunc_VOID__MINIOBJECT callback;
546    GCClosure *cc;
547    gpointer data1, data2;
548
549    cc = (GCClosure *) closure;
550
551    g_return_if_fail(n_param_values == 2);
552
553    if (G_CCLOSURE_SWAP_DATA(closure)) {
554       data1 = closure->data;
555       data2 = g_value_peek_pointer(param_values + 0);
556    } else {
557       data1 = g_value_peek_pointer(param_values + 0);
558       data2 = closure->data;
559    }
560    callback = (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data : cc->callback);
561
562    callback(data1, gst_value_get_mini_object(param_values + 1), data2);
563 }
564
565 static void
566 evas_video_sink_class_init(EvasVideoSinkClass* klass)
567 {
568    GObjectClass* gobject_class;
569    GstBaseSinkClass* gstbase_sink_class;
570
571    gobject_class = G_OBJECT_CLASS(klass);
572    gstbase_sink_class = GST_BASE_SINK_CLASS(klass);
573
574    g_type_class_add_private(klass, sizeof(EvasVideoSinkPrivate));
575
576    gobject_class->set_property = evas_video_sink_set_property;
577    gobject_class->get_property = evas_video_sink_get_property;
578
579    g_object_class_install_property (gobject_class, PROP_EVAS_OBJECT,
580                                     g_param_spec_pointer ("evas-object", "Evas Object",
581                                                           "The Evas object where the display of the video will be done",
582                                                           G_PARAM_READWRITE));
583
584    g_object_class_install_property (gobject_class, PROP_WIDTH,
585                                     g_param_spec_int ("width", "Width",
586                                                       "The width of the video",
587                                                       0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
588
589    g_object_class_install_property (gobject_class, PROP_HEIGHT,
590                                     g_param_spec_int ("height", "Height",
591                                                       "The height of the video",
592                                                       0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
593
594    gobject_class->dispose = evas_video_sink_dispose;
595
596    gstbase_sink_class->set_caps = evas_video_sink_set_caps;
597    gstbase_sink_class->stop = evas_video_sink_stop;
598    gstbase_sink_class->start = evas_video_sink_start;
599    gstbase_sink_class->unlock = evas_video_sink_unlock;
600    gstbase_sink_class->unlock_stop = evas_video_sink_unlock_stop;
601    gstbase_sink_class->render = evas_video_sink_render;
602    gstbase_sink_class->preroll = evas_video_sink_preroll;
603
604    evas_video_sink_signals[REPAINT_REQUESTED] = g_signal_new("repaint-requested",
605                                                              G_TYPE_FROM_CLASS(klass),
606                                                              (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
607                                                              0,
608                                                              0,
609                                                              0,
610                                                              marshal_VOID__MINIOBJECT,
611                                                              G_TYPE_NONE, 1, GST_TYPE_BUFFER);
612 }
613
614 gboolean
615 gstreamer_plugin_init (GstPlugin * plugin)
616 {
617    return gst_element_register (plugin,
618                                 "emotion-sink",
619                                 GST_RANK_NONE,
620                                 EVAS_TYPE_VIDEO_SINK);
621 }
622
623 GstElement *
624 gstreamer_video_sink_new(Emotion_Gstreamer_Video *ev,
625                          Evas_Object *o,
626                          const char *uri)
627 {
628    GstElement *playbin;
629    GstElement *sink;
630    Evas_Object *obj;
631    GstStateChangeReturn res;
632
633    obj = _emotion_image_get(o);
634    if (!obj)
635      {
636         ERR("Not Evas_Object specified");
637         return NULL;
638      }
639
640    playbin = gst_element_factory_make("playbin2", "playbin");
641    if (!playbin)
642      {
643         ERR("Unable to create 'playbin' GstElement.");
644         return NULL;
645      }
646
647    sink = gst_element_factory_make("emotion-sink", "sink");
648    if (!sink)
649      {
650         ERR("Unable to create 'emotion-sink' GstElement.");
651         goto unref_pipeline;
652      }
653
654    g_object_set(G_OBJECT(playbin), "video-sink", sink, NULL);
655    g_object_set(G_OBJECT(playbin), "uri", uri, NULL);
656    g_object_set(G_OBJECT(sink), "evas-object", obj, NULL);
657
658    res = gst_element_set_state(playbin, GST_STATE_PAUSED);
659    if (res == GST_STATE_CHANGE_FAILURE)
660      {
661         ERR("Unable to set GST_STATE_PAUSED.");
662         goto unref_pipeline;
663      }
664
665    res = gst_element_get_state(playbin, NULL, NULL, GST_CLOCK_TIME_NONE);
666    if (res != GST_STATE_CHANGE_SUCCESS)
667      {
668         ERR("Unable to get GST_CLOCK_TIME_NONE.");
669         goto unref_pipeline;
670      }
671
672    evas_object_data_set(obj, "_emotion_gstreamer_video", ev);
673
674    return playbin;
675
676  unref_pipeline:
677    gst_object_unref(playbin);
678    return NULL;
679 }