1 /*
2  * Copyright (C) 2020 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 
17 #include <linux/input.h>
18 
19 #include <memory>
20 
21 #include <android-base/logging.h>
22 #include <gflags/gflags.h>
23 #include <libyuv.h>
24 
25 #include "common/libs/fs/shared_fd.h"
26 #include "host/frontend/webrtc/connection_observer.h"
27 #include "host/frontend/webrtc/display_handler.h"
28 #include "host/frontend/webrtc/lib/streamer.h"
29 #include "host/libs/config/cuttlefish_config.h"
30 #include "host/libs/config/logging.h"
31 #include "host/libs/screen_connector/screen_connector.h"
32 
33 DEFINE_int32(touch_fd, -1, "An fd to listen on for touch connections.");
34 DEFINE_int32(keyboard_fd, -1, "An fd to listen on for keyboard connections.");
35 DEFINE_int32(frame_server_fd, -1, "An fd to listen on for frame updates");
36 DEFINE_bool(write_virtio_input, false,
37             "Whether to send input events in virtio format.");
38 
39 using cuttlefish::CfConnectionObserverFactory;
40 using cuttlefish::DisplayHandler;
41 using cuttlefish::webrtc_streaming::Streamer;
42 using cuttlefish::webrtc_streaming::StreamerConfig;
43 
44 class CfOperatorObserver : public cuttlefish::webrtc_streaming::OperatorObserver {
45  public:
46   virtual ~CfOperatorObserver() = default;
OnRegistered()47   virtual void OnRegistered() override {
48     LOG(VERBOSE) << "Registered with Operator";
49   }
OnClose()50   virtual void OnClose() override {
51     LOG(FATAL) << "Connection with Operator unexpectedly closed";
52   }
OnError()53   virtual void OnError() override {
54     LOG(FATAL) << "Error encountered in connection with Operator";
55   }
56 };
57 
main(int argc,char ** argv)58 int main(int argc, char **argv) {
59   cuttlefish::DefaultSubprocessLogging(argv);
60   ::gflags::ParseCommandLineFlags(&argc, &argv, true);
61 
62   auto touch_server = cuttlefish::SharedFD::Dup(FLAGS_touch_fd);
63   auto keyboard_server = cuttlefish::SharedFD::Dup(FLAGS_keyboard_fd);
64   close(FLAGS_touch_fd);
65   close(FLAGS_keyboard_fd);
66   // Accepting on these sockets here means the device won't register with the
67   // operator as soon as it could, but rather wait until crosvm's input display
68   // devices have been initialized. That's OK though, because without those
69   // devices there is no meaningful interaction the user can have with the
70   // device.
71   auto touch_client = cuttlefish::SharedFD::Accept(*touch_server);
72   auto keyboard_client = cuttlefish::SharedFD::Accept(*keyboard_server);
73 
74   auto cvd_config = cuttlefish::CuttlefishConfig::Get();
75   auto screen_connector = cuttlefish::ScreenConnector::Get(FLAGS_frame_server_fd);
76 
77   StreamerConfig streamer_config;
78 
79   streamer_config.device_id =
80       cvd_config->ForDefaultInstance().webrtc_device_id();
81   streamer_config.tcp_port_range = cvd_config->webrtc_tcp_port_range();
82   streamer_config.udp_port_range = cvd_config->webrtc_udp_port_range();
83   streamer_config.operator_server.addr = cvd_config->sig_server_address();
84   streamer_config.operator_server.port = cvd_config->sig_server_port();
85   streamer_config.operator_server.path = cvd_config->sig_server_path();
86   streamer_config.operator_server.security =
87       cvd_config->sig_server_strict()
88           ? WsConnection::Security::kStrict
89           : WsConnection::Security::kAllowSelfSigned;
90 
91   auto observer_factory = std::make_shared<CfConnectionObserverFactory>(
92       touch_client, keyboard_client);
93 
94   auto streamer = Streamer::Create(streamer_config, observer_factory);
95 
96   auto display_0 = streamer->AddDisplay(
97       "display_0", screen_connector->ScreenWidth(),
98       screen_connector->ScreenHeight(), cvd_config->dpi(), true);
99   auto display_handler =
100       std::make_shared<DisplayHandler>(display_0, screen_connector);
101 
102   observer_factory->SetDisplayHandler(display_handler);
103 
104   std::shared_ptr<cuttlefish::webrtc_streaming::OperatorObserver> operator_observer(
105       new CfOperatorObserver());
106   streamer->Register(operator_observer);
107 
108   display_handler->Loop();
109 
110   return 0;
111 }
112