Log play statistics on a timer with the Wowza Streaming Engine Java API

Use the following module in the Wowza Streaming Engine™ Java API 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.media.model.MediaCodecInfoAudio;
import com.wowza.wms.media.model.MediaCodecInfoVideo;
import com.wowza.wms.module.*;
import com.wowza.wms.stream.IMediaStream;
import com.wowza.wms.stream.IMediaStreamActionNotify3;
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());
		IMediaStreamActionNotify3 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());

		IMediaStreamActionNotify3 actionNotify = null;
		WMSProperties props = stream.getProperties();
		synchronized(props)
		{
			actionNotify = (IMediaStreamActionNotify3)stream.getProperties().get("streamActionNotifier");
		}
		if (actionNotify != null)
		{
			stream.removeClientListener(actionNotify);
			getLogger().info("removeClientListener: "+stream.getSrc());
		}
	}

	class StreamListener  implements IMediaStreamActionNotify3 
	{
		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)
		{
		}

		public void onCodecInfoAudio(IMediaStream stream,
				MediaCodecInfoAudio codecInfoAudio) {
		}

		public void onCodecInfoVideo(IMediaStream stream,
				MediaCodecInfoVideo codecInfoVideo) {
		}
	}

	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");
			}
		}
	}
}