1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 #include <atomic>
18 #include <thread>
19 #include "vsock_camera_device_3_4.h"
20 #include "vsock_connection.h"
21 
22 namespace android::hardware::camera::provider::V2_7::implementation {
23 
24 using ::android::hardware::camera::device::V3_4::implementation::
25     VsockCameraDevice;
26 
27 class VsockCameraServer {
28  public:
29   VsockCameraServer();
30   ~VsockCameraServer();
31 
32   VsockCameraServer(const VsockCameraServer&) = delete;
33   VsockCameraServer& operator=(const VsockCameraServer&) = delete;
34 
isRunning()35   bool isRunning() const { return is_running_.load(); }
36 
37   void start(unsigned int port, unsigned int cid);
38   void stop();
39 
40   using callback_t =
41       std::function<void(std::shared_ptr<cuttlefish::VsockConnection>,
42                          VsockCameraDevice::Settings)>;
43   void setConnectedCallback(callback_t callback);
44 
45  private:
46   void serverLoop(unsigned int port, unsigned int cid);
47   std::thread server_thread_;
48   std::atomic<bool> is_running_;
49   std::shared_ptr<cuttlefish::VsockServerConnection> connection_;
50   std::mutex settings_mutex_;
51   VsockCameraDevice::Settings settings_;
52   callback_t connected_callback_;
53 };
54 
55 }  // namespace android::hardware::camera::provider::V2_7::implementation
56