使用 C#/ASP.NET MVC 的逐帧 MJPEG 流式传输
Frame-by-frame MJPEG streaming with C#/ASP.NET MVC
我一直在尝试在 ASP.NET 中设置 MJPEG 流。我想从 URL 检索 MJEPG 流,并将我获得的每一帧发送到每个连接的客户端。我能够找到的示例仅从一组文件中读取,而不是来自 URL 的连续流,并通过 MultiStreamContent 发送整个文件。由于我是逐帧检索的,因此无法执行此操作。
我想知道是否可以用 ASP.NET MVC 做我想做的事。我目前正在使用 AForge 视频从 link 检索 MJPEG 流。
我的控制器代码 class:
using System.Net.Http;
using System.Web.Http;
using AForge.Video;
namespace VideoPrototypeMVC.Controllers
{
public class CameraController : ApiController
{
int framecounter = 0;
MJPEGStream stream = new MJPEGStream();
[HttpGet]
public void GetVideoContent()
{
stream.Source = @"http://127.0.0.1:5002/stream";
stream.NewFrame += new NewFrameEventHandler(showFrame);
stream.Start();
MultipartContent content = new MultipartContent();
while (stream.IsRunning)
{
//Continues streaming should be here?
}
}
//Can be used to display of a frame is available
private void showFrame(object sender, NewFrameEventArgs eventArgs)
{
framecounter++;
System.Diagnostics.Debug.WriteLine("New frame event: " + framecounter);
}
//Should be called at the end of the stream
private void stopStream(object sender, ReasonToFinishPlaying reason)
{
System.Diagnostics.Debug.WriteLine("Stop stream");
stream.Stop();
framecounter = 0;
}
}
}
这段代码不是最终的,但我只需要继续流下来。我找到了使用 Socket servers 的示例,但我想坚持使用 MVC,因为它可以让我更轻松地设置服务器的其余部分。
确保其他人也能解决这个问题。我设法将@Evk(再次感谢您)所说的话与我在此处找到的信息结合起来:creating my own MJPEG stream.
请注意:下面的代码只是 prototype/proof-of-concept!当我 运行 由于 StartStream 中的无休止 while 循环,我的处理器会达到 100%。将努力使它更基于事件,但我认为下面的代码更容易解释。
using System;
using System.IO;
using System.Net;
using System.Web;
using System.Net.Http;
using System.Web.Http;
using AForge.Video;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;
using System.Threading;
namespace VideoPrototypeMVC.Controllers
{
public class CameraController : ApiController
{
private MJPEGStream mjpegStream = new MJPEGStream();
private bool frameAvailable = false;
private Bitmap frame = null;
private string BOUNDARY = "frame";
/// <summary>
/// Initializer for the MJPEGstream
/// </summary>
CameraController()
{
mjpegStream.Source = @"{{INSERT STREAM URL}}";
mjpegStream.NewFrame += new NewFrameEventHandler(showFrameEvent);
}
[HttpGet]
public HttpResponseMessage GetVideoContent()
{
mjpegStream.Start();
var response = Request.CreateResponse();
response.Content = new PushStreamContent((Action<Stream, HttpContent, TransportContext>)StartStream);
response.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/x-mixed-replace; boundary=" + BOUNDARY);
return response;
}
/// <summary>
/// Craete an appropriate header.
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
private byte[] CreateHeader(int length)
{
string header =
"--" + BOUNDARY + "\r\n" +
"Content-Type:image/jpeg\r\n" +
"Content-Length:" + length + "\r\n\r\n";
return Encoding.ASCII.GetBytes(header);
}
public byte[] CreateFooter()
{
return Encoding.ASCII.GetBytes("\r\n");
}
/// <summary>
/// Write the given frame to the stream
/// </summary>
/// <param name="stream">Stream</param>
/// <param name="frame">Bitmap format frame</param>
private void WriteFrame(Stream stream, Bitmap frame)
{
// prepare image data
byte[] imageData = null;
// this is to make sure memory stream is disposed after using
using (MemoryStream ms = new MemoryStream())
{
frame.Save(ms, ImageFormat.Jpeg);
imageData = ms.ToArray();
}
// prepare header
byte[] header = CreateHeader(imageData.Length);
// prepare footer
byte[] footer = CreateFooter();
// Start writing data
stream.Write(header, 0, header.Length);
stream.Write(imageData, 0, imageData.Length);
stream.Write(footer, 0, footer.Length);
}
/// <summary>
/// While the MJPEGStream is running and clients are connected,
/// continue sending frames.
/// </summary>
/// <param name="stream">Stream to write to.</param>
/// <param name="httpContent">The content information</param>
/// <param name="transportContext"></param>
private void StartStream(Stream stream, HttpContent httpContent, TransportContext transportContext)
{
while (mjpegStream.IsRunning && HttpContext.Current.Response.IsClientConnected)
{
if (frameAvailable)
{
try
{
WriteFrame(stream, frame);
frameAvailable = false;
} catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e);
}
}else
{
Thread.Sleep(30);
}
}
stopStream();
}
/// <summary>
/// This event is thrown when a new frame is detected by the MJPEGStream
/// </summary>
/// <param name="sender">Object that is sending the event</param>
/// <param name="eventArgs">Data from the event, including the frame</param>
private void showFrameEvent(object sender, NewFrameEventArgs eventArgs)
{
frame = new Bitmap(eventArgs.Frame);
frameAvailable = true;
}
/// <summary>
/// Stop the stream.
/// </summary>
private void stopStream()
{
System.Diagnostics.Debug.WriteLine("Stop stream");
mjpegStream.Stop();
}
}
}
Arastelion 的回答很好,但是我注意到,如果您离开应用程序,那么后台仍有一个请求正在进行,这可能会占用大量资源
在 stream.FlushAsync() 中弹出;
stream.Close();
stream.Dispose();在 stopStream 似乎解决了这个问题之后。
using System;
using System.IO;
using System.Net;
using System.Web;
using System.Net.Http;
using System.Web.Http;
using AForge.Video;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;
using System.Threading;
namespace VideoPrototypeMVC.Controllers
{
public class CameraController : ApiController
{
private MJPEGStream mjpegStream = new MJPEGStream();
private bool frameAvailable = false;
private Bitmap frame = null;
private string BOUNDARY = "frame";
/// <summary>
/// Initializer for the MJPEGstream
/// </summary>
CameraController()
{
mjpegStream.Source = @"{{INSERT STREAM URL}}";
mjpegStream.NewFrame += new NewFrameEventHandler(showFrameEvent);
}
[HttpGet]
public HttpResponseMessage GetVideoContent()
{
mjpegStream.Start();
var response = Request.CreateResponse();
response.Content = new PushStreamContent((Action<Stream, HttpContent, TransportContext>)StartStream);
response.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/x-mixed-replace; boundary=" + BOUNDARY);
return response;
}
/// <summary>
/// Craete an appropriate header.
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
private byte[] CreateHeader(int length)
{
string header =
"--" + BOUNDARY + "\r\n" +
"Content-Type:image/jpeg\r\n" +
"Content-Length:" + length + "\r\n\r\n";
return Encoding.ASCII.GetBytes(header);
}
public byte[] CreateFooter()
{
return Encoding.ASCII.GetBytes("\r\n");
}
/// <summary>
/// Write the given frame to the stream
/// </summary>
/// <param name="stream">Stream</param>
/// <param name="frame">Bitmap format frame</param>
private void WriteFrame(Stream stream, Bitmap frame)
{
// prepare image data
byte[] imageData = null;
// this is to make sure memory stream is disposed after using
using (MemoryStream ms = new MemoryStream())
{
frame.Save(ms, ImageFormat.Jpeg);
imageData = ms.ToArray();
}
// prepare header
byte[] header = CreateHeader(imageData.Length);
// prepare footer
byte[] footer = CreateFooter();
// Start writing data
stream.Write(header, 0, header.Length);
stream.Write(imageData, 0, imageData.Length);
stream.Write(footer, 0, footer.Length);
}
/// <summary>
/// While the MJPEGStream is running and clients are connected,
/// continue sending frames.
/// </summary>
/// <param name="stream">Stream to write to.</param>
/// <param name="httpContent">The content information</param>
/// <param name="transportContext"></param>
private void StartStream(Stream stream, HttpContent httpContent, TransportContext transportContext)
{
while (mjpegStream.IsRunning && HttpContext.Current.Response.IsClientConnected)
{
if (frameAvailable)
{
try
{
WriteFrame(stream, frame);
frameAvailable = false;
} catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e);
}
}else
{
Thread.Sleep(30);
}
}
stopStream();
stream.FlushAsync();
stream.Close();
stream.Dispose();
}
/// <summary>
/// This event is thrown when a new frame is detected by the MJPEGStream
/// </summary>
/// <param name="sender">Object that is sending the event</param>
/// <param name="eventArgs">Data from the event, including the frame</param>
private void showFrameEvent(object sender, NewFrameEventArgs eventArgs)
{
frame = new Bitmap(eventArgs.Frame);
frameAvailable = true;
}
/// <summary>
/// Stop the stream.
/// </summary>
private void stopStream()
{
System.Diagnostics.Debug.WriteLine("Stop stream");
mjpegStream.Stop();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
stopStream();
}
base.Dispose(disposing);
}
}
}
我一直在尝试在 ASP.NET 中设置 MJPEG 流。我想从 URL 检索 MJEPG 流,并将我获得的每一帧发送到每个连接的客户端。我能够找到的示例仅从一组文件中读取,而不是来自 URL 的连续流,并通过 MultiStreamContent 发送整个文件。由于我是逐帧检索的,因此无法执行此操作。 我想知道是否可以用 ASP.NET MVC 做我想做的事。我目前正在使用 AForge 视频从 link 检索 MJPEG 流。 我的控制器代码 class:
using System.Net.Http;
using System.Web.Http;
using AForge.Video;
namespace VideoPrototypeMVC.Controllers
{
public class CameraController : ApiController
{
int framecounter = 0;
MJPEGStream stream = new MJPEGStream();
[HttpGet]
public void GetVideoContent()
{
stream.Source = @"http://127.0.0.1:5002/stream";
stream.NewFrame += new NewFrameEventHandler(showFrame);
stream.Start();
MultipartContent content = new MultipartContent();
while (stream.IsRunning)
{
//Continues streaming should be here?
}
}
//Can be used to display of a frame is available
private void showFrame(object sender, NewFrameEventArgs eventArgs)
{
framecounter++;
System.Diagnostics.Debug.WriteLine("New frame event: " + framecounter);
}
//Should be called at the end of the stream
private void stopStream(object sender, ReasonToFinishPlaying reason)
{
System.Diagnostics.Debug.WriteLine("Stop stream");
stream.Stop();
framecounter = 0;
}
}
}
这段代码不是最终的,但我只需要继续流下来。我找到了使用 Socket servers 的示例,但我想坚持使用 MVC,因为它可以让我更轻松地设置服务器的其余部分。
确保其他人也能解决这个问题。我设法将@Evk(再次感谢您)所说的话与我在此处找到的信息结合起来:creating my own MJPEG stream.
请注意:下面的代码只是 prototype/proof-of-concept!当我 运行 由于 StartStream 中的无休止 while 循环,我的处理器会达到 100%。将努力使它更基于事件,但我认为下面的代码更容易解释。
using System;
using System.IO;
using System.Net;
using System.Web;
using System.Net.Http;
using System.Web.Http;
using AForge.Video;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;
using System.Threading;
namespace VideoPrototypeMVC.Controllers
{
public class CameraController : ApiController
{
private MJPEGStream mjpegStream = new MJPEGStream();
private bool frameAvailable = false;
private Bitmap frame = null;
private string BOUNDARY = "frame";
/// <summary>
/// Initializer for the MJPEGstream
/// </summary>
CameraController()
{
mjpegStream.Source = @"{{INSERT STREAM URL}}";
mjpegStream.NewFrame += new NewFrameEventHandler(showFrameEvent);
}
[HttpGet]
public HttpResponseMessage GetVideoContent()
{
mjpegStream.Start();
var response = Request.CreateResponse();
response.Content = new PushStreamContent((Action<Stream, HttpContent, TransportContext>)StartStream);
response.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/x-mixed-replace; boundary=" + BOUNDARY);
return response;
}
/// <summary>
/// Craete an appropriate header.
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
private byte[] CreateHeader(int length)
{
string header =
"--" + BOUNDARY + "\r\n" +
"Content-Type:image/jpeg\r\n" +
"Content-Length:" + length + "\r\n\r\n";
return Encoding.ASCII.GetBytes(header);
}
public byte[] CreateFooter()
{
return Encoding.ASCII.GetBytes("\r\n");
}
/// <summary>
/// Write the given frame to the stream
/// </summary>
/// <param name="stream">Stream</param>
/// <param name="frame">Bitmap format frame</param>
private void WriteFrame(Stream stream, Bitmap frame)
{
// prepare image data
byte[] imageData = null;
// this is to make sure memory stream is disposed after using
using (MemoryStream ms = new MemoryStream())
{
frame.Save(ms, ImageFormat.Jpeg);
imageData = ms.ToArray();
}
// prepare header
byte[] header = CreateHeader(imageData.Length);
// prepare footer
byte[] footer = CreateFooter();
// Start writing data
stream.Write(header, 0, header.Length);
stream.Write(imageData, 0, imageData.Length);
stream.Write(footer, 0, footer.Length);
}
/// <summary>
/// While the MJPEGStream is running and clients are connected,
/// continue sending frames.
/// </summary>
/// <param name="stream">Stream to write to.</param>
/// <param name="httpContent">The content information</param>
/// <param name="transportContext"></param>
private void StartStream(Stream stream, HttpContent httpContent, TransportContext transportContext)
{
while (mjpegStream.IsRunning && HttpContext.Current.Response.IsClientConnected)
{
if (frameAvailable)
{
try
{
WriteFrame(stream, frame);
frameAvailable = false;
} catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e);
}
}else
{
Thread.Sleep(30);
}
}
stopStream();
}
/// <summary>
/// This event is thrown when a new frame is detected by the MJPEGStream
/// </summary>
/// <param name="sender">Object that is sending the event</param>
/// <param name="eventArgs">Data from the event, including the frame</param>
private void showFrameEvent(object sender, NewFrameEventArgs eventArgs)
{
frame = new Bitmap(eventArgs.Frame);
frameAvailable = true;
}
/// <summary>
/// Stop the stream.
/// </summary>
private void stopStream()
{
System.Diagnostics.Debug.WriteLine("Stop stream");
mjpegStream.Stop();
}
}
}
Arastelion 的回答很好,但是我注意到,如果您离开应用程序,那么后台仍有一个请求正在进行,这可能会占用大量资源 在 stream.FlushAsync() 中弹出; stream.Close(); stream.Dispose();在 stopStream 似乎解决了这个问题之后。
using System;
using System.IO;
using System.Net;
using System.Web;
using System.Net.Http;
using System.Web.Http;
using AForge.Video;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;
using System.Threading;
namespace VideoPrototypeMVC.Controllers
{
public class CameraController : ApiController
{
private MJPEGStream mjpegStream = new MJPEGStream();
private bool frameAvailable = false;
private Bitmap frame = null;
private string BOUNDARY = "frame";
/// <summary>
/// Initializer for the MJPEGstream
/// </summary>
CameraController()
{
mjpegStream.Source = @"{{INSERT STREAM URL}}";
mjpegStream.NewFrame += new NewFrameEventHandler(showFrameEvent);
}
[HttpGet]
public HttpResponseMessage GetVideoContent()
{
mjpegStream.Start();
var response = Request.CreateResponse();
response.Content = new PushStreamContent((Action<Stream, HttpContent, TransportContext>)StartStream);
response.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/x-mixed-replace; boundary=" + BOUNDARY);
return response;
}
/// <summary>
/// Craete an appropriate header.
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
private byte[] CreateHeader(int length)
{
string header =
"--" + BOUNDARY + "\r\n" +
"Content-Type:image/jpeg\r\n" +
"Content-Length:" + length + "\r\n\r\n";
return Encoding.ASCII.GetBytes(header);
}
public byte[] CreateFooter()
{
return Encoding.ASCII.GetBytes("\r\n");
}
/// <summary>
/// Write the given frame to the stream
/// </summary>
/// <param name="stream">Stream</param>
/// <param name="frame">Bitmap format frame</param>
private void WriteFrame(Stream stream, Bitmap frame)
{
// prepare image data
byte[] imageData = null;
// this is to make sure memory stream is disposed after using
using (MemoryStream ms = new MemoryStream())
{
frame.Save(ms, ImageFormat.Jpeg);
imageData = ms.ToArray();
}
// prepare header
byte[] header = CreateHeader(imageData.Length);
// prepare footer
byte[] footer = CreateFooter();
// Start writing data
stream.Write(header, 0, header.Length);
stream.Write(imageData, 0, imageData.Length);
stream.Write(footer, 0, footer.Length);
}
/// <summary>
/// While the MJPEGStream is running and clients are connected,
/// continue sending frames.
/// </summary>
/// <param name="stream">Stream to write to.</param>
/// <param name="httpContent">The content information</param>
/// <param name="transportContext"></param>
private void StartStream(Stream stream, HttpContent httpContent, TransportContext transportContext)
{
while (mjpegStream.IsRunning && HttpContext.Current.Response.IsClientConnected)
{
if (frameAvailable)
{
try
{
WriteFrame(stream, frame);
frameAvailable = false;
} catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e);
}
}else
{
Thread.Sleep(30);
}
}
stopStream();
stream.FlushAsync();
stream.Close();
stream.Dispose();
}
/// <summary>
/// This event is thrown when a new frame is detected by the MJPEGStream
/// </summary>
/// <param name="sender">Object that is sending the event</param>
/// <param name="eventArgs">Data from the event, including the frame</param>
private void showFrameEvent(object sender, NewFrameEventArgs eventArgs)
{
frame = new Bitmap(eventArgs.Frame);
frameAvailable = true;
}
/// <summary>
/// Stop the stream.
/// </summary>
private void stopStream()
{
System.Diagnostics.Debug.WriteLine("Stop stream");
mjpegStream.Stop();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
stopStream();
}
base.Dispose(disposing);
}
}
}