gstlal_insertgap.c 39 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
/*
 * set the metadata on an output buffer
 */


static void set_metadata(GSTLALInsertGap *element, GstBuffer *buf, guint64 outsamples, gboolean gap, gboolean discont) {

	GST_BUFFER_OFFSET(buf) = element->next_out_offset;
	element->next_out_offset += outsamples;
	GST_BUFFER_OFFSET_END(buf) = element->next_out_offset;
	GST_BUFFER_PTS(buf) = element->t0 + gst_util_uint64_scale_int_round(GST_BUFFER_OFFSET(buf) - element->offset0, GST_SECOND, element->rate);
	GST_BUFFER_DURATION(buf) = element->t0 + gst_util_uint64_scale_int_round(GST_BUFFER_OFFSET_END(buf) - element->offset0, GST_SECOND, element->rate) - GST_BUFFER_TIMESTAMP(buf);
	GST_BUFFER_FLAG_UNSET(buf, GST_BUFFER_FLAG_GAP);
	if(discont)
		GST_BUFFER_FLAG_SET(buf, GST_BUFFER_FLAG_DISCONT);
	if(gap)
		GST_BUFFER_FLAG_SET(buf, GST_BUFFER_FLAG_GAP);
	else
		GST_BUFFER_FLAG_UNSET(buf, GST_BUFFER_FLAG_GAP);
}


174 175 176 177 178 179
#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) { \
180
			result = FALSE; \
181
			for(j = 0; j < array_length; j += 2) \
182
				result |= data[i] > bad_data_intervals[j] && data[i] < bad_data_intervals[j + 1]; \
183 184
		} \
		if(remove_nan) \
185
			result &= !isnan((double) data[i]); \
186
		if(remove_inf) \
187
			result &= !isinf((double) data[i]); \
188 189
	} \
	return result; \
190 191 192
}


193 194 195 196 197
DEFINE_CHECK_DATA(float);
DEFINE_CHECK_DATA(double);
DEFINE_CHECK_DATA(guint32);


