summaryrefslogtreecommitdiff
path: root/gst/autoplug/gstautoplugcache.c
blob: cfadc1efa509e102ce6da4595b3602815409b285 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* GStreamer
 * Copyright (C) 2001 RidgeRun, Inc. (www.ridgerun.com)
 *
 * gstautoplugcache.c: Data cache for the dynamic autoplugger
 *
 * 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.
 */

#include <gst/gst.h>

GstElementDetails gst_autoplugcache_details = {
  "AutoplugCache",
  "Connection",
  "Data cache for the dynamic autoplugger",
  VERSION,
  "Erik Walthinsen <omega@temple-baptist.com>",
  "(C) 2001 RidgeRun, Inc. (www.ridgerun.com)",
};

#define GST_TYPE_AUTOPLUGCACHE \
  (gst_autoplugcache_get_type())
#define GST_AUTOPLUGCACHE(obj) \
  (GTK_CHECK_CAST((obj),GST_TYPE_AUTOPLUGCACHE,GstAutoplugCache))
#define GST_AUTOPLUGCACHE_CLASS(klass) \
  (GTK_CHECK_CLASS_CAST((klass),GST_TYPE_AUTOPLUGCACHE,GstAutoplugCacheClass))
#define GST_IS_AUTOPLUGCACHE(obj) \
  (GTK_CHECK_TYPE((obj),GST_TYPE_AUTOPLUGCACHE))
#define GST_IS_AUTOPLUGCACHE_CLASS(obj) \
  (GTK_CHECK_CLASS_TYPE((klass),GST_TYPE_AUTOPLUGCACHE))

typedef struct _GstAutoplugCache GstAutoplugCache;
typedef struct _GstAutoplugCacheClass GstAutoplugCacheClass;

struct _GstAutoplugCache {
  GstElement element;

  GstPad *sinkpad, *srcpad;

  gboolean caps_proxy;

  GList *cache;
  GList *cache_start;
  gint buffer_count;
  GList *current_playout;
  gboolean fire_empty;
  gboolean fire_first;
};

struct _GstAutoplugCacheClass {
  GstElementClass parent_class;

  void		(*first_buffer)		(GstElement *element, GstBuffer *buf);
  void		(*cache_empty)		(GstElement *element);
};


/* Cache signals and args */
enum {
  FIRST_BUFFER,
  CACHE_EMPTY,
  LAST_SIGNAL
};

enum {
  ARG_0,
  ARG_BUFFER_COUNT,
  ARG_CAPS_PROXY,
  ARG_RESET
};


static void			gst_autoplugcache_class_init	(GstAutoplugCacheClass *klass);
static void			gst_autoplugcache_init		(GstAutoplugCache *cache);

static void			gst_autoplugcache_set_arg	(GtkObject *object, GtkArg *arg, guint id);
static void			gst_autoplugcache_get_arg	(GtkObject *object, GtkArg *arg, guint id);

static void			gst_autoplugcache_loop		(GstElement *element);

static GstPadNegotiateReturn    gst_autoplugcache_nego_src	(GstPad *pad, GstCaps **caps, gpointer *data);
static GstPadNegotiateReturn    gst_autoplugcache_nego_sink	(GstPad *pad, GstCaps **caps, gpointer *data);
static GstElementStateReturn	gst_autoplugcache_change_state	(GstElement *element);


static GstElementClass *parent_class = NULL;
static guint gst_autoplugcache_signals[LAST_SIGNAL] = { 0 };

GtkType
gst_autoplugcache_get_type(void) {
  static GtkType autoplugcache_type = 0;

  if (!autoplugcache_type) {
    static const GtkTypeInfo autoplugcache_info = {
      "GstAutoplugCache",
      sizeof(GstAutoplugCache),
      sizeof(GstAutoplugCacheClass),
      (GtkClassInitFunc)gst_autoplugcache_class_init,
      (GtkObjectInitFunc)gst_autoplugcache_init,
      (GtkArgSetFunc)gst_autoplugcache_set_arg,
      (GtkArgGetFunc)gst_autoplugcache_get_arg,
      (GtkClassInitFunc)NULL,
    };
    autoplugcache_type = gtk_type_unique (GST_TYPE_ELEMENT, &autoplugcache_info);
  }
  return autoplugcache_type;
}

