aboutsummaryrefslogtreecommitdiffstats
path: root/fsomusicd/src/musicplayer.vala
blob: 95a670ef34caa185706c861b49267d6449e94107 (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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/*
 * File Name: musicplayer.vala
 * Creation Date: 23-08-2009
 * Last Modified: 09-01-2010 18:54:15
 *
 * Authored by Frederik 'playya' Sdun <Frederik.Sdun@googlemail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */
using GLib;
using DBus;
using Gst;
using FreeSmartphone;
using Gee;

namespace FsoMusic
{
    public class MusicPlayer: FsoFramework.AbstractObject , FreeSmartphone.MusicPlayer
    {
        private int _wait_counter = 0;
        //Workaround to detect dec/inc. Is it possible to do this another way?
        private bool waiting = false;
        private int wait_counter{ get{return _wait_counter;}
            set{
                if( value < 0 )
                    logger.error( @"someone tried to set wait_counter to: $value < 0 waiting $waiting" );
                else
                {
                    logger.info( @"Set wait_counter: $wait_counter -> $value wating: $waiting" );
                    _wait_counter = value;
                    if( _wait_counter == 1 && ! waiting )
                    {
                        waiting = true;
                        if( cur_state == FreeSmartphone.MusicPlayerState.PLAYING )
                             was_playing = true;
                        else
                             was_playing = false;
                        logger.info( @"Restart playing: $was_playing" );
                        pause();
                    }
                    else if( _wait_counter == 0 && waiting )
                    {
                        waiting = false;
                        if( was_playing )
                            play();
                        logger.info( @"Restarted playing: $was_playing" );
                    }
                }
            }
        }
        private bool was_playing = false;

        private unowned KeyFile key_file;
        private HashTable<string,string> audio_codecs = new HashTable<string,string>( str_hash, str_equal);
        private HashTable<string,string> audio_srcs = new HashTable<string,string>( str_hash, str_equal);
        private static string element_tail = "volume name=volume ! alsasink" ;
        private string current_song
        {
            get { return _current_song; }
            set
            {
                logger.debug( @"current song: $value" );
                this._current_song = value;
                set_playing( value );
            }
        }
        private string _current_song = "";
        private HashTable<ObjectPath,Playlist> playlists = new HashTable<ObjectPath,Playlist>( str_hash, str_equal);
        private ObjectPath current_playlist
        {
            get{ return _current_playlist; }
            set
            {
                var playlist = playlists.lookup( value );
                if( playlist != null )
                {
                    try
                    {
                        current_song = playlist.get_current_file();
                    }
                    catch (MusicPlayerPlaylistError e)
                    {
                        logger.debug( @"Get current song for $value: $(e.message)" );
                    }
                    _current_playlist = value;
                    logger.debug( @"new CurrentPlaylist: $value" );
                    playing_changed( value );
                }
                else
                     logger.error( @"Cannot find a Playlist for: $value" );
            }
        }
        private ObjectPath _current_playlist;
        private Gst.Pipeline audio_pipeline;
        private Gst.Bus audio_bus;
        private Gst.Element volume;
        private Gst.Element core_stream;
        private Gst.Element source;
        //Save some time to avoid recreating of the Pipeline
        private string last_extension = null;
        private string last_protcol = null;
        private MusicPlayerState cur_state = FreeSmartphone.MusicPlayerState.STOPPED;
        private MusicPlayerState _state {
            get{ return this.cur_state; }
            set
            {
                if( value != cur_state )
                {
                    this.cur_state = value;
                    state( value );
                }
            }
        }
        private uint timeout_pos_handle = -1;
        private int _current_position = 0;
        private int current_position
        {
            get{ return _current_position; }
            set
            {
                if( value != _current_position )
                {
                    this._current_position = value;
                    progress( value );
                }
            }
        }

        private HashTable<string,GLib.Value?> cur_song_info = new HashTable<string,GLib.Value?>( str_hash, str_equal);

        private int current_volume = 100;


        public MusicPlayer( KeyFile kf )
        {
            this.key_file = kf;

            var playlist_dir = Config.get_playlist_dir();
            try
            {
                foreach( var group in kf.get_groups() )
                {
                    if( group != Config.MUSIC_PLAYER_GROUP )
                    {
                        logger.debug( @"Try open playlist from '$group'" );
                        var pl = new Playlist( group, key_file, this );
                        add_playlist( group, pl );
                    }
                }
            }
            catch (GLib.Error e)
            {
                logger.debug( @"Open config directory $playlist_dir for playlist: $(e.message)" );
            }
        }
        construct
        {
            setup_audio_elements();
            setup_source_elements();
        }
        ~MusicPlayer()
        {
            save();
            if( audio_pipeline != null )
                audio_pipeline.set_state( Gst.State.NULL );
        }
        //
        // org.freesmartphone.MusicPlayer
        //
        public async HashTable<string,GLib.Value?> get_info_for_file( string file ) throws MusicPlayerError, DBus.Error
        {
            var helper = new TagHelper();
            if( ! FileUtils.test( file, FileTest.EXISTS ) )
                 throw new MusicPlayerError.FILE_NOT_FOUND( @"The file $file does not exist" );
            var pipe = new Pipeline( "file_pipeline" );

            var filesrc = Gst.ElementFactory.make( "filesrc", "src" );
            filesrc.set( "location", file );

            var id3demux = Gst.ElementFactory.make( "id3demux", "demux" );

            var fakesink = Gst.ElementFactory.make( "fakesink", "sink" );

            pipe.add_many( filesrc, id3demux, fakesink );
            filesrc.link( id3demux );
            id3demux.link( fakesink );
            id3demux.link( filesrc );
            fakesink.link( id3demux );

            //var pipe = parse_launch( "filesrc name=source ! id3demux ! fakesink" ) as Pipeline;
            //pipe.get_by_name( "source" ).set( "location", file );

            var file_bus = pipe.get_bus();
            pipe.set_state( Gst.State.PLAYING );

            bool stop = false;

            while( !stop )
            {

                if( ! file_bus.have_pending() )
                     continue;
                else
                {
                    var message = file_bus.pop();
                    switch( message.type )
                    {
                        case MessageType.TAG:
                            debug("new tag");
                            Gst.TagList tag_list;
                            message.parse_tag ( out tag_list );
                            tag_list.foreach ( helper.foreach_tag );
                            break;
                        case MessageType.ERROR:
                            GLib.Error err;
                            string debug;
                            message.parse_error( out err, out debug );
                            logger.debug( @"Parsing file tags for $file failed: $(err.message)." );
                            stop = true;
                            break;
                        default:
                            logger.info( @"Ignoring $(message.type) for $file" );
                            break;
                    }
                }
            }
            pipe.set_state( Gst.State.NULL );
            return helper.tags;
        }
        public async HashTable<string,GLib.Value?> get_playing_info() throws MusicPlayerError, DBus.Error
        {
            return this.cur_song_info;
        }
        public async string get_playing() throws MusicPlayerError, DBus.Error
        {
            if( current_song == null )
                 throw new MusicPlayerError.NO_FILE_SELECTED( "No file selected" );
            return current_song;
        }
        public async void set_playing( string file ) throws MusicPlayerError, DBus.Error
        {
            string ext = file.rchr( file.len(), '.' );
            ext = ext == null ? "": ext;
            string protocol = get_protocol_for_uri( file );
            if( last_extension == ext && last_protcol == protocol )
            {
                audio_pipeline.set_state( Gst.State.NULL );
                var src = audio_pipeline.get_by_name( "source" );
                src.set( "location", file );
            }
            else
            {
                if( audio_pipeline != null )
                    audio_pipeline.set_state( Gst.State.NULL );

                var srcs = audio_srcs.lookup( protocol );
                if( srcs == null )
                     throw new MusicPlayerError.PROTOCOL_NOT_SUPPORTED( "Can't open %s".printf( file ) );

                var codec = audio_codecs.lookup( ext );
                if( codec == null )
                     throw new MusicPlayerError.FILETYPE_NOT_SUPPORTED( "Can't open %s".printf( file ) );
                var el = @"$srcs ! $codec ! $element_tail";


                logger.debug( @"Elements: \"$el\" for $file" );

                try
                {
                    var element = parse_launch( el );
                    var tmp_pipe = element as Pipeline;
                    volume = tmp_pipe.get_by_name( "volume" );
                    set_volume( current_volume );
                    core_stream = tmp_pipe.get_by_name( "core" );
                    source = tmp_pipe.get_by_name( "source" );
                    source.set( "location", file );
                    audio_bus = tmp_pipe.get_bus();
                    audio_bus.add_watch( bus_callback );
                    last_extension = ext;
                    last_protcol = protocol;
                    Source.remove( timeout_pos_handle );
                    audio_pipeline = tmp_pipe;
                    this.audio_pipeline.set_state( Gst.State.READY );
                    this._state = FreeSmartphone.MusicPlayerState.STOPPED;

                    this.cur_song_info = new HashTable<string,GLib.Value?>( str_hash, str_equal );
                    this.cur_song_info.insert( "filename", file );
                    playing_changed( file );
                }
                catch (GLib.Error e)
                {
                    logger.error( @"Parsing arguments: \"$(el)\" $(e.message)" );
                }
            }
        }
        public async void play() throws MusicPlayerError, DBus.Error
        {
            if( ! waiting )
            {
                if( audio_pipeline == null )
                     throw new FreeSmartphone.MusicPlayerError.NO_FILE_SELECTED( "No file selected" );
                this.audio_pipeline.set_state( Gst.State.PLAYING );
                this._state = FreeSmartphone.MusicPlayerState.PLAYING;
                timeout_pos_handle = Timeout.add( Config.poll_timeout, position_timeout );
            }
            else
                 logger.info( @"Reject playing, because we are waiting for some system events" );
        }
        public async void pause() throws MusicPlayerError, DBus.Error
        {
            if( audio_pipeline == null )
                throw new FreeSmartphone.MusicPlayerError.NO_FILE_SELECTED( "No file selected" );
            this.audio_pipeline.set_state( Gst.State.PAUSED );
            this._state = FreeSmartphone.MusicPlayerState.PAUSED;
            Source.remove( this.timeout_pos_handle );
        }
        public async void stop() throws MusicPlayerError, DBus.Error
        {
            this.audio_pipeline.set_state( Gst.State.READY );
            this._state = FreeSmartphone.MusicPlayerState.STOPPED;
            Source.remove( this.timeout_pos_handle );
        }
        public async void previous() throws MusicPlayerError, DBus.Error
        {
            if( current_playlist == null )
                throw new MusicPlayerError.NO_PLAYLIST_SELECTED( "No Playlist Selected" );
            var playlist = playlists.lookup( current_playlist );
            try
            {
                this.current_song = playlist.get_previous();
            }
            catch (MusicPlayerPlaylistError e)
            {
                stop();
                throw new MusicPlayerError.PLAYLIST_OUT_OF_FILES( "Playlist %s has no more elements".printf(this.current_playlist));
            }
            play();
        }
        public async void next() throws MusicPlayerError, DBus.Error
        {
            if( current_playlist == null )
            {
                stop();
                throw new MusicPlayerError.PLAYLIST_OUT_OF_FILES( "No Playlist Selected" );
            }
            var playlist = playlists.lookup( current_playlist );
            try
            {
                this.current_song = playlist.get_next();
            }
            catch (MusicPlayerPlaylistError e)
            {
                stop();
                throw new MusicPlayerError.PLAYLIST_OUT_OF_FILES( "Playlist %s has no more elements".printf(this.current_playlist));
            }
            play();
        }
        public async void seek_forward( int step ) throws MusicPlayerError, DBus.Error
        {
            jump( current_position + step );
        }
        public async void seek_backward( int step ) throws MusicPlayerError, DBus.Error
        {
            jump( current_position - step );
        }
        public async void jump( int pos ) throws MusicPlayerError, DBus.Error
        {
            audio_pipeline.seek_simple( Gst.Format.TIME, Gst.SeekFlags.NONE, ((int64)pos) * 1000000000 / Config.precision );
        }
        public async ObjectPath[] get_playlists() throws MusicPlayerError, DBus.Error
        {
            var keys = this.playlists.get_keys();
            ObjectPath[] paths = new ObjectPath[keys.length()];
            int i = 0;
            foreach( var key in keys)
            {
                paths[i++] = key;
            }
            return paths;
        }
        public async ObjectPath get_current_playlist() throws MusicPlayerError, DBus.Error
        {
            if( current_playlist == null )
                 throw new MusicPlayerError.NO_PLAYLIST_SELECTED( "No current playlist" );
            return current_playlist;
        }
        public async void set_current_playlist( ObjectPath list ) throws MusicPlayerError, DBus.Error
        {
            var playlist = playlists.lookup( list );
            if( playlist == null )
                 throw new MusicPlayerError.UNKNOWN_PLAYLIST( "Playlist not found for %s".printf( list.rchr( list.len(),'/' ).next_char() ) );
            current_playlist = list;
        }
        public async void delete_playlist( ObjectPath list ) throws MusicPlayerError, DBus.Error
        {
            var the_list = playlists.lookup( list );
            the_list.delete_files();
            playlists.remove( list );
            playlist_removed( list );
            the_list = null;

        }
        public async ObjectPath new_playlist( string name ) throws MusicPlayerError, DBus.Error
        {
            var list = new Playlist( name, key_file, this );
            return add_playlist( name, list );
        }
        public ObjectPath add_playlist( string name, Playlist pl ) throws MusicPlayerError, DBus.Error
        {
            var obj_path = generate_path_for_playlist( name );
            var tmpobj = playlists.lookup( obj_path );
            if( tmpobj != null )
                 return obj_path;
            playlists.insert( obj_path, pl );
            playlist_added( obj_path );
            subsystem.registerServiceObject( FsoFramework.MusicPlayer.ServiceDBusName,
                                              obj_path, pl );
            return obj_path;
        }
        public string[] search( string query ) throws MusicPlayerError, DBus.Error
        {
            return new string[0];
        }
        public async int get_volume() throws DBus.Error
        {
            Gst.Value ret = Gst.Value();
            ret.init( typeof( double ) );
            //workround for vala bug in gst bidings
            //get_volume won't work anymore
            Gst.ChildProxy.get_property( volume, "volume", ret );
            return (int)( ret.get_double() * 100.0 );
        }
        public async void set_volume( int vol ) throws MusicPlayerError, DBus.Error
        {
            if( vol > 1000 )
                 vol = 1000;
            else if( vol < 0 )
                 vol = 0;
            Gst.ChildProxy.set( volume, "volume", (double)( vol / 100.0 ) );
            current_volume = vol;
        }

        public async void push_pause()
        {
            wait_counter ++;
            logger.info( @"push_pause: $wait_counter" );
        }
        public async void pop_pause()
        {
            wait_counter --;
            logger.info( @"pop_pause: $wait_counter" );
        }
        //
        // None DBus Interface methods
        //
        public void set_gst_state( Gst.State s )
        {
            switch( s )
            {
                case Gst.State.READY:
                case Gst.State.NULL:
                case Gst.State.VOID_PENDING:
                    _state = FreeSmartphone.MusicPlayerState.STOPPED;
                    break;
                case Gst.State.PAUSED:
                    _state = FreeSmartphone.MusicPlayerState.PAUSED;
                    break;
                case Gst.State.PLAYING:
                    _state = FreeSmartphone.MusicPlayerState.PLAYING;
                    break;
                default:
                    break;
            }
        }
        public void save()
        {
            logger.debug( "Saving MusicPlayer" );
            var song = this.current_song == null ? "": this.current_song;
            var list = this.current_playlist == null ? "" : this.current_playlist;
            key_file.set_string( Config.MUSIC_PLAYER_GROUP, Config.LAST_PLAYED, song );
            key_file.set_string( Config.MUSIC_PLAYER_GROUP, Config.LAST_PLAYLIST, list );

            foreach( var k in playlists.get_values() )
            {
                k.save();
            }
            logger.info( @"saving config to $(Config.get_config_path())" );
            save_keyfile_to_file( key_file, Config.get_config_path() );
        }
        //
        // private methods
        //
        private void setup_audio_elements()
        {
            register_element( "mad name=core", ".mp3", audio_codecs );
            if( ! register_element( "oggdemux ! ivorbisdec name=core ! audioconvert", ".ogg", audio_codecs ) )
                 register_element( "oggdemux ! vorbisdec name=core ! audioconvert", ".ogg", audio_codecs );
            register_element( "flacdec name=core ! audioconvert", ".flac", audio_codecs );
            register_element( "waveparse name=core", ".wav", audio_codecs );
            register_element( "siddec name=core" , ".sid", audio_codecs );
            register_element( "modplug name=core", ".mod", audio_codecs );
        }
        private void setup_source_elements()
        {
            register_element( "filesrc name=source", "", audio_srcs );
            register_element( "filesrc name=source", "file" , audio_srcs );
            if( ! register_element( "souphttpsrc name=source", "http", audio_srcs ) )
                register_element( "neonhttpsrc name=source", "http", audio_srcs );
            register_element( "mmssrc name=source", "mms" , audio_srcs );
        }
        private bool register_element( string elements, string extension, HashTable<string,string> to )
        {
            if( ! gst_plugins_installed( elements ) )
                 return false;
            to.insert( extension, elements );
            debug( @"Registered audio elements \"$elements\" for $extension" );
            return true;
        }
        private string get_protocol_for_uri( string uri )
        {
            if( uri.has_prefix( "/" ) )
                 return "file";
            string prefix = uri.split( ":", 2 )[0];
            return prefix;
        }

        private DBus.ObjectPath generate_path_for_playlist( string name )
        {
            DBus.ObjectPath ret = null;
            try
            {
                var regex = new Regex( "[^[:alnum:]_]+" );
                var tmp = regex.replace( name, name.len(), 0, "_" );
                ret = new DBus.ObjectPath ( FsoFramework.MusicPlayer.PlaylistServicePathPrefix + "/" + tmp );
            }
            catch( RegexError e )
            {
                logger.error( @"Replacing $name for ObjectPath: $(e.message)" );
            }
            return ret;
        }

        private bool gst_plugins_installed( string pipe )
        {
            //split into elements
            logger.debug( @"pipe: $pipe" );
            var pipe_splitted = pipe.split( "!" );
            foreach( var p in pipe_splitted )
            {
                logger.debug( @"element full: $p" );
                var element_name = p.strip().split( " ", 2 )[0];

                if( Gst.ElementFactory.make( element_name, null) == null )
                {
                    logger.error( @"Cannot load '$element_name'. Try install gst-plugin-'$element_name'" );
                    return false;
                }
            }
            return true;
        }

        //
        // Bus Callbacks
        //
        private void foreach_tag (Gst.TagList list, string tag)
        {
            Gst.Value val;
            Gst.TagList.copy_value( out val, list, tag );
            if( ! val.holds( typeof( Gst.Buffer ) ) && ! val.holds( typeof( Gst.Date ) ) )
                cur_song_info.insert( tag, val );
        }
        private bool bus_callback (Gst.Bus bus, Gst.Message message)
        {
            switch( message.type )
            {
                case MessageType.ERROR:
                    GLib.Error err;
                    string debug;
                    message.parse_error( out err, out debug );
                    logger.error( @"Message: $debug $(err.message)" );
                    break;
                case MessageType.WARNING:
                    GLib.Error err;
                    string debug;
                    message.parse_warning( out err, out debug );
                    logger.warning( @"Message: $debug $(err.message)" );
                    break;
                case MessageType.INFO:
                    GLib.Error err;
                    string debug;
                    message.parse_info( out err, out debug );
                    logger.info( @"Message: $debug $(err.message)" );
                    break;
                case MessageType.EOS:
                    logger.info("End of stream");
                    next();
                    break;
                case MessageType.STATE_CHANGED:
                    Gst.State oldstate;
                    Gst.State newstate;
                    Gst.State pending;
                    message.parse_state_changed( out oldstate, out newstate, out pending );
                    set_gst_state( newstate );
                    break;
                case MessageType.TAG:
                    Gst.TagList tag_list;
                    message.parse_tag (out tag_list);
                    tag_list.foreach (foreach_tag);
                    break;
                case MessageType.BUFFERING:
                    this._state = FreeSmartphone.MusicPlayerState.BUFFERING;
                    break;
                default:
                    debug( @"ignored message: $(message.type)" );
                    break;
            }
            return true;
        }
        public bool position_timeout()
        {
            Gst.Format fmt = Gst.Format.TIME;
            int64 cur;
            if( core_stream.query_position( ref fmt, out cur ) )
            {
                current_position = (int)(cur * Config.precision / 1000000000 );
            }
            else
            {
                logger.error("Get position");
            }
            //Call me again
            return true;
        }
        //
        // FsoFramework.AbstractObject
        //
        public override string repr()
        {
            return @"<$(this.get_type().name())>";
        }
        //
        // public methods
        //

        public async void jump_to_file_in_playlist( Playlist pl )
        {
            string name = null;
            try
            {
                name = yield pl.get_name();
            }
            catch (GLib.Error e)
            {
                logger.error( @"Lookup name for JumpTo: $(e.message)" );
                return;
            }
            var objpath = generate_path_for_playlist( name );
            current_playlist = objpath;
            try
            {
                current_song = pl.get_current_file();

            }
            catch (MusicPlayerPlaylistError mppe)
            {
                logger.debug( @"Tried to jump into playlist '$name': $(mppe.message)" );
            }
        }

        private class TagHelper: GLib.Object
        {
            public HashTable<string,GLib.Value?> tags{
                get;
                set;
                default = new HashTable<string,GLib.Value?>(str_hash, str_equal );}
            public void foreach_tag( Gst.TagList list, string tag )
            {
                debug( @"new tag $tag" );
                Gst.Value val;
                Gst.TagList.copy_value( out val, list, tag );
                if( ! val.holds( typeof( Gst.Buffer ) ) && ! val.holds( typeof( Gst.Date ) ) )
                    tags.insert( tag, val );
            }

        }

        //
        // Helpers
        //
        public static void save_keyfile_to_file( GLib.KeyFile kf, string file )
        {
            var f = FileStream.open( file, "w" );
            if( f == null )
                 FsoFramework.Logger.defaultLogger().error( @"Cannot open $file: $(strerror(GLib.errno))");
            else
            {
                string data = kf.to_data();
                FsoFramework.Logger.defaultLogger().info( @"Saving KeyFile to $file" );
                f.puts(data);
            }
        }
    }
}