198
#define DEFINE_PROCESS_INBUF(DTYPE,COMPLEX) \
199
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, gboolean empty_sinkbuf) \
200 201
{ \
	GstFlowReturn result = GST_FLOW_OK; \
202
	guint64 blocks, max_block_length; \
203 204 205 206
 \
	/*
	 * First, deal with discontinuity if necessary
	 */ \
207
	if(element->fill_discont && (element->last_sinkbuf_ets != 0) && (sinkbuf_pts != element->last_sinkbuf_ets)) { \
208
 \
209
		g_print("Filling Discont!\n"); \
210 211
		guint64 standard_blocks, last_block_length, buffer_num, sample_num, missing_samples = 0; \
		DTYPE COMPLEX sample_value; \
212 213 214
 \
		/* Track discont length and number of zero-length buffers */ \
		element->discont_time += (sinkbuf_pts - element->last_sinkbuf_ets); \
215 216 217
		element->empty_bufs += (empty_sinkbuf ? 1 : 0); \
		/* If there was no zero-length buffer, but the "duration" passed to this function was zero, then the timer sent this buffer. */ \
		element->timeout_bufs += (empty_sinkbuf || sinkbuf_dur ? 0 : 1); \
218 219 220 221 222 223 224 225 226
 \
		/* 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 */ \
227 228
		if(sinkbuf_dur && (sinkbuf_offset != sinkbuf_offset_end || element->empty_bufs || element->timeout_bufs)) \
			GST_WARNING_OBJECT(element, "filling discontinuity lasting %f seconds (%lu samples) including %lu zero-length buffers and %lu timeout buffers, 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, element->timeout_bufs, (double) sinkbuf_pts / 1000000000.0 - (double) element->discont_time / 1000000000.0, sinkbuf_offset); \
229
 \
230 231
		standard_blocks = missing_samples / max_block_length; \
		last_block_length = missing_samples % max_block_length; \
232 233 234 235 236 237 238 239 240 241
		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; \
242 243
				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++) { \
244 245 246
					*discont_buf_data = sample_value; \
					discont_buf_data++; \
				} \
247
				discont_buf = gst_buffer_new_wrapped((discont_buf_data - max_block_length * element->channels), max_block_length * element->channels * sizeof(DTYPE COMPLEX)); \
248 249 250 251 252 253
				if(G_UNLIKELY(!discont_buf)) { \
					GST_ERROR_OBJECT(element, "failure creating sub-buffer"); \
					result = GST_FLOW_ERROR; \
					goto done; \
				} \
 \
254 255
				/* set flags, offsets, and timestamps. */ \
				set_metadata(element, discont_buf, max_block_length, element->insert_gap, FALSE); \
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
 \
				/* 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; \
271
			last_discont_buf_data = g_malloc(last_block_length * element->channels * sizeof(DTYPE COMPLEX)); \
272
			guint sample_num; \
273
			for(sample_num = 0; sample_num < last_block_length * element->channels; sample_num++) { \
274 275 276
				*last_discont_buf_data = sample_value; \
				last_discont_buf_data++; \
			} \
277
			last_discont_buf = gst_buffer_new_wrapped((last_discont_buf_data - last_block_length * element->channels), last_block_length * element->channels * sizeof(DTYPE COMPLEX)); \
278 279 280 281 282 283
			if(G_UNLIKELY(!last_discont_buf)) { \
				GST_ERROR_OBJECT(element, "failure creating sub-buffer"); \
				result = GST_FLOW_ERROR; \
				goto done; \
			} \
 \
284 285
			/* set flags, offsets, and timestamps. */ \
			set_metadata(element, last_discont_buf, last_block_length, element->insert_gap, FALSE); \
286 287 288 289 290 291 292 293 294 295
 \
			/* 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; \
			} \
		} \
	} \
296 297 298 299 300
	if(!sinkbuf_dur) \
		goto done; \
 \
	element->discont_time = 0; \
	element->empty_bufs = 0; \
301
	element->timeout_bufs = 0; \
302 303 304 305
 \
	/*
	 * Now, use data on input buffer to make next output buffer(s)
	 */ \
306
	gboolean data_is_bad, srcbuf_gap, srcbuf_gap_next; \
307 308
	guint64 offset, length, current_srcbuf_length = 0; \
	int i; \
309 310 311 312
 \
	/* 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 */ \
313
	length = sinkbuf_offset_end - sinkbuf_offset; \
314 315 316 317
	max_block_length = (length + blocks - 1) / blocks; /* ceil */ \
	g_assert_cmpuint(max_block_length, >, 0); \
 \
	/* Check first sample */ \
318
	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); \
319
	srcbuf_gap = (sinkbuf_gap && (!(element->remove_gap))) || ((element->insert_gap) && data_is_bad); \
320
	for(offset = 0; offset < length; offset++) { \
321
		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); \
322
		srcbuf_gap_next = (sinkbuf_gap && (!(element->remove_gap))) || ((element->insert_gap) && data_is_bad); \
323 324 325 326 327 328 329
		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]; \
		} \
330
		current_srcbuf_length++; \
331 332
		indata += element->channels; \
		outdata += element->channels; \
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
 \
		/* 
		 * 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--; \
350 351
				indata -= element->channels; \
				outdata -= element->channels; \
352 353
			} \
			GstBuffer *srcbuf; \
354
			DTYPE COMPLEX *srcbuf_data; \
355 356 357
			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)); \
358 359
 \
			if(G_UNLIKELY(!srcbuf)) { \
360
				GST_ERROR_OBJECT(element, "failure creating sub-buffer"); \
361
				result = GST_FLOW_ERROR; \
362
				goto done; \
363 364 365 366 367 368
			} \
 \
			/*
			 * only the first subbuffer of a buffer flagged as a
			 * discontinuity is a discontinuity.
			 */ \
369 370 371 372
			gboolean need_discont = sinkbuf_discont && (offset + 1 - current_srcbuf_length == 0) && ((!(element->fill_discont)) || (element->last_sinkbuf_ets == 0)); \
			/* set flags, offsets, and timestamps. */ \
			set_metadata(element, srcbuf, current_srcbuf_length, srcbuf_gap, need_discont); \
 \
