## Chapter 19. Plugins

Available in Red5 is a Plugin architecture system to enable to extend features into Red5 for an entire server or application. Plugins are loaded on startup and then configured on a per application basis.

| Tip
Currently only available via SVN trunk.

## 19.1. Loading

Plugins are compiled into jar files which will be loaded and parsed by the server on startup.

Path to load the plugins is in:

/path/to/red5/plugins

## 19.2. Configuring

Plugins can be configured via the application adaptor by setting a property inside the red5-web.xml config file. The plugins property has one child node which is a list of plugins to load for the application

    <bean id="web.handler" class="org.red5.demos.oflaDemo.Application">
    <property name="plugins">
                <list>
    ...

Inside the list can be configured multiple plugins beans

    <bean class="org.red5.server.plugin.PluginDescriptor">
    	<property name="pluginName" value="authPlugin"/>
        <property name="pluginType" value="org.red5.server.plugin.auth.AuthPlugin"/>
        <property name="method" value="getRed5AuthenticationHandler"/>
        <property name="methodReturnType" value="org.red5.server.plugin.auth.Red5AuthenticationHandler"/>
    </bean>

- pluginName - the name of the plugin compiled into the plugin

- pluginType - the fully qualified name of the plugin class ie org.red5.server.plugin.auth.AuthPlugin

- method - method is a getter for a factory method as the plugin may provide multiple features ie getRed5AuthenticationHandler

- methodReturnType - methodReturnType is the fully qualified name of the plugin factory class to provider a certain feature ie org.red5.server.plugin.auth.Red5AuthenticationHandler

Plugins are able to be configured with config properties using a property setter which is a spring hashmap list of values ie

    <bean class="org.red5.server.plugin.PluginDescriptor">
                        <property name="pluginName" value="securityPlugin"/>
                        <property name="pluginType" value="org.red5.server.plugin.security.SecurityPlugin"/>
                        <property name="method" value="getPlaybackSecurityHandler"/>
                        <property name="methodReturnType" value="org.red5.server.plugin.security.PlaybackSecurityHandler"/>
                        <property name="properties">
                            <map>
                                <entry>
                                    <key><value>htmlDomains</value></key>
                                    <value>file:///path/to/allowedHTMLdomains.txt</value>
                                </entry>
                                <entry>
                                    <key><value>swfDomains</value></key>
                                    <value>file:///path/to/allowedSWFdomains.txt</value>
                                </entry>
                            </map>
                        </property>
                    </bean>

Where using spring map syntax each property key / value is within an entry tag

    <entry>
              <key><value>htmlDomains</value></key>
                                    <value>file:///path/to/allowedHTMLdomains.txt</value>
                                </entry>

| Tip
Due to some issues with spring context paths loading in plugins, full absolute paths are required to files to be loaded ie file:///path/to/allowedHTMLdomains.txt

## 19.3. Developing

To begin developing a new plugin it's best to download already built ones from SVN to use as a template ie

http://red5.googlecode.com/svn/java/plugins/trunk/securityplugin/

To configure some ant properties to compile the plugin correctly, inside build.properties update the following

red5.root=/www/red5_server_xuggle_timestamp_fixes
main-class=org.red5.server.plugin.security.SecurityPlugin

Where red5.root is the path to red5 and main-class is the fully qualified name of the plugin.

At the top of the ant build script, update the project name to the name of the plugin which will be used to generate the plugin jar file

    <project name="securityplugin" basedir="." default="all" xmlns:ivy="antlib:org.apache.ivy.ant">

| Tip
Make sure the following ant property is set to either the main-class property or static to the plugin fully qualified name. If this is not setup correctly the Plugin loader will detect there is no manifest and not load the plugin. <attribute name="Red5-Plugin-Main-Class" value="${main-class}"/>

### 19.3.1. Plugin Main Class

