Skip to content

Commit

Permalink
feat: 新增更多CDN选项,且默认单独关闭音频CDN
Browse files Browse the repository at this point in the history
  • Loading branch information
orz12 committed Jul 20, 2024
1 parent 71edc77 commit 97ea882
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 47 deletions.
114 changes: 114 additions & 0 deletions lib/models/video/play/CDN.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//https://github.com/yujincheng08/BiliRoaming/blob/master/app/src/main/res/values/strings_raw.xml
//https://github.com/yujincheng08/BiliRoaming/blob/master/app/src/main/res/values/arrays.xml
enum CDNService {
baseUrl,
backupUrl,
ali,
alib,
alio1,
cos,
cosb,
coso1,
hw,
hwb,
hwo1,
hw_08c,
hw_08h,
hw_08ct,
tf_hw,
tf_tx,
akamai,
aliov,
cosov,
hwov,
hk_bcache,
}

extension CDNServiceDesc on CDNService {
static final List<String> _descList = [
'基础 URL(不推荐)',
'备用 URL',
'ali(阿里云)',
'alib(阿里云)',
'alio1(阿里云)',
'cos(腾讯云)',
'cosb(腾讯云,VOD 加速类型)',
'coso1(腾讯云)',
'hw(华为云,融合 CDN)',
'hwb(华为云,融合 CDN)',
'hwo1(华为云,融合 CDN)',
'08c(华为云,融合 CDN)',
'08h(华为云,融合 CDN)',
'08ct(华为云,融合 CDN)',
'tf_hw(华为云)',
'tf_tx(腾讯云)',
'akamai(Akamai 海外)',
'aliov(阿里云海外)',
'cosov(腾讯云海外)',
'hwov(华为云海外)',
'hk_bcache(Bilibili海外)',
];
String get description => _descList[index];
}

extension CDNServiceHost on CDNService {
static final List<String> _hostList = [
'',
'',
'upos-sz-mirrorali.bilivideo.com',
'upos-sz-mirroralib.bilivideo.com',
'upos-sz-mirroralio1.bilivideo.com',
'upos-sz-mirrorcos.bilivideo.com',
'upos-sz-mirrorcosb.bilivideo.com',
'upos-sz-mirrorcoso1.bilivideo.com',
'upos-sz-mirrorhw.bilivideo.com',
'upos-sz-mirrorhwb.bilivideo.com',
'upos-sz-mirrorhwo1.bilivideo.com',
'upos-sz-mirror08c.bilivideo.com',
'upos-sz-mirror08h.bilivideo.com',
'upos-sz-mirror08ct.bilivideo.com',
'upos-tf-all-hw.bilivideo.com',
'upos-tf-all-tx.bilivideo.com',
'upos-hz-mirrorakam.akamaized.net',
'upos-sz-mirroraliov.bilivideo.com',
'upos-sz-mirrorcosov.bilivideo.com',
'upos-sz-mirrorhwov.bilivideo.com',
'cn-hk-eq-bcache-01.bilivideo.com',
];
String get host => _hostList[index];
}

extension CDNServiceCode on CDNService {
static final List<String> _codeList = [
'baseUrl',
'backupUrl',
'ali',
'alib',
'alio1',
'cos',
'cosb',
'coso1',
'hw',
'hwb',
'hwo1',
'hw_08c',
'hw_08h',
'hw_08ct',
'tf_hw',
'tf_tx',
'akamai',
'aliov',
'cosov',
'hwov',
'hk_bcache',
];
String get code => _codeList[index];

static CDNService? fromCode(String code) {
final index = _codeList.indexOf(code);
if (index != -1) {
return CDNService.values[index];
}
return null;
}
}
10 changes: 3 additions & 7 deletions lib/pages/live_room/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LiveRoomController extends GetxController {
PlPlayerController plPlayerController =
PlPlayerController.getInstance(videoType: 'live');
Rx<RoomInfoH5Model> roomInfoH5 = RoomInfoH5Model().obs;
late bool enableCDN;
// late bool enableCDN;

@override
void onInit() {
Expand All @@ -35,7 +35,7 @@ class LiveRoomController extends GetxController {
}
}
// CDN优化
enableCDN = setting.get(SettingBoxKey.enableCDN, defaultValue: true);
// enableCDN = setting.get(SettingBoxKey.enableCDN, defaultValue: true);
}

