emotion: advertise native resolution.
[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    gboolean update_size;
40    GstVideoFormat format;
41
42    GMutex* buffer_mutex;
43    GCond* data_cond;
44
45    // If this is TRUE all processing should finish ASAP
46    // This is necessary because there could be a race between
47    // unlock() and render(), where unlock() wins, signals the
48    // GCond, then render() tries to render a frame although
49    // everything else isn't running anymore. This will lead
50    // to deadlocks because render() holds the stream lock.
51    //
52    // Protected by the buffer mutex
53    Eina_Bool unlocked : 1;
54    Eina_Bool preroll : 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->update_size = TRUE;
97    priv->format = GST_VIDEO_FORMAT_UNKNOWN;
98    priv->data_cond = g_cond_new();
99    priv->buffer_mutex = g_mutex_new();
100    priv->preroll = EINA_FALSE;
101    priv->unlocked = EINA_FALSE;
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 = 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         evas_object_image_fill_set(priv->o, 0, 0, priv->width, priv->height);
386         priv->update_size = FALSE;
387      }
388
389    evas_data = (unsigned char *)evas_object_image_data_get(priv->o, 1);
390
391    // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
392    // Here we convert to Evas's BGRA.
393    if (priv->format == GST_VIDEO_FORMAT_BGR) {
394       unsigned char *evas_tmp;
395       int x;
396       int y;
397
398       evas_tmp = evas_data;
399       /* FIXME: could this be optimized ? */
400       for (x = 0; x < priv->height; x++) {
401          for (y = 0; y < priv->width; y++) {
402             evas_tmp[0] = gst_data[0];
403             evas_tmp[1] = gst_data[1];
404             evas_tmp[2] = gst_data[2];
405             evas_tmp[3] = 255;
406             gst_data += 3;
407             evas_tmp += 4;
408          }
409       }
410    }
411
412    // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
413    // Here we convert to Evas's BGRA.
414    if (priv->format == GST_VIDEO_FORMAT_BGRx) {
415       unsigned char *evas_tmp;
416       int x;
417       int y;
418
419       evas_tmp = evas_data;
420       /* FIXME: could this be optimized ? */
421       for (x = 0; x < priv->height; x++) {
422          for (y = 0; y < priv->width; y++) {
423             evas_tmp[0] = gst_data[0];
424             evas_tmp[1] = gst_data[1];
425             evas_tmp[2] = gst_data[2];
426             evas_tmp[3] = 255;
427             gst_data += 4;
428             evas_tmp += 4;
429          }
430       }
431    }
432
433    // Evas's BGRA has pre-multiplied alpha while GStreamer's doesn't.
434    // Here we convert to Evas's BGRA.
435    if (priv->format == GST_VIDEO_FORMAT_BGRA) {
436       unsigned char *evas_tmp;
437       int x;
438       int y;
439       unsigned char alpha;
440
441       evas_tmp = evas_data;
442       /* FIXME: could this be optimized ? */
443       for (x = 0; x < priv->height; x++) {
444          for (y = 0; y < priv->width; y++) {
445             alpha = gst_data[3];
446             evas_tmp[0] = (gst_data[0] * alpha) / 255;
447             evas_tmp[1] = (gst_data[1] * alpha) / 255;
448             evas_tmp[2] = (gst_data[2] * alpha) / 255;
449             evas_tmp[3] = alpha;
450             gst_data += 4;
451             evas_tmp += 4;
452          }
453       }
454    }
455
456    if (priv->format == GST_VIDEO_FORMAT_I420) {
457       int i;
458       unsigned char **rows;
459
460       evas_object_image_pixels_dirty_set(priv->o, 1);
461       rows = (unsigned char **)evas_data;
462
463       for (i = 0; i < priv->height; i++)
464         rows[i] = &gst_data[i * priv->width];
465
466       rows += priv->height;
467       for (i = 0; i < (priv->height / 2); i++)
468         rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
469
470       rows += priv->height / 2;
471       for (i = 0; i < (priv->height / 2); i++)
472         rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
473    }
474
475    if (priv->format == GST_VIDEO_FORMAT_YV12) {
476       int i;
477       unsigned char **rows;
478
479       evas_object_image_pixels_dirty_set(priv->o, 1);
480
481       rows = (unsigned char **)evas_data;
482
483       for (i = 0; i < priv->height; i++)
484         rows[i] = &gst_data[i * priv->width];
485
486       rows += priv->height;
487       for (i = 0; i < (priv->height / 2); i++)
488         rows[i] = &gst_data[priv->height * priv->width + priv->height * (priv->width /4) + i * (priv->width / 2)];
489
490       rows += priv->height / 2;
491       for (i = 0; i < (priv->height / 2); i++)
492         rows[i] = &gst_data[priv->height * priv->width + i * (priv->width / 2)];
493    }
494
495    evas_object_image_data_update_add(priv->o, 0, 0, priv->width, priv->height);
496    evas_object_image_data_set(priv->o, evas_data);
497    evas_object_image_pixels_dirty_set(priv->o, 0);
498
499    ev = evas_object_data_get(priv->o, "_emotion_gstreamer_video");
500    _emotion_frame_new(ev->obj);
501
502    vstream = eina_list_nth(ev->video_streams, ev->video_stream_nbr - 1);
503
504    gst_element_query_position(ev->pipeline, &fmt, &pos);
505    ev->position = (double)pos / (double)GST_SECOND;
506
507    vstream->width = priv->width;
508    vstream->height = priv->height;
509    ev->ratio = (double) priv->width / (double) priv->height;
510
511    _emotion_video_pos_update(ev->obj, ev->position, vstream->length_time);
512    _emotion_frame_resize(ev->obj, priv->width, priv->height, ev->ratio);
513
514  exit_point:
515    gst_buffer_unref(buffer);
516
517    if (priv->preroll) return ;
518
519    g_mutex_lock(priv->buffer_mutex);
520
521    if (priv->unlocked) {
522       g_mutex_unlock(priv->buffer_mutex);
523       return;
524    }
525
526    g_cond_signal(priv->data_cond);
527    g_mutex_unlock(priv->buffer_mutex);
528 }
529
530 static void
531 unlock_buffer_mutex(EvasVideoSinkPrivate* priv)
532 {
533    g_mutex_lock(priv->buffer_mutex);
534
535    priv->unlocked = EINA_TRUE;
536    g_cond_signal(priv->data_cond);
537    g_mutex_unlock(priv->buffer_mutex);
538 }
539
540 static void
541 marshal_VOID__MINIOBJECT(GClosure * closure, GValue * return_value __UNUSED__,
542                          guint n_param_values, const GValue * param_values,
543                          gpointer invocation_hint __UNUSED__, gpointer marshal_data)
544 {
545    typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1, gpointer data2);
546    marshalfunc_VOID__MINIOBJECT callback;
547    GCClosure *cc;
548    gpointer data1, data2;
549
550    cc = (GCClosure *) closure;
551
552    g_return_if_fail(n_param_values == 2);
553
554    if (G_CCLOSURE_SWAP_DATA(closure)) {
555       data1 = closure->data;
556       data2 = g_value_peek_pointer(param_values + 0);
557    } else {
558       data1 = g_value_peek_pointer(param_values + 0);
559       data2 = closure->data;
560    }
561    callback = (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data : cc->callback);
562
563    callback(data1, gst_value_get_mini_object(param_values + 1), data2);
564 }
565
566 static void
567 evas_video_sink_class_init(EvasVideoSinkClass* klass)
568 {
569    GObjectClass* gobject_class;
570    GstBaseSinkClass* gstbase_sink_class;
571
572    gobject_class = G_OBJECT_CLASS(klass);
573    gstbase_sink_class = GST_BASE_SINK_CLASS(klass);
574
575    g_type_class_add_private(klass, sizeof(EvasVideoSinkPrivate));
576
577    gobject_class->set_property = evas_video_sink_set_property;
578    gobject_class->get_property = evas_video_sink_get_property;
579
580    g_object_class_install_property (gobject_class, PROP_EVAS_OBJECT,
581                                     g_param_spec_pointer ("evas-object", "Evas Object",
582                                                           "The Evas object where the display of the video will be done",
583                                                           G_PARAM_READWRITE));
584
585    g_object_class_install_property (gobject_class, PROP_WIDTH,
586                                     g_param_spec_int ("width", "Width",
587                                                       "The width of the video",
588                                                       0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
589
590    g_object_class_install_property (gobject_class, PROP_HEIGHT,
591                                     g_param_spec_int ("height", "Height",
592                                                       "The height of the video",
593                                                       0, 65536, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
594
595    gobject_class->dispose = evas_video_sink_dispose;
596
597    gstbase_sink_class->set_caps = evas_video_sink_set_caps;
598    gstbase_sink_class->stop = evas_video_sink_stop;
599    gstbase_sink_class->start = evas_video_sink_start;
600    gstbase_sink_class->unlock = evas_video_sink_unlock;
601    gstbase_sink_class->unlock_stop = evas_video_sink_unlock_stop;
602    gstbase_sink_class->render = evas_video_sink_render;
603    gstbase_sink_class->preroll = evas_video_sink_preroll;
604
605    evas_video_sink_signals[REPAINT_REQUESTED] = g_signal_new("repaint-requested",
606                                                              G_TYPE_FROM_CLASS(klass),
607                                                              (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
608                                                              0,
609                                                              0,
610                                                              0,
611                                                              marshal_VOID__MINIOBJECT,
612                                                              G_TYPE_NONE, 1, GST_TYPE_BUFFER);
613 }
614
615 gboolean
616 gstreamer_plugin_init (GstPlugin * plugin)
617 {
618    return gst_element_register (plugin,
619                                 "emotion-sink",
620                                 GST_RANK_NONE,
621                                 EVAS_TYPE_VIDEO_SINK);
622 }
623
624 GstElement *
625 gstreamer_video_sink_new(Emotion_Gstreamer_Video *ev,
626                          Evas_Object *o,
627                          const char *uri)
628 {
629    GstElement *playbin;
630    GstElement *sink;
631    Evas_Object *obj;
632    GstStateChangeReturn res;
633
634    obj = _emotion_image_get(o);
635    if (!obj)
636      {
637         ERR("Not Evas_Object specified");
638         return NULL;
639      }
640
641    playbin = gst_element_factory_make("playbin2", "playbin");
642    if (!playbin)
643      {
644         ERR("Unable to create 'playbin' GstElement.");
645         return NULL;
646      }
647
648    sink = gst_element_factory_make("emotion-sink", "sink");
649    if (!sink)
650      {
651         ERR("Unable to create 'emotion-sink' GstElement.");
652         goto unref_pipeline;
653      }
654
655    g_object_set(G_OBJECT(playbin), "video-sink", sink, NULL);
656    g_object_set(G_OBJECT(playbin), "uri", uri, NULL);
657    g_object_set(G_OBJECT(sink), "evas-object", obj, NULL);
658
659    res = gst_element_set_state(playbin, GST_STATE_PAUSED);
660    if (res == GST_STATE_CHANGE_FAILURE)
661      {
662         ERR("Unable to set GST_STATE_PAUSED.");
663         goto unref_pipeline;
664      }
665
666    res = gst_element_get_state(playbin, NULL, NULL, GST_CLOCK_TIME_NONE);
667    if (res != GST_STATE_CHANGE_SUCCESS)
668      {
669         ERR("Unable to get GST_CLOCK_TIME_NONE.");
670         goto unref_pipeline;
671      }
672
673    evas_object_data_set(obj, "_emotion_gstreamer_video", ev);
674
675    return playbin;
676
677  unref_pipeline:
678    gst_object_unref(playbin);
679    return NULL;
680 }