Application Development Tips and Tricks > Application design and development > Managing bandwidth

 

Managing bandwidth

You can control the amount of data the server sends to each client by providing an approximate bandwidth capacity. There are a few ways to do so. One way is to configure the capacity for the Flash Communication Server in the configuration file (Config.xml). For more information on this technique, see Managing Flash Communication Server Help. Another way is to use NetStream.receiveVideo to specify the frame rate per second of the incoming video (the default value is the frame rate of the movie). A third way to match the data flow with capacity is to use Camera.setQuality on the client side and Client.setBandwidthLimit on the server side to inform the server of the client's capacity. This technique is illustrated in the following sample.

The doc_bandwidth sample allows the user to choose between different bandwidth settings. On the client side, the microphone and camera settings are updated according to the user's selection. On the server side, the bandwidth limit is updated. You can find the FLA and SWF files for the doc_bandwidth sample in the /flashcom_help/help_collateral/doc_bandwidth directory under the installed Macromedia Flash MX authoring directories or in the server's installation directory.

To see this application in action, create a directory named doc_bandwidth in your Flash Communication Server applications directory, and then open doc_bandwidth.swf.

The client-side ActionScript provides the interface for the user to choose between three settings: Modem, DSL, or LAN. There are three buttons on the stage, which when selected return 1 for a modem setting, 2 for a DSL setting, or 3 for a LAN setting. When the user changes the bandwidth limit choice, the camera and microphone settings are updated. Because this change is barely noticeable, extra code in the updateBandwidth function also changes the screen size based on the settings chosen.

_root.Modem_btn.onPress = function() {
	// Call updateBandwidth to change
	// camera and mic settings and inform
	// the server
	_root.updateBandwidth(1);
}

_root.DSL_btn.onPress = function() {
	_root.updateBandwidth(2);
}

_root.LAN_btn.onPress = function() {
	_root.updateBandwidth(3);
}

On the first frame of the FLA file, you can find the following code, which initializes the camera and microphone, plays the stream from the server, and updates the camera and microphone settings based on the user's choices.

Note: If you are using speakers instead of a headset, you may want to comment out the call to Microphone.get() to avoid audio feedback. For more information, see Avoiding audio feedback.

#include "NetDebug.as"

stop();

// Initialize movie by getting a camera and microphone, and
// Configure the initial camera and microphone settings
client_cam = Camera.get();
client_cam.setMode(150,120,5);
client_cam.setQuality(0, 90);

client_mic = Microphone.get();
client_mic.setRate(22);

// Get the stream to publish and play
function getPlayStream() {

	// Get new net connection
	client_nc = new NetConnection();
	
	// Handle status message
	client_nc.onStatus = function(info) {
		trace("Level: " + info.level + newline + "Code: " + info.code);
	}

	client_nc.connect("rtmp:/doc_bandwidth/room_01");
	
	// Create a stream to which to publish
	out_ns = new NetStream(client_nc);
	out_ns.attachVideo(client_cam);
	out_ns.attachAudio(client_mic);
	out_ns.publish("myAV");

	// Create a stream to receive the published data
	in_ns = new NetStream(client_nc);
	Output_mc.fromSrvr.attachVideo(in_ns);
	Output_mc.fromSrvr.attachAudio(in_ns);
	in_ns.play("myAV");

}

// Called from the bandwidth buttons
function updateBandwidth(b) {
	
	// Respond to a change in the bandwidth
	// If "Modem" was selected
	if ( b == 1 ) {
		client_cam.setMode(160,120,2);
		client_cam.setQuality(0, 75);
		client_cam.setKeyFrameInterval(3);
		client_mic.setRate(5);
		
		// For demonstration purposes, change size of screen
		Output_mc._height = 100;
		Output_mc._width = 150;
		
	// If "DSL" was selected
	} else if ( b == 2 ) {
		client_cam.setMode(160,120,5);
		client_cam.setQuality(0, 85);
		cam.setKeyFrameInterval(5);
		client_mic.setRate(11);
		
		// For demonstration purposes, change size of screen
		Output_mc._height = 130;
		Output_mc._width = 175;

	// If "LAN" was selected
	} else /*if ( b == 3 )*/ {
		client_cam = Camera.get();
		client_cam.setMode(160,120,15);
		client_cam.setQuality(0, 90);
		client_cam.setKeyFrameInterval(10);
		client_mic.setRate(22);
		
		// For demonstration purposes, change size of screen
		Output_mc._height = 150;
		Output_mc._width = 200;

	}
	// Call the server function setBandwidth and pass the user's
	// selection, b.
	client_nc.call("setBandwidth", 0, b);
}

// Get the stream to play

getPlayStream();

Meanwhile, the main.asc file contains a setBandwidth function, which when called from the client-side code, updates the bandwidth limit with the appropriate settings.

// If server-side code is part of the application,
// it must define an onConnect function that accepts
// the client connection.
application.onConnect = function(client) {

	// Establish the connection
	application.acceptConnection(client);
}

// Called when user presses a bandwidth choice (Modem=1, DSL=2, LAN=3)
Client.prototype.setBandwidth = function(bw) {	

	// set the bandwidth for the client
	if ( bw == 1 ) {
		// modem settings
		this.setBandwidthLimit( 35000/8, 22000/8 );
	} else if ( bw == 2 ) {
		// DSL settings
		this.setBandwidthLimit( 800000/8, 100000/8 );
	} else /*if ( bw == 3 )*/ {
		// LAN settings
		this.setBandwidthLimit( 400000, 400000 );
	}
}