RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Video on a Budget: Two Ways to Deliver Flash : Page 2

Weigh the pros and cons of streaming vs. progressive downloads for FLV content, and learn how to deliver video on demand without breaking the bank.

Client-Side Connection
As you can see, both progressive download and streaming video have their purposes. How does this choice affect your client-side player? Fortunately, scripting your own playback is identical in both methods, save for one line of code. Therefore it makes sense to learn both methods at once. In a future article I'll discuss how to script a more complete player.

The first step is to create an instance of a video object in your Flash file. This is analogous to turning your TV on. In the upper-right corner of the Library panel in the Flash IDE, use the Library menu to create a new video (see Figure 2). Name it myVideo and select ActionScript-controlled as the only enabled option in the Video Properties dialog (see Figure 3). Drag the video object to the stage and give it an instance name of myVideo. Now you're ready for the ActionScript.

Figure 2. To script your own video playback, you must first create a video object.
Figure 3. Rather than embed or attach a pre-existing video, allow dynamic asset assignment by making your video object ActionScript-controlled.

The second step is to open a pipeline to the video source by establishing a NetConnection. This is a bit like subscribing to a video-on-demand service. Later you'll pick which video to watch but, for streaming servers, you need to connect first. Begin by creating the connection object:

var myConnection:NetConnection = new NetConnection();
Using the object you created, connect to the video source with the connect method. This is the most important line of this sample code, when choosing between streaming and progressive download assets. Using Adobe's Flash Media Server 2 as the server example, you will connect to an RTMP (Real Time Messaging Protocol) stream:

However, when scripting for progressive download assets, you must tell Flash that you will not be seeking to connect to a server. In this case, use null as the connect parameter instead:

The remainder of the connection script is identical for both streaming and progressive download assets. Continue by creating a NetStream object to act in cooperation with your connection. This allows you to pick the video you want to watch.

var myStream:NetStream = new NetStream(myConnection);
Finally, attach the NetStream object to your on-stage video window and play the video of your choice.

While you may want to script additional controller elements, such as a play/pause button, a scrubber, and a volume button, you can now automatically play a video from either local or server addresses. As far as progressive download sources go, you're essentially finished. When using streaming assets, you will probably want to embellish the server negotiation a bit, which I'll discuss in a moment. First, however, what are your server options?

Flash Media Server 2
As part of the Flash platform, Adobe's feature-rich Flash Media Server 2 (formerly Flash Communication Server or "FlashCom") supports many Flash features, including several not directly related to video. Due to space constraints, I'll briefly introduce you to FMS here; if there is enough demand, I'll write a more in-depth look at the application in a future article.

FMS is a dedicated streaming software package that can run on Windows and Linux operating systems. Flash clients written to work with the server establish a persistent connection to FMS for reduced latency and high-performance video streaming. Like most servers, it can accommodate multiple processes, and offers all the benefits of streaming I discussed previously. In addition, it can stream live video sources, and record video input, from a web cam, for example.

FMS also provides a rather robust server-side scripting environment (ActionScript) that allows you to develop server applications boasting a variety of features. It can provide bandwidth detection to direct users to a pre-existing source that is optimized for the detected connection speed, playlist management, ad-insertion, and multiple camera angles or synchronized tracks. It even supports multi-directional, multi-user streaming for video chat, video messaging, and video conferencing applications.

Finally, in addition to the enhanced security of streaming files (such as access control and no client caching of assets) Flash Media Server can deliver assets over SSL and its unique transfer protocol (Adobe's RTMP, discussed previously) also reduces stream hijacking.

To communicate intelligently with FMS, you can add a variety of supported ActionScript. FMS even comes with its own set of components for rapid application development. A simple example of enhancing your client to work with FMS expands on the previous sample code. By checking the status of your existing connection, you can decode to play your video, or respond with messages indicating a busy server or closed connection.

myConnection.onStatus = function(srvrData) {
    if (srvrData.code == "NetConnection.Connect.Success") {
    	    // successful connection
        var myStream:NetStream = new NetStream(myConnection);
    } else if (srvrData.code == "NetConnection.Connect.Closed") {
        // connection closed by user or server
        trace("Connection closed.");
    } else if (srvrData.code == "NetConnection.Connect.Rejected") {
        // connection rejected and killed by server
        trace("Server busy. Try again later.");
        delete myConnection.onStatus;
All of this robust support comes at a hefty price tag. The server costs $4,500 and the lack of support for anything but Windows and Linux may cause you to look elsewhere. Fortunately, you can let others do the serving for you. Authorized vendors offer the Flash Video Streaming Service (FVSS) which transfers the burden of server purchase and maintenance to paid FMS professionals. Depending on your needs, long-term service may approach and even surpass the expense of owning your own server, but it's worth investigating for those who are just getting started (see the ' Resources' sidebar).

What if you don't need all the rich features that FMS offers? What if you only want to stream FLVs as economically as possible? Is free economical enough? Take a look at the two following basic FLV streaming alternatives

A very exciting, free, open source alternative to FMS is LightTPD. "Lighty," as it is affectionately known, takes its name from "Light Footprint + HTTP Daemon," meaning a tiny HTTP server. As its name implies, Lighty can serve much more than FLV files. It is an impressive web server that features load management, CGI and FastCGI support, PHP, Ruby, Python, Perl, URL-rewriting, and more, and is available for Windows, Linux, Mac OS X, and other platforms.

As of version, however, Lighty features two optional server modules that are ideal for streaming FLVs. The first, mod_flv_streaming, is specifically dedicated to FLV streaming. This added module enables Lighty to deliver small packets of FLV information to the client with the same "instant on," interactive punch provided by FMS2. With this alone, you can provide a rewarding playback experience with fast responsive video that can be immediately scrubbed.

If you then add the mod_secdownload module, you can control the security of your streams to prevent deep linking from unauthorized users. This module allows you to encrypt temporary access to a URL that times out at a predetermined time.

Due to space constraints, I'll leave it to you to use the related resources links to install and use Lighty. Be sure to configure the FLV streaming module and optional mod_secdownload module.

To take advantage of streaming FLVs with Lighty, you'll need a compatible player configured to work with the server. Think of this as a component created for this purpose, but one which can be dynamically controlled through the URL in the player's host page. You'll need to encode your FLV files with metadata, which I'll discuss at the end of this article.

A nice simple player was created by Stefan Richter (with props to Lee Brimelow) for his PHP FLV streaming solution (discussed in the next section). Lighty's creator, Jan Kneschke, with the help of Fabian Topfstedt, adapted this player for Lighty.

PHP and ColdFusion Alternatives
If you don't want to run your own server, even the tiny Lighty server, you can also accomplish this modified type of streaming using server-side scripting middleware. Briefly, a server script sends portions of an FLV when requested, rather than buffering the lead of an FLV and downloading the entire file.

Stefan Richter, a.k.a. FlashComGuru, got me interested in this last winter with his PHP solution. It's very easy to use and will work on any server with PHP support. It also comes complete with a sample player along with the PHP code. Richter cites Brian Bailey's blogging on the subject and the chattyfig FlashComm list for getting the ball rolling, which is another testament to the great energy out there in the Flash community.

Subsequently, Steve Savage developed a ColdFusion solution based on Richter's effort and blogging from Christian Cantrell—more community efforts bearing fruit.

While not as robust as the streaming features provided by Flash Media Server 2, these implementations allow you to fetch portions of an FLV when requested, dramatically improving the responsiveness of the videos and vastly decreasing the load placed on the server. Best of all, they are free. (If you use any of these solutions, consider a donation or a note of thanks to the developers!)

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date