playerInit(source) async {
Expand All @@ -62,11 +62,7 @@ class LiveRoomController extends GetxController {
List<CodecItem> codec =
res['data'].playurlInfo.playurl.stream.first.format.first.codec;
CodecItem item = codec.first;
String videoUrl = enableCDN
? VideoUtils.getCdnUrl(item)
: (item.urlInfo?.first.host)! +
item.baseUrl! +
item.urlInfo!.first.extra!;
String videoUrl = VideoUtils.getCdnUrl(item);
await playerInit(videoUrl);
return res;
}
Expand Down
43 changes: 37 additions & 6 deletions lib/pages/setting/video_setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:PiliPalaX/models/video/play/quality.dart';
import 'package:PiliPalaX/models/video/play/CDN.dart';
import 'package:PiliPalaX/pages/setting/widgets/select_dialog.dart';
import 'package:PiliPalaX/utils/storage.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';

import 'widgets/switch_item.dart';

Expand All @@ -23,6 +25,7 @@ class _VideoSettingState extends State<VideoSetting> {
late dynamic secondDecode;
late dynamic hardwareDecoding;
late dynamic videoSync;
late dynamic defaultCDNService;

@override
void initState() {
Expand All @@ -39,6 +42,8 @@ class _VideoSettingState extends State<VideoSetting> {
defaultValue: Platform.isAndroid ? 'auto-safe' : 'auto');
videoSync =
setting.get(SettingBoxKey.videoSync, defaultValue: 'display-resample');
defaultCDNService = setting.get(SettingBoxKey.CDNService,
defaultValue: CDNService.ali.code);
}

@override
Expand Down Expand Up @@ -80,11 +85,37 @@ class _VideoSettingState extends State<VideoSetting> {
setKey: SettingBoxKey.p1080,
defaultVal: true,
),
const SetSwitchItem(
title: 'CDN优化',
subTitle: '使用优质CDN线路',
leading: Icon(Icons.network_check_outlined),
setKey: SettingBoxKey.enableCDN,
ListTile(
title: Text('CDN 设置', style: titleStyle),
leading: Icon(MdiIcons.cloudPlusOutline),
subtitle: Text(
'当前使用:${CDNServiceCode.fromCode(defaultCDNService)!.description},部分 CDN 可能失效,如无法播放请尝试切换',
style: subTitleStyle,
),
onTap: () async {
String? result = await showDialog(
context: context,
builder: (context) {
return SelectDialog<String>(
title: 'CDN 设置',
value: defaultCDNService,
values: CDNService.values.map((e) {
return {'title': e.description, 'value': e.code};
}).toList());
},
);
if (result != null) {
defaultCDNService = result;
setting.put(SettingBoxKey.CDNService, result);
setState(() {});
}
},
),
SetSwitchItem(
title: '音频不跟随 CDN 设置',
subTitle: '直接采用备用 URL,可解决部分视频无声',
leading: Icon(MdiIcons.musicNotePlus),
setKey: SettingBoxKey.disableAudioCDN,
defaultVal: true,
),
ListTile(
Expand Down Expand Up @@ -117,7 +148,7 @@ class _VideoSettingState extends State<VideoSetting> {
ListTile(
dense: false,
title: Text('默认音质', style: titleStyle),
leading: const Icon(Icons.audiotrack_outlined),
leading: const Icon(Icons.music_video_outlined),
subtitle: Text(
'当前音质:${AudioQualityCode.fromCode(defaultAudioQa)!.description!}',
style: subTitleStyle,
Expand Down
19 changes: 11 additions & 8 deletions lib/pages/video/detail/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class VideoDetailController extends GetxController
Floating? floating;
late PreferredSizeWidget headerControl;

late bool enableCDN;
// late bool enableCDN;
late int? cacheVideoQa;
late String cacheDecode;
late String cacheSecondDecode;
Expand Down Expand Up @@ -138,7 +138,8 @@ class VideoDetailController extends GetxController
heroTag: heroTag,
);
// CDN优化
enableCDN = setting.get(SettingBoxKey.enableCDN, defaultValue: true);
// enableCDN = setting.get(SettingBoxKey.enableCDN, defaultValue: true);

// 预设的画质
cacheVideoQa = setting.get(SettingBoxKey.defaultVideoQa,
defaultValue: VideoQuality.values.last.code);
Expand Down Expand Up @@ -388,9 +389,10 @@ class VideoDetailController extends GetxController
(e) => e.codecs!.startsWith(currentDecodeFormats.code),
orElse: () => videosList.first);

videoUrl = enableCDN
? VideoUtils.getCdnUrl(firstVideo)
: (firstVideo.backupUrl ?? firstVideo.baseUrl!);
// videoUrl = enableCDN
// ? VideoUtils.getCdnUrl(firstVideo)
// : (firstVideo.backupUrl ?? firstVideo.baseUrl!);
videoUrl = VideoUtils.getCdnUrl(firstVideo);

/// 优先顺序 设置中指定质量 -> 当前可选的最高质量
late AudioItem? firstAudio;
Expand All @@ -415,9 +417,10 @@ class VideoDetailController extends GetxController
}
firstAudio = audiosList.firstWhere((e) => e.id == closestNumber,
orElse: () => audiosList.first);
audioUrl = enableCDN
? VideoUtils.getCdnUrl(firstAudio)
: (firstAudio.backupUrl ?? firstAudio.baseUrl!);
// audioUrl = enableCDN
// ? VideoUtils.getCdnUrl(firstAudio)
// : (firstAudio.backupUrl ?? firstAudio.baseUrl!);
audioUrl = VideoUtils.getCdnUrl(firstAudio);
if (firstAudio.id != null) {
currentAudioQa = AudioQualityCode.fromCode(firstAudio.id!)!;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/utils/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ class SettingBoxKey {
allowRotateScreen = 'allowRotateScreen',
horizontalScreen = 'horizontalScreen',
p1080 = 'p1080',
enableCDN = 'enableCDN',
CDNService = 'CDNService',
disableAudioCDN = 'disableAudioCDN',
// enableCDN = 'enableCDN',
autoPiP = 'autoPiP',
pipNoDanmaku = 'pipNoDanmaku',
enableAutoLongPressSpeed = 'enableAutoLongPressSpeed',
Expand Down
Loading

0 comments on commit 97ea882

Please sign in to comment.