View difference between Paste ID: EL2R6J5b and
SHOW: | | - or go back to the newest paste.
1-
1+
// Copyright 2012 David J Smith, Nanch LLC
2
// This code is available to the general public for use in personal or commercial products free of charge with no implied or explicit liability
3
4
// This code was used in [ EZ Drop ], a fast and easy way to get files to your droid
5
// https://market.android.com/details?id=co.dropper.ez.android.free
6
7
package co.dropper.ez.android.free;
8
9
import java.util.Queue;
10
import java.util.concurrent.ConcurrentLinkedQueue;
11
12
public class LowPassBitrateEstimator {
13
14
	Queue<BitrateRecord> arrivals = new ConcurrentLinkedQueue<BitrateRecord>();
15
	
16
	long baseTime = System.currentTimeMillis();
17
	long millisecondInterval = 250;
18
	private double multiplier =  1000 / millisecondInterval;
19
	private double alpha = .96;
20
	long lastEstimatedTime = System.currentTimeMillis() - baseTime;
21
	double currentAverage = 0;
22
	
23
	public LowPassBitrateEstimator() {
24
	}
25
26
	public double estimate() {
27
		long now = System.currentTimeMillis() - baseTime;
28
29
		if (now - lastEstimatedTime < millisecondInterval) {
30
			return currentAverage * multiplier;
31
		}
32
		
33
		if (arrivals.size() == 0) {
34
			if (now > millisecondInterval) {
35
				for (int i = 0; i < (now - lastEstimatedTime)
36
						/ millisecondInterval; i++) {
37
					currentAverage = alpha * currentAverage + (1 - alpha) * 0;
38
					lastEstimatedTime += millisecondInterval;
39
				}
40
			}
41
		} else {
42
			
43
			
44
			long totalBytesReceived = 0;
45
			BitrateRecord br = new BitrateRecord();
46
			br = arrivals.peek();
47
48
			while (br.t < now - millisecondInterval) {
49
				while (br.t < lastEstimatedTime + millisecondInterval) {
50
					totalBytesReceived += br.v;
51
					arrivals.remove();
52
					if (arrivals.size() == 0) {
53
						break;
54
					}
55
					br = arrivals.peek();
56
				}
57
58
				currentAverage = alpha * currentAverage + (1 - alpha)
59
						* totalBytesReceived;
60
				lastEstimatedTime += millisecondInterval;
61
				totalBytesReceived = 0;
62
63
				if (arrivals.size() == 0) {
64
					break;
65
				}
66
			}
67
		}
68
		return currentAverage * multiplier;
69
	}
70
	
71
	public void push(int val){
72
		
73
		BitrateRecord br = new BitrateRecord();
74
		br.t = System.currentTimeMillis() - baseTime;
75
		br.v = val;
76
		arrivals.add(br);
77
	}
78
}
79
80
81
82
----------------------------- BEGIN BitrateRecord.java -----------------------------
83
84
package co.dropper.ez.android.free;
85
public class BitrateRecord {
86
	public long t;
87
	public long v;
88
}