Streaming webcam
mikeolasz opened this issue · 3 comments
Dear @juliusfriedman,
I'm trying to figure out how to achive webcamera streaming without luck. I went through the UnitTests to get a better understanding of the library but I still can't manage to establish a proper stream. I'm using AForge to contact the camera and am trying to send the frames via Media.Rtsp.Server.MediaTypes.RFC2435Media.
static Media.Rtsp.RtspServer server;
static Media.Rtsp.Server.MediaTypes.RFC2435Media source;
static void Main(string[] args)
{
// enumerate video devices
VideoCaptureDevice videoSource = null;
var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
// create video source
bool found = false;
for (int i = 0; i < videoDevices.Count && !found; i++)
{
if (videoDevices[i].Name.Contains("RICOH R Development Kit UVC Capture"))
{
videoSource = new VideoCaptureDevice(videoDevices[i].MonikerString);
found = true;
}
}
if (_videoSource == null)
{
throw new Exception("Could not find device.");
}
// set NewFrame event handler
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
var serverIp = Media.Common.Extensions.Socket.SocketExtensions.GetFirstUnicastIPAddress(System.Net.Sockets.AddressFamily.InterNetwork);
server = new Media.Rtsp.RtspServer(serverIp, 554)
{
Logger = new Media.Rtsp.Server.RtspServerConsoleLogger()
};
source = new Media.Rtsp.Server.MediaTypes.RFC2435Media("test", null, false, 1920, 1080, true);
server.TryAddMedia(source);
server.Start();
videoSource.Start();
}
private static void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone(new Rectangle(0,0,1920,1080), PixelFormat.Format32bppArgb);
source.Packetize(bitmap);
}
What I'm trying to do is in the snippet above. I'm able to establish the connection between an RtspServer and VLC player but it's kinda stuttering, has some wierd flashing (if like it went from BGR to RGB) and it's like the packets are messed up. I mean it's like the following:
|.............|.............|.............| <- these are representing the frames
.....{.............} <- and it seems like this, like if there would be data from the previous frame
The library seems nice and easy to use but I can't get it to work properly.
Could you please aid me in any direction? Thanks in advance.
Hello, thanks for your interest in the library.
Your video frame event appears to be cloning a bitmap to packetize it, you would likely have more success if you just passed the bitmap directly to the packetize function directly, this function will perform any resizing required but the most efficient way is to not have to resize at all e.g. to have the stream resolution set to the same resolution of that as the source.
You may also want to adjust the quality parameter of the stream to the lowest value which provides you with the acceptable visual quality for your application.
See the Mirror stream in the UnitTests when testing the RtspServer which does almost the same thing you are trying to achieve but from the desktop rather than a camera source, it should be trivial to adapt it for your needs.
Hello,
Thank you for your response.
Unfortunately it didn't help, but I made an example of what is really happening.
Following the examples I made a source that reads from a directory where I put https://imgur.com/a/W5UMe](this) image and set the Loop property to true and what I was able to capture via VLC is https://imgur.com/a/c6xoX.
Also I noticed that when I try to stream the camera it happens to play 100 frames very very quickly like in under a second, then it freezes and waits for a couple of seconds and the stream resumes and plays the next couple of frames really quickly then it freezes again.
What am I doing wrong? Is there a framerate or a timestamp property that needs to be set? I tried to play with VLC's buffering setting but it didn't help.
Thanks
It's hard to say if what your describing is GC from the server or network jitter on the client playing the stream without taking a closer look.
The stream will set the timestamps based on the wall clock when not looping so your shouldn't have to do anything other then send your frames when you need to send them.
I would suggest limiting the frames which you send to the client, i.e. send at a lower frame rate and see if that provides you with further insight as to where your problem lies.