summaryrefslogtreecommitdiff
path: root/src/thread-tracker.c
blob: 612fc6771378742a367fab929f0b63e3bd23881b (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * thread-tracker.c - Tracks threads in a GstPipeline
 * Copyright (C) 2009-2010 Collabora Ltd.
 * Copyright (C) 2009-2010 Nokia Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

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

#include <pthread.h>
#include <sys/syscall.h>
#include <unistd.h>

#include <gst/gst.h>

#include "thread-tracker.h"

static gboolean add_pad_locked (struct ObjectTrackingData *otd, GstPad *pad);


void
thread_tracking_init (struct ThreadTrackingData *ttd,
    GMutex *mutex,
    setup_thread_prio restore_thread_prio)
{
  ttd->object_threads =
      g_hash_table_new (g_direct_hash, g_direct_equal);
  ttd->object_tids =
      g_hash_table_new (g_direct_hash, g_direct_equal);
  ttd->otds = g_ptr_array_new ();
  ttd->mutex = mutex;
  ttd->restore_thread_prio = restore_thread_prio;
}

void
object_tracking_init (struct ThreadTrackingData *ttd,
    struct ObjectTrackingData *otd,
    setup_thread_prio setup_thread_prio)
{
  otd->ttd = ttd;
  otd->objects = g_ptr_array_new ();
  otd->setup_thread_prio = setup_thread_prio;
  g_ptr_array_add (ttd->otds, otd);
}


void
thread_tracking_data_free (struct ThreadTrackingData *ttd)
{
  g_hash_table_destroy (ttd->object_threads);
  g_hash_table_destroy (ttd->object_tids);
  g_ptr_array_free (ttd->otds, TRUE);
}

void
object_tracking_data_free (struct ObjectTrackingData *otd)
{
  g_ptr_array_free (otd->objects, TRUE);
  g_ptr_array_remove (otd->ttd->otds, otd);
}



static void
remove_object (gpointer data, GObject *where_the_obj_was)
{
  struct ObjectTrackingData *otd = data;

  g_mutex_lock (otd->ttd->mutex);
  g_ptr_array_remove (otd->objects, where_the_obj_was);
  g_mutex_unlock (otd->ttd->mutex);
}

static void
pad_linked (GstPad *pad, GstPad *peer, struct ObjectTrackingData *otd);

/* Returns TRUE if it is a object
 * Returns FALSE if it was already added
 */

static gboolean
insert_object_locked (struct ObjectTrackingData *otd, gpointer object)
{
  guint i;
  pthread_t thread;
  pid_t tid;

  for (i = 0; i < otd->objects->len; i++)
    if (g_ptr_array_index (otd->objects, i) == object)
      return FALSE;

  g_ptr_array_add (otd->objects, object);
  g_object_weak_ref (G_OBJECT (object), remove_object, otd);


  thread = GPOINTER_TO_UINT (
      g_hash_table_lookup (otd->ttd->object_threads, object));
  tid = GPOINTER_TO_UINT (
      g_hash_table_lookup (otd->ttd->object_tids, object));

  if (thread && tid)
    otd->setup_thread_prio (thread, tid);

  return TRUE;
}

static void
pad_added (GstElement *element, GstPad *pad, struct ObjectTrackingData *otd)
{
  add_pad_locked (otd, pad);
}

/* Returns TRUE if it is a new pad
 * Returns FALSE if it was already added
 */

static gboolean
add_pad_locked (struct ObjectTrackingData *otd, GstPad *pad)
{
  GstObject *parent;

  if (!insert_object_locked (otd, pad))
    return FALSE;

  parent = gst_pad_get_parent (pad);

  if (parent)
    {
      if (GST_IS_PAD (parent))
        {
          add_pad_locked (otd, GST_PAD_CAST (parent));
        }
      else
        {
          if (insert_object_locked (otd, parent) &&
              (GST_IS_ELEMENT (parent) && !GST_IS_BIN (parent)))
            {
              g_signal_connect (parent, "pad-added", G_CALLBACK (pad_added),
                  otd);
            }
        }
      gst_object_unref (parent);
    }

  g_signal_connect (pad, "linked", G_CALLBACK (pad_linked), otd);

  return TRUE;
}

static void
pad_iter_locked (gpointer data, gpointer user_data)
{
  struct ObjectTrackingData *otd = user_data;
  GstPad *pad = data;

  if (add_pad_locked (otd, pad))
    {
      GstPad *peerpad = gst_pad_get_peer (pad);

      if (peerpad)
        {
          if (add_pad_locked (otd, peerpad))
            {
              GstIterator *iter;

              iter = gst_pad_iterate_internal_links (peerpad);
              if (iter)
                {
                  while (gst_iterator_foreach (iter, pad_iter_locked, otd) ==
                      GST_ITERATOR_RESYNC)
                    gst_iterator_resync (iter);
                  gst_iterator_free (iter);
                }
            }
          gst_object_unref (peerpad);
        }
    }
  gst_object_unref (pad);
}

static void
pad_linked (GstPad *pad, GstPad *peer, struct ObjectTrackingData *otd)
{
  g_mutex_lock (otd->ttd->mutex);
  if (add_pad_locked (otd, peer))
    {
      GstIterator *iter = gst_pad_iterate_internal_links (peer);

      if (iter)
        {
          while (gst_iterator_foreach (iter, pad_iter_locked, otd) ==
              GST_ITERATOR_RESYNC)
            gst_iterator_resync (iter);

          gst_iterator_free (iter);
        }
    }
  g_mutex_unlock (otd->ttd->mutex);
}

static void
pad_internal_links_iter_locked (gpointer data, gpointer user_data)
{
  struct ObjectTrackingData *otd = user_data;
  GstPad *pad = data;
  GstIterator *iter;

  iter = gst_pad_iterate_internal_links (pad);
  while (gst_iterator_foreach (iter, pad_iter_locked, otd) ==
      GST_ITERATOR_RESYNC)
    gst_iterator_resync (iter);
  gst_iterator_free (iter);

  gst_object_unref (pad);
}


void
object_tracker_add_sink (struct ObjectTrackingData *otd, GstElement *sink)
{
  GstIterator *iter;

  g_mutex_lock (otd->ttd->mutex);

  iter = gst_element_iterate_sink_pads (sink);
  while (gst_iterator_foreach (iter, pad_iter_locked, otd) ==
      GST_ITERATOR_RESYNC)
    gst_iterator_resync (iter);

  gst_iterator_resync (iter);
  while (gst_iterator_foreach (iter, pad_internal_links_iter_locked, otd) ==
      GST_ITERATOR_RESYNC)
    gst_iterator_resync (iter);
  gst_iterator_free (iter);

  g_mutex_unlock (otd->ttd->mutex);
}

void
object_tracker_add_src (struct ObjectTrackingData *otd, GstElement *src)
{
  GstIterator *iter;

  g_mutex_lock (otd->ttd->mutex);

  iter = gst_element_iterate_src_pads (src);
  while (gst_iterator_foreach (iter, pad_iter_locked, otd) ==
      GST_ITERATOR_RESYNC)
    gst_iterator_resync (iter);

  gst_iterator_resync (iter);
  while (gst_iterator_foreach (iter, pad_internal_links_iter_locked, otd) ==
      GST_ITERATOR_RESYNC)
    gst_iterator_resync (iter);
  gst_iterator_free (iter);

  g_mutex_unlock (otd->ttd->mutex);
}


void
enter_thread (struct ThreadTrackingData *ttd, GstObject *src)
{
  pthread_t thread = pthread_self ();
  pid_t tid = syscall (__NR_gettid);
  guint i, j;

  g_mutex_lock (ttd->mutex);
  g_hash_table_insert (ttd->object_threads, src, GINT_TO_POINTER (thread));
  g_hash_table_insert (ttd->object_tids, src, GINT_TO_POINTER (tid));

  for (i = 0; g_ptr_array_index (ttd->otds, i); i++)
    {
      struct ObjectTrackingData *otd = g_ptr_array_index (ttd->otds, i);

      for (j = 0; j < otd->objects->len; j++)
        {
          if (g_ptr_array_index (otd->objects, j) == src)
            otd->setup_thread_prio (thread, tid);
          break;
        }
    }

  g_mutex_unlock (ttd->mutex);
}


void
leave_thread (struct ThreadTrackingData *ttd, GstObject *src, GstElement *owner)
{
  pthread_t thread = pthread_self ();
  pid_t tid = syscall (__NR_gettid);

  g_mutex_lock (ttd->mutex);
  g_hash_table_remove (ttd->object_threads, src);
  g_hash_table_remove (ttd->object_tids, src);
  g_mutex_unlock (ttd->mutex);

  ttd->restore_thread_prio (thread, tid);
}