ارسال ویدیو بصورت Async توسط Web Api
نویسنده: آرمین ضیاء
تاریخ: ۱۳۹۳/۰۵/۲۶ ۱۵:۱۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
}
} public class VideoStream
{
private readonly string _filename;
private long _contentLength;
public long FileLength
{
get { return _contentLength; }
}
public VideoStream(string videoPath)
{
_filename = videoPath;
using (var video = File.Open(_filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
_contentLength = video.Length;
}
}
public async void WriteToStream(Stream outputStream,
HttpContent content, TransportContext context)
{
try
{
var buffer = new byte[65536];
using (var video = File.Open(_filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var length = (int)video.Length;
var bytesRead = 1;
while (length > 0 && bytesRead > 0)
{
bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
await outputStream.WriteAsync(buffer, 0, bytesRead);
length -= bytesRead;
}
}
}
catch (HttpException)
{
return;
}
finally
{
outputStream.Close();
}
}
} while (length > 0 && bytesRead > 0)
{
bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
await outputStream.WriteAsync(buffer, 0, bytesRead);
length -= bytesRead;
} public class VideoController : ApiController
{
[Route("api/video/{ext}/{fileName}")]
public HttpResponseMessage Get(string ext, string fileName)
{
string videoPath = HostingEnvironment.MapPath(string.Format("~/Videos/{0}.{1}", fileName, ext));
if (File.Exists(videoPath))
{
FileInfo fi = new FileInfo(videoPath);
var video = new VideoStream(videoPath);
var response = Request.CreateResponse();
response.Content = new PushStreamContent((Action<Stream, HttpContent, TransportContext>)video.WriteToStream,
new MediaTypeHeaderValue("video/" + ext));
response.Content.Headers.Add("Content-Disposition", "attachment;filename=" + fi.Name.Replace(" ", ""));
response.Content.Headers.Add("Content-Length", video.FileLength.ToString());
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
}
} [Route("api/video/{ext}/{fileName}")] api/video/mp4/sample
string videoPath = HostingEnvironment.MapPath(string.Format("~/Videos/{0}.{1}", fileName, ext)); var video = new VideoStream(videoPath);
var response = Request.CreateResponse();
response.Content = new PushStreamContent((Action<Stream, HttpContent, TransportContext>)video.WriteToStream, new MediaTypeHeaderValue("video/" + ext)); response.Content.Headers.Add("Content-Disposition", "attachment;filename=" + fileName);
response.Content.Headers.Add("Content-Length", video.FileLength.ToString()); if(File.Exists(videoPath))
{
// removed for bravity
}
else
{
return Request.CreateResponse(HttpStatusCode.NotFound);
} public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
} <div>
<div>
<video width="480" height="270" controls="controls" preload="auto">
<source src="/api/video/mp4/sample" type="video/mp4" />
Your browser does not support the video tag.
</video>
</div>
</div> IIS یک سری ابزار برای اینکار داره:
IIS Media Services: http://www.iis.net/media
Smooth Streaming Client: http://www.iis.net/download/SmoothClient
همچنین اصل کار به DirectX ختم میشه. دو نمونه پیاده سازی:
http://www.codeproject.com/Articles/7637/DirectX-Video-Stream-and-frame-capture
http://channel9.msdn.com/forums/TechOff/93476-Programatically-Using-A-Webcam-In-C/