Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

feat: the lock screen plays Canvas or an animated album cover

My-Name-Is-Jeffffe92a948f8e
diff --git a/docs/cleanroom/motion-catalog.md b/docs/cleanroom/motion-catalog.md
new file mode 100644
index 00000000..65cae2a9
--- /dev/null
+++ b/docs/cleanroom/motion-catalog.md
@@ -0,0 +1,173 @@
+# Spec: Apple Music catalog lookups (`SGMotionCatalog.m`)
+
+A functional spec for a clean-room implementation. It says what the code must do and what the
+services return, as observed on 2026-10-05. It says nothing about how any other implementation is
+structured. Implement it without reading spoti.pw's or Chroma's code from after commit `c790445`.
+
+## Where it goes
+
+`tweak/Sources/Shared/AnimatedArtwork/SGMotionCatalog.m`, Objective-C with ARC, iOS 16 deployment
+target. It implements these functions from `AnimatedArtwork.h`:
+
+| Function | Contract |
+|---|---|
+| `SGMotionNameKey(name)` | A comparison key for an artist or album name |
+| `SGMotionStreamIn(master, pixels)` | The URI of the stream to download, from an HLS master playlist |
+| `SGMotionWholeFileIn(media)` | The URI of the one file a media playlist's segments all come from, or nil |
+| `SGMotionAlbumCover(artist, album, shape, pixels, done)` | An album's animated cover as a local file, or nil |
+| `SGMotionArtistLogo(artist, pixels, done)` | An artist's logo as a `UIImage`, or nil |
+
+It may use:
+
+- `SGMotionFile(remote, done)` from `SGMotionStore.m`, which downloads a remote video to a local file
+- `SGFlag(SGKeyMotionLowData, NO)` from `Core/SGCore.h`
+- `SGLog(format, ...)` from `Core/SGCore.h`, with every line prefixed `motion: `
+
+Every `done` block runs on the main queue, exactly once.
+
+## Behaviour
+
+### Name keys
+
+- Case and diacritics do not matter.
+- Punctuation does not matter. "Short n’ Sweet" and "Short n' Sweet" give the same key.
+- An edition in round or square brackets does not matter: "Midnights (The Til Dawn Edition)" gives the
+ same key as "Midnights".
+- Anything after " - " does not matter: "Espresso - Single" gives the same key as "Espresso".
+- A non-string input gives an empty key.
+
+### Artist match
+
+Spotify names the lead artist. Apple Music may name several, for example "Xavier Omär & ELHAE".
+Two artist names match when the words of one appear, in order and as whole words, inside the other.
+Empty names never match.
+
+### Album cover
+
+1. Return nil at once when the artist or album key is empty.
+2. Search the catalog for albums with the term "artist album". Ask for `editorialVideo`.
+3. Use the first result whose artist matches, whose album key equals the requested album key, and
+ whose `editorialVideo` has one of the entries in step 4. Editions of one album share a key, and
+ only one of them may have the motion.
+4. From its `editorialVideo`, take the first that exists of:
+ - tall shape: `motionTallVideo3x4`, `motionDetailTall`, `motionSquareVideo1x1`, `motionDetailSquare`
+ - square shape: `motionSquareVideo1x1`, `motionDetailSquare`
+5. That entry's `video` is the URL of an HLS master playlist. Download it, pick a stream with
+ `SGMotionStreamIn`, download that stream's media playlist, and get its file with
+ `SGMotionWholeFileIn`. Resolve each relative URI against the playlist it came from.
+6. Pass the file's absolute URL to `SGMotionFile` and hand its result to `done`.
+
+Remember each answer for the rest of the launch, per shape, artist key and album key. An album with
+no animated cover is remembered as having none. A failed request (network error or non-200 status)
+is not remembered, so the next call asks again.
+
+### Stream choice
+
+Each stream in a master playlist is an `#EXT-X-STREAM-INF:` line followed by its URI line. Its
+attributes include `RESOLUTION=WxH` and `CODECS="..."`.
+
+- Take the narrowest stream at least `pixels` wide.
+- If none is that wide, take the widest.
+- Between streams of the same width, prefer HEVC (`CODECS` starting `hvc1` or `hev1`) over H.264.
+- Return nil when there is no usable stream.
+
+### Whole file
+
+A media playlist may name one file for all of its segments. `#EXT-X-MAP:URI="..."` names it for the
+header, and each segment's URI line names it again, with `#EXT-X-BYTERANGE` lines giving the ranges.
+Return that URI when every URI in the playlist is the same, and nil otherwise.
+
+### Artist logo
+
+1. Return nil at once when the artist key is empty.
+2. Search the catalog for artists with the artist name. Ask for `editorialArtwork`.
+3. Use the first result whose name key equals the artist key.
+4. Its `editorialArtwork.musicContentColorLogoTrimmed` has `url`, `width` and `height`. The URL is a
+ template that ends in `{w}x{h}bb.jpg`.
+5. Fill the template for `pixels` wide, the height in the logo's own aspect, and ask for `png` instead
+ of `jpg`. The PNG has an alpha channel.
+6. Download it and return the image.
+
+Remember each answer, the image or none, for the rest of the launch.
+
+## The services
+
+### Token
+
+The catalog needs a developer token, a JWT. Get it from Apple Music's web player:
+
+1. GET `https://music.apple.com/us/browse`. The HTML names one script at a path like
+ `/assets/index~c10ba4a68d.js`. The hash changes with each web player release.
+2. GET that script. It is several megabytes, so search it off the main queue.
+3. It holds several JWTs in quotes. Decode each payload (base64url). Use the one whose `iss` claim is
+ `AMPWebPlay`. Its `exp` claim is the expiry in Unix seconds, about ten weeks out. It also carries
+ `root_https_origin: ["apple.com"]`.
+
+Keep the token in `NSUserDefaults` under `spotifyglass.motion.token`. Reuse it while it is valid for
+more than another hour. Callers that ask while a token is being read wait for that read. When the
+catalog answers 401, read the token again and retry that request once.
+
+Requests to `music.apple.com` send a desktop Safari User-Agent.
+
+### Catalog search
+
+ GET https://amp-api.music.apple.com/v1/catalog/us/search
+ ?term=<term>&types=<albums|artists>&limit=10&extend=<editorialVideo|editorialArtwork>
+ Authorization: Bearer <token>
+ Origin: https://music.apple.com
+
+Without the Origin header the answer is 401. The token does not work on `api.music.apple.com`
+for this data.
+
+Albums answer, trimmed:
+
+ {"results": {"albums": {"data": [{"id": "1689131527", "attributes": {
+ "name": "Midnights (The Til Dawn Edition)", "artistName": "Taylor Swift",
+ "editorialVideo": {
+ "motionDetailSquare": {"video": "https://mvod.itunes.apple.com/.../xxx.m3u8", "previewFrame": {...}},
+ "motionDetailTall": {"video": "...", "previewFrame": {...}},
+ "motionSquareVideo1x1": {"video": "...", "previewFrame": {...}},
+ "motionTallVideo3x4": {"video": "...", "previewFrame": {...}}}}}]}}}
+
+Albums without an animated cover have no `editorialVideo`. A search with no albums may have no
+`albums` key at all.
+
+Artists answer, trimmed:
+
+ {"results": {"artists": {"data": [{"id": "159260351", "attributes": {
+ "name": "Taylor Swift",
+ "editorialArtwork": {"musicContentColorLogoTrimmed": {
+ "url": "https://is1-ssl.mzstatic.com/image/thumb/.../{w}x{h}bb.jpg",
+ "width": 4826, "height": 1264, "bgColor": "000000"}}}}]}}}
+
+Any field may be missing or of another type. Check each type before use.
+
+### Playlists
+
+A master playlist (abridged):
+
+ #EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=2114513,BANDWIDTH=2251133,CODECS="avc1.64001f",FRAME-RATE=30.000,RESOLUTION=768x768
+ P544849304_..._768x768.m3u8
+ #EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=1674892,BANDWIDTH=1936611,CODECS="hvc1.2.20000000.L123.B0",FRAME-RATE=30.000,RESOLUTION=768x768
+ ...
+
+Square covers run from 360x360 to 2160x2160, in both H.264 and HEVC.
+
+A media playlist:
+
+ #EXTM3U
+ #EXT-X-TARGETDURATION:3
+ #EXT-X-PLAYLIST-TYPE:VOD
+ #EXT-X-MAP:URI="P544849304_Anull_video_gr240_sdr_768x768-.mp4",BYTERANGE="896@0"
+ #EXTINF:2.83333,
+ #EXT-X-BYTERANGE:622900@896
+ P544849304_Anull_video_gr240_sdr_768x768-.mp4
+ #EXTINF:2.83333,
+ #EXT-X-BYTERANGE:717722@623796
+ P544849304_Anull_video_gr240_sdr_768x768-.mp4
+
+That file, downloaded whole, is a playable fragmented MP4 (about 6 MB at 768x768).
+
+### Low Data Mode
+
+Every request sets `allowsConstrainedNetworkAccess` to `SGFlag(SGKeyMotionLowData, NO)`.
diff --git a/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtwork.h b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtwork.h
new file mode 100644
index 00000000..e3340d3f
--- /dev/null
+++ b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtwork.h
@@ -0,0 +1,36 @@
+// Moving artwork for a track: its Canvas, or Apple Music's animated album cover where the album has one.
+// SGMotionCatalog.m looks covers and logos up in Apple Music's catalog. SGMotionStore.m keeps every video
+// as a local file, the form the system's lock screen takes.
+#import <UIKit/UIKit.h>
+
+// Off until switched on.
+#define SGKeyLockScreenMotion @"spotifyglass.lockscreen.motion"
+// Off until switched on: download in Low Data Mode too.
+#define SGKeyMotionLowData @"spotifyglass.motion.lowdata"
+
+typedef NS_ENUM(NSInteger, SGMotionShape) {
+ SGMotionSquare, // 1:1
+ SGMotionTall, // 3:4
+};
+
+// Apple Music's animated cover for an album, as a local file, or nil. `done` runs on the main queue.
+// `pixels` is the width it is shown at; the closest stream at or above it is downloaded.
+void SGMotionAlbumCover(NSString *artist, NSString *album, SGMotionShape shape, CGFloat pixels,
+ void (^done)(NSURL *file));
+
+// The artist's logo from Apple Music, a transparent PNG `pixels` wide, or nil. Main queue.
+void SGMotionArtistLogo(NSString *artist, CGFloat pixels, void (^done)(UIImage *logo));
+
+// Any remote video (a Canvas) as a local file, or nil. Main queue.
+void SGMotionFile(NSURL *remote, void (^done)(NSURL *file));
+
+// The first frame of a local video. Main queue.
+void SGMotionPoster(NSURL *file, void (^done)(UIImage *poster));
+
+// Settings: the lock screen switch and Low Data Mode.
+NSArray *SGLockScreenMotionRows(void);
+
+// Pure steps, for the harness.
+NSString *SGMotionNameKey(NSString *name);
+NSString *SGMotionStreamIn(NSString *master, CGFloat pixels);
+NSString *SGMotionWholeFileIn(NSString *media);
diff --git a/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.m b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.m
new file mode 100644
index 00000000..10994bc4
--- /dev/null
+++ b/tweak/Sources/Shared/AnimatedArtwork/AnimatedArtworkSettings.m
@@ -0,0 +1,14 @@
+// The lock screen's moving artwork and whether it downloads in Low Data Mode. iOS 26 and up only.
+#import "Core/SGCore.h"
+#import "Settings/SGModPage.h"
+#import "AnimatedArtwork.h"
+
+NSArray *SGLockScreenMotionRows(void) {
+ if (@available(iOS 26.0, *)) {
+ return @[
+ SGOptionRow(@"Moving artwork", @"The track's Canvas, else Apple Music's animated cover", SGKeyLockScreenMotion),
+ SGOptionRow(@"Download in Low Data Mode", @"About 2 to 6 MB a song", SGKeyMotionLowData),
+ ];
+ }
+ return @[];
+}
diff --git a/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.x b/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.x
new file mode 100644
index 00000000..84c076ec
--- /dev/null
+++ b/tweak/Sources/Shared/AnimatedArtwork/LockScreenMotion.x
@@ -0,0 +1,117 @@
+// The lock screen's artwork moves: the track's Canvas, else Apple Music's animated album cover. iOS 26
+// takes a local video through MPMediaItemAnimatedArtwork under one of two now playing keys, 1:1 or 3:4,
+// picked here by the video's own shape.
+//
+// Spotify's now playing info passes through with the artwork added. Lock screen lyrics hooks the same
+// setter and keeps Spotify's own dictionary behind the getter, so this hook stores nothing of it: once
+// a video is ready, the info the getter gives back is sent again with its clock moved on.
+#import <MediaPlayer/MediaPlayer.h>
+#import "Core/SGCore.h"
+#import "AnimatedArtwork.h"
+#import "Shared/Player/PlayerState.h"
+
+// Read on whatever thread Spotify sets the info from, written on the main one.
+static NSObject *sg_lock;
+static NSString *sg_title;
+static NSDictionary *sg_motion;
+static double sg_elapsed, sg_rate;
+static CFAbsoluteTime sg_clockAt;
+
+static NSString *sg_track; // main thread: the URI the artwork is for
+
+API_AVAILABLE(ios(26.0))
+static void resend(void) {
+ MPNowPlayingInfoCenter *center = MPNowPlayingInfoCenter.defaultCenter;
+ NSMutableDictionary *info = [center.nowPlayingInfo mutableCopy];
+ if (!info) return;
+ @synchronized (sg_lock) {
+ if (sg_clockAt > 0) info[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @(sg_elapsed + sg_rate * (CFAbsoluteTimeGetCurrent() - sg_clockAt));
+ }
+ center.nowPlayingInfo = info;
+}
+
+API_AVAILABLE(ios(26.0))
+static void show(NSString *uri, NSURL *file) {
+ if (!file || ![uri isEqualToString:sg_track]) return;
+ SGMotionPoster(file, ^(UIImage *poster) {
+ if (!poster || ![uri isEqualToString:sg_track]) return;
+ CGSize size = poster.size;
+ BOOL square = fabs(size.width - size.height) < size.height * 0.05;
+ NSString *key = square ? MPNowPlayingInfoProperty1x1AnimatedArtwork : MPNowPlayingInfoProperty3x4AnimatedArtwork;
+ MPMediaItemAnimatedArtwork *artwork = [[MPMediaItemAnimatedArtwork alloc] initWithArtworkID:file.lastPathComponent
+ previewImageRequestHandler:^(CGSize wanted, void (^completion)(UIImage *)) { completion(poster); }
+ videoAssetFileURLRequestHandler:^(CGSize wanted, void (^completion)(NSURL *)) { completion(file); }];
+ @synchronized (sg_lock) {
+ sg_motion = @{key: artwork};
+ }
+ SGLog(@"lock motion: %@ as %@", file.lastPathComponent, square ? @"1:1" : @"3:4");
+ resend();
+ });
+}
+
+@interface SGLockScreenMotion : NSObject <SGPlayerStateObserver>
+@end
+
+@implementation SGLockScreenMotion
+- (void)playerStateDidChange:(SPTPlayerState *)state {
+ if (@available(iOS 26.0, *)) {
+ NSString *uri = SGURIString(state.track.URI);
+ if (!uri || [uri isEqualToString:sg_track]) return;
+ sg_track = uri;
+ @synchronized (sg_lock) {
+ sg_title = state.track.trackTitle;
+ sg_motion = nil;
+ }
+ NSDictionary *metadata = [state.track.metadata isKindOfClass:NSDictionary.class] ? state.track.metadata : nil;
+ NSString *artist = state.track.artistName, *album = metadata[@"album_title"];
+ id type = metadata[@"canvas.type"], address = metadata[@"canvas.url"];
+ BOOL video = [type isKindOfClass:NSString.class] && [type rangeOfString:@"video" options:NSCaseInsensitiveSearch].location != NSNotFound;
+ NSURL *canvas = video && [address isKindOfClass:NSString.class] ? [NSURL URLWithString:address] : nil;
+ CGFloat pixels = UIScreen.mainScreen.nativeBounds.size.width;
+ void (^apple)(void) = ^{
+ SGMotionAlbumCover(artist, album, SGMotionTall, pixels, ^(NSURL *file) { show(uri, file); });
+ };
+ if (!canvas) {
+ apple();
+ return;
+ }
+ SGMotionFile(canvas, ^(NSURL *file) {
+ if (file) show(uri, file);
+ else apple();
+ });
+ }
+}
+@end
+
+%hook MPNowPlayingInfoCenter
+- (void)setNowPlayingInfo:(NSDictionary *)info {
+ NSDictionary *motion = nil;
+ @synchronized (sg_lock) {
+ if (info[MPNowPlayingInfoPropertyElapsedPlaybackTime]) {
+ sg_elapsed = [info[MPNowPlayingInfoPropertyElapsedPlaybackTime] doubleValue];
+ sg_rate = [info[MPNowPlayingInfoPropertyPlaybackRate] doubleValue];
+ sg_clockAt = CFAbsoluteTimeGetCurrent();
+ }
+ if (sg_motion && [info[MPMediaItemPropertyTitle] isEqual:sg_title]) motion = sg_motion;
+ }
+ if (!motion) {
+ %orig;
+ return;
+ }
+ NSMutableDictionary *withMotion = [info mutableCopy];
+ [withMotion addEntriesFromDictionary:motion];
+ %orig(withMotion);
+}
+%end
+
+%ctor {
+ if (!SGFlag(SGKeyLockScreenMotion, NO)) return;
+ if (@available(iOS 26.0, *)) {
+ sg_lock = [NSObject new];
+ %init;
+ static SGLockScreenMotion *observer;
+ observer = [SGLockScreenMotion new];
+ SGAddPlayerStateObserver(observer);
+ SGLog(@"lock motion: on, the lock screen takes %@", MPNowPlayingInfoCenter.supportedAnimatedArtworkKeys);
+ }
+}
diff --git a/tweak/Sources/Shared/AnimatedArtwork/SGMotionCatalog.m b/tweak/Sources/Shared/AnimatedArtwork/SGMotionCatalog.m
new file mode 100644
index 00000000..470a22e3
--- /dev/null
+++ b/tweak/Sources/Shared/AnimatedArtwork/SGMotionCatalog.m
@@ -0,0 +1,397 @@
+// Animated album covers and artist logos from Apple Music's catalog. The catalog takes the developer
+// token Apple Music's web player carries, so the token is read out of the web player's script and
+// kept until it is about to expire. Answers are kept for the launch.
+#import "Core/SGCore.h"
+#import "AnimatedArtwork.h"
+
+static NSString *const kTokenKey = @"spotifyglass.motion.token";
+static NSString *const kBrowse = @"https://music.apple.com/us/browse";
+static NSString *const kSearch = @"https://amp-api.music.apple.com/v1/catalog/us/search";
+static NSString *const kSafari = @"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15";
+// A token this close to its expiry is read again rather than used.
+static const NSTimeInterval kTokenMargin = 3600;
+
+// Main queue only. NSNull is kept for an album or artist that has nothing.
+static NSMutableDictionary<NSString *, id> *sg_kept;
+static NSMutableDictionary<NSString *, NSMutableArray *> *sg_waiting;
+static NSMutableArray<void (^)(NSString *)> *sg_tokenWaiting;
+
+static void setUp(void) {
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{
+ sg_kept = [NSMutableDictionary dictionary];
+ sg_waiting = [NSMutableDictionary dictionary];
+ sg_tokenWaiting = [NSMutableArray array];
+ });
+}
+
+static id dig(id value, NSString *path) {
+ for (NSString *key in [path componentsSeparatedByString:@"/"]) {
+ if (![value isKindOfClass:NSDictionary.class]) return nil;
+ value = value[key];
+ }
+ return value;
+}
+
+static NSString *textOf(NSData *data) {
+ return data ? [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : nil;
+}
+
+#pragma mark - names
+
+static NSString *plain(NSString *name) {
+ NSString *key = [name stringByFoldingWithOptions:NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch
+ locale:nil].lowercaseString;
+ // Punctuation goes without leaving a gap, so "n’" and "n'" both become "n".
+ key = [key stringByReplacingOccurrencesOfString:@"[^\\p{L}\\p{N}\\s]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, key.length)];
+ key = [key stringByReplacingOccurrencesOfString:@"\\s+" withString:@" " options:NSRegularExpressionSearch range:NSMakeRange(0, key.length)];
+ return [key stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
+}
+
+NSString *SGMotionNameKey(NSString *name) {
+ if (![name isKindOfClass:NSString.class]) return @"";
+ // Apple Music adds " - Single" and " - EP" to names Spotify gives bare.
+ NSRange dash = [name rangeOfString:@" - "];
+ if (dash.location != NSNotFound) name = [name substringToIndex:dash.location];
+ NSString *edition = @"\\([^)]*\\)|\\[[^\\]]*\\]";
+ NSString *bare = plain([name stringByReplacingOccurrencesOfString:edition withString:@" " options:NSRegularExpressionSearch
+ range:NSMakeRange(0, name.length)]);
+ // A name that is all brackets keeps them, so it still has a key.
+ return bare.length ? bare : plain(name);
+}
+
+// Apple Music may credit several artists where Spotify names the lead, so either name may hold the
+// other as a run of whole words.
+static BOOL sameArtist(NSString *a, NSString *b) {
+ if (!a.length || !b.length) return NO;
+ NSString *x = [NSString stringWithFormat:@" %@ ", a], *y = [NSString stringWithFormat:@" %@ ", b];
+ return [x containsString:y] || [y containsString:x];
+}
+
+#pragma mark - playlists
+
+static NSArray<NSString *> *linesOf(NSString *playlist) {
+ NSMutableArray<NSString *> *lines = [NSMutableArray array];
+ for (NSString *line in [playlist componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet]) {
+ NSString *trimmed = [line stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
+ if (trimmed.length) [lines addObject:trimmed];
+ }
+ return lines;
+}
+
+static NSString *firstGroup(NSString *pattern, NSString *text) {
+ NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
+ NSTextCheckingResult *match = [regex firstMatchInString:text options:0 range:NSMakeRange(0, text.length)];
+ return match ? [text substringWithRange:[match rangeAtIndex:1]] : nil;
+}
+
+NSString *SGMotionStreamIn(NSString *master, CGFloat pixels) {
+ if (![master isKindOfClass:NSString.class]) return nil;
+ NSArray<NSString *> *lines = linesOf(master);
+ NSString *best = nil;
+ NSInteger bestWidth = 0;
+ BOOL bestHEVC = NO;
+ for (NSUInteger i = 0; i + 1 < lines.count; i++) {
+ if (![lines[i] hasPrefix:@"#EXT-X-STREAM-INF:"] || [lines[i + 1] hasPrefix:@"#"]) continue;
+ NSInteger width = [firstGroup(@"RESOLUTION=(\\d+)x\\d+", lines[i]) integerValue];
+ if (width <= 0) continue;
+ NSString *codecs = firstGroup(@"CODECS=\"([^\"]*)\"", lines[i]);
+ BOOL hevc = [codecs hasPrefix:@"hvc1"] || [codecs hasPrefix:@"hev1"];
+ BOOL wide = width >= pixels, bestWide = bestWidth >= pixels;
+ BOOL better = !best || (wide != bestWide ? wide
+ : width != bestWidth ? (wide ? width < bestWidth : width > bestWidth)
+ : hevc && !bestHEVC);
+ if (!better) continue;
+ best = lines[i + 1];
+ bestWidth = width;
+ bestHEVC = hevc;
+ }
+ return best;
+}
+
+NSString *SGMotionWholeFileIn(NSString *media) {
+ if (![media isKindOfClass:NSString.class]) return nil;
+ NSMutableSet<NSString *> *uris = [NSMutableSet set];
+ for (NSString *line in linesOf(media)) {
+ NSString *uri = [line hasPrefix:@"#"] ? firstGroup(@"URI=\"([^\"]*)\"", line) : line;
+ if (uri) [uris addObject:uri];
+ }
+ return uris.count == 1 ? uris.anyObject : nil;
+}
+
+#pragma mark - requests
+
+static NSMutableURLRequest *requestFor(NSURL *url) {
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
+ request.allowsConstrainedNetworkAccess = SGFlag(SGKeyMotionLowData, NO);
+ if ([url.host isEqualToString:@"music.apple.com"]) [request setValue:kSafari forHTTPHeaderField:@"User-Agent"];
+ return request;
+}
+
+// `parse` runs off the main queue on a 200 answer. `done` gets what it made and the status, 0 for no
+// answer at all, on the main queue.
+static void fetch(NSURLRequest *request, id (^parse)(NSData *data), void (^done)(id result, NSInteger status)) {
+ if (!request.URL) {
+ dispatch_async(dispatch_get_main_queue(), ^{ done(nil, 0); });
+ return;
+ }
+ [[NSURLSession.sharedSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
+ NSInteger status = !error && [response isKindOfClass:NSHTTPURLResponse.class] ? ((NSHTTPURLResponse *)response).statusCode : 0;
+ id result = status == 200 && data ? parse(data) : nil;
+ if (status != 200) SGLog(@"motion: %@%@ failed (%ld, %@)", request.URL.host, request.URL.path, (long)status, error.localizedDescription);
+ dispatch_async(dispatch_get_main_queue(), ^{ done(result, status); });
+ }] resume];
+}
+
+#pragma mark - token
+
+static NSDictionary *claimsOf(NSString *jwt) {
+ NSArray<NSString *> *parts = [jwt isKindOfClass:NSString.class] ? [jwt componentsSeparatedByString:@"."] : nil;
+ if (parts.count != 3) return nil;
+ NSMutableString *payload = [[[parts[1] stringByReplacingOccurrencesOfString:@"-" withString:@"+"]
+ stringByReplacingOccurrencesOfString:@"_" withString:@"/"] mutableCopy];
+ while (payload.length % 4) [payload appendString:@"="];
+ NSData *data = [[NSData alloc] initWithBase64EncodedString:payload options:0];
+ id claims = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
+ return [claims isKindOfClass:NSDictionary.class] ? claims : nil;
+}
+
+static BOOL usable(NSString *jwt) {
+ id expiry = claimsOf(jwt)[@"exp"];
+ return [expiry isKindOfClass:NSNumber.class] && [expiry doubleValue] - NSDate.date.timeIntervalSince1970 > kTokenMargin;
+}
+
+// The script holds several JWTs; the web player's own is the one the catalog takes.
+static NSString *tokenIn(NSData *script) {
+ NSString *text = textOf(script);
+ if (!text) return nil;
+ NSRegularExpression *jwt = [NSRegularExpression regularExpressionWithPattern:@"eyJ[A-Za-z0-9_-]+\\.eyJ[A-Za-z0-9_-]+\\.[A-Za-z0-9_-]+" options:0 error:nil];
+ for (NSTextCheckingResult *match in [jwt matchesInString:text options:0 range:NSMakeRange(0, text.length)]) {
+ NSString *candidate = [text substringWithRange:match.range];
+ if ([claimsOf(candidate)[@"iss"] isEqual:@"AMPWebPlay"]) return candidate;
+ }
+ return nil;
+}
+
+// The script's name carries a hash that changes with every web player release, so the page names it.
+static void readToken(void (^done)(NSString *token)) {
+ NSURL *browse = [NSURL URLWithString:kBrowse];
+ fetch(requestFor(browse), ^id(NSData *data) {
+ NSString *html = textOf(data);
+ if (!html) return nil;
+ NSRange path = [html rangeOfString:@"/assets/index[^\"'\\s>]*\\.js" options:NSRegularExpressionSearch];
+ return path.location == NSNotFound ? nil : [NSURL URLWithString:[html substringWithRange:path] relativeToURL:browse].absoluteURL;
+ }, ^(NSURL *script, NSInteger status) {
+ if (!script) {
+ SGLog(@"motion: no web player script on the browse page (%ld)", (long)status);
+ done(nil);
+ return;
+ }
+ fetch(requestFor(script), ^id(NSData *data) { return tokenIn(data); }, ^(NSString *token, NSInteger scriptStatus) {
+ SGLog(@"motion: token %@ in %@", token ? @"found" : @"not found", script.lastPathComponent);
+ done(token);
+ });
+ });
+}
+
+static void withToken(void (^use)(NSString *token)) {
+ NSString *stored = [NSUserDefaults.standardUserDefaults stringForKey:kTokenKey];
+ if (usable(stored)) {
+ use(stored);
+ return;
+ }
+ [sg_tokenWaiting addObject:[use copy]];
+ if (sg_tokenWaiting.count > 1) return;
+ readToken(^(NSString *token) {
+ if (token) [NSUserDefaults.standardUserDefaults setObject:token forKey:kTokenKey];
+ NSArray<void (^)(NSString *)> *waiting = [sg_tokenWaiting copy];
+ [sg_tokenWaiting removeAllObjects];
+ for (void (^waiter)(NSString *) in waiting) waiter(token);
+ });
+}
+
+// Another request may have read a new token since this one was sent; that one stays.
+static void forgetToken(NSString *token) {
+ if ([[NSUserDefaults.standardUserDefaults stringForKey:kTokenKey] isEqualToString:token])
+ [NSUserDefaults.standardUserDefaults removeObjectForKey:kTokenKey];
+}
+
+#pragma mark - catalog
+
+static NSString *escaped(NSString *value) {
+ NSCharacterSet *unreserved = [NSCharacterSet characterSetWithCharactersInString:
+ @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"];
+ return [value stringByAddingPercentEncodingWithAllowedCharacters:unreserved] ?: @"";
+}
+
+// `found` is the search's results of that type, empty when it has none; `answered` is NO when the
+// catalog could not be asked, so the caller does not remember the absence.
+static void search(NSString *type, NSString *term, NSString *extend, BOOL renew, void (^done)(NSArray *found, BOOL answered)) {
+ withToken(^(NSString *token) {
+ if (!token) {
+ done(nil, NO);
+ return;
+ }
+ NSString *address = [NSString stringWithFormat:@"%@?term=%@&types=%@&limit=10&extend=%@", kSearch, escaped(term), type, extend];
+ NSMutableURLRequest *request = requestFor([NSURL URLWithString:address]);
+ [request setValue:[@"Bearer " stringByAppendingString:token] forHTTPHeaderField:@"Authorization"];
+ // The catalog refuses the web player's token without the web player's origin.
+ [request setValue:@"https://music.apple.com" forHTTPHeaderField:@"Origin"];
+ fetch(request, ^id(NSData *data) {
+ id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
+ id found = dig(json, [NSString stringWithFormat:@"results/%@/data", type]);
+ return [found isKindOfClass:NSArray.class] ? found : @[];
+ }, ^(NSArray *found, NSInteger status) {
+ if (status == 401 && renew) {
+ SGLog(@"motion: token refused, reading it again");
+ forgetToken(token);
+ search(type, term, extend, NO, done);
+ return;
+ }
+ done(found, status == 200);
+ });
+ });
+}
+
+// Runs `look` once per key and launch. `look` calls `answer` exactly once; `keep` is NO for a failure
+// worth asking again.
+static void lookUp(NSString *key, void (^done)(id value), void (^look)(void (^answer)(id value, BOOL keep))) {
+ setUp();
+ id kept = sg_kept[key];
+ if (kept) {
+ done(kept == NSNull.null ? nil : kept);
+ return;
+ }
+ NSMutableArray *waiting = sg_waiting[key];
+ if (waiting) {
+ [waiting addObject:[done copy]];
+ return;
+ }
+ sg_waiting[key] = [NSMutableArray arrayWithObject:[done copy]];
+ look(^(id value, BOOL keep) {
+ if (keep) sg_kept[key] = value ?: NSNull.null;
+ NSArray *callers = sg_waiting[key];
+ [sg_waiting removeObjectForKey:key];
+ for (void (^caller)(id) in callers) caller(value);
+ });
+}
+
+#pragma mark - album covers
+
+static NSString *videoOf(NSDictionary *editorialVideo, SGMotionShape shape) {
+ NSArray<NSString *> *names = shape == SGMotionTall
+ ? @[@"motionTallVideo3x4", @"motionDetailTall", @"motionSquareVideo1x1", @"motionDetailSquare"]
+ : @[@"motionSquareVideo1x1", @"motionDetailSquare"];
+ for (NSString *name in names) {
+ id video = dig(editorialVideo, [name stringByAppendingString:@"/video"]);
+ if ([video isKindOfClass:NSString.class] && [video length]) return video;
+ }
+ return nil;
+}
+
+// The master playlist names a stream per size and codec; the chosen stream's playlist names one file.
+static void wholeFileOf(NSURL *master, CGFloat pixels, void (^answer)(id file, BOOL keep)) {
+ fetch(requestFor(master), ^id(NSData *data) {
+ NSString *uri = SGMotionStreamIn(textOf(data), pixels);
+ return uri ? [NSURL URLWithString:uri relativeToURL:master].absoluteURL : nil;
+ }, ^(NSURL *stream, NSInteger status) {
+ if (!stream) {
+ answer(nil, status == 200);
+ return;
+ }
+ fetch(requestFor(stream), ^id(NSData *data) {
+ NSString *uri = SGMotionWholeFileIn(textOf(data));
+ return uri ? [NSURL URLWithString:uri relativeToURL:stream].absoluteURL : nil;
+ }, ^(NSURL *file, NSInteger streamStatus) {
+ answer(file, streamStatus == 200);
+ });
+ });
+}
+
+void SGMotionAlbumCover(NSString *artist, NSString *album, SGMotionShape shape, CGFloat pixels, void (^done)(NSURL *file)) {
+ if (!NSThread.isMainThread) {
+ dispatch_async(dispatch_get_main_queue(), ^{ SGMotionAlbumCover(artist, album, shape, pixels, done); });
+ return;
+ }
+ NSString *artistKey = SGMotionNameKey(artist), *albumKey = SGMotionNameKey(album);
+ if (!artistKey.length || !albumKey.length) {
+ done(nil);
+ return;
+ }
+ NSString *key = [NSString stringWithFormat:@"album\n%ld\n%@\n%@", (long)shape, artistKey, albumKey];
+ // The remote file is what is kept: the store may drop its local copy later in the launch.
+ lookUp(key, ^(NSURL *remote) { SGMotionFile(remote, done); }, ^(void (^answer)(id, BOOL)) {
+ search(@"albums", [NSString stringWithFormat:@"%@ %@", artist, album], @"editorialVideo", YES, ^(NSArray *found, BOOL answered) {
+ if (!answered) {
+ answer(nil, NO);
+ return;
+ }
+ // Editions share a key (Midnights and Midnights (3am Edition), clean and explicit), and only one of
+ // them may have the motion: the first match that has it.
+ NSString *video = nil;
+ for (id result in found) {
+ id attributes = dig(result, @"attributes");
+ if (!sameArtist(artistKey, SGMotionNameKey(dig(attributes, @"artistName")))) continue;
+ if (![SGMotionNameKey(dig(attributes, @"name")) isEqualToString:albumKey]) continue;
+ video = videoOf(dig(attributes, @"editorialVideo"), shape);
+ if (video) break;
+ }
+ NSURL *master = video ? [NSURL URLWithString:video] : nil;
+ SGLog(@"motion: %@ by %@ has %@", album, artist, master ? @"an animated cover" : @"no animated cover");
+ if (!master) {
+ answer(nil, YES);
+ return;
+ }
+ wholeFileOf(master, pixels, answer);
+ });
+ });
+}
+
+#pragma mark - artist logos
+
+// The URL is a template ending in {w}x{h}bb.jpg; the same image comes as a PNG with its transparency.
+static NSURL *logoURL(id logo, CGFloat pixels) {
+ id url = dig(logo, @"url"), width = dig(logo, @"width"), height = dig(logo, @"height");
+ if (![url isKindOfClass:NSString.class] || ![width isKindOfClass:NSNumber.class] || ![height isKindOfClass:NSNumber.class]) return nil;
+ if ([width doubleValue] <= 0 || [height doubleValue] <= 0) return nil;
+ long w = MAX(1, lround(pixels)), h = MAX(1, lround(pixels * [height doubleValue] / [width doubleValue]));
+ NSString *filled = [[url stringByReplacingOccurrencesOfString:@"{w}" withString:@(w).stringValue]
+ stringByReplacingOccurrencesOfString:@"{h}" withString:@(h).stringValue];
+ if ([filled hasSuffix:@".jpg"]) filled = [[filled substringToIndex:filled.length - 4] stringByAppendingString:@".png"];
+ return [NSURL URLWithString:filled];
+}
+
+void SGMotionArtistLogo(NSString *artist, CGFloat pixels, void (^done)(UIImage *logo)) {
+ if (!NSThread.isMainThread) {
+ dispatch_async(dispatch_get_main_queue(), ^{ SGMotionArtistLogo(artist, pixels, done); });
+ return;
+ }
+ NSString *artistKey = SGMotionNameKey(artist);
+ if (!artistKey.length) {
+ done(nil);
+ return;
+ }
+ lookUp([@"logo\n" stringByAppendingString:artistKey], ^(UIImage *logo) { done(logo); }, ^(void (^answer)(id, BOOL)) {
+ search(@"artists", artist, @"editorialArtwork", YES, ^(NSArray *found, BOOL answered) {
+ if (!answered) {
+ answer(nil, NO);
+ return;
+ }
+ NSURL *url = nil;
+ for (id result in found) {
+ id attributes = dig(result, @"attributes");
+ if (![SGMotionNameKey(dig(attributes, @"name")) isEqualToString:artistKey]) continue;
+ url = logoURL(dig(attributes, @"editorialArtwork/musicContentColorLogoTrimmed"), pixels);
+ break;
+ }
+ SGLog(@"motion: %@ has %@", artist, url ? @"a logo" : @"no logo");
+ if (!url) {
+ answer(nil, YES);
+ return;
+ }
+ fetch(requestFor(url), ^id(NSData *data) { return [UIImage imageWithData:data]; }, ^(UIImage *logo, NSInteger status) {
+ answer(logo, status == 200);
+ });
+ });
+ });
+}
diff --git a/tweak/Sources/Shared/AnimatedArtwork/SGMotionStore.m b/tweak/Sources/Shared/AnimatedArtwork/SGMotionStore.m
new file mode 100644
index 00000000..54440895
--- /dev/null
+++ b/tweak/Sources/Shared/AnimatedArtwork/SGMotionStore.m
@@ -0,0 +1,81 @@
+// Remote videos kept as local files in Caches/Vitrine/Motion, named by a hash of their address. A video
+// asked for again while it downloads joins the download in flight. The folder keeps the newest kFiles.
+#import <AVFoundation/AVFoundation.h>
+#import <CommonCrypto/CommonDigest.h>
+#import "Core/SGCore.h"
+#import "AnimatedArtwork.h"
+
+static const NSUInteger kFiles = 40;
+
+static NSURL *folder(void) {
+ NSURL *caches = [NSFileManager.defaultManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask].firstObject;
+ NSURL *url = [caches URLByAppendingPathComponent:@"Vitrine/Motion" isDirectory:YES];
+ [NSFileManager.defaultManager createDirectoryAtURL:url withIntermediateDirectories:YES attributes:nil error:nil];
+ return url;
+}
+
+static NSURL *localFor(NSURL *remote) {
+ NSData *address = [remote.absoluteString dataUsingEncoding:NSUTF8StringEncoding];
+ unsigned char digest[CC_SHA1_DIGEST_LENGTH];
+ CC_SHA1(address.bytes, (CC_LONG)address.length, digest);
+ NSMutableString *name = [NSMutableString string];
+ for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) [name appendFormat:@"%02x", digest[i]];
+ return [folder() URLByAppendingPathComponent:[name stringByAppendingPathExtension:@"mp4"]];
+}
+
+static void trim(void) {
+ NSArray<NSURLResourceKey> *keys = @[NSURLContentModificationDateKey];
+ NSArray<NSURL *> *files = [NSFileManager.defaultManager contentsOfDirectoryAtURL:folder() includingPropertiesForKeys:keys
+ options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
+ if (files.count <= kFiles) return;
+ NSArray<NSURL *> *oldestFirst = [files sortedArrayUsingComparator:^NSComparisonResult(NSURL *a, NSURL *b) {
+ NSDate *left = nil, *right = nil;
+ [a getResourceValue:&left forKey:NSURLContentModificationDateKey error:nil];
+ [b getResourceValue:&right forKey:NSURLContentModificationDateKey error:nil];
+ return [left compare:right];
+ }];
+ for (NSUInteger i = 0; i + kFiles < oldestFirst.count; i++) [NSFileManager.defaultManager removeItemAtURL:oldestFirst[i] error:nil];
+}
+
+static NSMutableDictionary<NSURL *, NSMutableArray *> *sg_waiting;
+
+void SGMotionFile(NSURL *remote, void (^done)(NSURL *file)) {
+ if (!remote) {
+ done(nil);
+ return;
+ }
+ NSURL *local = localFor(remote);
+ if ([NSFileManager.defaultManager fileExistsAtPath:local.path]) {
+ done(local);
+ return;
+ }
+ if (!sg_waiting) sg_waiting = [NSMutableDictionary dictionary];
+ if (sg_waiting[remote]) {
+ [sg_waiting[remote] addObject:[done copy]];
+ return;
+ }
+ sg_waiting[remote] = [NSMutableArray arrayWithObject:[done copy]];
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:remote];
+ request.allowsConstrainedNetworkAccess = SGFlag(SGKeyMotionLowData, NO);
+ [[NSURLSession.sharedSession downloadTaskWithRequest:request completionHandler:^(NSURL *temporary, NSURLResponse *response, NSError *error) {
+ NSInteger status = [response isKindOfClass:NSHTTPURLResponse.class] ? ((NSHTTPURLResponse *)response).statusCode : 0;
+ // The temporary file is gone once this handler returns, so it moves before the hop to main.
+ BOOL kept = temporary && status == 200 && [NSFileManager.defaultManager moveItemAtURL:temporary toURL:local error:nil];
+ if (kept) trim();
+ else SGLog(@"motion: %@ not downloaded (%ld, %@)", remote.lastPathComponent, (long)status, error.localizedDescription);
+ dispatch_async(dispatch_get_main_queue(), ^{
+ NSArray *waiting = sg_waiting[remote];
+ [sg_waiting removeObjectForKey:remote];
+ for (void (^waiter)(NSURL *) in waiting) waiter(kept ? local : nil);
+ });
+ }] resume];
+}
+
+void SGMotionPoster(NSURL *file, void (^done)(UIImage *poster)) {
+ AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:[AVURLAsset assetWithURL:file]];
+ generator.appliesPreferredTrackTransform = YES;
+ [generator generateCGImageAsynchronouslyForTime:kCMTimeZero completionHandler:^(CGImageRef image, CMTime actual, NSError *error) {
+ UIImage *poster = image ? [UIImage imageWithCGImage:image] : nil;
+ dispatch_async(dispatch_get_main_queue(), ^{ done(poster); });
+ }];
+}
diff --git a/tweak/Sources/Shared/Player/PlayerSettings.m b/tweak/Sources/Shared/Player/PlayerSettings.m
index 729ec332..7405d559 100644
--- a/tweak/Sources/Shared/Player/PlayerSettings.m
+++ b/tweak/Sources/Shared/Player/PlayerSettings.m
@@ -2,9 +2,12 @@
#import "Core/SGCore.h"
#import "Settings/SGModPage.h"
#import "PlayerSettings.h"
+#import "Shared/AnimatedArtwork/AnimatedArtwork.h"
UIViewController *SGLockScreenWidgetPage(void) {
- return [[SGModPage alloc] initWithTitle:@"Lock screen widget" intro:SGRestartNote sections:@[
+ NSArray *motion = SGLockScreenMotionRows();
+ NSArray<SGModSection *> *mod = motion.count ? @[SGSection(@"Moving artwork", motion)] : @[];
+ return [[SGModPage alloc] initWithTitle:@"Lock screen widget" intro:SGRestartNote sections:[mod arrayByAddingObjectsFromArray:@[
SGSection(@"Controls", @[
SGFlagRow(@"Like and dislike buttons", @"ios-feature-lockscreen.like_dislike_enabled"),
SGFlagRow(@"Skip button on podcasts", @"ios-feature-lockscreen.skip_button_on_podcasts"),
@@ -16,5 +19,5 @@ UIViewController *SGLockScreenWidgetPage(void) {
SGFlagRow(@"Video artwork", @"ios-feature-lockscreen.vit_artwork_enabled"),
SGFlagRow(@"Companion content", @"ios-feature-lockscreen.companion_content_enabled"),
]),
- ] footer:nil];
+ ]] footer:nil];
}