summaryrefslogtreecommitdiff
path: root/legacy/emotion/src/modules/gstreamer/emotion_gstreamer_pipeline.c
blob: be2a6126764532ab34780715298003fce8b64b7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include <unistd.h>
#include <fcntl.h>

#include "emotion_private.h"
#include "emotion_gstreamer.h"
#include "emotion_gstreamer_pipeline.h"


gboolean
emotion_pipeline_pause(GstElement *pipeline)
{
   GstStateChangeReturn res;

   res = gst_element_set_state((pipeline), GST_STATE_PAUSED);
   if (res == GST_STATE_CHANGE_FAILURE)
     {
	ERR("could not pause");
	return 0;
     }

   res = gst_element_get_state((pipeline), NULL, NULL, GST_CLOCK_TIME_NONE);
   if (res != GST_STATE_CHANGE_SUCCESS)
     {
	ERR("could not complete pause");
	return 0;
     }

   return 1;
}

Eina_Bool
_emotion_pipeline_build(Emotion_Gstreamer_Video *ev, const char *file)
{
  GstElement *sink;
  int i;

   ev->pipeline = gst_element_factory_make("playbin2", "pipeline");
   if (!ev->pipeline)
     {
       ERR("could not create playbin2 element");
       return EINA_FALSE;
     }

   ev->eos_bus = gst_pipeline_get_bus(GST_PIPELINE(ev->pipeline));
   if (!ev->eos_bus)
     {
       ERR("could not create BUS");
       goto unref_pipeline;
     }

   sink = gst_element_factory_make("fakesink", "videosink");
   if (!sink)
     {
       ERR("could not create video sink");
       goto unref_pipeline;
     }
   g_object_set(G_OBJECT(sink), "sync", TRUE, NULL);
   g_object_set(G_OBJECT(sink), "signal-handoffs", TRUE, NULL);
   g_signal_connect(G_OBJECT(sink),
                    "handoff",
                    G_CALLBACK(cb_handoff), ev);
   g_object_set(G_OBJECT(ev->pipeline), "video-sink", sink, NULL);

   sink = gst_element_factory_make("autoaudiosink", "audiosink");
   if (!sink)
     {
       ERR("could not create audio sink");
       goto unref_pipeline;
     }
   g_object_set(G_OBJECT(ev->pipeline), "audio-sink", sink, NULL);

   if ((*file == '/') || (*file == '~'))
     {
       char *uri;

       uri = g_filename_to_uri(file, NULL, NULL);
       if (uri)
         {
           DBG("Setting file %s\n", uri);
           g_object_set(G_OBJECT(ev->pipeline), "uri", uri, NULL);
           free(uri);
         }
       else
         {
           ERR("could not create new uri from %s", file);
           goto unref_pipeline;
         }
     }
   else
     {
       DBG("Setting file %s\n", file);
       g_object_set(G_OBJECT(ev->pipeline), "uri", file, NULL);
     }

   if (!emotion_pipeline_pause(ev->pipeline))
     goto unref_pipeline;

   g_object_get(G_OBJECT(ev->pipeline),
                 "n-audio", &ev->audio_stream_nbr,
                 "n-video", &ev->video_stream_nbr,
                 NULL);

   if ((ev->video_stream_nbr == 0) && (ev->audio_stream_nbr == 0))
     {
       ERR("No audio nor video stream found");
       goto unref_pipeline;
     }

   /* Video streams */

   for (i = 0; i < ev->video_stream_nbr; i++)
     {
       GstPad       *pad;
       GstCaps      *caps;
       GstStructure *structure;
       GstQuery     *query;
       const GValue *val;
       gchar        *str;
       Eina_Bool     build_stream = EINA_FALSE;

       gdouble length_time = 0.0;
       gint width;
       gint height;
       gint fps_num;
       gint fps_den;
       guint32 fourcc = 0;

       g_signal_emit_by_name(ev->pipeline, "get-video-pad", i, &pad);
       if (!pad)
         continue;

       caps = gst_pad_get_negotiated_caps(pad);
       if (!caps)
         goto unref_pad_v;
       structure = gst_caps_get_structure(caps, 0);
       str = gst_caps_to_string(caps);

       if (!gst_structure_get_int(structure, "width", &width))
         goto unref_caps_v;
       if (!gst_structure_get_int(structure, "height", &height))
         goto unref_caps_v;
       if (!gst_structure_get_fraction(structure, "framerate", &fps_num, &fps_den))
         goto unref_caps_v;

       if (g_str_has_prefix(str, "video/x-raw-yuv"))
         {
           val = gst_structure_get_value(structure, "format");
           fourcc = gst_value_get_fourcc(val);
         }
       else if (g_str_has_prefix(str, "video/x-raw-rgb"))
         fourcc = GST_MAKE_FOURCC('A', 'R', 'G', 'B');
       else
         goto unref_caps_v;

       query = gst_query_new_duration(GST_FORMAT_TIME);
       if (gst_pad_peer_query(pad, query))
         {
           gint64 t;

           gst_query_parse_duration(query, NULL, &t);
           length_time = (double)t / (double)GST_SECOND;
         }
       else
         goto unref_query_v;

       build_stream = EINA_TRUE;

     unref_query_v:
       gst_query_unref(query);
     unref_caps_v:
       gst_caps_unref(caps);
     unref_pad_v:
       gst_object_unref(pad);

       if (build_stream)
         {
           Emotion_Video_Stream *vstream;

           vstream = emotion_video_stream_new(ev);
           if (!vstream) continue;

           vstream->length_time = length_time;
           vstream->width = width;
           vstream->height = height;
           vstream->fps_num = fps_num;
           vstream->fps_den = fps_den;
           vstream->fourcc = fourcc;
         }
     }

   /* Audio streams */

   for (i = 0; i < ev->audio_stream_nbr; i++)
     {
       GstPad       *pad;
       GstCaps      *caps;
       GstStructure *structure;
       GstQuery     *query;
       Eina_Bool     build_stream = EINA_FALSE;

       gdouble length_time = 0.0;
       gint channels;
       gint samplerate;

       g_signal_emit_by_name(ev->pipeline, "get-audio-pad", i, &pad);
       if (!pad)
         continue;

       caps = gst_pad_get_negotiated_caps(pad);
       if (!caps)
         goto unref_pad_a;
       structure = gst_caps_get_structure(caps, 0);

       if (!gst_structure_get_int(structure, "channels", &channels))
         goto unref_caps_a;
       if (!gst_structure_get_int(structure, "rate", &samplerate))
         goto unref_caps_a;

       query = gst_query_new_duration(GST_FORMAT_TIME);
       if (gst_pad_peer_query(pad, query))
         {
           gint64 t;

           gst_query_parse_duration(query, NULL, &t);
           length_time = (double)t / (double)GST_SECOND;
         }
       else
         goto unref_query_a;

       build_stream = EINA_TRUE;

     unref_query_a:
       gst_query_unref(query);
     unref_caps_a:
       gst_caps_unref(caps);
     unref_pad_a:
       gst_object_unref(pad);

       if (build_stream)
         {
           Emotion_Audio_Stream *astream;

           astream = (Emotion_Audio_Stream *)calloc(1, sizeof(Emotion_Audio_Stream));
           if (!astream) continue;
           ev->audio_streams = eina_list_append(ev->audio_streams, astream);
           if (eina_error_get())
             {
               free(astream);
               continue;
             }

           astream->length_time = length_time;
           astream->channels = channels;
           astream->samplerate = samplerate;
         }
     }

   /* Visualization sink */

   if (ev->video_stream_nbr == 0)
     {
       GstElement *vis = NULL;
       Emotion_Video_Stream *vstream;
       Emotion_Audio_Stream *astream;
       gint flags;
       const char *vis_name;

       if (!(vis_name = emotion_visualization_element_name_get(ev->vis)))
         {
           printf ("pb vis name %d\n", ev->vis);
           goto finalize;
         }

       astream = (Emotion_Audio_Stream *)eina_list_data_get(ev->audio_streams);

       vis = gst_element_factory_make(vis_name, "vissink");
       vstream = emotion_video_stream_new(ev);
       if (!vstream)
         goto finalize;
       else
         DBG("could not create visualization stream");

       vstream->length_time = astream->length_time;
       vstream->width = 320;
       vstream->height = 200;
       vstream->fps_num = 25;
       vstream->fps_den = 1;
       vstream->fourcc = GST_MAKE_FOURCC('A', 'R', 'G', 'B');

       g_object_set(G_OBJECT(ev->pipeline), "vis-plugin", vis, NULL);
       g_object_get(G_OBJECT(ev->pipeline), "flags", &flags, NULL);
       flags |= 0x00000008;
       g_object_set(G_OBJECT(ev->pipeline), "flags", flags, NULL);
     }

 finalize:

   ev->video_stream_nbr = eina_list_count(ev->video_streams);
   ev->audio_stream_nbr = eina_list_count(ev->audio_streams);

   if (ev->video_stream_nbr == 1)
     {
       Emotion_Video_Stream *vstream;

       vstream = (Emotion_Video_Stream *)eina_list_data_get(ev->video_streams);
       ev->ratio = (double)vstream->width / (double)vstream->height;
     }

   return EINA_TRUE;

 unref_pipeline:
   gst_object_unref(ev->pipeline);

   return EINA_FALSE;
}

