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


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


404 405 406 407 408 409 410
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);

411 412 413 414 415
	GstDateTime *current_gst_time;
	gchar *current_utc_time;
	struct tm tm;
	guint64 current_time, ets_min;

416 417 418 419 420 421 422 423
	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.
		 */
424
naptime:
425
		/* don't hog a billion CPUs */
426 427 428 429
		if(element->block_duration < G_MAXUINT64 / 2)
			sleep(element->block_duration / 1000000000.0);
		else
			sleep(1);
430 431

		/* Get the current real time as a string */
432 433 434 435
		current_gst_time = gst_date_time_new_now_utc();
		current_utc_time = gst_date_time_to_iso8601_string(current_gst_time);
		if(current_utc_time == NULL)
			goto naptime;
436 437 438 439

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

440
		/* Time in nanoseconds */
441 442 443
		current_time = (guint64) XLALUTCToGPS(&tm) * 1000000000 + (guint64) gst_date_time_get_microsecond(current_gst_time) * 1000;
		gst_date_time_unref(current_gst_time);
		g_free(current_utc_time);
444 445 446 447 448
		/*
		 * 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.
		 */
449
		ets_min = current_time - element->wait_time;
450 451
		/* 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);
452

453
		g_mutex_lock(&element->mutex);
454
		if((double) ets_min - (double) element->last_sinkbuf_ets > 1000000000.0 / element->rate) {
455 456 457 458
			/*
			 * 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.
			 */
459 460 461
			GstFlowReturn result;
			switch(element->data_type) {
			case GSTLAL_INSERTGAP_U32:
462
				result = process_inbuf_guint32(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, ets_min, FALSE, FALSE);
463 464
				break;
			case GSTLAL_INSERTGAP_F32:
465
				result = process_inbuf_float(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, ets_min, FALSE, FALSE);
466 467
				break;
			case GSTLAL_INSERTGAP_F64:
468
				result = process_inbuf_double(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, ets_min, FALSE, FALSE);
469 470
				break;
			case GSTLAL_INSERTGAP_Z64:
471
				result = process_inbuf_floatcomplex(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, ets_min, TRUE, FALSE);
472 473
				break;
			case GSTLAL_INSERTGAP_Z128:
474
				result = process_inbuf_doublecomplex(NULL, NULL, element, TRUE, TRUE, 0, 0, 0, ets_min, TRUE, FALSE);
475 476 477 478 479 480 481 482 483
				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));

		}
484
		g_mutex_unlock(&element->mutex);
485

486
		if(element->finished_running)
487 488 489 490 491
			pthread_exit(NULL);
	}
}


492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
/*
 * ============================================================================
 *
 *				     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);
517 518 519 520
		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);
521
		if(success) {
522
			/* record stream parameters */
523
			element->rate = GST_AUDIO_INFO_RATE(&info);
524
			element->channels = GST_AUDIO_INFO_CHANNELS(&info);
525
			element->unit_size = GST_AUDIO_INFO_BPF(&info);
526 527
			if(!strcmp(name, GST_AUDIO_NE(U32))) {
				element->data_type = GSTLAL_INSERTGAP_U32;
528
				g_assert_cmpuint(element->unit_size, ==, element->channels * 4);
529
			} else if(!strcmp(name, GST_AUDIO_NE(F32))) {
530
				element->data_type = GSTLAL_INSERTGAP_F32;
531
				g_assert_cmpuint(element->unit_size, ==, element->channels * 4);
532 533
			} else if(!strcmp(name, GST_AUDIO_NE(F64))) {
				element->data_type = GSTLAL_INSERTGAP_F64;
534
				g_assert_cmpuint(element->unit_size, ==, element->channels * 8);
535 536
			} else if(!strcmp(name, GST_AUDIO_NE(Z64))) {
				element->data_type = GSTLAL_INSERTGAP_Z64;
537
				g_assert_cmpuint(element->unit_size, ==, element->channels * 8);
538 539
			} else if(!strcmp(name, GST_AUDIO_NE(Z128))) {
				element->data_type = GSTLAL_INSERTGAP_Z128;
540
				g_assert_cmpuint(element->unit_size, ==, element->channels * 16);
541 542
			} else
				g_assert_not_reached();
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 568 569 570
		}
		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));

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

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

609 610 611
	/* 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)));
612 613 614 615 616 617 618 619 620
		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;
	}

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

652
	g_mutex_lock(&element->mutex);
653

654
	/* We'll need these to decide gaps, offsets, and timestamps on the outgoing buffer(s) */
655
	gboolean sinkbuf_gap = GST_BUFFER_FLAG_IS_SET(sinkbuf, GST_BUFFER_FLAG_GAP);
656
	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));
657
	guint64 sinkbuf_offset = GST_BUFFER_OFFSET(sinkbuf);
658
	guint64 sinkbuf_offset_end = GST_BUFFER_OFFSET_END(sinkbuf);
659
	GstClockTime sinkbuf_dur = GST_BUFFER_DURATION(sinkbuf);
660 661
	GstClockTime sinkbuf_pts = GST_BUFFER_PTS(sinkbuf);

662 663 664 665 666 667
	/* 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);
	}

668 669 670 671 672 673 674 675 676 677 678 679 680 681
	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:
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 705 706
	}

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

709 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
	/*
	 * done
	 */

done:
	return result;
}


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


/*
 * properties
 */


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


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

784 785 786 787 788
		break;
	}
	case ARG_BLOCK_DURATION:
		element->block_duration = g_value_get_uint64(value);
		break;
789 790 791
	case ARG_CHOP_LENGTH:
		element->chop_length = g_value_get_uint64(value);
		break;
792 793 794
	case ARG_WAIT_TIME:
		element->wait_time = g_value_get_uint64(value);
		break;
795 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
	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;
823 824 825
	case ARG_FILL_DISCONT:
		g_value_set_boolean(value, element->fill_discont);
		break;
826 827 828
	case ARG_REPLACE_VALUE:
		g_value_set_double(value, element->replace_value);
		break;
829 830 831 832 833 834 835 836 837 838 839 840 841 842
	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);

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

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

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

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

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

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

	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
		)
	);
1054 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
}


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

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