FlashMediaServer开发指南-第一章

来源:互联网 发布:长沙源码信息要交钱吗 编辑:程序博客网 时间:2024/04/29 09:21
Chapter 1: Getting started

一、关于服务器的版本

Adobe Flash Media Server offers a combination of streaming media and interactivity 
for building rich media applications. Flash Media Server offers instant start, 
live video streams, and variable streaming rates based on the user’s bandwidth.


There are three editions of Flash Media Server:
  . Flash Media Interactive Server: 全功能版本.
  . Flash Media Development Server: Flash Media Interactive Server的开发版 
                                    支持Flash Media Interactive Server所有功能,但有连接数的限制。
  . Flash Media Streaming Server: 仅支持直播和点播流服务。
                                  这个版本的服务器不支持服务端的脚本或流记录


For more information about which features each edition supports, 
see the Technical Overview at www.adobe.com/go/learn_fms_techov_en.


二、Flash Media Server概览

2.1 客户-服务端架构

可以将Flash Media Server 看作是一个转发器(hub). 
Applications connect to the hub using Real-Time Messaging Protocol. 
The server can send data to and receive data from many connected users.
A user can capture live video or audio using a camera and microphone 
attached to a computer running Adobe Flash Player or Adobe AIR and 
publish it to a server that streams it to thousands of users worldwide. 
Users worldwide can participate in an online game, with all moves synchronized for all users.


Users connect to the server through a network connection.
A connection is similar to a large pipe and can carry many streams of data.
Each stream travels in one direction and transports content between one client and the server.
每台服务器都能处理大并发量的连接,连接个数只受服务器能力的限制。


运行在FMS上的应用都是C-S架构,
客户端的应用程序使用Adobe Flash或Adobe Flex开发,
运行在Flash Player,Adobe AIR, 或Flash? Lite? 3上。它能捕捉并显示音频和视频,并处理用户交互。
服务端的应用程序运行在服务器端,它管理客户端的连接、授权,写服务器的文件系统,执行其它任务。


客户端必须初始化到服务端的连接。
一旦连接建立了,客户端就可以和服务端,和其它客户端进行通信。
更强大的是,客户端可以连接到运行在服务器上的应用程序的实例,
例如,这样的应用程序实例可以是在线游戏的对不同用户组创建的不同房间,
在这种情况下,每个房间是一个实例。


一个应用程序的多个实例可以同时运行,
每个应用实例都有它们自己的唯一号并提供唯一的资源给客户端。
多个客户端可以连接到同一个应用实例或不同的实例。


图1 
多个客户端连接到运行在FMS上的多个应用(sudoku and scrabble) ,
及单个应用下的多个实例(room 2, room 1, and room 2) 。


2.2 媒体应用程序的组成部分

客户端应用程序使用ActionScript开发,并编译成SWF文件。
服务端应用程序使用服务端的ActionScript开发 ,
它和ActionScript 1.0很像,但它运行在服务端,而不是客户端。
一个媒体应用程序通常用来记录或直播音频或视频,
它们可以是erver to client, client to server, or server to server的流。


A typical Flash Media Server application has these parts:
一个典型的Flash Media Server应用程序通常包含下面这些部分:
1. 客户端的用户接口
客户端会显示给用户接口,如一个视频的启动、停止或暂停。
用户接口可以使用Adobe Flash or Adobe Flex来开发,运行在Flash Player, Adobe AIR, or Flash Lite 3上。


2. 客户端的ActionScript程序
客户端的ActionScript程序用来处理用户交互,并连接到服务器,如
Flash Media Server 3 and later support ActionScript 3.0, ActionScript 2.0, and ActionScript 1.0.


3. 视频或音频文件
多数媒体应用程序的流录制功能会录制从服务端到客户端的音频或视频。
Flash Media Server supports playback of a variety of stream formats, 
including Flash Video (FLV), MPEG-3 (MP3), and MPEG-4 (MP4 and F4V).


4. 摄像头或麦克风
可以使用Adobe Flash Media Live Encoder来将视频或音频的流推送到服务器,
You can also create your own client that captures live audio and video. 
In both cases, you need a camera and a microphone to capture the video and audio.


5. 服务端的ActionScript程序
大多数应用程序会要用到服务端ActionScript代码,它会保存成后缀名为.asc的文件,
意思是ActionScript Communication File。
这个文件要么命名成main.asc,要么是myApplication.asc。
服务端的代码处理服务端的工作,如音视频流、定义当用户建立连接或断开连接时的动作等。


