enum PlaybinState { STOPPED, PAUSED, PLAYING, } private class SourceFuncWrapper { public SourceFunc inner; public SourceFuncWrapper () { this.inner = null; } } class Playbin : GLib.Object { private Mpv.Handle mpv = new Mpv.Handle (); public PlaybinState state { get; private set; default = PlaybinState.STOPPED; } private int _volume = 100; public int volume { get { return _volume; } set { _volume = value; assert (mpv.set_property_int64 ("volume", value) >= 0); } } public bool _mute = false; public bool mute { get { return _mute; } set { _mute = value; assert (mpv.set_property_flag ("mute", value) >= 0); } } public uint play_queue_position { get; private set; } public Subsonic.Song? song { get; private set; } public signal void now_playing (); public double position { get; private set; default = 0.0; } public double duration { get; private set; default = 0.0; } public Subsonic.Client api { get; set; default = null; } private ListModel _play_queue = null; public ListModel play_queue { get { return _play_queue; } set { assert (_play_queue == null); // only set this once _play_queue = value; value.items_changed.connect (on_play_queue_items_changed); } } private void on_play_queue_items_changed (ListModel play_queue, uint position, uint removed, uint added) { try { for (uint i = 0; i < removed; i += 1) { assert (this.mpv.command ({"playlist-remove", position.to_string ()}) >= 0); } for (uint i = 0; i < added; i += 1) { assert (this.mpv.command ({"loadfile", this.api.stream_uri (((Subsonic.Song) play_queue.get_item (position+i)).id), "insert-at-play", (position+i).to_string ()}) >= 0); } } catch (Error e) { error ("could not update mpv playlist: %s\n", e.message); } } public Playbin () { assert (this.mpv.initialize () >= 0); assert (this.mpv.set_property_string ("user-agent", Audrey.Const.user_agent) >= 0); assert (this.mpv.set_property_string ("video", "no") >= 0); assert (this.mpv.set_property_string ("prefetch-playlist", "yes") >= 0); assert (this.mpv.set_property_string ("gapless-audio", "yes") >= 0); assert (this.mpv.observe_property (0, "time-pos", Mpv.Format.DOUBLE) >= 0); assert (this.mpv.observe_property (1, "duration", Mpv.Format.DOUBLE) >= 0); assert (this.mpv.observe_property (2, "playlist-pos", Mpv.Format.INT64) >= 0); this.mpv.wakeup_callback = () => { Idle.add (() => { while (true) { var event = this.mpv.wait_event (0.0); if (event.event_id == Mpv.EventId.NONE) break; switch (event.event_id) { case Mpv.EventId.PROPERTY_CHANGE: switch (event.reply_userdata) { case 0: var data = (Mpv.EventProperty *) event.data; assert (data.name == "time-pos"); if (data.format == Mpv.Format.NONE) { this.position = 0.0; } else { assert (data.format == Mpv.Format.DOUBLE); this.position = * (double *) data.data; } break; case 1: var data = (Mpv.EventProperty *) event.data; assert (data.name == "duration"); if (data.format == Mpv.Format.NONE) { this.duration = 0.0; } else { assert (data.format == Mpv.Format.DOUBLE); this.duration = * (double *) data.data; } break; case 2: var data = (Mpv.EventProperty *) event.data; assert (data.name == "playlist-pos"); if (data.format == Mpv.Format.NONE) { this.play_queue_position = 0; } else { assert (data.format == Mpv.Format.INT64); this.play_queue_position = (uint) * (int64 *) data.data; } break; default: assert (false); break; } break; case Mpv.EventId.START_FILE: // ignore break; case Mpv.EventId.FILE_LOADED: this.song = (Subsonic.Song) this.play_queue.get_item (this.play_queue_position); this.now_playing (); break; case Mpv.EventId.PLAYBACK_RESTART: // ignore break; case Mpv.EventId.SEEK: // ignore break; case Mpv.EventId.END_FILE: // ignore break; // deprecated, ignore case Mpv.EventId.IDLE: case Mpv.EventId.TICK: // uninteresting, ignore case Mpv.EventId.AUDIO_RECONFIG: break; default: print ("got unimplemented %s\n", event.event_id.to_string ()); break; } } return false; }); }; } public void seek (double position) { this.position = position; assert (this.mpv.command ({"seek", position.to_string (), "absolute"}) >= 0); } // manually changes which track in the play queue to play public void select_track (uint position) requires (position < this.play_queue.get_n_items ()) { assert (this.mpv.command ({"playlist-play-index", position.to_string ()}) >= 0); this.state = PlaybinState.PLAYING; } public void pause () { assert (this.state != PlaybinState.STOPPED); this.state = PlaybinState.PAUSED; assert (this.mpv.command ({"pause"}) >= 0); } public void play () { assert (this.state != PlaybinState.STOPPED); this.state = PlaybinState.PLAYING; assert (this.mpv.command ({"play"}) >= 0); } public void next_track () { assert (this.state != PlaybinState.STOPPED); this.state = PlaybinState.PLAYING; assert (this.mpv.command ({"playlist-next-playlist"}) >= 0); } public void prev_track () { assert (this.state != PlaybinState.STOPPED); this.state = PlaybinState.PLAYING; assert (this.mpv.command ({"playlist-prev-playlist"}) >= 0); } }