Influxis Flash Media Interactive Server 3.5 Hosting Specialists


Understanding Buffering with Real-time Streaming Flash Video

One of the most often overlooked aspects of deploying real-time streaming Flash Video is buffering. And when I say “real-time streaming Flash Video”, I mean any FLV file that is served from Adobe Flash Media Server, such as a hosted server on Influxis.com, or a Flash Video Streaming Service provider like Akamai or Limelight. Any URI starting with “rtmp:”, “rtmps:”, or “rtmpt:” is considered a real-time streaming Flash Video.

NOTE: If you’re deploying Flash Video from a web server using HTTP, then the following discussion about buffering does not apply. The “buffer” of a progressive download Flash Video is the amount of the file that has downloaded and cached to the browser.

So, once you’ve decided to use real-time streaming (RTMP) in your Flash Video deployment scenario, you must consider the bitrate (or data rate) of the Flash Video, or FLV, content you will serve to your audience. An FLV file can be encoded with only one bitrate–there’s no such beast as a “multibitrate” (or MBR) Flash Video file, as there is with Windows Media or other popular streaming web video formats. You can create multiple versions of a Flash Video clip, encoding as many bitrates as you would like to make available to your audience, and use ActionScript to pick the right FLV file to serve to the end-user. But, that’s another topic entirely–let’s get back to buffering.

Buffering real-time streaming video is a bit awkward with the Flash Player. Let’s define what a buffer is. The buffer for a Flash Video clip is simply the amount of video content, in seconds, that has temporarily cached to the user’s computer from the Flash Media Server. You can set the buffer time to any value you want, with the NetStream.setBufferTime() method in ActionScript 2.0 (AS2) or the NetStream.bufferTime property in ActionScript 3.0 (AS3). If you’re fond of using the FLVPlayback component (either AS2 or AS3), you can use the FLVPlayback.bufferTime property to also control the buffer time of a real-time streaming Flash Video clip.

Now let me be clear: the buffer time is the amount of video content in seconds to cache on the user’s computer or device before playback of the video can begin. The buffer time is not necessarily how long the user will have to wait until playback starts. For example, if you have a 500Kbps video stream and you set the buffer time of the stream to 3 seconds, then 1500 kilobits (equivalent to 187,500 bytes or 183 KB) of the FLV file needs to download and temporarily cache before the video can start to play. If you have a 1500Kbps connection speed to the Flash Media Server, then you have to wait for only one second before the video begins playback, because that’s the amount of time it would take to buffer 3 seconds of 500 Kbps video. However, if you have a 100Kbps connection to the Flash Media Server, you would have to wait 15 seconds for the first three seconds of the Flash Video clip to buffer.

Now that you understand how the buffer mechanism works with respect to time, let’s look at why the Flash Player manages the buffer with playback states. When you first set the bufferTime property (or call the NetStream.setBufferTime() method in AS2), the Flash Player waits to play the stream until that amount of video time has buffered into memory. Once it has, you can adjust the buffer time to a new value without necessarily interrupting current playback. (Actually, there’s a neat technique called “dual buffering” well-documented by Fabio Sonnati on Adobe DevNet, but again, that’s another topic of discussion.) The Flash Player will also attempt to buffer more video than you’ve requested with your buffer time, permitted that your connection speed can accommodate it. For example, if you set the bufferTime property to 10 seconds, the Flash Player may actually be try to buffer 20 seconds, adjusting the buffer between 10 and 20 seconds as the video plays. The bufferTime property, therefore, is simply the minimum amount of video time that must buffer to the Flash Player before playback can begin (or resume if the buffer has emptied).

Of course, there’s some bad news for buffering in the Flash Player: anytime you seek or pause the Flash Video clip, the entire buffer is dumped. If you’ve thought that you could buffer the entire video clip for slow connection speeds, forget about it–once the user pauses or seeks the video (if those operations are indeed permitted by your playback controls), then the user has to wait all over again for the buffer to refill. This is just a fact of life (or FOL, in geek terminology) with the Flash Player. Even if you’ve buffered the content that is seeked to by the user, the buffer is dumped.

