Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

refactor: simplify WebViewVideoSourceProvider async resolution logic

Predidit70451ceca6a0
diff --git a/lib/providers/webview_video_source_provider.dart b/lib/providers/webview_video_source_provider.dart
index 7e586ecf..5ee1d8ef 100644
--- a/lib/providers/webview_video_source_provider.dart
+++ b/lib/providers/webview_video_source_provider.dart
@@ -10,15 +10,14 @@ import 'package:kazumi/providers/video_source_provider.dart';
/// 仅在 [dispose] 时才真正销毁 WebView。
class WebViewVideoSourceProvider implements IVideoSourceProvider {
WebviewItemController? _webview;
- StreamSubscription? _subscription;
StreamSubscription? _logSubscription;
-
+
/// 单个 Provider 实例不能实现并发解析,单个 Provider 实例只能持有一个 Webview
/// 但是 Provider 可以在正在进行的解析未完成时,取消该解析并开始新的解析
/// 通过递增 ID 标识最新请求,取消旧请求
- int resolveId = 0;
+ int _resolveId = 0;
- final StreamController<String> _logController =
+ final StreamController<String> _logController =
StreamController<String>.broadcast();
Stream<String> get onLog => _logController.stream;
@@ -29,16 +28,13 @@ class WebViewVideoSourceProvider implements IVideoSourceProvider {
int offset = 0,
Duration timeout = const Duration(seconds: 15),
}) async {
- resolveId++;
- final currentResolveId = resolveId;
- await _subscription?.cancel();
- _subscription = null;
+ _resolveId++;
+ final currentResolveId = _resolveId;
- // 复用或初始化 WebView
if (_webview == null) {
_webview = WebviewItemControllerFactory.getController();
await _webview!.init();
-
+
_logSubscription = _webview!.onLog.listen((log) {
if (!_logController.isClosed) {
_logController.add(log);
@@ -46,68 +42,46 @@ class WebViewVideoSourceProvider implements IVideoSourceProvider {
});
}
- final localCompleter = Completer<VideoSource>();
- StreamSubscription? currentSubscription;
-
try {
- if (currentResolveId != resolveId) {
- throw const VideoSourceCancelledException();
- }
-
- // 订阅视频源解析结果
- currentSubscription = _webview!.onVideoURLParser.listen((event) {
- if (currentResolveId == resolveId && !localCompleter.isCompleted) {
- localCompleter.complete(VideoSource(
- url: event.$1,
- offset: event.$2,
- type: VideoSourceType.online,
- ));
- }
- });
- _subscription = currentSubscription;
-
- if (currentResolveId != resolveId) {
- throw const VideoSourceCancelledException();
- }
-
- // 加载 URL 并等待解析结果
await _webview!.loadUrl(
episodeUrl,
useLegacyParser,
offset: offset,
);
- if (currentResolveId != resolveId) {
+ if (currentResolveId != _resolveId) {
throw const VideoSourceCancelledException();
}
- final result = await localCompleter.future.timeout(
+ final event = await _webview!.onVideoURLParser.first.timeout(
timeout,
onTimeout: () {
- if (currentResolveId != resolveId) {
+ if (currentResolveId != _resolveId) {
throw const VideoSourceCancelledException();
}
throw VideoSourceTimeoutException(timeout);
},
);
- if (currentResolveId != resolveId) {
+ if (currentResolveId != _resolveId) {
throw const VideoSourceCancelledException();
}
- return result;
+ return VideoSource(
+ url: event.$1,
+ offset: event.$2,
+ type: VideoSourceType.online,
+ );
} catch (e) {
if (e is VideoSourceCancelledException) {
rethrow;
}
- if (currentResolveId != resolveId) {
+ if (currentResolveId != _resolveId) {
throw const VideoSourceCancelledException();
}
rethrow;
} finally {
- await currentSubscription?.cancel();
- if (currentResolveId == resolveId) {
- _subscription = null;
+ if (currentResolveId == _resolveId) {
await _webview?.unloadPage();
}
}
@@ -115,9 +89,7 @@ class WebViewVideoSourceProvider implements IVideoSourceProvider {
@override
void cancel() {
- resolveId++;
- _subscription?.cancel();
- _subscription = null;
+ _resolveId++;
}
@override