I am working on a live streaming project. In that we need to show live scenes on local screens along with broadcasting. But for this, use of encoded stream is not a good choice as encoded stream has delay which can not be use as live feed. So I got an idea to create an archive file during encoding which we can use for display on live field.
The code I am using for encoding is (I'm using Microsoft expression Encoder for encoding):
public void PushVideoToPubPoint(LiveJob job)
{
try
{
string strPublishPointUrl = new Uri("http://My_server/PushToPublishPoint/LiveSmoothStream.isml");
if (!AddPreset())
MessageBox.Show("Could not locate preset containing {0}, will use defaults.", GlobalVar.PresetFile);
var format = new PushBroadcastPublishFormat();
format.PublishingPoint = new Uri(strPublishPointUrl);
fileName = String.Format(@"c:\Video_{0:ddMMyyyy_hhmmss}.ismv", DateTime.Now);
var fileOut = new FileArchivePublishFormat(fileName);
job.PublishFormats.Add(format);
job.PublishFormats.Add(fileOut);
job.ActivateSource(_deviceSource);
job.Status += new EventHandler<EncodeStatusEventArgs>(OnProgress);
job.StartEncoding();
if (_deviceSource != null)
{
_deviceSource.PreviewWindow =
new PreviewWindow(new HandleRef(panelVideoPreview, panelVideoPreview.Handle));
job.ActivateSource(_deviceSource);
}
ConvertAssetFileToByteArray(fileName, job);
}
catch (Exception ex)
{
toolStripStatusLabel1.Text = "Error: " + ex.Message;
oEncodingStatus = false;
}
}
Here, I am using FileArchivePublishFormat to create and archive file with PushBroadcastPublishFormat. Now, I need to access "Live" this archived file for play but as this file is already in use so I can't play it directly. So, I idea what is running in my mind is to read the bytes from this file by someway. So, I am using a method ConvertAssetFileToByteArray for doing this and trying to code like this:
public static void ConvertAssetFileToByteArray(string filename, LiveJob job)
{
byte[] aAssetFileInBytes = null;
try
{
using (FileStream fsAsset = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var reader = new StreamReader(fsAsset))
{
long offset=0;
while (job.IsCapturing)
{
var bytesInFile = (int)fsAsset.Length;
aAssetFileInBytes = new Byte[bytesInFile];
long bytesRead = fsAsset.Read(aAssetFileInBytes, (int) offset, bytesInFile);
offset = bytesInFile;
}
}
}
}
catch (Exception e)
{
}
}
But, It's still giving the error:
The process cannot access the file 'c:\Video_02082013_034400.ismv because it is being used by another process.
Somebody please take me out of this issue.
Any other idea or suggestion will also be appreciated.
Thanks.