-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcamera.js
191 lines (166 loc) · 4.33 KB
/
camera.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Camera represents a single IP camera.
*
* Maintains the camera location, access times and statistics, current
* frame, and so forth. Primarily acts as a dictionary for camera state
* information. The controller actually performs core functionality.
*
* Camera:
* setPause()
* togglePause()
* url.setLocal()
* url.setRemote()
* url.getImageUrl()
*/
function Camera(json)
{
// Id, assigned externally.
var id = -1;
// Object refs (TODO: Shouldn't need to talk to controller!)
// XXX: Queue uses this to call UI update on image load.
var controller;
// CameraUrl object. Supports source URL manipulation to support
// multiple hosts and camera image quality features.
this.url = null;
// RequestQueue object. Responsible for loading images, managing
// callbacks, etc.
this.queue = null;
// Camera model, vendor, location labels.
this.model = "";
this.vendor = "";
this.location = "";
// Load time statistics
this.times = {
firstRequested: null,
firstLoaded: null,
lastRequested: null,
lastLoaded: null,
lastLoaded_requestTime: null, // Request time of last loaded frame
lastFailed: null,
lastAborted: null
};
// Load counts
this.counts = {
request: 0,
load: 0,
fail: 0,
abort: 0,
cachePurge: 0 // Purged from queue before loaded
};
// Pause, throttle, etc.
this.isPaused = false;
this.throttle = 0; // Throttle to apply (in ms). Dynamically updated.
// Current frame. Updated by RequestQueue.
this.curFrame = new Image();
///////// CONSTRUCT ////////
this.queue = new RequestQueue();
this.queue.camera = this;
if(json) {
this.url = new CameraUrl(json);
this.location = json['location'];
this.vendor = json['vendor'];
this.model = json['type'];
};
/**
* Set the pause state.
* Without argument, treated as setPause(true).
*/
this.setPause = function(pause)
{
if(typeof pause == 'undefined') {
this.isPaused = true;
}
else {
this.isPaused = Boolean(pause);
}
};
/**
* Toggle pause state.
*/
this.togglePause = function()
{
this.isPaused = !this.isPaused;
}
};
/**
* Camera image/video URL manipulation.
*
* Automatically adds the required browser cache breaking timestamp
* to each URL. This ultimately makes the loader work.
*
* TODO: Add ability to support camera features, such as quality and
* size parameters. Not all cameras have these, but it would be nice
* for those that do.
*/
function CameraUrl(json)
{
// IP Camera Location -- FULL URL
// Used when there is no camera configurability and no choice in server
this.fullUrl = null;
// IP Camera Location -- BROKEN IN COMPONENTS
// Used when we can switch camera options or use an alternate server
this.remoteHost = ""; // Over Internet
this.localHost = ""; // Local network (if available)
this.currentHost = ""; // The host we're using now.
this.imagePath = "";
this.videoPath = "";
this.params = null; // TODO: Support for camera quality, etc. params.
if(json) {
if("full-url" in json) {
this.fullUrl = json['full-url'];
}
else {
this.remoteHost = json['remote-host'];
this.localHost = json['local-host'];
this.imagePath = json['image'];
this.videoPath = json['video'];
this.params = json['image-params'];
}
// Default to using remote host if both are specified.
this.currentHost = this.remoteHost ? this.remoteHost : this.localHost;
}
/**
* Request a new timestamped URL.
* A timestamp is appended that breaks the browser cache.
*/
this.getImageUrl = function()
{
var timestamp;
var url;
if(this.fullUrl) {
url = this.fullUrl;
}
else {
url = "http://" + this.currentHost + this.imagePath;
}
// Build query string
timestamp = new Date().getTime().toString();
// FIXME: Ensure proper query string is built.
if(url.search("/\\?/") == -1) {
url += "?t=" + timestamp;
}
else {
url += "&t=" + timestamp;
}
// FIXME: proper quality, size parameter handling.
// This is just to support Linksys cameras. Won't work elsewhere.
if(this.params) {
url += "&size=2&quality=3";
}
return url;
}
/**
* Set source server as local if exists.
*/
this.setLocal = function()
{
this.currentHost = this.localHost ? this.localHost : this.remoteHost;
}
/**
* Set source server as remote if exists.
*/
this.setRemote = function()
{
this.currentHost = this.remoteHost ? this.remoteHost : this.localHost;
}
};