gstlal_insertgap.c 38.4 KB
Newer Older
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
/*
 * Copyright (C) 2016 Aaron Viets <aaron.viets@ligo.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


/**
 * SECTION:gstlal_insertgap
 * @short_description: This element replaces undesired values, specified
 * by the property #bad_data_intervals, with gaps.
 *
 * The primary purpose of this element is to reject certain values
 * considered by the user to be "bad data." This can be done by flagging
 * that data as a gap, replacing it with a specified value, or both. The
28 29
 * criteria for bad data is specified by the array property
 * #bad_data_intervals. Array indices 0, 2, 4, etc.,
30 31 32 33 34
 * represent maxima, and array indices 1, 3, 5, etc., represent the 
 * corresponding minima. For example, if 
 * #bad_data_intervals = [0, 1, 2, 3],
 * then any values that fall in one of the closed intervals [-inf, 0],
 * [1, 2], or [3, inf] will be rejected and gapped and/or replaced
35
 * as specified by the user. To reject a single value, say zero,
36
 * #bad_data_intervals should be [-max_double, 0, 0, max_double].
37 38 39 40
 * If the data stream is complex, the real and and imaginary parts of
 * each input is tested, and if either is bad, the value is rejected.
 * The #bad_data_intervals and #replace_value properties are applied to
 * both real and imaginary parts.
41 42 43
 * This element also has the ability to fill in discontinuities if the
 * property #fill-discont is set to true. Presentation timestamps and
 * buffer offsets are adjusted as needed.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 */


/*
 * ============================================================================
 *
 *				  Preamble
 *
 * ============================================================================
 */


/*
 * stuff from C
 */


61
#define _XOPEN_SOURCE
62 63
#include <math.h>
#include <string.h>
64
#include <complex.h>
65 66 67
#include <time.h>
#include <pthread.h>
#include <unistd.h>
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86


/*
 * stuff from gobject/gstreamer
 */


#include <glib.h>
#include <gst/gst.h>
#include <gst/audio/audio.h>


/*
 * our own stuff
 */


#include <gstlal/gstlal.h>
#include <gstlal/gstlal_debug.h>
87
#include <gstlal/gstlal_audio_info.h>
88
#include <gstlal_insertgap.h>
89
#include <lal/Date.h>
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107


/*
 * ============================================================================
 *
 *			   GStreamer Boiler Plate
 *
 * ============================================================================
 */


static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE(
	GST_BASE_TRANSFORM_SINK_NAME,
	GST_PAD_SINK,
	GST_PAD_ALWAYS,
	GST_STATIC_CAPS(
		"audio/x-raw, " \
		"rate = (int) [1, MAX], " \
108
		"channels = (int) [1, MAX], " \
109
		"format = (string) { "GST_AUDIO_NE(U32)", " GST_AUDIO_NE(F32) ", " GST_AUDIO_NE(F64) ", " GST_AUDIO_NE(Z64) ", " GST_AUDIO_NE(Z128) " }, " \
110 111 112 113 114 115 116 117 118 119 120 121 122
		"layout = (string) interleaved, " \
		"channel-mask = (bitmask) 0"
	)
);


static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE(
	GST_BASE_TRANSFORM_SRC_NAME,
	GST_PAD_SRC,
	GST_PAD_ALWAYS,
	GST_STATIC_CAPS(
		"audio/x-raw, " \
		"rate = (int) [1, MAX], " \
123
		"channels = (int) [1, MAX], " \
124
		"format = (string) { "GST_AUDIO_NE(U32)", " GST_AUDIO_NE(F32) ", " GST_AUDIO_NE(F64) ", " GST_AUDIO_NE(Z64) ", " GST_AUDIO_NE(Z128) " }, " \
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
		"layout = (string) interleaved, " \
		"channel-mask = (bitmask) 0"
	)
);


#define GST_CAT_DEFAULT gstlal_insertgap_debug
GST_DEBUG_CATEGORY_STATIC(GST_CAT_DEFAULT);


G_DEFINE_TYPE_WITH_CODE(
	GSTLALInsertGap,
	gstlal_insertgap,
	GST_TYPE_ELEMENT,
	GST_DEBUG_CATEGORY_INIT(GST_CAT_DEFAULT, "lal_insertgap", 0, "lal_insertgap element")
);


/*
 * ============================================================================
 *
 *			       Utilities
 *
 * ============================================================================
 */


152 153 154 155 156 157
#define DEFINE_CHECK_DATA(DTYPE) \
static gboolean check_data_ ## DTYPE(DTYPE *data, double *bad_data_intervals, int array_length, int num_checks, gboolean remove_nan, gboolean remove_inf) { \
	int i, j; \
	gboolean result = TRUE; \
	for(i = 0; i < num_checks; i++) { \
		if(bad_data_intervals) { \
158
			result = FALSE; \
159
			for(j = 0; j < array_length; j += 2) \
160
				result |= data[i] > bad_data_intervals[j] && data[i] < bad_data_intervals[j + 1]; \
161 162
		} \
		if(remove_nan) \
163
			result &= !isnan((double) data[i]); \
164
		if(remove_inf) \
165
			result &= !isinf((double) data[i]); \
166 167
	} \
	return result; \
168 169 170
}


171 172 173 174 175
DEFINE_CHECK_DATA(float);
DEFINE_CHECK_DATA(double);
DEFINE_CHECK_DATA(guint32);