373 374 375
			if(srcbuf_gap_next != srcbuf_gap) { \
				/* We need to reset our place in the input buffer */ \
				offset++; \
376 377
				indata += element->channels; \
				outdata += element->channels; \
378 379 380 381 382 383
				current_srcbuf_length = 1; \
				srcbuf_gap = srcbuf_gap_next; \
			} else \
				current_srcbuf_length = 0; \
 \
			/* push buffer downstream */ \
384 385
			GST_DEBUG_OBJECT(element, "pushing sub-buffer %" GST_BUFFER_BOUNDARIES_FORMAT, GST_BUFFER_BOUNDARIES_ARGS(srcbuf)); \
			result = gst_pad_push(element->srcpad, srcbuf); \
386
			if(G_UNLIKELY(result != GST_FLOW_OK)) { \
387 388
				GST_WARNING_OBJECT(element, "push failed: %s", gst_flow_get_name(result)); \
				goto done; \
389 390 391
			} \
		} \
	} \
392 393
done: \
	element->last_sinkbuf_ets = sinkbuf_pts + sinkbuf_dur; \
394 395 396 397
	return result; \
}


398
DEFINE_PROCESS_INBUF(guint32, )
399 400 401 402
DEFINE_PROCESS_INBUF(float, )
DEFINE_PROCESS_INBUF(double, )
DEFINE_PROCESS_INBUF(float,complex)
DEFINE_PROCESS_INBUF(double,complex)
403 404


405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
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 */
422 423 424 425
		if(element->block_duration < G_MAXUINT64 / 2)
			sleep(element->block_duration / 1000000000.0);
		else
			sleep(1);
426 427 428 429 430 431 432 433 434

		/* 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);

435
		/* Time in nanoseconds */
436
		guint64 current_time = (guint64) XLALUTCToGPS(&tm) * 1000000000 + (guint64) gst_date_time_get_microsecond(current_gst_time) * 1000;
437 438 439 440 441 442 443 444
		/*
		 * The minimum allowable current buffer timestamp.  If the input buffer
		 * has an earlier timestamp, we will push output buffers without waiting
		 * any longer for input.
		 */
		guint64 ets_min = current_time - element->wait_time;
		/* It needs to be a multiple of the sample period. */
		ets_min = gst_util_uint64_scale_int_round(gst_util_uint64_scale_int(ets_min, element->rate, 1000000000), 1000000000, element->rate);
445

446 447 448 449 450 451 452
		g_mutex_lock(&element->mutex);
		if((double) ets_min - (double) element->last_sinkbuf_ets + 1000000000.0 - (double) (rand() % 2000000000) > 1000000000.0 / element->rate) {
			g_print("input_buffer_timer going to push a buffer: ets_min=%lu, last_sinkbuf_ets=%lu\n", ets_min, element->last_sinkbuf_ets);
			/*
			 * Then we need to push a buffer.  Note the requirement that the
			 * time is late enough for us to produce at least one full sample.
			 */
453 454 455
			GstFlowReturn result;
			switch(element->data_type) {
			case GSTLAL_INSERTGAP_U32:
456
				result = process_inbuf_guint32(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, ets_min, FALSE, FALSE);
457 458
				break;
			case GSTLAL_INSERTGAP_F32:
459
				result = process_inbuf_float(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, ets_min, FALSE, FALSE);
460 461
				break;
			case GSTLAL_INSERTGAP_F64:
462
				result = process_inbuf_double(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, ets_min, FALSE, FALSE);
463 464
				break;
			case GSTLAL_INSERTGAP_Z64:
465
				result = process_inbuf_floatcomplex(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, ets_min, TRUE, FALSE);
466 467
				break;
			case GSTLAL_INSERTGAP_Z128:
468
				result = process_inbuf_doublecomplex(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, ets_min, TRUE, FALSE);
469 470 471 472 473 474 475 476
				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));

477
			g_print("input_buffer_timer pushed a buffer\n");
478
		}
479
		g_mutex_unlock(&element->mutex);
480

481 482
		if(element->finished_running) {
			g_print("input_buffer_timer pthread_exit()\n");
483
			pthread_exit(NULL);
484
		}
485 486 487 488
	}
}


489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
/*
 * ============================================================================
 *
 *				     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);
514 515 516 517
		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);
518
		if(success) {
519
			/* record stream parameters */
