Video Calling
ConnectyCube Video Calling P2P API is built on top of WebRTC protocol and based on top of WebRTC Mesh architecture.
Max people per P2P call is 4.
To get a difference between P2P calling and Conference calling please read our ConnectyCube Calling API comparison blog page.
Required preparations for supported platforms
iOS
Add the following entries to your Info.plist file, located in <project root>/ios/Runner/Info.plist
:
This entries allow your app to access the camera and microphone.
Android
Ensure the following permission is present in your Android Manifest file, located in <project root>/android/app/src/main/AndroidManifest.xml
:
If you need to use a Bluetooth device, please add:
The Flutter project template adds it, so it may already be there.
Also you will need to set your build settings to Java 8, because official WebRTC jar now uses static methods in EglBase
interface. Just add this to your app level build.gradle
:
If necessary, in the same build.gradle
you will need to increase minSdkVersion
of defaultConfig
up to 18
(currently default Flutter generator set it to 16
).
macOS
Add the following entries to your *.entitlements files, located in <project root>/macos/Runner
:
This entries allow your app to access the internet, microphone, and camera.
Windows
It does not require any special preparations.
Web
It does not require any special preparations.
Linux
It does not require any special preparations.
P2PClient setup
ConnectyCube Chat API is used as a signaling transport for Video Calling API, so in order to start using Video Calling API you need to connect to Chat.
To manage P2P calls in flutter you should use P2PClient
. Please see code below to find out possible functionality.
Create call session
In order to use Video Calling API you need to create a call session object - choose your opponents with whom you
will have a call and a type of session (VIDEO or AUDIO). P2PSession
creates via P2PClient
:
Add listeners
Below described main helpful callbacks and listeners:
Initiate a call
The userInfo
is used to pass any extra parameters in the request to your opponents.
After this, your opponents will receive a new call session in callback:
Accept a call
To accept a call the following code snippet is used:
After this, you will get a confirmation in the following callback:
Also, both the caller and opponents will get a special callback with the remote stream:
From this point, you and your opponents should start seeing each other.
Receive a call in background
See CallKit section below.
Reject a call
After this, the caller will get a confirmation in the following callback:
Sometimes, it could a situation when you received a call request and want to reject it, but the call session object has not arrived yet. It could be in a case when you integrated CallKit to receive call requests while an app is in background/killed state. To do a reject in this case, the following snippet can be used:
End a call
After this, the opponents will get a confirmation in the following callback:
Monitor session connections state
To monitor the states of your peer connections (users) you need to implement the RTCSessionStateCallback
abstract class:
Mute audio
Switch audio output
For iOS/Android platforms use:
For Chrome-based browsers and Desktop platforms use:
Mute video
Switch video cameras
For iOS/Android platforms use:
For the Web platform and Desktop platforms use:
Get available cameras list
Get available Audio input devices list
Get available Audio output devices list
Use the custom media stream
Toggle the torch
Screen Sharing
The Screen Sharing feature allows you to share the screen from your device to other call members. Currently the Connectycube Flutter SDK supports the Screen Sharing feature for all supported platforms.
For switching to the screen sharing during the call use next code snippet:
Android specifics of targeting the targetSdkVersion
to the version 31 and above
After updating the targetSdkVersion
to the version 31 you can encounter an error:
To avoid it do the next changes and modifications in your project:
1. Connect the flutter_background
plugin to your project using:
2. Add to the file app_name/android/app/src/main/AndroidManifest.xml
to section manifest
next permissions:
3. Add to the file app_name/android/app/src/main/AndroidManifest.xml
to section application
next service:
4. Create the next function somewhere in your project:
and call it somewhere after the initialization of the app or before starting the screen sharing.
5. Call the function FlutterBackground.enableBackgroundExecution()
just before starting the screen
sharing and function FlutterBackground.disableBackgroundExecution()
after ending the screen sharing
or finishing the call.
IOS screen sharing using the Screen Broadcasting feature.
The Connectycube Flutter SDK supports two types of Screen sharing on the iOS platform. There are In-app screen sharing and Screen Broadcasting. The In-app screen sharing doesn’t require any additional preparation on the app side. But the Screen Broadcasting feature requires some.
All required features we already added to our P2P Calls sample.
Below is the step-by-step guide on adding it to your app. It contains the following steps:
- Add the
Broadcast Upload Extension
; - Add required files from our sample to your iOS project;
- Update project configuration files with your credentials;
Add the Broadcast Upload Extension
For creating the extension you need to add a new target to your application, selecting the
Broadcast Upload Extension
template. Fill in the desired name, change the language to Swift, make
sure Include UI Extension
(see screenshot) is not selected, as we don’t need custom UI for our case, then press
Finish. You will see that a new folder with the extension’s name was added to the project’s tree,
containing the SampleHandler.swift
class. Also, make sure to update the Deployment Info, for the
newly created extension, to iOS 14 or newer. To learn more about creating App Extensions check the
official documentation.
Add the required files from our sample to your own iOS project
After adding the extension you should add prepared files from our sample to your own project. Copy
next files from our Broadcast Extension
directory: Atomic.swift
,
Broadcast Extension.entitlements
(the name can be different according to your extension’s name),
DarwinNotificationCenter.swift
, SampleHandler.swift
(replace the automatically created file),
SampleUploader.swift
, SocketConnection.swift
. Then open your project in Xcode and link these files
with your iOS project using Xcode tools. For it, call the context menu of your extension directory, select
‘Add Files to “Runner”…’ (see screenshot) and select files copied to your extension directory before.
Update project configuration files
Do the following for your iOS project configuration files:
1. Add both the app and the extension to the same App Group. For it, add to both (app and extension)
*.entitlements
files next lines:
where the group.com.connectycube.flutter
is your App group. To learn about working with app groups,
see Adding an App to an App Group.
We recommend you create the app group in the Apple Developer Console before.
Next, add the App group id value to the
app’s Info.plist
of your app for the RTCAppGroupIdentifier
key:
where the group.com.connectycube.flutter
is your App group.
2. Add a new key RTCScreenSharingExtension
to the app’s Info.plist
with the extension’s Bundle Identifier
as the value:
where the com.connectycube.flutter.p2p-call-sample.app.Broadcast-Extension
is the Bundle ID of your
Broadcast Extension. Take it from the Xcode
3. Update SampleHandler.swift
’s appGroupIdentifier
constant with the App Group name your
app and extension are both registered to.
where the group.com.connectycube.flutter
is your app group.
4. Make sure voip
is added to UIBackgroundModes
, in the app’s Info.plist
, in order to work when
the app is in the background.
After performing mentioned actions you can switch to Screen sharing during the call using useIOSBroadcasting = true
:
Requesting desktop capture source
Desktop platforms require the capture source (Screen or Window) for screen sharing. We prepared a widget ready for using that requests the available sources from the system and provides them to a user for selection. After that, you can use it as the source for screen sharing.
In code it can look in a next way:
The default capture source (usually it is the default screen) will be captured if set null
as a capture source
for the desktop platform.
WebRTC Stats reporting
Stats reporting is an insanely powerful tool that can provide detailed info about a call.
There is info about the media, peer connection, codecs, certificates, etc. To enable stats report you should first set stats
reporting frequency using RTCConfig
.
Then you can subscribe to the stream with reports using the instance of the call session:
To disable fetching Stats reports set this parameter as 0.
Monitoring mic level and video bitrate using Stats
Also, we prepared the helpful manager CubeStatsReportsManager
for processing Stats reports and getting
some helpful information like the opponent’s mic level and video bitrate.
For its work, you just need to configure the RTCConfig
as described above. Then create the
instance of CubeStatsReportsManager
and initialize it with the call session.
After that you can subscribe on the interested data:
After finishing the call you should dispose of the manager for avoiding memory leaks. You can do it in
the onSessionClosed
callback:
Configurations
ConnectyCube Flutter SDK provides possibility to change some default parameters for call session.
Media stream configurations
Use instance of RTCMediaConfig
class to change some default media stream configs.
Call quality
Limit bandwidth
Despite WebRTC engine uses automatic quality adjustement based on available Internet bandwidth, sometimes it’s better to set the max available bandwidth cap which will result in a better and smoother user experience. For example, if you know you have a bad internet connection, you can limit the max available bandwidth to e.g. 256 Kbit/s.
This can be done either when initiate a call via RTCMediaConfig.instance.maxBandwidth = 512
which will result in limiting the max vailable bandwidth for ALL participants or/and during a call:
which will result in limiting the max available bandwidth for current user only.
Call connection configurations
Use instance of RTCConfig
class to change some default call connection configs.
Recording
(coming soon)
CallKit
A ready Flutter P2P Calls sample with CallKit integrated is available at GitHub. All the below code snippets will be taken from it.
For mobile apps, it can be a situation when an opponent’s user app is either in closed (killed)or background (inactive) state.
In this case, to be able to still receive a call request, a flow called CallKit is used. It’s a mix of CallKit API + Push Notifications API + VOIP Push Notifications API.
The complete flow is similar to the following:
- a call initiator should send a push notification (for iOS it’s a VOIP push notification) along with a call request
- when an opponent’s app is killed or in a background state - an opponent will receive a push notification about an incoming call, so the CallKit incoming call screen will be displayed, where a user can accept/reject the call.
The ConnectyCube Flutter Call Kit plugin should be used to integrate CallKit functionality.
Below we provide a detailed guide on additional steps that needs to be performed in order to integrate CallKit into a Flutter app.
Connect the ConnectyCube Flutter Call Kit plugin
Please follow the plugin’s guide on how to connect it to your project.
Initiate a call
When initiate a call via callSession.startCall()
, additionally we need to send a push notification
(standard for Android user and VOIP for iOS). This is required for an opponent(s) to be able to receive
an incoming call request when an app is in the background or killed state.
The following request will initiate a standard push notification for Android and a VOIP push notification for iOS:
Receive call request in background/killed state
The goal of CallKit is to receive call requests when an app is in the background or killed state. For iOS we will use CallKit and for Android we will use standard capabilities.
The ConnectyCube Flutter Call Kit plugin covers all main functionality for receiving the calls in the background.
First of all, you need to subscribe on a push notifications to have the possibility of receiving a push notifications in the background.
You can request the subscription token from the ConnectyCube Flutter Call Kit plugin. It will return the VoIP token for iOS and the common FCM token for Android.
Then you can create the subscription using the token above. In code it will look next:
During the Android app life, the FCM token can be refreshed. Use the next code snippet to listen to this event and renew the subscription on the Connectycube back-end:
After correctly performing the steps above the ConnectyCube Flutter Call Kit plugin automatically will show the CallKit screen for iOS and the Incoming Call notification for Android.
!> Pay attention: the Android platform requires some preparations for showing the notifications, working in the background, and starting the app from a notification. It includes next:
- Request the permission on showing notifications (using the permission_handler plugin):
- Request permission for starting the app from notification (using the permission_handler plugin):
- Start/Stop foreground service when navigating the app to the background/foreground during the call (using flutter_background plugin):
Follow the official plugin’s doc for more details.
Listen to the callbacks from ConnectyCube Flutter Call Kit plugin
After displaying the CallKit/Call Notification you need to react to callbacks produced by this screen. Use the next code snippets to start listening to them:
Also, when performing any operations e.g. start call, accept, reject, stop, etc, we need to report back to CallKit lib - to have both app UI and CallKit UI in sync:
For the callUUID
we will use call’s currentCall!.sessionId
.
All the above code snippets can be found in a ready Flutter P2P Calls sample with CallKit integrated.