static void
gst_autoplugcache_class_init (GstAutoplugCacheClass *klass)
{
  GtkObjectClass *gtkobject_class;
  GstElementClass *gstelement_class;

  gtkobject_class = (GtkObjectClass*)klass;
  gstelement_class = (GstElementClass*)klass;

  parent_class = gtk_type_class (GST_TYPE_ELEMENT);

  gst_autoplugcache_signals[FIRST_BUFFER] =
    gtk_signal_new ("first_buffer", GTK_RUN_LAST, gtkobject_class->type,
                    GTK_SIGNAL_OFFSET (GstAutoplugCacheClass, first_buffer),
                    gtk_marshal_NONE__POINTER, GTK_TYPE_NONE, 1,
                    GTK_TYPE_POINTER);
  gst_autoplugcache_signals[CACHE_EMPTY] =
    gtk_signal_new ("cache_empty", GTK_RUN_LAST, gtkobject_class->type,
                    GTK_SIGNAL_OFFSET (GstAutoplugCacheClass, cache_empty),
                    gtk_marshal_NONE__NONE, GTK_TYPE_NONE, 0);
  gtk_object_class_add_signals (gtkobject_class, gst_autoplugcache_signals, LAST_SIGNAL);

  gtk_object_add_arg_type ("GstAutoplugCache::buffer_count", GTK_TYPE_INT,
                           GTK_ARG_READABLE, ARG_BUFFER_COUNT);
  gtk_object_add_arg_type ("GstAutoplugCache::caps_proxy", GTK_TYPE_BOOL,
                           GTK_ARG_READWRITE, ARG_CAPS_PROXY);
  gtk_object_add_arg_type ("GstAutoplugCache::reset", GTK_TYPE_BOOL,
                           GTK_ARG_WRITABLE, ARG_RESET);

  gtkobject_class->set_arg = gst_autoplugcache_set_arg;
  gtkobject_class->get_arg = gst_autoplugcache_get_arg;

  gstelement_class->change_state = gst_autoplugcache_change_state;
}

static void
gst_autoplugcache_init (GstAutoplugCache *cache)
{
  gst_element_set_loop_function(GST_ELEMENT(cache), GST_DEBUG_FUNCPTR(gst_autoplugcache_loop));

  cache->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
//  gst_pad_set_negotiate_function (cache->sinkpad, gst_autoplugcache_nego_sink);
  gst_element_add_pad (GST_ELEMENT(cache), cache->sinkpad);

  cache->srcpad = gst_pad_new ("src", GST_PAD_SRC);
//  gst_pad_set_negotiate_function (cache->srcpad, gst_autoplugcache_nego_src);
  gst_element_add_pad (GST_ELEMENT(cache), cache->srcpad);

  cache->caps_proxy = TRUE;	// TESTING!
        gst_pad_set_negotiate_function (cache->sinkpad, GST_DEBUG_FUNCPTR(gst_autoplugcache_nego_sink));
        gst_pad_set_negotiate_function (cache->srcpad, GST_DEBUG_FUNCPTR(gst_autoplugcache_nego_src));

  // provide a zero basis for the cache
  cache->cache = g_list_prepend(NULL, NULL);
  cache->cache_start = cache->cache;
  cache->buffer_count = 0;
  cache->current_playout = 0;
  cache->fire_empty = FALSE;
  cache->fire_first = FALSE;
}

static void
gst_autoplugcache_loop (GstElement *element)
{
  GstAutoplugCache *cache;
  GstBuffer *buf = NULL;

  cache = GST_AUTOPLUGCACHE (element);

  /* Theory:
   * The cache is a doubly-linked list.  The front of the list is the most recent
   * buffer, the end of the list is the first buffer.  The playout pointer always
   * points to the latest buffer sent out the end.  cache points to the front
   * (most reccent) of the list at all times.  cache_start points to the first 
   * buffer, i.e. the end of the list.
   * If the playout pointer does not have a prev (towards the most recent) buffer 
   * (== NULL), a buffer must be pulled from the sink pad and added to the cache.
   * When the playout pointer gets reset (as in a set_arg), the cache is walked
   * without problems, because the playout pointer has a non-NULL next.  When
   * the playout pointer hits the end of cache again it has to start pulling.
   */

  do {
    // the first time through, the current_playout pointer is going to be NULL
    if (cache->current_playout == NULL) {
      // get a buffer
      buf = gst_pad_pull (cache->sinkpad);

      // add it to the cache, though cache == NULL
      gst_buffer_ref (buf);
      cache->cache = g_list_prepend (cache->cache, buf);
      cache->buffer_count++;

      // set the current_playout pointer
      cache->current_playout = cache->cache;

      gtk_signal_emit (GTK_OBJECT(cache), gst_autoplugcache_signals[FIRST_BUFFER], buf);

      // send the buffer on its way
      gst_pad_push (cache->srcpad, buf);
    }

    // the steady state is where the playout is at the front of the cache
    else if (g_list_previous(cache->current_playout) == NULL) {

      // if we've been told to fire an empty signal (after a reset)
      if (cache->fire_empty) {
        int oldstate = GST_STATE(cache);
        fprintf(stderr,"at front of cache, about to pull, but firing signal\n");
        gst_object_ref (GST_OBJECT (cache));
        gtk_signal_emit (GTK_OBJECT(cache), gst_autoplugcache_signals[CACHE_EMPTY], NULL);
        if (GST_STATE(cache) != oldstate) {
          gst_object_ref (GST_OBJECT (cache));
          GST_DEBUG(GST_CAT_AUTOPLUG, "state changed during signal, aborting\n");
          cothread_switch(cothread_current_main());
        }
        gst_object_unref (GST_OBJECT (cache));
      }

      // get a buffer
      buf = gst_pad_pull (cache->sinkpad);

      // add it to the front of the cache
      gst_buffer_ref (buf);
      cache->cache = g_list_prepend (cache->cache, buf);
      cache->buffer_count++;

      // set the current_playout pointer
      cache->current_playout = cache->cache;

      // send the buffer on its way
      gst_pad_push (cache->srcpad, buf);
    }

    // otherwise we're trundling through existing cached buffers
    else {
      // move the current_playout pointer
      cache->current_playout = g_list_previous (cache->current_playout);

      if (cache->fire_first) {
        gtk_signal_emit (GTK_OBJECT(cache), gst_autoplugcache_signals[FIRST_BUFFER], buf);
        cache->fire_first = FALSE;
      }

      // push that buffer
      gst_pad_push (cache->srcpad, GST_BUFFER(cache->current_playout->data));
    }
  } while (!GST_FLAG_IS_SET (element, GST_ELEMENT_COTHREAD_STOPPING));
}