176
#define DEFINE_PROCESS_INBUF(DTYPE,COMPLEX) \
177
static GstFlowReturn process_inbuf_ ## DTYPE ## COMPLEX(const DTYPE COMPLEX *indata, DTYPE COMPLEX *outdata, GSTLALInsertGap *element, gboolean sinkbuf_gap, gboolean sinkbuf_discont, guint64 sinkbuf_offset, guint64 sinkbuf_offset_end, GstClockTime sinkbuf_dur, GstClockTime sinkbuf_pts, gboolean complex_data) \
178 179
{ \
	GstFlowReturn result = GST_FLOW_OK; \
180
	guint64 blocks, max_block_length; \
181 182 183 184 185
 \
	/*
	 * First, deal with discontinuity if necessary
	 */ \
	if(element->fill_discont && (element->last_sinkbuf_offset_end != 0) && (sinkbuf_pts != element->last_sinkbuf_ets)) { \
186 187 188
 \
		guint64 standard_blocks, last_block_length, buffer_num, sample_num, missing_samples = 0; \
		DTYPE COMPLEX sample_value; \
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
 \
		/* Track discont length and number of zero-length buffers */ \
		element->discont_time += (sinkbuf_pts - element->last_sinkbuf_ets); \
		element->empty_bufs += (sinkbuf_dur ? 0 : 1); \
 \
		/* Find number of missing samples and max block length in samples */ \
		missing_samples = gst_util_uint64_scale_int_round(sinkbuf_pts - element->last_sinkbuf_ets, element->rate, 1000000000); \
		blocks = (sinkbuf_pts - element->last_sinkbuf_ets + element->block_duration - 1) / element->block_duration; /* ceil */ \
		g_assert_cmpuint(blocks, >, 0); /* make sure that the discont is not zero length */ \
		max_block_length = (missing_samples + blocks - 1) / blocks; /* ceil */ \
		g_assert_cmpuint(max_block_length, >, 0); \
 \
		/* Message for debugging */ \
		if(sinkbuf_dur && sinkbuf_offset != sinkbuf_offset_end) \
			GST_WARNING_OBJECT(element, "filling discontinuity lasting %f seconds (%lu samples) including %lu zero-length buffers and starting at %f seconds (offset %lu)", (((double) element->discont_time) / 1000000000.0), gst_util_uint64_scale_int_round(element->discont_time, element->rate, 1000000000), element->empty_bufs, (double) sinkbuf_pts / 1000000000.0 - (double) element->discont_time / 1000000000.0, sinkbuf_offset); \
 \
205 206
		standard_blocks = missing_samples / max_block_length; \
		last_block_length = missing_samples % max_block_length; \
207 208 209 210 211 212 213 214 215 216
		if(complex_data) \
			sample_value = (element->replace_value < G_MAXDOUBLE) ? ((DTYPE) (element->replace_value)) * (1 + I) : 0; \
		else \
			sample_value = (element->replace_value < G_MAXDOUBLE) ? ((DTYPE) (element->replace_value)) : 0; \
 \
		/* first make and push any buffers of size max_buffer_size */ \
		if(standard_blocks != 0) { \
			for(buffer_num = 0; buffer_num < standard_blocks; buffer_num++) { \
				GstBuffer *discont_buf; \
				DTYPE COMPLEX *discont_buf_data; \
217 218
				discont_buf_data = g_malloc(max_block_length * element->channels * sizeof(DTYPE COMPLEX)); \
				for(sample_num = 0; sample_num < max_block_length * element->channels; sample_num++) { \
219 220 221
					*discont_buf_data = sample_value; \
					discont_buf_data++; \
				} \
222
				discont_buf = gst_buffer_new_wrapped((discont_buf_data - max_block_length * element->channels), max_block_length * element->channels * sizeof(DTYPE COMPLEX)); \
223 224 225 226 227 228 229 230 231 232 233
				if(G_UNLIKELY(!discont_buf)) { \
					GST_ERROR_OBJECT(element, "failure creating sub-buffer"); \
					result = GST_FLOW_ERROR; \
					goto done; \
				} \
 \
				/* set flags, caps, offset, and timestamps. */ \
				GST_BUFFER_OFFSET(discont_buf) = element->last_sinkbuf_offset_end + element->discont_offset + buffer_num * max_block_length; \
				GST_BUFFER_OFFSET_END(discont_buf) = GST_BUFFER_OFFSET(discont_buf) + max_block_length; \
				GST_BUFFER_PTS(discont_buf) = element->last_sinkbuf_ets + gst_util_uint64_scale_round(sinkbuf_pts - element->last_sinkbuf_ets, (guint64) buffer_num * max_block_length, missing_samples); \
				GST_BUFFER_DURATION(discont_buf) = element->last_sinkbuf_ets + gst_util_uint64_scale_round(sinkbuf_pts - element->last_sinkbuf_ets, ((guint64) buffer_num + 1) * max_block_length, missing_samples) - GST_BUFFER_PTS(discont_buf); \
234
				GST_BUFFER_FLAG_UNSET(discont_buf, GST_BUFFER_FLAG_DISCONT); \
235 236
				if(element->insert_gap) \
					GST_BUFFER_FLAG_SET(discont_buf, GST_BUFFER_FLAG_GAP); \
237 238
				else \
					GST_BUFFER_FLAG_UNSET(discont_buf, GST_BUFFER_FLAG_GAP); \
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
 \
				/* push buffer downstream */ \
				GST_DEBUG_OBJECT(element, "pushing sub-buffer %" GST_BUFFER_BOUNDARIES_FORMAT, GST_BUFFER_BOUNDARIES_ARGS(discont_buf)); \
				result = gst_pad_push(element->srcpad, discont_buf); \
				if(G_UNLIKELY(result != GST_FLOW_OK)) { \
					GST_WARNING_OBJECT(element, "push failed: %s", gst_flow_get_name(result)); \
					goto done; \
				} \
			} \
		} \
 \
		/* then make and push the remainder buffer */ \
		if(last_block_length != 0) { \
			GstBuffer *last_discont_buf; \
			DTYPE COMPLEX *last_discont_buf_data; \
254
			last_discont_buf_data = g_malloc(last_block_length * element->channels * sizeof(DTYPE COMPLEX)); \
255
			guint sample_num; \
256
			for(sample_num = 0; sample_num < last_block_length * element->channels; sample_num++) { \
257 258 259
				*last_discont_buf_data = sample_value; \
				last_discont_buf_data++; \
			} \
260
			last_discont_buf = gst_buffer_new_wrapped((last_discont_buf_data - last_block_length * element->channels), last_block_length * element->channels * sizeof(DTYPE COMPLEX)); \
261 262 263 264 265 266 267 268 269 270 271
			if(G_UNLIKELY(!last_discont_buf)) { \
				GST_ERROR_OBJECT(element, "failure creating sub-buffer"); \
				result = GST_FLOW_ERROR; \
				goto done; \
			} \
 \
			/* set flags, caps, offset, and timestamps. */ \
			GST_BUFFER_OFFSET(last_discont_buf) = element->last_sinkbuf_offset_end + element->discont_offset + missing_samples - last_block_length; \
			GST_BUFFER_OFFSET_END(last_discont_buf) = GST_BUFFER_OFFSET(last_discont_buf) + last_block_length; \
			GST_BUFFER_PTS(last_discont_buf) = element->last_sinkbuf_ets + gst_util_uint64_scale_round(sinkbuf_pts - element->last_sinkbuf_ets, missing_samples - last_block_length, missing_samples); \
			GST_BUFFER_DURATION(last_discont_buf) = sinkbuf_pts - GST_BUFFER_PTS(last_discont_buf); \
272
			GST_BUFFER_FLAG_UNSET(last_discont_buf, GST_BUFFER_FLAG_DISCONT); \
273 274
			if(element->insert_gap) \
				GST_BUFFER_FLAG_SET(last_discont_buf, GST_BUFFER_FLAG_GAP); \
275 276
			else \
				GST_BUFFER_FLAG_UNSET(last_discont_buf, GST_BUFFER_FLAG_GAP); \
277 278 279 280 281 282 283 284 285
 \
			/* push buffer downstream */ \
			GST_DEBUG_OBJECT(element, "pushing sub-buffer %" GST_BUFFER_BOUNDARIES_FORMAT, GST_BUFFER_BOUNDARIES_ARGS(last_discont_buf)); \
			result = gst_pad_push(element->srcpad, last_discont_buf); \
			if(G_UNLIKELY(result != GST_FLOW_OK)) { \
				GST_WARNING_OBJECT(element, "push failed: %s", gst_flow_get_name(result)); \
				goto done; \
			} \
		} \
286
		element->discont_offset += missing_samples; \
287
	} \
288 289 290 291 292
	if(!sinkbuf_dur) \
		goto done; \
 \
	element->discont_time = 0; \
	element->empty_bufs = 0; \
293 294 295 296
 \
	/*
	 * Now, use data on input buffer to make next output buffer(s)
	 */ \
297
	gboolean data_is_bad, srcbuf_gap, srcbuf_gap_next; \
298 299
	guint64 offset, length, current_srcbuf_length = 0; \
	int i; \
300 301 302 303
 \
	/* compute length of incoming buffer and maximum block length in samples */ \
	blocks = (sinkbuf_dur + element->block_duration - 1) / element->block_duration; /* ceil */ \
	g_assert_cmpuint(blocks, >, 0); /* make sure that the sinkbuf is not zero length */ \
304
	length = sinkbuf_offset_end - sinkbuf_offset; \
305 306 307 308
	max_block_length = (length + blocks - 1) / blocks; /* ceil */ \
	g_assert_cmpuint(max_block_length, >, 0); \
 \
	/* Check first sample */ \
309
	data_is_bad = !check_data_ ## DTYPE((DTYPE *) indata, element->bad_data_intervals, element->array_length, (1 + (int) complex_data) * element->channels, element->remove_nan, element->remove_inf); \
310
	srcbuf_gap = (sinkbuf_gap && (!(element->remove_gap))) || ((element->insert_gap) && data_is_bad); \
311
	for(offset = 0; offset < length; offset++) { \
312
		data_is_bad = !check_data_ ## DTYPE((DTYPE *) indata, element->bad_data_intervals, element->array_length, (1 + (int) complex_data) * element->channels, element->remove_nan, element->remove_inf); \
313
		srcbuf_gap_next = (sinkbuf_gap && (!(element->remove_gap))) || ((element->insert_gap) && data_is_bad); \
314 315 316 317 318 319 320
		if(complex_data) { \
			for(i = 0; i < element->channels; i++) \
				outdata[i] = (((element->replace_value) < G_MAXDOUBLE) && data_is_bad) ? ((DTYPE) (element->replace_value)) * (1 + I) : indata[i]; \
		} else { \
			for(i = 0; i < element->channels; i++) \
				outdata[i] = (((element->replace_value) < G_MAXDOUBLE) && data_is_bad) ? (DTYPE) (element->replace_value) : indata[i]; \
		} \
321
		current_srcbuf_length++; \
322 323
		indata += element->channels; \
		outdata += element->channels; \
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
 \
		/* 
		 * We need to push an output buffer if:
		 * 1) The number of samples to be output equals the maximum output buffer size
		 * 2) We have reached the end of the input buffer 
		 * 3) The output data changes from non-gap to gap or vice-versa
		 */ \
		if((current_srcbuf_length >= max_block_length) || (offset >= length - 1) || (srcbuf_gap_next != srcbuf_gap)) { \
			if((current_srcbuf_length > max_block_length) || (offset > length - 1)) \
				g_assert_not_reached(); \
			if(srcbuf_gap_next != srcbuf_gap) { \
				/*
				 * In this case, we don't want the most recent sample since its
				 * gap state is different. Put it on the next output buffer.
				 */ \
				offset--; \
				current_srcbuf_length--; \
341 342
				indata -= element->channels; \
				outdata -= element->channels; \
343 344
			} \
			GstBuffer *srcbuf; \
345
			DTYPE COMPLEX *srcbuf_data; \
346 347 348
			srcbuf_data = g_malloc(current_srcbuf_length * element->channels * sizeof(DTYPE COMPLEX)); \
			memcpy(srcbuf_data, (outdata - current_srcbuf_length * element->channels), current_srcbuf_length * element->channels * sizeof(DTYPE COMPLEX)); \
			srcbuf = gst_buffer_new_wrapped(srcbuf_data, current_srcbuf_length * element->channels * sizeof(DTYPE COMPLEX)); \
349 350
 \
			if(G_UNLIKELY(!srcbuf)) { \
351
				GST_ERROR_OBJECT(element, "failure creating sub-buffer"); \
352
				result = GST_FLOW_ERROR; \
353
				goto done; \
354 355 356
			} \
 \
			/* set flags, caps, offset, and timestamps. */ \
357
			GST_BUFFER_OFFSET(srcbuf) = sinkbuf_offset + element->discont_offset + offset + 1 - current_srcbuf_length; \
358
			GST_BUFFER_OFFSET_END(srcbuf) = GST_BUFFER_OFFSET(srcbuf) + current_srcbuf_length; \
359
			GST_BUFFER_PTS(srcbuf) = sinkbuf_pts + gst_util_uint64_scale_int_round(sinkbuf_dur, offset + 1 - current_srcbuf_length, length); \
360 361 362 363 364 365 366 367 368 369
			GST_BUFFER_DURATION(srcbuf) = sinkbuf_pts + gst_util_uint64_scale_int_round(sinkbuf_dur, offset + 1, length) - GST_BUFFER_PTS(srcbuf); \
			if(srcbuf_gap) \
				GST_BUFFER_FLAG_SET(srcbuf, GST_BUFFER_FLAG_GAP); \
			else \
				GST_BUFFER_FLAG_UNSET(srcbuf, GST_BUFFER_FLAG_GAP); \
 \
			/*
			 * only the first subbuffer of a buffer flagged as a
			 * discontinuity is a discontinuity.
			 */ \
370
			if(sinkbuf_discont && (offset + 1 - current_srcbuf_length == 0) && ((!(element->fill_discont)) || (element->last_sinkbuf_ets == 0))) \
371
				GST_BUFFER_FLAG_SET(srcbuf, GST_BUFFER_FLAG_DISCONT); \
372 373
			else \
				GST_BUFFER_FLAG_UNSET(srcbuf, GST_BUFFER_FLAG_DISCONT); \
374 375 376
			if(srcbuf_gap_next != srcbuf_gap) { \
				/* We need to reset our place in the input buffer */ \
				offset++; \
377 378
				indata += element->channels; \
				outdata += element->channels; \
379 380 381 382 383 384
				current_srcbuf_length = 1; \
				srcbuf_gap = srcbuf_gap_next; \
			} else \
				current_srcbuf_length = 0; \
 \
			/* push buffer downstream */ \
385 386
			GST_DEBUG_OBJECT(element, "pushing sub-buffer %" GST_BUFFER_BOUNDARIES_FORMAT, GST_BUFFER_BOUNDARIES_ARGS(srcbuf)); \
			result = gst_pad_push(element->srcpad, srcbuf); \
387
			if(G_UNLIKELY(result != GST_FLOW_OK)) { \
388 389
				GST_WARNING_OBJECT(element, "push failed: %s", gst_flow_get_name(result)); \
				goto done; \
390 391 392
			} \
		} \
	} \
393 394
done: \
	element->last_sinkbuf_ets = sinkbuf_pts + sinkbuf_dur; \
395
	element->last_sinkbuf_offset_end = sinkbuf_offset_end ? sinkbuf_offset_end : element->last_sinkbuf_offset_end; \
396 397 398 399
	return result; \
}


