import 'package:agora_rtc_engine/agora_rtc_engine.dart' as agora; import 'package:flutter/material.dart'; import 'package:maibu_satabot_v2/features/remote_control/presentation/widgets/webrtc/webrtc_local_player.dart'; import '../../domain/video/pv_camera_direction.dart'; import '../models/pv_video_fit.dart'; import '../models/pv_video_source.dart'; import '../video/pv_agora_scope.dart'; import 'pv_local_rtsp_video.dart'; import 'pv_usb_camera_video.dart'; /// Front/rear interaction for the overview video area; the manual HUD owns its /// own direction selection so its small preview can display the opposite view. class PvSwitchableCameraView extends StatefulWidget { const PvSwitchableCameraView({ super.key, required this.source, required this.fit, }); final PvVideoSource source; final PvVideoFit fit; @override State createState() => _PvSwitchableCameraViewState(); } class _PvSwitchableCameraViewState extends State { bool _rear = false; @override Widget build(BuildContext context) => GestureDetector( behavior: HitTestBehavior.opaque, onDoubleTap: () => setState(() => _rear = !_rear), child: Stack( fit: StackFit.expand, children: [ PvCameraView( source: widget.source, fit: widget.fit, direction: _rear ? PvCameraDirection.rear : PvCameraDirection.front, ), Positioned( bottom: 8, right: 8, child: IgnorePointer( child: Text( '${_rear ? "后视" : "前视"} · 双击切换前后', style: const TextStyle( color: Colors.white, backgroundColor: Colors.black54, fontSize: 12, ), ), ), ), ], ), ); } /// Central transport dispatcher. Camera identity stays separate from video /// source selection, so another transport can implement the same four views. class PvCameraView extends StatelessWidget { const PvCameraView({ super.key, required this.source, this.direction = PvCameraDirection.front, this.streamUrl = '', this.fit = PvVideoFit.contain, }); final PvVideoSource source; final PvCameraDirection direction; final String streamUrl; final PvVideoFit fit; @override Widget build(BuildContext context) => switch (source) { PvVideoSource.agoraRtc => _AgoraCameraView(direction: direction, fit: fit), PvVideoSource.usbCamera => const PvUsbCameraVideo(), PvVideoSource.localRtsp => const PvLocalRtspVideo(), PvVideoSource.webrtc => streamUrl.isNotEmpty ? WebRTCLocalPlayer( streamUrl: streamUrl, showLeftPip: false, showRightPip: false, mainViewAlignment: Alignment.center, ) : const ColoredBox( color: Colors.black87, child: Center( child: Icon( Icons.videocam_off, size: 48, color: Colors.white38, ), ), ), }; } class _AgoraCameraView extends StatelessWidget { const _AgoraCameraView({required this.direction, required this.fit}); final PvCameraDirection direction; final PvVideoFit fit; @override Widget build(BuildContext context) { final session = PvAgoraScope.of(context); return AnimatedBuilder( animation: session, builder: (context, _) { final config = session.config; final engine = session.engine; final uid = config?.uidFor(direction); final canRender = engine != null && session.joined && uid != null; final playing = uid != null && session.hasVideo(uid); return ColoredBox( color: Colors.black, child: Stack( fit: StackFit.expand, children: [ if (canRender) IgnorePointer( child: agora.AgoraVideoView( key: ValueKey('agora/${session.generation}/$uid'), controller: agora.VideoViewController.remote( rtcEngine: engine, canvas: agora.VideoCanvas( uid: uid, setupMode: agora.VideoViewSetupMode.videoViewSetupAdd, renderMode: fit == PvVideoFit.cover ? agora.RenderModeType.renderModeHidden : agora.RenderModeType.renderModeFit, ), connection: agora.RtcConnection( channelId: config!.channel, ), useAndroidSurfaceView: false, ), ), ), if (!playing) Center( child: Padding( padding: const EdgeInsets.all(12), child: Text( session.message ?? '${direction.label} · UID $uid\n等待该路视频上线', textAlign: TextAlign.center, style: const TextStyle( color: Colors.white70, fontSize: 12, ), ), ), ), if (playing && session.tokenWarning != null) Align( alignment: Alignment.bottomCenter, child: Text( session.tokenWarning!, style: const TextStyle(color: Colors.amber, fontSize: 11), ), ), ], ), ); }, ); } }