fix segfault
[enigma2.git] / 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 int eServiceMP3::getInfo(int w)
348 {
349         switch (w)
350         {
351         case sTitle:
352         case sArtist:
353         case sAlbum:
354         case sComment:
355         case sTracknumber:
356         case sGenre:
357                 return resIsString;
358
359         default:
360                 return resNA;
361         }
362 }
363
364 std::string eServiceMP3::getInfoString(int w)
365 {
366         gchar *tag = 0;
367         switch (w)
368         {
369         case sTitle:
370                 tag = GST_TAG_TITLE;
371                 break;
372         case sArtist:
373                 tag = GST_TAG_ARTIST;
374                 break;
375         case sAlbum:
376                 tag = GST_TAG_ALBUM;
377                 break;
378         case sComment:
379                 tag = GST_TAG_COMMENT;
380                 break;
381         case sTracknumber:
382                 tag = GST_TAG_TRACK_NUMBER;
383                 break;
384         case sGenre:
385                 tag = GST_TAG_GENRE;
386                 break;
387         default:
388                 return "";
389         }
390         
391         if (!m_stream_tags || !tag)
392                 return "";
393         
394         gchar *value;
395         
396         if (gst_tag_list_get_string(m_stream_tags, tag, &value))
397         {
398                 std::string res = value;
399                 g_free(value);
400                 return res;
401         }
402         
403         return "";
404 }
405
406
407                 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
408                 {
409                         if (tag)
410                                 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
411                         
412                 }
413
414 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
415 {
416         switch (GST_MESSAGE_TYPE (msg))
417         {
418         case GST_MESSAGE_EOS:
419                 eDebug("end of stream!");
420                 m_event((iPlayableService*)this, evEOF);
421                 break;
422         case GST_MESSAGE_ERROR:
423         {
424                 gchar *debug;
425                 GError *err;
426                 gst_message_parse_error (msg, &err, &debug);
427                 g_free (debug);
428                 eWarning("Gstreamer error: %s", err->message);
429                 g_error_free(err);
430                 exit(0);
431                 break;
432         }
433         case GST_MESSAGE_TAG:
434         {
435                 GstTagList *tags, *result;
436                 gst_message_parse_tag(msg, &tags);
437                 eDebug("is tag list: %d", GST_IS_TAG_LIST(tags));
438
439                 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
440                 if (result)
441                 {
442                         if (m_stream_tags)
443                                 gst_tag_list_free(m_stream_tags);
444                         m_stream_tags = result;
445                 }
446                 gst_tag_list_free(tags);
447                 
448                 eDebug("listing tags..");
449                 gst_tag_list_foreach(m_stream_tags, foreach, 0);
450                 eDebug("ok");
451
452                 if (m_stream_tags)
453                 {
454                         gchar *title;
455                         eDebug("is tag list: %d", GST_IS_TAG_LIST(m_stream_tags));
456                         if (gst_tag_list_get_string(m_stream_tags, GST_TAG_TITLE, &title))
457                         {
458                                 eDebug("TITLE: %s", title);
459                                 g_free(title);
460                         } else
461                                 eDebug("no title");
462                 } else
463                         eDebug("no tags");
464                 
465                 eDebug("tag list updated!");
466                 break;
467         }
468         default:
469                 eDebug("unknown message");
470                 break;
471         }
472 }
473
474 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
475 {
476         eServiceMP3 *_this = (eServiceMP3*)user_data;
477         _this->m_pump.send(1);
478                 /* wake */
479         return GST_BUS_PASS;
480 }
481
482 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
483 {
484         eServiceMP3 *_this = (eServiceMP3*)user_data;
485         GstCaps *caps;
486         GstStructure *str;
487         GstPad *audiopad;
488         
489         /* only link once */
490         audiopad = gst_element_get_pad (_this->m_gst_audio, "sink");
491         if (GST_PAD_IS_LINKED (audiopad)) {
492                 g_object_unref (audiopad);
493                 return;
494         }
495         
496         /* check media type */
497         caps = gst_pad_get_caps (pad);
498         str = gst_caps_get_structure (caps, 0);
499         if (!g_strrstr (gst_structure_get_name (str), "audio")) {
500                 gst_caps_unref (caps);
501                 gst_object_unref (audiopad);
502                 return;
503         }
504         
505         gst_caps_unref (caps);
506         gst_pad_link (pad, audiopad);
507 }
508
509
510 void eServiceMP3::gstPoll(const int&)
511 {
512                 /* ok, we have a serious problem here. gstBusSyncHandler sends 
513                    us the wakup signal, but likely before it was posted.
514                    the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
515                    
516                    I need to understand the API a bit more to make this work 
517                    proplerly. */
518         usleep(1);
519         
520         GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
521         GstMessage *message;
522         while (message = gst_bus_pop (bus))
523         {
524                 gstBusCall(bus, message);
525                 gst_message_unref (message);
526         }
527 }
528
529 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
530 #else
531 #warning gstreamer not available, not building media player
532 #endif