400
DEFINE_PROCESS_INBUF(guint32, )
401 402 403 404
DEFINE_PROCESS_INBUF(float, )
DEFINE_PROCESS_INBUF(double, )
DEFINE_PROCESS_INBUF(float,complex)
DEFINE_PROCESS_INBUF(double,complex)
405 406


407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
static void *input_buffer_timer(void *void_element) {

	/* Give the element a few seconds to get started before barfing out fake data */
	sleep(5);

	GSTLALInsertGap *element = GSTLAL_INSERTGAP(void_element);

	while(TRUE) {
		/*
		 * When element->wait_time > 0, this function runs continuously on a
		 * separate thread, checking the timestamps of input buffers,
		 * comparing them against real time.  If the latest timestamp in the 
		 * input data is more than the wait time behind real time, we will
		 * push buffers from the source pad.
		 */

		/* don't hog a billion CPUs */
		sleep(1);

		/* Get the current real time as a string */
		GstDateTime *current_gst_time = gst_date_time_new_now_utc();
		gchar *current_utc_time = gst_date_time_to_iso8601_string(current_gst_time);

		/* parse DateTime to gps time */
		struct tm tm;
		strptime(current_utc_time, "%Y-%m-%dT%H:%M:%SZ", &tm);

		/* time in nanoseconds */
		guint64 current_time = (guint64) XLALUTCToGPS(&tm) * 1000000000 + (guint64) gst_date_time_get_microsecond(current_gst_time) * 1000;

		if(element->last_sinkbuf_ets + element->wait_time < current_time) {
			/* Then we need to push a buffer */
			g_mutex_lock(&element->mutex);
			GstFlowReturn result;
			switch(element->data_type) {
			case GSTLAL_INSERTGAP_U32:
				result = process_inbuf_guint32(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, current_time - element->wait_time, FALSE);
				break;
			case GSTLAL_INSERTGAP_F32:
				result = process_inbuf_float(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, current_time - element->wait_time, FALSE);
				break;
			case GSTLAL_INSERTGAP_F64:
				result = process_inbuf_double(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, current_time - element->wait_time, FALSE);
				break;
			case GSTLAL_INSERTGAP_Z64:
				result = process_inbuf_floatcomplex(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, current_time - element->wait_time, TRUE);
				break;
			case GSTLAL_INSERTGAP_Z128:
				result = process_inbuf_doublecomplex(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, current_time - element->wait_time, TRUE);
				break;
			default:
				g_assert_not_reached();
			}

			if(G_UNLIKELY(result != GST_FLOW_OK))
				GST_WARNING_OBJECT(element, "push failed in function input_buffer_timer(): %s", gst_flow_get_name(result));

			g_mutex_unlock(&element->mutex);
		}

		if(element->finished_running)
			pthread_exit(NULL);
	}
}


