diff options
author | Mikhail Burakov <mburakov@mailbox.org> | 2024-11-18 06:34:31 +0100 |
---|---|---|
committer | Mikhail Burakov <mburakov@mailbox.org> | 2024-11-18 06:34:31 +0100 |
commit | 5be09d7edff924e7486d743e9d15edf07f555341 (patch) | |
tree | 064680d394732b0d1c00bfbbbb1ce3d5ed3d6e5e /main.cc | |
parent | 8a93cfdc03683eedc270bae957288af853f22a61 (diff) |
WIPnext
Diffstat (limited to 'main.cc')
-rw-r--r-- | main.cc | 76 |
1 files changed, 76 insertions, 0 deletions
@@ -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; + } +} |