三、 搭建开发环境

3.1 服务器安装

可以使用服务器的免费开发版来开发和测试应用。
最简单的开发环境是将Flash或Flex也安装在用作服务器的同一台机器上。


1. 服务器安装
Run the installer to install an edition of Flash Media Server.


2. Windows下启动服务器
When you install the server, you can set it to start automatically when you boot your computer. 
If the server is not already started, you can start it manually.
1) From the Start menu, select All Programs> Adobe> Flash Media Server 3.5
    > Start Adobe Flash Media Server 3.5.
2) From the Start menu, select All Programs> Adobe> Flash Media Server 3.5
    > Start Flash Media Administration Server 3.
Note: You need the Administration Server if you want to open the Administration Console
 (for example, to view server trace() messages or connection counts).


3. 验证服务器是否在运行
1) Open Control Panel> Administrative Tools> Services. 
In the Services window, make sure that both Flash Media Administration Server 
and Flash Media Server are started.
2) To troubleshoot start-up issues, check the logs in the RootInstall/logs folder. 
The master.xx.log file and the core.xx.log file show failures. 
The edge.xx.log file shows on which ports the is listening.


3.2 安装 Flash

Download and install Flash at www.adobe.com/go/flash.


3.3 安装 Flex

Download and install the Adobe Flex SDK or Adobe Flex? Builder? at www.adobe.com/go/flex.


四、 创建一个 Hello World 应用程序

1. 概览
Note: 下面的章节只能用在Flash Media Interactive Server 和 Flash Media Development Server版本,
不能对Flash Media Streaming Server开发服务端的代码。


本例演示了一个从客户端到服务端并返回的简单通信。
当用户点击按钮时,客户端连接到服务器,客户端调用一个服务端的函数,它会返回一个字符串。
当服务端回应时,客户端会显示这个字符串。
这个示例文件在:RootInstall\documentation\samples\HelloWorld 目录下。


2. 创建用户接口
1) Start Flash and select Create New> Flash File (ActionScript 3.0).
2) In the Document Class field, enter HelloWorld. 
    If you see an ActionScript Class Warning message about a missing definition—click OK. 
    You will add the class file in the next section.
3) Choose Windows> Components. 
    Click User Interface and double-click Button to add it to the Stage. 
    On the Properties tab, enter the instance name connectBtn.
4) Add a Label component above the button, and give it the instance name textLbl.
5) Save the file as HelloWorld.fla.
You can save the client files to any location.


3. 写客户端的脚本
This script provides two button actions, either connecting to or disconnecting from the server.
When connecting, the script calls the server with a string (“World”), 
which triggers a response that displays the returned string (“Hello, World!”).
1)Choose File> New> ActionScript File. 
   Check that the Target box has HelloWorld.fla.
2)Declare the package and import the required Flash classes: 
package {
  import flash.display.MovieClip;
  import flash.net.Responder;
  import flash.net.NetConnection;
  import flash.events.MouseEvent;
  public class HelloWorld extends MovieClip {
  }
}
3)Inside the HelloWorld class declaration, 
   declare variables for the connection and the server responder: 
private var nc:NetConnection;
private var myResponder:Responder = new Responder(onReply);


4)Define the class constructor. 
   Set the label and button display values, 
   and add an event listener to the button: 
public function HelloWorld() {
  textLbl.text = "";
  connectBtn.label = "Connect";
  connectBtn.addEventListener(MouseEvent.CLICK, connectHandler);
}


5)Define the event listener actions, which depend on the button’s current label:
public function connectHandler(event:MouseEvent):void {
  if (connectBtn.label == "Connect") {
    trace("Connecting...");
    nc = new NetConnection();
    // Connect to the server.
    nc.connect("rtmp://localhost/HelloWorld");
    // Call the server's client function serverHelloMsg, in HelloWorld.asc.
    nc.call("serverHelloMsg", myResponder, "World");
    connectBtn.label = "Disconnect";
  } else {
    trace("Disconnecting...");
    // Close the connection.
    nc.close();
    connectBtn.label = "Connect";
    textLbl.text = "";
  }
}


6)Define the responder function, which sets the label’s display value: 
private function onReply(result:Object):void {
    trace("onReply received value: " + result);
    textLbl.text = String(result);
}