473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
/*
 * ============================================================================
 *
 *				     Sink Pad
 *
 * ============================================================================
 */


/*
 * sink_event()
 */


static gboolean sink_event(GstPad *pad, GstObject *parent, GstEvent *event)
{
	GSTLALInsertGap *element = GSTLAL_INSERTGAP(parent);
	gboolean success = TRUE;
	GST_DEBUG_OBJECT(pad, "Got %s event on sink pad", GST_EVENT_TYPE_NAME(event));

	switch(GST_EVENT_TYPE(event)) {
	case GST_EVENT_CAPS: {
		GstCaps *caps;
		GstAudioInfo info;
		gst_event_parse_caps(event, &caps);
498 499 500 501
		success &= gstlal_audio_info_from_caps(&info, caps);
		GstStructure *str = gst_caps_get_structure(caps, 0);
		const gchar *name = gst_structure_get_string(str, "format");
		success &= (name != NULL);
502
		if(success) {
503
			/* record stream parameters */
504
			element->rate = GST_AUDIO_INFO_RATE(&info);
505
			element->channels = GST_AUDIO_INFO_CHANNELS(&info);
506
			element->unit_size = GST_AUDIO_INFO_BPF(&info);
507 508
			if(!strcmp(name, GST_AUDIO_NE(U32))) {
				element->data_type = GSTLAL_INSERTGAP_U32;
509
				g_assert_cmpuint(element->unit_size, ==, element->channels * 4);
510
			} else if(!strcmp(name, GST_AUDIO_NE(F32))) {
511
				element->data_type = GSTLAL_INSERTGAP_F32;
512
				g_assert_cmpuint(element->unit_size, ==, element->channels * 4);
513 514
			} else if(!strcmp(name, GST_AUDIO_NE(F64))) {
				element->data_type = GSTLAL_INSERTGAP_F64;
515
				g_assert_cmpuint(element->unit_size, ==, element->channels * 8);
516 517
			} else if(!strcmp(name, GST_AUDIO_NE(Z64))) {
				element->data_type = GSTLAL_INSERTGAP_Z64;
518
				g_assert_cmpuint(element->unit_size, ==, element->channels * 8);
519 520
			} else if(!strcmp(name, GST_AUDIO_NE(Z128))) {
				element->data_type = GSTLAL_INSERTGAP_Z128;
521
				g_assert_cmpuint(element->unit_size, ==, element->channels * 16);
522 523
			} else
				g_assert_not_reached();
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
		}
		break;
	}

	default:
		break;
	}

	if(!success) {
		gst_event_unref(event);
	} else {
		success = gst_pad_event_default(pad, parent, event);
	}
	return success;
}


