Click here to Skip to main content
15,936,802 members
Articles / Programming Languages / C#

C# WinForm - RTSP IP Camera Viewer

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
12 Jun 2024MIT2 min read 5.8K   329   14   10
Simple C# WinForm application for realtime playing, snapshoting and recording RTSP stream from any IP camera
In this article I will show you probably the easiest way to play, snapshot and record RTSP stream of any IP camera using the VLC library and VlcControl. I will also show you all the necessary dependencies and requirements. Source code included, feel free to use it for your purposes.

Links

For testing purposes you can use my demo IP camera RTSP stream:
rtsp://stream.szep.cz/user=test_password=test_channel=1_stream=1

Introduction

RTSP stands for Real-Time Streaming Protocol, a network protocol designed for transmitting multimedia data over a network. It is encountered in audio/video transmission, it works on the application layer of the ISO/OSI reference model and the default port is 554. Practically every IP camera or NVR supports RTSP. Remember, sometimes you have to enable RTSP on the camera and the specific RTSP address may be different depending on the IP camera manufacturer.

You can easily play the RTSP stream in your C# WinForm application using the VLC library and VlcControl.

The example project is able to play, snapshot, record and save video stream to file. It also checks if 32-bit VLC Media Player is installed on your computer - if not, it will direct you to download it.

Image 1

Necessary Things to Do

You must have VLC Media Player already installed on computer where you want to execute this project.

If you want to use VLCControl in your own project, first of all you have to install NuGet package 'VLC.DotNet.Forms':
Project > Manage NuGet Packages > Browse > Type 'VLC.DotNet.Forms' > Install

Image 2

Then add VlcControl to your formr:
Toolbox > VlcControl > drop VlcControl somewhere into your form

Image 3

Image 4

All VLC Controls needs to use vlclib.dll located in VLC installation folder, in my case C:\Program Files (x86)\VideoLAN\VLC.

This path must be always set in VlcLibDirectory property (you can easily set this property in designer). In example project, VlcLibDirectory is already set.

Image 5

Using the code

At the moment we already have 32-bit VLC Media Player installed on computer, NuGet package installed and VlcControl, in my case named VLCPlayer, added to the form.

1. How to play RTSP stream

C#
 VLCPlayer.Stop();

 VLCPlayer.SetMedia(new Uri("rtsp://something.xx/"), string.Empty);

 VLCPlayer.Play();

or you can also use

C#
VLCPlayer.Play(new Uri("rtsp://something.xx/"));

2. How to play and record RTSP stream, then save recording to file

You have to use double backslash when defining the path, otherways it doesn't work.

C#
 bool IsRecording;

 if (!IsRecording)
 {

     // Start recording

     if (VLCPlayer.IsPlaying)
     {

         string path = $@"c:\\Users\\{Environment.UserName}\\Desktop\\{Guid.NewGuid()}.mp4";


         // Magic option string that creates recording:

         var mediaOptions = new[] { ":sout=#duplicate{dst=display,dst=std{access=file,mux=mp4,dst=\"" + path + "\"}" };


         VLCPlayer.SetMedia(new Uri("rtsp://something.xx/"), mediaOptions);

         VLCPlayer.Play();


         IsRecording = true;


     }

 }

 else
 {

     // Stop recording

     IsRecording = false;

     VLCPlayer.Stop();

     // keep playing without recording

     VLCPlayer.SetMedia(new Uri("rtsp://something.xx/"), string.Empty);

     VLCPlayer.Play();

 }

3. How to take snapshot, then save to file

You have to use double backslash when defining the path, otherways it doesn't work.

C#
 if (VLCPlayer.IsPlaying)
 {

     string path = $@"c:\\Users\\{Environment.UserName}\\Desktop\\{Guid.NewGuid()}.png";

     VLCPlayer.TakeSnapshot(path);

 }

or if you want to specify output image size in pixels (for example 400x300):

C#
 if (VLCPlayer.IsPlaying)
 {

     string path = $@"c:\\Users\\{Environment.UserName}\\Desktop\\{Guid.NewGuid()}.png";

     VLCPlayer.TakeSnapshot(path, 400, 300);

 }

4. How to set the audio volume

C#
VLCPlayer.Audio.Volume = 69;

5. How to mute the audio

C#
VLCPlayer.Audio.IsMute = true;

6. How to set custom aspect ratio

C#
VLCPlayer.Video.AspectRatio = "16:9"; // 4:3 , 0:0 ...

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Network Administrator
Czech Republic Czech Republic
network administrator, installer of video surveillance systems, programmer

Comments and Discussions

 
QuestionOutdated? Pin
Ralph Lechterbeck22-Jun-24 18:34
Ralph Lechterbeck22-Jun-24 18:34 
AnswerRe: Outdated? Pin
Jakub Szep23-Jun-24 6:50
Jakub Szep23-Jun-24 6:50 
QuestionThanks, helped a lot. Pin
Peter Vroegindewey22-Jun-24 0:32
Peter Vroegindewey22-Jun-24 0:32 
AnswerRe: Thanks, helped a lot. Pin
Jakub Szep23-Jun-24 6:56
Jakub Szep23-Jun-24 6:56 
QuestionFile empty Pin
Member 1485426021-Jun-24 2:15
Member 1485426021-Jun-24 2:15 
AnswerRe: File empty Pin
Jakub Szep23-Jun-24 7:03
Jakub Szep23-Jun-24 7:03 
QuestionThe ZIP File link appears to be broken....... Pin
MisfitGeek17-Jun-24 7:30
MisfitGeek17-Jun-24 7:30 
AnswerRe: The ZIP File link appears to be broken....... Pin
Jakub Szep17-Jun-24 11:06
Jakub Szep17-Jun-24 11:06 
QuestionRaspberry Pi Pin
Steven Cook17-Jun-24 5:33
Steven Cook17-Jun-24 5:33 
AnswerRe: Raspberry Pi Pin
Jakub Szep17-Jun-24 8:06
Jakub Szep17-Jun-24 8:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.