520
			element->rate = GST_AUDIO_INFO_RATE(&info);
521
			element->channels = GST_AUDIO_INFO_CHANNELS(&info);
522
			element->unit_size = GST_AUDIO_INFO_BPF(&info);
523 524
			if(!strcmp(name, GST_AUDIO_NE(U32))) {
				element->data_type = GSTLAL_INSERTGAP_U32;
525
				g_assert_cmpuint(element->unit_size, ==, element->channels * 4);
526
			} else if(!strcmp(name, GST_AUDIO_NE(F32))) {
527
				element->data_type = GSTLAL_INSERTGAP_F32;
528
				g_assert_cmpuint(element->unit_size, ==, element->channels * 4);
529 530
			} else if(!strcmp(name, GST_AUDIO_NE(F64))) {
				element->data_type = GSTLAL_INSERTGAP_F64;
531
				g_assert_cmpuint(element->unit_size, ==, element->channels * 8);
532 533
			} else if(!strcmp(name, GST_AUDIO_NE(Z64))) {
				element->data_type = GSTLAL_INSERTGAP_Z64;
534
				g_assert_cmpuint(element->unit_size, ==, element->channels * 8);
535 536
			} else if(!strcmp(name, GST_AUDIO_NE(Z128))) {
				element->data_type = GSTLAL_INSERTGAP_Z128;
537
				g_assert_cmpuint(element->unit_size, ==, element->channels * 16);
538 539
			} else
				g_assert_not_reached();
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
		}
		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));

568 569
	if(GST_BUFFER_PTS_IS_VALID(sinkbuf)) {
		/* Set the timestamp of the first output sample) */
570
		if(element->t0 == GST_CLOCK_TIME_NONE) {
571
			element->offset0 = element->next_out_offset = GST_BUFFER_OFFSET(sinkbuf) + gst_util_uint64_scale_int_round(element->chop_length, element->rate, GST_SECOND);
572
			element->t0 = GST_BUFFER_PTS(sinkbuf) + element->chop_length;
573 574 575 576 577 578 579 580 581 582 583
			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);
			}
		}
584

585 586 587 588 589 590 591 592
		/* 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);
593 594 595
			gst_buffer_unref(sinkbuf);
			goto done;
		} else if(GST_BUFFER_PTS(sinkbuf) < element->t0) {
596 597
			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);
598 599 600 601 602 603 604 605
			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;
		}
	}

606 607 608
	/* 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)));
609 610 611 612 613 614 615 616 617
		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;
	}

618
	/* if buffer is zero length and we are filling in discontinuities, fill it in, unless it has no valid timestamp. */
619
	if(element->fill_discont && (GST_BUFFER_DURATION(sinkbuf) == 0 || GST_BUFFER_OFFSET(sinkbuf) == GST_BUFFER_OFFSET_END(sinkbuf))) {
620
		if(GST_BUFFER_PTS_IS_VALID(sinkbuf) && GST_BUFFER_PTS(sinkbuf) > element->last_sinkbuf_ets) {
621
			g_mutex_lock(&element->mutex);
622 623
			switch(element->data_type) {
			case GSTLAL_INSERTGAP_U32:
624
				result = process_inbuf_guint32(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, GST_BUFFER_PTS(sinkbuf), FALSE, TRUE);
625 626
				break;
			case GSTLAL_INSERTGAP_F32:
627
				result = process_inbuf_float(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, GST_BUFFER_PTS(sinkbuf), FALSE, TRUE);
628 629
				break;
			case GSTLAL_INSERTGAP_F64:
630
				result = process_inbuf_double(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, GST_BUFFER_PTS(sinkbuf), FALSE, TRUE);
631 632
				break;
			case GSTLAL_INSERTGAP_Z64:
633
				result = process_inbuf_floatcomplex(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, GST_BUFFER_PTS(sinkbuf), TRUE, TRUE);
634 635
				break;
			case GSTLAL_INSERTGAP_Z128:
636
				result = process_inbuf_doublecomplex(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, GST_BUFFER_PTS(sinkbuf), TRUE, TRUE);
637 638 639 640
				break;
			default:
				g_assert_not_reached();
			}
641
			g_mutex_unlock(&element->mutex);
642 643 644 645
		} 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);
		}