/*
 * chain()
 */


static GstFlowReturn chain(GstPad *pad, GstObject *parent, GstBuffer *sinkbuf)
{
	GSTLALInsertGap *element = GSTLAL_INSERTGAP(parent);
	GstFlowReturn result = GST_FLOW_OK;
	GST_DEBUG_OBJECT(element, "received %" GST_BUFFER_BOUNDARIES_FORMAT, GST_BUFFER_BOUNDARIES_ARGS(sinkbuf));

552 553
	if(GST_BUFFER_PTS_IS_VALID(sinkbuf)) {
		/* Set the timestamp of the first output sample) */
554
		if(element->t0 == GST_CLOCK_TIME_NONE) {
555
			element->t0 = GST_BUFFER_PTS(sinkbuf) + element->chop_length;
556 557 558 559 560 561 562 563 564 565 566
			if(element->wait_time > 0) {
				/*
				 * Start monitoring the arrival of new buffers.
				 * Fill in data if they are late.
				 */
				pthread_t *thread = g_malloc(sizeof(pthread_t));
				int failure = pthread_create(thread, NULL, input_buffer_timer, (void *) element);
				if (failure)
					GST_ERROR_OBJECT(element, "Return value of pthread_create() is %d", failure);
			}
		}
567

568 569 570 571 572 573 574 575
		/* If we are throwing away any initial data, do it now, and send a zero-length buffer downstream to let other elements know when to expect the first buffer */
		if(element->chop_length && GST_BUFFER_PTS(sinkbuf) + GST_BUFFER_DURATION(sinkbuf) <= element->t0) {
			GstBuffer *srcbuf = gst_buffer_new();
			GST_BUFFER_OFFSET(srcbuf) = GST_BUFFER_OFFSET(sinkbuf) + gst_util_uint64_scale_round(element->t0 - GST_BUFFER_PTS(sinkbuf), (guint64) element->rate, 1000000000);
			GST_BUFFER_OFFSET_END(srcbuf) = GST_BUFFER_OFFSET(srcbuf);
			GST_BUFFER_PTS(srcbuf) = element->t0;
			GST_BUFFER_DURATION(srcbuf) = 0;
			result = gst_pad_push(element->srcpad, srcbuf);
576 577 578
			gst_buffer_unref(sinkbuf);
			goto done;
		} else if(GST_BUFFER_PTS(sinkbuf) < element->t0) {
579 580
			guint64 size_removed = element->unit_size * gst_util_uint64_scale_round(element->t0 - GST_BUFFER_PTS(sinkbuf), (guint64) element->rate, 1000000000);
			guint64 time_removed = gst_util_uint64_scale_round(size_removed / element->unit_size, 1000000000, (guint64) element->rate);
581 582 583 584 585 586 587 588
			guint64 newsize = element->unit_size * (GST_BUFFER_OFFSET_END(sinkbuf) - GST_BUFFER_OFFSET(sinkbuf)) - size_removed;
			gst_buffer_resize(sinkbuf, size_removed, newsize);
			GST_BUFFER_OFFSET(sinkbuf) = GST_BUFFER_OFFSET(sinkbuf) + size_removed / element->unit_size;
			GST_BUFFER_PTS(sinkbuf) = GST_BUFFER_PTS(sinkbuf) + time_removed;
			GST_BUFFER_DURATION(sinkbuf) = GST_BUFFER_DURATION(sinkbuf) - time_removed;
		}
	}

589 590 591
	/* if buffer does not possess valid metadata or is zero length and we are not filling in discontinuities, push gap downstream */
	if(!(GST_BUFFER_PTS_IS_VALID(sinkbuf) && GST_BUFFER_DURATION_IS_VALID(sinkbuf) && GST_BUFFER_OFFSET_IS_VALID(sinkbuf) && GST_BUFFER_OFFSET_END_IS_VALID(sinkbuf)) || (!element->fill_discont && (GST_BUFFER_DURATION(sinkbuf) == 0 || GST_BUFFER_OFFSET(sinkbuf) == GST_BUFFER_OFFSET_END(sinkbuf)))) {
		GST_DEBUG_OBJECT(element, "pushing gap buffer at timestamp %lu seconds", (long unsigned) GST_TIME_AS_SECONDS(GST_BUFFER_PTS(sinkbuf)));
592 593 594 595 596 597 598 599 600
		GstBuffer *srcbuf;
		srcbuf = gst_buffer_copy(sinkbuf);
		GST_BUFFER_FLAG_SET(srcbuf, GST_BUFFER_FLAG_GAP);
		result = gst_pad_push(element->srcpad, srcbuf);
		if(G_UNLIKELY(result != GST_FLOW_OK))
			GST_WARNING_OBJECT(element, "push failed: %s", gst_flow_get_name(result));
		goto done;
	}

601
	/* if buffer is zero length and we are filling in discontinuities, fill it in, unless it has no valid timestamp. */
602
	if(element->fill_discont && (GST_BUFFER_DURATION(sinkbuf) == 0 || GST_BUFFER_OFFSET(sinkbuf) == GST_BUFFER_OFFSET_END(sinkbuf))) {
603
		if(GST_BUFFER_PTS_IS_VALID(sinkbuf) && GST_BUFFER_PTS(sinkbuf) > element->last_sinkbuf_ets) {
604
			g_mutex_lock(&element->mutex);
605 606 607 608 609
			switch(element->data_type) {
			case GSTLAL_INSERTGAP_U32:
				result = process_inbuf_guint32(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, GST_BUFFER_PTS(sinkbuf), FALSE);
				break;
			case GSTLAL_INSERTGAP_F32:
610
				result = process_inbuf_float(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, GST_BUFFER_PTS(sinkbuf), FALSE);
611 612
				break;
			case GSTLAL_INSERTGAP_F64:
613
				result = process_inbuf_double(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, GST_BUFFER_PTS(sinkbuf), FALSE);
614 615
				break;
			case GSTLAL_INSERTGAP_Z64:
616
				result = process_inbuf_floatcomplex(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, GST_BUFFER_PTS(sinkbuf), TRUE);
617 618
				break;
			case GSTLAL_INSERTGAP_Z128:
619
				result = process_inbuf_doublecomplex(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, GST_BUFFER_PTS(sinkbuf), TRUE);
620 621 622 623
				break;
			default:
				g_assert_not_reached();
			}
624
			g_mutex_unlock(&element->mutex);
625 626 627 628
		} else {
			GST_DEBUG_OBJECT(element, "dropping zero length buffer at timestamp %lu seconds", (long unsigned) GST_TIME_AS_SECONDS(GST_BUFFER_PTS(sinkbuf)));
			gst_buffer_unref(sinkbuf);
		}
629 630 631
		goto done;
	}

632
	g_mutex_lock(&element->mutex);
633

634
	/* We'll need these to decide gaps, offsets, and timestamps on the outgoing buffer(s) */
635 636 637
	gboolean sinkbuf_gap = GST_BUFFER_FLAG_IS_SET(sinkbuf, GST_BUFFER_FLAG_GAP);
	gboolean sinkbuf_discont = GST_BUFFER_FLAG_IS_SET(sinkbuf, GST_BUFFER_FLAG_DISCONT);
	guint64 sinkbuf_offset = GST_BUFFER_OFFSET(sinkbuf);
638
	guint64 sinkbuf_offset_end = GST_BUFFER_OFFSET_END(sinkbuf);
639
	GstClockTime sinkbuf_dur = GST_BUFFER_DURATION(sinkbuf);
640 641
	GstClockTime sinkbuf_pts = GST_BUFFER_PTS(sinkbuf);

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
	if(sinkbuf_pts >= element->last_sinkbuf_ets) {
		GstMapInfo inmap;
		gst_buffer_map(sinkbuf, &inmap, GST_MAP_READ);

		g_assert_cmpuint(inmap.size % element->unit_size, ==, 0);
		if(!element->chop_length || sinkbuf_pts > element->t0)
			g_assert_cmpuint((sinkbuf_offset_end - sinkbuf_offset), ==, inmap.size / element->unit_size); /* sanity checks */

		/* outdata will be filled with the data that goes on the outgoing buffer(s) */
		void *outdata;
		outdata = g_malloc((sinkbuf_offset_end - sinkbuf_offset) * element->unit_size);

		switch(element->data_type) {
		case GSTLAL_INSERTGAP_U32:
			result = process_inbuf_guint32((guint32 *) inmap.data, outdata, element, sinkbuf_gap, sinkbuf_discont, sinkbuf_offset, sinkbuf_offset_end, sinkbuf_dur, sinkbuf_pts, FALSE);
			break;
		case GSTLAL_INSERTGAP_F32:
			result = process_inbuf_float((float *) inmap.data, outdata, element, sinkbuf_gap, sinkbuf_discont, sinkbuf_offset, sinkbuf_offset_end, sinkbuf_dur, sinkbuf_pts, FALSE);
			break;
		case GSTLAL_INSERTGAP_F64:
			result = process_inbuf_double((double *) inmap.data, outdata, element, sinkbuf_gap, sinkbuf_discont, sinkbuf_offset, sinkbuf_offset_end, sinkbuf_dur, sinkbuf_pts, FALSE);
			break;
		case GSTLAL_INSERTGAP_Z64:
			result = process_inbuf_floatcomplex((float complex *) inmap.data, outdata, element, sinkbuf_gap, sinkbuf_discont, sinkbuf_offset, sinkbuf_offset_end, sinkbuf_dur, sinkbuf_pts, TRUE);
			break;
		case GSTLAL_INSERTGAP_Z128:
			result = process_inbuf_doublecomplex((double complex *) inmap.data, outdata, element, sinkbuf_gap, sinkbuf_discont, sinkbuf_offset, sinkbuf_offset_end, sinkbuf_dur, sinkbuf_pts, TRUE);
			break;

		default:
			g_assert_not_reached();
		}
674

675 676 677
		g_free(outdata);
		outdata = NULL;
		gst_buffer_unmap(sinkbuf, &inmap);
678 679 680
	}

	gst_buffer_unref(sinkbuf);
681 682
	g_mutex_unlock(&element->mutex);

683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
	/*
	 * done
	 */

done:
	return result;
}


