Problem when trying to stream encrypted movies.

.NET Edition of the F-IN-BOX
Roomie
Posts: 22
Joined: Wed Dec 19, 2007 6:05 am

Problem when trying to stream encrypted movies.

Postby Roomie » Mon Sep 15, 2008 1:42 pm

My application used to run fine until I encrypted the movie (flv) files my application uses. Now for some users it sometimes crashes and leaves them with this error message:

Error message wrote:System.IO.IOException: An I/O-error occurred,
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at f_in_box__lib.StreamBasedOnIStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.Security.Cryptography.CryptoStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at Pausit.FromStreamToStreamWriter.Main()


My guess is that the requested file has not been decrypted enough when my original stream tries to read more, but that’s just my guess.

My question is if there’s any way I can make sure that the crypto stream has buffered up enough before it gets read by the original stream. Or even better if it can decrypt the whole movie before it gets read.

Any help appreciated since I’m a bit stuck on this.
I’m still not all that comfortable when working with streams so please give me descriptive suggestions if you can.

Patrik

My code looks like this:

Code: Select all

private void OnLoadExternalResourceByFullPath(object sender, String URL, System.IO.Stream Stream, ref bool Handled)
{
    string tempString = Application.StartupPath + "\\VideoData\" + URL.Substring(11);

    System.IO.FileStream FLVStream = System.IO.File.OpenRead(tempString);

    FromStreamToStreamWriter writer = new FromStreamToStreamWriter(FLVStream, Stream);

    Handled = true;
}


Code: Select all

class FromStreamToStreamWriter
    {
        internal System.IO.FileStream FromStream;
        internal System.IO.Stream ToStream;
        internal System.Threading.Thread Thread;

        public FromStreamToStreamWriter(System.IO.FileStream FromStream, System.IO.Stream ToStream)
        {
            this.FromStream = FromStream;
            this.ToStream = ToStream;

            Thread = new System.Threading.Thread(new System.Threading.ThreadStart(Main));

            Thread.Start();
        }

        private void Main()
        {

           
            PasswordDeriveBytes pdb = new PasswordDeriveBytes("Password", new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
           
            Rijndael alg = Rijndael.Create();
                       
            alg.Key = pdb.GetBytes(32);
            alg.IV = pdb.GetBytes(16);

            CryptoStream cs = new CryptoStream(ToStream, alg.CreateDecryptor(), CryptoStreamMode.Write);

            const int size = 64 * 1024;
            byte[] buffer = new byte[size];
            int ReadBytes;

            while ((ReadBytes = FromStream.Read(buffer, 0, size)) > 0)
            {
                try
                {
                    cs.Write(buffer, 0, ReadBytes);
                }
                catch (System.IO.IOException e)
                {
                    MessageBox.Show(e.ToString());
                    break;
                }
            }

            cs.Close();
            FromStream.Close();
        }
    }

Softanics
Site Admin
Posts: 1402
Joined: Sat Sep 18, 2004 3:03 am
Location: Russia, St. Petersburg
Contact:

Postby Softanics » Mon Sep 15, 2008 5:55 pm

Thank you for your question.

ToStream.Write can throw IOException when Flash stops loading for some reason, for example, when new movie / resource should be loaded.

Does is it really crash? You handle IOException in your code, right?

Thank you.
Best regards, Artem A. Razin,
F-IN-BOX support
Ask your question here: http://www.f-in-box.com/support.html

Roomie
Posts: 22
Joined: Wed Dec 19, 2007 6:05 am

Postby Roomie » Mon Sep 15, 2008 6:14 pm

Thank you for helping out.

Well first of, I can't get this error on my own computer (or any computer I’ve tried the application on) so it’s kind of hard to bug test. But evidently the error occurs on my users machines. Secondly I never had anyone report this error before I encrypted the movies.

How would you suggest I handle such an exception? Could you give me some code sample or some descriptions on what I want do, as I said in my original post I’m still not so comfortable when it comes to handling streams.

Thanks again
Patrik

Roomie
Posts: 22
Joined: Wed Dec 19, 2007 6:05 am

Postby Roomie » Tue Sep 16, 2008 10:55 am

Or if my previous question was unclear, what exactly could I do to distinguish such an exception from other IOExceptions.

I’ve read something about InnerException. Is that something I could use here or is it not valid?

Thanks again
Patrik

Softanics
Site Admin
Posts: 1402
Joined: Sat Sep 18, 2004 3:03 am
Location: Russia, St. Petersburg
Contact:

Postby Softanics » Wed Sep 17, 2008 8:22 am

Your code seems to be correct.

When this exception throws, the block:

Code: Select all

   catch (System.IO.IOException e)
                {
                    MessageBox.Show(e.ToString());
                    break;
                }


executes. This is correct.
Best regards, Artem A. Razin,

F-IN-BOX support

Ask your question here: http://www.f-in-box.com/support.html

danielgianni
Posts: 8
Joined: Thu Nov 22, 2007 1:33 pm

Catch all Exception

Postby danielgianni » Thu Sep 18, 2008 1:42 pm

Get your all Exception to details error:

Code: Select all

catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
    break;
}


This code shows all the details of any exception. This code display all details from any exception. Easy to debug!


Softanics wrote:Your code seems to be correct.

When this exception throws, the block:

Code: Select all

   catch (System.IO.IOException e)
                {
                    MessageBox.Show(e.ToString());
                    break;
                }


executes. This is correct.

Softanics
Site Admin
Posts: 1402
Joined: Sat Sep 18, 2004 3:03 am
Location: Russia, St. Petersburg
Contact:

Postby Softanics » Sat Sep 20, 2008 8:48 am

You should handle IOException separately. Just ignore it. It means that the loading has stopped.

Thank you.
Best regards, Artem A. Razin,

F-IN-BOX support

Ask your question here: http://www.f-in-box.com/support.html


Return to “.NET Edition”

Who is online

Users browsing this forum: No registered users and 15 guests

cron