Table of Contents
To support Screen-Sharing in the Room, you need to enable Screen-Sharing during Room Creation by setting: { "screen_share": true; })
in the JSON Payload. To receive Shared Screen, you need to subscribe to the Screen-Share Stream ID# 101 and play it on the Video Player.
Note: If you have Android 11 (API level 30) or higher, then you must create a foreground service and access the camera or microphone in the foreground service. Declare the camera
or microphone
foreground service types, respectively, as attributes of your <service>
component. [ Read More… ]
Start Screen Sharing
The EnxRoom.startScreenShare()
method is used to create a Screen-Sharing Stream @ 6fps to publish in the Room. Screen Sharing continues even when the Application runs in the background.
Class: EnxRoom
Observer: public void setScreenShareObserver(EnxScreenShareObserver-Object)
Method: public void startScreenShare()
Callbacks:
onScreenSharedStarted
– Notification to everyone in the Room when Screen-Sharing starts.
// Initiate Screen Share Observer to receive Callbacks
room.setScreenShareObserver(this);
// Start Screen Share
room.startScreenShare();
// A new Screen Share Stream is available, receive Information
public void onScreenSharedStarted(EnxStream enxStream) {
// Get EnxPlayerView from ensStream
// And add to View
yourView.addView(enxStream.mEnxPlayerView);
}
Stop Screen Sharing
The EnxRoom.stopScreenShare()
method is used to stop the ongoing Screen-Sharing.
Class: EnxRoom
Method: public void stopScreenShare()
Callbacks:
onScreenSharedStopped
– Notification to everyone in the Room when Screen-Sharing stops.
room.stopScreenShare(); // Stop Screen Share
// Screen Share has stopped. Receive Information
public void onScreenSharedStopped(EnxStream enxStream){
// And remove View from yourView
yourView.removeAllViews();
}
Error Codes / Exceptions:
Code | Description |
---|---|
5107 | Repeated startScreenShare() call made while previous request is in process. |
1170 | Screen-Sharing not supported in your subscription. |
Force Stop other’s Screen Sharing
Availability: Android SDK 2.1.2+
If moderator wishes to force stop any ongoing Screen Share by other user in the Room, he can do so by using EnxRoom.stopAllSharing()
method. Note, this is Moderator exclusive feature, can’t be executed from Participant’s end point.
This method also force stops any ongoing Canvas Streaming.
Class: EnxRoom
Observer: ACK CallBack
Method: public void stopAllSharing()
Callbacks:
onStopAllSharingACK
– Notification to everyone in the Room when Screen-Sharing stops.
EnxRoom.stopAllSharing() // Force Stop the Shared Screen
// Notification to all when share stops
public void onStopAllSharingACK(JSONObject jsonObject) {
// Code here... to handle your UI
}
Implementing a Foreground Service
The implementation part is quite similar to that of a service. Let’s check the step-by-step procedure for doing this.
- Create a foreground service
- Add permission in manifest
- Register foreground service in manifest
Create a Foreground Service
public class ForegroundService extends Service {
public static final String CHANNEL_ID = "ForegroundServiceChannel";
int NOTIFICATION_ID = 121412;
public NotificationManager notificationManager;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
if (intent.getAction().equals("ACTION_START")) {
showOnGoingCall();
}else {
stopForegroundService();
}
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void showOnGoingCall() {
String app_Name = getResources().getString(R.string.app_name);
Intent notificationIntent = new Intent(this, VideoConferenceActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
String CHANNEL_ID = getResources().getString(R.string.app_name);
Notification notification;
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Sync Service", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Service Name");
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(app_Name)
.setCategory(Notification.CATEGORY_SERVICE)
.setContentText("Ongoing call")
.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentIntent(pendingIntent)
.build();
} else {
notification = new Notification.Builder(this)
.setContentTitle("Project Name")
.setOngoing(true)
.setContentText("Ongoing call")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent).build();
notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
startForeground(NOTIFICATION_ID, notification);
}
private void stopForegroundService() {
stopForeground(true);
stopSelf();
}
}
Add Permission in Manifest
We need to request foreground service permission for Android 9 (API level 28) or higher. We need to specify the permission below in our manifest file:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Register Foreground Service in Manifest
<service android:name=".activity.ForegroundService" android:enabled="true" android:foregroundServiceType="mediaProjection" android:exported="true" />Call Foreground Service in VideoConferenceActivity public void startService() { Intent serviceIntent = new Intent(VideoConferenceActivity.this, ForegroundService.class); serviceIntent.setAction("ACTION_START");// serviceIntent.putExtra("inputExtra", "Foreground Service Example in Android"); ContextCompat.startForegroundService(VideoConferenceActivity.this, serviceIntent); } public void stopService() { Intent serviceIntent = new Intent(VideoConferenceActivity.this, ForegroundService.class); serviceIntent.setAction("ACTION_STOP"); stopService(serviceIntent);// ContextCompat.startForegroundService(getActivity(), serviceIntent); } stopService() call in onDestroy method and startService() call in onStart method