/*
 * ============================================================================
 *
 *			  GObject Method Overrides
 *
 * ============================================================================
 */


/*
 * properties
 */


enum property {
	ARG_INSERT_GAP = 1,
	ARG_REMOVE_GAP,
	ARG_REMOVE_NAN,
	ARG_REMOVE_INF,
711
	ARG_FILL_DISCONT,
712 713
	ARG_REPLACE_VALUE,
	ARG_BAD_DATA_INTERVALS,
714
	ARG_BLOCK_DURATION,
715 716
	ARG_CHOP_LENGTH,
	ARG_WAIT_TIME
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
};


static void set_property(GObject *object, enum property prop_id, const GValue *value, GParamSpec *pspec)
{
	GSTLALInsertGap *element = GSTLAL_INSERTGAP(object);

	GST_OBJECT_LOCK(element);

	switch (prop_id) {
	case ARG_INSERT_GAP:
		element->insert_gap = g_value_get_boolean(value);
		break;
	case ARG_REMOVE_GAP:
		element->remove_gap = g_value_get_boolean(value);
		break;
	case ARG_REMOVE_NAN:
		element->remove_nan = g_value_get_boolean(value);
		break;
	case ARG_REMOVE_INF:
		element->remove_inf = g_value_get_boolean(value);
		break;
739 740 741
	case ARG_FILL_DISCONT:
		element->fill_discont = g_value_get_boolean(value);
		break;
742 743 744 745
	case ARG_REPLACE_VALUE:
		element->replace_value = g_value_get_double(value);
		break;
	case ARG_BAD_DATA_INTERVALS: {
746 747 748 749
		if(element->bad_data_intervals) {
			g_free(element->bad_data_intervals);
			element->bad_data_intervals = NULL;
		}
750
		element->array_length = gst_value_array_get_size(value);
751
		if(element->array_length % 2)
752 753 754 755 756 757
			GST_ERROR_OBJECT(element, "Array length for property bad-data-intervals must be even");
		element->bad_data_intervals = g_malloc(element->array_length * sizeof(double));
		int i;
		for(i = 0; i < element->array_length; i++)
			element->bad_data_intervals[i] = g_value_get_double(gst_value_array_get_value(value, i));

758 759 760 761 762
		break;
	}
	case ARG_BLOCK_DURATION:
		element->block_duration = g_value_get_uint64(value);
		break;
763 764 765
	case ARG_CHOP_LENGTH:
		element->chop_length = g_value_get_uint64(value);
		break;
766 767 768
	case ARG_WAIT_TIME:
		element->wait_time = g_value_get_uint64(value);
		break;
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
		break;
	}

	GST_OBJECT_UNLOCK(element);
}