646 647 648
		goto done;
	}

649
	g_mutex_lock(&element->mutex);
650

651
	/* We'll need these to decide gaps, offsets, and timestamps on the outgoing buffer(s) */
652
	gboolean sinkbuf_gap = GST_BUFFER_FLAG_IS_SET(sinkbuf, GST_BUFFER_FLAG_GAP);
653
	gboolean sinkbuf_discont = G_UNLIKELY(GST_BUFFER_IS_DISCONT(sinkbuf) || GST_BUFFER_OFFSET(sinkbuf) != element->next_out_offset || !GST_CLOCK_TIME_IS_VALID(element->t0));
654
	guint64 sinkbuf_offset = GST_BUFFER_OFFSET(sinkbuf);
655
	guint64 sinkbuf_offset_end = GST_BUFFER_OFFSET_END(sinkbuf);
656
	GstClockTime sinkbuf_dur = GST_BUFFER_DURATION(sinkbuf);
657 658
	GstClockTime sinkbuf_pts = GST_BUFFER_PTS(sinkbuf);

659 660 661 662 663 664
	/* If we're not filling discontinuities, reset the timestamp and offset bookkeeping. */
	if(!element->fill_discont && element->t0 != GST_CLOCK_TIME_NONE) {
		element->offset0 = element->next_out_offset = GST_BUFFER_OFFSET(sinkbuf);
		element->t0 = GST_BUFFER_PTS(sinkbuf);
	}

665
	if(sinkbuf_pts >= element->last_sinkbuf_ets) {
666 667 668
		g_print("chain pushing buffer\n");
		if(sinkbuf_pts == element->last_sinkbuf_ets)
			g_print("chain pushing buffer no discont\n");
669 670 671 672 673 674 675 676 677 678 679 680 681
		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:
682
			result = process_inbuf_guint32((guint32 *) inmap.data, outdata, element, sinkbuf_gap, sinkbuf_discont, sinkbuf_offset, sinkbuf_offset_end, sinkbuf_dur, sinkbuf_pts, FALSE, FALSE);
683 684
			break;
		case GSTLAL_INSERTGAP_F32:
685
			result = process_inbuf_float((float *) inmap.data, outdata, element, sinkbuf_gap, sinkbuf_discont, sinkbuf_offset, sinkbuf_offset_end, sinkbuf_dur, sinkbuf_pts, FALSE, FALSE);
686 687
			break;
		case GSTLAL_INSERTGAP_F64:
688
			result = process_inbuf_double((double *) inmap.data, outdata, element, sinkbuf_gap, sinkbuf_discont, sinkbuf_offset, sinkbuf_offset_end, sinkbuf_dur, sinkbuf_pts, FALSE, FALSE);
689 690
			break;
		case GSTLAL_INSERTGAP_Z64:
691
			result = process_inbuf_floatcomplex((float complex *) inmap.data, outdata, element, sinkbuf_gap, sinkbuf_discont, sinkbuf_offset, sinkbuf_offset_end, sinkbuf_dur, sinkbuf_pts, TRUE, FALSE);
692 693
			break;
		case GSTLAL_INSERTGAP_Z128:
694
			result = process_inbuf_doublecomplex((double complex *) inmap.data, outdata, element, sinkbuf_gap, sinkbuf_discont, sinkbuf_offset, sinkbuf_offset_end, sinkbuf_dur, sinkbuf_pts, TRUE, FALSE);
695 696 697 698 699
			break;

		default:
			g_assert_not_reached();
		}
700

701 702 703
		g_free(outdata);
		outdata = NULL;
		gst_buffer_unmap(sinkbuf, &inmap);
704
		g_print("chain done pushing buffer\n");
705 706 707
	}

	gst_buffer_unref(sinkbuf);
708 709
	g_mutex_unlock(&element->mutex);

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
	/*
	 * done
	 */

done:
	return result;
}


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


/*
 * properties
 */