7)Save the file as HelloWorld.as to the same folder as the HelloWorld.fla file.


4. 写服务端的脚本
1)Choose File> New> ActionScript Communications File.
2)Define the server-side function and the connection logic: 
application.onConnect = function( client ) {
    client.serverHelloMsg = function( helloStr ) {
    return "Hello, " + helloStr + "!";
    }
    application.acceptConnection( client );
}


3)Save the file as HelloWorld.asc in the RootInstall/applications/HelloWorld folder. 
(Create the “HelloWorld” folder when you save the file.)


5. 编译并运行应用程序
1)Verify that the server is running.
2)Select the HelloWorld.fla file tab.
3)Choose Control> Test Movie.
4)Click the Connect button.
   “Hello, World!” is displayed, and the button label changes to Disconnect.
5)Click the Disconnect button.
The output of the trace() statements is displayed in the Flash Output window.




五、 创建一个应用程序

1. 开发客户端代码
A client has code written in ActionScript that connects to the server, 
handles events, and does other work. With Flash, you can use ActionScript 3.0, 2.0, or 1.0, 
but ActionScript 3.0 offers many new features. With Flex, you must use ActionScript 3.0.
For information about learning ActionScript and working with video, 
see the following resources:
  . “Working with Video” in Programming ActionScript 3.0 
     at www.adobe.com/go/learn_fms_video_en.
  . “ActionScript 2.0 Migration” appendix in the ActionScript 3.0 Language 
     and Components Reference.
  . ActionScript 3.0 Language and Components Reference at www.adobe.com/go/learn_fms_as3lr_en.
  . Flash Help Resource Center at www.adobe.com/go/learn_fms_flashhrc_en
  . Flex Help Resource Center at www.adobe.com/go/learn_fms_flexhrc_en
See also
“Copy client-side files to a web server” on page?11


2. 开发服务端代码
In general, applications require server-side code written in Server-Side ActionScript 
if they need to do any of the following:
  . Authenticate clients
    By user name and password, or by credentials stored in an application server or database.
  . Implement connection logic
    By taking some action when a client connects or disconnects.
  . Update clients
    By calling remote methods on clients or updating shared objects 
    that affect all connected clients.
  . Handle streams
    By allowing you to play, record, and manage streams sent to and from the server.
  . Connect to other servers
    By calling a web service or creating a network socket to an application server or database.


Place the server-side code in a file named main.asc or yourApplicationName.asc, 
where yourApplicationName is a folder in the RootInstall/applications folder. 
For example, to create an application called skatingClips, 
create the folder RootInstall/applications/skatingClips. 
The server-side code would be in a file called main.asc or skatingClips.asc 
in the skatingClips folder.
The server-side code goes at the top level of the application directory, 
or in its scripts subdirectory. For example, you can use either of these locations:
RootInstall/applications/appName
RootInstall/applications/appName/scripts


By default, the applications folder is in the root installation folder 
(C:\Program Files\Adobe\Flash Media Server 3.5\applications, on Windows). 
To configure the location of the applications folder, edit the fms.ini 
or the Vhost.xml configuration file. In the fms.ini file, 
edit the following parameter: 
VHOST.APPSDIR = C:\Program Files\Adobe\Flash Media Server 3.5\applications. 
In the Vhost.xml file, edit the AppsDir element.


See also
“Copy server-side script files to the server” on page?10


3. 客户端和应用程序对象
Server-side scripts have access to two special objects, 
the client object and the application object. 
When a client connects to an application on Flash Media Server, 
the server creates an instance of the server-side Client class to represent the client. 
An application can have thousands of clients connected. 
In your server-side code, you can use the client object to send and receive messages 
to individual clients.


Each application also has a single application object,
which is an instance of the server-side Application class. 
The application object represents the application instance. 
You can use it to accept clients, disconnect them, shut down the application, and so on.


4. 开发双字节的应用程序
If you use Server-Side ActionScript to develop an application that uses double-byte text 
(such as an Asian language character set), place your server-side code in a main.asc file 
that is UTF-8 encoded. Use a JavaScript editor, such as the Script window in Flash 
or Adobe? Dreamweaver?, that encodes files to the UTF-8 standard. 
Use built-in JavaScript methods, such as Date.toLocaleString(), 
to convert the string to the locale encoding for that system.
Some simple text editors might not encode files to the UTF-8 standard. However, 
some editors provide a Save As option to encode files in the UTF-8 standard.


