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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/api/webcam_private/visca_webcam.h"
#include <stddef.h>
#include <stdint.h>
#include <utility>
#include "base/bind.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace {
// Message terminator:
const char kViscaTerminator = 0xFF;
// Response types:
const char kViscaResponseNetworkChange = 0x38;
const char kViscaResponseAck = 0x40;
const char kViscaResponseError = 0x60;
// The default pan speed is kMaxPanSpeed /2 and the default tilt speed is
// kMaxTiltSpeed / 2.
const int kMaxPanSpeed = 0x18;
const int kMaxTiltSpeed = 0x14;
const int kDefaultPanSpeed = 0x18 / 2;
const int kDefaultTiltSpeed = 0x14 / 2;
// Pan-Tilt-Zoom movement comands from http://www.manualslib.com/manual/...
// 557364/Cisco-Precisionhd-1080p12x.html?page=31#manual
// Reset the address of each device in the VISCA chain (broadcast). This is used
// when resetting the VISCA network.
const char kSetAddressCommand[] = {0x88, 0x30, 0x01, 0xFF};
// Clear all of the devices, halting any pending commands in the VISCA chain
// (broadcast). This is used when resetting the VISCA network.
const char kClearAllCommand[] = {0x88, 0x01, 0x00, 0x01, 0xFF};
// Command: {0x8X, 0x09, 0x06, 0x12, 0xFF}, X = 1 to 7: target device address.
// Response: {0xY0, 0x50, 0x0p, 0x0q, 0x0r, 0x0s, 0x0t, 0x0u, 0x0v, 0x0w, 0xFF},
// Y = socket number; pqrs: pan position; tuvw: tilt position.
const char kGetPanTiltCommand[] = {0x81, 0x09, 0x06, 0x12, 0xFF};
// Command: {0x8X, 0x01, 0x06, 0x02, 0x0p, 0x0t, 0x0q, 0x0r, 0x0s, 0x0u, 0x0v,
// 0x0w, 0x0y, 0x0z, 0xFF}, X = 1 to 7: target device address; p = pan speed;
// t = tilt speed; qrsu = pan position; vwyz = tilt position.
const char kSetPanTiltCommand[] = {0x81, 0x01, 0x06, 0x02, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xFF};
// Command: {0x8X, 0x01, 0x06, 0x05, 0xFF}, X = 1 to 7: target device address.
const char kResetPanTiltCommand[] = {0x81, 0x01, 0x06, 0x05, 0xFF};
// Command: {0x8X, 0x09, 0x04, 0x47, 0xFF}, X = 1 to 7: target device address.
// Response: {0xY0, 0x50, 0x0p, 0x0q, 0x0r, 0x0s, 0xFF}, Y = socket number;
// pqrs: zoom position.
const char kGetZoomCommand[] = {0x81, 0x09, 0x04, 0x47, 0xFF};
// Command: {0x8X, 0x01, 0x04, 0x47, 0x0p, 0x0q, 0x0r, 0x0s, 0xFF}, X = 1 to 7:
// target device address; pqrs: zoom position;
const char kSetZoomCommand[] = {0x81, 0x01, 0x04, 0x47, 0x00,
0x00, 0x00, 0x00, 0xFF};
// Command: {0x8X, 0x01, 0x06, 0x01, 0x0p, 0x0t, 0x03, 0x01, 0xFF}, X = 1 to 7:
// target device address; p: pan speed; t: tilt speed.
const char kPTUpCommand[] = {0x81, 0x01, 0x06, 0x01, 0x00,
0x00, 0x03, 0x01, 0xFF};
// Command: {0x8X, 0x01, 0x06, 0x01, 0x0p, 0x0t, 0x03, 0x02, 0xFF}, X = 1 to 7:
// target device address; p: pan speed; t: tilt speed.
const char kPTDownCommand[] = {0x81, 0x01, 0x06, 0x01, 0x00,
0x00, 0x03, 0x02, 0xFF};
// Command: {0x8X, 0x01, 0x06, 0x01, 0x0p, 0x0t, 0x0, 0x03, 0xFF}, X = 1 to 7:
// target device address; p: pan speed; t: tilt speed.
const char kPTLeftCommand[] = {0x81, 0x01, 0x06, 0x01, 0x00,
0x00, 0x01, 0x03, 0xFF};
// Command: {0x8X, 0x01, 0x06, 0x01, 0x0p, 0x0t, 0x02, 0x03, 0xFF}, X = 1 to 7:
// target device address; p: pan speed; t: tilt speed.
const char kPTRightCommand[] = {0x81, 0x01, 0x06, 0x01, 0x00,
0x00, 0x02, 0x03, 0xFF};
// Command: {0x8X, 0x01, 0x06, 0x01, 0x03, 0x03, 0x03, 0x03, 0xFF}, X = 1 to 7:
// target device address.
const char kPTStopCommand[] = {0x81, 0x01, 0x06, 0x01, 0x03,
0x03, 0x03, 0x03, 0xFF};
#define CHAR_VECTOR_FROM_ARRAY(array) \
std::vector<char>(array, array + arraysize(array))
int ShiftResponseLowerBits(char c, size_t shift) {
return static_cast<int>(c & 0x0F) << shift;
}
bool CanBuildResponseInt(const std::vector<char>& response,
size_t start_index) {
return response.size() >= start_index + 4;
}
int BuildResponseInt(const std::vector<char>& response, size_t start_index) {
return ShiftResponseLowerBits(response[start_index], 12) +
ShiftResponseLowerBits(response[start_index + 1], 8) +
ShiftResponseLowerBits(response[start_index + 2], 4) +
ShiftResponseLowerBits(response[start_index + 3], 0);
}
void ResponseToCommand(std::vector<char>* command,
size_t start_index,
uint16_t response) {
DCHECK(command);
std::vector<char>& command_ref = *command;
command_ref[start_index] |= ((response >> 12) & 0x0F);
command_ref[start_index + 1] |= ((response >> 8) & 0x0F);
command_ref[start_index + 2] |= ((response >> 4 & 0x0F));
command_ref[start_index + 3] |= (response & 0x0F);
}
int CalculateSpeed(int desired_speed, int max_speed, int default_speed) {
int speed = std::min(desired_speed, max_speed);
return speed > 0 ? speed : default_speed;
}
int GetPositiveValue(int value) {
return value < 0x8000 ? value : value - 0xFFFF;
}
} // namespace
namespace extensions {
ViscaWebcam::ViscaWebcam() : pan_(0), tilt_(0), weak_ptr_factory_(this) {}
ViscaWebcam::~ViscaWebcam() {
}
void ViscaWebcam::Open(const std::string& path,
const std::string& extension_id,
const OpenCompleteCallback& open_callback) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&ViscaWebcam::OpenOnIOThread, weak_ptr_factory_.GetWeakPtr(),
path, extension_id, open_callback));
}
void ViscaWebcam::OpenOnIOThread(const std::string& path,
const std::string& extension_id,
const OpenCompleteCallback& open_callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
api::serial::ConnectionOptions options;
// Set the receive buffer size to receive the response data 1 by 1.
options.buffer_size.reset(new int(1));
options.persistent.reset(new bool(false));
options.bitrate.reset(new int(9600));
options.cts_flow_control.reset(new bool(false));
// Enable send and receive timeout error.
options.receive_timeout.reset(new int(3000));
options.send_timeout.reset(new int(3000));
options.data_bits = api::serial::DATA_BITS_EIGHT;
options.parity_bit = api::serial::PARITY_BIT_NO;
options.stop_bits = api::serial::STOP_BITS_ONE;
serial_connection_.reset(new SerialConnection(path, extension_id));
serial_connection_->Open(
options, base::Bind(&ViscaWebcam::OnConnected,
weak_ptr_factory_.GetWeakPtr(), open_callback));
}
void ViscaWebcam::OnConnected(const OpenCompleteCallback& open_callback,
bool success) {
if (!success) {
PostOpenFailureTask(open_callback);
return;
}
Send(CHAR_VECTOR_FROM_ARRAY(kSetAddressCommand),
base::Bind(&ViscaWebcam::OnAddressSetCompleted,
weak_ptr_factory_.GetWeakPtr(), open_callback));
}
void ViscaWebcam::OnAddressSetCompleted(
const OpenCompleteCallback& open_callback,
bool success,
const std::vector<char>& response) {
commands_.pop_front();
if (!success) {
PostOpenFailureTask(open_callback);
return;
}
Send(CHAR_VECTOR_FROM_ARRAY(kClearAllCommand),
base::Bind(&ViscaWebcam::OnClearAllCompleted,
weak_ptr_factory_.GetWeakPtr(), open_callback));
}
void ViscaWebcam::OnClearAllCompleted(const OpenCompleteCallback& open_callback,
bool success,
const std::vector<char>& response) {
commands_.pop_front();
if (!success) {
PostOpenFailureTask(open_callback);
return;
}
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(open_callback, true));
}
void ViscaWebcam::Send(const std::vector<char>& command,
const CommandCompleteCallback& callback) {
commands_.push_back(std::make_pair(command, callback));
// If this is the only command in the queue, send it now.
if (commands_.size() == 1) {
serial_connection_->Send(
command, base::Bind(&ViscaWebcam::OnSendCompleted,
weak_ptr_factory_.GetWeakPtr(), callback));
}
}
void ViscaWebcam::OnSendCompleted(const CommandCompleteCallback& callback,
int bytes_sent,
api::serial::SendError error) {
// TODO(xdai): Check |bytes_sent|?
if (error == api::serial::SEND_ERROR_NONE) {
ReceiveLoop(callback);
} else {
callback.Run(false, std::vector<char>());
}
}
void ViscaWebcam::ReceiveLoop(const CommandCompleteCallback& callback) {
serial_connection_->Receive(base::Bind(&ViscaWebcam::OnReceiveCompleted,
weak_ptr_factory_.GetWeakPtr(),
callback));
}
void ViscaWebcam::OnReceiveCompleted(const CommandCompleteCallback& callback,
const std::vector<char>& data,
api::serial::ReceiveError error) {
data_buffer_.insert(data_buffer_.end(), data.begin(), data.end());
if (error != api::serial::RECEIVE_ERROR_NONE || data_buffer_.empty()) {
// Clear |data_buffer_|.
std::vector<char> response;
response.swap(data_buffer_);
callback.Run(false, response);
return;
}
// Success case. If waiting for more data, then loop until encounter the
// terminator.
if (data_buffer_.back() != kViscaTerminator) {
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(&ViscaWebcam::ReceiveLoop,
weak_ptr_factory_.GetWeakPtr(), callback));
return;
}
// Success case, and a complete response has been received.
// Clear |data_buffer_|.
std::vector<char> response;
response.swap(data_buffer_);
if (response.size() < 2 ||
(static_cast<int>(response[1]) & 0xF0) == kViscaResponseError) {
callback.Run(false, response);
} else if ((static_cast<int>(response[1]) & 0xF0) != kViscaResponseAck &&
(static_cast<int>(response[1]) & 0xFF) !=
kViscaResponseNetworkChange) {
callback.Run(true, response);
} else {
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(&ViscaWebcam::ReceiveLoop,
weak_ptr_factory_.GetWeakPtr(), callback));
}
}
void ViscaWebcam::OnCommandCompleted(const SetPTZCompleteCallback& callback,
bool success,
const std::vector<char>& response) {
// TODO(xdai): Error handling according to |response|.
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(callback, success));
ProcessNextCommand();
}
void ViscaWebcam::OnInquiryCompleted(InquiryType type,
const GetPTZCompleteCallback& callback,
bool success,
const std::vector<char>& response) {
if (!success) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(callback, false, 0));
ProcessNextCommand();
return;
}
bool is_valid_response = false;
switch (type) {
case INQUIRY_PAN:
is_valid_response = CanBuildResponseInt(response, 2);
break;
case INQUIRY_TILT:
is_valid_response = CanBuildResponseInt(response, 6);
break;
case INQUIRY_ZOOM:
is_valid_response = CanBuildResponseInt(response, 2);
break;
}
if (!is_valid_response) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(callback, false, 0));
ProcessNextCommand();
return;
}
int value = 0;
switch (type) {
case INQUIRY_PAN:
// See kGetPanTiltCommand for the format of response.
pan_ = BuildResponseInt(response, 2);
value = GetPositiveValue(pan_);
break;
case INQUIRY_TILT:
// See kGetPanTiltCommand for the format of response.
tilt_ = BuildResponseInt(response, 6);
value = GetPositiveValue(tilt_);
break;
case INQUIRY_ZOOM:
// See kGetZoomCommand for the format of response.
value = BuildResponseInt(response, 2);
break;
}
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(callback, true, value));
ProcessNextCommand();
}
void ViscaWebcam::ProcessNextCommand() {
commands_.pop_front();
if (commands_.empty())
return;
// If there are pending commands, process the next one.
const std::vector<char> next_command = commands_.front().first;
const CommandCompleteCallback next_callback = commands_.front().second;
serial_connection_->Send(
next_command, base::Bind(&ViscaWebcam::OnSendCompleted,
weak_ptr_factory_.GetWeakPtr(), next_callback));
}
void ViscaWebcam::PostOpenFailureTask(
const OpenCompleteCallback& open_callback) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(open_callback, false /* success? */));
}
void ViscaWebcam::GetPan(const GetPTZCompleteCallback& callback) {
Send(CHAR_VECTOR_FROM_ARRAY(kGetPanTiltCommand),
base::Bind(&ViscaWebcam::OnInquiryCompleted,
weak_ptr_factory_.GetWeakPtr(), INQUIRY_PAN, callback));
}
void ViscaWebcam::GetTilt(const GetPTZCompleteCallback& callback) {
Send(CHAR_VECTOR_FROM_ARRAY(kGetPanTiltCommand),
base::Bind(&ViscaWebcam::OnInquiryCompleted,
weak_ptr_factory_.GetWeakPtr(), INQUIRY_TILT, callback));
}
void ViscaWebcam::GetZoom(const GetPTZCompleteCallback& callback) {
Send(CHAR_VECTOR_FROM_ARRAY(kGetZoomCommand),
base::Bind(&ViscaWebcam::OnInquiryCompleted,
weak_ptr_factory_.GetWeakPtr(), INQUIRY_ZOOM, callback));
}
void ViscaWebcam::SetPan(int value,
int pan_speed,
const SetPTZCompleteCallback& callback) {
int actual_pan_speed =
CalculateSpeed(pan_speed, kMaxPanSpeed, kDefaultPanSpeed);
pan_ = value;
std::vector<char> command = CHAR_VECTOR_FROM_ARRAY(kSetPanTiltCommand);
command[4] |= actual_pan_speed;
command[5] |= kDefaultTiltSpeed;
ResponseToCommand(&command, 6, static_cast<uint16_t>(pan_));
ResponseToCommand(&command, 10, static_cast<uint16_t>(tilt_));
Send(command, base::Bind(&ViscaWebcam::OnCommandCompleted,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void ViscaWebcam::SetTilt(int value,
int tilt_speed,
const SetPTZCompleteCallback& callback) {
int actual_tilt_speed =
CalculateSpeed(tilt_speed, kMaxTiltSpeed, kDefaultTiltSpeed);
tilt_ = value;
std::vector<char> command = CHAR_VECTOR_FROM_ARRAY(kSetPanTiltCommand);
command[4] |= kDefaultPanSpeed;
command[5] |= actual_tilt_speed;
ResponseToCommand(&command, 6, static_cast<uint16_t>(pan_));
ResponseToCommand(&command, 10, static_cast<uint16_t>(tilt_));
Send(command, base::Bind(&ViscaWebcam::OnCommandCompleted,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void ViscaWebcam::SetZoom(int value, const SetPTZCompleteCallback& callback) {
int actual_value = std::max(value, 0);
std::vector<char> command = CHAR_VECTOR_FROM_ARRAY(kSetZoomCommand);
ResponseToCommand(&command, 4, actual_value);
Send(command, base::Bind(&ViscaWebcam::OnCommandCompleted,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void ViscaWebcam::SetPanDirection(PanDirection direction,
int pan_speed,
const SetPTZCompleteCallback& callback) {
int actual_pan_speed =
CalculateSpeed(pan_speed, kMaxPanSpeed, kDefaultPanSpeed);
std::vector<char> command = CHAR_VECTOR_FROM_ARRAY(kPTStopCommand);
switch (direction) {
case PAN_STOP:
break;
case PAN_RIGHT:
command = CHAR_VECTOR_FROM_ARRAY(kPTRightCommand);
command[4] |= actual_pan_speed;
command[5] |= kDefaultTiltSpeed;
break;
case PAN_LEFT:
command = CHAR_VECTOR_FROM_ARRAY(kPTLeftCommand);
command[4] |= actual_pan_speed;
command[5] |= kDefaultTiltSpeed;
break;
}
Send(command, base::Bind(&ViscaWebcam::OnCommandCompleted,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void ViscaWebcam::SetTiltDirection(TiltDirection direction,
int tilt_speed,
const SetPTZCompleteCallback& callback) {
int actual_tilt_speed =
CalculateSpeed(tilt_speed, kMaxTiltSpeed, kDefaultTiltSpeed);
std::vector<char> command = CHAR_VECTOR_FROM_ARRAY(kPTStopCommand);
switch (direction) {
case TILT_STOP:
break;
case TILT_UP:
command = CHAR_VECTOR_FROM_ARRAY(kPTUpCommand);
command[4] |= kDefaultPanSpeed;
command[5] |= actual_tilt_speed;
break;
case TILT_DOWN:
command = CHAR_VECTOR_FROM_ARRAY(kPTDownCommand);
command[4] |= kDefaultPanSpeed;
command[5] |= actual_tilt_speed;
break;
}
Send(command, base::Bind(&ViscaWebcam::OnCommandCompleted,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void ViscaWebcam::Reset(bool pan,
bool tilt,
bool zoom,
const SetPTZCompleteCallback& callback) {
// pan and tilt are always reset together in Visca Webcams.
if (pan || tilt) {
Send(CHAR_VECTOR_FROM_ARRAY(kResetPanTiltCommand),
base::Bind(&ViscaWebcam::OnCommandCompleted,
weak_ptr_factory_.GetWeakPtr(), callback));
}
if (zoom) {
// Set the default zoom value to 100 to be consistent with V4l2 webcam.
const int kDefaultZoom = 100;
SetZoom(kDefaultZoom, callback);
}
}
void ViscaWebcam::OpenForTesting(
scoped_ptr<SerialConnection> serial_connection) {
serial_connection_ = std::move(serial_connection);
}
SerialConnection* ViscaWebcam::GetSerialConnectionForTesting() {
return serial_connection_.get();
}
} // namespace extensions
|