summaryrefslogtreecommitdiff
path: root/src/udp.c
blob: 470df3653ab76f62bbf6ddfa530fd09c386f0b09 (plain) (blame)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 *  sydra
 *
 *  sydra is a toolbox which allows you to set up multiple bidirectional
 *  Video/Audio streams from external locations.
 *  sydra has been written to be used for the Elevate Festival in Graz
 *  Austria in order to involve external locations to present themselves
 *  at the festival.
 *  Sydra is based on GStreamer and is written in C.
 *
 *
 *  Copyright (C) 2014 Christian Pointner <equinox@spreadspace.org>
 *
 *  This file is part of sydra.
 *
 *  sydra 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
 *  any later version.
 *
 *  sydra 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 sydra. If not, see <http://www.gnu.org/licenses/>.
 *
 *  In addition, as a special exception, the copyright holders hereby
 *  grant permission for non-GPL-compatible GStreamer plugins to be used
 *  and distributed together with GStreamer and sydra.
 *  This permission goes above and beyond the permissions granted by the
 *  GPL license sydra is covered by.
 */

#include "datatypes.h"

#include <string.h>
#include <glib.h>
#include <glib-unix.h>
#include <gio/gio.h>
#include <gst/gst.h>

#include "utils.h"
#include "log.h"


#define UDP_PROTO_MAGIC "SYDRA:"
#define UDP_PROTO_MAGIC_LEN (sizeof(UDP_PROTO_MAGIC)-1)
#define UDP_PROTO_CMD_ADD_CLIENT "add_client\n"
#define UDP_PROTO_CMD_ADD_CLIENT_LEN (sizeof(UDP_PROTO_CMD_ADD_CLIENT)-1)
#define UDP_PROTO_CMD_RM_CLIENT "remove_client\n"
#define UDP_PROTO_CMD_RM_CLIENT_LEN (sizeof(UDP_PROTO_CMD_RM_CLIENT)-1)

static struct udp_client* udp_client_new(struct sockaddr_storage *addr, socklen_t addrlen)
{
  struct udp_client* client = g_new(struct udp_client, 1);
  if(!client) return NULL;

  switch(addr->ss_family)
  {
  case AF_INET: client->host_.type_ = IPv4; client->host_.port_ = ntohs(((struct sockaddr_in*)addr)->sin_port); break;
  case AF_INET6: client->host_.type_ = IPv6; client->host_.port_ = ntohs(((struct sockaddr_in6*)addr)->sin6_port); break;
  default: g_free(client); return NULL;
  }

  int errcode  = getnameinfo((struct sockaddr *)addr, addrlen, client->host_.addr_, sizeof(client->host_.addr_),
                             NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV);
  if (errcode != 0) {
    log_printf(WARNING, "getnameinfo() error: %s", gai_strerror(errcode));
    g_free(client);
    return NULL;
  }
  return client;
}

static void udpsink_add_client(struct sockaddr_storage *addr, socklen_t addrlen, struct udp_sink* sink)
{
  struct udp_client* client = udp_client_new(addr, addrlen);
  if(!client) return;

  GTimeVal now;
  g_get_current_time(&now);
  client->last_seen_ = GST_TIMEVAL_TO_TIME(now);

  GList* entry = g_list_find_custom(sink->clients_, client, cmp_udp_client);
  gchar* name = gst_element_get_name(sink->udp_);
  if(!entry) {
    log_printf(INFO, "adding host %s%c%d to list of receivers for element %s",
               client->host_.addr_, client->host_.type_ == IPv4 ? ':' : '.', client->host_.port_, name);
    g_signal_emit_by_name(G_OBJECT(sink->udp_), "add", client->host_.addr_, client->host_.port_, NULL);
    sink->clients_ = g_list_append(sink->clients_, client);
  } else {
    log_printf(DEBUG, "not adding host %s%c%d to list of receivers for element %s since it already exists",
               client->host_.addr_, client->host_.type_ == IPv4 ? ':' : '.', client->host_.port_, name);
    ((struct udp_client*)(entry->data))->last_seen_ = client->last_seen_;
    g_free(client);
  }
  g_free(name);
}

static void udpsink_remove_client(struct sockaddr_storage *addr, socklen_t addrlen, struct udp_sink* sink)
{
  struct udp_client* client = udp_client_new(addr, addrlen);
  if(!client) return;

  GList* removee = g_list_find_custom(sink->clients_, client, cmp_udp_client);
  gchar* name = gst_element_get_name(sink->udp_);
  if(!removee) {
    log_printf(WARNING, "client %s%c%d not found in client list for %s - nothing removed",
               client->host_.addr_, client->host_.type_ == IPv4 ? ':' : '.', client->host_.port_, name, name);
  } else {
    g_signal_emit_by_name(G_OBJECT(sink->udp_), "remove", client->host_.addr_, client->host_.port_, NULL);
    log_printf(INFO, "client %s%c%d removed from list of receivers for element %s (request)",
               client->host_.addr_, client->host_.type_ == IPv4 ? ':' : '.', client->host_.port_, name, name);
    gpointer rdata = removee->data;
    sink->clients_ = g_list_remove(sink->clients_, removee->data);
    g_free(rdata);
  }
  g_free(name);
  g_free(client);
}

