|
| 1 | +import 'package:duanju_app/core_bridge.dart'; |
| 2 | +import 'package:duanju_app/models.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:integration_test/integration_test.dart'; |
| 5 | +import 'package:media_kit/media_kit.dart'; |
| 6 | + |
| 7 | +const fixtureBase = String.fromEnvironment('FIXTURE_BASE_URL'); |
| 8 | + |
| 9 | +/// Resolves each synthetic fixture through the native core, then plays the |
| 10 | +/// resulting plan through media_kit exactly as the app does, including the |
| 11 | +/// decryption key for the CENC fixture. |
| 12 | +void main() { |
| 13 | + final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 14 | + |
| 15 | + testWidgets('resolve and play every fixture through media_kit', (tester) async { |
| 16 | + MediaKit.ensureInitialized(); |
| 17 | + final repository = NativeRepository(); |
| 18 | + await repository.initialize(); |
| 19 | + |
| 20 | + const drama = Drama( |
| 21 | + id: 'hongguo:700001', |
| 22 | + source: 'hongguo', |
| 23 | + title: '设备播放验证', |
| 24 | + episodes: 3, |
| 25 | + ); |
| 26 | + final fixtures = { |
| 27 | + 1: ('clear.mp4', ''), |
| 28 | + 2: ('index.m3u8', ''), |
| 29 | + 3: ('encrypted.mp4', '00112233445566778899aabbccddeeff'), |
| 30 | + }; |
| 31 | + |
| 32 | + final report = <Map<String, Object?>>[]; |
| 33 | + for (final entry in fixtures.entries) { |
| 34 | + final number = entry.key; |
| 35 | + final (file, key) = entry.value; |
| 36 | + final episode = Episode({ |
| 37 | + 'id': '$number', |
| 38 | + 'source': 'hongguo', |
| 39 | + 'currentEpisode': number, |
| 40 | + 'title': '第$number集', |
| 41 | + 'videoUrl': '$fixtureBase/$file', |
| 42 | + 'referer': '$fixtureBase/', |
| 43 | + }, number); |
| 44 | + |
| 45 | + String? resolveError; |
| 46 | + PlaybackPlan? plan; |
| 47 | + try { |
| 48 | + plan = await repository.resolve(drama, episode); |
| 49 | + } catch (error) { |
| 50 | + resolveError = error.toString(); |
| 51 | + } |
| 52 | + if (plan == null) { |
| 53 | + // ignore: avoid_print |
| 54 | + print('[PlanProbe] ep$number ($file) resolve FAILED: $resolveError'); |
| 55 | + report.add({ |
| 56 | + 'episode': number, |
| 57 | + 'file': file, |
| 58 | + 'resolveError': resolveError, |
| 59 | + 'playable': false, |
| 60 | + }); |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + // ignore: avoid_print |
| 65 | + print('[PlanProbe] ep$number ($file) resolved url=${plan.url} ' |
| 66 | + 'local=${plan.local} keyFromPlan=${plan.decryptionKey.length}'); |
| 67 | + |
| 68 | + final effectiveKey = key.isNotEmpty ? key : plan.decryptionKey; |
| 69 | + final player = Player( |
| 70 | + configuration: const PlayerConfiguration(bufferSize: 8 * 1024 * 1024), |
| 71 | + ); |
| 72 | + String? playError; |
| 73 | + try { |
| 74 | + final platform = player.platform; |
| 75 | + if (platform is NativePlayer) { |
| 76 | + await platform.setProperty( |
| 77 | + 'demuxer-lavf-o', |
| 78 | + [ |
| 79 | + 'seg_max_retry=3', |
| 80 | + 'strict=experimental', |
| 81 | + 'allowed_extensions=ALL', |
| 82 | + 'protocol_whitelist=[http,https,tcp,tls,crypto,data,file]', |
| 83 | + if (effectiveKey.isNotEmpty) 'decryption_key=$effectiveKey', |
| 84 | + ].join(','), |
| 85 | + ); |
| 86 | + } |
| 87 | + await player.open( |
| 88 | + Media(plan.url, httpHeaders: plan.headers), |
| 89 | + play: true, |
| 90 | + ); |
| 91 | + final deadline = DateTime.now().add(const Duration(seconds: 30)); |
| 92 | + while (DateTime.now().isBefore(deadline)) { |
| 93 | + if (player.state.position.inMilliseconds > 300) break; |
| 94 | + await Future<void>.delayed(const Duration(milliseconds: 250)); |
| 95 | + } |
| 96 | + } catch (error) { |
| 97 | + playError = error.toString(); |
| 98 | + } |
| 99 | + // Headless runs have no video surface, so width stays null; real progress |
| 100 | + // and a known duration are the reliable signals that decoding worked. |
| 101 | + final playable = |
| 102 | + playError == null && |
| 103 | + player.state.duration.inMilliseconds > 0 && |
| 104 | + player.state.position.inMilliseconds > 300; |
| 105 | + // ignore: avoid_print |
| 106 | + print('[PlanProbe] ep$number playable=$playable err=$playError ' |
| 107 | + 'width=${player.state.width} dur=${player.state.duration} ' |
| 108 | + 'pos=${player.state.position}'); |
| 109 | + report.add({ |
| 110 | + 'episode': number, |
| 111 | + 'file': file, |
| 112 | + 'playable': playable, |
| 113 | + 'playError': playError, |
| 114 | + 'width': player.state.width, |
| 115 | + 'durationMs': player.state.duration.inMilliseconds, |
| 116 | + 'positionMs': player.state.position.inMilliseconds, |
| 117 | + }); |
| 118 | + await player.dispose(); |
| 119 | + await repository.release(plan.session); |
| 120 | + } |
| 121 | + |
| 122 | + binding.reportData ??= {}; |
| 123 | + binding.reportData!['planProbe'] = report; |
| 124 | + final allPlayable = report.every((row) => row['playable'] == true); |
| 125 | + expect(allPlayable, isTrue, |
| 126 | + reason: 'every fixture must play through media_kit: $report'); |
| 127 | + }, timeout: const Timeout(Duration(minutes: 5))); |
| 128 | +} |
0 commit comments