Capture and Preview Local Media
Note
About the code examples for the Control Media Capture section:
- For .NET MAUI, use the C# code.
- For macOS, use the iOS code.
Control Media Capture
To start capturing media, invoke the Start
method of your App.LocalMedia
implementation. This method is inherited from the FM.LiveSwitch.RtcLocalMedia<T>
base class; you don't have to implement it yourself. The Start
method returns a promise, which resolves when the instance begins to capture data from the user's camera and microphone. If media can't be captured, then the promise is rejected. You should always specify a reject action, so that you can be notified if an error occurs. Starting media capture is demonstrated below.
localMedia.Start().Then((FM.LiveSwitch.LocalMedia lm) =>
{
FM.LiveSwitch.Log.Info("Local Media has started successfully!");
}).Fail((Exception ex) =>
{
FM.LiveSwitch.Log.Error("Local Media has failed to start");
});
Stopping capture works the same way. Invoke the Stop
method of your App.LocalMedia
class to stop capturing camera and microphone data. Again, this returns a promise, which you can use to determine when the media capture has stopped.
localMedia.Stop().Then((FM.LiveSwitch.LocalMedia lm) =>
{
FM.LiveSwitch.Log.Info("media capture stopped");
}).Fail((Exception ex) =>
{
FM.LiveSwitch.Log.Error(ex.Message);
});
You can start and stop your local media instance as many times as necessary. When you are completely finished with the local media instance, you should destroy it by invoking its Destroy
method. This ensures that any input devices are released back to the system.
localMedia.Destroy();
Display a Local Preview
Now that you know how to capture data from a user's camera and microphone, you want to be able to show the user a preview of the video that they are sending out. The easiest way to do this is to let LiveSwitch handle it. You can do this by assigning the user's local media to an instance of FM.LiveSwitch.LayoutManager
, which is a class to manage the local and remote video feeds for a video conference.
The specific LayoutManager
instance that you should use varies based on your platform but all of the implementations work in largely the same way. Before a video session starts, you create an instance of your layout manager class and pass it a container parameter. The container is used as a canvas for the layout manager and controls the dimensions of displayed video. For example, if you assign the layout manager a 400x400 container and there are four people in a video session, the layout manager draws each person's video in a 200x200 block.
The code samples below show how to initialize a LayoutManager
instance for each platform. Note that the API is identical except for the types of objects used.
#if WINFORMS
var layoutManager = new FM.LiveSwitch.WinForms.LayoutManager((System.Windows.Forms.Control)container);
#else // WPF
var layoutManager = new FM.LiveSwitch.Wpf.LayoutManager((System.Windows.Controls.Canvas)container);
#endif
Once you have created a LayoutManager
instance, you can assign the local view from your App.LocalMedia
instance to it. You first retrieve the view by accessing the View
property of the App.LocalMedia
instance. This returns an object of an appropriate type for your platform. You can now assign this view to the layout manager by invoking the SetLocalView
method, demonstrated below.
When you are done with a media session, you also want to remove this view from the layout manager. This is done by invoking the UnsetLocalView
method of the layout manager.