• Module to limit the number of connections to an application

    Here is a simple module that limits the number of connections to an application:

    Code:
    package com.wowza.wms.plugin.collection.module;
    
    import com.wowza.wms.amf.*;
    import com.wowza.wms.application.*;
    import com.wowza.wms.client.*;
    import com.wowza.wms.httpstreamer.model.IHTTPStreamerSession;
    import com.wowza.wms.request.*;
    import com.wowza.wms.rtp.model.*;
    import com.wowza.wms.module.*;
    
    public class ModuleLimitConnectionsToApplication extends ModuleBase
    {
    	static final public int MAXCONNECTIONS = 200;
    	
    	private int maxApplicationConnections = MAXCONNECTIONS;
    	
    	public void onAppStart(IApplicationInstance appInstance)
    	{
    		this.maxApplicationConnections = appInstance.getProperties().getPropertyInt("maxApplicationConnections", maxApplicationConnections);
    		getLogger().info("ModuleLimitConnectionsToApplication limit: " + maxApplicationConnections);
    	}
    	
    	public void changeLimit(IClient client, RequestFunction function,
    			AMFDataList params) {
    		client.getAppInstance().broadcastMsg("handlerName");
    		Integer newLimit = params.getInt(PARAM1);
    		this.maxApplicationConnections = newLimit;
    	}
    	
    	public void onConnect(IClient client, RequestFunction function, AMFDataList params)
    	{	 
    		IApplicationInstance appInstance = client.getAppInstance();
    		IApplication app = appInstance.getApplication();
    		long count = app.getConnectionCounter().getCurrent(); 
    		
    		String flashver = client.getFlashVer();
    		getLogger().info("Flashver: " + flashver);
    		
    		Boolean isPublisher = false;
    		try
    		{
    		isPublisher = flashver.startsWith(client.getAppInstance().getProperties().getPropertyStr("AllowEncoder"));
    		}
    		catch(Exception ex) 
    		{
    		}
    		
    		getLogger().info("ModuleLimitConnectionsToApplication count: " + (count + 1));
    		
    		if ((count+1) > this.maxApplicationConnections && !isPublisher)
    		{
    			client.rejectConnection("Over application connection limit ["+app.getName()+"/"+appInstance.getName()+"]: Limit is: "+this.maxApplicationConnections);
    			getLogger().info("ModuleLimitConnectionsToApplication Flash connection rejected ");
    		}
    	}
    	
    	public void onHTTPSessionCreate(IHTTPStreamerSession httpSession)
    	{
    		IApplicationInstance appInstance = httpSession.getAppInstance();
    		IApplication app = appInstance.getApplication();
    		long count = app.getConnectionCounter().getCurrent();
    		
    		getLogger().info("ModuleLimitConnectionsToApplication count: " + (count + 1));
    		
    		if ((count+1) > this.maxApplicationConnections)
    		{
    			httpSession.rejectSession();
    			getLogger().info("ModuleLimitConnectionsToApplication Smooth connection rejected ");
    		}		
    	}
    
    
    	public void onRTPSessionCreate(RTPSession rtpSession)
    	{
    		IApplicationInstance appInstance = rtpSession.getAppInstance();
    		IApplication app = appInstance.getApplication();
    		long count = app.getConnectionCounter().getCurrent();
    
    		getLogger().info("ModuleLimitConnectionsToApplication count: " + (count + 1));
    		
    		if ((count+1) > this.maxApplicationConnections)
    		{
    			rtpSession.rejectSession();
    			getLogger().info("ModuleLimitConnectionsToApplication RTSP connection rejected ");
    		}
    	}	
    }
    A compiled version of this module is included in the Wowza Modules Collection. Download and unzip the collection, then copy /lib/wms-plugin-collection.jar from the package to the Wowza /lib folder. Then restart Wowza.

    Add this Module last in the Modules list of /conf/[app-name]/Application.xml

    Code:
    <Module> 
    	<Name>ModuleLimitStreamViewers</Name> 
    	<Description>Limit connects to an Application</Description> 
    	<Class>com.wowza.wms.plugin.collection.module.ModuleLimitConnectionsToApplication</Class> 
    </Module>
    You can set the limit by setting the following property in the <Properties> section at the bottom of [install-dir]/conf/[application]/Application.xml (where [application] is the name of your application).

    Code:
    <Property>
    	<Name>maxApplicationConnections</Name>
    	<Value>200</Value>
    	<Type>Integer</Type>
    </Property>
    Logging
    The logs will show the limit and the number of connections. In this example, it is limited to 200 connections.
    Code:
    comment	server	INFO	200	-	ModuleLimitConnectionsToApplication limit: 200
    The logs will report the number of connections. In this example, there is 1 current connection.
    Code:
    comment	server	INFO	200	-	ModuleLimitConnectionsToApplication count: 01
    When you have exceeding the number of connections allowed, you will see a comment in the log such as:
    Code:
    comment	server	INFO   	200	-	ModuleLimitConnectionsToApplication Flash connection rejected
    comment	server	INFO	        200	-	ModuleLimitConnectionsToApplication Cupertino connection rejected
    comment   server       WARN	200	-	HTTPStreamerAdapterCupertinoStreamer.onPlaylist[vod/mp4:sample.mp4/playlist.m3u8]: Over license limit
    comment	server	WARN	200	-	HTTPStreamerAdapterSanJoseStreamer.onPlaylist: Over license limit
    If you are you using a live encoder, also add this property:
    If you are using a Telestream Wirecast encoder, set the value to "Wirecast". If you are using an RTMP live encoder that uses the Flash Media version, set this value to "FM".

    <Property>
    <Name>AllowEncoder</Name>
    <Value>Wirecast</Value> <!--FM, Wirecast-->
    </Property>


    Comments 14 Comments
    1. juangiga -
      Hi, any form to limit the number of listeners-conections (rmtp shoutcast -re-streamaing) by alias file?
    1. rrlanham -
      There is no connection limit feature involved in the Alias system.

      Richard
    1. Lam-Strim -
      Hi, I'm using the plugin.collection.module.ModuleLimitConnectionsToA pplication, in order to limit the number of viewers on a shoutcast-record application.

      Do I have to set something like :
      <Property>
      <Name>AllowEncoder</Name>
      <Value>Wirecast</Value> <!--FM, Wirecast-->
      </Property>
      in order to prevent the connection icecast/wowza from being rejected ?

      thank you,
      Alex
    1. rrlanham -
      Alex,

      You shouldn't have to do that. You are pulling from edgecast source, which I don't think will trigger anything in this module. Subscribing clients will be limited.

      Richard
    1. Lam-Strim -
      Thank you
    1. ibenarobeno -
      Looks like it does not work with nDVR application. Is there a way to get the limit working there as well?
    1. rrlanham -
      Enabling nDVR should not affect this module. Make sure it is being loaded correctly, check the access log around the time of the app-start event for that application, see if there are any load errors for that module.

      Richard
    1. GuillaumeRemy -
      On my WowzaMediaServer-3.0.3 when i used ModuleLimitConnectionsToApplication to my Application.xml my application do not respond.
    1. rrlanham -
      Take a look at the access and error log for a clue. Run Wowza in stand-alone (/bin/startup.bat) mode instead of as a service to see log output in the console.

      Richard
    1. f3209485 -
      what the use of the function changeLimit? I can't find it in server-side API.
      how to trigger the function?
    1. rrlanham -
      It is a function in that example, not in the API. It is set by remote call from a Flash RTMP client with NetConnection.call("changeLimit", 100). 100 is the new limit

      Richard
    1. f3209485 -
      thank you
    1. f3209485 -
      “import com.wowza.wms.amf.*;”
      is it a jar file?
      where can i find it?
      i want use it in my program,how?
    1. rrlanham -
      It is part of the Wowza core. Take a look at the ServerSideModules example to see how AMF data is sent to Flash RTMP clients, how these map to client-side data.

      Richard