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 #define LOG_TAG "VsockCameraServer"
17 #include "vsock_camera_server.h"
18 #include <log/log.h>
19 
20 namespace android::hardware::camera::provider::V2_7::implementation {
21 
22 using ::android::hardware::camera::device::V3_4::implementation::
23     VsockCameraDevice;
24 namespace {
25 
containsValidSettings(const VsockCameraDevice::Settings & settings)26 bool containsValidSettings(const VsockCameraDevice::Settings& settings) {
27   return settings.width > 0 && settings.height > 0 && settings.frame_rate > 0.0;
28 }
29 
readSettingsFromJson(VsockCameraDevice::Settings & settings,const Json::Value & json)30 bool readSettingsFromJson(VsockCameraDevice::Settings& settings,
31                           const Json::Value& json) {
32   VsockCameraDevice::Settings new_settings;
33   new_settings.width = json["width"].asInt();
34   new_settings.height = json["height"].asInt();
35   new_settings.frame_rate = json["frame_rate"].asDouble();
36   if (containsValidSettings(new_settings)) {
37     settings = new_settings;
38     return true;
39   } else {
40     return false;
41   }
42 }
43 
44 }  // namespace
45 
VsockCameraServer()46 VsockCameraServer::VsockCameraServer() {
47   ALOGI("%s: Create server", __FUNCTION__);
48   connection_ = std::make_shared<cuttlefish::VsockServerConnection>();
49 }
50 
~VsockCameraServer()51 VsockCameraServer::~VsockCameraServer() {
52   ALOGI("%s: Destroy server", __FUNCTION__);
53   stop();
54 }
55 
start(unsigned int port,unsigned int cid)56 void VsockCameraServer::start(unsigned int port, unsigned int cid) {
57   stop();
58   is_running_ = true;
59   server_thread_ = std::thread([this, port, cid] { serverLoop(port, cid); });
60 }
61 
stop()62 void VsockCameraServer::stop() {
63   connection_->ServerShutdown();
64   is_running_ = false;
65   if (server_thread_.joinable()) {
66     server_thread_.join();
67   }
68 }
69 
setConnectedCallback(callback_t callback)70 void VsockCameraServer::setConnectedCallback(callback_t callback) {
71   connected_callback_ = callback;
72   std::lock_guard<std::mutex> lock(settings_mutex_);
73   if (callback && connection_->IsConnected() &&
74       containsValidSettings(settings_)) {
75     callback(connection_, settings_);
76   }
77 }
78 
serverLoop(unsigned int port,unsigned int cid)79 void VsockCameraServer::serverLoop(unsigned int port, unsigned int cid) {
80   while (is_running_.load()) {
81     ALOGI("%s: Accepting connections...", __FUNCTION__);
82     if (connection_->Connect(
83             port, cid,
84             std::nullopt /* vhost_user_vsock: because it's guest */)) {
85       auto json_settings = connection_->ReadJsonMessage();
86       VsockCameraDevice::Settings settings;
87       if (readSettingsFromJson(settings, json_settings)) {
88         std::lock_guard<std::mutex> lock(settings_mutex_);
89         settings_ = settings;
90         if (connected_callback_) {
91           connected_callback_(connection_, settings);
92         }
93         ALOGI("%s: Client connected", __FUNCTION__);
94       } else {
95         ALOGE("%s: Could not read settings", __FUNCTION__);
96       }
97     } else {
98       ALOGE("%s: Accepting connections failed", __FUNCTION__);
99     }
100   }
101   ALOGI("%s: Exiting", __FUNCTION__);
102 }
103 
104 }  // namespace android::hardware::camera::provider::V2_7::implementation
105