aboutsummaryrefslogtreecommitdiff
path: root/libs/gst/net/gstnettimepacket.c
blob: bf8a0276724db2488215b6843d8bcef6f569f18b (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
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
/* GStreamer
 * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
/**
 * SECTION:gstnettimepacket
 * @short_description: Helper structure to construct clock packets used
 *                     by network clocks.
 * @see_also: #GstClock, #GstNetClientClock, #GstNetTimeProvider
 *
 * Various functions for receiving, sending an serializing #GstNetTimePacket
 * structures.
 *
 * Last reviewed on 2005-11-23 (0.9.5)
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <glib.h>

#ifdef __CYGWIN__
# include <unistd.h>
# include <fcntl.h>
#endif

#include "gstnettimepacket.h"


/**
 * gst_net_time_packet_new:
 * @buffer: a buffer from which to construct the packet, or NULL
 *
 * Creates a new #GstNetTimePacket from a buffer received over the network. The
 * caller is responsible for ensuring that @buffer is at least
 * #GST_NET_TIME_PACKET_SIZE bytes long.
 *
 * If @buffer is #NULL, the local and remote times will be set to
 * #GST_CLOCK_TIME_NONE.
 *
 * MT safe. Caller owns return value (g_free to free).
 *
 * Returns: The new #GstNetTimePacket.
 */
GstNetTimePacket *
gst_net_time_packet_new (const guint8 * buffer)
{
  GstNetTimePacket *ret;

  g_assert (sizeof (GstClockTime) == 8);

  ret = g_new0 (GstNetTimePacket, 1);

  if (buffer) {
    ret->local_time = GST_READ_UINT64_BE (buffer);
    ret->remote_time = GST_READ_UINT64_BE (buffer + sizeof (GstClockTime));
  } else {
    ret->local_time = GST_CLOCK_TIME_NONE;
    ret->remote_time = GST_CLOCK_TIME_NONE;
  }

  return ret;
}

/**
 * gst_net_time_packet_serialize:
 * @packet: the #GstNetTimePacket
 *
 * Serialized a #GstNetTimePacket into a newly-allocated sequence of
 * #GST_NET_TIME_PACKET_SIZE bytes, in network byte order. The value returned is
 * suitable for passing to write(2) or sendto(2) for communication over the
 * network.
 *
 * MT safe. Caller owns return value (g_free to free).
 *
 * Returns: A newly allocated sequence of #GST_NET_TIME_PACKET_SIZE bytes.
 */
guint8 *
gst_net_time_packet_serialize (const GstNetTimePacket * packet)
{
  guint8 *ret;

  g_assert (sizeof (GstClockTime) == 8);

  ret = g_new0 (guint8, GST_NET_TIME_PACKET_SIZE);

  GST_WRITE_UINT64_BE (ret, packet->local_time);
  GST_WRITE_UINT64_BE (ret + sizeof (GstClockTime), packet->remote_time);

  return ret;
}

/**
 * gst_net_time_packet_receive:
 * @fd: a file descriptor created by socket(2)
 * @addr: a pointer to a sockaddr to hold the address of the sender
 * @len: a pointer to the size of the data pointed to by @addr
 *
 * Receives a #GstNetTimePacket over a socket. Handles interrupted system calls,
 * but otherwise returns NULL on error. See recvfrom(2) for more information on
 * how to interpret @sockaddr.
 *
 * MT safe. Caller owns return value (g_free to free).
 *
 * Returns: The new #GstNetTimePacket.
 */
GstNetTimePacket *
gst_net_time_packet_receive (gint fd, struct sockaddr * addr, socklen_t * len)
{
  guint8 buffer[GST_NET_TIME_PACKET_SIZE];
  gint ret;

  while (TRUE) {
#ifdef G_OS_WIN32
    ret = recvfrom (fd, (char *) buffer, GST_NET_TIME_PACKET_SIZE,
#else
    ret = recvfrom (fd, buffer, GST_NET_TIME_PACKET_SIZE,
#endif
        0, (struct sockaddr *) addr, len);
    if (ret < 0) {
      if (errno != EAGAIN && errno != EINTR)
        goto receive_error;
      else
        continue;
    } else if (ret < GST_NET_TIME_PACKET_SIZE) {
      goto short_packet;
    } else {
      return gst_net_time_packet_new (buffer);
    }
  }

receive_error:
  {
    GST_DEBUG ("receive error %d: %s (%d)", ret, g_strerror (errno), errno);
    return NULL;
  }
short_packet:
  {
    GST_DEBUG ("someone sent us a short packet (%d < %d)",
        ret, GST_NET_TIME_PACKET_SIZE);
    return NULL;
  }
}

/**
 * gst_net_time_packet_send:
 * @packet: the #GstNetTimePacket
 * @fd: a file descriptor created by socket(2)
 * @addr: a pointer to a sockaddr to hold the address of the sender
 * @len: the size of the data pointed to by @addr
 *
 * Sends a #GstNetTimePacket over a socket. Essentially a thin wrapper around
 * sendto(2) and gst_net_time_packet_serialize(). 
 *
 * MT safe.
 *
 * Returns: The return value of sendto(2).
 */
gint
gst_net_time_packet_send (const GstNetTimePacket * packet, gint fd,
    struct sockaddr * addr, socklen_t len)
{
#if defined __CYGWIN__
  gint fdflags;
#elif defined G_OS_WIN32
  gulong flags;
#endif

  guint8 *buffer;
  gint ret, send_flags;

  g_return_val_if_fail (packet != NULL, -EINVAL);

#ifdef __CYGWIN__
  send_flags = 0;
  fdflags = fcntl (fd, F_GETFL);
  fcntl (fd, F_SETFL, fdflags | O_NONBLOCK);
#elif defined G_OS_WIN32
  flags = 1;
  send_flags = 0;
#else
  send_flags = MSG_DONTWAIT;
#endif

  buffer = gst_net_time_packet_serialize (packet);

#ifdef G_OS_WIN32
  ioctlsocket (fd, FIONBIO, &flags);    /* Set nonblocking mode */
  ret =
      sendto (fd, (char *) buffer, GST_NET_TIME_PACKET_SIZE, send_flags, addr,
      len);
#else
  ret = sendto (fd, buffer, GST_NET_TIME_PACKET_SIZE, send_flags, addr, len);
#endif

#ifdef __CYGWIN__
  fcntl (fd, F_SETFL, fdflags);
#endif

  g_free (buffer);

  return ret;
}