blob: bd16f4cfe948e3f9f342cb0ee6645390979afd05 [file] [log] [blame]
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -08001#include <webrtc/AdbWebSocketHandler.h>
2#include <webrtc/DTLS.h>
3#include <webrtc/MyWebSocketHandler.h>
4#include <webrtc/RTPSocketHandler.h>
5#include <webrtc/ServerState.h>
6#include <webrtc/STUNMessage.h>
7
8#include <https/HTTPServer.h>
9#include <https/PlainSocket.h>
10#include <https/RunLoop.h>
11#include <https/SafeCallbackable.h>
12#include <https/SSLSocket.h>
13#include <https/Support.h>
14#include <media/stagefright/foundation/hexdump.h>
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080015
16#include <iostream>
17#include <unordered_map>
18
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080019#include <gflags/gflags.h>
20
21DEFINE_string(
22 public_ip,
23 "0.0.0.0",
24 "Public IPv4 address of your server, a.b.c.d format");
25DEFINE_string(
26 assets_dir,
27 "webrtc",
28 "Directory with location of webpage assets.");
29DEFINE_string(
30 certs_dir,
31 "webrtc/certs",
32 "Directory to certificates.");
33
34DEFINE_int32(touch_fd, -1, "An fd to listen on for touch connections.");
35DEFINE_int32(keyboard_fd, -1, "An fd to listen on for keyboard connections.");
Jorge E. Moreirae049b792019-12-18 18:17:48 -080036DEFINE_int32(frame_server_fd, -1, "An fd to listen on for frame updates");
37DEFINE_bool(write_virtio_input, false, "Whether to send input events in virtio format.");
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080038
39DEFINE_string(adb, "", "Interface:port of local adb service.");
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080040
41int main(int argc, char **argv) {
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080042 ::gflags::ParseCommandLineFlags(&argc, &argv, true);
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080043
44 SSLSocket::Init();
45 DTLS::Init();
46
47 auto runLoop = RunLoop::main();
48
49 auto state = std::make_shared<ServerState>(
50 runLoop, ServerState::VideoFormat::VP8);
51
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080052 auto port = 8443; // Change to 8080 to use plain http instead of https.
53
54 auto httpd = std::make_shared<HTTPServer>(
55 runLoop,
56 "0.0.0.0",
57 port,
58 port == 8080
59 ? ServerSocket::TransportType::TCP
60 : ServerSocket::TransportType::TLS,
61 FLAGS_certs_dir + "/server.crt",
62 FLAGS_certs_dir + "/server.key");
63
64 const std::string index_html = FLAGS_assets_dir + "/index.html";
65 const std::string receive_js = FLAGS_assets_dir + "/js/receive.js";
66 const std::string logcat_js = FLAGS_assets_dir + "/js/logcat.js";
67 const std::string style_css = FLAGS_assets_dir + "/style.css";
68
69 httpd->addStaticFile("/index.html", index_html.c_str());
70 httpd->addStaticFile("/js/receive.js", receive_js.c_str());
71 httpd->addStaticFile("/js/logcat.js", logcat_js.c_str());
72 httpd->addStaticFile("/style.css", style_css.c_str());
73
74 httpd->addWebSocketHandlerFactory(
75 "/control",
76 [runLoop, state]{
77 auto id = state->acquireHandlerId();
78
79 auto handler =
80 std::make_shared<MyWebSocketHandler>(runLoop, state, id);
81
82 return std::make_pair(0 /* OK */, handler);
83 });
84
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080085 if (!FLAGS_adb.empty()) {
86 httpd->addWebSocketHandlerFactory(
87 "/control_adb",
88 [runLoop]{
89 auto handler = std::make_shared<AdbWebSocketHandler>(
90 runLoop, FLAGS_adb);
91
92 handler->run();
93
94 return std::make_pair(0 /* OK */, handler);
95 });
96 }
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080097
98 httpd->run();
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080099 runLoop->run();
100
101 return 0;
102}