blob: 6d7fce865a436d8bbd07a466d6f9730655532d3a [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>
15#include <media/stagefright/Utils.h>
16
17#include <iostream>
18#include <unordered_map>
19
20#if defined(TARGET_ANDROID)
21#include <gflags/gflags.h>
22
23DEFINE_string(
24 public_ip,
25 "0.0.0.0",
26 "Public IPv4 address of your server, a.b.c.d format");
27DEFINE_string(
28 assets_dir,
29 "webrtc",
30 "Directory with location of webpage assets.");
31DEFINE_string(
32 certs_dir,
33 "webrtc/certs",
34 "Directory to certificates.");
35
36DEFINE_int32(touch_fd, -1, "An fd to listen on for touch connections.");
37DEFINE_int32(keyboard_fd, -1, "An fd to listen on for keyboard connections.");
Jorge E. Moreirae049b792019-12-18 18:17:48 -080038DEFINE_int32(frame_server_fd, -1, "An fd to listen on for frame updates");
39DEFINE_bool(write_virtio_input, false, "Whether to send input events in virtio format.");
Jorge E. Moreiraa18ff1a2019-12-17 18:20:56 -080040
41DEFINE_string(adb, "", "Interface:port of local adb service.");
42#endif
43
44int main(int argc, char **argv) {
45#if defined(TARGET_ANDROID)
46 ::gflags::ParseCommandLineFlags(&argc, &argv, true);
47#else
48 (void)argc;
49 (void)argv;
50#endif
51
52 SSLSocket::Init();
53 DTLS::Init();
54
55 auto runLoop = RunLoop::main();
56
57 auto state = std::make_shared<ServerState>(
58 runLoop, ServerState::VideoFormat::VP8);
59
60#if 1
61 auto port = 8443; // Change to 8080 to use plain http instead of https.
62
63 auto httpd = std::make_shared<HTTPServer>(
64 runLoop,
65 "0.0.0.0",
66 port,
67 port == 8080
68 ? ServerSocket::TransportType::TCP
69 : ServerSocket::TransportType::TLS,
70 FLAGS_certs_dir + "/server.crt",
71 FLAGS_certs_dir + "/server.key");
72
73 const std::string index_html = FLAGS_assets_dir + "/index.html";
74 const std::string receive_js = FLAGS_assets_dir + "/js/receive.js";
75 const std::string logcat_js = FLAGS_assets_dir + "/js/logcat.js";
76 const std::string style_css = FLAGS_assets_dir + "/style.css";
77
78 httpd->addStaticFile("/index.html", index_html.c_str());
79 httpd->addStaticFile("/js/receive.js", receive_js.c_str());
80 httpd->addStaticFile("/js/logcat.js", logcat_js.c_str());
81 httpd->addStaticFile("/style.css", style_css.c_str());
82
83 httpd->addWebSocketHandlerFactory(
84 "/control",
85 [runLoop, state]{
86 auto id = state->acquireHandlerId();
87
88 auto handler =
89 std::make_shared<MyWebSocketHandler>(runLoop, state, id);
90
91 return std::make_pair(0 /* OK */, handler);
92 });
93
94#if defined(TARGET_ANDROID)
95 if (!FLAGS_adb.empty()) {
96 httpd->addWebSocketHandlerFactory(
97 "/control_adb",
98 [runLoop]{
99 auto handler = std::make_shared<AdbWebSocketHandler>(
100 runLoop, FLAGS_adb);
101
102 handler->run();
103
104 return std::make_pair(0 /* OK */, handler);
105 });
106 }
107#endif
108
109 httpd->run();
110#else
111 uint16_t receiverPort = 63843;
112 std::string receiverUFrag = "N1NB";
113 std::string receiverPassword = "deadbeef";
114
115 uint16_t senderPort = 63844;
116 std::string senderUFrag = "ABCD";
117 std::string senderPassword = "wooops";
118
119 auto sender = std::make_shared<RTPSocketHandler>(
120 runLoop,
121 RTPSocketHandler::Mode::CONTROLLER,
122 AF_INET,
123 senderPort,
124 false /* isChrome */);
125
126 sender->addSession(
127 senderUFrag,
128 senderPassword,
129 receiverUFrag,
130 receiverPassword);
131
132 sockaddr_in addr;
133 memset(&addr, 0, sizeof(addr));
134 addr.sin_family = AF_INET;
135 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
136 addr.sin_port = htons(senderPort);
137
138 sockaddr_storage senderAddr;
139 memcpy(&senderAddr, &addr, sizeof(addr));
140
141 auto receiver = std::make_shared<RTPSocketHandler>(
142 runLoop,
143 RTPSocketHandler::Mode::CONTROLLEE,
144 AF_INET,
145 receiverPort,
146 false /* isChrome */);
147
148 receiver->addSession(
149 receiverUFrag,
150 receiverPassword,
151 senderUFrag,
152 senderPassword,
153 senderAddr);
154
155 sender->run();
156 receiver->run();
157
158 receiver->kick();
159#endif
160
161 runLoop->run();
162
163 return 0;
164}