Problem with streaming flvs

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

Problem with streaming flvs

Postby Roomie » Sun Feb 10, 2008 10:17 am

Hey Artem, sorry that I bother you again but I seem to have problem again :oops:

I get a problem with my stream when I try to load multiple flvs at the same time. Everything works fine if I only load a couple of flvs but when I try to load more than 4 it seems like they get mixed up. OK, I know this is probably not the best explanation but I'm not sure how to describe it better.

In my Flash file I have 2 video controls layered on top of each other and I'm loading an flv into each of them and when these are finished I load another set of flvs (se my code snippets for more info).

If you know how I can do this better please let me know. And last I can just say that my AS code works when I run it in Flash player, which makes me think it has something to do with how I load the streams in my C# code and how the F in box control handles the streams.

Here's my relevant code:

ActionScript:

Code: Select all

var videosPath:String = "http://FLV/";
var videos:Array = new Array("0001MNS01.flv", "0000MNL03.flv", "0003MNI01.flv", "0003MNL01.flv", "0003MNO01.flv", "0000MNL03.flv", "0002MNS01.flv");
var markings:Array = new Array("0001TNS01.flv", "", "", "", "", "", "0002TNS01.flv");
var duration:Number = 0;
var timePlayed:Number = 0;
var ratio:Number = 0;
var id:Number = 0;
var ready:Boolean = true;

if (videos.length > 0) {
   var nc:NetConnection = new NetConnection();
   nc.connect(null);
   var ns:NetStream = new NetStream(nc);
   videoPlayer.attachVideo(ns);
   
   var nc2:NetConnection = new NetConnection();
   nc2.connect(null);
   var ns2:NetStream = new NetStream(nc2);
   markingsVideoPlayer.attachVideo(ns2);
   
   if (videos[currentVideo] != ""){
      ns.play(videosPath + videos[currentVideo]);
   }
   if (markings[currentVideo] != ""){
      ns2.play(videosPath + markings[currentVideo]);
   }
      
   ns.onMetaData = function(evt:Object):Void  {
      duration = evt.duration;
      
      id = setInterval(function ():Void {
         track._width = (ns.time*ratio)+(timePlayed*ratio)+1;
      }, 50);
      ready = true;
   }

   ns.onStatus = function(evt:Object):Void  {
      if (ready && this.time>0 && this.time>=(duration-0.5)) {
         ready = false;
         clearInterval(id);
         timePlayed += duration;
         trace("timePlayed: "+timePlayed);
         currentVideo++;
         if (currentVideo<videos.length) {
            ns.play(videosPath + videos[currentVideo]);
            
            if (markings[currentVideo] != ""){
               ns2.play(videosPath + markings[currentVideo]);
            }else{
               ns2.close();
               markingsVideoPlayer.clear();
            }
         } else {
            delete this.onStatus;
            play(); //Move to next scene
         }
      }
   }
   
} else {
   play(); //Move to next scene
}



C#:

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);

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

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

            Stream.Close();

            Handled = true;
        }

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

Postby Softanics » Mon Feb 11, 2008 8:11 am

Thank you for your question.

What if you provide a FLV content in a separate thread, the same result?

If yes, could you please upload anywhere all sources and send me link to support at f-in-box.com ?
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 » Wed Feb 13, 2008 5:36 am

I tried using some of your code from Sample 8 but I can't get it to work.

This is the code I have in my form class:

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);
       
            Stream.Close();

            Handled = true;
        }


And here's my somewhat modified FromStreamToStreamWriter class:

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()
        {
            const int nSize = 64 * 1024;
            byte[] buffer = new byte[nSize];

            int nReadBytes;

            while (true)
            {
                nReadBytes = FromStream.Read(buffer, 0, nSize);

                if (0 == nReadBytes)
                    break;

                try
                {
                    ToStream.Write(buffer, 0, nReadBytes);     //this is line 320
                }
                catch (System.IO.IOException e)
                {
                    // Loading is interrupted
                    MessageBox.Show(e.ToString());
                    break;
                }
            }
               

            FromStream.Close();
            ToStream.Close();
        }
    }


And this is the error I get:
System.IO.IOException: 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 Pausit.FromStreamToStreamWriter.Main() in D:\Bits and Pixels\Kunder\Pausit\Application\FormExercises.cs:line 320


Line 320 holds this code: ToStream.Write(buffer, 0, nReadBytes);

Any help appreciated.

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

Postby Softanics » Wed Feb 13, 2008 11:02 am

Roomie wrote:I tried using some of your code from Sample 8 but I can't get it to work.

This is the code I have in my form class:

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);
       
            Stream.Close();

            Handled = true;
        }


Don't close Stream... Stream is using into the thread.
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 » Wed Feb 13, 2008 6:48 pm

oops, silly mistake :oops:
I removed the close on Stream, but it still doesn't work.
It's back to the same behaviour as stated in my original post.

I know you asked me to send you the code but it's not that easy since it contains a lot of proprietary code.

I'm thinking I might need to preload the movies before playing them. Do you think that could be a solution and if so would you know how to go about doing that.

As always, any help appreciated.

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

Postby Softanics » Wed Feb 13, 2008 8:27 pm

You can send me only FLA / SWF file with some 4 FLVs -- just to help me to see how it works under usual flash player. After that, I will prepare a project that provides FLVs from resources, and, hopefully, we'll resolve the issue. Is it possible?
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 » Wed Feb 13, 2008 8:41 pm

Yes I can prepare that, where would you like me to send the files?
Thanks for the help!

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

Postby Softanics » Wed Feb 13, 2008 9:11 pm

Roomie wrote:Yes I can prepare that, where would you like me to send the files?


support (at) f-in-box.com
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 11 guests

cron