add dummy setTarget, add skip support
[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::setTarget(int target)
235 {
236         return -1;
237 }
238
239 RESULT eServiceMP3::pause(ePtr<iPauseableService> &ptr)
240 {
241         ptr=this;
242         return 0;
243 }
244
245 RESULT eServiceMP3::setSlowMotion(int ratio)
246 {
247         return -1;
248 }
249
250 RESULT eServiceMP3::setFastForward(int ratio)
251 {
252         return -1;
253 }
254   
255                 // iPausableService
256 RESULT eServiceMP3::pause()
257 {
258         if (!m_gst_pipeline)
259                 return -1;
260         gst_element_set_state(m_gst_pipeline, GST_STATE_PAUSED);
261         return 0;
262 }
263
264 RESULT eServiceMP3::unpause()
265 {
266         if (!m_gst_pipeline)
267                 return -1;
268         gst_element_set_state(m_gst_pipeline, GST_STATE_PLAYING);
269         return 0;
270 }
271
272         /* iSeekableService */
273 RESULT eServiceMP3::seek(ePtr<iSeekableService> &ptr)
274 {
275         ptr = this;
276         return 0;
277 }
278
279 RESULT eServiceMP3::getLength(pts_t &pts)
280 {
281         if (!m_gst_pipeline)
282                 return -1;
283         if (m_state != stRunning)
284                 return -1;
285         
286         GstFormat fmt = GST_FORMAT_TIME;
287         gint64 len;
288         
289         if (!gst_element_query_duration(m_gst_pipeline, &fmt, &len))
290                 return -1;
291         
292                 /* len is in nanoseconds. we have 90 000 pts per second. */
293         
294         pts = len / 11111;
295         return 0;
296 }
297
298 RESULT eServiceMP3::seekTo(pts_t to)
299 {
300         if (!m_gst_pipeline)
301                 return -1;
302
303                 /* convert pts to nanoseconds */
304         gint64 time_nanoseconds = to * 11111LL;
305         if (!gst_element_seek (m_gst_pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
306                 GST_SEEK_TYPE_SET, time_nanoseconds,
307                 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
308         {
309                 eDebug("SEEK failed");
310                 return -1;
311         }
312         return 0;
313 }
314
315 RESULT eServiceMP3::seekRelative(int direction, pts_t to)
316 {
317         if (!m_gst_pipeline)
318                 return -1;
319
320         pause();
321
322         pts_t ppos;
323         getPlayPosition(ppos);
324         ppos += to * direction;
325         if (ppos < 0)
326                 ppos = 0;
327         seekTo(ppos);
328         
329         unpause();
330
331         return 0;
332 }
333
334 RESULT eServiceMP3::getPlayPosition(pts_t &pts)
335 {
336         if (!m_gst_pipeline)
337                 return -1;
338         if (m_state != stRunning)
339                 return -1;
340         
341         GstFormat fmt = GST_FORMAT_TIME;
342         gint64 len;
343         
344         if (!gst_element_query_position(m_gst_pipeline, &fmt, &len))
345                 return -1;
346         
347                 /* len is in nanoseconds. we have 90 000 pts per second. */
348         pts = len / 11111;
349         return 0;
350 }
351
352 RESULT eServiceMP3::setTrickmode(int trick)
353 {
354                 /* trickmode currently doesn't make any sense for us. */
355         return -1;
356 }
357
358 RESULT eServiceMP3::isCurrentlySeekable()
359 {
360         return 1;
361 }
362
363 RESULT eServiceMP3::info(ePtr<iServiceInformation>&i)
364 {
365         i = this;
366         return 0;
367 }
368
369 RESULT eServiceMP3::getName(std::string &name)
370 {
371         name = "MP3 File: " + m_filename;
372         return 0;
373 }
374
375 int eServiceMP3::getInfo(int w)
376 {
377         switch (w)
378         {
379         case sTitle:
380         case sArtist:
381         case sAlbum:
382         case sComment:
383         case sTracknumber:
384         case sGenre:
385                 return resIsString;
386
387         default:
388                 return resNA;
389         }
390 }
391
392 std::string eServiceMP3::getInfoString(int w)
393 {
394         gchar *tag = 0;
395         switch (w)
396         {
397         case sTitle:
398                 tag = GST_TAG_TITLE;
399                 break;
400         case sArtist:
401                 tag = GST_TAG_ARTIST;
402                 break;
403         case sAlbum:
404                 tag = GST_TAG_ALBUM;
405                 break;
406         case sComment:
407                 tag = GST_TAG_COMMENT;
408                 break;
409         case sTracknumber:
410                 tag = GST_TAG_TRACK_NUMBER;
411                 break;
412         case sGenre:
413                 tag = GST_TAG_GENRE;
414                 break;
415         default:
416                 return "";
417         }
418         
419         if (!m_stream_tags || !tag)
420                 return "";
421         
422         gchar *value;
423         
424         if (gst_tag_list_get_string(m_stream_tags, tag, &value))
425         {
426                 std::string res = value;
427                 g_free(value);
428                 return res;
429         }
430         
431         return "";
432 }
433
434
435                 void foreach(const GstTagList *list, const gchar *tag, gpointer user_data)
436                 {
437                         if (tag)
438                                 eDebug("Tag: %c%c%c%c", tag[0], tag[1], tag[2], tag[3]);
439                         
440                 }
441
442 void eServiceMP3::gstBusCall(GstBus *bus, GstMessage *msg)
443 {
444         gchar *string = gst_structure_to_string(gst_message_get_structure(msg));
445         eDebug("gst_message: %s", string);
446         g_free(string);
447         
448         
449         switch (GST_MESSAGE_TYPE (msg))
450         {
451         case GST_MESSAGE_EOS:
452                 m_event((iPlayableService*)this, evEOF);
453                 break;
454         case GST_MESSAGE_ERROR:
455         {
456                 gchar *debug;
457                 GError *err;
458                 gst_message_parse_error (msg, &err, &debug);
459                 g_free (debug);
460                 eWarning("Gstreamer error: %s", err->message);
461                 g_error_free(err);
462                 exit(0);
463                 break;
464         }
465         case GST_MESSAGE_TAG:
466         {
467                 GstTagList *tags, *result;
468                 gst_message_parse_tag(msg, &tags);
469
470                 result = gst_tag_list_merge(m_stream_tags, tags, GST_TAG_MERGE_PREPEND);
471                 if (result)
472                 {
473                         if (m_stream_tags)
474                                 gst_tag_list_free(m_stream_tags);
475                         m_stream_tags = result;
476                 }
477                 gst_tag_list_free(tags);
478                 break;
479         }
480         default:
481                 break;
482         }
483 }
484
485 GstBusSyncReply eServiceMP3::gstBusSyncHandler(GstBus *bus, GstMessage *message, gpointer user_data)
486 {
487         eServiceMP3 *_this = (eServiceMP3*)user_data;
488         _this->m_pump.send(1);
489                 /* wake */
490         return GST_BUS_PASS;
491 }
492
493 void eServiceMP3::gstCBnewPad(GstElement *decodebin, GstPad *pad, gboolean last, gpointer user_data)
494 {
495         eServiceMP3 *_this = (eServiceMP3*)user_data;
496         GstCaps *caps;
497         GstStructure *str;
498         GstPad *audiopad;
499         
500         /* only link once */
501         audiopad = gst_element_get_pad (_this->m_gst_audio, "sink");
502         if (GST_PAD_IS_LINKED (audiopad)) {
503                 g_object_unref (audiopad);
504                 return;
505         }
506         
507         
508         /* check media type */
509         caps = gst_pad_get_caps (pad);
510         str = gst_caps_get_structure (caps, 0);
511         if (!g_strrstr (gst_structure_get_name (str), "audio")) {
512                 gst_caps_unref (caps);
513                 gst_object_unref (audiopad);
514                 return;
515         }
516         
517         gst_caps_unref (caps);
518         gst_pad_link (pad, audiopad);
519 }
520
521
522 void eServiceMP3::gstPoll(const int&)
523 {
524                 /* ok, we have a serious problem here. gstBusSyncHandler sends 
525                    us the wakup signal, but likely before it was posted.
526                    the usleep, an EVIL HACK (DON'T DO THAT!!!) works around this.
527                    
528                    I need to understand the API a bit more to make this work 
529                    proplerly. */
530         usleep(1);
531         
532         GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (m_gst_pipeline));
533         GstMessage *message;
534         while (message = gst_bus_pop (bus))
535         {
536                 gstBusCall(bus, message);
537                 gst_message_unref (message);
538         }
539 }
540
541 eAutoInitPtr<eServiceFactoryMP3> init_eServiceFactoryMP3(eAutoInitNumbers::service+1, "eServiceFactoryMP3");
542 #else
543 #warning gstreamer not available, not building media player
544 #endif