-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathPlayableContentFeeder.java
More file actions
361 lines (305 loc) · 15.2 KB
/
PlayableContentFeeder.java
File metadata and controls
361 lines (305 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Copyright 2021 devgianlu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.gianlu.librespot.audio;
import com.google.protobuf.ByteString;
import com.spotify.metadata.Metadata;
import com.spotify.storage.StorageResolve.StorageResolveResponse;
import okhttp3.HttpUrl;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.gianlu.librespot.audio.cdn.CdnFeedHelper;
import xyz.gianlu.librespot.audio.cdn.CdnManager;
import xyz.gianlu.librespot.audio.format.AudioQualityPicker;
import xyz.gianlu.librespot.audio.format.SuperAudioFormat;
import xyz.gianlu.librespot.audio.storage.AudioFileFetch;
import xyz.gianlu.librespot.audio.storage.StorageFeedHelper;
import xyz.gianlu.librespot.common.NameThreadFactory;
import xyz.gianlu.librespot.common.Utils;
import xyz.gianlu.librespot.core.Session;
import xyz.gianlu.librespot.core.TokenProvider;
import xyz.gianlu.librespot.metadata.EpisodeId;
import xyz.gianlu.librespot.metadata.LocalId;
import xyz.gianlu.librespot.metadata.PlayableId;
import xyz.gianlu.librespot.metadata.TrackId;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static xyz.gianlu.librespot.audio.storage.ChannelManager.CHUNK_SIZE;
/**
* @author Gianlu
*/
public final class PlayableContentFeeder {
private static final Logger LOGGER = LoggerFactory.getLogger(PlayableContentFeeder.class);
private static final String STORAGE_RESOLVE_INTERACTIVE = "/storage-resolve/files/audio/interactive/%s";
private static final String STORAGE_RESOLVE_INTERACTIVE_PREFETCH = "/storage-resolve/files/audio/interactive_prefetch/%s";
protected final Session session;
public PlayableContentFeeder(@NotNull Session session) {
this.session = session;
}
@Nullable
private static Metadata.Track pickAlternativeIfNecessary(@NotNull Metadata.Track track) {
if (track.getFileCount() > 0) return track;
for (Metadata.Track alt : track.getAlternativeList()) {
if (alt.getFileCount() > 0) {
Metadata.Track.Builder builder = track.toBuilder();
builder.clearFile();
builder.addAllFile(alt.getFileList());
return builder.build();
}
}
return null;
}
@NotNull
public final LoadedStream load(@NotNull PlayableId id, @NotNull AudioQualityPicker audioQualityPicker, boolean preload, @Nullable HaltListener haltListener) throws CdnManager.CdnException, ContentRestrictedException, TokenProvider.TokenException, IOException {
if (id instanceof TrackId)
return loadTrack((TrackId) id, audioQualityPicker, preload, haltListener);
else if (id instanceof EpisodeId)
return loadEpisode((EpisodeId) id, audioQualityPicker, preload, haltListener);
else
throw new IllegalArgumentException("Unknown content: " + id);
}
@NotNull
private StorageResolveResponse resolveStorageInteractive(@NotNull ByteString fileId, boolean preload) throws IOException, TokenProvider.TokenException {
try (Response resp = session.api().send("GET", String.format(preload ? STORAGE_RESOLVE_INTERACTIVE_PREFETCH : STORAGE_RESOLVE_INTERACTIVE, Utils.bytesToHex(fileId)), null, null)) {
if (resp.code() != 200) throw new IOException(resp.code() + ": " + resp.message());
ResponseBody body = resp.body();
if (body == null) throw new IOException("Response body is empty!");
return StorageResolveResponse.parseFrom(body.byteStream());
}
}
private @NotNull LoadedStream loadTrack(@NotNull TrackId id, @NotNull AudioQualityPicker audioQualityPicker, boolean preload, @Nullable HaltListener haltListener) throws IOException, TokenProvider.TokenException, ContentRestrictedException, CdnManager.CdnException {
Metadata.Track original = session.api().getMetadata4Track(id);
Metadata.Track track = pickAlternativeIfNecessary(original);
if (track == null) {
String country = session.countryCode();
if (country != null) ContentRestrictedException.checkRestrictions(country, original.getRestrictionList());
LOGGER.error("Couldn't find playable track: " + id.toSpotifyUri());
throw new FeederException();
}
return loadTrack(track, audioQualityPicker, preload, haltListener);
}
@NotNull
@Contract("_, null, null, _, _, _ -> fail")
private LoadedStream loadCdnStream(@NotNull Metadata.AudioFile file, @Nullable Metadata.Track track, @Nullable Metadata.Episode episode, @NotNull String urlStr, boolean preload, @Nullable HaltListener haltListener) throws IOException, CdnManager.CdnException {
if (track == null && episode == null)
throw new IllegalStateException();
HttpUrl url = HttpUrl.get(urlStr);
if (track != null) return CdnFeedHelper.loadTrack(session, track, file, url, preload, haltListener);
else return CdnFeedHelper.loadEpisode(session, episode, file, url, haltListener);
}
@NotNull
@Contract("_, null, null, _, _ -> fail")
private LoadedStream loadStream(@NotNull Metadata.AudioFile file, @Nullable Metadata.Track track, @Nullable Metadata.Episode episode, boolean preload, @Nullable HaltListener haltListener) throws IOException, TokenProvider.TokenException, CdnManager.CdnException {
if (track == null && episode == null)
throw new IllegalStateException();
StorageResolveResponse resp = resolveStorageInteractive(file.getFileId(), preload);
switch (resp.getResult()) {
case CDN:
if (track != null) return CdnFeedHelper.loadTrack(session, track, file, resp, preload, haltListener);
else return CdnFeedHelper.loadEpisode(session, episode, file, resp, haltListener);
case STORAGE:
try {
if (track != null) return StorageFeedHelper.loadTrack(session, track, file, preload, haltListener);
else return StorageFeedHelper.loadEpisode(session, episode, file, preload, haltListener);
} catch (AudioFileFetch.StorageNotAvailable ex) {
LOGGER.info("Storage is not available. Going CDN: " + ex.cdnUrl);
return loadCdnStream(file, track, episode, ex.cdnUrl, preload, haltListener);
}
case RESTRICTED:
throw new IllegalStateException("Content is restricted!");
case UNRECOGNIZED:
throw new IllegalStateException("Content is unrecognized!");
default:
throw new IllegalStateException("Unknown result: " + resp.getResult());
}
}
@NotNull
private LoadedStream loadTrack(@NotNull Metadata.Track track, @NotNull AudioQualityPicker audioQualityPicker, boolean preload, @Nullable HaltListener haltListener) throws IOException, CdnManager.CdnException, TokenProvider.TokenException {
Metadata.AudioFile file = audioQualityPicker.getFile(track.getFileList());
if (file == null) {
LOGGER.error("Couldn't find any suitable audio file, available: {}", Utils.formatsToString(track.getFileList()));
throw new FeederException();
}
return loadStream(file, track, null, preload, haltListener);
}
@NotNull
private LoadedStream loadEpisode(@NotNull EpisodeId id, @NotNull AudioQualityPicker audioQualityPicker, boolean preload, @Nullable HaltListener haltListener) throws IOException, TokenProvider.TokenException, CdnManager.CdnException {
Metadata.Episode episode = session.api().getMetadata4Episode(id);
if (episode.hasExternalUrl()) {
return CdnFeedHelper.loadEpisodeExternal(session, episode, haltListener);
} else {
Metadata.AudioFile file = audioQualityPicker.getFile(episode.getAudioList());
if (file == null) {
LOGGER.error("Couldn't find any suitable audio file, available: {}", Utils.formatsToString(episode.getAudioList()));
throw new FeederException();
}
return loadStream(file, null, episode, preload, haltListener);
}
}
private static class FileAudioStream implements DecodedAudioStream {
private static final Logger LOGGER = LoggerFactory.getLogger(FileAudioStream.class);
private final File file;
private final RandomAccessFile raf;
private final byte[][] buffer;
private final int chunks;
private final int size;
private final boolean[] available;
private final boolean[] requested;
private final ExecutorService executorService = Executors.newCachedThreadPool(new NameThreadFactory((r) -> "file-async-" + r.hashCode()));
FileAudioStream(File file) throws IOException {
this.file = file;
this.raf = new RandomAccessFile(file, "r");
this.size = (int) raf.length();
this.chunks = (size + CHUNK_SIZE - 1) / CHUNK_SIZE;
this.buffer = new byte[chunks][];
this.available = new boolean[chunks];
this.requested = new boolean[chunks];
}
@Override
public @NotNull AbsChunkedInputStream stream() {
return new AbsChunkedInputStream(false) {
@Override
protected byte[][] buffer() {
return buffer;
}
@Override
public int size() {
return size;
}
@Override
protected boolean[] requestedChunks() {
return requested;
}
@Override
protected boolean[] availableChunks() {
return available;
}
@Override
protected int chunks() {
return chunks;
}
@Override
protected void requestChunkFromStream(int index) {
executorService.submit(() -> {
try {
raf.seek((long) index * CHUNK_SIZE);
raf.read(buffer[index]);
notifyChunkAvailable(index);
} catch (IOException ex) {
notifyChunkError(index, new ChunkException(ex));
}
});
}
@Override
public void streamReadHalted(int chunk, long time) {
LOGGER.warn("Not dispatching stream read halted event {chunk: {}}", chunk);
}
@Override
public void streamReadResumed(int chunk, long time) {
LOGGER.warn("Not dispatching stream read resumed event {chunk: {}}", chunk);
}
};
}
@Override
public @NotNull SuperAudioFormat codec() {
return SuperAudioFormat.MP3; // FIXME: Detect codec
}
@Override
public @NotNull String describe() {
return "{file: " + file.getAbsolutePath() + "}";
}
@Override
public int decryptTimeMs() {
return 0;
}
}
public static class LoadedStream {
public final MetadataWrapper metadata;
public final DecodedAudioStream in;
public final NormalizationData normalizationData;
public final Metrics metrics;
public LoadedStream(@NotNull Metadata.Track track, @NotNull DecodedAudioStream in, @Nullable NormalizationData normalizationData, @NotNull Metrics metrics) {
this.metadata = new MetadataWrapper(track, null, null);
this.in = in;
this.normalizationData = normalizationData;
this.metrics = metrics;
}
public LoadedStream(@NotNull Metadata.Episode episode, @NotNull DecodedAudioStream in, @Nullable NormalizationData normalizationData, @NotNull Metrics metrics) {
this.metadata = new MetadataWrapper(null, episode, null);
this.in = in;
this.normalizationData = normalizationData;
this.metrics = metrics;
}
private LoadedStream(@NotNull LocalId id, @NotNull DecodedAudioStream in) {
this.metadata = new MetadataWrapper(null, null, id);
this.in = in;
this.normalizationData = null;
this.metrics = new Metrics(null, false, 0);
}
@NotNull
public static LoadedStream forLocalFile(@NotNull LocalId id, @NotNull File file) throws IOException {
return new LoadedStream(id, new FileAudioStream(file));
}
}
public static class Metrics {
public final String fileId;
public final boolean preloadedAudioKey;
public final int audioKeyTime;
public Metrics(@Nullable ByteString fileId, boolean preloadedAudioKey, int audioKeyTime) {
this.fileId = fileId == null ? null : Utils.bytesToHex(fileId).toLowerCase();
this.preloadedAudioKey = preloadedAudioKey;
this.audioKeyTime = audioKeyTime;
if (preloadedAudioKey && audioKeyTime != -1)
throw new IllegalStateException();
}
}
public static class FeederException extends IOException {
FeederException() {
}
}
public static class ContentRestrictedException extends Exception {
public static void checkRestrictions(@NotNull String country, @NotNull List<Metadata.Restriction> restrictions) throws ContentRestrictedException {
for (Metadata.Restriction restriction : restrictions)
if (isRestricted(country, restriction))
throw new ContentRestrictedException();
}
private static boolean isInList(@NotNull String list, @NotNull String match) {
for (int i = 0; i < list.length(); i += 2)
if (list.substring(i, i + 2).equals(match))
return true;
return false;
}
private static boolean isRestricted(@NotNull String countryCode, @NotNull Metadata.Restriction restriction) {
if (restriction.hasCountriesAllowed()) {
String allowed = restriction.getCountriesAllowed();
if (allowed.isEmpty()) return true;
if (!isInList(restriction.getCountriesForbidden(), countryCode))
return true;
}
if (restriction.hasCountriesForbidden())
return isInList(restriction.getCountriesForbidden(), countryCode);
return false;
}
}
}