enum property {
	ARG_INSERT_GAP = 1,
	ARG_REMOVE_GAP,
	ARG_REMOVE_NAN,
	ARG_REMOVE_INF,
738
	ARG_FILL_DISCONT,
739 740
	ARG_REPLACE_VALUE,
	ARG_BAD_DATA_INTERVALS,
741
	ARG_BLOCK_DURATION,
742 743
	ARG_CHOP_LENGTH,
	ARG_WAIT_TIME
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
};


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;
766 767 768
	case ARG_FILL_DISCONT:
		element->fill_discont = g_value_get_boolean(value);
		break;
769 770 771 772
	case ARG_REPLACE_VALUE:
		element->replace_value = g_value_get_double(value);
		break;
	case ARG_BAD_DATA_INTERVALS: {
773 774 775 776
		if(element->bad_data_intervals) {
			g_free(element->bad_data_intervals);
			element->bad_data_intervals = NULL;
		}
777
		element->array_length = gst_value_array_get_size(value);
778
		if(element->array_length % 2)
779 780 781 782 783 784
			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));

785 786 787 788 789
		break;
	}
	case ARG_BLOCK_DURATION:
		element->block_duration = g_value_get_uint64(value);
		break;
790 791 792
	case ARG_CHOP_LENGTH:
		element->chop_length = g_value_get_uint64(value);
		break;
793 794 795
	case ARG_WAIT_TIME:
		element->wait_time = g_value_get_uint64(value);
		break;
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
	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;
824 825 826
	case ARG_FILL_DISCONT:
		g_value_set_boolean(value, element->fill_discont);
		break;
827 828 829
	case ARG_REPLACE_VALUE:
		g_value_set_double(value, element->replace_value);
		break;
830 831 832 833 834 835 836 837 838 839 840 841 842 843
	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);

844 845 846 847
		break;
	case ARG_BLOCK_DURATION:
		g_value_set_uint64(value, element->block_duration);
		break;
848 849 850
	case ARG_CHOP_LENGTH:
		g_value_set_uint64(value, element->chop_length);
		break;
851 852 853
	case ARG_WAIT_TIME:
		g_value_set_uint64(value, element->wait_time);
		break;
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
	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;

879 880 881 882
	/* Needed for thread used to time input buffers */
	element->finished_running = TRUE;
	g_mutex_clear(&element->mutex);

883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
	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",
901 902 903
		"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.",
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
		"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",
920 921 922
			"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.",
923 924 925 926 927 928 929 930 931 932 933
			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",
934 935 936 937
			"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.",
938 939 940 941 942 943 944 945 946 947 948
			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",
949 950
			"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.",
951 952 953 954 955 956 957 958 959 960 961
			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",
962 963
			"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.",
964 965 966 967 968
			TRUE,
			G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT
		)
	);

969 970 971 972 973 974
	g_object_class_install_property(
		gobject_class,
		ARG_FILL_DISCONT,
		g_param_spec_boolean(
			"fill-discont",
			"Fill discontinuity",
975 976
			"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.",
977 978 979 980 981
			FALSE,
			G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT
		)
	);

982 983 984 985 986 987
	g_object_class_install_property(
		gobject_class,
		ARG_REPLACE_VALUE,
		g_param_spec_double(
			"replace-value",
			"Replace value",
988 989
			"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.", 
990 991 992 993 994 995 996 997
			-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,
998
		gst_param_spec_array(
999 1000
			"bad-data-intervals",
			"Bad data intervals",
1001 1002 1003 1004
			"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.",
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
			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",
1022 1023
			"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.",
1024 1025 1026 1027
			0, G_MAXUINT64, G_MAXUINT64 / 2,
			G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT
		)
	);
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039

	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
		)
	);
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

	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
		)
	);
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
}


/*
 * 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 */
1086
	element->t0 = GST_CLOCK_TIME_NONE;
1087 1088 1089
	element->offset0 = GST_BUFFER_OFFSET_NONE;
	element->next_out_offset = GST_BUFFER_OFFSET_NONE;
	g_mutex_init(&element->mutex);
1090 1091
	element->bad_data_intervals = NULL;
	element->array_length = 0;
1092
	element->rate = 0;
1093
	element->channels = 0;
1094
	element->unit_size = 0;
1095
	element->last_sinkbuf_ets = 0;
1096 1097
	element->discont_time = 0;
	element->empty_bufs = 0;
1098
	element->timeout_bufs = 0;
1099 1100 1101

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