• How to modify or control a stream by overriding playback

    This article includes example code of various actions you can do when using an event handler to override the client's RTMP playback request. Overriding the play command may be useful when you want to control stream access, change stream properties, log data for analysis, or perform other actions.

    Contents


    Restrict access to the stream
    Change the stream name


    Restrict access to the stream

    The following example code is the first step for overriding play to control access to content. Each call from the Flash player to play content will be intercepted by this module. You can then decide to let playback continue or return an error message that is handled by the NetStream.onStatus event handler within the client.

    package com.wowza.wms.plugin.test.module;
    
    import java.util.*; 
    
    import com.wowza.wms.amf.*;
    import com.wowza.wms.client.*;
    import com.wowza.wms.module.*;
    import com.wowza.wms.request.*;
    import com.wowza.wms.stream.*;
    import com.wowza.wms.application.*;
    
    public class ModuleOverrideExample extends ModuleBase
    {
    	public void play(IClient client, RequestFunction function, AMFDataList params)
    	{
    		boolean doit = true;
    		IMediaStream stream = getStream(client, function);		
    		String streamName = getParamString(params, PARAM1);
    
    		// check to see if client should be allowed to play content
    		doit = false;
    		
    		if (doit)
    			invokePrevious(this, client, function, params);
    		else
    			sendStreamOnStatusError(stream, "NetStream.Play.Failed", "Access denied: "+streamName);
    	}
    }
    From here, you can specify who can access the stream by including an allowed list of IP addresses, using URL parameters passed by the player, or using other controls as desired.

    The ModuleOverridePlayLimitIPs module example below overrides the play command and compares the client IP address to an allowed list, which is read from a property in Application.xml, to control access for RTMP playback.

    package com.wowza.wms.example;
    
    import com.wowza.wms.amf.*; 
    import com.wowza.wms.client.*;
    import com.wowza.wms.module.*;
    import com.wowza.wms.stream.*;
    import com.wowza.wms.request.*;
    
    public class ModuleOverridePlayLimitIPs extends ModuleBase {
    
    	public void play(IClient client, RequestFunction function,
    	        AMFDataList params) {
    		getLogger().info("override play: limit IPs");
    		IMediaStream stream = getStream(client, function);
    		String streamName = getParamString(params, PARAM1);	
    		Boolean doit = false;
    		String ips[] = client.getAppInstance().getProperties().getPropertyStr("AllowedIPs","").split(",");
    		
    		for (int i=0;i<ips.length; i++)
    		{
    			if (client.getIp().equals(ips[i]))
    			{
    				doit = true;
    				break;
    			}
    		}
    		
    		if (doit)
    		{
    			invokePrevious(this, client, function, params);
    		}
    		else
    		{
    			sendStreamOnStatusError(stream, "NetStream.Play.Failed", "IP denied: "+streamName);
    		}	
    	}
    }
    To control which IP addresses are allowed, add this property to the Application.xml file:

    <Property>
    	<Name>AllowedIPs</Name>
    	<Value>127.0.0.1,192.168.1.100</Value>
    </Property>

    Changing the stream name

    The following example module overrides play and remaps the stream name.

    package com.wowza.wms.example.module;
    
    import com.wowza.wms.amf.*;
    import com.wowza.wms.client.*;
    import com.wowza.wms.module.*;
    import com.wowza.wms.request.*;
    
    public class ModuleOverridePlayRemapStream extends ModuleBase {
    	
    	public void play(IClient client, RequestFunction function,
    	        AMFDataList params) {
    		getLogger().info("Overriding Play");
    		
    		String streamName = getParamString(params, PARAM1);
    		
    		if (streamName.equalsIgnoreCase("test"))
    				params.set(PARAM1, "sample.mp4");
    		
    		invokePrevious(client, function, params);
    	}
    }


    Published: For Wowza Streaming Engine on 10-03-2010.

    If you're having problems or want to discuss this article, post in our forum.