|
|
|
|
@ -1,9 +1,11 @@
|
|
|
|
|
import 'dart:async';
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_callkit_incoming/flutter_callkit_incoming.dart';
|
|
|
|
|
import 'package:flutter_ios_voip_kit/flutter_ios_voip_kit.dart';
|
|
|
|
|
import 'package:flutter_webrtc/flutter_webrtc.dart';
|
|
|
|
|
import 'package:just_audio/just_audio.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/app_state/app_state.dart';
|
|
|
|
|
@ -26,6 +28,7 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
RTCVideoRenderer? remoteRenderer;
|
|
|
|
|
|
|
|
|
|
final AudioPlayer player = AudioPlayer();
|
|
|
|
|
final FlutterIOSVoIPKit voIPKit = FlutterIOSVoIPKit.instance;
|
|
|
|
|
MediaStream? _localStream;
|
|
|
|
|
late CallDataModel outGoingCallData;
|
|
|
|
|
bool isMicOff = false;
|
|
|
|
|
@ -46,8 +49,6 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
|
|
|
|
|
late BuildContext providerContext;
|
|
|
|
|
|
|
|
|
|
List<MediaDeviceInfo> devices = [];
|
|
|
|
|
|
|
|
|
|
void initCallListeners({required BuildContext context}) {
|
|
|
|
|
providerContext = context;
|
|
|
|
|
if (kDebugMode) {
|
|
|
|
|
@ -58,7 +59,7 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
chatHubConnection!.on("OnOfferAsync", onOfferAsync);
|
|
|
|
|
chatHubConnection!.on("OnAnswerOffer", onAnswerOffer);
|
|
|
|
|
chatHubConnection!.on("OnHangUpAsync", onHangUpAsync);
|
|
|
|
|
// chatHubConnection!.on("OnCallDeclinedAsync", onCallDeclinedAsync);
|
|
|
|
|
chatHubConnection!.on("OnCallDeclinedAsync", onCallDeclinedAsync);
|
|
|
|
|
// chatHubConnection!.on("OnIncomingCallAsync", OnIncomingCallAsync);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -121,6 +122,10 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
|
|
|
|
|
// OutGoing Listeners
|
|
|
|
|
void onCallAcceptedAsync(List<Object?>? params) async {
|
|
|
|
|
if (_timer!.isActive && _start > 5) {
|
|
|
|
|
_timer!.cancel();
|
|
|
|
|
_start = 25;
|
|
|
|
|
}
|
|
|
|
|
dynamic items = params!.toList();
|
|
|
|
|
RTCSessionDescription description = await _createOffer();
|
|
|
|
|
await _pc.setLocalDescription(description);
|
|
|
|
|
@ -232,7 +237,8 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
if (chatHubConnection != null && !isUserOnline) {
|
|
|
|
|
chatHubConnection!.stop();
|
|
|
|
|
}
|
|
|
|
|
await FlutterCallkitIncoming.endAllCalls();
|
|
|
|
|
if (Platform.isAndroid) await FlutterCallkitIncoming.endAllCalls();
|
|
|
|
|
if (Platform.isIOS) await voIPKit.endCall();
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
if (isOutGoingCall) {
|
|
|
|
|
@ -289,28 +295,24 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onHangUpAsync(List<Object?>? params) {
|
|
|
|
|
Future<void> onHangUpAsync(List<Object?>? params) async {
|
|
|
|
|
print("--------------------- onHangUp ASYNC ---------------------------------");
|
|
|
|
|
|
|
|
|
|
dynamic items = params!.toList();
|
|
|
|
|
// if (kDebugMode) {
|
|
|
|
|
// logger.i("res: " + items.toString());
|
|
|
|
|
// }
|
|
|
|
|
if (items[0]["id"] != AppState().chatDetails!.response!.id) {
|
|
|
|
|
if (kDebugMode) {
|
|
|
|
|
print("Call Ended By Other User");
|
|
|
|
|
}
|
|
|
|
|
if (Platform.isIOS) await voIPKit.endCall();
|
|
|
|
|
if (Platform.isAndroid) await FlutterCallkitIncoming.endAllCalls();
|
|
|
|
|
if (isIncomingCall) {
|
|
|
|
|
endCall(isUserOnline: isUserOnline).then((bool value) {
|
|
|
|
|
if (isCallConnected && isUserOnline) {
|
|
|
|
|
isCallConnected = false;
|
|
|
|
|
if (!AppState().isLogged) {
|
|
|
|
|
Navigator.of(AppRoutes.navigatorKey.currentContext!).pop();
|
|
|
|
|
Navigator.of(AppRoutes.navigatorKey.currentContext!).canPop();
|
|
|
|
|
} else {
|
|
|
|
|
Navigator.of(AppRoutes.navigatorKey.currentContext!).popUntil(ModalRoute.withName(AppRoutes.dashboard));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Navigator.of(AppRoutes.navigatorKey.currentContext!).pop();
|
|
|
|
|
Navigator.of(AppRoutes.navigatorKey.currentContext!).canPop();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
@ -320,7 +322,8 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
isCallConnected = false;
|
|
|
|
|
Navigator.of(AppRoutes.navigatorKey.currentContext!).popUntil(ModalRoute.withName(AppRoutes.dashboard));
|
|
|
|
|
} else {
|
|
|
|
|
Navigator.of(AppRoutes.navigatorKey.currentContext!).pop();
|
|
|
|
|
// Navigator.of(AppRoutes.navigatorKey.currentContext!).canPop();
|
|
|
|
|
Navigator.of(AppRoutes.navigatorKey.currentContext!).popUntil(ModalRoute.withName(AppRoutes.dashboard));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
@ -330,11 +333,20 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
print("Call Ended By Me");
|
|
|
|
|
}
|
|
|
|
|
if (isOutGoingCall) {
|
|
|
|
|
// if (!isCallConnected) {
|
|
|
|
|
// if (_timer!.isActive && _start == 5) {
|
|
|
|
|
// _timer!.cancel();
|
|
|
|
|
// _start = 25;
|
|
|
|
|
// Navigator.of(AppRoutes.navigatorKey.currentContext!).popUntil(ModalRoute.withName(AppRoutes.chatDetailed));
|
|
|
|
|
// endCall(isUserOnline: isUserOnline);
|
|
|
|
|
// return Utils.showToast("${items[0]["userName"]}" + " has declined call", longDuration: true);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
if (isCallConnected && isUserOnline) {
|
|
|
|
|
isCallConnected = false;
|
|
|
|
|
Navigator.of(AppRoutes.navigatorKey.currentContext!).popUntil(ModalRoute.withName(AppRoutes.chatDetailed));
|
|
|
|
|
} else {
|
|
|
|
|
Navigator.of(AppRoutes.navigatorKey.currentContext!).pop();
|
|
|
|
|
Navigator.of(AppRoutes.navigatorKey.currentContext!).canPop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -363,6 +375,43 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
// });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Outgoing Timer for Decline Call
|
|
|
|
|
|
|
|
|
|
Timer? _timer;
|
|
|
|
|
int _start = 25;
|
|
|
|
|
|
|
|
|
|
void startTimer() {
|
|
|
|
|
Duration oneSec = const Duration(seconds: 1);
|
|
|
|
|
_timer = Timer.periodic(
|
|
|
|
|
oneSec,
|
|
|
|
|
(Timer timer) {
|
|
|
|
|
if (_start == 0) {
|
|
|
|
|
timer.cancel();
|
|
|
|
|
} else {
|
|
|
|
|
print(_start);
|
|
|
|
|
_start--;
|
|
|
|
|
if (isOutGoingCall) {
|
|
|
|
|
if (!isCallConnected) {
|
|
|
|
|
if (_timer!.isActive && _start == 5) {
|
|
|
|
|
_timer!.cancel();
|
|
|
|
|
_start = 25;
|
|
|
|
|
Navigator.of(AppRoutes.navigatorKey.currentContext!).popUntil(ModalRoute.withName(AppRoutes.chatDetailed));
|
|
|
|
|
endCall(isUserOnline: isUserOnline);
|
|
|
|
|
return Utils.showToast("${outGoingCallData.receiverName}" + " has declined call", longDuration: true);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (isCallConnected) {
|
|
|
|
|
_timer!.cancel();
|
|
|
|
|
_start = 25;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Future<void> OnIncomingCallAsync(List<Object?>? params) async {
|
|
|
|
|
// print("--------------------- On Incoming Call ---------------------------------------");
|
|
|
|
|
// dynamic items = params!.toList();
|
|
|
|
|
@ -413,24 +462,16 @@ class ChatCallProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
// // }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
void onCallDeclinedAsync(List<Object?>? params) {
|
|
|
|
|
print("================= On Declained ========================");
|
|
|
|
|
logger.d(params);
|
|
|
|
|
// endCall().then((bool value) {
|
|
|
|
|
// if (value) {
|
|
|
|
|
// isCallEnded = true;
|
|
|
|
|
// notifyListeners();
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// if (params != null) {
|
|
|
|
|
// endCall(isUserOnline: isUserOnline).then((bool value) {
|
|
|
|
|
// if (isCallConnected) {
|
|
|
|
|
// // Navigator.of(AppRoutes.navigatorKey.currentContext!).pop();
|
|
|
|
|
// isCallConnected = false;
|
|
|
|
|
// }
|
|
|
|
|
// isCallEnded = true;
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
Future<void> onCallDeclinedAsync(List<Object?>? params) async {
|
|
|
|
|
print("================= On Declined ========================");
|
|
|
|
|
dynamic items = params!.toList();
|
|
|
|
|
if (isOutGoingCall) {
|
|
|
|
|
Navigator.of(AppRoutes.navigatorKey.currentContext!).popUntil(ModalRoute.withName(AppRoutes.chatDetailed));
|
|
|
|
|
bool val = await endCall(isUserOnline: isUserOnline);
|
|
|
|
|
if (Platform.isIOS) await voIPKit.endCall();
|
|
|
|
|
if (Platform.isAndroid) await FlutterCallkitIncoming.endAllCalls();
|
|
|
|
|
return Utils.showToast("${items[1]["userName"]}" + " has declined call", longDuration: true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//// Invoke Methods
|
|
|
|
|
|