2022-02-15 09:25:13 +01:00
|
|
|
import 'dart:core';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
2022-02-19 09:27:03 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2022-02-15 09:25:13 +01:00
|
|
|
|
|
|
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
2022-09-10 12:12:52 +02:00
|
|
|
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
|
2022-02-15 09:25:13 +01:00
|
|
|
import 'package:flutter_webrtc/flutter_webrtc.dart' as webrtc_impl;
|
|
|
|
import 'package:matrix/matrix.dart';
|
2022-02-19 09:27:03 +01:00
|
|
|
import 'package:webrtc_interface/webrtc_interface.dart' hide Navigator;
|
2022-02-15 09:25:13 +01:00
|
|
|
|
2022-09-10 12:12:52 +02:00
|
|
|
import 'package:fluffychat/pages/chat_list/chat_list.dart';
|
2022-02-15 09:25:13 +01:00
|
|
|
import 'package:fluffychat/pages/dialer/dialer.dart';
|
2022-09-10 12:12:52 +02:00
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
|
|
|
import 'package:fluffychat/widgets/fluffy_chat_app.dart';
|
|
|
|
import '../../utils/famedlysdk_store.dart';
|
|
|
|
import '../../utils/voip/callkeep_manager.dart';
|
2022-02-15 09:25:13 +01:00
|
|
|
import '../../utils/voip/user_media_manager.dart';
|
|
|
|
|
2022-09-10 12:12:52 +02:00
|
|
|
class VoipPlugin with WidgetsBindingObserver implements WebRTCDelegate {
|
|
|
|
final Client client;
|
|
|
|
VoipPlugin(this.client) {
|
2022-02-15 09:25:13 +01:00
|
|
|
voip = VoIP(client, this);
|
2022-09-10 12:12:52 +02:00
|
|
|
Connectivity()
|
|
|
|
.onConnectivityChanged
|
|
|
|
.listen(_handleNetworkChanged)
|
|
|
|
.onError((e) => _currentConnectivity = ConnectivityResult.none);
|
2022-02-15 09:25:13 +01:00
|
|
|
Connectivity()
|
|
|
|
.checkConnectivity()
|
|
|
|
.then((result) => _currentConnectivity = result)
|
|
|
|
.catchError((e) => _currentConnectivity = ConnectivityResult.none);
|
|
|
|
if (!kIsWeb) {
|
|
|
|
final wb = WidgetsBinding.instance;
|
2022-05-12 11:25:34 +02:00
|
|
|
wb.addObserver(this);
|
2022-09-10 12:12:52 +02:00
|
|
|
didChangeAppLifecycleState(wb.lifecycleState);
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bool background = false;
|
|
|
|
bool speakerOn = false;
|
|
|
|
late VoIP voip;
|
|
|
|
ConnectivityResult? _currentConnectivity;
|
|
|
|
OverlayEntry? overlayEntry;
|
|
|
|
|
|
|
|
void _handleNetworkChanged(ConnectivityResult result) async {
|
|
|
|
/// Got a new connectivity status!
|
|
|
|
if (_currentConnectivity != result) {
|
|
|
|
voip.calls.forEach((_, sess) {
|
|
|
|
sess.restartIce();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_currentConnectivity = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-09-10 12:12:52 +02:00
|
|
|
void didChangeAppLifecycleState(AppLifecycleState? state) {
|
2022-02-15 09:25:13 +01:00
|
|
|
Logs().v('AppLifecycleState = $state');
|
2022-09-10 12:12:52 +02:00
|
|
|
background = (state == AppLifecycleState.detached ||
|
|
|
|
state == AppLifecycleState.paused);
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 12:12:52 +02:00
|
|
|
void addCallingOverlay(String callId, CallSession call) {
|
|
|
|
final context = kIsWeb
|
|
|
|
? ChatList.contextForVoip!
|
|
|
|
: FluffyChatApp.routerKey.currentContext!; // web is weird
|
2022-02-15 09:25:13 +01:00
|
|
|
if (overlayEntry != null) {
|
2022-09-10 12:12:52 +02:00
|
|
|
Logs().e('[VOIP] addCallingOverlay: The call session already exists?');
|
|
|
|
overlayEntry!.remove();
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
2022-02-19 09:27:03 +01:00
|
|
|
// Overlay.of(context) is broken on web
|
|
|
|
// falling back on a dialog
|
|
|
|
if (kIsWeb) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => Calling(
|
2022-02-15 09:25:13 +01:00
|
|
|
context: context,
|
|
|
|
client: client,
|
|
|
|
callId: callId,
|
|
|
|
call: call,
|
2022-02-19 09:27:03 +01:00
|
|
|
onClear: () => Navigator.of(context).pop(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
overlayEntry = OverlayEntry(
|
|
|
|
builder: (_) => Calling(
|
|
|
|
context: context,
|
|
|
|
client: client,
|
|
|
|
callId: callId,
|
|
|
|
call: call,
|
|
|
|
onClear: () {
|
|
|
|
overlayEntry?.remove();
|
|
|
|
overlayEntry = null;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
Overlay.of(context)!.insert(overlayEntry!);
|
|
|
|
}
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
MediaDevices get mediaDevices => webrtc_impl.navigator.mediaDevices;
|
|
|
|
|
|
|
|
@override
|
2022-09-10 12:12:52 +02:00
|
|
|
// remove this from sdk once callkeep is stable
|
|
|
|
bool get isBackgroud => false;
|
2022-02-15 09:25:13 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isWeb => kIsWeb;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<RTCPeerConnection> createPeerConnection(
|
|
|
|
Map<String, dynamic> configuration,
|
|
|
|
[Map<String, dynamic> constraints = const {}]) =>
|
|
|
|
webrtc_impl.createPeerConnection(configuration, constraints);
|
|
|
|
|
|
|
|
@override
|
|
|
|
VideoRenderer createRenderer() {
|
|
|
|
return webrtc_impl.RTCVideoRenderer();
|
|
|
|
}
|
|
|
|
|
2022-09-10 12:12:52 +02:00
|
|
|
Future<bool> get hasCallingAccount async =>
|
|
|
|
kIsWeb ? false : await CallKeepManager().hasPhoneAccountEnabled;
|
|
|
|
|
2022-02-15 09:25:13 +01:00
|
|
|
@override
|
|
|
|
void playRingtone() async {
|
2022-09-10 12:12:52 +02:00
|
|
|
if (!background && !await hasCallingAccount) {
|
2022-02-15 09:25:13 +01:00
|
|
|
try {
|
|
|
|
await UserMediaManager().startRingingTone();
|
|
|
|
} catch (_) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void stopRingtone() async {
|
2022-09-10 12:12:52 +02:00
|
|
|
if (!background && !await hasCallingAccount) {
|
2022-02-15 09:25:13 +01:00
|
|
|
try {
|
|
|
|
await UserMediaManager().stopRingingTone();
|
|
|
|
} catch (_) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void handleNewCall(CallSession call) async {
|
2022-09-10 12:12:52 +02:00
|
|
|
if (PlatformInfos.isAndroid) {
|
|
|
|
// probably works on ios too
|
|
|
|
final hasCallingAccount = await CallKeepManager().hasPhoneAccountEnabled;
|
|
|
|
if (call.direction == CallDirection.kIncoming &&
|
|
|
|
hasCallingAccount &&
|
|
|
|
call.type == CallType.kVoice) {
|
|
|
|
///Popup native telecom manager call UI for incoming call.
|
|
|
|
final callKeeper = CallKeeper(CallKeepManager(), call);
|
|
|
|
CallKeepManager().addCall(call.callId, callKeeper);
|
|
|
|
await CallKeepManager().showCallkitIncoming(call);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
final wasForeground = await FlutterForegroundTask.isAppOnForeground;
|
|
|
|
await Store().setItem(
|
|
|
|
'wasForeground', wasForeground == true ? 'true' : 'false');
|
|
|
|
FlutterForegroundTask.setOnLockScreenVisibility(true);
|
|
|
|
FlutterForegroundTask.wakeUpScreen();
|
|
|
|
FlutterForegroundTask.launchApp();
|
|
|
|
} catch (e) {
|
|
|
|
Logs().e('VOIP foreground failed $e');
|
|
|
|
}
|
|
|
|
// use fallback flutter call pages for outgoing and video calls.
|
|
|
|
addCallingOverlay(call.callId, call);
|
|
|
|
try {
|
|
|
|
if (!hasCallingAccount) {
|
|
|
|
ScaffoldMessenger.of(FluffyChatApp.routerKey.currentContext!)
|
|
|
|
.showSnackBar(const SnackBar(
|
|
|
|
content: Text(
|
|
|
|
'No calling accounts found (used for native calls UI)',
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
Logs().e('failed to show snackbar');
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 09:25:13 +01:00
|
|
|
} else {
|
2022-09-10 12:12:52 +02:00
|
|
|
addCallingOverlay(call.callId, call);
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void handleCallEnded(CallSession session) async {
|
|
|
|
if (overlayEntry != null) {
|
2022-09-10 12:12:52 +02:00
|
|
|
overlayEntry!.remove();
|
2022-02-15 09:25:13 +01:00
|
|
|
overlayEntry = null;
|
2022-09-10 12:12:52 +02:00
|
|
|
if (PlatformInfos.isAndroid) {
|
|
|
|
FlutterForegroundTask.setOnLockScreenVisibility(false);
|
|
|
|
FlutterForegroundTask.stopService();
|
|
|
|
final wasForeground = await Store().getItem('wasForeground');
|
|
|
|
wasForeground == 'false' ? FlutterForegroundTask.minimizeApp() : null;
|
|
|
|
}
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
}
|
2022-06-20 15:35:22 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
void handleGroupCallEnded(GroupCall groupCall) {
|
|
|
|
// TODO: implement handleGroupCallEnded
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void handleNewGroupCall(GroupCall groupCall) {
|
|
|
|
// TODO: implement handleNewGroupCall
|
|
|
|
}
|
2022-07-09 10:18:53 +02:00
|
|
|
|
2022-10-12 13:31:29 +02:00
|
|
|
@override
|
|
|
|
// TODO: implement canHandleNewCall
|
|
|
|
bool get canHandleNewCall =>
|
|
|
|
voip.currentCID == null && voip.currentGroupCID == null;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void handleMissedCall(CallSession session) {
|
|
|
|
// TODO: implement handleMissedCall
|
|
|
|
}
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|