The plugin main class requires to extend the Red5Plugin base class which the plugin loader will then use this as the main class for loading the plugin and factory methods.

    public class SecurityPlugin extends Red5Plugin {

    	private static Logger log = Red5LoggerFactory.getLogger(SecurityPlugin.class, "plugins");

    	private static Serializer serializer = new Serializer();

    	private MultiThreadedApplicationAdapter application;

    	public void doStart() throws Exception {
    		log.debug("Start");
    	}

    	public void doStop() throws Exception {
    		log.debug("Stop");
    	}

    	public String getName() {
    		return "securityPlugin";
    	}

    	public void setApplication(MultiThreadedApplicationAdapter app) {
    		log.trace("Setting application adapter: {}", app);
    		this.application = app;
    	}

    	//methods specific to this plug-in

    	public PlaybackSecurityHandler getPlaybackSecurityHandler() {
    		PlaybackSecurityHandler ph = null;
    		try {
    			ph = (PlaybackSecurityHandler) Class.forName("org.red5.server.plugin.security.PlaybackSecurityHandler").newInstance();
    			ph.setApplication(application);
    		} catch (Exception e) {
    			log.error("PlaybackSecurityHandler could not be loaded", e);
    		}
    		return ph;
    	}

    	public PublishSecurityHandler getPublishSecurityHandler() {
    		PublishSecurityHandler ps = null;
    		try {
    			ps = (PublishSecurityHandler) Class.forName("org.red5.server.plugin.security.PublishSecurityHandler").newInstance();
    			ps.setApplication(application);
    		} catch (Exception e) {
    			log.error("PublishSecurityHandler could not be loaded", e);
    		}
    		return ps;
    	}

    	public SharedObjectSecurityHandler getSharedObjectSecurityHandler() {
    		SharedObjectSecurityHandler sh = null;
    		try {
    			sh = (SharedObjectSecurityHandler) Class.forName("org.red5.server.plugin.security.SharedObjectSecurityHandler").newInstance();
    			sh.setApplication(application);
    		} catch (Exception e) {
    			log.error("SharedObjectSecurityHandler could not be loaded", e);
    		}
    		return sh;
    	}

    	//common methods

    	/**
    	 * Invokes the "onStatus" event on the client, passing our derived status.
    	 *
    	 * @param conn
    	 * @param status
    	 */
    	public static void writeStatus(IConnection conn, StatusObject status) {
    		//make a buffer to put our data in
    		IoBuffer buf = IoBuffer.allocate(128);
    		buf.setAutoExpand(true);
    		//create amf output
    		Output out = new Output(buf);
    		//mark it as an amf object
    		buf.put(AMF.TYPE_OBJECT);
    		//serialize our status
        	status.serialize(out, serializer);
        	//write trailer
    		buf.put((byte) 0x00);
    		buf.put((byte) 0x00);
    		buf.put(AMF.TYPE_END_OF_OBJECT);
    		//make the buffer read to be read
    		buf.flip();

    		//create an RTMP event of Notify type
    		IRTMPEvent event = new Notify(buf);

    		//construct a packet
    		Header header = new Header();
    		Packet packet = new Packet(header, event);

    		//get our stream id
    		int streamId = BaseRTMPHandler.getStreamId();
    		//set channel to "data" which im pretty sure is 3
    		header.setChannelId(3);
    		header.setTimer(event.getTimestamp()); //0
    		header.setStreamId(streamId);
    		header.setDataType(event.getDataType());

    		//write to the client
    		((RTMPConnection) conn).write(packet);
    	}

    }

The getter method getName is required to be set so the plugin can be identified and loaded correctly using the plugin config in the application

    public String getName() {
    		return "securityPlugin";
    }

    public void doStart() throws Exception {
    		log.debug("Start");
    	}

    	public void doStop() throws Exception {
    		log.debug("Stop");
    	}

    	public String getName() {
    		return "securityPlugin";
    	}

    	public void setApplication(MultiThreadedApplicationAdapter app) {
    		log.trace("Setting application adapter: {}", app);
    		this.application = app;
    	}

Other methods are doStart , doStop and setApplication setter method which is required to set a reference to the loaded application.

The plugin factory method is required to be configured and return a reference to the factory class which is then loaded in the application config. A reference to the application can also be set if desired so the factory method class can manipulate methods and properties on the application.

    public PlaybackSecurityHandler getPlaybackSecurityHandler() {
    		PlaybackSecurityHandler ph = null;
    		try {
    			ph = (PlaybackSecurityHandler) Class.forName("org.red5.server.plugin.security.PlaybackSecurityHandler").newInstance();
    			ph.setApplication(application);
    		} catch (Exception e) {
    			log.error("PlaybackSecurityHandler could not be loaded", e);
    		}
    		return ph;
    	}

### 19.3.2. Factory Method Class

The factory method class is what gets configured to load and is where the features to run happen. It is required to extend the ApplicationLifeCycle class as well as implement the IRed5PluginHandler interface.

    public abstract class SecurityBase extends ApplicationLifecycle implements IRed5PluginHandler {
    ...

Setter methods for both application and properties is required so the class is able to get a reference to the application as well as have property configs set.

    	public void setApplication(MultiThreadedApplicationAdapter app) {
    		application = app;
    	}

    	public void setProperties(Map<String, Object> props) {
    		properties = props;
    	}
    }

An init method is required to be overridden to enable the factory class to start and then enable features and manipulate the application

    	@Override
    	public void init() {
    		if (properties.containsKey("htmlDomains")) {
    			htmlDomains = properties.get("htmlDomains").toString();
    		}
    		if (properties.containsKey("swfDomains")) {
    			swfDomains = properties.get("swfDomains").toString();
    		}

    		allowedHTMLDomains = readValidDomains(htmlDomains, "HTMLDomains");

    		// Populating the list of domains which are allowed to host a SWF file
    		// which may connect to this application
    		allowedSWFDomains = readValidDomains(swfDomains, "SWFDomains");

    		// Logging
    		if (HTMLDomainsAuth) {
    			log.debug("Authentication of HTML page URL domains is enabled");
    		}
    		if (SWFDomainsAuth) {
    			log.debug("Authentication of SWF URL domains is enabled");
    		}

    		log.debug("...loading completed.");

    		//now register with the application
    		application.registerStreamPlaybackSecurity(this);
    	}

Prev | Home | Next
Chapter 18. Testing Red5 | Red5 Open Source Flash Server | Appendix A. RTMP Specification