In this tutorial, you'll learn how to share screens in a conference.
In the previous tutorial, you've learned that to stream media, we created a local media object to capture a user's local audio and video data.
Likewise, to share a user's screen at the same time, we'll create an additional screen share local media object to capture the local user's screen. Then, we'll create an additional upstream connection to upload the screen share local media. Other participants get the screen share downstream connection the same way as the other media downstream.
In the previous tutorial, to capture user's video with their camera, you created a CameraLocalMedia class derived from the LocalMedia class. This class overrides the CreateVideoSource method and returns a CameraSource as your video source. To support screen capture, we'll create a ScreenShareLocalMedia class derived from the LocalMedia class and override the CreateVideoSource method to return a ScreenSource as the video source.
Paste the following code to the ScreenShareLocalMedia.cs file.
// Local Media implementation for screen share
class ScreenShareLocalMedia : LocalMedia
{
public ScreenShareLocalMedia(bool disableAudio, bool disableVideo, AecContext aecContext)
: base(disableAudio, disableVideo, aecContext)
{
Initialize();
}
protected override VideoSource CreateVideoSource()
{
return new FM.LiveSwitch.WinForms.ScreenSource(3);
}
protected override ViewSink<System.Windows.Controls.Image> CreateViewSink()
{
return new FM.LiveSwitch.Wpf.ImageSink()
{
ViewScale = LayoutScale.Contain
};
}
}
In the previous tutorial, to capture user's video with their camera, you created a CameraLocalMedia class derived from the LocalMedia class. This class overrides the CreateVideoSource method and returns a CameraSource as your video source. To support screen capture, we'll create a ScreenShareLocalMedia class derived from the LocalMedia class and override the CreateVideoSource method to return a ScreenSource as the video source.
Paste the following code to the ScreenShareLocalMedia.java file.
public class ScreenShareLocalMedia extends LocalMedia<FrameLayout> {
private final MediaProjectionSource projectionSource;
public ScreenShareLocalMedia(MediaProjection projection, Context context, boolean disableAudio, boolean disableVideo, AecContext aecContext) {
super(context, disableAudio, disableVideo, aecContext);
this.context = context;
projectionSource = new MediaProjectionSource(projection, context, 1);
super.initialize();
}
@Override
protected VideoSource createVideoSource() {
return projectionSource;
}
@Override
protected ViewSink<FrameLayout> createViewSink() {
return new fm.liveswitch.android.OpenGLSink(context);
}
}
In the previous tutorial, to capture user's video with their camera, you created a CameraLocalMedia class derived from the LocalMedia class. This class overrides the CreateVideoSource method and returns a CameraSource as your video source. To support screen capture, we'll create a ScreenShareLocalMedia class derived from the LocalMedia class and override the CreateVideoSource method to return a ScreenSource as the video source.
Paste the following code to the ScreenShareLocalMedia.swift file.
You've learned that to capture a user's microphone and camera, you must create an fm.liveswitch.LocalMedia instance. To capture a user's screen instead of their camera, simply provide one additional parameter to the fm.liveswitch.LocalMedia instance.
The additional third parameter determines whether or not to capture the user's screen. We set it and the second parameter to true to capture the user's screen. We set the first parameter to false to turn off audio.
Paste the following code into the HelloWorldLogic class in the HelloWorldLogic.ts file:
// Create a new local media for screen capturing.
public localScreenMedia = new fm.liveswitch.LocalMedia(false, true, true);
Start and Stop Screen Share
To share video from the camera and video from screen capture at the same time, we need to create another upstream connection. To do so, we create a new ScreenShareLocalMedia instance and pass it into the OpenSfuUpstreamConnection function to create an SfuUpstreamConnection for the screen share media. This is similar to starting and stopping the camera local media and making an upstream connection.
Paste the following code into the HelloWorldLogic class:
In the MainWindow.xaml.cs file, uncomment the codes between the <Screen Share> and </Screen Share> tags.
In the ScreenShareFragment.java file, uncomment all the code that's commented out.
The Android Fragment serves as good reference on how to set up Media Projection for screen sharing. This requires a background service similarly implemented as the BackgroundService.java file. Feel free to follow the procedure there for other projects. Note that for the background service to work, it must be defined as a service in the AndroidManifest.xml file, which the project has already done for you.
In the ScreenshareUI.swift file, uncomment all the code that's commented out.
In the index.ts file, uncomment the codes between the <Screen Share> and </Screen Share> tags.
Run Your App
Run your app in your project IDE. Click Join and then click Toggle Screen Share to share your screen. Your app UI should look similar to the following: