Memory leaks playing Shoutcast/Icecast streams in Flash

Update: Zach Callear has published a complete shoutcast player in flash without memory leaks based on the tricks described in this post.

Update: For those interested in an easy streaming solution, try haxeVideo.

Trying to play music on the Wii, I’ve encountered a problem in the flash Sound object.

While you listen to an mp3 in streaming mode: snd.loadSound(url, true), flash will keep in memory the music played.
A Shoutcast/Icecast stream like an Internet radio, is like a never ending mp3, witch take a large amount of memory, and eventually make the player crash. In the Wii this happen after few minutes.

After some googling, I’ve found that the only solution is to reload the stream after some time, freeing the buffer.
On a pc this can be made every 30 minutes, but on the Wii every 2 minutes.

To avoid the pause when flash reload the stream, is possible to preload in the background another Sound object, and then crossfade the two. In this way there is only an imperceptible glitch due to the lack of synchronization of the two streams.

So here how to do a radio player in flash without the memory leak:

  1. In a Sound object snd1 load the stream
  2. After some time (2 minutes on the Wii, depending on the bitrate), start loading another Sound object, snd2
  3. After a “buffer time” (depending on the connection, 10 second should be fine) fade the two Sound
  4. Free the fist sound snd1 = null
  5. Repeat from step 2

