Skip to content

Commit 2019af8

Browse files
committed
Refactor PlaybackResolver and fix cacheKeyOf
In commonCacheKeyOf the result of an Objects.hash() was ignored
1 parent 1e076ea commit 2019af8

1 file changed

Lines changed: 74 additions & 103 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/resolver/PlaybackResolver.java

Lines changed: 74 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static org.schabi.newpipe.extractor.stream.AudioStream.UNKNOWN_BITRATE;
44
import static org.schabi.newpipe.extractor.stream.VideoStream.RESOLUTION_UNKNOWN;
5-
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
65
import static org.schabi.newpipe.player.helper.PlayerDataSource.LIVE_STREAM_EDGE_GAP_MILLIS;
76

87
import android.net.Uri;
@@ -88,7 +87,7 @@ private static StringBuilder commonCacheKeyOf(final StreamInfo info,
8887
// cache useless.
8988
if (resolutionOrBitrateUnknown && mediaFormat == null) {
9089
cacheKey.append(" ");
91-
Objects.hash(stream.getContent(), stream.getManifestUrl());
90+
cacheKey.append(Objects.hash(stream.getContent(), stream.getManifestUrl()));
9291
}
9392

9493
return cacheKey;
@@ -250,154 +249,112 @@ private static ProgressiveMediaSource buildProgressiveMediaSource(
250249
final Stream stream,
251250
final String cacheKey,
252251
final MediaItemTag metadata) throws ResolverException {
253-
final String url = stream.getContent();
254-
255-
if (isNullOrEmpty(url)) {
256-
throw new ResolverException(
257-
"Try to generate a progressive media source from an empty string or from a "
258-
+ "null object");
259-
} else {
260-
return dataSource.getProgressiveMediaSourceFactory().createMediaSource(
261-
new MediaItem.Builder()
262-
.setTag(metadata)
263-
.setUri(Uri.parse(url))
264-
.setCustomCacheKey(cacheKey)
265-
.build());
266-
}
252+
throwResolverExceptionIfUrlNullOrEmpty(stream.getContent());
253+
return dataSource.getProgressiveMediaSourceFactory().createMediaSource(
254+
new MediaItem.Builder()
255+
.setTag(metadata)
256+
.setUri(Uri.parse(stream.getContent()))
257+
.setCustomCacheKey(cacheKey)
258+
.build());
267259
}
268260

269261
private static DashMediaSource buildDashMediaSource(final PlayerDataSource dataSource,
270262
final Stream stream,
271263
final String cacheKey,
272264
final MediaItemTag metadata)
273265
throws ResolverException {
274-
final boolean isUrlStream = stream.isUrl();
275-
if (isUrlStream && isNullOrEmpty(stream.getContent())) {
276-
throw new ResolverException(
277-
"Could not build a DASH media source from an empty or a null URL content");
278-
}
279266

280-
if (isUrlStream) {
267+
if (stream.isUrl()) {
268+
throwResolverExceptionIfUrlNullOrEmpty(stream.getContent());
281269
return dataSource.getDashMediaSourceFactory().createMediaSource(
282270
new MediaItem.Builder()
283271
.setTag(metadata)
284272
.setUri(Uri.parse(stream.getContent()))
285273
.setCustomCacheKey(cacheKey)
286274
.build());
287-
} else {
288-
String baseUrl = stream.getManifestUrl();
289-
if (baseUrl == null) {
290-
baseUrl = "";
291-
}
292-
293-
final Uri uri = Uri.parse(baseUrl);
275+
}
294276

295-
try {
296-
return dataSource.getDashMediaSourceFactory().createMediaSource(
297-
createDashManifest(stream.getContent(), stream),
298-
new MediaItem.Builder()
299-
.setTag(metadata)
300-
.setUri(uri)
301-
.setCustomCacheKey(cacheKey)
302-
.build());
303-
} catch (final IOException e) {
304-
throw new ResolverException(
305-
"Could not create a DASH media source/manifest from the manifest text");
306-
}
277+
try {
278+
return dataSource.getDashMediaSourceFactory().createMediaSource(
279+
createDashManifest(stream.getContent(), stream),
280+
new MediaItem.Builder()
281+
.setTag(metadata)
282+
.setUri(manifestUrlToUri(stream.getManifestUrl()))
283+
.setCustomCacheKey(cacheKey)
284+
.build());
285+
} catch (final IOException e) {
286+
throw new ResolverException(
287+
"Could not create a DASH media source/manifest from the manifest text", e);
307288
}
308289
}
309290

310291
private static DashManifest createDashManifest(final String manifestContent,
311292
final Stream stream) throws IOException {
312-
final ByteArrayInputStream dashManifestInput = new ByteArrayInputStream(
313-
manifestContent.getBytes(StandardCharsets.UTF_8));
314-
String baseUrl = stream.getManifestUrl();
315-
if (baseUrl == null) {
316-
baseUrl = "";
317-
}
318-
319-
return new DashManifestParser().parse(Uri.parse(baseUrl), dashManifestInput);
293+
return new DashManifestParser().parse(manifestUrlToUri(stream.getManifestUrl()),
294+
new ByteArrayInputStream(manifestContent.getBytes(StandardCharsets.UTF_8)));
320295
}
321296

322297
private static HlsMediaSource buildHlsMediaSource(final PlayerDataSource dataSource,
323298
final Stream stream,
324299
final String cacheKey,
325300
final MediaItemTag metadata)
326301
throws ResolverException {
327-
final boolean isUrlStream = stream.isUrl();
328-
if (isUrlStream && isNullOrEmpty(stream.getContent())) {
329-
throw new ResolverException(
330-
"Could not build a HLS media source from an empty or a null URL content");
331-
}
332-
333-
if (isUrlStream) {
302+
if (stream.isUrl()) {
303+
throwResolverExceptionIfUrlNullOrEmpty(stream.getContent());
334304
return dataSource.getHlsMediaSourceFactory(null).createMediaSource(
335305
new MediaItem.Builder()
336306
.setTag(metadata)
337307
.setUri(Uri.parse(stream.getContent()))
338308
.setCustomCacheKey(cacheKey)
339309
.build());
340-
} else {
341-
final NonUriHlsDataSourceFactory.Builder hlsDataSourceFactoryBuilder =
342-
new NonUriHlsDataSourceFactory.Builder();
343-
hlsDataSourceFactoryBuilder.setPlaylistString(stream.getContent());
344-
String manifestUrl = stream.getManifestUrl();
345-
if (manifestUrl == null) {
346-
manifestUrl = "";
347-
}
348-
return dataSource.getHlsMediaSourceFactory(hlsDataSourceFactoryBuilder)
349-
.createMediaSource(new MediaItem.Builder()
350-
.setTag(metadata)
351-
.setUri(Uri.parse(manifestUrl))
352-
.setCustomCacheKey(cacheKey)
353-
.build());
354310
}
311+
312+
final NonUriHlsDataSourceFactory.Builder hlsDataSourceFactoryBuilder =
313+
new NonUriHlsDataSourceFactory.Builder();
314+
hlsDataSourceFactoryBuilder.setPlaylistString(stream.getContent());
315+
316+
return dataSource.getHlsMediaSourceFactory(hlsDataSourceFactoryBuilder)
317+
.createMediaSource(new MediaItem.Builder()
318+
.setTag(metadata)
319+
.setUri(manifestUrlToUri(stream.getManifestUrl()))
320+
.setCustomCacheKey(cacheKey)
321+
.build());
355322
}
356323

357324
private static SsMediaSource buildSSMediaSource(final PlayerDataSource dataSource,
358325
final Stream stream,
359326
final String cacheKey,
360327
final MediaItemTag metadata)
361328
throws ResolverException {
362-
final boolean isUrlStream = stream.isUrl();
363-
if (isUrlStream && isNullOrEmpty(stream.getContent())) {
364-
throw new ResolverException(
365-
"Could not build a SS media source from an empty or a null URL content");
366-
}
367-
368-
if (isUrlStream) {
329+
if (stream.isUrl()) {
330+
throwResolverExceptionIfUrlNullOrEmpty(stream.getContent());
369331
return dataSource.getSSMediaSourceFactory().createMediaSource(
370332
new MediaItem.Builder()
371333
.setTag(metadata)
372334
.setUri(Uri.parse(stream.getContent()))
373335
.setCustomCacheKey(cacheKey)
374336
.build());
375-
} else {
376-
String baseUrl = stream.getManifestUrl();
377-
if (baseUrl == null) {
378-
baseUrl = "";
379-
}
380-
381-
final Uri uri = Uri.parse(baseUrl);
337+
}
382338

383-
final SsManifest smoothStreamingManifest;
384-
try {
385-
final ByteArrayInputStream smoothStreamingManifestInput = new ByteArrayInputStream(
386-
stream.getContent().getBytes(StandardCharsets.UTF_8));
387-
smoothStreamingManifest = new SsManifestParser().parse(uri,
388-
smoothStreamingManifestInput);
389-
} catch (final IOException e) {
390-
throw new ResolverException("Error when parsing manual SS manifest", e);
391-
}
339+
final Uri manifestUri = manifestUrlToUri(stream.getManifestUrl());
392340

393-
return dataSource.getSSMediaSourceFactory().createMediaSource(
394-
smoothStreamingManifest,
395-
new MediaItem.Builder()
396-
.setTag(metadata)
397-
.setUri(uri)
398-
.setCustomCacheKey(cacheKey)
399-
.build());
341+
final SsManifest smoothStreamingManifest;
342+
try {
343+
final ByteArrayInputStream smoothStreamingManifestInput = new ByteArrayInputStream(
344+
stream.getContent().getBytes(StandardCharsets.UTF_8));
345+
smoothStreamingManifest = new SsManifestParser().parse(manifestUri,
346+
smoothStreamingManifestInput);
347+
} catch (final IOException e) {
348+
throw new ResolverException("Error when parsing manual SS manifest", e);
400349
}
350+
351+
return dataSource.getSSMediaSourceFactory().createMediaSource(
352+
smoothStreamingManifest,
353+
new MediaItem.Builder()
354+
.setTag(metadata)
355+
.setUri(manifestUri)
356+
.setCustomCacheKey(cacheKey)
357+
.build());
401358
}
402359
//endregion
403360

@@ -435,8 +392,6 @@ private static MediaSource createYoutubeMediaSource(final Stream stream,
435392
createDashManifest(manifestString, stream), stream, cacheKey,
436393
metadata);
437394
} catch (final CreationException | IOException | NullPointerException e) {
438-
Log.e(TAG, "Error when generating the DASH manifest of YouTube ended live stream",
439-
e);
440395
throw new ResolverException(
441396
"Error when generating the DASH manifest of YouTube ended live stream", e);
442397
}
@@ -540,7 +495,23 @@ private static ProgressiveMediaSource buildYoutubeProgressiveMediaSource(
540495
//endregion
541496

542497

543-
//region resolver exception
498+
//region Utils
499+
private static Uri manifestUrlToUri(final String manifestUrl) {
500+
return Uri.parse(Objects.requireNonNullElse(manifestUrl, ""));
501+
}
502+
503+
private static void throwResolverExceptionIfUrlNullOrEmpty(@Nullable final String url)
504+
throws ResolverException {
505+
if (url == null) {
506+
throw new ResolverException("Null stream url");
507+
} else if (url.isEmpty()) {
508+
throw new ResolverException("Empty stream url");
509+
}
510+
}
511+
//endregion
512+
513+
514+
//region Resolver exception
544515
final class ResolverException extends Exception {
545516
public ResolverException(final String message) {
546517
super(message);

0 commit comments

Comments
 (0)