How to calculate the bitrate of a video on demand stream (ModuleCalculateBitrate)

The following module calculates video-on-demand (VOD) bitrate in bits-per-second.

Note: This module has been removed from the Wowza Module Collection because it has been replaced by a core method. See ModuleCore.getStreamLength(IClient client, RequestFunction function, AMFDataList params) in the Wowza Streaming Engine Server-Side API documentation. The source code has been retained here for reference.
package com.wowza.wms.plugin.test.module;

import com.wowza.wms.amf.*;
import com.wowza.wms.application.*;
import com.wowza.wms.client.*;
import com.wowza.wms.request.*;
import com.wowza.wms.stream.*;
import com.wowza.wms.util.*;
import com.wowza.wms.module.*;

public class ModuleCalculateBitrate extends ModuleBase
{
	public void getStreamBitrate(IClient client, RequestFunction function, AMFDataList params)
	{
		double ret = 0;
		String streamName = getParamString(params, PARAM1);
		String streamExt = MediaStream.BASE_STREAM_EXT;
		String queryStr = "";

		String[] streamDecode = ModuleUtils.decodeStreamExtension(streamName, streamExt);
		streamName = streamDecode[0];
		streamExt = streamDecode[1];

		boolean isStreamNameURL = streamName.indexOf("://") >= 0;
		int streamQueryIdx = streamName.indexOf("?");
		if (!isStreamNameURL && streamQueryIdx >= 0)
		{
			queryStr = streamName.substring(streamQueryIdx+1);
			streamName = streamName.substring(0, streamQueryIdx);
		}

		IApplicationInstance appInstance = client.getAppInstance();
		String oldStreamName = null;

		IMediaReader mediaReader = MediaReaderFactory.getInstance(appInstance, client.getVHost().getMediaReaders(), streamExt);
		if (mediaReader != null)
		{
			MediaStreamMap streams = appInstance.getStreams();
			IMediaStream stream = new MediaStreamDisconnected();
			stream.init(streams, 0, new WMSProperties());
			stream.setName(streamName, oldStreamName, streamExt, queryStr, 0, -1, 1);
			stream.setClient(client);

			String basePath = client.getAppInstance().getStreamStoragePath();
			mediaReader.init(client.getAppInstance(), stream, streamExt, basePath, streamName);
			long lastTC = mediaReader.getDuration();
			double duration = (double)lastTC/1000.0;
			long length = mediaReader.getLength();
			if (duration != 0)
				ret = (((double)length*8.0)/duration);
			mediaReader.close();

			getLogger().debug("calculateBitrate duration:"+duration+" length:"+length+" ret:"+ret);
		}
		else
			getLogger().warn("calculateBitrate: Could not create MediaReader for stream: "+streamName);

		getLogger().info("calculateBitrate ["+streamName+"] bitrate:"+ret);
		sendResult(client, params, ret);
	}

}
Client-side code looks like this:

AS2:
var resultObj:Object = new Object();
resultObj.onResult = function(bitrate:Number)
{
    trace("bitrate: "+bitrate);
}
nc.call("getStreamBitrate", resultObj, streamName);
AS3:
private function bitrateReturn(bitrate:Number):void
{
    trace("bitrate: "+bitrate);
}
nc.call("getStreamBitrate", new Responder(bitrateReturn), streamName);

Wowza media server software and all components, including modules, source code, and other related items offered on this page, are copyrighted (c) 2006-2014 by Wowza Media Systems, LLC, all rights reserved, and are licensed pursuant to the Wowza Media Software End User License Agreement.
Updated: For Wowza Streaming Engine 4.0 on 02-26-2014.

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