Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / adder / gstadder.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2001 Thomas <thomas@apestaart.org>
4  *               2005,2006 Wim Taymans <wim@fluendo.com>
5  *
6  * adder.c: Adder element, N in, one out, samples are added
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 /**
24  * SECTION:element-adder
25  *
26  * The adder allows to mix several streams into one by adding the data.
27  * Mixed data is clamped to the min/max values of the data format.
28  *
29  * The adder currently mixes all data received on the sinkpads as soon as
30  * possible without trying to synchronize the streams.
31  *
32  * <refsect2>
33  * <title>Example launch line</title>
34  * |[
35  * gst-launch audiotestsrc freq=100 ! adder name=mix ! audioconvert ! alsasink audiotestsrc freq=500 ! mix.
36  * ]| This pipeline produces two sine waves mixed together.
37  * </refsect2>
38  *
39  * Last reviewed on 2006-05-09 (0.10.7)
40  */
41 /* Element-Checklist-Version: 5 */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46 #include "gstadder.h"
47 #include <string.h>             /* strcmp */
48 #include "gstadderorc.h"
49
50 /* highest positive/lowest negative x-bit value we can use for clamping */
51 #define MAX_INT_32  ((gint32) (0x7fffffff))
52 #define MAX_INT_16  ((gint16) (0x7fff))
53 #define MAX_INT_8   ((gint8)  (0x7f))
54 #define MAX_UINT_32 ((guint32)(0xffffffff))
55 #define MAX_UINT_16 ((guint16)(0xffff))
56 #define MAX_UINT_8  ((guint8) (0xff))
57
58 #define MIN_INT_32  ((gint32) (0x80000000))
59 #define MIN_INT_16  ((gint16) (0x8000))
60 #define MIN_INT_8   ((gint8)  (0x80))
61 #define MIN_UINT_32 ((guint32)(0x00000000))
62 #define MIN_UINT_16 ((guint16)(0x0000))
63 #define MIN_UINT_8  ((guint8) (0x00))
64
65 enum
66 {
67   PROP_0,
68   PROP_FILTER_CAPS
69 };
70
71 #define GST_CAT_DEFAULT gst_adder_debug
72 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
73
74 /* elementfactory information */
75
76 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
77 #define CAPS \
78   GST_AUDIO_CAPS_MAKE ("{ S32LE, U32LE, S16LE, U16LE, S8, U8, F32LE, F64LE }")
79 #else
80 #define CAPS \
81   GST_AUDIO_CAPS_MAKE ("{ S32BE, U32BE, S16BE, U16BE, S8, U8, F32BE, F64BE }")
82 #endif
83
84 static GstStaticPadTemplate gst_adder_src_template =
85 GST_STATIC_PAD_TEMPLATE ("src",
86     GST_PAD_SRC,
87     GST_PAD_ALWAYS,
88     GST_STATIC_CAPS (CAPS)
89     );
90
91 static GstStaticPadTemplate gst_adder_sink_template =
92 GST_STATIC_PAD_TEMPLATE ("sink_%u",
93     GST_PAD_SINK,
94     GST_PAD_REQUEST,
95     GST_STATIC_CAPS (CAPS)
96     );
97
98 #define gst_adder_parent_class parent_class
99 G_DEFINE_TYPE (GstAdder, gst_adder, GST_TYPE_ELEMENT);
100
101 static void gst_adder_dispose (GObject * object);
102 static void gst_adder_set_property (GObject * object, guint prop_id,
103     const GValue * value, GParamSpec * pspec);
104 static void gst_adder_get_property (GObject * object, guint prop_id,
105     GValue * value, GParamSpec * pspec);
106
107 static gboolean gst_adder_setcaps (GstAdder * adder, GstPad * pad,
108     GstCaps * caps);
109 static gboolean gst_adder_src_query (GstPad * pad, GstObject * parent,
110     GstQuery * query);
111 static gboolean gst_adder_sink_query (GstPad * pad, GstObject * parent,
112     GstQuery * query);
113 static gboolean gst_adder_src_event (GstPad * pad, GstObject * parent,
114     GstEvent * event);
115 static gboolean gst_adder_sink_event (GstPad * pad, GstObject * parent,
116     GstEvent * event);
117
118 static GstPad *gst_adder_request_new_pad (GstElement * element,
119     GstPadTemplate * temp, const gchar * unused, const GstCaps * caps);
120 static void gst_adder_release_pad (GstElement * element, GstPad * pad);
121
122 static GstStateChangeReturn gst_adder_change_state (GstElement * element,
123     GstStateChange transition);
124
125 static GstBuffer *gst_adder_do_clip (GstCollectPads * pads,
126     GstCollectData * data, GstBuffer * buffer, gpointer user_data);
127 static GstFlowReturn gst_adder_collected (GstCollectPads * pads,
128     gpointer user_data);
129
130 /* non-clipping versions (for float) */
131 #define MAKE_FUNC_NC(name,type)                                 \
132 static void name (type *out, type *in, gint samples) {          \
133   gint i;                                                       \
134   for (i = 0; i < samples; i++)                                 \
135     out[i] += in[i];                                            \
136 }
137
138 /* *INDENT-OFF* */
139 MAKE_FUNC_NC (add_float64, gdouble)
140 /* *INDENT-ON* */
141
142 /* we can only accept caps that we and downstream can handle.
143  * if we have filtercaps set, use those to constrain the target caps.
144  */
145 static GstCaps *
146 gst_adder_sink_getcaps (GstPad * pad, GstCaps * filter)
147 {
148   GstAdder *adder;
149   GstCaps *result, *peercaps, *sinkcaps, *filter_caps;
150
151   adder = GST_ADDER (GST_PAD_PARENT (pad));
152
153   GST_OBJECT_LOCK (adder);
154   /* take filter */
155   if ((filter_caps = adder->filter_caps)) {
156     if (filter)
157       filter_caps =
158           gst_caps_intersect_full (filter, filter_caps,
159           GST_CAPS_INTERSECT_FIRST);
160     else
161       gst_caps_ref (filter_caps);
162   } else {
163     filter_caps = filter ? gst_caps_ref (filter) : NULL;
164   }
165   GST_OBJECT_UNLOCK (adder);
166
167   if (filter_caps && gst_caps_is_empty (filter_caps)) {
168     GST_WARNING_OBJECT (pad, "Empty filter caps");
169     return filter_caps;
170   }
171
172   /* get the downstream possible caps */
173   peercaps = gst_pad_peer_query_caps (adder->srcpad, filter_caps);
174
175   /* get the allowed caps on this sinkpad */
176   sinkcaps = gst_pad_get_current_caps (pad);
177   if (sinkcaps == NULL) {
178     sinkcaps = gst_pad_get_pad_template_caps (pad);
179     if (!sinkcaps)
180       sinkcaps = gst_caps_new_any ();
181   }
182
183   if (peercaps) {
184     /* if the peer has caps, intersect */
185     GST_DEBUG_OBJECT (adder, "intersecting peer and template caps");
186     result =
187         gst_caps_intersect_full (peercaps, sinkcaps, GST_CAPS_INTERSECT_FIRST);
188     gst_caps_unref (peercaps);
189     gst_caps_unref (sinkcaps);
190   } else {
191     /* the peer has no caps (or there is no peer), just use the allowed caps
192      * of this sinkpad. */
193     /* restrict with filter-caps if any */
194     if (filter_caps) {
195       GST_DEBUG_OBJECT (adder, "no peer caps, using filtered sinkcaps");
196       result =
197           gst_caps_intersect_full (filter_caps, sinkcaps,
198           GST_CAPS_INTERSECT_FIRST);
199       gst_caps_unref (sinkcaps);
200     } else {
201       GST_DEBUG_OBJECT (adder, "no peer caps, using sinkcaps");
202       result = sinkcaps;
203     }
204   }
205
206   if (filter_caps)
207     gst_caps_unref (filter_caps);
208
209   GST_LOG_OBJECT (adder, "getting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
210       GST_PAD_NAME (pad), result);
211
212   return result;
213 }
214
215 static gboolean
216 gst_adder_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
217 {
218   gboolean res = FALSE;
219
220   switch (GST_QUERY_TYPE (query)) {
221     case GST_QUERY_CAPS:
222     {
223       GstCaps *filter, *caps;
224
225       gst_query_parse_caps (query, &filter);
226       caps = gst_adder_sink_getcaps (pad, filter);
227       gst_query_set_caps_result (query, caps);
228       gst_caps_unref (caps);
229       res = TRUE;
230       break;
231     }
232     default:
233       res = gst_pad_query_default (pad, parent, query);
234       break;
235   }
236   return res;
237 }
238
239 typedef struct
240 {
241   GstPad *pad;
242   GstCaps *caps;
243 } IterData;
244
245 static void
246 setcapsfunc (const GValue * item, IterData * data)
247 {
248   GstPad *otherpad = g_value_get_object (item);
249
250   if (otherpad != data->pad)
251     gst_pad_set_caps (data->pad, data->caps);
252 }
253
254 /* the first caps we receive on any of the sinkpads will define the caps for all
255  * the other sinkpads because we can only mix streams with the same caps.
256  */
257 static gboolean
258 gst_adder_setcaps (GstAdder * adder, GstPad * pad, GstCaps * caps)
259 {
260   GstAudioInfo info;
261   GstIterator *it;
262   GstIteratorResult ires;
263   IterData idata;
264   gboolean done;
265
266   GST_LOG_OBJECT (adder, "setting caps pad %p,%s to %" GST_PTR_FORMAT, pad,
267       GST_PAD_NAME (pad), caps);
268
269   it = gst_element_iterate_pads (GST_ELEMENT_CAST (adder));
270
271   /* FIXME, see if the other pads can accept the format. Also lock the
272    * format on the other pads to this new format. */
273   idata.caps = caps;
274   idata.pad = pad;
275
276   done = FALSE;
277   while (!done) {
278     ires = gst_iterator_foreach (it, (GstIteratorForeachFunction) setcapsfunc,
279         &idata);
280
281     switch (ires) {
282       case GST_ITERATOR_RESYNC:
283         gst_iterator_resync (it);
284         break;
285       default:
286         done = TRUE;
287         break;
288     }
289   }
290
291   if (!gst_audio_info_from_caps (&info, caps))
292     goto invalid_format;
293
294   switch (GST_AUDIO_INFO_FORMAT (&info)) {
295     case GST_AUDIO_FORMAT_S8:
296       adder->func = (GstAdderFunction) add_int8;
297       break;
298     case GST_AUDIO_FORMAT_U8:
299       adder->func = (GstAdderFunction) add_uint8;
300       break;
301     case GST_AUDIO_FORMAT_S16:
302       adder->func = (GstAdderFunction) add_int16;
303       break;
304     case GST_AUDIO_FORMAT_U16:
305       adder->func = (GstAdderFunction) add_uint16;
306       break;
307     case GST_AUDIO_FORMAT_S32:
308       adder->func = (GstAdderFunction) add_int32;
309       break;
310     case GST_AUDIO_FORMAT_U32:
311       adder->func = (GstAdderFunction) add_uint32;
312       break;
313     case GST_AUDIO_FORMAT_F32:
314       adder->func = (GstAdderFunction) add_float32;
315       break;
316     case GST_AUDIO_FORMAT_F64:
317       adder->func = (GstAdderFunction) add_float64;
318       break;
319     default:
320       goto invalid_format;
321   }
322   return TRUE;
323
324   /* ERRORS */
325 invalid_format:
326   {
327     GST_DEBUG_OBJECT (adder, "invalid format set as caps");
328     return FALSE;
329   }
330 }
331
332 /* FIXME, the duration query should reflect how long you will produce
333  * data, that is the amount of stream time until you will emit EOS.
334  *
335  * For synchronized mixing this is always the max of all the durations
336  * of upstream since we emit EOS when all of them finished.
337  *
338  * We don't do synchronized mixing so this really depends on where the
339  * streams where punched in and what their relative offsets are against
340  * eachother which we can get from the first timestamps we see.
341  *
342  * When we add a new stream (or remove a stream) the duration might
343  * also become invalid again and we need to post a new DURATION
344  * message to notify this fact to the parent.
345  * For now we take the max of all the upstream elements so the simple
346  * cases work at least somewhat.
347  */
348 static gboolean
349 gst_adder_query_duration (GstAdder * adder, GstQuery * query)
350 {
351   gint64 max;
352   gboolean res;
353   GstFormat format;
354   GstIterator *it;
355   gboolean done;
356   GValue item = { 0, };
357
358   /* parse format */
359   gst_query_parse_duration (query, &format, NULL);
360
361   max = -1;
362   res = TRUE;
363   done = FALSE;
364
365   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
366   while (!done) {
367     GstIteratorResult ires;
368
369     ires = gst_iterator_next (it, &item);
370     switch (ires) {
371       case GST_ITERATOR_DONE:
372         done = TRUE;
373         break;
374       case GST_ITERATOR_OK:
375       {
376         GstPad *pad = g_value_get_object (&item);
377         gint64 duration;
378
379         /* ask sink peer for duration */
380         res &= gst_pad_peer_query_duration (pad, format, &duration);
381         /* take max from all valid return values */
382         if (res) {
383           /* valid unknown length, stop searching */
384           if (duration == -1) {
385             max = duration;
386             done = TRUE;
387           }
388           /* else see if bigger than current max */
389           else if (duration > max)
390             max = duration;
391         }
392         g_value_reset (&item);
393         break;
394       }
395       case GST_ITERATOR_RESYNC:
396         max = -1;
397         res = TRUE;
398         gst_iterator_resync (it);
399         break;
400       default:
401         res = FALSE;
402         done = TRUE;
403         break;
404     }
405   }
406   g_value_unset (&item);
407   gst_iterator_free (it);
408
409   if (res) {
410     /* and store the max */
411     GST_DEBUG_OBJECT (adder, "Total duration in format %s: %"
412         GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
413     gst_query_set_duration (query, format, max);
414   }
415
416   return res;
417 }
418
419 static gboolean
420 gst_adder_query_latency (GstAdder * adder, GstQuery * query)
421 {
422   GstClockTime min, max;
423   gboolean live;
424   gboolean res;
425   GstIterator *it;
426   gboolean done;
427   GValue item = { 0, };
428
429   res = TRUE;
430   done = FALSE;
431
432   live = FALSE;
433   min = 0;
434   max = GST_CLOCK_TIME_NONE;
435
436   /* Take maximum of all latency values */
437   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
438   while (!done) {
439     GstIteratorResult ires;
440
441     ires = gst_iterator_next (it, &item);
442     switch (ires) {
443       case GST_ITERATOR_DONE:
444         done = TRUE;
445         break;
446       case GST_ITERATOR_OK:
447       {
448         GstPad *pad = g_value_get_object (&item);
449         GstQuery *peerquery;
450         GstClockTime min_cur, max_cur;
451         gboolean live_cur;
452
453         peerquery = gst_query_new_latency ();
454
455         /* Ask peer for latency */
456         res &= gst_pad_peer_query (pad, peerquery);
457
458         /* take max from all valid return values */
459         if (res) {
460           gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
461
462           if (min_cur > min)
463             min = min_cur;
464
465           if (max_cur != GST_CLOCK_TIME_NONE &&
466               ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
467                   (max == GST_CLOCK_TIME_NONE)))
468             max = max_cur;
469
470           live = live || live_cur;
471         }
472
473         gst_query_unref (peerquery);
474         g_value_reset (&item);
475         break;
476       }
477       case GST_ITERATOR_RESYNC:
478         live = FALSE;
479         min = 0;
480         max = GST_CLOCK_TIME_NONE;
481         res = TRUE;
482         gst_iterator_resync (it);
483         break;
484       default:
485         res = FALSE;
486         done = TRUE;
487         break;
488     }
489   }
490   g_value_unset (&item);
491   gst_iterator_free (it);
492
493   if (res) {
494     /* store the results */
495     GST_DEBUG_OBJECT (adder, "Calculated total latency: live %s, min %"
496         GST_TIME_FORMAT ", max %" GST_TIME_FORMAT,
497         (live ? "yes" : "no"), GST_TIME_ARGS (min), GST_TIME_ARGS (max));
498     gst_query_set_latency (query, live, min, max);
499   }
500
501   return res;
502 }
503
504 static gboolean
505 gst_adder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
506 {
507   GstAdder *adder = GST_ADDER (parent);
508   gboolean res = FALSE;
509
510   switch (GST_QUERY_TYPE (query)) {
511     case GST_QUERY_POSITION:
512     {
513       GstFormat format;
514
515       gst_query_parse_position (query, &format, NULL);
516
517       switch (format) {
518         case GST_FORMAT_TIME:
519           /* FIXME, bring to stream time, might be tricky */
520           gst_query_set_position (query, format, adder->segment.position);
521           res = TRUE;
522           break;
523         case GST_FORMAT_DEFAULT:
524           gst_query_set_position (query, format, adder->offset);
525           res = TRUE;
526           break;
527         default:
528           break;
529       }
530       break;
531     }
532     case GST_QUERY_DURATION:
533       res = gst_adder_query_duration (adder, query);
534       break;
535     case GST_QUERY_LATENCY:
536       res = gst_adder_query_latency (adder, query);
537       break;
538     default:
539       /* FIXME, needs a custom query handler because we have multiple
540        * sinkpads */
541       res = gst_pad_query_default (pad, parent, query);
542       break;
543   }
544
545   return res;
546 }
547
548 typedef struct
549 {
550   GstEvent *event;
551   gboolean flush;
552 } EventData;
553
554 static gboolean
555 forward_event_func (const GValue * val, GValue * ret, EventData * data)
556 {
557   GstPad *pad = g_value_get_object (val);
558   GstEvent *event = data->event;
559
560   gst_event_ref (event);
561   GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
562   if (!gst_pad_push_event (pad, event)) {
563     GST_WARNING_OBJECT (pad, "Sending event  %p (%s) failed.",
564         event, GST_EVENT_TYPE_NAME (event));
565     /* quick hack to unflush the pads, ideally we need a way to just unflush
566      * this single collect pad */
567     if (data->flush)
568       gst_pad_send_event (pad, gst_event_new_flush_stop (TRUE));
569   } else {
570     g_value_set_boolean (ret, TRUE);
571     GST_LOG_OBJECT (pad, "Sent event  %p (%s).",
572         event, GST_EVENT_TYPE_NAME (event));
573   }
574
575   /* continue on other pads, even if one failed */
576   return TRUE;
577 }
578
579 /* forwards the event to all sinkpads, takes ownership of the
580  * event
581  *
582  * Returns: TRUE if the event could be forwarded on all
583  * sinkpads.
584  */
585 static gboolean
586 forward_event (GstAdder * adder, GstEvent * event, gboolean flush)
587 {
588   gboolean ret;
589   GstIterator *it;
590   GstIteratorResult ires;
591   GValue vret = { 0 };
592   EventData data;
593
594   GST_LOG_OBJECT (adder, "Forwarding event %p (%s)", event,
595       GST_EVENT_TYPE_NAME (event));
596
597   data.event = event;
598   data.flush = flush;
599
600   g_value_init (&vret, G_TYPE_BOOLEAN);
601   g_value_set_boolean (&vret, FALSE);
602   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
603   while (TRUE) {
604     ires =
605         gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func,
606         &vret, &data);
607     switch (ires) {
608       case GST_ITERATOR_RESYNC:
609         GST_WARNING ("resync");
610         gst_iterator_resync (it);
611         g_value_set_boolean (&vret, TRUE);
612         break;
613       case GST_ITERATOR_OK:
614       case GST_ITERATOR_DONE:
615         ret = g_value_get_boolean (&vret);
616         goto done;
617       default:
618         ret = FALSE;
619         goto done;
620     }
621   }
622 done:
623   gst_iterator_free (it);
624   GST_LOG_OBJECT (adder, "Forwarded event %p (%s), ret=%d", event,
625       GST_EVENT_TYPE_NAME (event), ret);
626   gst_event_unref (event);
627
628   return ret;
629 }
630
631 static gboolean
632 gst_adder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
633 {
634   GstAdder *adder;
635   gboolean result;
636
637   adder = GST_ADDER (parent);
638
639   GST_DEBUG_OBJECT (pad, "Got %s event on src pad",
640       GST_EVENT_TYPE_NAME (event));
641
642   switch (GST_EVENT_TYPE (event)) {
643     case GST_EVENT_SEEK:
644     {
645       GstSeekFlags flags;
646       gdouble rate;
647       GstSeekType curtype, endtype;
648       gint64 cur, end;
649       gboolean flush;
650
651       /* parse the seek parameters */
652       gst_event_parse_seek (event, &rate, NULL, &flags, &curtype,
653           &cur, &endtype, &end);
654
655       if ((curtype != GST_SEEK_TYPE_NONE) && (curtype != GST_SEEK_TYPE_SET)) {
656         result = FALSE;
657         GST_DEBUG_OBJECT (adder,
658             "seeking failed, unhandled seek type for start: %d", curtype);
659         goto done;
660       }
661       if ((endtype != GST_SEEK_TYPE_NONE) && (endtype != GST_SEEK_TYPE_SET)) {
662         result = FALSE;
663         GST_DEBUG_OBJECT (adder,
664             "seeking failed, unhandled seek type for end: %d", endtype);
665         goto done;
666       }
667
668       flush = (flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH;
669
670       /* check if we are flushing */
671       if (flush) {
672         /* make sure we accept nothing anymore and return WRONG_STATE */
673         gst_collect_pads_set_flushing (adder->collect, TRUE);
674
675         /* flushing seek, start flush downstream, the flush will be done
676          * when all pads received a FLUSH_STOP. */
677         gst_pad_push_event (adder->srcpad, gst_event_new_flush_start ());
678
679         /* We can't send FLUSH_STOP here since upstream could start pushing data
680          * after we unlock adder->collect.
681          * We set flush_stop_pending to TRUE instead and send FLUSH_STOP after
682          * forwarding the seek upstream or from gst_adder_collected,
683          * whichever happens first.
684          */
685         g_atomic_int_set (&adder->flush_stop_pending, TRUE);
686       }
687       GST_DEBUG_OBJECT (adder, "handling seek event: %" GST_PTR_FORMAT, event);
688
689       /* now wait for the collected to be finished and mark a new
690        * segment. After we have the lock, no collect function is running and no
691        * new collect function will be called for as long as we're flushing. */
692       GST_OBJECT_LOCK (adder->collect);
693       adder->segment.rate = rate;
694       if (curtype == GST_SEEK_TYPE_SET)
695         adder->segment.start = cur;
696       else
697         adder->segment.start = 0;
698       if (endtype == GST_SEEK_TYPE_SET)
699         adder->segment.stop = end;
700       else
701         adder->segment.stop = GST_CLOCK_TIME_NONE;
702       if (flush) {
703         /* Yes, we need to call _set_flushing again *WHEN* the streaming threads
704          * have stopped so that the cookie gets properly updated. */
705         gst_collect_pads_set_flushing (adder->collect, TRUE);
706       }
707       GST_OBJECT_UNLOCK (adder->collect);
708       GST_DEBUG_OBJECT (adder, "forwarding seek event: %" GST_PTR_FORMAT,
709           event);
710
711       /* we're forwarding seek to all upstream peers and wait for one to reply
712        * with a newsegment-event before we send a newsegment-event downstream */
713       g_atomic_int_set (&adder->wait_for_new_segment, TRUE);
714       result = forward_event (adder, event, flush);
715       if (!result) {
716         /* seek failed. maybe source is a live source. */
717         GST_DEBUG_OBJECT (adder, "seeking failed");
718       }
719       if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
720               TRUE, FALSE)) {
721         GST_DEBUG_OBJECT (adder, "pending flush stop");
722         gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop (TRUE));
723       }
724       break;
725     }
726     case GST_EVENT_QOS:
727       /* QoS might be tricky */
728       result = FALSE;
729       break;
730     case GST_EVENT_NAVIGATION:
731       /* navigation is rather pointless. */
732       result = FALSE;
733       break;
734     default:
735       /* just forward the rest for now */
736       GST_DEBUG_OBJECT (adder, "forward unhandled event: %s",
737           GST_EVENT_TYPE_NAME (event));
738       result = forward_event (adder, event, FALSE);
739       break;
740   }
741
742 done:
743
744   return result;
745 }
746
747 static gboolean
748 gst_adder_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
749 {
750   GstAdder *adder;
751   gboolean ret = TRUE;
752
753   adder = GST_ADDER (parent);
754
755   GST_DEBUG_OBJECT (pad, "Got %s event on sink pad",
756       GST_EVENT_TYPE_NAME (event));
757
758   switch (GST_EVENT_TYPE (event)) {
759     case GST_EVENT_CAPS:
760     {
761       GstCaps *caps;
762
763       gst_event_parse_caps (event, &caps);
764       ret = gst_adder_setcaps (adder, pad, caps);
765       gst_event_unref (event);
766
767       goto beach;
768     }
769     case GST_EVENT_FLUSH_STOP:
770       /* we received a flush-stop. The collect_event function will push the
771        * event past our element. We simply forward all flush-stop events, even
772        * when no flush-stop was pending, this is required because collectpads
773        * does not provide an API to handle-but-not-forward the flush-stop.
774        * We unset the pending flush-stop flag so that we don't send anymore
775        * flush-stop from the collect function later.
776        */
777       GST_OBJECT_LOCK (adder->collect);
778       g_atomic_int_set (&adder->new_segment_pending, TRUE);
779       g_atomic_int_set (&adder->flush_stop_pending, FALSE);
780       /* Clear pending tags */
781       if (adder->pending_events) {
782         g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
783         g_list_free (adder->pending_events);
784         adder->pending_events = NULL;
785       }
786       GST_OBJECT_UNLOCK (adder->collect);
787       break;
788     case GST_EVENT_TAG:
789       GST_OBJECT_LOCK (adder->collect);
790       /* collect tags here so we can push them out when we collect data */
791       adder->pending_events = g_list_append (adder->pending_events, event);
792       GST_OBJECT_UNLOCK (adder->collect);
793       goto beach;
794     case GST_EVENT_SEGMENT:
795       if (g_atomic_int_compare_and_exchange (&adder->wait_for_new_segment,
796               TRUE, FALSE)) {
797         /* make sure we push a new segment, to inform about new basetime
798          * see FIXME in gst_adder_collected() */
799         g_atomic_int_set (&adder->new_segment_pending, TRUE);
800       }
801       break;
802     default:
803       break;
804   }
805
806   /* now GstCollectPads can take care of the rest, e.g. EOS */
807   ret = adder->collect_event (pad, parent, event);
808
809 beach:
810   return ret;
811 }
812
813 static void
814 gst_adder_class_init (GstAdderClass * klass)
815 {
816   GObjectClass *gobject_class = (GObjectClass *) klass;
817   GstElementClass *gstelement_class = (GstElementClass *) klass;
818
819   gobject_class->set_property = gst_adder_set_property;
820   gobject_class->get_property = gst_adder_get_property;
821   gobject_class->dispose = gst_adder_dispose;
822
823   /**
824    * GstAdder:caps:
825    *
826    * Since: 0.10.24
827    */
828   g_object_class_install_property (gobject_class, PROP_FILTER_CAPS,
829       g_param_spec_boxed ("caps", "Target caps",
830           "Set target format for mixing (NULL means ANY). "
831           "Setting this property takes a reference to the supplied GstCaps "
832           "object.", GST_TYPE_CAPS,
833           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
834
835   gst_element_class_add_pad_template (gstelement_class,
836       gst_static_pad_template_get (&gst_adder_src_template));
837   gst_element_class_add_pad_template (gstelement_class,
838       gst_static_pad_template_get (&gst_adder_sink_template));
839   gst_element_class_set_details_simple (gstelement_class, "Adder",
840       "Generic/Audio",
841       "Add N audio channels together",
842       "Thomas Vander Stichele <thomas at apestaart dot org>");
843
844   gstelement_class->request_new_pad =
845       GST_DEBUG_FUNCPTR (gst_adder_request_new_pad);
846   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_adder_release_pad);
847   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_adder_change_state);
848 }
849
850 static void
851 gst_adder_init (GstAdder * adder)
852 {
853   GstPadTemplate *template;
854
855   template = gst_static_pad_template_get (&gst_adder_src_template);
856   adder->srcpad = gst_pad_new_from_template (template, "src");
857   gst_object_unref (template);
858
859   gst_pad_set_query_function (adder->srcpad,
860       GST_DEBUG_FUNCPTR (gst_adder_src_query));
861   gst_pad_set_event_function (adder->srcpad,
862       GST_DEBUG_FUNCPTR (gst_adder_src_event));
863   GST_PAD_SET_PROXY_CAPS (adder->srcpad);
864   gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
865
866   gst_audio_info_init (&adder->info);
867   adder->padcount = 0;
868   adder->func = NULL;
869
870   adder->filter_caps = NULL;
871
872   /* keep track of the sinkpads requested */
873   adder->collect = gst_collect_pads_new ();
874   gst_collect_pads_set_function (adder->collect,
875       GST_DEBUG_FUNCPTR (gst_adder_collected), adder);
876   gst_collect_pads_set_clip_function (adder->collect,
877       GST_DEBUG_FUNCPTR (gst_adder_do_clip), adder);
878 }
879
880 static void
881 gst_adder_dispose (GObject * object)
882 {
883   GstAdder *adder = GST_ADDER (object);
884
885   if (adder->collect) {
886     gst_object_unref (adder->collect);
887     adder->collect = NULL;
888   }
889   gst_caps_replace (&adder->filter_caps, NULL);
890   if (adder->pending_events) {
891     g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
892     g_list_free (adder->pending_events);
893     adder->pending_events = NULL;
894   }
895
896   G_OBJECT_CLASS (parent_class)->dispose (object);
897 }
898
899 static void
900 gst_adder_set_property (GObject * object, guint prop_id,
901     const GValue * value, GParamSpec * pspec)
902 {
903   GstAdder *adder = GST_ADDER (object);
904
905   switch (prop_id) {
906     case PROP_FILTER_CAPS:{
907       GstCaps *new_caps = NULL;
908       GstCaps *old_caps;
909       const GstCaps *new_caps_val = gst_value_get_caps (value);
910
911       if (new_caps_val != NULL) {
912         new_caps = (GstCaps *) new_caps_val;
913         gst_caps_ref (new_caps);
914       }
915
916       GST_OBJECT_LOCK (adder);
917       old_caps = adder->filter_caps;
918       adder->filter_caps = new_caps;
919       GST_OBJECT_UNLOCK (adder);
920
921       if (old_caps)
922         gst_caps_unref (old_caps);
923
924       GST_DEBUG_OBJECT (adder, "set new caps %" GST_PTR_FORMAT, new_caps);
925       break;
926     }
927     default:
928       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
929       break;
930   }
931 }
932
933 static void
934 gst_adder_get_property (GObject * object, guint prop_id, GValue * value,
935     GParamSpec * pspec)
936 {
937   GstAdder *adder = GST_ADDER (object);
938
939   switch (prop_id) {
940     case PROP_FILTER_CAPS:
941       GST_OBJECT_LOCK (adder);
942       gst_value_set_caps (value, adder->filter_caps);
943       GST_OBJECT_UNLOCK (adder);
944       break;
945     default:
946       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
947       break;
948   }
949 }
950
951
952 static GstPad *
953 gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
954     const gchar * unused, const GstCaps * caps)
955 {
956   gchar *name;
957   GstAdder *adder;
958   GstPad *newpad;
959   gint padcount;
960
961   if (templ->direction != GST_PAD_SINK)
962     goto not_sink;
963
964   adder = GST_ADDER (element);
965
966   /* increment pad counter */
967 #if GLIB_CHECK_VERSION(2,29,5)
968   padcount = g_atomic_int_add (&adder->padcount, 1);
969 #else
970   padcount = g_atomic_int_exchange_and_add (&adder->padcount, 1);
971 #endif
972
973   name = g_strdup_printf ("sink_%u", padcount);
974   newpad = gst_pad_new_from_template (templ, name);
975   GST_DEBUG_OBJECT (adder, "request new pad %s", name);
976   g_free (name);
977
978   gst_pad_set_query_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_sink_query));
979   gst_collect_pads_add_pad (adder->collect, newpad, sizeof (GstCollectData),
980       NULL);
981
982   /* FIXME: hacked way to override/extend the event function of
983    * GstCollectPads; because it sets its own event function giving the
984    * element no access to events */
985   adder->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
986   gst_pad_set_event_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_sink_event));
987
988   /* takes ownership of the pad */
989   if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
990     goto could_not_add;
991
992   return newpad;
993
994   /* errors */
995 not_sink:
996   {
997     g_warning ("gstadder: request new pad that is not a SINK pad\n");
998     return NULL;
999   }
1000 could_not_add:
1001   {
1002     GST_DEBUG_OBJECT (adder, "could not add pad");
1003     gst_collect_pads_remove_pad (adder->collect, newpad);
1004     gst_object_unref (newpad);
1005     return NULL;
1006   }
1007 }
1008
1009 static void
1010 gst_adder_release_pad (GstElement * element, GstPad * pad)
1011 {
1012   GstAdder *adder;
1013
1014   adder = GST_ADDER (element);
1015
1016   GST_DEBUG_OBJECT (adder, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1017
1018   gst_collect_pads_remove_pad (adder->collect, pad);
1019   gst_element_remove_pad (element, pad);
1020 }
1021
1022 static GstBuffer *
1023 gst_adder_do_clip (GstCollectPads * pads, GstCollectData * data,
1024     GstBuffer * buffer, gpointer user_data)
1025 {
1026   GstAdder *adder = GST_ADDER (user_data);
1027   gint rate, bpf;
1028
1029   rate = GST_AUDIO_INFO_RATE (&adder->info);
1030   bpf = GST_AUDIO_INFO_BPF (&adder->info);
1031
1032   buffer = gst_audio_buffer_clip (buffer, &data->segment, rate, bpf);
1033
1034   return buffer;
1035 }
1036
1037 static GstFlowReturn
1038 gst_adder_collected (GstCollectPads * pads, gpointer user_data)
1039 {
1040   /*
1041    * combine streams by adding data values
1042    * basic algorithm :
1043    * - this function is called when all pads have a buffer
1044    * - get available bytes on all pads.
1045    * - repeat for each input pad :
1046    *   - read available bytes, copy or add to target buffer
1047    *   - if there's an EOS event, remove the input channel
1048    * - push out the output buffer
1049    *
1050    * todo:
1051    * - would be nice to have a mixing mode, where instead of adding we mix
1052    *   - for float we could downscale after collect loop
1053    *   - for int we need to downscale each input to avoid clipping or
1054    *     mix into a temp (float) buffer and scale afterwards as well
1055    */
1056   GstAdder *adder;
1057   GSList *collected, *next = NULL;
1058   GstFlowReturn ret;
1059   GstBuffer *outbuf = NULL, *gapbuf = NULL;
1060   gpointer outdata = NULL;
1061   guint outsize;
1062   gint64 next_offset;
1063   gint64 next_timestamp;
1064   gint rate, bps, bpf;
1065
1066   adder = GST_ADDER (user_data);
1067
1068   /* this is fatal */
1069   if (G_UNLIKELY (adder->func == NULL))
1070     goto not_negotiated;
1071
1072   if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
1073           TRUE, FALSE)) {
1074     GST_DEBUG_OBJECT (adder, "pending flush stop");
1075     gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop (TRUE));
1076   }
1077
1078   /* get available bytes for reading, this can be 0 which could mean empty
1079    * buffers or EOS, which we will catch when we loop over the pads. */
1080   outsize = gst_collect_pads_available (pads);
1081   /* can only happen when no pads to collect or all EOS */
1082   if (outsize == 0)
1083     goto eos;
1084
1085   rate = GST_AUDIO_INFO_RATE (&adder->info);
1086   bps = GST_AUDIO_INFO_BPS (&adder->info);
1087   bpf = GST_AUDIO_INFO_BPF (&adder->info);
1088
1089   GST_LOG_OBJECT (adder,
1090       "starting to cycle through channels, %d bytes available (bps = %d)",
1091       outsize, bpf);
1092
1093   for (collected = pads->data; collected; collected = next) {
1094     GstCollectData *collect_data;
1095     GstBuffer *inbuf;
1096     gboolean is_gap;
1097
1098     /* take next to see if this is the last collectdata */
1099     next = g_slist_next (collected);
1100
1101     collect_data = (GstCollectData *) collected->data;
1102
1103     /* get a buffer of size bytes, if we get a buffer, it is at least outsize
1104      * bytes big. */
1105     inbuf = gst_collect_pads_take_buffer (pads, collect_data, outsize);
1106     /* NULL means EOS or an empty buffer so we still need to flush in
1107      * case of an empty buffer. */
1108     if (inbuf == NULL) {
1109       GST_LOG_OBJECT (adder, "channel %p: no bytes available", collect_data);
1110       continue;
1111     }
1112
1113     is_gap = GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP);
1114
1115     /* Try to make an output buffer */
1116     if (outbuf == NULL) {
1117       /* if this is a gap buffer but we have some more pads to check, skip it.
1118        * If we are at the last buffer, take it, regardless if it is a GAP
1119        * buffer or not. */
1120       if (is_gap && next) {
1121         GST_DEBUG_OBJECT (adder, "skipping, non-last GAP buffer");
1122         /* we keep the GAP buffer, if we don't have anymore buffers (all pads
1123          * EOS, we can use this one as the output buffer. */
1124         if (gapbuf == NULL)
1125           gapbuf = inbuf;
1126         else
1127           gst_buffer_unref (inbuf);
1128         continue;
1129       }
1130
1131       GST_LOG_OBJECT (adder, "channel %p: preparing output buffer of %d bytes",
1132           collect_data, outsize);
1133
1134       outdata = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
1135     } else {
1136       if (!is_gap) {
1137         /* we had a previous output buffer, mix this non-GAP buffer */
1138         guint8 *indata;
1139         gsize insize;
1140
1141         indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
1142
1143         /* all buffers should have outsize, there are no short buffers because we
1144          * asked for the max size above */
1145         g_assert (insize == outsize);
1146
1147         GST_LOG_OBJECT (adder, "channel %p: mixing %" G_GSIZE_FORMAT " bytes"
1148             " from data %p", collect_data, insize, indata);
1149
1150         /* further buffers, need to add them */
1151         adder->func ((gpointer) outdata, (gpointer) indata, insize / bps);
1152         gst_buffer_unmap (inbuf, indata, insize);
1153       } else {
1154         /* skip gap buffer */
1155         GST_LOG_OBJECT (adder, "channel %p: skipping GAP buffer", collect_data);
1156       }
1157       gst_buffer_unref (inbuf);
1158     }
1159   }
1160   if (outbuf)
1161     gst_buffer_unmap (outbuf, outdata, outsize);
1162
1163   if (outbuf == NULL) {
1164     /* no output buffer, reuse one of the GAP buffers then if we have one */
1165     if (gapbuf) {
1166       GST_LOG_OBJECT (adder, "reusing GAP buffer %p", gapbuf);
1167       outbuf = gapbuf;
1168     } else
1169       /* assume EOS otherwise, this should not happen, really */
1170       goto eos;
1171   } else if (gapbuf)
1172     /* we had an output buffer, unref the gapbuffer we kept */
1173     gst_buffer_unref (gapbuf);
1174
1175   if (g_atomic_int_compare_and_exchange (&adder->new_segment_pending, TRUE,
1176           FALSE)) {
1177     GstEvent *event;
1178
1179     /* FIXME, use rate/applied_rate as set on all sinkpads.
1180      * - currently we just set rate as received from last seek-event
1181      *
1182      * When seeking we set the start and stop positions as given in the seek
1183      * event. We also adjust offset & timestamp acordingly.
1184      * This basically ignores all newsegments sent by upstream.
1185      */
1186     event = gst_event_new_segment (&adder->segment);
1187
1188     if (adder->segment.rate > 0.0) {
1189       adder->segment.position = adder->segment.start;
1190     } else {
1191       adder->segment.position = adder->segment.stop;
1192     }
1193     adder->offset = gst_util_uint64_scale (adder->segment.position,
1194         rate, GST_SECOND);
1195     GST_INFO_OBJECT (adder, "seg_start %" G_GUINT64_FORMAT ", seg_end %"
1196         G_GUINT64_FORMAT, adder->segment.start, adder->segment.stop);
1197     GST_INFO_OBJECT (adder, "timestamp %" G_GINT64_FORMAT ",new offset %"
1198         G_GINT64_FORMAT, adder->segment.position, adder->offset);
1199
1200     if (event) {
1201       if (!gst_pad_push_event (adder->srcpad, event)) {
1202         GST_WARNING_OBJECT (adder->srcpad, "Sending event failed");
1203       }
1204     } else {
1205       GST_WARNING_OBJECT (adder->srcpad, "Creating new segment event for "
1206           "start:%" G_GINT64_FORMAT "  end:%" G_GINT64_FORMAT " failed",
1207           adder->segment.start, adder->segment.stop);
1208     }
1209   }
1210
1211   if (G_UNLIKELY (adder->pending_events)) {
1212     GList *tmp = adder->pending_events;
1213
1214     while (tmp) {
1215       GstEvent *ev = (GstEvent *) tmp->data;
1216
1217       gst_pad_push_event (adder->srcpad, ev);
1218       tmp = g_list_next (tmp);
1219     }
1220     g_list_free (adder->pending_events);
1221     adder->pending_events = NULL;
1222   }
1223
1224   /* for the next timestamp, use the sample counter, which will
1225    * never accumulate rounding errors */
1226   if (adder->segment.rate > 0.0) {
1227     next_offset = adder->offset + outsize / bpf;
1228   } else {
1229     next_offset = adder->offset - outsize / bpf;
1230   }
1231   next_timestamp = gst_util_uint64_scale (next_offset, GST_SECOND, rate);
1232
1233
1234   /* set timestamps on the output buffer */
1235   if (adder->segment.rate > 0.0) {
1236     GST_BUFFER_TIMESTAMP (outbuf) = adder->segment.position;
1237     GST_BUFFER_OFFSET (outbuf) = adder->offset;
1238     GST_BUFFER_OFFSET_END (outbuf) = next_offset;
1239     GST_BUFFER_DURATION (outbuf) = next_timestamp - adder->segment.position;
1240   } else {
1241     GST_BUFFER_TIMESTAMP (outbuf) = next_timestamp;
1242     GST_BUFFER_OFFSET (outbuf) = next_offset;
1243     GST_BUFFER_OFFSET_END (outbuf) = adder->offset;
1244     GST_BUFFER_DURATION (outbuf) = adder->segment.position - next_timestamp;
1245   }
1246
1247   adder->offset = next_offset;
1248   adder->segment.position = next_timestamp;
1249
1250   /* send it out */
1251   GST_LOG_OBJECT (adder, "pushing outbuf %p, timestamp %" GST_TIME_FORMAT
1252       " offset %" G_GINT64_FORMAT, outbuf,
1253       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1254       GST_BUFFER_OFFSET (outbuf));
1255   ret = gst_pad_push (adder->srcpad, outbuf);
1256
1257   GST_LOG_OBJECT (adder, "pushed outbuf, result = %s", gst_flow_get_name (ret));
1258
1259   return ret;
1260
1261   /* ERRORS */
1262 not_negotiated:
1263   {
1264     GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
1265         ("Unknown data received, not negotiated"));
1266     return GST_FLOW_NOT_NEGOTIATED;
1267   }
1268 eos:
1269   {
1270     GST_DEBUG_OBJECT (adder, "no data available, must be EOS");
1271     gst_pad_push_event (adder->srcpad, gst_event_new_eos ());
1272     return GST_FLOW_EOS;
1273   }
1274 }
1275
1276 static GstStateChangeReturn
1277 gst_adder_change_state (GstElement * element, GstStateChange transition)
1278 {
1279   GstAdder *adder;
1280   GstStateChangeReturn ret;
1281
1282   adder = GST_ADDER (element);
1283
1284   switch (transition) {
1285     case GST_STATE_CHANGE_NULL_TO_READY:
1286       break;
1287     case GST_STATE_CHANGE_READY_TO_PAUSED:
1288       adder->segment.position = 0;
1289       adder->offset = 0;
1290       adder->flush_stop_pending = FALSE;
1291       adder->new_segment_pending = TRUE;
1292       adder->wait_for_new_segment = FALSE;
1293       gst_segment_init (&adder->segment, GST_FORMAT_TIME);
1294       gst_collect_pads_start (adder->collect);
1295       break;
1296     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1297       break;
1298     case GST_STATE_CHANGE_PAUSED_TO_READY:
1299       /* need to unblock the collectpads before calling the
1300        * parent change_state so that streaming can finish */
1301       gst_collect_pads_stop (adder->collect);
1302       break;
1303     default:
1304       break;
1305   }
1306
1307   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1308
1309   switch (transition) {
1310     default:
1311       break;
1312   }
1313
1314   return ret;
1315 }
1316
1317
1318 static gboolean
1319 plugin_init (GstPlugin * plugin)
1320 {
1321   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "adder", 0,
1322       "audio channel mixing element");
1323
1324   gst_adder_orc_init ();
1325
1326   if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
1327     return FALSE;
1328   }
1329
1330   return TRUE;
1331 }
1332
1333 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1334     GST_VERSION_MINOR,
1335     "adder",
1336     "Adds multiple streams",
1337     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)