static gboolean on_udp_desc_ready(gint fd, GIOCondition cond, gpointer user_data)
{
  struct udp_sink* sink = (struct udp_sink*)user_data;
  if(!sink || !(sink->udp_)) {
    log_printf(WARNING, "File descriptor %d is ready for reading - but supplied element is NULL removing callback", fd);
    return FALSE;
  }

  struct sockaddr_storage addr;
  socklen_t addrlen = sizeof(addr);
  u_int8_t buf[2048];
  ssize_t bytes = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, &addrlen);
  if(bytes < 1) {
    if(errno == EINTR)
      return TRUE;

    log_printf(WARNING, "Error while receiving UDP data on fd %d, will remove callback", fd);
    return FALSE;
  }

  if(bytes < UDP_PROTO_MAGIC_LEN || memcmp(UDP_PROTO_MAGIC, buf, UDP_PROTO_MAGIC_LEN)) {
    log_printf(DEBUG, "client discovery: ignoring invalid incoming packet");
    return TRUE;
  }

  u_int8_t* cmd = &(buf[UDP_PROTO_MAGIC_LEN]);
  if(bytes == (UDP_PROTO_MAGIC_LEN+UDP_PROTO_CMD_ADD_CLIENT_LEN) &&
     !memcmp(UDP_PROTO_CMD_ADD_CLIENT, cmd, UDP_PROTO_CMD_ADD_CLIENT_LEN)) {
    udpsink_add_client(&addr, addrlen, sink);
  } else if(bytes == (UDP_PROTO_MAGIC_LEN+UDP_PROTO_CMD_RM_CLIENT_LEN) &&
     !memcmp(UDP_PROTO_CMD_RM_CLIENT, cmd, UDP_PROTO_CMD_RM_CLIENT_LEN)) {
    udpsink_remove_client(&addr, addrlen, sink);
  } else {
    log_printf(DEBUG, "client discovery: ignoring invalid command");
  }
  return TRUE;
}

static void check_client_timeout(struct udp_sink* sink, int timeout)
{
  GTimeVal now;
  g_get_current_time(&now);
  GstClockTime timeout_time = GST_TIMEVAL_TO_TIME(now) - (timeout * GST_SECOND);

  GList *c = sink->clients_;
  while(c != NULL) {
    GList *next = c->next;
    struct udp_client* client = (struct udp_client*)c->data;
    if(client->last_seen_ < timeout_time) {
      g_signal_emit_by_name(G_OBJECT(sink->udp_), "remove", client->host_.addr_, client->host_.port_, NULL);
      gchar* name = gst_element_get_name(sink->udp_);
      log_printf(INFO, "client %s%c%d removed from list of receivers for element %s (timeout)",
                 client->host_.addr_, client->host_.type_ == IPv4 ? ':' : '.', client->host_.port_, name, name);
      g_free(name);
      g_free(c->data);
      sink->clients_ = g_list_delete_link(sink->clients_, c);
    }
    c = next;
  }
}

static gboolean check_all_client_timeouts(gpointer user_data)
{
  struct udp_sinks* sinks = (struct udp_sinks*)user_data;
  check_client_timeout(&(sinks->rtp_video_), sinks->client_timeout_);
  check_client_timeout(&(sinks->rtcp_video_), sinks->client_timeout_);
  check_client_timeout(&(sinks->rtp_audio_), sinks->client_timeout_);
  check_client_timeout(&(sinks->rtcp_audio_), sinks->client_timeout_);
  return TRUE;
}

static gboolean attach_udpsink(struct udp_sink* sink, const char* name, const char* prop)
{
  GSocket *sock;
  g_object_get(G_OBJECT(sink->udp_), prop, &sock, NULL);
  int fd = g_socket_get_fd(sock);

  guint id = g_unix_fd_add(fd, G_IO_IN, on_udp_desc_ready, sink);
  if(id <= 0) {
    log_printf(ERROR, "Error while adding %s file descriptor (%d) to main loop", name, fd);
    return FALSE;
  }
  log_printf(DEBUG, "successfully installed callback for %s (fd: %d) for reading (id: %d)", name, fd, id);
  return TRUE;
}

gboolean attach_udpsinks(struct udp_sinks *sinks)
{
  if(!sinks)
    return FALSE;

  if(!attach_udpsink(&(sinks->rtp_video_), "RTP(video) IPv4", "used-socket") ||
     !attach_udpsink(&(sinks->rtp_video_), "RTP(video) IPv6", "used-socket-v6") ||
     !attach_udpsink(&(sinks->rtcp_video_), "RTCP(video) IPv4", "used-socket") ||
     !attach_udpsink(&(sinks->rtcp_video_), "RTCP(video) IPv6", "used-socket-v6") ||
     !attach_udpsink(&(sinks->rtp_audio_), "RTP(audio) IPv4", "used-socket") ||
     !attach_udpsink(&(sinks->rtp_audio_), "RTP(audio) IPv6", "used-socket-v6") ||
     !attach_udpsink(&(sinks->rtcp_audio_), "RTCP(audio) IPv4", "used-socket") ||
     !attach_udpsink(&(sinks->rtcp_audio_), "RTCP(audio) IPv6", "used-socket-v6")) {
    return FALSE;
  }
  if(sinks->client_timeout_ > 0) {
    if(!g_timeout_add_seconds(1, check_all_client_timeouts, sinks))
      return FALSE;
  }

  return TRUE;
}


gboolean attach_udpsources(struct udp_sources *sources)
{
  if(!sources)
    return FALSE;

  log_printf(WARNING, "udp source keep alive messages are not implemented yet!");

  return TRUE;
}