summaryrefslogtreecommitdiff
path: root/main.cc
diff options
context:
space:
mode:
Diffstat (limited to 'main.cc')
-rw-r--r--main.cc76
1 files changed, 76 insertions, 0 deletions
diff --git a/main.cc b/main.cc
new file mode 100644
index 0000000..ba723a1
--- /dev/null
+++ b/main.cc
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2024 Mikhail Burakov. This file is part of streamer.
+ *
+ * streamer is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * streamer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with streamer. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <pipewire/pipewire.h>
+
+#include <csignal>
+#include <cstdint>
+#include <cstdlib>
+#include <exception>
+#include <iostream>
+#include <system_error>
+
+#include "util.h"
+
+namespace {
+
+volatile sig_atomic_t g_signal;
+void OnSignal(int status) { g_signal = status; }
+
+void SetupSignalHandler(int sig, void (*func)(int)) {
+ struct sigaction sa {};
+ sa.sa_handler = func;
+ if (sigemptyset(&sa.sa_mask) || sigaddset(&sa.sa_mask, sig)) {
+ throw std::system_error(errno, std::system_category(),
+ FROM_HERE "Failed to configure signal set");
+ }
+ if (sigaction(sig, &sa, NULL)) {
+ throw std::system_error(errno, std::system_category(),
+ FROM_HERE "Failed to set signal action");
+ }
+}
+
+} // namespace
+
+int main(int argc, char* argv[]) {
+ try {
+ if (argc < 2) {
+ throw std::runtime_error(FROM_HERE "Usage: streamer <port>");
+ }
+
+ int port = std::atoi(argv[1]);
+ if (0 >= port || port > UINT16_MAX) {
+ throw std::runtime_error(FROM_HERE "Invalid port number");
+ }
+
+ pw_init(&argc, &argv);
+ Defer defer_pw_deinit([] { pw_deinit(); });
+
+ SetupSignalHandler(SIGINT, OnSignal);
+ SetupSignalHandler(SIGPIPE, SIG_IGN);
+ SetupSignalHandler(SIGTERM, OnSignal);
+
+ while (!g_signal) {
+ // BLAH
+ }
+
+ return EXIT_SUCCESS;
+ } catch (const std::exception& ex) {
+ std::cerr << ex.what() << std::endl;
+ return EXIT_FAILURE;
+ }
+}