Skip to content

Commit d34b67c

Browse files
danirabbitzeebok
andauthored
Send artwork to MPRIS (#805)
* Send artwork to MPRIS * Use cache dir to fix sandbox * Better variable name * Don't re-fetch art if we already cached it --------- Co-authored-by: Ryan Kornheisl <[email protected]>
1 parent c76587c commit d34b67c

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

src/AudioObject.vala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
public class Music.AudioObject : Object {
77
public string uri { get; construct; }
8-
public Gdk.Texture texture { get; set; }
8+
public Gdk.Texture? texture { get; set; default = null; }
9+
public string album { get; set; }
910
public string artist { get; set; }
1011
public string title { get; set; }
1112
public int64 duration { get; set; default = 0; }
13+
public string art_url { get; set; default = ""; }
1214

1315
public AudioObject (string uri) {
1416
Object (uri: uri);

src/DBus/MprisPlayer.vala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public class Music.MprisPlayer : Object {
3434
_metadata.insert ("xesam:artist", array);
3535
}
3636

37+
if (audio.art_url != "") {
38+
_metadata.insert ("mpris:artUrl", audio.art_url);
39+
}
40+
3741
if (audio.title != null) {
3842
_metadata.insert ("xesam:title", audio.title);
3943
}

src/PlaybackManager.vala

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ public class Music.PlaybackManager : Object {
176176

177177
unowned Gst.TagList? tag_list = info.get_tags ();
178178

179+
string _album;
180+
tag_list.get_string (Gst.Tags.ALBUM, out _album);
181+
if (_album != null) {
182+
audio_object.album = _album;
183+
}
184+
179185
string _title;
180186
tag_list.get_string (Gst.Tags.TITLE, out _title);
181187
if (_title != null) {
@@ -190,12 +196,37 @@ public class Music.PlaybackManager : Object {
190196
audio_object.artist = _("Unknown");
191197
}
192198

193-
var sample = get_cover_sample (tag_list);
194-
if (sample != null) {
195-
var buffer = sample.get_buffer ();
199+
string art_hash = uri;
200+
if (_artist != null && _album != null) {
201+
art_hash = "%s:%s".printf (_artist, _album);
202+
}
203+
204+
var art_file = File.new_for_path (Path.build_path (
205+
Path.DIR_SEPARATOR_S,
206+
get_art_cache_dir (),
207+
Checksum.compute_for_string (SHA256, art_hash)
208+
));
196209

197-
if (buffer != null) {
198-
audio_object.texture = Gdk.Texture.for_pixbuf (get_pixbuf_from_buffer (buffer));
210+
if (art_file.query_exists ()) {
211+
audio_object.art_url = art_file.get_uri ();
212+
audio_object.texture = Gdk.Texture.from_file (art_file);
213+
} else {
214+
var sample = get_cover_sample (tag_list);
215+
if (sample != null) {
216+
var buffer = sample.get_buffer ();
217+
218+
if (buffer != null) {
219+
var texture = Gdk.Texture.for_pixbuf (get_pixbuf_from_buffer (buffer));
220+
audio_object.texture = texture;
221+
222+
save_art_file.begin (texture, art_file, (obj, res) => {
223+
try {
224+
audio_object.art_url = save_art_file.end (res);
225+
} catch (Error e) {
226+
critical (e.message);
227+
}
228+
});
229+
}
199230
}
200231
}
201232
} else {
@@ -429,6 +460,24 @@ public class Music.PlaybackManager : Object {
429460
return pix;
430461
}
431462

463+
private async string save_art_file (Gdk.Texture texture, File file) throws Error requires (texture != null) {
464+
DirUtils.create_with_parents (get_art_cache_dir (), 0755);
465+
466+
var ostream = yield file.create_async (NONE);
467+
yield ostream.write_bytes_async (texture.save_to_png_bytes ());
468+
469+
return file.get_uri ();
470+
}
471+
472+
private string get_art_cache_dir () {
473+
return Path.build_path (
474+
Path.DIR_SEPARATOR_S,
475+
Environment.get_user_cache_dir (),
476+
GLib.Application.get_default ().application_id,
477+
"art"
478+
);
479+
}
480+
432481
private void on_items_changed () {
433482
var shuffle_action_action = (SimpleAction) GLib.Application.get_default ().lookup_action (Application.ACTION_SHUFFLE);
434483
has_items = queue_liststore.get_n_items () > 0;

0 commit comments

Comments
 (0)