import 'dart:async'; import 'package:agora_rtc_engine/agora_rtc_engine.dart'; import 'package:audioplayers/audioplayers.dart'; import 'package:dating_touchme_app/generated/assets.dart'; import 'package:dating_touchme_app/im/im_manager.dart'; import 'package:dating_touchme_app/model/rtc/rtc_channel_data.dart'; import 'package:dating_touchme_app/network/network_service.dart'; import 'package:dating_touchme_app/rtc/rtc_manager.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; import 'package:im_flutter_sdk/im_flutter_sdk.dart'; import 'chat_controller.dart'; /// 通话类型 enum CallType { voice, // 语音通话 video, // 视频通话 } /// 通话状态 enum CallStatus { waitCalling, // 等待接通 calling, // 通话中 missed, // 未接听 cancelled, // 已取消 rejected, // 已拒绝 } /// 通话会话信息 class CallSession { final String targetUserId; final CallType callType; final CallStatus status; final bool isInitiator; // 是否是发起方 final DateTime startTime; DateTime? endTime; CallSession({ required this.targetUserId, required this.callType, required this.status, required this.isInitiator, required this.startTime, this.endTime, }); /// 获取通话时长(秒) int get duration { final end = endTime ?? DateTime.now(); return end.difference(startTime).inSeconds; } } /// 通话相关控制器 class CallController extends GetxController { static CallController? _instance; static CallController get instance { _instance ??= Get.put(CallController()); return _instance!; } CallController({NetworkService? networkService}) : _networkService = networkService ?? Get.find(); final NetworkService _networkService; /// 当前频道信息 final Rxn rtcChannel = Rxn(); /// 是否正在创建频道 final RxBool isCreatingChannel = false.obs; // 当前正在进行的通话 final Rx currentCall = Rx(null); // 通话计时器(用于记录通话时长) Timer? _callTimer; int _callDurationSeconds = 0; final RxInt callDurationSeconds = RxInt(0); // 音频播放器(用于播放来电铃声) final AudioPlayer _callAudioPlayer = AudioPlayer(); bool _isPlayingCallAudio = false; @override void onInit() { super.onInit(); print('📞 [CallController] 通话控制器已初始化'); // 监听音频播放完成事件,实现循环播放 _callAudioPlayer.onPlayerComplete.listen((_) async { if (_isPlayingCallAudio) { // 如果还在播放状态,重新播放(循环播放) try { await _callAudioPlayer.play(AssetSource(Assets.audioCall.replaceFirst('assets/', ''))); } catch (e) { print('❌ [CallController] 循环播放来电铃声失败: $e'); _isPlayingCallAudio = false; } } }); } /// 创建一对一RTC频道 /// [type] 1为音频,2为视频 Future createOneOnOneRtcChannel({required int type}) async { if (isCreatingChannel.value) { print('⚠️ 正在创建频道,请稍候'); return null; } // 验证 type 参数 if (type != 1 && type != 2) { SmartDialog.showToast('类型参数错误:1为音频,2为视频'); return null; } isCreatingChannel.value = true; try { final response = await _networkService.rtcApi.createOneOnOneRtcChannel({ 'type': type, }); if (response.data.isSuccess && response.data.data != null) { rtcChannel.value = response.data.data; print('✅ 创建一对一RTC频道成功: ${response.data.data?.channelId}'); return response.data.data; } else { final message = response.data.message.isNotEmpty ? response.data.message : '创建频道失败'; SmartDialog.showToast(message); print('❌ 创建一对一RTC频道失败: $message'); return null; } } catch (e) { final errorMessage = '创建频道异常:$e'; SmartDialog.showToast(errorMessage); print('❌ $errorMessage'); return null; } finally { isCreatingChannel.value = false; } } /// 创建音频通话频道 Future createAudioChannel() { return createOneOnOneRtcChannel(type: 1); } /// 创建视频通话频道 Future createVideoChannel() { return createOneOnOneRtcChannel(type: 2); } /// 发起通话 /// [targetUserId] 目标用户ID /// [callType] 通话类型:语音或视频 /// [chatController] 聊天控制器,用于发送通话消息 Future initiateCall({ required String targetUserId, required CallType callType, ChatController? chatController, }) async { try { if (currentCall.value != null) { SmartDialog.showToast('已有通话正在进行中'); return false; } print('📞 [CallController] 发起${callType == CallType.video ? "视频" : "语音"}通话,目标用户: $targetUserId'); // 发起通话前,先创建一对一 RTC 频道 final type = callType == CallType.video ? 2 : 1; // 1为音频,2为视频 final channelData = await createOneOnOneRtcChannel(type: type); if (channelData == null) { print('❌ [CallController] 创建RTC频道失败,无法发起通话'); SmartDialog.showToast('创建通话频道失败'); return false; } print('✅ [CallController] RTC频道创建成功: ${channelData.channelId}'); // 创建通话会话 final session = CallSession( targetUserId: targetUserId, callType: callType, status: CallStatus.calling, isInitiator: true, startTime: DateTime.now(), ); currentCall.value = session; // 发送通话消息(发起) final callTypeStr = callType == CallType.video ? 'video' : 'voice'; await _sendCallMessage( targetUserId: targetUserId, callType: callTypeStr, callStatus: 'waitCalling', // 初始状态为未接听,等待对方响应 channelId: channelData.channelId, // 传递频道ID chatController: chatController, ); startCallAudio(); // 根据通话类型设置摄像头状态 try { if (callType == CallType.voice) { // 语音通话:禁用视频(关闭摄像头) await RTCManager.instance.disableVideo(); print('📞 [CallController] 语音通话,已关闭摄像头'); } else { // 视频通话:启用视频(打开摄像头) await RTCManager.instance.enableVideo(); print('📞 [CallController] 视频通话,已打开摄像头'); } } catch (e) { print('⚠️ [CallController] 设置视频状态失败: $e'); // 继续执行,不阻止加入频道 } // 加入 RTC 频道,发起真正的通话 try { await RTCManager.instance.joinChannel( token: channelData.token, channelId: channelData.channelId, uid: channelData.uid, role: ClientRoleType.clientRoleBroadcaster, rtcType: RTCType.call, ); print('✅ [CallController] 已加入 RTC 频道: ${channelData.channelId}'); } catch (e) { print('❌ [CallController] 加入 RTC 频道失败: $e'); SmartDialog.showToast('加入通话频道失败'); currentCall.value = null; return false; } return true; } catch (e) { print('❌ [CallController] 发起通话失败: $e'); SmartDialog.showToast('发起通话失败: $e'); currentCall.value = null; return false; } } /// 接听通话 /// [message] 通话消息 /// [chatController] 聊天控制器,用于更新通话消息 Future acceptCall({ required EMMessage message, ChatController? chatController, }) async { try { final callInfo = _parseCallInfo(message); if (callInfo == null) { return false; } final targetUserId = message.from ?? ''; final callTypeStr = callInfo['callType'] as String?; final callType = callTypeStr == 'video' ? CallType.video : CallType.voice; print('📞 [CallController] 接听${callType == CallType.video ? "视频" : "语音"}通话'); // 创建通话会话 final session = CallSession( targetUserId: targetUserId, callType: callType, status: CallStatus.calling, isInitiator: false, startTime: DateTime.now(), ); currentCall.value = session; // 停止播放来电铃声(已接通) stopCallAudio(); // 开始计时 _startCallTimer(); // 更新通话消息状态为通话中 await _updateCallMessageStatus( message: message, callStatus: 'calling', callDuration: 0, chatController: chatController, ); // TODO: 这里可以集成实际的通话SDK,接听通话 // 例如:await RTCManager.instance.acceptCall(targetUserId, callType); return true; } catch (e) { print('❌ [CallController] 接听通话失败: $e'); SmartDialog.showToast('接听通话失败: $e'); return false; } } /// 拒绝通话 /// [message] 通话消息 /// [chatController] 聊天控制器,用于更新通话消息 Future rejectCall({ required EMMessage message, ChatController? chatController, }) async { try { print('📞 [CallController] 拒绝通话'); // 停止播放来电铃声(已拒绝) stopCallAudio(); // 更新通话消息状态为已拒绝 await _updateCallMessageStatus( message: message, callStatus: 'rejected', chatController: chatController, ); // 清理通话会话 currentCall.value = null; // TODO: 这里可以集成实际的通话SDK,拒绝通话 // 例如:await RTCManager.instance.rejectCall(message.from ?? ''); return true; } catch (e) { print('❌ [CallController] 拒绝通话失败: $e'); SmartDialog.showToast('拒绝通话失败: $e'); return false; } } /// 取消通话 /// [message] 通话消息(可选,如果是发起方取消) /// [chatController] 聊天控制器,用于更新通话消息 Future cancelCall({ EMMessage? message, ChatController? chatController, }) async { try { print('📞 [CallController] 取消通话'); // 如果有消息,更新通话消息状态为已取消 if (message != null) { await _updateCallMessageStatus( message: message, callStatus: 'cancelled', chatController: chatController, ); } // 停止播放来电铃声(已取消) stopCallAudio(); // 停止计时 _stopCallTimer(); // 清理通话会话 currentCall.value = null; // TODO: 这里可以集成实际的通话SDK,取消通话 // 例如:await RTCManager.instance.cancelCall(); return true; } catch (e) { print('❌ [CallController] 取消通话失败: $e'); SmartDialog.showToast('取消通话失败: $e'); return false; } } /// 结束通话(通话完成) /// [callDuration] 通话时长(秒) /// [chatController] 聊天控制器,用于更新通话消息 Future endCall({ required int callDuration, EMMessage? message, ChatController? chatController, }) async { try { print('📞 [CallController] 结束通话,时长: ${callDuration}秒'); // 停止播放来电铃声(通话结束) stopCallAudio(); // 停止计时 _stopCallTimer(); // 如果有消息,更新通话消息状态为通话中(显示时长) if (message != null) { await _updateCallMessageStatus( message: message, callStatus: 'calling', callDuration: callDuration, chatController: chatController, ); } // 清理通话会话 currentCall.value = null; // TODO: 这里可以集成实际的通话SDK,结束通话 // 例如:await RTCManager.instance.endCall(); return true; } catch (e) { print('❌ [CallController] 结束通话失败: $e'); SmartDialog.showToast('结束通话失败: $e'); return false; } } /// 开始通话计时 void _startCallTimer() { _callDurationSeconds = 0; callDurationSeconds.value = 0; _callTimer?.cancel(); _callTimer = Timer.periodic(Duration(seconds: 1), (timer) { _callDurationSeconds++; callDurationSeconds.value = _callDurationSeconds; }); } /// 停止通话计时 void _stopCallTimer() { _callTimer?.cancel(); _callTimer = null; _callDurationSeconds = 0; callDurationSeconds.value = 0; } /// 发送通话消息 Future _sendCallMessage({ required String targetUserId, required String callType, required String callStatus, int? callDuration, String? channelId, ChatController? chatController, }) async { try { // 如果提供了 chatController,使用它发送消息 if (chatController != null) { return await chatController.sendCallMessage( callType: callType, callStatus: callStatus, callDuration: callDuration, channelId: channelId, ); } // 否则直接通过 IMManager 发送自定义消息 final callParams = { 'callType': callType, 'callStatus': callStatus, }; if (callDuration != null) { callParams['callDuration'] = callDuration.toString(); } if (channelId != null && channelId.isNotEmpty) { callParams['channelId'] = channelId; } final message = await IMManager.instance.sendCustomMessage( targetUserId, 'call', callParams, ); return message != null; } catch (e) { print('❌ [CallController] 发送通话消息失败: $e'); return false; } } /// 更新通话消息状态(使用modifyMessage修改现有消息) Future _updateCallMessageStatus({ required EMMessage message, required String callStatus, int? callDuration, ChatController? chatController, }) async { try { // 解析现有通话信息 final callInfo = _parseCallInfo(message); if (callInfo == null) { return false; } final callType = callInfo['callType'] as String? ?? 'voice'; final messageId = message.msgId; if (messageId.isEmpty) { print('❌ [CallController] 消息ID为空,无法修改消息'); return false; } // 如果是自定义消息,使用modifyMessage修改 if (message.body.type == MessageType.CUSTOM) { // 构建新的参数 final callParams = { 'callType': callType, 'callStatus': callStatus, }; if (callDuration != null) { callParams['callDuration'] = callDuration.toString(); } // 创建新的消息体 final customBody = EMCustomMessageBody( event: 'call', params: callParams, ); // 使用modifyMessage修改消息 final success = await IMManager.instance.modifyMessage( messageId: messageId, msgBody: customBody, attributes: null, // 不修改扩展属性 ); if (success) { print('✅ [CallController] 消息修改成功: messageId=$messageId, callStatus=$callStatus'); // 如果提供了chatController,更新本地消息列表 if (chatController != null) { // 更新消息体中的参数 try { final index = chatController.messages.indexWhere((msg) => msg.msgId == messageId); if (index != -1) { final updatedMessage = chatController.messages[index]; if (updatedMessage.body.type == MessageType.CUSTOM) { final customBody = updatedMessage.body as EMCustomMessageBody; // 创建新的参数Map并更新 final updatedParams = Map.from(customBody.params ?? {}); updatedParams['callType'] = callType; updatedParams['callStatus'] = callStatus; if (callDuration != null) { updatedParams['callDuration'] = callDuration.toString(); } // 注意:EMCustomMessageBody的params可能是只读的,这里可能需要重新创建消息 // 暂时先通知UI更新,实际的消息体更新会在收到onMessageContentChanged回调时处理 chatController.update(); } } } catch (e) { print('⚠️ [CallController] 更新本地消息列表失败: $e'); } } } return success; } // 如果不是自定义消息,返回失败 return false; } catch (e) { print('❌ [CallController] 更新通话消息状态失败: $e'); return false; } } /// 从自定义消息中解析通话信息 Map? _parseCallInfo(EMMessage message) { try { if (message.body.type == MessageType.CUSTOM) { final customBody = message.body as EMCustomMessageBody; if (customBody.event == 'call' && customBody.params != null) { final params = customBody.params!; return { 'callType': params['callType'] ?? 'voice', 'callStatus': params['callStatus'] ?? 'missed', 'callDuration': params['callDuration'] != null ? int.tryParse(params['callDuration']!) : null, }; } } } catch (e) { print('解析通话信息失败: $e'); } return null; } /// 检查是否有正在进行的通话 bool get isInCall => currentCall.value != null; /// 获取当前通话时长(秒) int get currentCallDuration => callDurationSeconds.value; /// 开始播放来电铃声(循环播放) /// 可以是发起方或接收方调用 Future startCallAudio() async { if (_isPlayingCallAudio) { return; // 已经在播放中 } try { _isPlayingCallAudio = true; print('🔊 [CallController] 开始播放来电铃声'); await _callAudioPlayer.play(AssetSource(Assets.audioCall.replaceFirst('assets/', ''))); } catch (e) { print('❌ [CallController] 播放来电铃声失败: $e'); _isPlayingCallAudio = false; } } /// 停止播放来电铃声 /// 可以是发起方或接收方调用 Future stopCallAudio() async { if (!_isPlayingCallAudio) { return; // 没有在播放 } try { _isPlayingCallAudio = false; print('🔇 [CallController] 停止播放来电铃声'); await _callAudioPlayer.stop(); } catch (e) { print('❌ [CallController] 停止播放来电铃声失败: $e'); } } @override void onClose() { stopCallAudio(); _stopCallTimer(); currentCall.value = null; rtcChannel.value = null; _callAudioPlayer.dispose(); super.onClose(); } }