RTMP streaming
Products: Video Capture SDK .Net, Video Edit SDK .Net, Media Blocks SDK .Net
RTMP (Real-Time Messaging Protocol) output in VisioForge provides a robust solution for streaming video and audio content over the internet. This guide explains how to configure and use RTMP output effectively in your applications.
Cross-platform RTMP output (X-engines: VideoCaptureCoreX, VideoEditCoreX, Media Blocks)
The RTMPOutput class serves as a comprehensive configuration hub for RTMP streaming, supporting various video and audio encoders across different platforms. It implements multiple interfaces including IVideoEditXBaseOutput
and IVideoCaptureXBaseOutput
, making it versatile for both video editing and capture scenarios.
Basic Setup
To begin using RTMP output, you'll need to create an instance with your streaming URL:
var rtmpOutput = new RTMPOutput("rtmp://your-streaming-server/stream-key");
If you don't have the URL ready during initialization, you can set it later:
var rtmpOutput = new RTMPOutput();
rtmpOutput.Sink.Location = "rtmp://your-streaming-server/stream-key";
Set the output to the Video Capture SDK engine.
core.Outputs_Add(rtmpOutput, true); // core is an instance of VideoCaptureCoreX
Set the output to the Video Edit SDK engine.
core.Output_Format = rtmpOutput; // videoEdit is an instance of VideoEditCoreX
Media Blocks SDK sample:
var rtmpSink = new RTMPSinkBlock(new RTMPSinkSettings() { Location = "rtmp://streaming-server/stream" });
pipeline.Connect(h264Encoder.Output, rtmpSink.CreateNewInput(MediaBlockPadMediaType.Video));
pipeline.Connect(aacEncoder.Output, rtmpSink.CreateNewInput(MediaBlockPadMediaType.Audio));
Video Encoder Configuration
The RTMPOutput class supports multiple video encoders, automatically selecting the most appropriate default based on your platform:
Supported Video Encoders
- OpenH264 (Default for most platforms)
- NVENC H264 (NVIDIA GPU acceleration)
- QSV H264 (Intel Quick Sync)
- AMF H264 (AMD acceleration)
- HEVC/H265 variants (MF HEVC, NVENC HEVC, QSV HEVC, AMF H265)
Example of configuring a specific video encoder:
// Using NVIDIA encoder if available
if (NVENCH264EncoderSettings.IsAvailable())
{
rtmpOutput.Video = new NVENCH264EncoderSettings();
}
Audio Encoder Configuration
The system supports various AAC encoders, with platform-specific defaults:
Supported Audio Encoders
- VO-AAC (Default for non-Windows platforms)
- AVENC AAC
- MF AAC (Default for Windows)
Example of custom audio encoder configuration:
// Using MF AAC encoder on Windows
rtmpOutput.Audio = new MFAACEncoderSettings();
Platform-Specific Considerations
Windows
- Default video encoder: OpenH264
- Default audio encoder: MF AAC
- Additional support for Microsoft Media Foundation HEVC encoding
macOS
- Uses AppleMediaH264EncoderSettings for video encoding
- Default audio encoder: VO-AAC
Cross-Platform Compatibility
The class automatically handles platform-specific differences through conditional compilation:
#if __MACOS__
Video = new AppleMediaH264EncoderSettings();
#else
Video = new OpenH264EncoderSettings();
#endif
Best Practices
-
Encoder Selection: Always check encoder availability before using hardware-accelerated options:
if (NVENCH264EncoderSettings.IsAvailable())
{
// Use NVIDIA encoding
} -
Error Handling: Implement proper error handling when setting up streaming:
try
{
var rtmpOutput = new RTMPOutput(streamUrl);
// Configure encoders and start streaming
}
catch (Exception ex)
{
// Handle initialization errors
} -
Resource Management: Properly dispose of resources when streaming is complete.
Advanced Usage
Dynamic Encoder Selection
You can programmatically enumerate available encoders:
var rtmpOutput = new RTMPOutput();
var availableVideoEncoders = rtmpOutput.GetVideoEncoders();
var availableAudioEncoders = rtmpOutput.GetAudioEncoders();
Custom Sink Configuration
The RTMPSinkSettings class allows for detailed streaming configuration:
rtmpOutput.Sink = new RTMPSinkSettings
{
Location = "rtmp://streaming-server/stream",
// Additional sink configuration as needed
};
Troubleshooting
Common issues to watch for:
- Ensure proper URL formatting for your streaming service
- Check encoder availability before attempting to use hardware acceleration
- Monitor system resources when using software encoding
- Verify network connectivity and bandwidth availability
Performance Considerations
To achieve optimal streaming performance:
- Choose hardware-accelerated encoders when available
- Monitor CPU and GPU usage
- Ensure proper network bandwidth
- Consider using lower encoding profiles for constrained environments
Windows-only RTSP output (VideoCaptureCore and VideoEditCore engines)
Video and audio encoder configuration
Set the Network_Streaming_Enabled
property to true to enable network streaming.
VideoCapture1.Network_Streaming_Enabled = true;
Set network streaming format to RTMP_FFMPEG_EXE
and create FFMPEGEXEOutput
object.
VideoCapture1.Network_Streaming_Format = NetworkStreamingFormat.RTMP_FFMPEG_EXE;
var ffmpegOutput = new FFMPEGEXEOutput();
Set the output format and video and audio codecs.
ffmpegOutput.FillDefaults(DefaultsProfile.MP4_H264_AAC, true);
Set the output muxer.
ffmpegOutput.OutputMuxer = OutputMuxer.FLV;
Configure output.
VideoCapture1.Network_Streaming_Output = ffmpegOutput;
Enable audio streaming (required for YouTube and Facebook Live).
VideoCapture1.Network_Streaming_Audio_Enabled = true;
YouTube streaming
Set the URL to rtmp://a.rtmp.youtube.com/live2/
+ YouTube stream key.
VideoCapture1.Network_Streaming_URL = "rtmp://a.rtmp.youtube.com/live2/" + "YOUR KEY HERE";
Facebook Live streaming
Set the URL to rtmps://live-api-s.facebook.com:443/rtmp/
+ Facebook stream key.
VideoCapture1.Network_Streaming_URL = "rtmps://live-api-s.facebook.com:443/rtmp/" + "YOUR KEY HERE";
Other RTMP servers
Set the URL to the RTMP server URL.
VideoCapture1.Network_Streaming_URL = "rtmp server url";