Set UTF-8 encoding in Dreamweaver
1)Check the document encoding setting by selecting Modify> Page Properties, 
   then Document Encoding. Choose Unicode (UTF-8).
2)Change the inline input setting by selecting Edit> Preferences (Windows) 
   or Dreamweaver> Preferences (Mac OS), and then click General. 
   Select Enable Double-Byte Online Input to enable double-byte text.


Use double-byte characters as method names
  . Assign method names using the object array operator, 
    not the dot operator: 
    // This is the CORRECT way to create double-byte method names
    obj["Any_hi_byte_name"] = function(){}
    // This is the INCORRECT way to create double-byte method names.
    obj.Any_hi_byte_name = function() {}


六、 测试应用程序

1. 测试并调试客户端脚本
To help test a client-side script, use trace() statements to monitor each processing point.
The output is shown in the Flash Output window (this example is from the 
“Create a Hello World application” on page?3):
To debug a client-side script, use the Debug menu in Flash to set breakpoints, 
step into functions, and so forth. You can inspect the state of the script 
with Windows> Debug Panels.


2. 测试并调试服务端脚本
To test a server-side script, use trace() statements to monitor each processing point.
View the output of trace() statements in the Live Log in the Administration Console.
To open the Administration Console, 
choose Start> All Programs> Adobe> Flash Media Server 3.5> Flash Media Administration Console.
When a client connects to an application on the server, 
the application is loaded and can be seen in the Administration Console.
To load an application directly from the Administration Console, 
select from the New Instance list of available application names. 
You can also stop an application or reload it—in either case, 
all clients are disconnected.


Note: When you edit and save an .asc file, the changes do not take effect 
until the application is restarted. Use the Administration Console 
to restart the application, then connect to the application again.
For each application instance, you can observe its live log, clients, 
shared objects, if any, streams in use, and performance statistics.
The Administration Console checking the performance of 
an application while it is running


View the output of a server-side script
The output of the trace() statements in a main.asc file is sent to the following log file:
RootInstall/logs/_defaultVHost_/yourApplicationName/yourInstanceName/application.xx.log


Where yourInstanceName is _definst_ by default and xx is the instance number, 
00 for the most recent log file, 01 for the previous instance, and so on. 
You can view a log file with any text editor.
While an application is running, you can view the Live Log in the Administration Console. 
In the Administration Console opens, click View Applications, then Live Log.


3. 在控制台中调试
To playback streams and inspect data about shared objects,
an application must make a special debug connection to the Administration Console.


The availability and number of debugging sessions is set in the AllowDebugDefault 
and MaxPendingDebugConnections elements of the Application.xml configuration file. 
By default, debugging is not allowed. To override the debug setting in the Application.xml file, 
add the following code to an application’s server-side code: application.allowDebug = true;
Note: Set allowDebug to false before deploying the application.


To start a debugging session:
1)Open the Administration Console.
2)Choose View Applications.
3)Select the application to debug from the list or choose New Instance 
   and create a new instance of an application.
4)Press the Streams button to see the list of playing streams, if any.
5)Click on one of the streams.
6)Press the Play Stream button.
7)A pop-up window will open and the stream will play.
8)Press the Shared Objects button to see the application’s shared objects, if any.
9)Select a shared object.
10)Press the Close Debug button to end the debug session.




七、 部署应用程序

1. Register an application with the server
To connect to an application, the server must know that the application exists. 
This process is called registering the application with the server. 
To register an application with the server, create a folder for the application 
in the applications folder. For example, create the following folder to register 
an application called “myApplication”:
RootInstall/applications/myApplication
The client-side code that connects to the application looks like the following:
myNetConnection.connect("rtmp://fms.examples.com/myApplication");
To create instances of an application, create subfolders. For example, 
the following folder creates an instance of myApplication called “room1”:
RootInstall/applications/myApplication/room1.
The client-side code that connects to the application instance looks like the following:
myNetConnection.connect("rtmp://fms.examples.com/myApplication/room1");
Every application must have a folder in the applications folder. Usually, 
the application folder contains the server-side script and any media assets, 
but the folder can also be empty. It can also contain an application-specific 
Application.xml file.