static GstPadNegotiateReturn
gst_autoplugcache_nego_src (GstPad *pad, GstCaps **caps, gpointer *data)
{
  GstAutoplugCache *cache = GST_AUTOPLUGCACHE (GST_PAD_PARENT (pad));

  return gst_pad_negotiate_proxy (pad, cache->sinkpad, caps);
}

static GstPadNegotiateReturn
gst_autoplugcache_nego_sink (GstPad *pad, GstCaps **caps, gpointer *data)
{
  GstAutoplugCache *cache = GST_AUTOPLUGCACHE (GST_PAD_PARENT (pad));

  return gst_pad_negotiate_proxy (pad, cache->srcpad, caps);
}


static GstElementStateReturn
gst_autoplugcache_change_state (GstElement *element)
{
  // FIXME this should do something like free the cache on ->NULL
  if (GST_ELEMENT_CLASS (parent_class)->change_state)
    return GST_ELEMENT_CLASS (parent_class)->change_state (element);

  return GST_STATE_SUCCESS;
}

static void
gst_autoplugcache_set_arg (GtkObject *object, GtkArg *arg, guint id)
{
  GstAutoplugCache *cache;

  cache = GST_AUTOPLUGCACHE (object);

  switch (id) {
    case ARG_CAPS_PROXY:
      cache->caps_proxy = GTK_VALUE_BOOL(*arg);
GST_DEBUG(0,"caps_proxy is %d\n",cache->caps_proxy);
      if (cache->caps_proxy) {
        gst_pad_set_negotiate_function (cache->sinkpad, GST_DEBUG_FUNCPTR(gst_autoplugcache_nego_sink));
        gst_pad_set_negotiate_function (cache->srcpad, GST_DEBUG_FUNCPTR(gst_autoplugcache_nego_src));
      } else {
        gst_pad_set_negotiate_function (cache->sinkpad, NULL);
        gst_pad_set_negotiate_function (cache->srcpad, NULL);
      }
      break;
    case ARG_RESET:
      // no idea why anyone would set this to FALSE, but just in case ;-)
      if (GTK_VALUE_BOOL(*arg)) {
        fprintf(stderr,"resetting playout pointer\n");
        // reset the playout pointer to the begining again
        cache->current_playout = cache->cache_start;
        // now we can fire a signal when the cache runs dry
        cache->fire_empty = TRUE;
        // also set it up to fire the first_buffer signal again
        cache->fire_first = TRUE;
      }
      break;
    default:
      break;
  }
}

static void
gst_autoplugcache_get_arg (GtkObject *object, GtkArg *arg, guint id)
{
  GstAutoplugCache *cache;

  cache = GST_AUTOPLUGCACHE (object);

  switch (id) {
    case ARG_BUFFER_COUNT:
      GTK_VALUE_INT(*arg) = cache->buffer_count;
      break;
    case ARG_CAPS_PROXY:
      GTK_VALUE_BOOL(*arg) = cache->caps_proxy;
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
  }
}

static gboolean
plugin_init (GModule *module, GstPlugin *plugin)
{
  GstElementFactory *factory;

  factory = gst_elementfactory_new ("autoplugcache", GST_TYPE_AUTOPLUGCACHE,
                                    &gst_autoplugcache_details);
  g_return_val_if_fail (factory != NULL, FALSE);

  gst_plugin_add_factory (plugin, factory);

  return TRUE;
}

GstPluginDesc plugin_desc = {
  GST_VERSION_MAJOR,
  GST_VERSION_MINOR,
  "autoplugcache",
  plugin_init
};