summaryrefslogtreecommitdiff
path: root/main.c
blob: addb0031fa96ad564a9d31a809cf887c9b74d787 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * 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 <errno.h>
#include <pipewire/pipewire.h>
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "audio_context.h"
#include "io_context.h"
#include "proto.h"
#include "util.h"
#include "video_context.h"

static volatile sig_atomic_t g_signal;
static void OnSignal(int status) { g_signal = status; }

static bool SetupSignalHandler(int sig, void (*func)(int)) {
  struct sigaction sa = {
      .sa_handler = func,
  };
  if (sigemptyset(&sa.sa_mask) || sigaddset(&sa.sa_mask, sig)) {
    LOG("Failed to configure signal set (%s)", strerror(errno));
    return false;
  }
  if (sigaction(sig, &sa, NULL)) {
    LOG("Failed to set signal action (%s)", strerror(errno));
    return false;
  }
  return true;
}

static void HandleClientSession(struct IoContext* io_context) {
  struct VideoContext* video_context = VideoContextCreate(io_context);
  if (!video_context) {
    LOG("Failed to create video context");
    return;
  }

  struct AudioContext* audio_context = NULL;
  while (!g_signal) {
    struct Proto* proto = IoContextRead(io_context);
    if (!proto) {
      LOG("Failed to read proto");
      goto leave;
    }

    switch (proto->header->type) {
      case kProtoTypeHello:
        if (audio_context) {
          LOG("Audio reconfiguration prohibited");
          proto->Destroy(proto);
          goto leave;
        }
        audio_context = AudioContextCreate(io_context, proto);
        if (!audio_context) {
          LOG("Failed to create audio context");
          goto leave;
        }
        break;
      case kProtoTypePing:
      case kProtoTypeUhid:
        break;
      default:
        LOG("Unexpected proto received");
        proto->Destroy(proto);
        goto leave;
    }
  }

leave:
  if (audio_context) AudioContextDestroy(audio_context);
  VideoContextDestroy(video_context);
}

int main(int argc, char* argv[]) {
  pw_init(&argc, &argv);
  if (argc < 2) {
    LOG("Usage: streamer <port>");
    goto leave;
  }

  int port = atoi(argv[1]);
  if (0 >= port || port > UINT16_MAX) {
    LOG("Invalid port \"%s\"", argv[1]);
    goto leave;
  }

  SetupSignalHandler(SIGINT, OnSignal);
  SetupSignalHandler(SIGPIPE, SIG_IGN);
  SetupSignalHandler(SIGTERM, OnSignal);

  while (!g_signal) {
    struct IoContext* io_context = IoContextCreate((uint16_t)port);
    if (!io_context) {
      LOG("Failed to create io context");
      goto leave;
    }

    HandleClientSession(io_context);
    IoContextDestroy(io_context);
  }

leave:
  pw_deinit();
  bool result = g_signal == SIGINT || g_signal == SIGTERM;
  return result ? EXIT_SUCCESS : EXIT_FAILURE;
}