Click here to Skip to main content
14,384,033 members
Rate this:
Please Sign up or sign in to vote.
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");

            // Applying the preset to the Encoding.
            if (!AddPreset())
                MessageBox.Show("Could not locate preset containing {0}, will use defaults.", GlobalVar.PresetFile);

            var format = new PushBroadcastPublishFormat();

            format.PublishingPoint = new Uri(strPublishPointUrl);

            // Sets up publishing format for file archival type
            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);

            //Sets up progress callback function
            job.Status += new EventHandler<EncodeStatusEventArgs>(OnProgress);

            //// Starts encoding
            job.StartEncoding();

            if (_deviceSource != null)
            {
                // Sets preview window to winform panel hosted by xaml window
                _deviceSource.PreviewWindow =
                    new PreviewWindow(new HandleRef(panelVideoPreview, panelVideoPreview.Handle));

                // Make this source the active one
                job.ActivateSource(_deviceSource);
            }

            // Read bytes from archived file.
            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
            {
                // Create the file stream to be used
                // to read the asset file.
                using (FileStream fsAsset = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (var reader = new StreamReader(fsAsset))
                    {
                        long offset=0;
                        while (job.IsCapturing)
                        {
                            // Instantiate the byte array.
                            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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100