|
2 | 2 |
|
3 | 3 | import static org.schabi.newpipe.extractor.stream.AudioStream.UNKNOWN_BITRATE; |
4 | 4 | import static org.schabi.newpipe.extractor.stream.VideoStream.RESOLUTION_UNKNOWN; |
5 | | -import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; |
6 | 5 | import static org.schabi.newpipe.player.helper.PlayerDataSource.LIVE_STREAM_EDGE_GAP_MILLIS; |
7 | 6 |
|
8 | 7 | import android.net.Uri; |
@@ -88,7 +87,7 @@ private static StringBuilder commonCacheKeyOf(final StreamInfo info, |
88 | 87 | // cache useless. |
89 | 88 | if (resolutionOrBitrateUnknown && mediaFormat == null) { |
90 | 89 | cacheKey.append(" "); |
91 | | - Objects.hash(stream.getContent(), stream.getManifestUrl()); |
| 90 | + cacheKey.append(Objects.hash(stream.getContent(), stream.getManifestUrl())); |
92 | 91 | } |
93 | 92 |
|
94 | 93 | return cacheKey; |
@@ -250,154 +249,112 @@ private static ProgressiveMediaSource buildProgressiveMediaSource( |
250 | 249 | final Stream stream, |
251 | 250 | final String cacheKey, |
252 | 251 | 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()); |
267 | 259 | } |
268 | 260 |
|
269 | 261 | private static DashMediaSource buildDashMediaSource(final PlayerDataSource dataSource, |
270 | 262 | final Stream stream, |
271 | 263 | final String cacheKey, |
272 | 264 | final MediaItemTag metadata) |
273 | 265 | 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 | | - } |
279 | 266 |
|
280 | | - if (isUrlStream) { |
| 267 | + if (stream.isUrl()) { |
| 268 | + throwResolverExceptionIfUrlNullOrEmpty(stream.getContent()); |
281 | 269 | return dataSource.getDashMediaSourceFactory().createMediaSource( |
282 | 270 | new MediaItem.Builder() |
283 | 271 | .setTag(metadata) |
284 | 272 | .setUri(Uri.parse(stream.getContent())) |
285 | 273 | .setCustomCacheKey(cacheKey) |
286 | 274 | .build()); |
287 | | - } else { |
288 | | - String baseUrl = stream.getManifestUrl(); |
289 | | - if (baseUrl == null) { |
290 | | - baseUrl = ""; |
291 | | - } |
292 | | - |
293 | | - final Uri uri = Uri.parse(baseUrl); |
| 275 | + } |
294 | 276 |
|
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); |
307 | 288 | } |
308 | 289 | } |
309 | 290 |
|
310 | 291 | private static DashManifest createDashManifest(final String manifestContent, |
311 | 292 | 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))); |
320 | 295 | } |
321 | 296 |
|
322 | 297 | private static HlsMediaSource buildHlsMediaSource(final PlayerDataSource dataSource, |
323 | 298 | final Stream stream, |
324 | 299 | final String cacheKey, |
325 | 300 | final MediaItemTag metadata) |
326 | 301 | 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()); |
334 | 304 | return dataSource.getHlsMediaSourceFactory(null).createMediaSource( |
335 | 305 | new MediaItem.Builder() |
336 | 306 | .setTag(metadata) |
337 | 307 | .setUri(Uri.parse(stream.getContent())) |
338 | 308 | .setCustomCacheKey(cacheKey) |
339 | 309 | .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()); |
354 | 310 | } |
| 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()); |
355 | 322 | } |
356 | 323 |
|
357 | 324 | private static SsMediaSource buildSSMediaSource(final PlayerDataSource dataSource, |
358 | 325 | final Stream stream, |
359 | 326 | final String cacheKey, |
360 | 327 | final MediaItemTag metadata) |
361 | 328 | 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()); |
369 | 331 | return dataSource.getSSMediaSourceFactory().createMediaSource( |
370 | 332 | new MediaItem.Builder() |
371 | 333 | .setTag(metadata) |
372 | 334 | .setUri(Uri.parse(stream.getContent())) |
373 | 335 | .setCustomCacheKey(cacheKey) |
374 | 336 | .build()); |
375 | | - } else { |
376 | | - String baseUrl = stream.getManifestUrl(); |
377 | | - if (baseUrl == null) { |
378 | | - baseUrl = ""; |
379 | | - } |
380 | | - |
381 | | - final Uri uri = Uri.parse(baseUrl); |
| 337 | + } |
382 | 338 |
|
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()); |
392 | 340 |
|
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); |
400 | 349 | } |
| 350 | + |
| 351 | + return dataSource.getSSMediaSourceFactory().createMediaSource( |
| 352 | + smoothStreamingManifest, |
| 353 | + new MediaItem.Builder() |
| 354 | + .setTag(metadata) |
| 355 | + .setUri(manifestUri) |
| 356 | + .setCustomCacheKey(cacheKey) |
| 357 | + .build()); |
401 | 358 | } |
402 | 359 | //endregion |
403 | 360 |
|
@@ -435,8 +392,6 @@ private static MediaSource createYoutubeMediaSource(final Stream stream, |
435 | 392 | createDashManifest(manifestString, stream), stream, cacheKey, |
436 | 393 | metadata); |
437 | 394 | } catch (final CreationException | IOException | NullPointerException e) { |
438 | | - Log.e(TAG, "Error when generating the DASH manifest of YouTube ended live stream", |
439 | | - e); |
440 | 395 | throw new ResolverException( |
441 | 396 | "Error when generating the DASH manifest of YouTube ended live stream", e); |
442 | 397 | } |
@@ -540,7 +495,23 @@ private static ProgressiveMediaSource buildYoutubeProgressiveMediaSource( |
540 | 495 | //endregion |
541 | 496 |
|
542 | 497 |
|
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 |
544 | 515 | final class ResolverException extends Exception { |
545 | 516 | public ResolverException(final String message) { |
546 | 517 | super(message); |
|
0 commit comments