ModuleStreamStats: Module to log stats for each play stream on a timer

Module to log stats for each play stream on a timer.

 

package com.wowza.wms.example.module;

import java.util.*;
import com.wowza.wms.application.*;
import com.wowza.wms.amf.*;
import com.wowza.wms.application.WMSProperties;
import com.wowza.wms.module.*;
import com.wowza.wms.stream.IMediaStream;
import com.wowza.wms.stream.IMediaStreamActionNotify2;
import java.util.Timer;
import com.wowza.util.IOPerformanceCounter;

public class ModuleStreamStats extends ModuleBase {
	
	public void onStreamCreate(IMediaStream stream) {
		getLogger().info("onStreamCreate by: " + stream.getClientId());
		IMediaStreamActionNotify2 actionNotify  = new StreamListener();
		
		WMSProperties props = stream.getProperties();
		synchronized(props)
		{
			props.put("streamActionNotifier", actionNotify);
		}
		stream.addClientListener(actionNotify);
	}
	public void onStreamDestroy(IMediaStream stream) {
		getLogger().info("onStreamDestroy by: " + stream.getClientId());
		
		IMediaStreamActionNotify2 actionNotify = null;
		WMSProperties props = stream.getProperties();
		synchronized(props)
		{
			actionNotify = (IMediaStreamActionNotify2)stream.getProperties().get("streamActionNotifier");
		}
		if (actionNotify != null)
		{
			stream.removeClientListener(actionNotify);
			getLogger().info("removeClientListener: "+stream.getSrc());
		}
	}
	
	class StreamListener  implements IMediaStreamActionNotify2 
	{
		public void onPlay(IMediaStream stream, String streamName, double playStart, double playLen, int playReset) 
		{
			streamName = stream.getName();
			getLogger().info("Stream Name: " + streamName);
			
			StreamStats watchdog = new StreamStats();
			IApplicationInstance appInstance;
			try
			{
				try
				{
					appInstance = stream.getClient().getAppInstance();
				}
				catch(Exception ex)
				{
					appInstance = stream.getRTPStream().getAppInstance();
				}
				
				watchdog.stream = stream;
				watchdog.start();
				appInstance.getProperties().setProperty(streamName, watchdog);
			}
			catch(Exception ex)
			{
			}
			
		}
		
		public void onMetaData(IMediaStream stream, AMFPacket metaDataPacket) 
		{
			getLogger().info("onMetaData By: " + stream.getClientId());
		}	
		
		public void onPauseRaw(IMediaStream stream, boolean isPause, double location) 
		{
			getLogger().info("onPauseRaw By: " + stream.getClientId());
		}

		public void onSeek(IMediaStream stream, double location)
		{
		}
		
		public void onStop(IMediaStream stream)
		{
			getLogger().info("onStop By: " + stream.getClientId());
			String streamName = stream.getName();
			StreamStats watchdog = (StreamStats)stream.getClient().getAppInstance().getProperties().getProperty(streamName);
			if (watchdog != null)
				watchdog.stop();
		}
		
		public void onUnPublish(IMediaStream stream, String streamName, boolean isRecord, boolean isAppend)
		{					
		}

		public  void onPublish(IMediaStream stream, String streamName, boolean isRecord, boolean isAppend)
		{	
		}
		public void onPause(IMediaStream stream, boolean isPause, double location)
		{
		}
	}
	
	private class StreamStats
	{
		public Timer mTimer;
		public TimerTask mTask;
		public IMediaStream stream;
		public StreamStats(){
			mTask = new TimerTask()
			{
				public void run() 
				{
					getLogger().info("Run StreamStats");
	                
                    if (stream!=null)
                    {
                    	IOPerformanceCounter perf = stream.getMediaIOPerformance();
                    
                    	getLogger().info("Perf: " + perf.getMessagesOutBytes());
                    }                	
				}
			};
		}
		
		public void start(){
			
			if (mTimer==null)
				mTimer = new Timer();
			mTimer.schedule(mTask, 10000, 10000);
			getLogger().info("Start StreamStats");
		}
		
		public void stop(){
			if (mTimer != null){
				mTimer.cancel();
				mTimer=null;
				getLogger().info("Stop StreamStats");				
			}
		}
	}
}
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s