static void get_property(GObject *object, enum property prop_id, GValue *value, GParamSpec *pspec)
{
	GSTLALInsertGap *element = GSTLAL_INSERTGAP(object);

	GST_OBJECT_LOCK(element);

	switch (prop_id) {
	case ARG_INSERT_GAP:
		g_value_set_boolean(value, element->insert_gap);
		break;
	case ARG_REMOVE_GAP:
		g_value_set_boolean(value, element->remove_gap);
		break;
	case ARG_REMOVE_NAN:
		g_value_set_boolean(value, element->remove_nan);
		break;
	case ARG_REMOVE_INF:
		g_value_set_boolean(value, element->remove_inf);
		break;
797 798 799
	case ARG_FILL_DISCONT:
		g_value_set_boolean(value, element->fill_discont);
		break;
800 801 802
	case ARG_REPLACE_VALUE:
		g_value_set_double(value, element->replace_value);
		break;
803 804 805 806 807 808 809 810 811 812 813 814 815 816
	case ARG_BAD_DATA_INTERVALS: ;
		GValue va = G_VALUE_INIT;
		g_value_init(&va, GST_TYPE_ARRAY);
		int i;
		for(i = 0; i < element->array_length; i++) {
			GValue v = G_VALUE_INIT;
			g_value_init(&v, G_TYPE_DOUBLE);
			g_value_set_double(&v, element->bad_data_intervals[i]);
			gst_value_array_append_value(&va, &v);
			g_value_unset(&v);
		}
		g_value_copy(&va, value);
		g_value_unset(&va);

817 818 819 820
		break;
	case ARG_BLOCK_DURATION:
		g_value_set_uint64(value, element->block_duration);
		break;
821 822 823
	case ARG_CHOP_LENGTH:
		g_value_set_uint64(value, element->chop_length);
		break;
824 825 826
	case ARG_WAIT_TIME:
		g_value_set_uint64(value, element->wait_time);
		break;
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
		break;
	}

	GST_OBJECT_UNLOCK(element);
}


/*
 * finalize()
 */


static void finalize(GObject *object)
{
	GSTLALInsertGap *element = GSTLAL_INSERTGAP(object);

	gst_object_unref(element->sinkpad);
	element->sinkpad = NULL;
	gst_object_unref(element->srcpad);
	element->srcpad = NULL;
	g_free(element->bad_data_intervals);
	element->bad_data_intervals = NULL;

852 853 854 855
	/* Needed for thread used to time input buffers */
	element->finished_running = TRUE;
	g_mutex_clear(&element->mutex);

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
	G_OBJECT_CLASS(gstlal_insertgap_parent_class)->finalize(object);
}


/*
 * class_init()
 */


