Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 1 | #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. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 15 | |
| 16 | #include <iostream> |
| 17 | #include <unordered_map> |
| 18 | |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 19 | #include <gflags/gflags.h> |
| 20 | |
| 21 | DEFINE_string( |
| 22 | public_ip, |
| 23 | "0.0.0.0", |
| 24 | "Public IPv4 address of your server, a.b.c.d format"); |
| 25 | DEFINE_string( |
| 26 | assets_dir, |
| 27 | "webrtc", |
| 28 | "Directory with location of webpage assets."); |
| 29 | DEFINE_string( |
| 30 | certs_dir, |
| 31 | "webrtc/certs", |
| 32 | "Directory to certificates."); |
| 33 | |
| 34 | DEFINE_int32(touch_fd, -1, "An fd to listen on for touch connections."); |
| 35 | DEFINE_int32(keyboard_fd, -1, "An fd to listen on for keyboard connections."); |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 36 | DEFINE_int32(frame_server_fd, -1, "An fd to listen on for frame updates"); |
| 37 | DEFINE_bool(write_virtio_input, false, "Whether to send input events in virtio format."); |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 38 | |
| 39 | DEFINE_string(adb, "", "Interface:port of local adb service."); |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 40 | |
| 41 | int main(int argc, char **argv) { |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 42 | ::gflags::ParseCommandLineFlags(&argc, &argv, true); |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 43 | |
| 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. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 52 | 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. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 85 | 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. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 97 | |
| 98 | httpd->run(); |
Jorge E. Moreira | a18ff1a | 2019-12-17 18:20:56 -0800 | [diff] [blame] | 99 | runLoop->run(); |
| 100 | |
| 101 | return 0; |
| 102 | } |