Sanders Publisher: O'Reilly Pub Date: March 31, 2008 Print ISBN-13: 978-0-596-51590-4 Pages: 273 Table of Contents Overview If you're interested in recording and streaming media using Fl
Trang 1by William B Sanders
Publisher: O'Reilly Pub Date: March 31, 2008 Print ISBN-13: 978-0-596-51590-4 Pages: 273
Table of Contents
Overview
If you're interested in recording and streaming media using
Flash Media Server 3 (FMS3) and Adobe's Real-Time MessagingProtocol, this unique 267-page PDF-only book is the perfect
primer It is not a reference, but a systematic guide to
developing FMS3 applications using ActionScript 3.0, with
chapters that focus on specific aspects of the server and howthey work FMS3 is very different from regular web servers
Because its open-socket server technology stays connected untilusers quit the application, you can stream audio, video, text,and other media in real time FMS3 is also quite different fromprevious versions, a fact that web developers familiar with FlashMedia Server 2 or Flash Communication Server 1.5 will quickly
discover Don't worry With Learning Flash Media Server 3 and a
little experience with Flash CS3 and ActionScript 3.0, anyonecan get up to speed in no time You'll learn how to install FMS3,organize your development environment with Apache web
server, and use the management console before diving into thewhys and hows of:
Recording and playing back streaming audio and video inVP6 and H.264 formats
Using the new Flash Media Encoder to stream and recordvideo
Camera and microphone settings
Trang 2feature server, the streaming-only server, and the limited-userdevelopment server It's a whole new world of media, and thisbook puts you right at the doorstep Ready to enter?
Trang 3Copyright
Chapter 1 Getting Started with Flash Media Server 3
Section 1.1 The New Flavors for Flash Media Server 3Section 1.2 What Is a Media Server?
Section 1.3 Installing FMS3
Section 1.4 Organizing Your Development EnvironmentSection 1.5 Testing FMS3 Connections
Section 1.6 Using the FMS3 Administration Console
Section 1.7 Using This Book
Chapter 2 Recording and Playing Back Streaming Audio andVideo
Section 2.1 Streaming and Broadcasting
Section 2.2 Minimalist Project
Section 2.3 Combined Record and Playback ApplicationChapter 3 Setting Your Camera and Microphone
Section 3.1 Camera and Microphone Methods for SettingParameters
Chapter 4 Nonpersistent Client-Side Remote Shared Objects
Trang 4Section 6.3 The Minimum Studio
Section 6.4 Introduction to the Server Side
Section 6.5 Dynamic Camera, Microphone, and BandwidthControls
Section 6.6 Bandwidth Checker
Section 6.7 Conclusion
Chapter 7 Working With Server-Side Files: The File ClassSection 7.1 Recording Data
Trang 5Section 10.1 Cue Points, Metadata, and Stream CompletionSection 10.2 Server-Side LoadVars Class
Trang 6Copyright © 2008, O'Reilly Media All rights reserved
Published by O'Reilly Media, Inc., 1005 Gravenstein HighwayNorth, Sebastopol, CA 95472
O'Reilly books may be purchased for educational, business, orsales promotional use Online editions are also available for
most titles (http://safari.oreilly.com) For more information,contact our corporate/institutional sales department: (800)
998-9938 or corporate@oreilly.com
Editor: Steve Weiss
Editor: Audrey Doyle
The O'Reilly logo is a registered trademark of O'Reilly Media,Inc Learning Flash Media Server, the image of a pearl-spottedbarbet, and related trade dress are trademarks of O'Reilly
Media, Inc
Many of the designations uses by manufacturers and sellers todistinguish their products are claimed as trademarks Wherethose designations appear in this book, and O'Reilly Media, Inc.was aware of a trademark claim, the designations have beenprinted in caps or initial caps
While every precaution has been taken in the preparation of thisbook, the publisher and authors assume no responsibility forerrors or omissions, or for damages resulting from the use ofthe information contained herein
Trang 7number of concurrent connections or bandwidth usage
The second version of FMS3 is the Flash Media Streaming
Server 3 (FMSS3) This server is limited to streaming video andaudio Think of it as similar to progressive downloading, exceptFMSS3 uses server streaming If your main goal is to streamaudio and video from a site, this option provides a much lowercost server than FMIS3, with no limits on concurrent users orbandwidth Using the Flash Media Encoder 2, you can use
FMSS3 for live video streaming of either H.264 or On2 VP6
video
The third version of FMS3 is the free Flash Media Development
Trang 8To avoid confusion, this book refers to all three versions simply
as FMS3 You will need to keep in mind that the FMSS3 servercannot be used for recording video or interactive chats, and hasother limitations However, using the Development version ofthe server, you can create and test all of the examples in thisbook You can even record your own FLV video files and playthem on either of the other two servers
Trang 9number of concurrent connections or bandwidth usage
The second version of FMS3 is the Flash Media Streaming
Server 3 (FMSS3) This server is limited to streaming video andaudio Think of it as similar to progressive downloading, exceptFMSS3 uses server streaming If your main goal is to streamaudio and video from a site, this option provides a much lowercost server than FMIS3, with no limits on concurrent users orbandwidth Using the Flash Media Encoder 2, you can use
FMSS3 for live video streaming of either H.264 or On2 VP6
video
The third version of FMS3 is the free Flash Media Development
Trang 10To avoid confusion, this book refers to all three versions simply
as FMS3 You will need to keep in mind that the FMSS3 servercannot be used for recording video or interactive chats, and hasother limitations However, using the Development version ofthe server, you can create and test all of the examples in thisbook You can even record your own FLV video files and playthem on either of the other two servers
Trang 11If you've used Flash Communication Server or Flash Media
Server 2 and understand its use, you can skip this section
Flash Media Server 3 is the latest version of Flash Media Serverand has several improvements However, if you're new to
working with open socket technology, including FMS3, read on.FMS3 is an open socket server The key difference between
open socket servers and Web servers is that as soon as youreceive information from a Web server, the connection is
broken It may look as if you're still connected to the Web
server, especially with a Flash page that's animating materials.However, that's not the way it works If you open a Web page,the Web server sends you the page along with all associatedgraphics, text, and other media; and your computer sends amessage back that says, "Got it!" (or something to that effect),and the connection closes With an open socket server, the
connection stays open until you quit the application or trigger
an event that cuts the connection Because the connection
remains open, you can stream audio, video, text, and any othermedia available on the Internet, in real time You just can't dothat with a regular Web server because it has an entirely
different architecture
1.2.1 Enter RTMP
When you use a regular Web page, you're most likely usingHTTP (Hypertext Transfer Protocol), which allows you to look atWeb pages To be able to work with streaming media, Adobedeveloped Real-Time Messaging Protocol (RTMP) Generally,when you use FMS3, you first connect to a Web server via HTTPand then to Flash Media Server using RTMP Because of thisarrangement, you're working simultaneously with different
protocols—one for the Web site and the other for the streamingmedia
1.2.2 Special Languages and Documentation
Trang 12APIs are extensions of ActionScript and are called Client-Side
Media ActionScript and Server-Side Media ActionScript, or in
this book, simply as CS ActionScript and SS ActionScript With
ActionScript 3.0, which is also used in this book, the client-sideActionScript has been fully integrated into the language As aresult, the documentation no longer comes with a special client-side ActionScript documentation file Instead, the entire
ActionScript 3.0 language reference is included The Server-Side ActionScript Language Reference is still included, however.
The full content of these APIs is richly described in PDF files
that come with FMS3 (When you install FMS3, these files areinstalled on your computer in Program Files Adobe
Flash Media Server 3 documentation.) Once you've
installed FMS3, you should immediately print this
documentation and put it in loose-leaf binders, or at least putthe files in a folder on your desktop so that they're easy to look
up You'll be using the documentation all the time The FMS3documentation is general and complete; in contrast, this book isnot a reference Rather this book is designed to get beginnersstarted, with a series of specialized chapters that show how touse different features of FMS3
Trang 13You can install FMS3 on either a Windows or Linux OS For aWindows server, you need at least Windows 2000, which meansyou can install it on your Windows Home, XP, or Vista edition Ifyou're installing it on your company's server, you'll probably beusing Windows 2003 Standard Edition, Windows 2005 Server, orthe new Windows 2008 Server For your Linux box, either RedHat Enterprise version 3 or 4 will do the trick You'll need a 3.2GHz or faster Pentium 4 (or some other X86-compatible
processor), 1 GB of RAM, and 50 MB of hard drive space Forthis book, I used a 3 GHz Pentium 4 with 1 GB of RAM running
on Windows XP, Windows Vista on another server, and Red Hatversion 4 on a Linux server
install FMS3 on a Linux server, but you cannot develop
applications on a Linux machine (Adobe doesn't make aLinux version of Flash.) This apparent paradox
recognizes that the two main server technologies are
Windows and Linux, and the two main development
platforms are Windows and Macintosh
As you will soon see, this doesn't mean that you cannot developand test FMS3 apps on your Macintosh Rather, it means thatyou'll just have to install FMS3 on a remote server (which wouldinclude your Windows computer sitting on the same desktop oreven in your lap) The big advantage of running your application
on one computer and FMS3 on another is that your processor
Trang 141.3.1 Installation Steps for Windows
Installing FMS3 is very easy on either Windows OS or Linux.First, you'll look at an example of how to install FMS3 on a
Windows XP computer using the Development Version freelyavailable online at www.adobe.com/products/flashmediaserver.Figure 1-1 shows the download page Be sure to select the
correct version for your server (or computer)
Figure 1-1 Selecting FMS3 version
Trang 15FlashMediaServer3.exe, as shown in Figure 1-2
Figure 1-2 Setup file in FMS3 folder
Trang 163 Setup Wizard appears, as shown in Figure 1-3
Figure 1-3 FMS3 Setup Wizard
Trang 17Figure 1-4 Serial Number entry box
Trang 18a serial number, the server will be configured as the FMDS3
(Development) server Press the Next button Figure 1-5 showsthe page that appears requesting a user name and password forthe module you will need to access for the Flash Media
Administration Services:
Figure 1-5 Administrator user name and password entry
Trang 19Figure 1-6 Setting ports
Trang 20default ports shown in Figure 1-3 By default, FMS3 is
configured to listen to Port 1935; unless you have a very goodreason, leave it at that (One good reason to change your
default port is if your system already has an application thatlistens to Port 1935.) However, you can include additional ports
to listen to For example, you can list:
1935,80,443
This means you can have a sequence of ports to listen to if
you're using more than the RTMPE (or RTMP) protocols, such asRTMPT (tunnel via HTTP) or RTMPS (tunnel via HTTPS)
Likewise, the administrative service (FMS3 console) listens toport 1111 That's a different service, and you don't want it tohave the same port as FMS3
Trang 21Server 3 TechNotes at the Adobe Web site for more information
To keep it simple, for now just use the defaults and click Next.The last page that appears before installation has all of the
details of the key directories and the port values Write thesedown and keep them where you can easily find them Figure 1-
in Figure 1-8 appears You can change the defaults here if youdon't want to see the ReadMe.pdf notice, start the server, or
Trang 22documentation, the Flash Media Administration Console,
starting and stopping both the media and administration serversand even an uninstaller Figure 1-9 shows what appears if yourinstallation succeeded:
Figure 1-9 Flash Media Server 3 selections
Trang 23Installing FMS3 on Linux is pretty easy as well Basically, all youneed to do is to extract the files from the compressed archive(FlashMediaServer3.tar.gz) and then untar the installation file Iextracted the file using the graphic user interface
Unpacking the install package creates a directory and places thepackage within it Even though you can see the icons that make
up the package, you have to go into the Terminal to install thesoftware From the Terminal, navigate to the new directory, inwhich you will see a file named installFMS
Trang 251.4.2 Client-Side Files
Your client-side files are the Flash FLA, AS, and SWF files thatyou're used to working with in Flash If you have your server onyour computer, they can go anywhere When you use the
Control Test Movie sequence with your application, your
Trang 26if you want to test your applications using a LAN or just usingyour browser, you need to place them where they will be
recognized by your Web server using the HTTP protocol on yourlocalhost (127.0.0.1) or local LAN IP address That is, all yourHTML and SWF files need to be placed in the Web server's rootdirectory Because it's easier to develop and test applications,you also can place the FLA and AS files in the same folders asthe SWF and HTML files Figure 1-11 shows a typical setup on aWindows XP box using an Apache Web server
Figure 1-11 Client-side files set up for remote access
Trang 27C:\Inetpub\wwwroot\FMS3apps
The same concept is at work, but you're just using a differentroot
NOTE
If you're working with two different "roots"—one for theserver side and one for the client side—make a shortcut
to each It will save you a lot of time and make
development go much smoother
You can develop applications on your Windows PC perfectly wellwithout using a Web server at all If you're most comfortabledeveloping and testing your applications on a single computer,you can develop your client-side applications wherever you
want Your desktop is as good a location as any
1.4.3 Setting Up with a Macintosh
My particular setup uses a Macintosh for development, whichconnects through a LAN to FMS3 on my Windows XP and Vistacomputers The good news is that Macintosh computers comewith Apache servers already installed, so all you have to do isfigure out where to place your client-side applications to hook
up to FMS3 on the server The next section shows how a LANsetup works with the Macintosh using the default path:
Home\sites\FMS3apps
Trang 28
computer's name, in my case, billsanders Figure 1-12 showsthe path to FMS3 applications on a Macintosh To make a
connection, you need to use the connection through your Webserver You will connect to FMS3 on a remote server becauseFMS3 doesn't run on Mac OS The next section shows how toconnect to local and remote servers, including how to set up aLAN link using a Macintosh as the primary development
computer
Figure 1-12 Path to client-side applications on Macintosh
Trang 29Time Messaging Protocol The newest version of RTMP is RTMPE
The protocol to connect to Flash Media Server 3 is RTMP—Real-(E for Encryption) The examples show both The older RTMP is
a bit faster, but RTMPE performs better for encryption and ismore secure You set up a net connection, and then place theRTMP path in your NetConnection instance The basic routine is
as follows:
nc = new NetConnection( );
nc.connect("rtmp:/appName");
If your path is to a server on the same computer (or physicalserver) as your application, you use only a single "/" slash;
however, if your path is to a remote server or LAN, you use adouble slash "//" as follows:
change RTMP paths, I just comment out the old string and
remove comment slashes from what I want to use The
examples in this book use the single slash, but the LAN version
is commented out Also, if you use a LAN, make sure that yourRTMP uses the IP address for the FMS3 server location, and notthe IP address where your client-side materials are being
Trang 30Once you've got your folder arrangements set up, you need tomake a connection to test whether your FMS3 is performing inconjunction with your applications To do this, you need a
simple FMS3 application to test If you don't have Flash
installed, download the latest version from the Adobe.com Website and install it You will need the Flash IDE to follow the
examples in this book The following steps show you how tocreate a simple FMS3 application to test:
Trang 32Remember, all server-side files must be placed on the samecomputer as the FMS3 server, even if you're using a LAN (You'dnever put server-side folders on a Macintosh!)
The folder name is the application name This concept may take
a bit of getting used to, but eventually it'll be as easy as pie.Figure 1-13 illustrates what your application should show Thefirst time you may have to wait a few seconds, especially if
you're using a LAN
You will soon learn that the prettiest message in the world is
"NetConnection.Connect.Success." That means your applicationhas successfully connected to Flash Media Server 3
Figure 1-13 Message indicating successful connection to
server
Trang 33This is the name and password you wrote down when you
installed your server (See Figure 1-5.) Type your user nameand password, and if your server is in a secure location, you canclick the check boxes to remember the information and
automatically open the console when you start it For
development, I use the Administration console all the time, so Ihave the user name and password automatically entered
However, if your development environment is not secure, youshould require that the user enter the user name and password.Launch the application FMS3Connect, developed in the previoussection In the Administration console, select the
ViewApplications Clients option When the application
successfully launches, you will see the name of the application,the number of clients currently using the application, plus otherinformation about the client as shown in Figure 1-14
Figure 1-14 Flash Media Administration Console
Trang 34Administration console more useful In subsequent chapters,you'll see how it can provide invaluable feedback on what yourserver is doing
Trang 35Learning with Flash Media Server 3 is designed to offer you
options, but at the heart of every chapter is a core set of codethat shows the minimum requirements needed for different
procedures Later chapters will have some material that is morecomplex, more sophisticated, and optimized for getting the
most out of FMS3 The chapters will include the minimalist set
of code for different operations This code is presented as a
procedure with the sole purpose of clarifying what must be inplace for something to happen It won't be sophisticated, but itwill work and help you understand how to accomplish differentgoals with FMS3 In the more advanced chapters, the same
operations may be rendered as a class or even a component.Sometimes in the same chapter you will see a range of
solutions for the same task, ranging from a simple procedure to
an object-oriented class to a design pattern
1.7.1 Flash and ActionScript 3.0
Unlike previous versions of Flash Media Server, FMS3 arriveswith the introduction of a major ActionScript revision
ActionScript 3.0 is the first true ECMAScript 4 implementation ofActionScript In the future, expect more languages to adopt oneECMA standard or another As Internet languages adopt thesestandards, different languages will overlap more and requireless work to learn a new one Because ActionScript 3.0
represents a very different way of programming in ActionScript
—more like Java, C++ and C#—you may need to take sometime to go over it All of the client-side code is in ActionScript3.0 and the server-side script is in ActionScript 1.0; so the
server-side script may actually be more familiar to you In fact,
if you don't know Flash and ActionScript, before you go too farwith FMS3, you'd better spend some time getting up to speed
on both All of the examples in this book use Flash CS3 and
ActionScript 3.0, which means that most of the code won't workwith earlier versions of Flash However, if you are using Flex 2
Trang 36the following folders and PDF files:
ActionScript 3.0 Language and Components Reference
documents ActionScript™ 3.0 (client-side AS Reference for
Trang 37Adobe Flash Media Interactive Server Plug-in API Reference, flashmediaserver_plug_in_API (folder).
Adobe Flash Media Server Configuration and Administration Guide, flashmediaserver_config_admin.pdf.
at hand is always important This book was designed with theseavailable documents in mind For the basic how-to, this bookshould serve you well, but you should also look at the FMS3 file
Trang 38on developing applications The more angles you look at FMS3from, the better you'll understand it.
Trang 39Chapter 2 Recording and Playing Back Streaming Audio and Video
signal gets the same stream It's like dropping a pebble in thewater—a concentric circle of waves sends out a "signal." Nomatter where viewers or listeners are, when the wave reachesthem, they get the same wave—like a TV picture or radio
transmission—as everyone else
When FMS3 sends out a stream of audio and video, it creates aseparate stream for each recipient Its "broadcasting" worksmore like the spokes on a wagon wheel, where everyone
connected gets his or her own stream So here the term
"broadcasting," really means a form of streamcasting—a
technology where everyone gets a separate stream If one
person is listening, my application sends only a single stream;but if 20 people are listening, it generates 20 streams Becausethe server automatically creates a separate stream for eachuser connected, you don't have to create all those streams inyour coding However, in deciding how to set up your
application, for bandwidth considerations you need to considerthe number of streams it may generate
If you've ever viewed online video, you've probably seen
different kinds without really realizing it If you click on a videoand have to wait a long time, what you're really waiting for is
Trang 40to play the video that's saved to your hard drive That worksfine except you have to wait until the file is fully downloadedbefore you can watch it Also, it's saved on your hard drive
taking up space A second type of video processing is called
progressive download It's like a hybrid of a video download and
streaming As the video file is downloaded, it begins to play
rather than wait for the whole thing to download However, thefile will end up on the user's hard drive, and processing is not
as smooth as true streaming The third type of video
processing, if you have FMS3 on your server, lets you streamvideo files from the server This chapter explains how to getstarted with both creating and streaming Flash Video (FLV) filesusing FMS3