Friday, 18 April 2008

Duplicate Streams in Flex to media server causing two copies of audio which sounds like an echo

The Flex mx\controls\videoClasses are not quite as up to par as they maybe should be.

What is happening is that when subscribing to a stream on a media server (in my case - wowza), I was finding that it was creating an extra stream when it shouldn't be basically.

After creating an equivalent program in the flash 9 environment, I found that the extra stream was infact created solely in the flex 2 environment. I was then astonished to find that when I moved to Flex 3, I was having the same problem - tut tut Adobe. But because of the many benefits to Flex which on the whole is pretty good, I decided to dig as far as I could (with the rather limited information out there just now) to find a fix and came across two very good sites, one excellent blog by Jeremy LaCivita and of course one from Adobe which gives me the impression that the problem is fixed - it isn't and still exists in Flex 3...

Anyway, the links to the two are as follows:

Blog - http://www.jlacivita.com/2007/11/14/race-conditions-in-flex-videodisplay/
Adobe - http://bugs.adobe.com/jira/browse/SDK-9809

Please feel free to read through them at your own will.

I'll be honest though, reading both basically left me with a couple more questions and had me experimenting for a couple of hours before I could get it to work - (mainly because I didn't understand how to underwrite the adobe classes - will make sense soon).

Before going into the fix, the symptoms created with this problem is that you typically hear two distinct audio streams or can sound like an echo, however this is intermittent and because both streams can play at virtually the same instant in time, you ears can't tell the difference - but most time with me it sounded like an echo...

The other two links provided above give a little more information on what the technical problem is but the fix is to basically kill off any extra streams that exist from within the adobe classes themselves.

The Fix - thank goodness..

I have basically downloaded the files from the adobe link which worked fine with Flex 2 but not Flex 3, so I have got hold of the Flex 3 classes, made the mods myself and included all for you to download here...

Instructions

Like I say, the problem I was having was how to get Flex to pick up the new classes which basically if placed in the correct location of your project folder - in the correct structure, Flex will build and compile before it looks at its own internal classes.

I have included two zip files, one for flex 2 and one for flex 3. If you extract to your desktop the one you need, you will see the structure intact, so you should see only the mx folder. This then contains the other folders so for e.g. mx\controls\videoClasses and there are two files in here named NCManager.as and VideoPlayer.as

For flex 2 build, the above two files are the only ones required and included. For the flex 3 build there is another file buried in there under mx\core called Version.as which of course this needs.

In order for Flex to pick this up on a Flex 2 project, simply include the mx folder at the root level.

In order for Flex to pick this up on a Flex 3 project, simply include the mx folder within the src folder.

In my case, I started the project in Flex 2, then imported to Flex 3 half way through, therefore there was no src folder, so the root was fine. I did of course use the files under Flex3VideoStreamFix.zip because it was still the Flex 3 compiler being used after the import (not that this is important).

So guys when you build, you should find that there is only one stream created and your problems should in theory go away...

I was in a real downer when I came across this problem but thanks to the above two links, I got it sorted, this document basically serves to make the process a little easier for you whether you are using Flex 2 or Flex 3.

Here are the files:

For Flex 2 - http://www.onlineinteract.com/flexvideostreamfix/Flex2VideoStreamFix.zip
For Flex 3 - http://www.onlineinteract.com/flexvideostreamfix/Flex3VideoStreamFix.zip

Disclaimer...

Please find that I cannot be held responsible for anything that may happen to your Flex build, please use these classes at your own risk...

What I will say though is that you are simply including them in your project in question - it shouldn't overwrite you Flex classes.

One other thing that might be helpful to know:

If you wish to view the Flex classes you will find it in a standard windows install under C:\Program Files\Adobe\Flex Builder 3\sdks\3.0.0\frameworks\projects\framework\src\mx\

for Flex 3 of course.

You might be amused to find that Adobe issues statements and declares variables such as stuff:Object which we personally thought was supreme... Well done guys, great to see that level of inspiration at Adobe Labs going on...

Well, I hope you guys find this helpful as I was extremely grateful to find the following two sites above and was extremely relieved to find that this works - so much so that I thought I should share my contribution with anyone else having this problem.

I would also like to thank two of my colleagues for helping me get this problem solved so thanks William and Douglas.

Good Luck and all the best

Gary Black