RtspClientSharp

repository·master·Indexed 21 days ago

https://github.com/bogdanovkirill/rtspclientsharp

A pure C# RTSP client implementation for .NET Standard 2.0 designed for high performance and scalability. It supports TCP, HTTP, and UDP transport protocols, and provides support for video codecs H.264 and MJPEG, as well as audio codecs AAC, G711A, G711U, PCM, and G726. The library is optimized for low GC pressure and asynchronous operations.

Tokens
1K
Snippets
2
Records
4
Agent score
23%

What's inside RtspClientSharp

  1. Overview of SimpleRtspPlayer example

    master

    The SimpleRtspPlayer is a reference implementation of an RTSP player built using the RtspClientSharp library. It demonstrates how to implement a video player using the WPF framework and the MVVM (Model-View-ViewModel) pattern.

    Key technical features include:

    • Frame Decoding: Utilizes the FFmpeg library for high-performance decoding.
    • Scaling Modes: Supports auto, stretch, and respect aspect ratio.
    • Interpolation Methods: Supports nearest, bilinear, and bicubic (which is the default).
    • Performance: Designed for low GC (Garbage Collection) pressure to ensure smooth video playback.
  2. Install RtspClientSharp via NuGet

    master

    Install the RtspClientSharp package using the NuGet package manager or the command line.

    Compatibility Requirements: Ensure your project targets .NET 4.6.1, .NET Core 2.0, or later. If your project targets an older version, the package will not be added correctly.

    nuget install RtspClientSharp
  3. Use RtspClientSharp to receive RTSP streams

    master

    To consume an RTSP stream, follow these steps:

    1. Create a Uri for the RTSP server.
    2. Create NetworkCredential if authentication is required.
    3. Initialize ConnectionParameters with the URI and credentials.
    4. Set the RtpTransport protocol (e.g., RtpTransportProtocol.TCP).
    5. Instantiate RtspClient with the parameters.
    6. Subscribe to the FrameReceived event to handle incoming media frames.
    7. Call ConnectAsync and ReceiveAsync to start the session.

    Important Note on Memory Management: The frame object in the FrameReceived event uses a buffer (accessible via the FrameSegment property) that is reused by the client to minimize GC pressure. If you need to process the frame asynchronously or use it after the event handler returns, you must make a deep copy of the frame data.

    var serverUri = new Uri("rtsp://192.168.1.77:554/ucast/11");
    var credentials = new NetworkCredential("admin", "123456");
    var connectionParameters = new ConnectionParameters(serverUri, credentials);
    connectionParameters.RtpTransport = RtpTransportProtocol.TCP;
    
    using(var rtspClient = new RtspClient(connectionParameters))
    {
        rtspClient.FrameReceived += (sender, frame) =>
        {
            // process (e.g. decode/save to file) encoded frame here or 
            // make deep copy to use it later because frame buffer (see FrameSegment property) will be reused by client
            switch (frame)
            {
                case RawH264IFrame h264IFrame:
                case RawH264PFrame h264PFrame:
                case RawJpegFrame jpegFrame:
                case RawAACFrame aacFrame:
                case RawG711AFrame g711AFrame:
                case RawG711UFrame g711UFrame:
                case RawPCMFrame pcmFrame:
                case RawG726Frame g726Frame:
                   break;
            }
        }
        
        await rtspClient.ConnectAsync(token);
        await rtspClient.ReceiveAsync(token);
    }