C # Use Emgu CV to play streaming media (RTSP ),
Emgu CVIs a cross platform. net wrapper to the OpenCV image processing library. allowing OpenCV functions to be called from. NET compatible programming ages such as C #, VB, VC ++, IronPython etc. the wrapper can be compiled by Visual Studio, Xamarin Studio and Unity, it can run on Windows, Linux, Mac OS X, iOS, Android and Windows Phone.
Emgu CV: http://www.emgu.com/wiki/index.php/main_page. download the 3.0 forbidden file. Emgu CV integrates ffmpeg.
Capture _capture = new Capture(fileName); //rtsp://user:password@192.168.1.66:554/ _capture.ImageGrabbed += capture_ImageGrabbed;_capture.Start();
Private void capture_ImageGrabbed (object sender, EventArgs e)
{
Try
{
Mat frame = new Mat ();
Lock (lockObj)
{
If (capture! = Null)
{
If (! Capture. Retrieve (frame ))
{
Frame. Dispose ();
Return;
}
If (frame. IsEmpty)
Return;
// You can use the ImageBox provided by Emgu CV to display the video or convert it to BitmapSource.
// Omitted
}
}
}
Catch (Exception ex)
{
}
}
public static class BitmapSourceConvert { /// <summary> /// Delete a GDI object /// </summary> /// <param name="o">The poniter to the GDI object to be deleted</param> /// <returns></returns> [DllImport("gdi32")] private static extern int DeleteObject(IntPtr o); /// <summary> /// Convert an IImage to a WPF BitmapSource. The result can be used in the Set Property of Image.Source /// </summary> /// <param name="image">The Emgu CV Image</param> /// <returns>The equivalent BitmapSource</returns> public static BitmapSource ToBitmapSource(IImage image) { using (System.Drawing.Bitmap source = image.Bitmap) { IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( ptr, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); DeleteObject(ptr); //release the HBitmap return bs; } } }
Note: Because ffmpeg uses TCP blocking by default to establish a connection, the connection timeout time is long. If the connection fails, blocking will occur. You need to set the ffmpeg connection mode or set the timeout time.
Setting method:
Public bool SetCaptureProperty (CapProp property, double value); // The Capture method. The specific settings are unknown, or Emgu CV does not provide this interface