I’ve made a BufferDisplay component (AS2) that you can use as a GUI to set the NetStream.bufferTime (or FLVPlayback.bufferTime) property at runtime. The component also monitors and displays the NetStream.bufferLength property. You can download a ZIP file of the FLA and AS files. Experiment with the working example below, changing the buffer time and seeking to new points in the video clip to see how the buffering process works.

The content in this area requires Flash Player 8 or higher.


15 Responses to “Understanding Buffering with Real-time Streaming Flash Video”

  1. Ajay says:

    >> Even if you’ve buffered the content that is seeked to by the user, the buffer is dumped.

    I also thought the same, bu I think Youtube managed to do some heck to overcome this issue. here is how it plays
    If you pause the video, it still keeps buffering
    If you you seek from within buffered length, it plays from the cache.

    I’m still trying to figure how they managed it. Arn’t they using FMS?

  2. No they are not using FMS! :)

  3. Vrvy says:

    Youtube is using progressive download (with some serverside logic to allow for seeking to certain keyframes) and not streaming.

  4. Abhinav Kapoor says:

    Starting with Flash Player 9,0,115,0 the buffer is no longer dumped when the stream is paused and it will continue buffering on the background.

  5. uberscooter says:

    Hello Robert,

    I bought your book, the Flash CS3 Professional Video and have been working through the CD. I’m also a member of the O’Reilly Safari library where I first found your book—then bot it too. Great work and very helpful.

    My Question:
    I’m trying to get the double buffering technique to work and it’s no problem when I hit the test movie. But as soon as I load the files to my web server to test it, I get a blank page.
    I put a small rectangle symbol just to test that the swf was working and I could see the symbol when I played the file via http from the web server; alas, the video and the buffer text was non-existent still.
    What am I doing wrong? I didn’t see any instructions on doing anything differently for deployment.
    Thank you in advance for your quick response,

  6. Hugo First says:

    Hi Robert,

    How does setBuffertime work on the publishing stream? I have set it to 5 seconds to allow for slow client connection speeds and the recorded video freezing. It seems that the longer the time value, the longer the video freezes for. What is happening here?

  7. werutzb says:

    Hi!

    I want to improve my SQL knowledge.
    I red so many SQL books and want to
    read more about SQL for my work as db2 database manager.

    What would you recommend?

    Thanks,
    Werutz

  8. [...] by DrPunchman on Mar.23, 2009, under ActionScript I’ll come back to this, but for right now here are some helpful links. > Understanding Buffering with Real-time Streaming Flash Video: http://labs.influxis.com/?p=4 [...]

  9. Hi,

    I am an Influxis customer and I’ve implemented the dual buffering method. Still, the playback from progressive download was better. I am still trying to find out a good solution. Great article!

  10. vylan says:

    Can you post the .zip again I get an error when trying to download it.

  11. jr says:

    Hey Robert, cool post.
    First, as vylan said, could you please post the ZIP file again – the link seems to be broken.
    Also, what about HTTP? Is there a way to control the player side buffering of an HTTP stream?
    Thanks,
    Jacky

  12. daneco says:

    is there another link of this zip file, it seems to be broken, this is exactly what ive been looking for, i would see the demo you have made
    thanks

  13. PB says:

    Anybody out there ?!?!
    link please !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  14. Great Post!!, job is really good, but url doesn´t work! . Congratulation!

  15. Barak says:

    Hi Robert,
    I`ve read this important post a couple of times over the last months. It has helped me a great deal.
    I`d like to ask you two questions, please:
    1.
    Whether your assertion about the emptying of the buffer on pause still holds in CS4 and CS5 ? The reason I`m asking this is that my t seconds bufferTime still reads t seconds after pause – furthermore it reads larger and larger as long as one waits before one resumes (which fits very well your description of it`s tendency to fill up more than it`s designated minimal size)
    2.
    What is the maximal capacity of the buffer ? What is the maximal bufferLength, before it fills to the `bream` ?
    Is ther a minimal size ? Can this size be controlled ? expanded?
    Many thanks for this text.

Leave a Reply