static void gstlal_insertgap_class_init(GSTLALInsertGapClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
	GstElementClass *element_class = GST_ELEMENT_CLASS(klass);

	gst_element_class_set_details_simple(
		element_class,
		"Replace unwanted data with gaps",
		"Filter",
874 875 876
		"Replace unwanted data, specified with the property bad-data-intervals, with gaps.\n\t\t\t   "
		"Also can replace with another value, given by replace-value. Can also remove gaps\n\t\t\t   "
		"where data is acceptable, and fill in discontinuities if desired.",
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
		"Aaron Viets <aaron.viets@ligo.org>"
	);

	gobject_class->set_property = GST_DEBUG_FUNCPTR(set_property);
	gobject_class->get_property = GST_DEBUG_FUNCPTR(get_property);
	gobject_class->finalize = GST_DEBUG_FUNCPTR(finalize);

	gst_element_class_add_pad_template(element_class, gst_static_pad_template_get(&src_factory));
	gst_element_class_add_pad_template(element_class, gst_static_pad_template_get(&sink_factory));

	g_object_class_install_property(
		gobject_class,
		ARG_INSERT_GAP,
		g_param_spec_boolean(
			"insert-gap",
			"Insert gap",
893 894 895
			"If set to true (default), any data fitting the criteria specified by the property\n\t\t\t"
			"bad-data-intervals is replaced with gaps. Also, NaN's and inf's are replaced with\n\t\t\t"
			"gaps if the properties remove-nan and remove-inf are set to true, respectively.",
896 897 898 899 900 901 902 903 904 905 906
			TRUE,
			G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT
		)
	);

	g_object_class_install_property(
		gobject_class,
		ARG_REMOVE_GAP,
		g_param_spec_boolean(
			"remove-gap",
			"Remove gap",
907 908 909 910
			"If set to true, any data in an input gap buffer that does not fit the criteria\n\t\t\t"
			"specified by the property bad-data-intervals will be marked as non-gap. If the\n\t\t\t"
			"property insert-gap is false and remove-gap is true, gaps with unacceptable\n\t\t\t"
			"data will be replaced by the value specified by the property replace-value.",
911 912 913 914 915 916 917 918 919 920 921
			FALSE,
			G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT
		)
	);

	g_object_class_install_property(
		gobject_class,
		ARG_REMOVE_NAN,
		g_param_spec_boolean(
			"remove-nan",
			"Remove NaN",
922 923
			"If set to true (default), NaN's in the data stream will be replaced with gaps\n\t\t\t"
			"and/or the replace-value, as specified by user.",
924 925 926 927 928 929 930 931 932 933 934
			TRUE,
			G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT
		)
	);

	g_object_class_install_property(
		gobject_class,
		ARG_REMOVE_INF,
		g_param_spec_boolean(
			"remove-inf",
			"Remove inf",
935 936
			"If set to true (default), infinities in the data stream will be replaced with\n\t\t\t"
			"gaps and/or the replace-value, as specified by user.",
937 938 939 940 941
			TRUE,
			G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT
		)
	);

942 943 944 945 946 947
	g_object_class_install_property(
		gobject_class,
		ARG_FILL_DISCONT,
		g_param_spec_boolean(
			"fill-discont",
			"Fill discontinuity",
948 949
			"If set to true, discontinuities in the data stream will be filled with the\n\t\t\t"
			"replace-value (if set, otherwise 0), and gapped if insert-gap is true.",
950 951 952 953 954
			FALSE,
			G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT
		)
	);

955 956 957 958 959 960
	g_object_class_install_property(
		gobject_class,
		ARG_REPLACE_VALUE,
		g_param_spec_double(
			"replace-value",
			"Replace value",
961 962
			"If set, this value is used to replace any data that fits the criteria\n\t\t\t"
			"specified by the property bad-data-intervals. If unset, values are not replaced.", 
963 964 965 966 967 968 969 970
			-G_MAXDOUBLE, G_MAXDOUBLE, G_MAXDOUBLE,
			G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT
		)
	);

	g_object_class_install_property(
		gobject_class,
		ARG_BAD_DATA_INTERVALS,
971
		gst_param_spec_array(
972 973
			"bad-data-intervals",
			"Bad data intervals",
974 975 976 977
			"Array containing minima and maxima of closed intervals in which data is\n\t\t\t"
			"considered unacceptable and will be replaced with gaps and/or the replace-value.\n\t\t\t"
			"Array indices 0, 2, 4, etc., represent maxima, and array indices 1, 3, 5, etc.,\n\t\t\t"
			"represent the corresponding minima.",
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
			g_param_spec_double(
				"coefficient",
				"Coefficient",
				"Coefficient",
				-G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
				G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS
			),
			G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS
		)
	);

	g_object_class_install_property(
		gobject_class,
		ARG_BLOCK_DURATION,
		g_param_spec_uint64(
			"block-duration",
			"Block duration",
995 996
			"Maximum output buffer duration in nanoseconds. Buffers may be smaller than this.\n\t\t\t"
			"Default is to not change buffer length except as required by added/removed gaps.",
997 998 999 1000
			0, G_MAXUINT64, G_MAXUINT64 / 2,
			G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT
		)
	);
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012

	g_object_class_install_property(
		gobject_class,
		ARG_CHOP_LENGTH,
		g_param_spec_uint64(
			"chop-length",
			"Chop length",
			"Amount of initial data to throw away before producing output data, in nanoseconds.",
			0, G_MAXUINT64, 0,
			G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT
		)
	);
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

	g_object_class_install_property(
		gobject_class,
		ARG_WAIT_TIME,
		g_param_spec_uint64(
			"wait-time",
			"Wait Time",
			"If nonzero, timestamps of input buffers are tracked and compared to real time.\n\t\t\t"
			"If the latest input timestamp is more than the wait-time (in nanoseconds) behind\n\t\t\t"
			"real time, output buffers are filled with the replace-value and pushed\n\t\t\t"
			"downstream. Default is to disable.",
			0, G_MAXUINT64, 0,
			G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT
		)
	);
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
}


/*
 * instance init
 */


static void gstlal_insertgap_init(GSTLALInsertGap *element)
{
	GstPad *pad;

	gst_element_create_all_pads(GST_ELEMENT(element));

	/* configure (and ref) sink pad */
	pad = gst_element_get_static_pad(GST_ELEMENT(element), "sink");
	gst_pad_set_event_function(pad, GST_DEBUG_FUNCPTR(sink_event));
	gst_pad_set_chain_function(pad, GST_DEBUG_FUNCPTR(chain));
	GST_PAD_SET_PROXY_CAPS(pad);
	GST_PAD_SET_PROXY_ALLOCATION(pad);
	GST_PAD_SET_PROXY_SCHEDULING(pad);
	element->sinkpad = pad;

	/* retrieve (and ref) src pad */
	pad = gst_element_get_static_pad(GST_ELEMENT(element), "src");
	GST_PAD_SET_PROXY_CAPS(pad);
	GST_PAD_SET_PROXY_ALLOCATION(pad);
	GST_PAD_SET_PROXY_SCHEDULING(pad);
	element->srcpad = pad;

	/* internal data */
1059
	g_mutex_init(&element->mutex);
1060 1061 1062
	element->t0 = GST_CLOCK_TIME_NONE;
	element->bad_data_intervals = NULL;
	element->array_length = 0;
1063
	element->rate = 0;
1064
	element->channels = 0;
1065
	element->unit_size = 0;
1066
	element->last_sinkbuf_ets = 0;
1067 1068
	element->last_sinkbuf_offset_end = 0;
	element->discont_offset = 0;
1069 1070
	element->discont_time = 0;
	element->empty_bufs = 0;
1071 1072 1073

	/* Needed for thread used to time input buffers */
	element->finished_running = FALSE;
1074
}