I implemented background video processing using .net ffmpeg wrapper http://www.mediasoftpro.com with progress bar indication to calculate how much video is processed and send information to web page to update progress bar indicator. Its working fine if only single process works at a time, but in case of two concurrent processes (start two video publishing at once let say from two different computers), progress bar suddenly mixed progress status. Here is my code where i used static objects to properly send information of single instance to progress bar.
static string FileName = "grey_03";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.Params["file"] != null)
{
FileName = Request.Params["file"].ToString();
}
}
}
public static double ProgressValue = 0;
public static MediaHandler _mhandler = new MediaHandler();
[WebMethod]
public static string EncodeVideo()
{
// MediaHandler _mhandler = new MediaHandler();
string RootPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath);
_mhandler.FFMPEGPath = HttpContext.Current.Server.MapPath("~\\ffmpeg_july_2012\\bin\\ffmpeg.exe");
_mhandler.InputPath = RootPath + "\\contents\\original";
_mhandler.OutputPath = RootPath + "\\contents\\mp4";
_mhandler.BackgroundProcessing = true;
_mhandler.FileName = "Grey.avi";
_mhandler.OutputFileName =FileName;
string presetpath = RootPath + "\\ffmpeg_july_2012\\presets\\libx264-ipod640.ffpreset";
_mhandler.Parameters = " -b:a 192k -b:v 500k -fpre \"" + presetpath + "\"";
_mhandler.OutputExtension = ".mp4";
_mhandler.VCodec = "libx264";
_mhandler.ACodec = "libvo_aacenc";
_mhandler.Channel = 2;
_mhandler.ProcessMedia();
return _mhandler.vinfo.ErrorCode.ToString();
}
[WebMethod]
public static string GetProgressStatus()
{
return Math.Round(_mhandler.vinfo.ProcessingCompleted, 2).ToString();
// if vinfo.processingcomplete==100, then you can get complete information from vinfo object and store it in database and perform other processing.
}
Here is jquery functions responsible for updating progress bar indication after every second etc.
$(function () {
$("#vprocess").on({
click: function (e) {
ProcessEncoding();
var IntervalID = setInterval(function () {
GetProgressValue(IntervalID);
}, 1000);
return false;
}
}, '#btn_process');
});
function GetProgressValue(intervalid) {
$.ajax({
type: "POST",
url: "concurrent_03.aspx/GetProgressStatus",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Do something interesting here.
$("#pstats").text(msg.d);
$("#pbar_int_01").attr('style', 'width: ' + msg.d + '%;');
if (msg.d == "100") {
$('#pbar01').removeClass("progress-danger");
$('#pbar01').addClass("progress-success");
if (intervalid != 0) {
clearInterval(intervalid);
}
FetchInfo();
}
}
});
}
The problem arises due to static mediahandler object
public static MediaHandler _mhandler = new MediaHandler();
I need a way to keep two concurrent processes information separate from each other in order to update progress bar with value exactly belong to that process.
I think you have a misunderstanding here.
Because you are using a
static
variable, you are seeing concurrency problems. This is because in ASP.NET,static
variables are shared by all requests because they are once-per-AppDomain until the app pool recycles.You should expose a WCF end-point that you can use to ask your service whether it is finished or not. This service should work a queue and call your
ffmpeg
program for each item in the queue. You haven't mentioned how many users will be calling your page, but it's probably better to design it this way so you can more simply control how many items the server is allowed to process at once. You will also run in to less problems because you are running a process under a service context rather than under ASP.NET.