aboutsummaryrefslogtreecommitdiff
path: root/debian/patches/0001-netclientclock-simplify-by-using-g_socket_condition_.patch
blob: a33e72d23406d1e5e02cb0a50278f42dba44df48 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
From fe082cbe24a4bfd7131921d820df1b7689da7b66 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= <tim@centricular.net>
Date: Fri, 10 Aug 2012 09:19:25 +0100
Subject: [PATCH] netclientclock: simplify by using
 g_socket_condition_timed_wait()

No need to use a custom main context and custom timeout sources,
just use g_socket_condition_timed_wait() instead, which was added
for exactly this case.

Also seems to help with the unit test deadlocking with glib 2.33.x

https://bugzilla.gnome.org/show_bug.cgi?id=681575
---
 libs/gst/net/gstnetclientclock.c |  186 +++++++++++---------------------------
 1 file changed, 55 insertions(+), 131 deletions(-)

diff --git a/libs/gst/net/gstnetclientclock.c b/libs/gst/net/gstnetclientclock.c
index 691c414..de9db2b 100644
--- a/libs/gst/net/gstnetclientclock.c
+++ b/libs/gst/net/gstnetclientclock.c
@@ -248,70 +248,11 @@ bogus_observation:
   }
 }
 
-typedef struct
-{
-  GSource source;
-  GstNetClientClock *clock;
-  gboolean *p_timeout;
-} GstNetClientClockTimeoutSource;
-
-static gboolean
-gst_net_client_clock_timeout_source_prepare (GSource * s, gint * p_timeout)
-{
-  GstNetClientClockTimeoutSource *source = (GstNetClientClockTimeoutSource *) s;
-  GstClockTime expiration_time = source->clock->priv->timeout_expiration;
-  GstClockTime now = gst_util_get_timestamp ();
-
-  if (now >= expiration_time || (expiration_time - now) <= GST_MSECOND) {
-    *p_timeout = 0;
-    return TRUE;
-  }
-
-  *p_timeout = (expiration_time - now) / GST_MSECOND;
-  GST_TRACE_OBJECT (source->clock, "time out in %d ms please", *p_timeout);
-  return FALSE;
-}
-
-static gboolean
-gst_net_client_clock_timeout_source_check (GSource * s)
-{
-  GstNetClientClockTimeoutSource *source = (GstNetClientClockTimeoutSource *) s;
-
-  return (gst_util_get_timestamp () >= source->clock->priv->timeout_expiration);
-}
-
-static gboolean
-gst_net_client_clock_timeout_source_dispatch (GSource * s, GSourceFunc cb,
-    gpointer data)
-{
-  GstNetClientClockTimeoutSource *source = (GstNetClientClockTimeoutSource *) s;
-
-  GST_TRACE_OBJECT (source->clock, "timed out");
-  *source->p_timeout = TRUE;
-  return TRUE;
-}
-
-static gboolean
-gst_net_client_clock_socket_cb (GSocket * socket, GIOCondition condition,
-    gpointer user_data)
-{
-  GIOCondition *p_cond = user_data;
-
-  GST_TRACE ("socket %p I/O condition: 0x%02x", socket, condition);
-  *p_cond = condition;
-  return TRUE;
-}
-
 static gpointer
 gst_net_client_clock_thread (gpointer data)
 {
   GstNetClientClock *self = data;
   GstNetTimePacket *packet;
-  GMainContext *ctx;
-  GSourceFuncs funcs = { NULL, };
-  GSource *source;
-  GIOCondition cond;
-  gboolean timeout;
   GSocket *socket = self->priv->socket;
   GError *err = NULL;
   GstClock *clock = data;
@@ -321,99 +262,82 @@ gst_net_client_clock_thread (gpointer data)
   g_socket_set_blocking (socket, TRUE);
   g_socket_set_timeout (socket, 0);
 
-  ctx = g_main_context_new ();
-
-  source = g_socket_create_source (socket, G_IO_IN, self->priv->cancel);
-  g_source_set_name (source, "GStreamer net client clock thread socket");
-  g_source_set_callback (source, (GSourceFunc) gst_net_client_clock_socket_cb,
-      &cond, NULL);
-  g_source_attach (source, ctx);
-  g_source_unref (source);
-
-  /* GSocket only support second granularity for timeouts, so roll our own
-   * timeout source (so we don't have to create a new source whenever the
-   * timeout changes, as we would have to do with the default timeout source) */
-  funcs.prepare = gst_net_client_clock_timeout_source_prepare;
-  funcs.check = gst_net_client_clock_timeout_source_check;
-  funcs.dispatch = gst_net_client_clock_timeout_source_dispatch;
-  funcs.finalize = NULL;
-  source = g_source_new (&funcs, sizeof (GstNetClientClockTimeoutSource));
-  ((GstNetClientClockTimeoutSource *) source)->clock = self;
-  ((GstNetClientClockTimeoutSource *) source)->p_timeout = &timeout;
-  g_source_set_name (source, "GStreamer net client clock timeout");
-  g_source_attach (source, ctx);
-  g_source_unref (source);
-
   while (!g_cancellable_is_cancelled (self->priv->cancel)) {
-    cond = 0;
-    timeout = FALSE;
-    g_main_context_iteration (ctx, TRUE);
-
-    if (g_cancellable_is_cancelled (self->priv->cancel))
-      break;
+    GstClockTime expiration_time = self->priv->timeout_expiration;
+    GstClockTime now = gst_util_get_timestamp ();
+    gint64 socket_timeout;
+
+    if (now >= expiration_time || (expiration_time - now) <= GST_MSECOND) {
+      socket_timeout = 0;
+    } else {
+      socket_timeout = (expiration_time - now) / GST_USECOND;
+    }
 
-    if (timeout) {
-      /* timed out, let's send another packet */
-      GST_DEBUG_OBJECT (self, "timed out");
+    GST_TRACE_OBJECT (self, "time out in %d microsecs please", socket_timeout);
 
-      packet = gst_net_time_packet_new (NULL);
+    if (!g_socket_condition_timed_wait (socket, G_IO_IN, socket_timeout,
+            self->priv->cancel, &err)) {
+      /* cancelled, timeout or error */
+      if (err->code == G_IO_ERROR_CANCELLED) {
+        GST_INFO_OBJECT (self, "cancelled");
+        g_clear_error (&err);
+        break;
+      } else if (err->code == G_IO_ERROR_TIMED_OUT) {
+        /* timed out, let's send another packet */
+        GST_DEBUG_OBJECT (self, "timed out");
 
-      packet->local_time = gst_clock_get_internal_time (GST_CLOCK (self));
+        packet = gst_net_time_packet_new (NULL);
 
-      GST_DEBUG_OBJECT (self, "sending packet, local time = %" GST_TIME_FORMAT,
-          GST_TIME_ARGS (packet->local_time));
+        packet->local_time = gst_clock_get_internal_time (GST_CLOCK (self));
 
-      gst_net_time_packet_send (packet, self->priv->socket,
-          self->priv->servaddr, NULL);
+        GST_DEBUG_OBJECT (self,
+            "sending packet, local time = %" GST_TIME_FORMAT,
+            GST_TIME_ARGS (packet->local_time));
 
-      g_free (packet);
+        gst_net_time_packet_send (packet, self->priv->socket,
+            self->priv->servaddr, NULL);
 
-      /* reset timeout (but are expecting a response sooner anyway) */
-      self->priv->timeout_expiration =
-          gst_util_get_timestamp () + gst_clock_get_timeout (clock);
-      continue;
-    }
+        g_free (packet);
 
-    /* got data to read? */
-    if ((cond & G_IO_IN)) {
+        /* reset timeout (but are expecting a response sooner anyway) */
+        self->priv->timeout_expiration =
+            gst_util_get_timestamp () + gst_clock_get_timeout (clock);
+      } else {
+        GST_DEBUG_OBJECT (self, "socket error: %s", err->message);
+        g_usleep (G_USEC_PER_SEC / 10); /* throttle */
+      }
+      g_clear_error (&err);
+    } else {
       GstClockTime new_local;
 
+      /* got packet */
+
       new_local = gst_clock_get_internal_time (GST_CLOCK (self));
 
       packet = gst_net_time_packet_receive (socket, NULL, &err);
 
-      if (err != NULL) {
+      if (packet != NULL) {
+        GST_LOG_OBJECT (self, "got packet back");
+        GST_LOG_OBJECT (self, "local_1 = %" GST_TIME_FORMAT,
+            GST_TIME_ARGS (packet->local_time));
+        GST_LOG_OBJECT (self, "remote = %" GST_TIME_FORMAT,
+            GST_TIME_ARGS (packet->remote_time));
+        GST_LOG_OBJECT (self, "local_2 = %" GST_TIME_FORMAT,
+            GST_TIME_ARGS (new_local));
+
+        /* observe_times will reset the timeout */
+        gst_net_client_clock_observe_times (self, packet->local_time,
+            packet->remote_time, new_local);
+
+        g_free (packet);
+      } else if (err != NULL) {
         GST_WARNING_OBJECT (self, "receive error: %s", err->message);
-        g_error_free (err);
-        err = NULL;
-        continue;
+        g_clear_error (&err);
       }
-
-      GST_LOG_OBJECT (self, "got packet back");
-      GST_LOG_OBJECT (self, "local_1 = %" GST_TIME_FORMAT,
-          GST_TIME_ARGS (packet->local_time));
-      GST_LOG_OBJECT (self, "remote = %" GST_TIME_FORMAT,
-          GST_TIME_ARGS (packet->remote_time));
-      GST_LOG_OBJECT (self, "local_2 = %" GST_TIME_FORMAT,
-          GST_TIME_ARGS (new_local));
-
-      /* observe_times will reset the timeout */
-      gst_net_client_clock_observe_times (self, packet->local_time,
-          packet->remote_time, new_local);
-
-      g_free (packet);
-      continue;
-    }
-
-    if ((cond & (G_IO_ERR | G_IO_HUP))) {
-      GST_DEBUG_OBJECT (self, "socket error?! %s", g_strerror (errno));
-      g_usleep (G_USEC_PER_SEC / 10);
-      continue;
     }
   }
 
   GST_INFO_OBJECT (self, "shutting down net client clock thread");
-  g_main_context_unref (ctx);
   return NULL;
 }
 
-- 
1.7.10.4