some async

This commit is contained in:
Erica Z 2024-10-25 22:09:57 +02:00
parent f436557bf5
commit 2b2ace0f5c
3 changed files with 40 additions and 1 deletions

View file

@ -4,6 +4,11 @@ public enum PlaybinState {
PLAYING, PLAYING,
} }
private struct CommandCallback {
unowned SourceFunc callback;
int error;
}
public class Playbin : GLib.Object { public class Playbin : GLib.Object {
private Mpv.Handle mpv = new Mpv.Handle (); private Mpv.Handle mpv = new Mpv.Handle ();
@ -55,6 +60,16 @@ public class Playbin : GLib.Object {
// try to prevent wait_event to be called twice // try to prevent wait_event to be called twice
private bool is_handling_event = false; private bool is_handling_event = false;
private async Mpv.Error mpv_command_async (string[] args) {
CommandCallback cc = {};
this.mpv.command_async ((uint64) &cc, args);
cc.callback = this.mpv_command_async.callback;
yield;
return cc.error;
}
public Playbin () { public Playbin () {
this._play_queue = new ListStore (typeof (Subsonic.Song)); this._play_queue = new ListStore (typeof (Subsonic.Song));
@ -169,6 +184,12 @@ public class Playbin : GLib.Object {
break; break;
case Mpv.EventId.COMMAND_REPLY:
unowned CommandCallback *cc = (CommandCallback *) event.reply_userdata;
cc.error = event.error;
cc.callback ();
break;
default: default:
// ignore by default // ignore by default
break; break;
@ -268,7 +289,7 @@ public class Playbin : GLib.Object {
} }
public void append_track (Subsonic.Song song) { public void append_track (Subsonic.Song song) {
assert (this.mpv.command({ assert (this.mpv.command ({
"loadfile", "loadfile",
this.api.stream_uri (song.id), this.api.stream_uri (song.id),
"append", "append",
@ -277,6 +298,18 @@ public class Playbin : GLib.Object {
this._play_queue.append (song); this._play_queue.append (song);
} }
public async void append_track_async (Subsonic.Song song) {
var err = yield this.mpv_command_async ({
"loadfile",
this.api.stream_uri (song.id),
"append",
});
assert (err >= 0);
if (this.state == STOPPED) this.play_queue_position += 1;
this._play_queue.append (song);
}
public void move_track (uint from, uint to) public void move_track (uint from, uint to)
requires (from < this._play_queue.get_n_items ()) requires (from < this._play_queue.get_n_items ())
requires (to < this._play_queue.get_n_items ()) requires (to < this._play_queue.get_n_items ())

View file

@ -224,6 +224,9 @@ public class Subsonic.Client : Object {
reader.read_element (i); reader.read_element (i);
callback (new Song (reader)); callback (new Song (reader));
reader.end_element (); reader.end_element ();
Idle.add (this.get_random_songs.callback);
yield;
} }
assert (reader.get_error () == null); assert (reader.get_error () == null);

View file

@ -65,6 +65,9 @@ namespace Mpv {
[CCode (cname = "mpv_command")] [CCode (cname = "mpv_command")]
public Error command ([CCode (array_length = false)] string[] args); public Error command ([CCode (array_length = false)] string[] args);
[CCode (cname = "mpv_command_async")]
public Error command_async (uint64 reply_userdata, [CCode (array_length = false)] string[] args);
[CCode (cname = "mpv_observe_property")] [CCode (cname = "mpv_observe_property")]
public Error observe_property (uint64 reply_userdata, string name, Format format); public Error observe_property (uint64 reply_userdata, string name, Format format);
} }