Permalink
Browse files

test adding 0.7 bwcheck app

  • Loading branch information...
1 parent af90b0d commit af2256c418ae00dc035af97a032db2701ae2283f @ultrasaurus committed Jun 26, 2009
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration>
+ <appender name="BWFILE"
+ class="ch.qos.logback.core.rolling.RollingFileAppender">
+ <File>logs/bwcheck.log</File>
+ <rollingPolicy
+ class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+ <FileNamePattern>logs/bwcheck.%d{yyyy-MM-dd}.log.gz</FileNamePattern>
+ </rollingPolicy>
+ <level value="INFO"/>
+ <layout class="ch.qos.logback.classic.PatternLayout">
+ <Pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</Pattern>
+ </layout>
+ </appender>
+
+ <logger name="org.red5.demos.bwcheck.BandwidthDetection">
+ <level value="INFO"/>
+ <appender-ref ref="BWFILE" />
+ </logger>
+
+</configuration>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
+webapp.contextPath=/bwcheck
+webapp.virtualHosts=*, localhost, localhost:8088, 127.0.0.1:8088
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
+<beans>
+
+ <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
+ <property name="location" value="/WEB-INF/red5-web.properties" />
+ </bean>
+
+ <bean id="web.context" class="org.red5.server.Context"
+ autowire="byType" />
+
+ <bean id="web.scope" class="org.red5.server.WebScope"
+ init-method="register">
+ <property name="server" ref="red5.server" />
+ <property name="parent" ref="global.scope" />
+ <property name="context" ref="web.context" />
+ <property name="handler" ref="bwcheck.handler" />
+ <property name="contextPath" value="${webapp.contextPath}" />
+ <property name="virtualHosts" value="${webapp.virtualHosts}" />
+ </bean>
+
+ <bean id="bwcheck.handler"
+ class="org.red5.demos.bwcheck.BWApplication"
+ singleton="true" />
+
+</beans>
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration>
+ <appender name="BWFILE"
+ class="ch.qos.logback.core.rolling.RollingFileAppender">
+ <File>logs/bwcheck.log</File>
+ <rollingPolicy
+ class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+ <FileNamePattern>logs/bwcheck.%d{yyyy-MM-dd}.log.gz</FileNamePattern>
+ </rollingPolicy>
+ <level value="INFO"/>
+ <layout class="ch.qos.logback.classic.PatternLayout">
+ <Pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</Pattern>
+ </layout>
+ </appender>
+
+ <logger name="org.red5.demos.bwcheck.BandwidthDetection">
+ <level value="INFO"/>
+ <appender-ref ref="BWFILE" />
+ </logger>
+
+</configuration>
@@ -0,0 +1,37 @@
+package org.red5.demos.bwcheck;
+
+import org.red5.demos.bwcheck.BandwidthDetection;
+
+import org.red5.server.adapter.MultiThreadedApplicationAdapter;
+import org.red5.server.api.IConnection;
+import org.red5.server.api.IScope;
+
+import org.red5.demos.bwcheck.security.PlaybackSecurity;
+import org.red5.demos.bwcheck.security.PublishSecurity;
+import org.red5.demos.bwcheck.security.SharedObjectSecurity;
+
+/**
+ * Bandwidth Detection demo for the Red5 Server.
+ *
+ * @author The Red5 Project (red5@osflash.org)
+ * @author Dan Rossi
+ */
+public class BWApplication extends MultiThreadedApplicationAdapter
+{
+ @Override
+ public boolean appStart(IScope app) {
+ registerStreamPublishSecurity(new PublishSecurity());
+ registerSharedObjectSecurity(new SharedObjectSecurity());
+ registerStreamPlaybackSecurity(new PlaybackSecurity());
+
+ return super.appStart(app);
+ }
+
+ @Override
+ public boolean appConnect(IConnection conn, Object[] params) {
+
+ BandwidthDetection detect = new BandwidthDetection();
+ detect.checkBandwidth(conn);
+ return super.appConnect(conn, params);
+ }
+}
@@ -0,0 +1,215 @@
+package org.red5.demos.bwcheck;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.red5.server.api.IConnection;
+import org.red5.server.api.Red5;
+import org.red5.server.api.service.IPendingServiceCall;
+import org.red5.server.api.service.IPendingServiceCallback;
+import org.red5.server.api.service.IServiceCapableConnection;
+import org.red5.server.api.stream.IStreamCapableConnection;
+
+import java.util.*;
+
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ *
+ * @author The Red5 Project (red5@osflash.org)
+ * @author Dan Rossi
+ */
+public class BandwidthDetection implements IPendingServiceCallback, IBandwidthDetection {
+
+ IConnection client = null;
+ double latency = 0;
+ double cumLatency = 1;
+ int count = 0;
+ int sent = 0;
+ double kbitDown = 0;
+ double deltaDown = 0;
+ double deltaTime = 0;
+
+ List<Long> pakSent = new ArrayList<Long>();
+ List<Long> pakRecv = new ArrayList<Long>();
+
+ private Map<String, Long> beginningValues;
+ private double[] payload = new double[1200];
+ private double[] payload_1 = new double[12000];
+ private double[] payload_2 = new double[12000];
+
+ protected static Logger log = LoggerFactory.getLogger(BandwidthDetection.class.getName());
+
+ public BandwidthDetection()
+ {
+
+ }
+
+ public void checkBandwidth(IConnection p_client)
+ {
+ this.calculateClientBw(p_client);
+ }
+
+ public void calculateClientBw(IConnection p_client)
+ {
+
+
+ for (int i=0; i<1200; i++){
+ payload[i] = Math.random();
+ }
+
+ p_client.setAttribute("payload", payload);
+
+ for (int i=0; i<12000; i++){
+ payload_1[i] = Math.random();
+ }
+
+ p_client.setAttribute("payload_1", payload_1);
+
+ for (int i=0; i<12000; i++){
+ payload_2[i] = Math.random();
+ }
+
+ p_client.setAttribute("payload_2", payload_2);
+
+ final IStreamCapableConnection beginningStats = this.getStats();
+ final Long start = new Long(System.nanoTime()/1000000); //new Long(System.currentTimeMillis());
+
+ this.client = p_client;
+ beginningValues = new HashMap<String, Long>();
+ beginningValues.put("b_down", beginningStats.getWrittenBytes());
+ beginningValues.put("b_up", beginningStats.getReadBytes());
+ beginningValues.put("time", start);
+
+ this.pakSent.add(start);
+ this.sent++;
+ log.info("Starting bandwidth check at " + start);
+ this.callBWCheck("");
+
+ }
+
+ /**
+ * Handle callback from service call.
+ */
+
+ public void resultReceived(IPendingServiceCall call) {
+ Long now = new Long(System.nanoTime()/1000000); //new Long(System.currentTimeMillis());
+ this.pakRecv.add(now);
+ Long timePassed = (now - this.beginningValues.get("time"));
+
+ this.count++;
+
+ log.info("count: "+count+ " sent: "+sent+" timePassed: "+timePassed+" latency: "+latency);
+
+ if (count == 1) {
+ latency = Math.min(timePassed, 800);
+ latency = Math.max(latency, 10);
+
+
+ // We now have a latency figure so can start sending test data.
+ // Second call. 1st packet sent
+ pakSent.add(now);
+ sent++;
+
+ log.info("Sending First Payload at " + now);
+ this.callBWCheck(this.client.getAttribute("payload"));
+
+
+ }
+
+
+ //To run a very quick test, uncomment the following if statement and comment out the next 3 if statements.
+
+ /*
+ else if (count == 2 && (timePassed < 2000)) {
+ pakSent.add(now);
+ sent++;
+ cumLatency++;
+ this.callBWCheck(this.client.getAttribute("payload"));
+ }
+ */
+
+
+ //The following will progressivly increase the size of the packets been sent until 1 second has elapsed.
+ else if ((count > 1 && count < 3) && (timePassed < 1000)) {
+ pakSent.add(now);
+ sent++;
+ cumLatency++;
+ log.info("Sending Second Payload at " + now);
+ this.callBWCheck(this.client.getAttribute("payload_1"));
+ }
+ else if ((count >=3 && count < 6) && (timePassed < 1000)) {
+ pakSent.add(now);
+ sent++;
+ cumLatency++;
+ log.info("Sending Third Payload at " + now);
+ this.callBWCheck(this.client.getAttribute("payload_1"));
+ }
+
+ else if (count >= 6 && (timePassed < 1000)) {
+ pakSent.add(now);
+ sent++;
+ cumLatency++;
+ log.info("Sending Fourth Payload at " + now);
+ this.callBWCheck(this.client.getAttribute("payload_2"));
+ }
+
+ //Time elapsed now do the calcs
+ else if (sent == count) {
+ // see if we need to normalize latency
+ if (latency >= 100) {
+ //make sure satelite and modem is detected properly
+ if (pakRecv.get(1) - pakRecv.get(0) > 1000) {
+ latency = 100;
+ }
+ }
+
+ this.client.removeAttribute("payload");
+ this.client.removeAttribute("payload_1");
+ this.client.removeAttribute("payload_2");
+
+ final IStreamCapableConnection endStats = this.getStats();
+ deltaDown = (endStats.getWrittenBytes() - beginningValues.get("b_down")) * 8 / 1000; // bytes to kbits
+ deltaTime = ((now - beginningValues.get("time")) - (latency * cumLatency)) / 1000; // total dl time - latency for each packet sent in secs
+ if (Math.round(deltaTime) <= 0) {
+ deltaTime = (now - beginningValues.get("time") + latency) / 1000;
+ }
+ kbitDown = Math.round(deltaDown / deltaTime); // kbits / sec
+
+ if (kbitDown < 100) kbitDown = 100;
+
+ log.info("onBWDone: kbitDown = " + kbitDown + ", deltaDown= " + deltaDown + ", deltaTime = " + deltaTime + ", latency = " + this.latency);
+
+ this.callBWDone(this.kbitDown, this.deltaDown, this.deltaTime, this.latency);
+ }
+
+ }
+
+ private void callBWCheck(Object params)
+ {
+ IConnection conn = Red5.getConnectionLocal();
+
+ if (conn instanceof IServiceCapableConnection) {
+ ((IServiceCapableConnection) conn).invoke("onBWCheck", new Object[]{params}, this);
+ }
+ }
+
+ private void callBWDone(double kbitDown, double deltaDown, double deltaTime, double latency)
+ {
+ IConnection conn = Red5.getConnectionLocal();
+
+ if (conn instanceof IServiceCapableConnection) {
+ ((IServiceCapableConnection) conn).invoke("onBWDone", new Object[]{kbitDown, deltaDown, deltaTime, latency});
+ }
+ }
+
+ private IStreamCapableConnection getStats()
+ {
+ IConnection conn = Red5.getConnectionLocal();
+ if (conn instanceof IStreamCapableConnection) {
+ return (IStreamCapableConnection) conn;
+ }
+ return null;
+ }
+}
@@ -0,0 +1,13 @@
+package org.red5.demos.bwcheck;
+
+import org.red5.server.api.IConnection;
+
+/**
+ *
+ * @author The Red5 Project (red5@osflash.org)
+ * @author Dan Rossi
+ */
+public interface IBandwidthDetection {
+ public void checkBandwidth(IConnection p_client);
+ public void calculateClientBw(IConnection p_client);
+}
@@ -0,0 +1,18 @@
+package org.red5.demos.bwcheck.security;
+
+import org.red5.server.api.IScope;
+import org.red5.server.api.stream.IStreamPlaybackSecurity;
+
+/**
+ *
+ * @author The Red5 Project (red5@osflash.org)
+ * @author Dan Rossi
+ */
+public class PlaybackSecurity implements IStreamPlaybackSecurity {
+
+ public boolean isPlaybackAllowed(IScope scope, String name, int start, int length, boolean flushPlaylist) {
+ return false;
+ }
+
+}
+
@@ -0,0 +1,17 @@
+package org.red5.demos.bwcheck.security;
+
+import org.red5.server.api.IScope;
+import org.red5.server.api.stream.IStreamPublishSecurity;
+
+/**
+ *
+ * @author The Red5 Project (red5@osflash.org)
+ * @author Dan Rossi
+ */
+public class PublishSecurity implements IStreamPublishSecurity {
+
+ public boolean isPublishAllowed(IScope scope, String name, String mode) {
+ return false;
+ }
+
+}
Oops, something went wrong.

0 comments on commit af2256c

Please sign in to comment.