Use a File Source
With LiveSwitch, you have total control over what you want to send to the Media Server (or to your peers). A common request is to send audio and video from a pre-recorded file instead of from the camera/microphone of a device. To do this, modify your LocalMedia
class to use a MatroskaAudioSource
and MatroskaVideoSource
by updating the CreateAudioSource
and CreateVideoSource
functions. As the class names imply, your files must use the Matroska container format. The files must also be readable using a known codec. Out-of-the-box support is provided for PCMU, PCMA, and Opus for audio and VP8, H.264, and VP9 for video. If your files are in a different container format or use a different code, they must first be transcoded (for example, using FFmpeg).
Note that the Matroska audio source and video source must be supplied with references to the Opus decoder, VP8 decoder, and H.264 decoder in case the remote receivers don't support the codec used in the recording. As needed, the media is transcoded for remote playback and to support handling of packet loss and keyframe requests.
Here is a sample local media class for .NET that uses Matroska files as the audio and video sources (download big-buck-bunny.mkv):
public class LocalMatroskaMedia : LocalMedia
{
class MatroskaAudioSource : Matroska.AudioSource
{
public MatroskaAudioSource(string path)
: base(path)
{ }
protected override AudioDecoder CreateOpusDecoder(AudioConfig config)
{
return new Opus.Decoder();
}
}
class MatroskaVideoSource : Matroska.VideoSource
{
public MatroskaVideoSource(string path)
: base(path)
{ }
protected override VideoDecoder CreateH264Decoder()
{
return new OpenH264.Decoder();
}
protected override VideoDecoder CreateVp8Decoder()
{
return new Vp8.Decoder();
}
protected override VideoDecoder CreateVp9Decoder()
{
return new Vp9.Decoder();
}
}
public LocalMatroskaMedia(bool disableAudio, bool disableVideo, AecContext aecContext)
: base(disableAudio, disableVideo, aecContext)
{
Initialize();
}
protected override AudioSource CreateAudioSource(AudioConfig config)
{
return new MatroskaAudioSource("big-buck-bunny.mkv");
}
protected override VideoSource CreateVideoSource()
{
return new MatroskaVideoSource("big-buck-bunny.mkv");
}
/// <summary>
/// Creates the view sink.
/// </summary>
/// <returns></returns>
#if WINFORMS
protected override ViewSink<FM.LiveSwitch.WinForms.PictureBoxControl> CreateViewSink()
{
return new FM.LiveSwitch.WinForms.PictureBoxSink()
{
ViewScale = LayoutScale.Contain
};
}
#else
protected override ViewSink<System.Windows.Controls.Image> CreateViewSink()
{
return new FM.LiveSwitch.Wpf.ImageSink()
{
ViewScale = LayoutScale.Contain
};
}
#endif
}