mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2025-04-20 00:47:54 +02:00
214 lines
5.9 KiB
Dart
214 lines
5.9 KiB
Dart
import 'dart:core';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
|
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
|
|
import 'package:flutter_webrtc/flutter_webrtc.dart' as webrtc_impl;
|
|
import 'package:matrix/matrix.dart';
|
|
import 'package:webrtc_interface/webrtc_interface.dart' hide Navigator;
|
|
|
|
import 'package:fluffychat/pages/dialer/dialer.dart';
|
|
import 'package:fluffychat/utils/famedlysdk_store.dart';
|
|
import '../../utils/voip/user_media_manager.dart';
|
|
|
|
class VoipPlugin extends WidgetsBindingObserver implements WebRTCDelegate {
|
|
VoipPlugin({required this.client, required this.context}) {
|
|
voip = VoIP(client, this);
|
|
try {
|
|
Connectivity()
|
|
.onConnectivityChanged
|
|
.listen(_handleNetworkChanged)
|
|
.onError((e) => _currentConnectivity = ConnectivityResult.none);
|
|
} catch (e, s) {
|
|
Logs().w('Could not subscribe network updates', e, s);
|
|
}
|
|
Connectivity()
|
|
.checkConnectivity()
|
|
.then((result) => _currentConnectivity = result)
|
|
.catchError((e) => _currentConnectivity = ConnectivityResult.none);
|
|
if (!kIsWeb) {
|
|
final wb = WidgetsBinding.instance;
|
|
wb.addObserver(this);
|
|
didChangeAppLifecycleState(wb.lifecycleState!);
|
|
}
|
|
}
|
|
|
|
final Client client;
|
|
bool background = false;
|
|
bool speakerOn = false;
|
|
late VoIP voip;
|
|
ConnectivityResult? _currentConnectivity;
|
|
ValueChanged<CallSession>? onIncomingCall;
|
|
OverlayEntry? overlayEntry;
|
|
|
|
// hacky workaround: in order to have [Overlay.of] working on web, the context
|
|
// mus explicitly be re-assigned
|
|
//
|
|
// hours wasted: 5
|
|
BuildContext context;
|
|
CallSession? get currentCall =>
|
|
voip.currentCID == null ? null : voip.calls[voip.currentCID];
|
|
|
|
GroupCall? get currentGroupCall => voip.currentGroupCID == null
|
|
? null
|
|
: voip.groupCalls[voip.currentGroupCID];
|
|
|
|
bool inMeeting = false;
|
|
|
|
void start() {
|
|
voip.startGroupCalls();
|
|
}
|
|
|
|
bool getInMeetingState() {
|
|
return currentCall != null || currentGroupCall != null;
|
|
}
|
|
|
|
String? get currentMeetingRoomId {
|
|
return currentCall?.room.id ?? currentGroupCall?.room.id;
|
|
}
|
|
|
|
CallSession? get call {
|
|
if (voip.currentCID != null) {
|
|
final call = voip.calls[voip.currentCID];
|
|
if (call != null && call.groupCallId != null) {
|
|
return call;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
GroupCall? get groupCall {
|
|
if (voip.currentCID != null) {
|
|
return voip.groupCalls[voip.currentGroupCID];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
void _handleNetworkChanged(ConnectivityResult result) async {
|
|
/// Got a new connectivity status!
|
|
if (_currentConnectivity != result) {
|
|
voip.calls.forEach((_, sess) {
|
|
sess.restartIce();
|
|
});
|
|
}
|
|
_currentConnectivity = result;
|
|
}
|
|
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
Logs().v('AppLifecycleState = $state');
|
|
background = !(state != AppLifecycleState.detached &&
|
|
state != AppLifecycleState.paused);
|
|
}
|
|
|
|
void addCallingOverlay(BuildContext context) {
|
|
Logs().d('[VOIP] addCallingOverlay: adding overlay');
|
|
if (overlayEntry != null) {
|
|
Logs().w('[VOIP] addCallingOverlay: The call session already exists?');
|
|
overlayEntry?.remove();
|
|
}
|
|
// Overlay.of(context) is broken on web
|
|
// falling back on a dialog
|
|
if (kIsWeb) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => Calling(
|
|
voipPlugin: this,
|
|
onClear: () => Navigator.of(context).pop(),
|
|
),
|
|
);
|
|
} else {
|
|
overlayEntry = OverlayEntry(
|
|
builder: (_) => Calling(
|
|
voipPlugin: this,
|
|
onClear: () {
|
|
overlayEntry?.remove();
|
|
overlayEntry = null;
|
|
}),
|
|
);
|
|
Overlay.of(context)!.insert(overlayEntry!);
|
|
}
|
|
Logs().d('[VOIP] addCallingOverlay: adding done');
|
|
}
|
|
|
|
@override
|
|
MediaDevices get mediaDevices => webrtc_impl.navigator.mediaDevices;
|
|
|
|
@override
|
|
bool get isBackgroud => background;
|
|
|
|
@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();
|
|
}
|
|
|
|
@override
|
|
void playRingtone() async {
|
|
if (!background) {
|
|
try {
|
|
await UserMediaManager().startRingingTone();
|
|
} catch (_) {}
|
|
}
|
|
}
|
|
|
|
@override
|
|
void stopRingtone() async {
|
|
if (!background) {
|
|
try {
|
|
await UserMediaManager().stopRingingTone();
|
|
} catch (_) {}
|
|
}
|
|
}
|
|
|
|
@override
|
|
void handleNewCall(CallSession call) async {
|
|
Logs().d('[VOIP] handling new call');
|
|
|
|
/// Popup CallingPage for incoming call.
|
|
addCallingOverlay(context);
|
|
Logs().d('[VOIP] overlay stuff should be there up there');
|
|
}
|
|
|
|
@override
|
|
void handleCallEnded(CallSession session) async {
|
|
if (overlayEntry != null) {
|
|
overlayEntry?.remove();
|
|
overlayEntry = null;
|
|
FlutterForegroundTask.setOnLockScreenVisibility(false);
|
|
final wasForeground = await Store().getItemBool('wasForeground');
|
|
!wasForeground ? FlutterForegroundTask.minimizeApp() : null;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void handleNewGroupCall(GroupCall groupCall) {
|
|
Logs().d('[VOIP] group handling new call');
|
|
|
|
/// Popup CallingPage for incoming call.
|
|
addCallingOverlay(context);
|
|
Logs().d('[VOIP] group overlay stuff should be there up there');
|
|
}
|
|
|
|
@override
|
|
void handleGroupCallEnded(GroupCall groupCall) {
|
|
if (overlayEntry != null) {
|
|
overlayEntry?.remove();
|
|
overlayEntry = null;
|
|
// FlutterForegroundTask.setOnLockScreenVisibility(false);
|
|
// final wasForeground = await Store().getItemBool('wasForeground');
|
|
// !wasForeground ? FlutterForegroundTask.minimizeApp() : null;
|
|
}
|
|
}
|
|
}
|