/* Send the video frame to the evas object */
void
cb_handoff(GstElement *fakesrc __UNUSED__,
           GstBuffer  *buffer,
           GstPad     *pad,
           gpointer    user_data)
{
   GstQuery *query;
   void *buf[2];

   Emotion_Gstreamer_Video *ev = (Emotion_Gstreamer_Video *)user_data;
   if (!ev)
     return;

   if (!ev->video_mute)
     {
	if (!ev->obj_data)
	  ev->obj_data = malloc(GST_BUFFER_SIZE(buffer) * sizeof(void));

	memcpy(ev->obj_data, GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
	buf[0] = GST_BUFFER_DATA(buffer);
	buf[1] = buffer;
	ecore_pipe_write(ev->pipe, buf, sizeof(buf));
     }
   else
     {
	Emotion_Audio_Stream *astream;
	astream = (Emotion_Audio_Stream *)eina_list_nth(ev->audio_streams, ev->audio_stream_nbr - 1);
	_emotion_video_pos_update(ev->obj, ev->position, astream->length_time);
     }

   query = gst_query_new_position(GST_FORMAT_TIME);
   if (gst_pad_query(gst_pad_get_peer(pad), query))
     {
	gint64 position;

	gst_query_parse_position(query, NULL, &position);
	ev->position = (double)position / (double)GST_SECOND;
     }
   gst_query_unref(query);
}

Emotion_Video_Stream *
emotion_video_stream_new(Emotion_Gstreamer_Video *ev)
{
   Emotion_Video_Stream *vstream;

   if (!ev) return NULL;

   vstream = (Emotion_Video_Stream *)calloc(1, sizeof(Emotion_Video_Stream));
   if (!vstream) return NULL;

   ev->video_streams = eina_list_append(ev->video_streams, vstream);
   if (eina_error_get())
     {
	free(vstream);
	return NULL;
     }
   return vstream;
}

void
emotion_video_stream_free(Emotion_Gstreamer_Video *ev, Emotion_Video_Stream *vstream)
{
   if (!ev || !vstream) return;

   ev->video_streams = eina_list_remove(ev->video_streams, vstream);
	free(vstream);
}

int
emotion_pipeline_cdda_track_count_get(void *video)
{
   Emotion_Gstreamer_Video *ev;
   GstBus                  *bus;
   guint                    tracks_count = 0;
   gboolean                 done;

   ev = (Emotion_Gstreamer_Video *)video;
   if (!ev) return tracks_count;

   done = FALSE;
   bus = gst_element_get_bus(ev->pipeline);
   if (!bus) return tracks_count;

   while (!done)
     {
	GstMessage *message;

	message = gst_bus_pop(bus);
	if (!message)
	  /* All messages read, we're done */
	  break;

	switch (GST_MESSAGE_TYPE(message))
	  {
	   case GST_MESSAGE_TAG:
		{
		   GstTagList *tags;

		   gst_message_parse_tag(message, &tags);

		   gst_tag_list_get_uint(tags, GST_TAG_TRACK_COUNT, &tracks_count);
		   if (tracks_count) done = TRUE;
		   break;
		}
	   case GST_MESSAGE_ERROR:
	   default:
	      break;
	  }
	gst_message_unref(message);
     }

   gst_object_unref(GST_OBJECT(bus));

   return tracks_count;
}

const char *
emotion_visualization_element_name_get(Emotion_Vis visualisation)
{
   switch (visualisation)
     {
      case EMOTION_VIS_NONE:
	 return NULL;
      case EMOTION_VIS_GOOM:
	 return "goom";
      case EMOTION_VIS_LIBVISUAL_BUMPSCOPE:
	 return "libvisual_bumpscope";
      case EMOTION_VIS_LIBVISUAL_CORONA:
	 return "libvisual_corona";
      case EMOTION_VIS_LIBVISUAL_DANCING_PARTICLES:
	 return "libvisual_dancingparticles";
      case EMOTION_VIS_LIBVISUAL_GDKPIXBUF:
	 return "libvisual_gdkpixbuf";
      case EMOTION_VIS_LIBVISUAL_G_FORCE:
	 return "libvisual_G-Force";
      case EMOTION_VIS_LIBVISUAL_GOOM:
	 return "libvisual_goom";
      case EMOTION_VIS_LIBVISUAL_INFINITE:
	 return "libvisual_infinite";
      case EMOTION_VIS_LIBVISUAL_JAKDAW:
	 return "libvisual_jakdaw";
      case EMOTION_VIS_LIBVISUAL_JESS:
	 return "libvisual_jess";
      case EMOTION_VIS_LIBVISUAL_LV_ANALYSER:
	 return "libvisual_lv_analyzer";
      case EMOTION_VIS_LIBVISUAL_LV_FLOWER:
	 return "libvisual_lv_flower";
      case EMOTION_VIS_LIBVISUAL_LV_GLTEST:
	 return "libvisual_lv_gltest";
      case EMOTION_VIS_LIBVISUAL_LV_SCOPE:
	 return "libvisual_lv_scope";
      case EMOTION_VIS_LIBVISUAL_MADSPIN:
	 return "libvisual_madspin";
      case EMOTION_VIS_LIBVISUAL_NEBULUS:
	 return "libvisual_nebulus";
      case EMOTION_VIS_LIBVISUAL_OINKSIE:
	 return "libvisual_oinksie";
      case EMOTION_VIS_LIBVISUAL_PLASMA:
	 return "libvisual_plazma";
      default:
	 return "goom";
     }
}