By default, the applications folder is in the root installation folder 
(C:\Program Files\Adobe\Flash Media Server 3.5\applications, on Windows). 
To configure the location of the applications folder, edit the fms.ini 
or the Vhost.xml configuration file. In the fms.ini file, edit the following parameter: 
VHOST.APPSDIR = C:\Program Files\Adobe\Flash Media Server 3.5\applications. I
n the Vhost.xml file, edit the AppsDir element.


2. Copy server-side script files to the server
Copy server-side script files for an application to the folder you registered on the server.
For example, for an application called “videoPlayer”, copy the main.asc file to 
RootInstall/applications/videoPlayer. You can also place server-side scripts in “scripts” 
subfolder. For example, you can use either of these locations:
RootInstall/applications/appName
RootInstall/applications/appName/scripts
Note: To replace a running application, copy the new files, 
then use the Administration Console to restart the application.


3. Packaging server-side files
Flash Media Server includes a command-line archive compiler utility, 
far.exe, which lets you package server-side scripts into a FAR file, 
which is an archive file like a ZIP file, to simplify deployment. 
You can also use the archive compiler utility to compile server-side script files 
to bytecode (with the file extension .ase) to speed the time required to 
load an application instance.


A large application can contain multiple server-side script files stored 
in different locations. Some files are located in the application directory 
and others are scattered in the script library paths that are defined in the server 
configuration file. To simplify deployment of your media application, you can package 
your server-side JS, ASC, and ASE files in a self-contained Flash Media Server archive 
file (a FAR file).
The FAR file is a package that includes the main script file 
(which is either main.js, main.asc, main.ase, applicationName.js, applicationName.asc, 
or applicationName.ase) and any other script files that are referred to in the main script.
The syntax for running the archive compiler utility to create a script package is 
as follows: c:\> far -package -archive <archive> -files <file1> [<file2> ... <fileN>]
The following table describes the command-line options available for far -package.
Note: If the main script refers to scripts in a subdirectory, the hierarchy 
must be maintained in the archive file. To maintain this hierarchy, 
Adobe recommends that you run the FAR utility in the same directory 
where the main script is located.


4. Copy media files to the server
Copy video and audio files to the streams/_definst_ folder in the application folder:
RootInstall/applications/someApplication/streams/_definst_/
Option                             Description
-archive archive                   Specifies the name of the archive file, which has a .far extension.
-files file1 [ file2 ... fileN ]   Specifies the list of files to be included in the archive file. At least one file is required.


If an application connects to an instance of the application, 
for example, nc.connect("rtmp://fms.example.com/someApplication/someInstance"), 
place the streams in the following folder:
RootInstall/applications/someApplication/streams/someInstance/
There are several ways to configure the server to look for media files stored in other locations. 
See the following links for more information.
See also
“Using the vod service” on page?14


5. Copy client-side files to a web server
You can deploy SWF files and HTML files on any web server. 
The SWF file contains the NetConnection.connect() call that connects 
to the application on Flash Media Server. The HTML file is a container for the SWF file. 
For more information, see the Flash documentation at www.adobe.com/go/documentation.


Deploy a Flash Lite SWF file or Adobe AIR content the same way you would deploy any 
Flash Lite or AIR application. For more information, see the Flash Lite 
and Adobe AIR documentation at www.adobe.com/go/documentation.
By default, Flash Media Server 3.5 and later install with Apache HTTP Server. 
If you installed and enabled the web server, you can deploy SWF files and HTML 
files from the same computer on which Flash Media Server is installed. 
You can also deploy JPG, GIF, and many other file types. For a complete list of 
the file types Apache serves by default, see the RootInstall/Apache2.2/conf/httpd.conf file.
By default, Apache is configured with the following aliases:
To serve files over HTTP, place the files in the RootInstall/webroot folder 
or an appropriate subfolder. To serve CGI programs, place the files in the 
RootInstall/Apache2.2/cgi-bin folder.


You can also add subfolders to these folders. Subfolders cannot 
use the following names from the fmshttpd.conf file: icons, error, SWFs, vod, cgi-bin, 
or local-cgi-bin. In addition, subfolder names cannot use the following reserved words: 
open, send, idle, fcs, fms.


By default, Apache is configured to serve all HTTP content from the 
RootInstall/webroot folder. If you want to provision the server to serve 
files from application-specific directories, edit the httpd.conf 
Apache configuration file.
0 0
原创粉丝点击