summaryrefslogtreecommitdiff
path: root/main.cc
blob: ba723a1d7132addc7909b142e998ffcaf8c0167a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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;
  }
}