Compare commits
2 commits
d666c8fb49
...
faa5d15e1e
Author | SHA1 | Date | |
---|---|---|---|
faa5d15e1e | |||
9f6bc7b10b |
2 changed files with 122 additions and 149 deletions
229
src/playbin.vala
229
src/playbin.vala
|
@ -57,6 +57,9 @@ public class Playbin : GLib.Object {
|
||||||
public ListStore _play_queue;
|
public ListStore _play_queue;
|
||||||
public ListModel play_queue { get { return this._play_queue; } }
|
public ListModel play_queue { get { return this._play_queue; } }
|
||||||
|
|
||||||
|
// try to prevent wait_event to be called twice
|
||||||
|
private bool is_handling_event = false;
|
||||||
|
|
||||||
private async Mpv.Error mpv_command_async (string[] args) {
|
private async Mpv.Error mpv_command_async (string[] args) {
|
||||||
CommandCallback cc = {};
|
CommandCallback cc = {};
|
||||||
|
|
||||||
|
@ -67,9 +70,6 @@ public class Playbin : GLib.Object {
|
||||||
return cc.error;
|
return cc.error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// should be Mpv.WakeupCallback, but i think there's a vala bug here
|
|
||||||
private SourceOnceFunc wakeup_callback; // anchor reference here, mpv won't remind us
|
|
||||||
|
|
||||||
public Playbin () {
|
public Playbin () {
|
||||||
this._play_queue = new ListStore (typeof (Subsonic.Song));
|
this._play_queue = new ListStore (typeof (Subsonic.Song));
|
||||||
|
|
||||||
|
@ -83,153 +83,126 @@ public class Playbin : GLib.Object {
|
||||||
assert (this.mpv.observe_property (2, "playlist-pos", Mpv.Format.INT64) >= 0);
|
assert (this.mpv.observe_property (2, "playlist-pos", Mpv.Format.INT64) >= 0);
|
||||||
assert (this.mpv.observe_property (3, "pause", Mpv.Format.FLAG) >= 0);
|
assert (this.mpv.observe_property (3, "pause", Mpv.Format.FLAG) >= 0);
|
||||||
|
|
||||||
int wakeup_fds[2];
|
this.mpv.wakeup_callback = () => {
|
||||||
try {
|
Idle.add (() => {
|
||||||
assert (Unix.open_pipe (wakeup_fds, 0));
|
if (this.is_handling_event) {
|
||||||
} catch (Error e) {
|
warning ("main thread mpv wakeup callback called twice");
|
||||||
error (@"could not open pipe for mpv wakeup: $(e.message)");
|
return false;
|
||||||
}
|
}
|
||||||
|
this.is_handling_event = true;
|
||||||
|
|
||||||
IOChannel wakeup_read = new IOChannel.unix_new (wakeup_fds[0]);
|
while (true) {
|
||||||
IOChannel wakeup_write = new IOChannel.unix_new (wakeup_fds[1]);
|
var event = this.mpv.wait_event (0.0);
|
||||||
|
if (event.event_id == Mpv.EventId.NONE) break;
|
||||||
|
|
||||||
wakeup_read.set_close_on_unref (true);
|
switch (event.event_id) {
|
||||||
wakeup_write.set_close_on_unref (true);
|
case Mpv.EventId.PROPERTY_CHANGE:
|
||||||
|
var data = event.parse_property ();
|
||||||
|
switch (event.reply_userdata) {
|
||||||
|
case 0:
|
||||||
|
assert (data.name == "time-pos");
|
||||||
|
if (data.format == Mpv.Format.NONE) {
|
||||||
|
this.position = 0.0;
|
||||||
|
} else {
|
||||||
|
this.position = data.parse_double ();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
try {
|
case 1:
|
||||||
wakeup_read.set_encoding (null);
|
assert (data.name == "duration");
|
||||||
wakeup_write.set_encoding (null);
|
if (data.format == Mpv.Format.NONE) {
|
||||||
wakeup_write.set_buffered (false);
|
// this.duration = 0.0; i think this prevents the fallback below from working
|
||||||
} catch (Error e) {
|
} else {
|
||||||
error (@"could not set up pipes for mpv wakeup: $(e.message)");
|
this.duration = data.parse_double ();
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
this.wakeup_callback = () => {
|
case 2:
|
||||||
try {
|
// here as a sanity check
|
||||||
wakeup_write.write_chars ({0}, null);
|
// should always match our own play_queu_position/state
|
||||||
} catch (Error e) {
|
assert (data.name == "playlist-pos");
|
||||||
error (@"could not write to mpv wakeup pipe: $(e.message)");
|
int64 playlist_pos = data.parse_int64 ();
|
||||||
}
|
if (playlist_pos < 0) {
|
||||||
};
|
if (this.state != PlaybinState.STOPPED) {
|
||||||
this.mpv.wakeup_callback = this.wakeup_callback;
|
error ("mpv has no current playlist entry, but we think it's index %u", this.play_queue_position);
|
||||||
|
}
|
||||||
|
assert (this.play_queue_position == this.play_queue.get_n_items ());
|
||||||
|
} else {
|
||||||
|
if (this.state == PlaybinState.STOPPED) {
|
||||||
|
error ("mpv is at playlist entry %u, but we're stopped", (uint) playlist_pos);
|
||||||
|
}
|
||||||
|
if (this.play_queue_position != (uint) playlist_pos) {
|
||||||
|
error ("mpv is at playlist entry %u, but we think it's %u", (uint) playlist_pos, this.play_queue_position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
assert (0 < wakeup_read.add_watch (IOCondition.IN, (source, condition) => {
|
case 3:
|
||||||
try {
|
// also here as a sanity check
|
||||||
wakeup_read.read_chars ({0}, null);
|
// should always match our own state
|
||||||
} catch (Error e) {
|
assert (data.name == "pause");
|
||||||
error (@"could not read from mpv wakeup pipe: $(e.message)");
|
bool pause = data.parse_flag ();
|
||||||
}
|
if (pause && this.state != PlaybinState.PAUSED) {
|
||||||
|
error (@"mpv is paused, but we are @(this.state)");
|
||||||
|
}
|
||||||
|
if (!pause && this.state == PlaybinState.PAUSED) {
|
||||||
|
error ("mpv is not paused, but we are paused");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
while (true) {
|
default:
|
||||||
var event = this.mpv.wait_event (0.0);
|
assert (false);
|
||||||
if (event.event_id == Mpv.EventId.NONE) break;
|
break;
|
||||||
|
|
||||||
switch (event.event_id) {
|
|
||||||
case Mpv.EventId.PROPERTY_CHANGE:
|
|
||||||
var data = event.parse_property ();
|
|
||||||
switch (event.reply_userdata) {
|
|
||||||
case 0:
|
|
||||||
assert (data.name == "time-pos");
|
|
||||||
if (data.format == Mpv.Format.NONE) {
|
|
||||||
this.position = 0.0;
|
|
||||||
} else {
|
|
||||||
this.position = data.parse_double ();
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 1:
|
case Mpv.EventId.START_FILE:
|
||||||
assert (data.name == "duration");
|
debug ("START_FILE received");
|
||||||
if (data.format == Mpv.Format.NONE) {
|
|
||||||
// this.duration = 0.0; i think this prevents the fallback below from working
|
// estimate duration from api data
|
||||||
} else {
|
// while mpv doesn't know it
|
||||||
this.duration = data.parse_double ();
|
this.duration = ((Subsonic.Song) this._play_queue.get_item (this.play_queue_position)).duration;
|
||||||
}
|
|
||||||
|
this.new_track ();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2:
|
case Mpv.EventId.END_FILE:
|
||||||
// here as a sanity check
|
var data = event.parse_end_file ();
|
||||||
// should always match our own play_queu_position/state
|
debug (@"END_FILE received (reason: $(data.reason))");
|
||||||
assert (data.name == "playlist-pos");
|
|
||||||
int64 playlist_pos = data.parse_int64 ();
|
if (data.error < 0) {
|
||||||
if (playlist_pos < 0) {
|
warning ("playback of track aborted: %s", data.error.to_string ());
|
||||||
if (this.state != PlaybinState.STOPPED) {
|
}
|
||||||
error ("mpv has no current playlist entry, but we think it's index %u", this.play_queue_position);
|
|
||||||
}
|
if (data.reason == Mpv.EndFileReason.EOF) {
|
||||||
assert (this.play_queue_position == this.play_queue.get_n_items ());
|
// assume this is a proper transition
|
||||||
} else {
|
this.play_queue_position += 1;
|
||||||
if (this.state == PlaybinState.STOPPED) {
|
|
||||||
error ("mpv is at playlist entry %u, but we're stopped", (uint) playlist_pos);
|
if (this.play_queue_position == this._play_queue.get_n_items ()) {
|
||||||
}
|
// reached the end (?)
|
||||||
if (this.play_queue_position != (uint) playlist_pos) {
|
this.state = PlaybinState.STOPPED;
|
||||||
error ("mpv is at playlist entry %u, but we think it's %u", (uint) playlist_pos, this.play_queue_position);
|
this.stopped ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3:
|
case Mpv.EventId.COMMAND_REPLY:
|
||||||
// also here as a sanity check
|
unowned CommandCallback *cc = (CommandCallback *) event.reply_userdata;
|
||||||
// should always match our own state
|
cc.error = event.error;
|
||||||
assert (data.name == "pause");
|
cc.callback ();
|
||||||
bool pause = data.parse_flag ();
|
|
||||||
if (pause && this.state != PlaybinState.PAUSED) {
|
|
||||||
error (@"mpv is paused, but we are @(this.state)");
|
|
||||||
}
|
|
||||||
if (!pause && this.state == PlaybinState.PAUSED) {
|
|
||||||
error ("mpv is not paused, but we are paused");
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
assert (false);
|
// ignore by default
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
case Mpv.EventId.START_FILE:
|
|
||||||
debug ("START_FILE received");
|
|
||||||
|
|
||||||
// estimate duration from api data
|
|
||||||
// while mpv doesn't know it
|
|
||||||
this.duration = ((Subsonic.Song) this._play_queue.get_item (this.play_queue_position)).duration;
|
|
||||||
|
|
||||||
this.new_track ();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case Mpv.EventId.END_FILE:
|
|
||||||
var data = event.parse_end_file ();
|
|
||||||
debug (@"END_FILE received (reason: $(data.reason))");
|
|
||||||
|
|
||||||
if (data.error < 0) {
|
|
||||||
warning ("playback of track aborted: %s", data.error.to_string ());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.reason == Mpv.EndFileReason.EOF) {
|
|
||||||
// assume this is a proper transition
|
|
||||||
this.play_queue_position += 1;
|
|
||||||
|
|
||||||
if (this.play_queue_position == this._play_queue.get_n_items ()) {
|
|
||||||
// reached the end (?)
|
|
||||||
this.state = PlaybinState.STOPPED;
|
|
||||||
this.stopped ();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case Mpv.EventId.COMMAND_REPLY:
|
|
||||||
unowned CommandCallback *cc = (CommandCallback *) event.reply_userdata;
|
|
||||||
cc.error = event.error;
|
|
||||||
cc.callback ();
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
// ignore by default
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
this.is_handling_event = false;
|
||||||
}));
|
return false;
|
||||||
|
});
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public void seek (double position) {
|
public void seek (double position) {
|
||||||
|
|
|
@ -43,7 +43,7 @@ namespace Mpv {
|
||||||
[CCode (cname = "mpv_wait_event")]
|
[CCode (cname = "mpv_wait_event")]
|
||||||
public unowned Event? wait_event (double timeout);
|
public unowned Event? wait_event (double timeout);
|
||||||
|
|
||||||
public unowned WakeupCallback wakeup_callback {
|
public WakeupCallback wakeup_callback {
|
||||||
[CCode (cname = "mpv_set_wakeup_callback")] set;
|
[CCode (cname = "mpv_set_wakeup_callback")] set;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue