Skip to content

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

syncplay: add video player embeded

Predidit8c17b56dfd54
diff --git a/lib/pages/player/player_controller.dart b/lib/pages/player/player_controller.dart
index dabcce94..0943b212 100644
--- a/lib/pages/player/player_controller.dart
+++ b/lib/pages/player/player_controller.dart
@@ -41,6 +41,10 @@ abstract class _PlayerController with Store {
// 一起看控制器
SyncplayClient? syncplayController;
+ @observable
+ String syncplayRoom = '';
+ @observable
+ int syncplayClientRtt = 0;
/// 视频比例类型
/// 1. AUTO
@@ -336,13 +340,15 @@ abstract class _PlayerController with Store {
}
}
- Future<void> seek(Duration duration) async {
+ Future<void> seek(Duration duration, {bool enableSync = true}) async {
currentPosition = duration;
danmakuController.clear();
await mediaPlayer.seek(duration);
if (syncplayController != null) {
setSyncPlayCurrentPosition();
- await requestSyncPlaySync();
+ if (enableSync) {
+ await requestSyncPlaySync();
+ }
}
}
@@ -350,9 +356,11 @@ abstract class _PlayerController with Store {
danmakuController.pause();
await mediaPlayer.pause();
playing = false;
- if (syncplayController != null && enableSync) {
+ if (syncplayController != null) {
setSyncPlayCurrentPosition();
- await requestSyncPlaySync();
+ if (enableSync) {
+ await requestSyncPlaySync();
+ }
}
}
@@ -360,15 +368,19 @@ abstract class _PlayerController with Store {
danmakuController.resume();
await mediaPlayer.play();
playing = true;
- if (syncplayController != null && enableSync) {
+ if (syncplayController != null) {
setSyncPlayCurrentPosition();
- await requestSyncPlaySync();
+ if (enableSync) {
+ await requestSyncPlaySync();
+ }
}
}
Future<void> dispose({bool disposeSyncPlayController = true}) async {
if (disposeSyncPlayController) {
try {
+ syncplayRoom = '';
+ syncplayClientRtt = 0;
await syncplayController?.disconnect();
syncplayController = null;
} catch (_) {}
@@ -441,17 +453,64 @@ abstract class _PlayerController with Store {
print('SyncPlay: error: ${error.message}');
},
);
+ syncplayController!.onRoomMessage.listen(
+ (message) {
+ if (message['type'] == 'init') {
+ if (message['username'] == '') {
+ KazumiDialog.showToast(
+ message: '您是当前房间中的唯一用户',
+ duration: const Duration(seconds: 5));
+ setSyncPlayPlayingBangumi();
+ } else {
+ KazumiDialog.showToast(
+ message: '您不是当前房间中的唯一用户, 当前以用户 ${message['username']} 进度为准');
+ }
+ }
+ if (message['type'] == 'left') {
+ KazumiDialog.showToast(
+ message: '${message['username']} 离开了房间',
+ duration: const Duration(seconds: 5));
+ }
+ if (message['type'] == 'joined') {
+ KazumiDialog.showToast(
+ message: '${message['username']} 加入了房间',
+ duration: const Duration(seconds: 5));
+ }
+ },
+ );
syncplayController!.onFileChangedMessage.listen(
(message) {
- print('SyncPlay: file changed by ${message['setBy']}: ${message['name']}');
+ print(
+ 'SyncPlay: file changed by ${message['setBy']}: ${message['name']}');
},
);
syncplayController!.onPositionChangedMessage.listen(
(message) {
- print('SyncPlay: position changed by ${message['setBy']}: ${message['calculatedPositon']} position: ${message['position']} paused: ${message['paused']} clientRtt: ${message['clientRtt']} serverRtt: ${message['serverRtt']} fd: ${message['fd']}');
+ print(
+ 'SyncPlay: position changed by ${message['setBy']}: ${message['calculatedPositon']} position: ${message['position']} paused: ${message['paused']} clientRtt: ${message['clientRtt']} serverRtt: ${message['serverRtt']} fd: ${message['fd']}');
+ syncplayClientRtt = (message['clientRtt'].toDouble() * 1000).toInt();
+ if ((currentPosition.inMilliseconds -
+ (message['calculatedPositon'].toDouble() * 1000).toInt())
+ .abs() >
+ 3000) {
+ seek(
+ Duration(
+ milliseconds:
+ (message['calculatedPositon'].toDouble() * 1000)
+ .toInt()),
+ enableSync: false);
+ }
+ if (message['paused'] != playing) {
+ if (message['paused']) {
+ pause(enableSync: false);
+ } else {
+ play(enableSync: false);
+ }
+ }
},
);
await syncplayController!.joinRoom(room, username);
+ syncplayRoom = room;
} catch (e) {
print('SyncPlay: error: $e');
}
@@ -483,13 +542,6 @@ abstract class _PlayerController with Store {
KazumiDialog.showToast(message: 'SyncPlay: please create a room first');
return;
}
- if (!syncplayController!.isConnected) {
- print('SyncPlay: Socket is closed, please create or join a room again');
- KazumiDialog.showToast(
- message:
- 'SyncPlay: Socket is closed, please create or join a room again');
- return;
- }
try {
await setSyncPlayPlayingBangumi();
} catch (e) {
diff --git a/lib/pages/player/player_controller.g.dart b/lib/pages/player/player_controller.g.dart
index 846302c0..9cff52b8 100644
--- a/lib/pages/player/player_controller.g.dart
+++ b/lib/pages/player/player_controller.g.dart
@@ -41,6 +41,38 @@ mixin _$PlayerController on _PlayerController, Store {
});
}
+ late final _$syncplayRoomAtom =
+ Atom(name: '_PlayerController.syncplayRoom', context: context);
+
+ @override
+ String get syncplayRoom {
+ _$syncplayRoomAtom.reportRead();
+ return super.syncplayRoom;
+ }
+
+ @override
+ set syncplayRoom(String value) {
+ _$syncplayRoomAtom.reportWrite(value, super.syncplayRoom, () {
+ super.syncplayRoom = value;
+ });
+ }
+
+ late final _$syncplayClientRttAtom =
+ Atom(name: '_PlayerController.syncplayClientRtt', context: context);
+
+ @override
+ int get syncplayClientRtt {
+ _$syncplayClientRttAtom.reportRead();
+ return super.syncplayClientRtt;
+ }
+
+ @override
+ set syncplayClientRtt(int value) {
+ _$syncplayClientRttAtom.reportWrite(value, super.syncplayClientRtt, () {
+ super.syncplayClientRtt = value;
+ });
+ }
+
late final _$aspectRatioTypeAtom =
Atom(name: '_PlayerController.aspectRatioType', context: context);
@@ -382,6 +414,8 @@ mixin _$PlayerController on _PlayerController, Store {
return '''
danDanmakus: ${danDanmakus},
danmakuOn: ${danmakuOn},
+syncplayRoom: ${syncplayRoom},
+syncplayClientRtt: ${syncplayClientRtt},
aspectRatioType: ${aspectRatioType},
superResolutionType: ${superResolutionType},
volume: ${volume},
diff --git a/lib/pages/player/player_item_panel.dart b/lib/pages/player/player_item_panel.dart
index 8a60791f..7f5b95f4 100644
--- a/lib/pages/player/player_item_panel.dart
+++ b/lib/pages/player/player_item_panel.dart
@@ -1027,58 +1027,75 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {
],
if (!playerController.danmakuOn) const Spacer(),
],
+
/// 一起看 (未完成)
- // MenuAnchor(
- // consumeOutsideTap: true,
- // onOpen: () {
- // widget.cancelHideTimer();
- // playerController.canHidePlayerPanel = false;
- // },
- // onClose: () {
- // widget.cancelHideTimer();
- // widget.startHideTimer();
- // playerController.canHidePlayerPanel = true;
- // },
- // builder: (BuildContext context,
- // MenuController controller, Widget? child) {
- // return TextButton(
- // onPressed: () {
- // if (controller.isOpen) {
- // controller.close();
- // } else {
- // controller.open();
- // }
- // },
- // child: const Text(
- // '一起看',
- // style: TextStyle(color: Colors.white),
- // ),
- // );
- // },
- // menuChildren: [
- // MenuItemButton(
- // onPressed: () {
- // showSyncPlayRoomCreateDialog();
- // },
- // child: const Padding(
- // padding:
- // EdgeInsets.fromLTRB(0, 10, 10, 10),
- // child: Text("创建房间"),
- // ),
- // ),
- // MenuItemButton(
- // onPressed: () async {
- // await playerController
- // .sendSyncPlayTestMessage();
- // },
- // child: const Padding(
- // padding:
- // EdgeInsets.fromLTRB(0, 10, 10, 10),
- // child: Text("发送测试消息"),
- // ),
- // ),
- // ],
- // ),
+ MenuAnchor(
+ consumeOutsideTap: true,
+ onOpen: () {
+ widget.cancelHideTimer();
+ playerController.canHidePlayerPanel = false;
+ },
+ onClose: () {
+ widget.cancelHideTimer();
+ widget.startHideTimer();
+ playerController.canHidePlayerPanel = true;
+ },
+ builder: (BuildContext context,
+ MenuController controller, Widget? child) {
+ return TextButton(
+ onPressed: () {
+ if (controller.isOpen) {
+ controller.close();
+ } else {
+ controller.open();
+ }
+ },
+ child: const Text(
+ '一起看',
+ style: TextStyle(color: Colors.white),
+ ),
+ );
+ },
+ menuChildren: [
+ MenuItemButton(
+ child: Padding(
+ padding:
+ EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text(
+ "当前房间: ${playerController.syncplayRoom == '' ? '未加入' : playerController.syncplayRoom}"),
+ ),
+ ),
+ MenuItemButton(
+ child: Padding(
+ padding:
+ EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text(
+ "网络延时: ${playerController.syncplayClientRtt}ms"),
+ ),
+ ),
+ MenuItemButton(
+ onPressed: () {
+ showSyncPlayRoomCreateDialog();
+ },
+ child: const Padding(
+ padding:
+ EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text("加入房间"),
+ ),
+ ),
+ MenuItemButton(
+ onPressed: () async {
+ await playerController
+ .sendSyncPlayTestMessage();
+ },
+ child: const Padding(
+ padding:
+ EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text("强制同步"),
+ ),
+ ),
+ ],
+ ),
// 超分辨率
MenuAnchor(
consumeOutsideTap: true,
diff --git a/lib/utils/syncplay.dart b/lib/utils/syncplay.dart
index abbd5af9..db865e3f 100644
--- a/lib/utils/syncplay.dart
+++ b/lib/utils/syncplay.dart
@@ -172,6 +172,8 @@ class SyncplayClient {
bool _isLocked = false;
StreamController<Map<String, dynamic>>? _generalMessageController =
StreamController.broadcast();
+ StreamController<Map<String, dynamic>>? _roomMessageController =
+ StreamController.broadcast();
StreamController<Map<String, dynamic>>? _flieChangedMessageController =
StreamController.broadcast();
StreamController<Map<String, dynamic>>? _positionChangedMessageController =
@@ -198,6 +200,11 @@ class SyncplayClient {
return _generalMessageController!.stream;
}
+ Stream<Map<String, dynamic>> get onRoomMessage {
+ _roomMessageController ??= StreamController.broadcast();
+ return _roomMessageController!.stream;
+ }
+
Stream<Map<String, dynamic>> get onFileChangedMessage {
_flieChangedMessageController ??= StreamController.broadcast();
return _flieChangedMessageController!.stream;
@@ -278,8 +285,8 @@ class SyncplayClient {
final jsonStr = buffer.substring(startIndex, endIndex + 1);
try {
- // print(
- // 'SyncPlay: [${DateTime.now().millisecondsSinceEpoch / 1000.0}] received message: $jsonStr');
+ print(
+ 'SyncPlay: [${DateTime.now().millisecondsSinceEpoch / 1000.0}] received message: $jsonStr');
_handleMessage(json.decode(jsonStr));
} catch (e) {
_generalMessageController?.addError(
@@ -327,7 +334,8 @@ class SyncplayClient {
if (json['State']['ping'].containsKey('serverRtt')) {
_serverRtt = json['State']['ping']['serverRtt']?.toDouble() ?? 0.0;
}
- _updateClientRttAndFd(json['State']["ping"]["clientLatencyCalculation"], _serverRtt);
+ _updateClientRttAndFd(
+ json['State']["ping"]["clientLatencyCalculation"], _serverRtt);
}
if (json['State'].containsKey('ignoringOnTheFly')) {
sendSyncPlaySyncRequestAck();
@@ -340,7 +348,10 @@ class SyncplayClient {
return;
}
_positionChangedMessageController?.add({
- 'calculatedPositon': (json['State']['playstate']['paused'] ?? true) ? (json['State']['playstate']['position']?.toDouble() ?? 0.0) : ((json['State']['playstate']['position']?.toDouble() ?? 0.0) + _fd),
+ 'calculatedPositon': (json['State']['playstate']['paused'] ?? true)
+ ? (json['State']['playstate']['position']?.toDouble() ?? 0.0)
+ : ((json['State']['playstate']['position']?.toDouble() ?? 0.0) +
+ _fd),
'position': json['State']['playstate']['position']?.toDouble() ?? 0.0,
'paused': json['State']['playstate']['paused'] ?? true,
'setBy': json['State']['playstate']['setBy'] ?? '',
@@ -351,6 +362,24 @@ class SyncplayClient {
});
return;
} else if (json.containsKey('Set')) {
+ if (json['Set'].containsKey('playlistIndex')) {
+ _roomMessageController?.add({
+ 'type': 'init',
+ 'username': json['Set']['playlistIndex']['user'] ?? '',
+ });
+ return;
+ }
+ if (json['Set'].containsKey('user')) {
+ Map<String, dynamic> userMap = data['Set']['user'];
+ userMap.forEach((username, details) {
+ var event = details['event'].keys.first ?? 'unknown';
+ _roomMessageController?.add({
+ 'type': event,
+ 'username': username,
+ });
+ });
+ return;
+ }
if (!json['Set'].containsKey('file')) {
return;
}
@@ -358,12 +387,8 @@ class SyncplayClient {
return;
}
_flieChangedMessageController?.add({
- 'duration': json['Set']['file']['duration']?.toDouble() ?? 0.0,
'name': json['Set']['file']['name'] ?? '',
- 'size': json['Set']['file']['size'] ?? 0,
- 'setBy': json['Set']['user'].keys.first ?? 'sysytem',
- 'room': json['Set']['user'][json['Set']['user'].keys.first]['room']
- ['name'],
+ 'setBy': json['Set']['user'].keys.first ?? '',
});
return;
} else {
@@ -458,6 +483,8 @@ class SyncplayClient {
print('SyncPlay: disconnecting from Syncplay server: $_host:$_port');
await _generalMessageController?.close();
_generalMessageController = null;
+ await _roomMessageController?.close();
+ _roomMessageController = null;
await _flieChangedMessageController?.close();
_flieChangedMessageController = null;
await _positionChangedMessageController?.close();
@@ -487,8 +514,8 @@ class SyncplayClient {
}
final json = message.toJson();
final jsonStr = jsonEncode(json);
- // print(
- // 'SyncPlay: [${DateTime.now().millisecondsSinceEpoch / 1000.0}] sending message: $jsonStr');
+ print(
+ 'SyncPlay: [${DateTime.now().millisecondsSinceEpoch / 1000.0}] sending message: $jsonStr');
_socket?.write('$jsonStr\r\n');
}