播放器修改,功能完善
This commit is contained in:
@@ -5,15 +5,19 @@ import 'package:flutter_webrtc/flutter_webrtc.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class WebRTCLocalPlayer extends StatefulWidget {
|
||||
final String streamUrl;
|
||||
final String streamUrl; // 第一个流(四分屏)
|
||||
final String? secondStreamUrl; // 第二条完整流,可选
|
||||
final bool showLeftPip;
|
||||
final bool showRightPip;
|
||||
final bool obstacleRecognitionFlag;
|
||||
|
||||
const WebRTCLocalPlayer({
|
||||
super.key,
|
||||
required this.streamUrl,
|
||||
this.secondStreamUrl,
|
||||
this.showLeftPip = true,
|
||||
this.showRightPip = true,
|
||||
this.obstacleRecognitionFlag = false,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -21,19 +25,17 @@ class WebRTCLocalPlayer extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _WebRTCLocalPlayerState extends State<WebRTCLocalPlayer> {
|
||||
final RTCVideoRenderer _renderer = RTCVideoRenderer();
|
||||
final RTCVideoRenderer _renderer = RTCVideoRenderer(); // 第一个流(四分屏)
|
||||
RTCVideoRenderer? _secondRenderer; // 第二条完整流
|
||||
|
||||
RTCPeerConnection? _peerConnection;
|
||||
RTCPeerConnection? _secondPeerConnection;
|
||||
|
||||
bool _isInitialized = false;
|
||||
bool _isFrontMain = true;
|
||||
bool _isFrontMain = true; // true=前,false=后
|
||||
|
||||
// 使用 ValueNotifier 配合局部刷新,提升拖拽性能
|
||||
final ValueNotifier<Offset> _leftPosNotifier = ValueNotifier(
|
||||
const Offset(20, 80),
|
||||
);
|
||||
final ValueNotifier<Offset> _rightPosNotifier = ValueNotifier(
|
||||
const Offset(200, 80),
|
||||
);
|
||||
final ValueNotifier<Offset> _leftPosNotifier = ValueNotifier(const Offset(20, 80));
|
||||
final ValueNotifier<Offset> _rightPosNotifier = ValueNotifier(const Offset(200, 80));
|
||||
bool _isPosInitialized = false;
|
||||
|
||||
@override
|
||||
@@ -45,42 +47,44 @@ class _WebRTCLocalPlayerState extends State<WebRTCLocalPlayer> {
|
||||
Future<void> _prepareAndConnect() async {
|
||||
try {
|
||||
await _renderer.initialize();
|
||||
if (!mounted) return;
|
||||
await _initWebRTCConnection();
|
||||
await _initWebRTCConnection(widget.streamUrl, _renderer, true);
|
||||
|
||||
if (widget.obstacleRecognitionFlag && widget.secondStreamUrl != null) {
|
||||
await _startSecondStream(widget.secondStreamUrl!);
|
||||
}
|
||||
|
||||
setState(() => _isInitialized = true);
|
||||
} catch (e) {
|
||||
debugPrint("初始化失败: $e");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _initWebRTCConnection() async {
|
||||
_peerConnection = await createPeerConnection({
|
||||
Future<void> _initWebRTCConnection(
|
||||
String url, RTCVideoRenderer renderer, bool isMain) async {
|
||||
final pc = await createPeerConnection({
|
||||
"sdpSemantics": "unified-plan",
|
||||
"iceServers": [
|
||||
{"urls": "stun:stun.l.google.com:19302"},
|
||||
],
|
||||
});
|
||||
|
||||
await _peerConnection!.addTransceiver(
|
||||
// 只接收视频
|
||||
await pc.addTransceiver(
|
||||
kind: RTCRtpMediaType.RTCRtpMediaTypeVideo,
|
||||
init: RTCRtpTransceiverInit(direction: TransceiverDirection.RecvOnly),
|
||||
);
|
||||
|
||||
_peerConnection!.onTrack = (RTCTrackEvent event) {
|
||||
if (event.track.kind == 'video' && event.streams.isNotEmpty) {
|
||||
if (mounted) setState(() => _renderer.srcObject = event.streams[0]);
|
||||
pc.onTrack = (RTCTrackEvent event) {
|
||||
if (event.streams.isNotEmpty && event.track.kind == 'video') {
|
||||
if (mounted) setState(() => renderer.srcObject = event.streams[0]);
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
RTCSessionDescription offer = await _peerConnection!.createOffer({
|
||||
'offerToReceiveVideo': 1,
|
||||
});
|
||||
await _peerConnection!.setLocalDescription(offer);
|
||||
RTCSessionDescription offer = await pc.createOffer({'offerToReceiveVideo': 1});
|
||||
await pc.setLocalDescription(offer);
|
||||
|
||||
Uri uri = Uri.parse(
|
||||
widget.streamUrl.replaceFirst('webrtc://', 'http://'),
|
||||
);
|
||||
Uri uri = Uri.parse(url.replaceFirst('webrtc://', 'http://'));
|
||||
String apiUrl = "http://${uri.host}:1985/rtc/v1/play/";
|
||||
|
||||
final response = await http.post(
|
||||
@@ -88,7 +92,7 @@ class _WebRTCLocalPlayerState extends State<WebRTCLocalPlayer> {
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({
|
||||
"api": apiUrl,
|
||||
"streamurl": widget.streamUrl,
|
||||
"streamurl": url,
|
||||
"clientip": null,
|
||||
"sdp": offer.sdp,
|
||||
}),
|
||||
@@ -96,179 +100,155 @@ class _WebRTCLocalPlayerState extends State<WebRTCLocalPlayer> {
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(response.body);
|
||||
await _peerConnection!.setRemoteDescription(
|
||||
await pc.setRemoteDescription(
|
||||
RTCSessionDescription(data['sdp'], 'answer'),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("信令错误: $e");
|
||||
}
|
||||
|
||||
if (isMain) _peerConnection = pc;
|
||||
else _secondPeerConnection = pc;
|
||||
}
|
||||
|
||||
Future<void> _startSecondStream(String url) async {
|
||||
_secondRenderer = RTCVideoRenderer();
|
||||
await _secondRenderer!.initialize();
|
||||
await _initWebRTCConnection(url, _secondRenderer!, false);
|
||||
}
|
||||
|
||||
Future<void> _stopSecondStream() async {
|
||||
if (_secondPeerConnection != null) {
|
||||
await _secondPeerConnection!.close();
|
||||
await _secondPeerConnection!.dispose();
|
||||
_secondPeerConnection = null;
|
||||
}
|
||||
if (_secondRenderer != null) {
|
||||
_secondRenderer!.srcObject = null;
|
||||
await _secondRenderer!.dispose();
|
||||
_secondRenderer = null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant WebRTCLocalPlayer oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.obstacleRecognitionFlag != oldWidget.obstacleRecognitionFlag) {
|
||||
if (widget.obstacleRecognitionFlag) {
|
||||
if (widget.secondStreamUrl != null) _startSecondStream(widget.secondStreamUrl!);
|
||||
} else {
|
||||
_stopSecondStream();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_peerConnection?.close();
|
||||
_peerConnection?.dispose();
|
||||
_secondPeerConnection?.close();
|
||||
_secondPeerConnection?.dispose();
|
||||
|
||||
_renderer.srcObject = null;
|
||||
_renderer.dispose();
|
||||
_secondRenderer?.srcObject = null;
|
||||
_secondRenderer?.dispose();
|
||||
|
||||
_leftPosNotifier.dispose();
|
||||
_rightPosNotifier.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
// 四分屏裁剪视图
|
||||
Widget _buildQuadrantView({required Alignment alignment}) {
|
||||
if (!_isInitialized || _renderer.srcObject == null)
|
||||
return Container(color: Colors.black);
|
||||
// -------------------- 渲染逻辑 --------------------
|
||||
|
||||
return RepaintBoundary(
|
||||
child: ClipRect(
|
||||
child: FractionallySizedBox(
|
||||
widthFactor: 2.0,
|
||||
heightFactor: 2.0,
|
||||
alignment: alignment,
|
||||
child: RTCVideoView(
|
||||
_renderer,
|
||||
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover,
|
||||
mirror: false,
|
||||
RTCVideoRenderer getFrontRenderer() {
|
||||
if (widget.obstacleRecognitionFlag && _secondRenderer != null) {
|
||||
return _secondRenderer!;
|
||||
}
|
||||
return _renderer;
|
||||
}
|
||||
|
||||
RTCVideoRenderer getBackRenderer() => _renderer;
|
||||
RTCVideoRenderer getLeftRenderer() => _renderer;
|
||||
RTCVideoRenderer getRightRenderer() => _renderer;
|
||||
|
||||
Widget _buildQuadrantView({required Alignment alignment, required RTCVideoRenderer renderer}) {
|
||||
if (renderer.srcObject == null) return Container(color: Colors.black);
|
||||
|
||||
if (renderer == _secondRenderer) {
|
||||
// 第二条流完整显示
|
||||
return RTCVideoView(renderer,
|
||||
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover, mirror: false);
|
||||
}
|
||||
|
||||
// 第一个流四分屏
|
||||
return ClipRect(
|
||||
child: FractionallySizedBox(
|
||||
widthFactor: 2.0,
|
||||
heightFactor: 2.0,
|
||||
alignment: alignment,
|
||||
child: RTCVideoView(renderer,
|
||||
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitCover, mirror: false),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMainViewWithFeathering() {
|
||||
final renderer = _isFrontMain ? getFrontRenderer() : getBackRenderer();
|
||||
|
||||
return Stack(
|
||||
children: [
|
||||
// 背景层(虚化 + 半透明黑色)
|
||||
Positioned.fill(
|
||||
child: _buildQuadrantView(
|
||||
alignment: _isFrontMain ? Alignment.topLeft : Alignment.topRight,
|
||||
renderer: renderer,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 主视角的矩形边缘羽化效果
|
||||
Widget _buildMainViewWithFeathering() {
|
||||
return ShaderMask(
|
||||
shaderCallback: (Rect bounds) {
|
||||
return const LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [
|
||||
Colors.transparent,
|
||||
Colors.white,
|
||||
Colors.white,
|
||||
Colors.transparent,
|
||||
],
|
||||
stops: [0.0, 0.15, 0.85, 1.0],
|
||||
).createShader(bounds);
|
||||
},
|
||||
blendMode: BlendMode.dstIn,
|
||||
child: ShaderMask(
|
||||
shaderCallback: (Rect bounds) {
|
||||
return const LinearGradient(
|
||||
begin: Alignment.centerLeft,
|
||||
end: Alignment.centerRight,
|
||||
colors: [
|
||||
Colors.transparent,
|
||||
Colors.white,
|
||||
Colors.white,
|
||||
Colors.transparent,
|
||||
],
|
||||
stops: [0.0, 0.05, 0.95, 1.0],
|
||||
).createShader(bounds);
|
||||
},
|
||||
blendMode: BlendMode.dstIn,
|
||||
child: _buildQuadrantView(
|
||||
alignment: _isFrontMain ? Alignment.topLeft : Alignment.topRight,
|
||||
Positioned.fill(
|
||||
child: BackdropFilter(
|
||||
filter: ImageFilter.blur(sigmaX: 45, sigmaY: 45),
|
||||
child: Container(color: Colors.black.withOpacity(0.55)),
|
||||
),
|
||||
),
|
||||
),
|
||||
// 中心主视角 + 羽化
|
||||
Center(
|
||||
child: ShaderMask(
|
||||
shaderCallback: (Rect bounds) {
|
||||
return const LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [Colors.transparent, Colors.white, Colors.white, Colors.transparent],
|
||||
stops: [0.0, 0.15, 0.85, 1.0],
|
||||
).createShader(bounds);
|
||||
},
|
||||
blendMode: BlendMode.dstIn,
|
||||
child: ShaderMask(
|
||||
shaderCallback: (Rect bounds) {
|
||||
return const LinearGradient(
|
||||
begin: Alignment.centerLeft,
|
||||
end: Alignment.centerRight,
|
||||
colors: [Colors.transparent, Colors.white, Colors.white, Colors.transparent],
|
||||
stops: [0.0, 0.05, 0.95, 1.0],
|
||||
).createShader(bounds);
|
||||
},
|
||||
blendMode: BlendMode.dstIn,
|
||||
child: AspectRatio(
|
||||
aspectRatio: 16 / 9,
|
||||
child: _buildQuadrantView(
|
||||
alignment: _isFrontMain ? Alignment.topLeft : Alignment.topRight,
|
||||
renderer: renderer,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!_isInitialized)
|
||||
return const Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
body: Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
body: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final double pipW = constraints.maxWidth / 5;
|
||||
final double pipH = pipW * 9 / 16;
|
||||
|
||||
// 核心:处理 Windows 窗口大小变化时的坐标修正
|
||||
Future.microtask(() {
|
||||
if (!mounted) return;
|
||||
_correctPosition(_leftPosNotifier, constraints, pipW, pipH);
|
||||
_correctPosition(_rightPosNotifier, constraints, pipW, pipH);
|
||||
});
|
||||
|
||||
if (!_isPosInitialized) {
|
||||
_leftPosNotifier.value = const Offset(20, 80);
|
||||
_rightPosNotifier.value = Offset(
|
||||
constraints.maxWidth - pipW - 20,
|
||||
80,
|
||||
);
|
||||
_isPosInitialized = true;
|
||||
}
|
||||
|
||||
return Stack(
|
||||
children: [
|
||||
// ① 背景虚化层(背景两侧模糊,模拟视觉聚焦)
|
||||
Positioned.fill(
|
||||
child: Stack(
|
||||
children: [
|
||||
_buildQuadrantView(
|
||||
alignment: _isFrontMain
|
||||
? Alignment.topLeft
|
||||
: Alignment.topRight,
|
||||
),
|
||||
Positioned.fill(
|
||||
child: BackdropFilter(
|
||||
filter: ImageFilter.blur(sigmaX: 45, sigmaY: 45),
|
||||
child: Container(color: Colors.black.withOpacity(0.55)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// ② 主视频层
|
||||
Center(
|
||||
child: GestureDetector(
|
||||
onDoubleTap: () =>
|
||||
setState(() => _isFrontMain = !_isFrontMain),
|
||||
child: AspectRatio(
|
||||
aspectRatio: 16 / 9,
|
||||
child: _buildMainViewWithFeathering(),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// ③ 悬浮小窗 - 解决阻尼感的 1:1 稳定拖拽
|
||||
if (widget.showLeftPip)
|
||||
_buildFastPip(
|
||||
_leftPosNotifier,
|
||||
Alignment.bottomLeft,
|
||||
constraints,
|
||||
pipW,
|
||||
pipH,
|
||||
),
|
||||
if (widget.showRightPip)
|
||||
_buildFastPip(
|
||||
_rightPosNotifier,
|
||||
Alignment.bottomRight,
|
||||
constraints,
|
||||
pipW,
|
||||
pipH,
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _correctPosition(
|
||||
ValueNotifier<Offset> notifier,
|
||||
BoxConstraints constraints,
|
||||
double w,
|
||||
double h,
|
||||
) {
|
||||
void _correctPosition(ValueNotifier<Offset> notifier, BoxConstraints constraints, double w, double h) {
|
||||
double maxX = (constraints.maxWidth - w).clamp(0.0, double.infinity);
|
||||
double maxY = (constraints.maxHeight - h).clamp(0.0, double.infinity);
|
||||
double newX = notifier.value.dx.clamp(0.0, maxX);
|
||||
@@ -278,14 +258,7 @@ class _WebRTCLocalPlayerState extends State<WebRTCLocalPlayer> {
|
||||
}
|
||||
}
|
||||
|
||||
// 极致丝滑的局部刷新组件
|
||||
Widget _buildFastPip(
|
||||
ValueNotifier<Offset> notifier,
|
||||
Alignment align,
|
||||
BoxConstraints constraints,
|
||||
double w,
|
||||
double h,
|
||||
) {
|
||||
Widget _buildFastPip(ValueNotifier<Offset> notifier, Alignment align, BoxConstraints constraints, double w, double h) {
|
||||
return ValueListenableBuilder<Offset>(
|
||||
valueListenable: notifier,
|
||||
builder: (context, pos, child) {
|
||||
@@ -295,7 +268,6 @@ class _WebRTCLocalPlayerState extends State<WebRTCLocalPlayer> {
|
||||
child: GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onPanUpdate: (details) {
|
||||
// 关键修正:使用 notifier.value.dx 替换 pos.dx,消除位移丢失导致的阻尼感
|
||||
final double nextX = (notifier.value.dx + details.delta.dx).clamp(
|
||||
0.0,
|
||||
(constraints.maxWidth - w).clamp(0.0, double.infinity),
|
||||
@@ -315,16 +287,57 @@ class _WebRTCLocalPlayerState extends State<WebRTCLocalPlayer> {
|
||||
height: h,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.5),
|
||||
blurRadius: 20,
|
||||
offset: const Offset(0, 8),
|
||||
),
|
||||
],
|
||||
boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.5), blurRadius: 20, offset: const Offset(0, 8))],
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: _buildQuadrantView(alignment: align),
|
||||
child: _buildQuadrantView(alignment: align, renderer: _renderer),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!_isInitialized) {
|
||||
return const Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
body: Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
body: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final double pipW = constraints.maxWidth / 5;
|
||||
final double pipH = pipW * 9 / 16;
|
||||
|
||||
Future.microtask(() {
|
||||
if (!mounted) return;
|
||||
_correctPosition(_leftPosNotifier, constraints, pipW, pipH);
|
||||
_correctPosition(_rightPosNotifier, constraints, pipW, pipH);
|
||||
});
|
||||
|
||||
if (!_isPosInitialized) {
|
||||
_leftPosNotifier.value = const Offset(20, 80);
|
||||
_rightPosNotifier.value = Offset(constraints.maxWidth - pipW - 20, 80);
|
||||
_isPosInitialized = true;
|
||||
}
|
||||
|
||||
return Stack(
|
||||
children: [
|
||||
Center(
|
||||
child: GestureDetector(
|
||||
onDoubleTap: () => setState(() => _isFrontMain = !_isFrontMain),
|
||||
child: _buildMainViewWithFeathering(),
|
||||
),
|
||||
),
|
||||
if (widget.showLeftPip)
|
||||
_buildFastPip(_leftPosNotifier, Alignment.bottomLeft, constraints, pipW, pipH),
|
||||
if (widget.showRightPip)
|
||||
_buildFastPip(_rightPosNotifier, Alignment.bottomRight, constraints, pipW, pipH),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user