I'm working on a Flutter app that incorporates WebRTC for audio/video calls. The WebRTC calls work well when the app is in the foreground, but I'm facing challenges in maintaining the call when the app goes into the background as the OS terminates operation inside the app and kills the voip instance
How I can make it work even when the app goes background or terminated state?
sample code:
class VoipApp with WidgetsBindingObserver implements WebRTCDelegate {
bool background = false;
final BuildContext context;
late final VoIP voIP;
bool _canHandleNewCall = true;
VoipApp(this.context) {
voIP = VoIP(locator<Client>(), this);
}
@override
void didChangeAppLifecycleState(AppLifecycleState? state) {
background = (state == AppLifecycleState.detached ||
state == AppLifecycleState.paused);
}
@override
bool get canHandleNewCall => _canHandleNewCall;
@override
Future<webrtc_impl.RTCPeerConnection> createPeerConnection(
Map<String, dynamic> configuration,
[Map<String, dynamic> constraints = const {}]) {
return webrtc_impl.createPeerConnection(configuration, constraints);
}
@override
webrtc_impl.VideoRenderer createRenderer() {
return webrtc_impl.RTCVideoRenderer();
}
@override
Future<void> handleCallEnded(CallSession session) async {
_canHandleNewCall = true;
}
@override
Future<void> handleGroupCallEnded(GroupCall groupCall) async {
throw UnimplementedError();
}
@override
Future<void> handleMissedCall(CallSession session) async {
Logs().i("MISSED CALL");
}
@override
Future<void> handleNewCall(CallSession session) async {
_canHandleNewCall = true;
switch (session.direction) {
case CallDirection.kIncoming:
Logs().i("[VOIP] INCOMING CALL");
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => CallScreen(
callSession: session,
),
),
);
break;
case CallDirection.kOutgoing:
Logs().i("[VOIP] OUTGOING CALL");
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => CallScreen(
callSession: session,
),
),
);
break;
}
}
@override
Future<void> handleNewGroupCall(GroupCall groupCall) {
throw UnimplementedError();
}
@override
bool get isWeb => kIsWeb;
@override
webrtc_interface.MediaDevices get mediaDevices =>
webrtc_impl.navigator.mediaDevices;
@override
Future<void> playRingtone() async {
}
@override
Future<void> stopRingtone() async {}
}