Remember that you have to pass a movieclip to the Sound object:
[as] var snd1 = new Sound(_root.sndMc1);
[/as] And when you free the object you have to wait a frame for the garbage collector to free the memory:
[as] snd1 = null;
//next frame..
snd1 = new Sound(_root.sndMc1);
[/as] Update: You can reload the same stream in another Sound object in this way:
[as] snd2.loadSound(“http://server:8000/;”+Math.random());
[/as] Without the ";"+Math.random() Flash will reuse the same connection

Here the source code of a proof-of-concept player:
Internet radio flash player

29 thoughts on “Memory leaks playing Shoutcast/Icecast streams in Flash

  1. dani says:

    Just wanted to tell ya I have been working on a flash webased player for ages for the radio station i work for…. and you my friend have helped me tremendously.. I put a player together and wahla we have a working flash player for shoutcast stream !! woot.. here is the primitive version http://www.just-believe.net/wiinamp.html .. now im just trying to figure out how to parse the stats from the radio site to mine.. Errr none seem to be working.. anyhow rock on my friend..!! supurb job and TY TY TY!!! you have helped tons!!

    Dani

  2. Madarco says:

    Thanks,
    it is possible to see the sources?
    For the stats, there should be an .xml file with the current song and some other stats, otherwise you can parse the html stats (just open the radio url in your browser).

    Your player seems to work good, however, the memory leaks I’m talking about are visible only with a small amount of ram and an high bitrate radio.
    On a modern pc and a internet radio those are difficult to notice. In my tests on the wii, WiiHear.com (64Kbps?) do “refresh” the opera browser every ~10 minutes. With highest bitrates this happens every 3-2 minutes.

  3. sniurs says:

    Hello, can anyone tell me how to do flash player for my website to play my shoutcast radio, i’ve been trying to do this for a long time but it doesnt work for me, so maby you can help me with this?

  4. dani says:

    Madarco ,

    hiya, i wasnt sure if this post went thru had a lil error.. guess it was hiding.. LOL.. anyhow.. Im STILL working on the stats.. what source were you referring too?? the fla? Im very new to flash.. so im just plugging and playing til i get something that works.. I took out the second sound.. for the memory leaks. because it was playing double.. I also noticed that the sound plays a bit loud at first. and then gets lower.. there are definitly some sound issues in my code.. I think i also need to adjust the codecs when i save im just not sure what format it needs to be in to sound quality when you turn up the volume.. (it sounds a lil distorted when you turn up the volume ) meaning i heard the song twice.. the second one was very low.. but.. i still heard it.. so.. Im still working on that.. i might retry the code exactly the way you have it unless you think you can fix it.. anyhow i would also be greatful for any help you are willing to give.. , since im still a newbie at this.. Also i have a now playing page its in php.. and ive tried a few things to parse the stats to the player with no luck.. anyhow let me know what you need ill be glad to share..
    Dani

  5. Madarco says:

    I suggest you to use one of the players, the one in this article is only a proof of concept.
    For the “now playing” you can parse the html page you see connecting to your shoutcast server with a web browser. I know that the shoutcast server has an XML file with the “now playing” informations but I don’t remember the name.

  6. frayde says:

    Hi,

    i’ve tried your wiiamp on a macbook and on a psp. i’m really sorry but memory leak is still present. On psp you just have to wait 30 sec to see a message “not enough memory”, and you must reboot your psp. On macbook, Virtual memory’s growing with no end and it crashes when 80gb of virtual memory is reached.

    I’m looking exactly for this flash player concept and so i ran a lot of test, on every plateforme i’ve tested, if you wait enough it will crash. Virtual memory is never released. I’ve read somewhere, perhaps on your site, that garbage collector waits next frame for freeing memory and in your code i only see one frame. I’m not flash guru but javascript one, so i can understand your code but not as you’re. If i can help you by testing , i own a wii, psp, macbook, pc, i will pleased to help you.

    Sorry for my english but i’ve learned it over the web so i may seem rude and it’s not my point :).

  7. Madarco says:

    Hi frayde,
    don’t worry for your English, mine is worst 🙂

    I’m really interested in your experience with the player, I will do some tests in the next days and let you know, maybe the example I’ve posted has some bug. In my experience, the wii doesn’t crash, so shouldn’t be memory leaks, however I’m not sure of this.

    In the mean time, have you tried http://www.wiihear.com/?
    They use a low bitrate, so the error message should pop later, but the problem should persist.
    What I’m trying to discover is if someone has found a solution, since it seems that everyone are just ignoring it.

    However, I have to warn you, even if with this method we could tackle the memory leaks, we couldn’t ever suppress the small glitch when the two tracks crossfade.
    A better solution may came from using FLV streams with red5: http://osflash.org/red5

  8. frayde says:

    Madarco,

    i’m gonna benchmark wiihear to see if the virtual memory problem is the same. I know about glitch but it doesn’t really worry me, because there are really not disturbing and overall quality is good. My purpose is just to not change my icecast server by a flash based one, because Red5 can handle up to 1000 connections and i probably need 5000 and more. Flash Media Server is too expensive for me. I’m not sure if we have a way to not fill virtual memory of a web-browser this way without reloading web-browser and restart the process. As i say, i’m javascript guru so actionscript is not really clear for me right now. I’m trying some idea based on your code and if i find something i’ll tell you.
    see ya

  9. Madarco says:

    I don’t know how many connections can handle Red5, it is surely more heavyweight than icecast but it uses the Mina library for the connections, so it should handle some thousand.

    For the memory leaks, I’ll do some test and I’ll let you know. I’m sure we can find a way 🙂

  10. frayde says:

    I’ve tried wiihear, and it has the same virtual memory filling problem. It’s slower because of the low bitrate but it’s the same. PSP is a really good plateform to test because it crashes in 1 to 2 min max. I’ve tried to split your code and concept in to 2 frames just to check if the garbage collector between two frames can solve this problem and i must admit that i can’t get it work at all :). Flash isn’t really my cup of tea. I’m not giving up because i think there’s alway a way :).

  11. frayde says:

    There’s no virtual or real memory problem on http://www.finetune.com/wii, but i think it’s due to streaming protocol which is rtmp://. Rtsp or Rtmp are a server side solution, not a client workaround like playing icecast streaming in flash player. I haven’t worked this week end, but i’ll still working on a workaround base on your code soon.

  12. Madarco says:

    So as I feared, the only way is to use Red5.
    Macromedia dominated for many years the field of the e-learning with Breeze.
    Everyone could have made a Breeze client but the only way to do audio/video communications in Flash is through an expensive license of Flash Media Server and to be sure nobody found a workaround they make the mp3 player as it is. Nice shot Macromedia 🙂

  13. Madarco says:

    I’ve already seen that, but I’ve tried it and seems to not solves the problem (still memory leaks).
    I’ve tried the free version obviously, maybe the “pro” is different.

    However, there will be always small gaps when the player free the memory.

    On a modern computer this will happen every 20-30 minutes, so its reasonable, but on the Wii or some other limited device that could happen every 2-3 minutes, so it isn’t feasible.

  14. Tom says:

    I was led to believe that within AS3 this issue can be resolved more easily, as previously in AS2 you had no access to the flash garbage collector, but now you do in AS3, I believe AS3 also allows proper socket connections.

    So this should clear the stored up data, which kills the player, browser and sometimes slows the heck out of a comp.

    If I’m wrong then I stand corrected, lol.

  15. Madarco says:

    Hi Tom,
    I don’t know if in AS3 this can be fixed, I haven’t tried yet.
    Since I’m interested in no-pc (wii, psp), AS3 isn’t an option.

  16. Tom says:

    Hi Madarco,

    I have to be honest I don’t know anything about the Wii or psp, well except from a consumer angle, lol.

    But your talking about actionscript2 or less I believe?

    Does ActionScript3 in Flash not work on the Wii or psp then?

    I just thought although they we’re talking about different formats, the flash would be cross platform, but I probably am of course wrong, lol.

  17. Madarco says:

    PSP, Symbian phones, Wii and all the small devices support only Flash Lite. The latest version of Flash Lite (2.1) is equivalent to Flash 7, so is only AS2 🙂

  18. blprnt says:

    For those of you who may, in the future, bang your head against this problem in the same way as I did, you might want to know that using a custom subclass of the Sound class will result in unpredictable disposal of the sound objects when you use the my_snd = null trick. It seems that only true members of the Sound class can handle streaming (and stopping streaming) properly.

  19. chris says:

    Hey, I’ve been trying to implement this for a while in Flash CS3 and have found that after a couple of swaps, the sound stops loading and playing altogether, in both of the two “swapping” sound objects. Any ideas? I can’t seem to figure out why other than it’s just some kind of memory leak.

  20. Pingback: SHOUTCast, SHOUTCast, Let It All Out « Nerdabilly

  21. Madarco says:

    I haven’t tried it yet in AS3, however, I’ll suggest you to focus on Red5 streaming server that solves this problem at the root and doesn’t produces the gaps that this solution does.

  22. Pingback: Madarco DevBlog » Blog Archive » haxeVideo

  23. Daniel says:

    hi guys, discussions you posted about appending hte stream.mp3 to the port really saved my day. I thought I was never going to work out why my mp3 player wouldn’t play in the browser, but instead requested text/html.
    I appended it as follows, :80/;stream.mp3, and the browser correctly downloads interprets it as an mp3 stream. Thanks a bunch.

Comments are closed.