spring streams EventAdapter

Adapting the position-based stream measurement to event-based measurement.

To adapt the standard position based stream measurement to an event-based model an adapter can be used to negotiate between the different models. The adapter allows to the event-based left side (the client side) to signal events like Start and Stop and mediates these events to stream positions at the right side. The mediation is simply done by increasing a simulated stream position internally using a timer for all states where the stream is playing (after the Start event etc).

function EventAdapter(pInfo, pVersion) {
	var playerInfo = pInfo;
	var playerVersion = pVersion;

	this.onStart = onStart;
	this.onStop  = onStop;
	this.onPause = onPause;
	this.onResume = onResume;
	this.onProgress = onProgress;
	this.setDuration = setDuration;

	var timer = setInterval(collect, 1000);

	var state = 0;
	var position = 0;

	function collect() {
		if(state == 1) position ++; 
	}


	/* external API (left side) */ 
	function onStart() {
		state = 1;
		position = 0;
	}


	function onStop() {
		state = 0;
		position = 0; // most players rewind on stop
	}

	

	function onPause() {
		state = 2;
	}

	

	function onResume() {
		state = 1;
	}

	

	/*
	 * sets the progress o the stream in seconds
	 * @param v the progress in seconds
	 * @return
	 */

	function onProgress(v) {
		if(typeof v != 'number') {
			v = parseInt(v);
		}

		if(v >= 0) position = v;
	}

	

	function setDuration(v) {
		if(typeof v != 'number') {
			v = parseInt(v);
		}

		if(v >= 0)
			duration = v;
	}

	

	/*  internal API (right side) */
	this.getMeta = getMeta;
	this.getDuration = getDuration;
	this.getPosition = getPosition;

	function getMeta(v) {
		return {
			"pl" : playerInfo,
			"plv": playerVersion,
			"sx" : screen.width,
			"sy" : screen.height
		}
	}

	

	function getDuration(v) {
		return duration;
	}

	function getPosition(v) {
		return position;
	}

}

To use the adaptor it is instantiated with the player-information

var adapter =  new EventAdapter("myOwnPlayer","V2.2");

and then added to the sensor using the track method:

sensors.track("stream1", desc, adapter);

The full example is 

<html>
<head>
<script src="springstream.js"></script>
<script src="eventadapter.js"></script>
</head>
<body onunload="unload();">
<form name="f"><input type="text" name="t" size="120"/></form>
<hr>

<form name="player">
<!-- 

	 example shows how to control the eventadapter using input-buttons 

-->
<input type="submit" value="Start" onClick="adapter.onStart(); return false;"/>
<input type="submit" value="Stop" onClick="adapter.onStop(); return false;"/>
<input type="submit" value="Pause" onClick="adapter.onPause(); return false;"/>
<input type="submit" value="Resume" onClick="adapter.onResume(); return false;"/>
<input type="text" name="position"/>
<input type="submit" value="&lt;- setPosition" onClick="adapter.onProgress(player.position.value); return false;"/>
</form>


<script type="text/javascript">

var sensors = new SpringStreams("test"); 
var adapter =  new EventAdapter("myOwnPlayer","V2.2");

adapter.setDuration(1000); // duration of the stream in seconds

var desc = {
		"stream": "teststream"
}

sensors.track("stream1", desc, adapter);
sensors.debug = function(v) {
	f.t.value = v;
}

function unload() {
	sensors.unload();
	// give time for submission
	var start= new Date(); var now = null; do now = new Date(); while(now-start < 100);
}

</script>	
</body>
</html>