use gstreamer for playing media files
[vuplus_dvbapp] / lib / service / servicemp3.cpp
1 #ifdef HAVE_GSTREAMER
2
3         /* note: this requires gstreamer 0.10.x and a big list of plugins. */
4         /* it's currently hardcoded to use a big-endian alsasink as sink. */
5 #include <lib/base/eerror.h>
6 #include <lib/base/object.h>
7 #include <lib/base/ebase.h>
8 #include <string>
9 #include <lib/service/servicemp3.h>
10 #include <lib/service/service.h>
11 #include <lib/base/init_num.h>
12 #include <lib/base/init.h>
13 #include <gst/gst.h>
14
15 // eServiceFactoryMP3
16
17 eServiceFactoryMP3::eServiceFactoryMP3()
18 {
19         ePtr<eServiceCenter> sc;
20         
21         eServiceCenter::getPrivInstance(sc);
22         if (sc)
23                 sc->addServiceFactory(eServiceFactoryMP3::id, this);
24
25         m_service_info = new eStaticServiceMP3Info();
26 }
27
28 eServiceFactoryMP3::~eServiceFactoryMP3()
29 {
30         ePtr<eServiceCenter> sc;
31         
32         eServiceCenter::getPrivInstance(sc);
33         if (sc)
34                 sc->removeServiceFactory(eServiceFactoryMP3::id);
35 }
36
37 DEFINE_REF(eServiceFactoryMP3)
38
39         // iServiceHandler
40 RESULT eServiceFactoryMP3::play(const eServiceReference &ref, ePtr<iPlayableService> &ptr)
41 {
42                 // check resources...
43         ptr = new eServiceMP3(ref.path.c_str());
44         return 0;
45 }
46
47 RESULT eServiceFactoryMP3::record(const eServiceReference &ref, ePtr<iRecordableService> &ptr)
48 {
49         ptr=0;
50         return -1;
51 }
52
53 RESULT eServiceFactoryMP3::list(const eServiceReference &, ePtr<iListableService> &ptr)
54 {
55         ptr=0;
56         return -1;
57 }
58
59 RESULT eServiceFactoryMP3::info(const eServiceReference &ref, ePtr<iStaticServiceInformation> &ptr)
60 {
61         ptr = m_service_info;
62         return 0;
63 }
64
65 RESULT eServiceFactoryMP3::offlineOperations(const eServiceReference &, ePtr<iServiceOfflineOperations> &ptr)
66 {
67         ptr = 0;
68         return -1;
69 }
70
71
72 // eStaticServiceMP3Info
73
74
75 // eStaticServiceMP3Info is seperated from eServiceMP3 to give information
76 // about unopened files.
77
78 // probably eServiceMP3 should use this class as well, and eStaticServiceMP3Info
79 // should have a database backend where ID3-files etc. are cached.
80 // this would allow listing the mp3 database based on certain filters.
81
82 DEFINE_REF(eStaticServiceMP3Info)
83
84 eStaticServiceMP3Info::eStaticServiceMP3Info()
85 {
86 }
87
88 RESULT eStaticServiceMP3Info::getName(const eServiceReference &ref, std::string &name)
89 {
90         size_t last = ref.path.rfind('/');
91         if (last != std::string::npos)
92                 name = ref.path.substr(last+1);
93         else
94                 name = ref.path;
95         return 0;
96 }
97
98 int eStaticServiceMP3Info::getLength(const eServiceReference &ref)
99 {
100         return -1;
101 }
102
103 // eServiceMP3
104
105 eServiceMP3::eServiceMP3(const char *filename): m_filename(filename), m_pump(eApp, 1)
106 {
107         m_stream_tags = 0;
108         CONNECT(m_pump.recv_msg, eServiceMP3::gstPoll);
109         GstElement *source, *decoder, *conv, *flt, *sink;
110         m_state = stIdle;
111         eDebug("SERVICEMP3 construct!");
112
113         m_gst_pipeline = gst_pipeline_new ("audio-player");
114         if (!m_gst_pipeline)
115                 eWarning("failed to create pipeline");
116
117         source = gst_element_factory_make ("filesrc", "file-source");
118         if (!source)
119                 eWarning("failed to create filesrc");
120                 
121         decoder = gst_element_factory_make ("decodebin", "decoder");
122         if (!decoder)
123                 eWarning("failed to create decodebin decoder");
124         
125         conv = gst_element_factory_make ("audioconvert", "converter");
126         if (!conv)
127                 eWarning("failed to create audioconvert");
128         
129         flt = gst_element_factory_make ("capsfilter", "flt");
130         if (!flt)
131                 eWarning("failed to create capsfilter");
132         
133                 /* workaround for [3des]' driver bugs: */
134         if (flt)
135         {
136                 GstCaps *caps = gst_caps_new_simple("audio/x-raw-int", "endianness", G_TYPE_INT, 4321, 0);
137                 g_object_set (G_OBJECT (flt), "caps", caps, 0);
138                 gst_caps_unref(caps);
139         }
140         
141         sink = gst_element_factory_make ("alsasink", "alsa-output");
142         if (!sink)
143                 eWarning("failed to create osssink");
144         
145         if (m_gst_pipeline && source && decoder && conv && sink)
146         {
147                 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline)), gstBusSyncHandler, this);
148
149                 g_object_set (G_OBJECT (source), "location", filename, NULL);
150                 g_signal_connect (decoder, "new-decoded-pad", G_CALLBACK(gstCBnewPad), this);
151
152                         /* gst_bin will take the 'floating references' */
153                 gst_bin_add_many (GST_BIN (m_gst_pipeline),
154                                         source, decoder, NULL);
155                 
156                 gst_element_link(source, decoder);
157                 
158                         /* create audio bin */
159                 m_gst_audio = gst_bin_new ("audiobin");
160                 GstPad *audiopad = gst_element_get_pad (conv, "sink");
161                 
162                 gst_bin_add_many(GST_BIN(m_gst_audio), conv, flt, sink, 0);
163                 gst_element_link_many(conv, flt, sink, 0);
164                 gst_element_add_pad(m_gst_audio, gst_ghost_pad_new ("sink", audiopad));
165                 gst_object_unref(audiopad);
166                 
167                 gst_bin_add (GST_BIN(m_gst_pipeline), m_gst_audio);
168         } else
169         {
170                 if (m_gst_pipeline)
171                         gst_object_unref(GST_OBJECT(m_gst_pipeline));
172                 if (source)
173                         gst_object_unref(GST_OBJECT(source));
174                 if (decoder)
175                         gst_object_unref(GST_OBJECT(decoder));
176                 if (conv)
177                         gst_object_unref(GST_OBJECT(conv));
178                 if (sink)
179                         gst_object_unref(GST_OBJECT(sink));
180                 eDebug("sorry, can't play.");
181         }
182         
183         gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
184 }
185
186 eServiceMP3::~eServiceMP3()
187 {
188         if (m_state == stRunning)
189                 stop();
190         
191         if (m_stream_tags)
192                 gst_tag_list_free(m_stream_tags);
193         
194         if (m_gst_pipeline)
195         {
196                 gst_object_unref (GST_OBJECT (m_gst_pipeline));
197                 eDebug("SERVICEMP3 destruct!");
198         }
199 }
200
201 DEFINE_REF(eServiceMP3);        
202
203 RESULT eServiceMP3::connectEvent(const Slot2<void,iPlayableService*,int> &event, ePtr<eConnection> &connection)
204 {
205         connection = new eConnection((iPlayableService*)this, m_event.connect(event));
206         return 0;
207 }
208
209 RESULT eServiceMP3::start()
210 {
211         assert(m_state == stIdle);
212         
213         m_state = stRunning;
214         if (m_gst_pipeline)
215         {
216                 eDebug("starting pipeline");
217                 gst_element_set_state (m_gst_pipeline, GST_STATE_PLAYING);
218         }
219         m_event(this, evStart);
220         return 0;
221 }
222
223 RESULT eServiceMP3::stop()
224 {
225         assert(m_state != stIdle);
226         if (m_state == stStopped)
227                 return -1;
228         printf("MP3: %s stop\n", m_filename.c_str());
229         gst_element_set_state(m_gst_pipeline, GST_STATE_NULL);
230         m_state = stStopped;
231         return 0;
232 }
233
234 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
235 {
236         ptr=this;
237         return 0;
238 }
239
240 RESULT eServiceMP3::setSlowMotion(int ratio)
241 {
242         return -1;
243 }
244
245 RESULT eServiceMP3::setFastForward(int ratio)
246 {
247         return -1;
248 }
249   
250                 // iPausableService
251 RESULT eServiceMP3::pause()
252 {
253         if (!m_gst_pipeline)
254                 return -1;
255         gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
256         return 0;
257 }
258
259 RESULT eServiceMP3::unpause()
260 {
261         if (!m_gst_pipeline)
262                 return -1;
263         gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
264         return 0;
265 }
266
267         /* iSeekableService */
268 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
269 {
270         ptr = this;
271         return 0;
272 }
273
274 RESULT eServiceMP3::getLength(pts_t &pts)
275 {
276         if (!m_gst_pipeline)
277                 return -1;
278         if (m_state != stRunning)
279                 return -1;
280         
281         GstFormat fmt = GST_FORMAT_TIME;
282         gint64 len;
283         
284         if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
285                 return -1;
286         
287                 /* len is in nanoseconds. we have 90 000 pts per second. */
288         
289         pts = len / 11111;
290         return 0;
291 }
292
293 RESULT eServiceMP3::seekTo(pts_t to)
294 {
295         /* implement me */
296         return -1;
297 }
298
299 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
300 {
301         /* implement me */
302         return -1;
303 }
304
305 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
306 {
307         if (!m_gst_pipeline)
308                 return -1;
309         if (m_state != stRunning)
310                 return -1;
311         
312         GstFormat fmt = GST_FORMAT_TIME;
313         gint64 len;
314         
315         if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
316                 return -1;
317         
318                 /* len is in nanoseconds. we have 90 000 pts per second. */
319         
320         pts = len / 11111;
321         return 0;
322 }
323
324 RESULT eServiceMP3::setTrickmode(int trick)
325 {
326                 /* trickmode currently doesn't make any sense for us. */
327         return -1;
328 }
329
330 RESULT eServiceMP3::isCurrentlySeekable()
331 {
332         return 1;
333 }
334
335 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
336 {
337         i = this;
338         return 0;
339 }
340
341 RESULT eServiceMP3::getName(std::string &name)
342 {
343         name = "MP3 File: " + m_filename;
344         return 0;
345 }
346
347                 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
348                 {
349                         if (tag)
350                                 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
351                         
352                 }
353
354 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
355 {
356         switch (GST_MESSAGE_TYPE (msg))
357         {
358         case GST_MESSAGE_EOS:
359                 eDebug("end of stream!");
360                 m_event((iPlayableService*)this, evEOF);
361                 break;
362         case GST_MESSAGE_ERROR:
363         {
364                 gchar *debug;
365                 GError *err;
366                 gst_message_parse_error (msg, &err, &debug);
367                 g_free (debug);
368                 eWarning("Gstreamer error: %s", err->message);
369                 g_error_free(err);
370                 exit(0);
371                 break;
372         }
373         case GST_MESSAGE_TAG:
374         {
375                 GstTagList *tags, *result;
376                 gst_message_parse_tag(msg, &tags);
377                 eDebug("is tag list: %d", GST_IS_TAG_LIST(tags));
378
379                 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
380                 if (result)
381                 {
382                         if (m_stream_tags)
383                                 gst_tag_list_free(m_stream_tags);
384                         m_stream_tags = result;
385                 }
386                 gst_tag_list_free(tags);
387                 
388                 eDebug("listing tags..");
389                 gst_tag_list_foreach(m_stream_tags, foreach, 0);
390                 eDebug("ok");
391
392                 if (m_stream_tags)
393                 {
394                         gchar *title;
395                         eDebug("is tag list: %d", GST_IS_TAG_LIST(m_stream_tags));
396                         if (gst_tag_list_get_string(m_stream_tags, GST_TAG_TITLE, &title))
397                         {
398                                 eDebug("TITLE: %s", title);
399                                 g_free(title);
400                         } else
401                                 eDebug("no title");
402                 } else
403                         eDebug("no tags");
404                 
405                 eDebug("tag list updated!");
406                 break;
407         }
408         default:
409                 eDebug("unknown message");
410                 break;
411         }
412 }
413
414 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
415 {
416         eServiceMP3 *_this = (eServiceMP3*)user_data;
417         _this->m_pump.send(1);
418                 /* wake */
419         return GST_BUS_PASS;
420 }
421
422 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
423 {
424         eServiceMP3 *_this = (eServiceMP3*)user_data;
425         GstCaps *caps;
426         GstStructure *str;
427         GstPad *audiopad;
428         
429         /* only link once */
430         audiopad = gst_element_get_pad (_this->m_gst_audio, "sink");
431         if (GST_PAD_IS_LINKED (audiopad)) {
432                 g_object_unref (audiopad);
433                 return;
434         }
435         
436         /* check media type */
437         caps = gst_pad_get_caps (pad);
438         str = gst_caps_get_structure (caps, 0);
439         if (!g_strrstr (gst_structure_get_name (str), "audio")) {
440                 gst_caps_unref (caps);
441                 gst_object_unref (audiopad);
442                 return;
443         }
444         
445         gst_caps_unref (caps);
446         gst_pad_link (pad, audiopad);
447 }
448
449
450 void eServiceMP3::gstPoll(const int&)
451 {
452                 /* ok, we have a serious problem here. gstBusSyncHandler sends 
453                    us the wakup signal, but likely before it was posted.
454                    the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
455                    
456                    I need to understand the API a bit more to make this work 
457                    proplerly. */
458         usleep(1);
459         
460         GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
461         GstMessage *message;
462         while (message = gst_bus_pop (bus))
463         {
464                 gstBusCall(bus, message);
465                 gst_message_unref (message);
466         }
467 }
468
469 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
470 #else
471 #warning gstreamer not available, not building media player
472 #endif