# Module to calculate bitrate of a video on demand stream

**URL:** <https://community.wowza.com/t/module-to-calculate-bitrate-of-a-video-on-demand-stream/80>\
**Category:** Wowza Streaming Engine\
**Created:** [October 22, 2008, 9:42am UTC](https://community.wowza.com/t/module-to-calculate-bitrate-of-a-video-on-demand-stream/80 "2008-10-22T09:42:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.wowza.com/system/32/447_2.png) [@system](https://community.wowza.com/u/system)\
**Post date:** [October 22, 2008, 9:42am UTC](https://community.wowza.com/t/module-to-calculate-bitrate-of-a-video-on-demand-stream/80/1 "2008-10-22T09:42:25Z")

</div>

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

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

```auto
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:

```auto
var resultObj:Object = new Object();
resultObj.onResult = function(bitrate:Number)
{
    trace("bitrate: "+bitrate);
}
nc.call("getStreamBitrate", resultObj, streamName);

```

AS3:

```auto
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](http://community.wowza.com/t/-/237)

Charlie

---

<div class="post-metadata">

**Author:** ![Masateru\_12288](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Masateru\_12288](https://community.wowza.com/u/Masateru_12288)\
**Post date:** [July 6, 2009, 8:58am UTC](https://community.wowza.com/t/module-to-calculate-bitrate-of-a-video-on-demand-stream/80/2 "2009-07-06T08:58:36Z")

</div>

I tried this module.

but, the following error message came out.

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

```

The code of Java and Flash copied the post it.

conf/[Application Name]/Application.xml

```auto
<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

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [July 6, 2009, 12:04pm UTC](https://community.wowza.com/t/module-to-calculate-bitrate-of-a-video-on-demand-stream/80/3 "2009-07-06T12:04:44Z")

</div>

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

Richard

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [July 7, 2009, 11:00am UTC](https://community.wowza.com/t/module-to-calculate-bitrate-of-a-video-on-demand-stream/80/4 "2009-07-07T11:00:27Z")

</div>

I’ll give it a try…

Richard

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [July 7, 2009, 2:48pm UTC](https://community.wowza.com/t/module-to-calculate-bitrate-of-a-video-on-demand-stream/80/5 "2009-07-07T14:48:14Z")

</div>

Masateru,

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

Richard

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [July 8, 2009, 11:12am UTC](https://community.wowza.com/t/module-to-calculate-bitrate-of-a-video-on-demand-stream/80/6 "2009-07-08T11:12:30Z")

</div>

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

Richard

---

<div class="post-metadata">

**Author:** ![Masateru\_12288](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Masateru\_12288](https://community.wowza.com/u/Masateru_12288)\
**Post date:** [July 7, 2009, 12:13am UTC](https://community.wowza.com/t/module-to-calculate-bitrate-of-a-video-on-demand-stream/80/7 "2009-07-07T00:13:13Z")

</div>

Hi

Setting of Streams

conf/[Application Name]/Application.xml

```auto
<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

---

<div class="post-metadata">

**Author:** ![Masateru\_12288](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Masateru\_12288](https://community.wowza.com/u/Masateru_12288)\
**Post date:** [July 8, 2009, 4:37am UTC](https://community.wowza.com/t/module-to-calculate-bitrate-of-a-video-on-demand-stream/80/8 "2009-07-08T04:37:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Masateru\_12288](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Masateru\_12288](https://community.wowza.com/u/Masateru_12288)\
**Post date:** [July 9, 2009, 12:55pm UTC](https://community.wowza.com/t/module-to-calculate-bitrate-of-a-video-on-demand-stream/80/9 "2009-07-09T12:55:59Z")

</div>

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.
