MediaCaster Auto Stop After 10 retries

This is a basic module which stops a media caster connection retrying from a edge repeater after 10 retries. The connection can be restarted by a client connection on the edge after it has been shutdown so does not prevent reconnects if new clients connect.

The module is broken into 2 seperate classes, a thread and an application module.

package  guru.thewowza.module.mediacaster;

import java.util.HashMap;
import com.wowza.wms.application.*;
import com.wowza.wms.client.IClient;
import com.wowza.wms.httpstreamer.model.IHTTPStreamerSession;
import com.wowza.wms.mediacaster.IMediaCaster;
import com.wowza.wms.mediacaster.IMediaCasterNotify2;
import com.wowza.wms.mediacaster.MediaCasterStreamItem;
import com.wowza.wms.module.*;
import com.wowza.wms.rtp.model.RTPSession;
import com.wowza.wms.stream.IMediaStream;
import com.wowza.wms.stream.IMediaStreamNameAliasProvider2;
import com.wowza.wms.stream.IMediaStreamPlay;
import com.wowza.wms.stream.livepacketizer.ILiveStreamPacketizer;

public class Resetter extends ModuleBase implements IMediaStreamNameAliasProvider2 {

	private int killLimit = 10;
	HashMap<String,Integer> StreamMap = new HashMap<String,Integer>();
	HashMap<MediaCasterStreamItem,String> StreamAlias = new HashMap<MediaCasterStreamItem,String>();
	ManageMediaCaster thisManager = null;

	public void onAppStart(IApplicationInstance appInstance) {
		String fullname = appInstance.getApplication().getName() + "/"
				+ appInstance.getName();
		getLogger().info("onAppStart: " + fullname);

		appInstance.setStreamNameAliasProvider(this);
		appInstance.addMediaCasterListener(new mediacasterNotify(appInstance,this.StreamMap,this.StreamAlias,this.killLimit));
		this.thisManager = new ManageMediaCaster(this.StreamAlias,getLogger());
		this.thisManager.setDaemon(true);
		this.thisManager.start();

	}

	public void onAppStop(IApplicationInstance appInstance) {
		String fullname = appInstance.getApplication().getName() + "/"
				+ appInstance.getName();
		getLogger().info("onAppStart: " + fullname);

		this.thisManager.quit();
	}

	public String resolvePlayAlias(IApplicationInstance appInstance, String name, IClient client) { return name; }

	public String resolvePlayAlias(IApplicationInstance appInstance, String name, IHTTPStreamerSession httpSession) { return name; }

	public String resolvePlayAlias(IApplicationInstance appInstance, String name, RTPSession rtpSession) { return name; }

	public String resolvePlayAlias(IApplicationInstance appInstance, String name, ILiveStreamPacketizer liveStreamPacketizer) { return name; }

	public String resolvePlayAlias(IApplicationInstance appInstance, String name) {	return name; }

	public String resolveStreamAlias(IApplicationInstance appInstance, String name) { return name;	}

	public String resolveStreamAlias(IApplicationInstance appInstance, String name, IMediaCaster mediaCaster) {
		if ( StreamAlias.containsKey(name) )
			{
			return "";
			}
		return name;
		}

	class mediacasterNotify implements  IMediaCasterNotify2
	{

		private int killLimit = 0;
		IApplicationInstance appInstance = null;
		HashMap<String,Integer> StreamMap = null;
		HashMap<MediaCasterStreamItem,String> StreamAlias = null;

		mediacasterNotify(IApplicationInstance appIns,HashMap<String,Integer> sm, HashMap<MediaCasterStreamItem,String> sa, int killlimit)
		{
			this.appInstance = appIns;
			this.StreamMap = sm;
			this.StreamAlias = sa;
			this.killLimit = killlimit;
		}

		public void onConnectFailure(IMediaCaster mediaCaster) { }

		public void onConnectStart(IMediaCaster mediaCaster) {
			int getCurrentCount=0;
			if ( this.StreamMap.keySet().contains(mediaCaster.getStream().getName()) )
			 	{
				getCurrentCount = this.StreamMap.get(mediaCaster.getStream().getName()); 
			 	}
			getCurrentCount++;
			this.StreamMap.put(mediaCaster.getStream().getName(), getCurrentCount);

			if ( getCurrentCount>this.killLimit )
				{
				synchronized(this.StreamAlias)
					{
					this.StreamAlias.put(mediaCaster.getMediaCasterStreamItem(), "fail");
					}
			 	}
		}

		public void onConnectSuccess(IMediaCaster mediaCaster) { }
		public void onStreamStart(IMediaCaster mediaCaster) { }
		public void onStreamStop(IMediaCaster mediaCaster) { }
		public void onMediaCasterCreate(IMediaCaster mediaCaster) { }
		public void onMediaCasterDestroy(IMediaCaster mediaCaster) { }
		public void onRegisterPlayer(IMediaCaster mediaCaster,IMediaStreamPlay player) { }
		public void onSetSourceStream(IMediaCaster mediaCaster,IMediaStream stream) { }
		public void onUnRegisterPlayer(IMediaCaster mediaCaster,IMediaStreamPlay player) { }	
	}
}

The thread class is as below. It is a simple thread which looks through the list of mediacasters to be stopped and shuts them down.

package  guru.thewowza.module.mediacaster;
import com.wowza.wms.logging.WMSLogger;
import com.wowza.wms.mediacaster.MediaCasterStreamItem;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class ManageMediaCaster extends Thread
{
	private int sleepTime = 2000;
	private WMSLogger Logger = null;
	HashMap<MediaCasterStreamItem,String> StreamAlias = null;

	public ManageMediaCaster(HashMap<MediaCasterStreamItem,String> sa, WMSLogger log)
	{
	this.StreamAlias = sa;
	this.Logger = log;
	}

	public synchronized void quit()
	{
	}

	public void run()
	{
		while (true)
			{	
			synchronized(this)
				{
				try
					{
					Thread.currentThread().wait(this.sleepTime);
					}
					catch (Exception e)
					{
					this.Logger.info("WorkerClass.run: wait: "+e.toString());
					}
				}
			List killList = new ArrayList();

			synchronized (this.StreamAlias)
				{
				Iterator theseItems = this.StreamAlias.keySet().iterator();
				while (theseItems.hasNext())
					{
					MediaCasterStreamItem test = theseItems.next();
					killList.add(test);
					test.shutdown(false);
					}
				}

			while (killList.size()>0)
				{
				MediaCasterStreamItem test = killList.remove(0);
				synchronized(this.StreamAlias)
					{
					this.StreamAlias.remove(test);
					}
				}

			}

		}

}

To use this module you will need to add the following into your Application via the Wowza Streaming Engine Manager

Name – Resetter
Description – Resetter
Class – guru.thewowza.module.mediacaster.Resetter


Comments are closed.