mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 07:55:18 +00:00
chore: avoid checking stream accessibility all the time
Only check stream accessible or not when first time it fails or gets blocked
This commit is contained in:
parent
ea329f40e8
commit
aee2c9282d
@ -9,9 +9,20 @@ enum SourceCodecs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum SourceQualities {
|
enum SourceQualities {
|
||||||
high,
|
high(2),
|
||||||
medium,
|
medium(1),
|
||||||
low,
|
low(0);
|
||||||
|
|
||||||
|
final int priority;
|
||||||
|
const SourceQualities(this.priority);
|
||||||
|
|
||||||
|
bool operator <(SourceQualities other) {
|
||||||
|
return priority < other.priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator >(SourceQualities other) {
|
||||||
|
return priority > other.priority;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef SiblingType<T extends TrackSourceInfo> = ({
|
typedef SiblingType<T extends TrackSourceInfo> = ({
|
||||||
|
@ -179,7 +179,7 @@ abstract class SourcedTrack extends BasicSourcedTrack {
|
|||||||
}).toList();
|
}).toList();
|
||||||
|
|
||||||
if (sameCodecSources.isNotEmpty) {
|
if (sameCodecSources.isNotEmpty) {
|
||||||
return preferences.audioQuality != SourceQualities.low
|
return preferences.audioQuality > SourceQualities.low
|
||||||
? sameCodecSources.first.url
|
? sameCodecSources.first.url
|
||||||
: sameCodecSources.last.url;
|
: sameCodecSources.last.url;
|
||||||
}
|
}
|
||||||
@ -190,7 +190,7 @@ abstract class SourcedTrack extends BasicSourcedTrack {
|
|||||||
return aDiff != bDiff ? aDiff - bDiff : a.quality.index - b.quality.index;
|
return aDiff != bDiff ? aDiff - bDiff : a.quality.index - b.quality.index;
|
||||||
});
|
});
|
||||||
|
|
||||||
return preferences.audioQuality != SourceQualities.low
|
return preferences.audioQuality > SourceQualities.low
|
||||||
? fallbackSource.firstOrNull?.url
|
? fallbackSource.firstOrNull?.url
|
||||||
: fallbackSource.lastOrNull?.url;
|
: fallbackSource.lastOrNull?.url;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import 'package:collection/collection.dart';
|
import 'package:collection/collection.dart';
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
import 'package:drift/drift.dart';
|
import 'package:drift/drift.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:spotube/models/database/database.dart';
|
import 'package:spotube/models/database/database.dart';
|
||||||
@ -6,6 +7,7 @@ import 'package:spotube/models/playback/track_sources.dart';
|
|||||||
import 'package:spotube/provider/database/database.dart';
|
import 'package:spotube/provider/database/database.dart';
|
||||||
import 'package:spotube/provider/user_preferences/user_preferences_provider.dart';
|
import 'package:spotube/provider/user_preferences/user_preferences_provider.dart';
|
||||||
import 'package:spotube/provider/youtube_engine/youtube_engine.dart';
|
import 'package:spotube/provider/youtube_engine/youtube_engine.dart';
|
||||||
|
import 'package:spotube/services/dio/dio.dart';
|
||||||
import 'package:spotube/services/logger/logger.dart';
|
import 'package:spotube/services/logger/logger.dart';
|
||||||
import 'package:spotube/services/song_link/song_link.dart';
|
import 'package:spotube/services/song_link/song_link.dart';
|
||||||
import 'package:spotube/services/sourced_track/enums.dart';
|
import 'package:spotube/services/sourced_track/enums.dart';
|
||||||
@ -389,14 +391,32 @@ class YoutubeSourcedTrack extends SourcedTrack {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<SourcedTrack> refreshStream() async {
|
Future<SourcedTrack> refreshStream() async {
|
||||||
|
List<TrackSource> validStreams = [];
|
||||||
|
|
||||||
|
for (final source in sources) {
|
||||||
|
final res = await globalDio.head(
|
||||||
|
source.url,
|
||||||
|
options:
|
||||||
|
Options(validateStatus: (status) => status != null && status < 500),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (res.statusCode! < 400) {
|
||||||
|
validStreams.add(source);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (validStreams.isEmpty) {
|
||||||
final manifest =
|
final manifest =
|
||||||
await ref.read(youtubeEngineProvider).getStreamManifest(info.id);
|
await ref.read(youtubeEngineProvider).getStreamManifest(info.id);
|
||||||
|
|
||||||
|
validStreams = toTrackSources(manifest);
|
||||||
|
}
|
||||||
|
|
||||||
final sourcedTrack = YoutubeSourcedTrack(
|
final sourcedTrack = YoutubeSourcedTrack(
|
||||||
ref: ref,
|
ref: ref,
|
||||||
siblings: siblings,
|
siblings: siblings,
|
||||||
source: source,
|
source: source,
|
||||||
sources: toTrackSources(manifest),
|
sources: validStreams,
|
||||||
info: info,
|
info: info,
|
||||||
query: query,
|
query: query,
|
||||||
);
|
);
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
import 'dart:isolate';
|
import 'dart:isolate';
|
||||||
|
|
||||||
import 'package:dio/dio.dart';
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:spotube/services/dio/dio.dart';
|
|
||||||
import 'package:spotube/services/logger/logger.dart';
|
|
||||||
import 'package:spotube/services/youtube_engine/youtube_engine.dart';
|
import 'package:spotube/services/youtube_engine/youtube_engine.dart';
|
||||||
import 'package:youtube_explode_dart/youtube_explode_dart.dart';
|
import 'package:youtube_explode_dart/youtube_explode_dart.dart';
|
||||||
|
|
||||||
@ -170,31 +167,13 @@ class YouTubeExplodeEngine implements YouTubeEngine {
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
final accessibleStreams = <AudioOnlyStreamInfo>[];
|
final audioStreams = streamManifest.audioOnly.where(
|
||||||
final stringBuffer = StringBuffer();
|
(stream) => stream.bitrate.bitsPerSecond >= 40960,
|
||||||
|
|
||||||
for (final stream in streamManifest.audioOnly) {
|
|
||||||
// Call dio head request to check if the stream is accessible
|
|
||||||
final response = await globalDio.headUri(
|
|
||||||
stream.url,
|
|
||||||
options: Options(
|
|
||||||
followRedirects: true,
|
|
||||||
validateStatus: (status) {
|
|
||||||
return status != null && status < 500;
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
stringBuffer.writeln(
|
return StreamManifest(
|
||||||
"Stream $videoId Status ${response.statusCode} Codec ${stream.audioCodec} "
|
audioStreams.map(
|
||||||
"Bitrate ${stream.bitrate} Container ${stream.container}",
|
(stream) => AudioOnlyStreamInfo(
|
||||||
);
|
|
||||||
|
|
||||||
if (response.statusCode != null &&
|
|
||||||
response.statusCode! >= 200 &&
|
|
||||||
response.statusCode! < 400) {
|
|
||||||
accessibleStreams.add(
|
|
||||||
AudioOnlyStreamInfo(
|
|
||||||
stream.videoId,
|
stream.videoId,
|
||||||
stream.tag,
|
stream.tag,
|
||||||
stream.url,
|
stream.url,
|
||||||
@ -211,14 +190,9 @@ class YouTubeExplodeEngine implements YouTubeEngine {
|
|||||||
stream.codec,
|
stream.codec,
|
||||||
stream.audioTrack,
|
stream.audioTrack,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
AppLogger.log.d(stringBuffer.toString());
|
|
||||||
|
|
||||||
return StreamManifest(accessibleStreams);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Video> getVideo(String videoId) async {
|
Future<Video> getVideo(String videoId) async {
|
||||||
|
Loading…
Reference in New Issue
Block a user