1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_BROWSER_API_WEBCAM_PRIVATE_VISCA_WEBCAM_H_
6 #define EXTENSIONS_BROWSER_API_WEBCAM_PRIVATE_VISCA_WEBCAM_H_
10 #include "base/callback.h"
11 #include "base/memory/weak_ptr.h"
12 #include "extensions/browser/api/serial/serial_connection.h"
13 #include "extensions/browser/api/webcam_private/webcam.h"
14 #include "extensions/common/api/serial.h"
16 namespace extensions
{
18 class ViscaWebcam
: public Webcam
{
20 ViscaWebcam(const std::string
& path
, const std::string
& extension_id
);
22 using OpenCompleteCallback
= base::Callback
<void(bool)>;
24 // Open and initialize the web camera. This is done by the following three
25 // steps (in order): 1. Open the serial port; 2. Request address; 3. Clear
26 // the command buffer. After these three steps completes, |open_callback| will
28 void Open(const OpenCompleteCallback
& open_callback
);
31 ~ViscaWebcam() override
;
39 using CommandCompleteCallback
=
40 base::Callback
<void(bool, const std::vector
<char>&)>;
42 void OpenOnIOThread(const OpenCompleteCallback
& open_callback
);
43 // Callback function that will be called after the serial connection has been
44 // opened successfully.
45 void OnConnected(const OpenCompleteCallback
& open_callback
, bool success
);
46 // Callback function that will be called after the address has been set
48 void OnAddressSetCompleted(const OpenCompleteCallback
& open_callback
,
50 const std::vector
<char>& response
);
51 // Callback function that will be called after the command buffer has been
52 // cleared successfully.
53 void OnClearAllCompleted(const OpenCompleteCallback
& open_callback
,
55 const std::vector
<char>& response
);
57 // Send or queue a command and wait for the camera's response.
58 void Send(const std::vector
<char>& command
,
59 const CommandCompleteCallback
& callback
);
60 void OnSendCompleted(const CommandCompleteCallback
& callback
,
62 api::serial::SendError error
);
63 void ReceiveLoop(const CommandCompleteCallback
& callback
);
64 void OnReceiveCompleted(const CommandCompleteCallback
& callback
,
65 const std::vector
<char>& data
,
66 api::serial::ReceiveError error
);
68 // Callback function that will be called after the send and reply of a command
69 // are both completed.
70 void OnCommandCompleted(const SetPTZCompleteCallback
& callback
,
72 const std::vector
<char>& response
);
73 // Callback function that will be called after the send and reply of an
74 // inquiry are both completed.
75 void OnInquiryCompleted(InquiryType type
,
76 const GetPTZCompleteCallback
& callback
,
78 const std::vector
<char>& response
);
81 void GetPan(const GetPTZCompleteCallback
& callback
) override
;
82 void GetTilt(const GetPTZCompleteCallback
& callback
) override
;
83 void GetZoom(const GetPTZCompleteCallback
& callback
) override
;
84 void SetPan(int value
,
86 const SetPTZCompleteCallback
& callback
) override
;
87 void SetTilt(int value
,
89 const SetPTZCompleteCallback
& callback
) override
;
90 void SetZoom(int value
, const SetPTZCompleteCallback
& callback
) override
;
91 void SetPanDirection(PanDirection direction
,
93 const SetPTZCompleteCallback
& callback
) override
;
94 void SetTiltDirection(TiltDirection direction
,
96 const SetPTZCompleteCallback
& callback
) override
;
100 const SetPTZCompleteCallback
& callback
) override
;
102 const std::string path_
;
103 const std::string extension_id_
;
105 scoped_ptr
<SerialConnection
> serial_connection_
;
107 // Stores the response for the current command.
108 std::vector
<char> data_buffer_
;
109 // Queues commands till the current command completes.
110 std::deque
<std::pair
<std::vector
<char>, CommandCompleteCallback
>> commands_
;
112 // Visca webcam always get/set pan-tilt together. |pan| and |tilt| are used to
113 // store the current value of pan and tilt positions.
117 base::WeakPtrFactory
<ViscaWebcam
> weak_ptr_factory_
;
119 DISALLOW_COPY_AND_ASSIGN(ViscaWebcam
);
122 } // namespace extensions
124 #endif // EXTENSIONS_BROWSER_API_WEBCAM_PRIVATE_VISCA_WEBCAM_H_