Module to calculate bitrate of a video on demand stream

Simple module to calculate video on demand bitrate in bits per second.

Note: It requires Wowza Media Server Pro 1.7.0 or greater.

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

A compiled version of this module is included in a collection of utility modules that you can download here:

http://community.wowza.com/t/-/237

Charlie

I tried this module.

but, the following error message came out.

ERROR server comment - RandomAccessReaderFactory: createRandomAccessReader: java.lang.NullPointerException

The code of Java and Flash copied the post it.

conf/[Application Name]/Application.xml

<Modules>
........
<Module>
<Name>ModuleCalculateBitrate</Name>
<Description>ModuleCalculateBitrate</Description>
<Class>com.wowza.wms.plugin.test.module.ModuleCalculateBitrate</Class>
</Module>
</Modules>

Server Version : Wowza Media Server Pro10 1.7.0 build11995

I think it means that the streamName you are passing to Wowza in the netconnection.call does not exist.

Richard

I’ll give it a try…

Richard

Masateru,

The code sample above has been updated. Try it now.

Richard

I tested it with Extremist.flv and Extemists.m4v. Both worked for me, and the results were accurate.

Richard

Hi

Setting of Streams

conf/[Application Name]/Application.xml

<Streams>
<StreamType>default</StreamType>
<StorageDir>${com.wowza.wms.AppHome}/content</StorageDir>
<Properties>
</Properties>
</Streams>

There are Extremists.flv and Extremists.m4v in the folder with at the installation.

M.Kagawa

Richard

I tried it.

It operated without abnormality!!

The file of m4v was not able to be acquired though the FLV file was able to be acquired in the code.

Masateru

Hi

Is the cause my environment?

Java Version: 1.6.0_06

Java VM Version: 10.0-b22

Java Spec Version: 1.6

There is FLV in variable identifier streamExt when call from FLash is “Extremist.flv”.

There is FLV in variable identifier streamExt when call from FLash is “Extremist.m4v”.

When “MP4” is input to variable identifier streamExt, it work.