655252a1a2363d87e83bf7a051afdb41dd2b3a9e
[profile/ivi/emotion.git] / src / modules / gstreamer / emotion_gstreamer_pipeline_cdda.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4 #include "emotion_gstreamer.h"
5 #include "emotion_gstreamer_pipeline.h"
6
7
8 static Emotion_Audio_Sink *_emotion_audio_sink_new  (Emotion_Gstreamer_Video *ev);
9 static void                _emotion_audio_sink_free (Emotion_Gstreamer_Video *ev, Emotion_Audio_Sink *asink);
10
11 int
12 emotion_pipeline_cdda_build(void *video, const char * device, unsigned int track)
13 {
14    GstElement              *cdiocddasrc;
15    Emotion_Video_Sink      *vsink;
16    Emotion_Audio_Sink      *asink;
17    Emotion_Gstreamer_Video *ev;
18    /*    GstFormat                format; */
19    /*    gint64                  tracks_count; */
20
21    ev = (Emotion_Gstreamer_Video *)video;
22    if (!ev) return 0;
23
24    cdiocddasrc = gst_element_factory_make("cdiocddasrc", "src");
25    if (!cdiocddasrc)
26      {
27         g_print("cdiocddasrc element missing. Install it.\n");
28         goto failure_cdiocddasrc;
29      }
30
31    if (device)
32      g_object_set(G_OBJECT(cdiocddasrc), "device", device, NULL);
33
34    g_object_set(G_OBJECT(cdiocddasrc), "track", track, NULL);
35
36    asink = _emotion_audio_sink_new(ev);
37    if (!asink)
38      goto failure_emotion_sink;
39
40    asink->sink = emotion_audio_sink_create(ev,  1);
41    if (!asink->sink)
42      goto failure_gstreamer_sink;
43
44    gst_bin_add_many((GST_BIN(ev->pipeline)), cdiocddasrc, asink->sink, NULL);
45
46    if (!gst_element_link(cdiocddasrc, asink->sink))
47      goto failure_link;
48
49    vsink = emotion_visualization_sink_create(ev, asink);
50    if (!vsink) goto failure_link;
51
52    if (!emotion_pipeline_pause(ev->pipeline))
53      goto failure_gstreamer_pause;
54
55      {
56         GstQuery *query;
57         GstPad   *pad;
58         GstCaps  *caps;
59         GstStructure *structure;
60
61         /* should always be found */
62         pad = gst_element_get_pad(cdiocddasrc, "src");
63
64         caps = gst_pad_get_caps(pad);
65         structure = gst_caps_get_structure(GST_CAPS(caps), 0);
66
67         gst_structure_get_int(structure, "channels", &asink->channels);
68         gst_structure_get_int(structure, "rate", &asink->samplerate);
69
70         gst_caps_unref(caps);
71
72         query = gst_query_new_duration(GST_FORMAT_TIME);
73         if (gst_pad_query(pad, query))
74           {
75              gint64 time;
76
77              gst_query_parse_duration(query, NULL, &time);
78              asink->length_time = (double)time / (double)GST_SECOND;
79              vsink->length_time = asink->length_time;
80           }
81         gst_query_unref(query);
82         gst_object_unref(GST_OBJECT(pad));
83      }
84
85    return 1;
86
87 failure_gstreamer_pause:
88    emotion_video_sink_free(ev, vsink);
89 failure_link:
90    gst_bin_remove(GST_BIN(ev->pipeline), asink->sink);
91 failure_gstreamer_sink:
92    _emotion_audio_sink_free(ev, asink);
93 failure_emotion_sink:
94    gst_bin_remove(GST_BIN(ev->pipeline), cdiocddasrc);
95 failure_cdiocddasrc:
96
97    return 0;
98 }
99
100 static Emotion_Audio_Sink *
101 _emotion_audio_sink_new(Emotion_Gstreamer_Video *ev)
102 {
103    Emotion_Audio_Sink *asink;
104
105    if (!ev) return NULL;
106
107    asink = (Emotion_Audio_Sink *)malloc(sizeof(Emotion_Audio_Sink));
108    if (!asink) return NULL;
109
110    ev->audio_sinks = eina_list_append(ev->audio_sinks, asink);
111    if (!eina_list_data_find(ev->audio_sinks, asink))
112      {
113         free(asink);
114         return NULL;
115      }
116    return asink;
117 }
118
119 static void
120 _emotion_audio_sink_free(Emotion_Gstreamer_Video *ev, Emotion_Audio_Sink *asink)
121 {
122    if (!ev || !asink) return;
123
124    ev->audio_sinks = eina_list_remove(ev->audio_sinks, asink);
125         free(asink);
126 }