File: dtmf.c

package info (click to toggle)
rtpengine 13.5.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,676 kB
  • sloc: ansic: 86,764; perl: 59,422; python: 3,193; sh: 1,030; makefile: 693; asm: 211
file content (991 lines) | stat: -rw-r--r-- 31,433 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
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
674
675
676
677
678
679
680
681
682
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
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
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
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
824
825
826
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
852
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
#include "dtmf.h"

#include <errno.h>

#include "bencode.h"
#include "control_ng.h"
#include "media_socket.h"
#include "log.h"
#include "call.h"
#include "dtmflib.h"
#include "main.h"
#include "rtplib.h"
#include "codec.h"
#include "ssrc.h"

static socket_t dtmf_log_sock;


static void dtmf_trigger_block_action(struct call_media *, struct call_monologue *);
static void dtmf_trigger_block_digit(struct call_media *, struct call_monologue *);
static void dtmf_trigger_unblock_action(struct call_media *, struct call_monologue *);
static void dtmf_trigger_start_rec(struct call_media *, struct call_monologue *);
static void dtmf_trigger_stop_rec(struct call_media *, struct call_monologue *);
static void dtmf_trigger_start_stop_rec(struct call_media *, struct call_monologue *);
static void dtmf_trigger_pause_rec(struct call_media *, struct call_monologue *);
static void dtmf_trigger_pause_resume_rec(struct call_media *, struct call_monologue *);
static void dtmf_trigger_start_pause_resume_rec(struct call_media *, struct call_monologue *);

struct dtmf_trigger_action dtmf_trigger_actions[__NUM_DTMF_TRIGGERS] = {
	[DTMF_TRIGGER_BLOCK] = {
		.matched = dtmf_trigger_block_action,
		.repeatable = false,
		.digit = dtmf_trigger_block_digit,
	},
	[DTMF_TRIGGER_UNBLOCK] = {
		.matched = dtmf_trigger_unblock_action,
		.repeatable = false,
	},
	[DTMF_TRIGGER_START_REC] = {
		.matched = dtmf_trigger_start_rec,
		.repeatable = true,
	},
	[DTMF_TRIGGER_STOP_REC] = {
		.matched = dtmf_trigger_stop_rec,
		.repeatable = true,
	},
	[DTMF_TRIGGER_START_STOP_REC] = {
		.matched = dtmf_trigger_start_stop_rec,
		.repeatable = true,
	},
	[DTMF_TRIGGER_PAUSE_REC] = {
		.matched = dtmf_trigger_pause_rec,
		.repeatable = true,
	},
	[DTMF_TRIGGER_PAUSE_RESUME_REC] = {
		.matched = dtmf_trigger_pause_resume_rec,
		.repeatable = true,
	},
	[DTMF_TRIGGER_START_PAUSE_RESUME_REC] = {
		.matched = dtmf_trigger_start_pause_resume_rec,
		.repeatable = true,
	},
};

const char *dtmf_trigger_types[__NUM_DTMF_TRIGGERS] = {
	[DTMF_TRIGGER_BLOCK] = "block DTMF",
	[DTMF_TRIGGER_UNBLOCK] = "unblock DTMF",
	[DTMF_TRIGGER_START_REC] = "start recording",
	[DTMF_TRIGGER_STOP_REC] = "stop recording",
	[DTMF_TRIGGER_START_STOP_REC] = "start/stop recording",
	[DTMF_TRIGGER_PAUSE_REC] = "pause recording",
	[DTMF_TRIGGER_PAUSE_RESUME_REC] = "pause/resume recording",
	[DTMF_TRIGGER_START_PAUSE_RESUME_REC] = "start/pause/resume recording",
};


bool dtmf_init(void) {
	ilog(LOG_DEBUG, "log dtmf over ng %d", rtpe_config.dtmf_via_ng);
	ilog(LOG_DEBUG, "no log injected dtmf %d", rtpe_config.dtmf_no_log_injects);
	if (!open_v46_socket(&dtmf_log_sock, SOCK_DGRAM)) {
		ilog(LOG_ERR, "Failed to open/connect DTMF logging socket: %s", strerror(errno));
		return false;
	}
	return true;
}

static unsigned int dtmf_volume_from_dsp(int vol) {
	if (vol > 0)
		return 0;
	else if (vol >= -63)
		return -1 * vol;
	else
		return 63;
}
char dtmf_code_to_char(int code) {
	static const char codes[] = "0123456789*#ABCD";
	if (code < 0 || code > 15)
		return 0;
	return codes[code];
}


static void dtmf_bencode_and_notify(struct call_media *media, unsigned int event, unsigned int volume,
		unsigned int duration, const endpoint_t *fsin, int clockrate)
{
	call_t *call = media->call;
	struct call_monologue *ml = media->monologue;

	bencode_buffer_t bencbuf;
	bencode_item_t *notify, *data, *tags;
	str encoded_data;
	int ret = bencode_buffer_init(&bencbuf);
	assert(ret == 0);

	notify = bencode_dictionary(&bencbuf);
	bencode_dictionary_add_string(notify, "notify", "onDTMF");
	data = bencode_dictionary_add_dictionary(notify, "data");
	tags = bencode_dictionary_add_list(data, "tags");

	bencode_dictionary_add_str(data, "callid", &call->callid);
	bencode_dictionary_add_str(data, "source_tag", &ml->tag);
	if (ml->label.s) {
		bencode_dictionary_add_str(data, "source_label", &ml->label);
	}

	tags_ht_iter iter;
	t_hash_table_iter_init(&iter, call->tags);
	struct call_monologue *tml;
	while (t_hash_table_iter_next(&iter, NULL, &tml))
		bencode_list_add_str(tags, &tml->tag);

	bencode_dictionary_add_string(data, "type", "DTMF");
	bencode_dictionary_add_string(data, "source_ip", sockaddr_print_buf(&fsin->address));
	bencode_dictionary_add_integer(data, "timestamp", rtpe_now / 1000000);
	bencode_dictionary_add_integer(data, "event", event);
	bencode_dictionary_add_integer(data, "duration", ((long long) duration * (1000000LL / clockrate)) / 1000LL);
	bencode_dictionary_add_integer(data, "volume", volume);

	encoded_data = bencode_collapse_str(notify);
	notify_ng_tcp_clients(&encoded_data);
	bencode_buffer_free(&bencbuf);
}

static GString *dtmf_json_print(struct call_media *media, unsigned int event, unsigned int volume,
		unsigned int duration,
		const endpoint_t *fsin, int clockrate)
{
	call_t *call = media->call;
	struct call_monologue *ml = media->monologue;

	GString *buf = g_string_new("");

	if (!clockrate)
		clockrate = 8000;

	g_string_append_printf(buf, "{"
			"\"callid\":\"" STR_FORMAT "\","
			"\"source_tag\":\"" STR_FORMAT "\","
			"\"source_label\":\"" STR_FORMAT "\","
			"\"tags\":[",
			STR_FMT(&call->callid),
			STR_FMT(&ml->tag),
			STR_FMT(ml->label.s ? &ml->label : &STR_EMPTY));

	tags_ht_iter iter;
	t_hash_table_iter_init(&iter, call->tags);
	int i = 0;
	struct call_monologue *tml;
	while (t_hash_table_iter_next(&iter, NULL, &tml)) {
		if (i != 0)
			g_string_append(buf, ",");
		g_string_append_printf(buf, "\"" STR_FORMAT "\"",
				STR_FMT(&tml->tag));
		i++;
	}

	g_string_append_printf(buf, "],"
			"\"type\":\"DTMF\",\"timestamp\":%lu,\"source_ip\":\"%s\","
			"\"event\":%u,\"duration\":%u,\"volume\":%u}",
			(unsigned long) rtpe_now / 1000000,
			sockaddr_print_buf(&fsin->address),
			(unsigned int) event,
			(duration * (1000000 / clockrate)) / 1000,
			(unsigned int) volume);

	return buf;
}

bool dtmf_do_logging(const call_t *c, bool injected) {
	if (injected && rtpe_config.dtmf_no_log_injects)
		return false;
	if (_log_facility_dtmf)
		return true;
	if (rtpe_config.dtmf_udp_ep.port)
		return true;
	if (c->dtmf_log_dest.address.family)
		return true;
	if (rtpe_config.dtmf_via_ng)
		return true;
	return false;
}

// media->dtmf_lock must be held
static void dtmf_end_event(struct call_media *media, unsigned int event, unsigned int volume,
		unsigned int duration, const endpoint_t *fsin, int clockrate, bool rfc_event, uint64_t ts, bool injected)
{
	if (!clockrate)
		clockrate = 8000;

	// don't add to recv list when it's injected, it can cause the list TS's to be out
	// of order breaking the dtmf-security and letting the generated PCM frames through
	if (!injected) {
		struct dtmf_event *ev = g_new0(__typeof(*ev), 1);
		*ev = (struct dtmf_event) { .code = 0, .ts = ts, .volume = 0 };
		t_queue_push_tail(&media->dtmf_recv, ev);
	}

	// only add to send list if injected, a delayed send, or not being blocked
	if (injected || !media->monologue->block_dtmf || media->monologue->dtmf_delay) {
		struct dtmf_event *ev = g_new0(__typeof(*ev), 1);
		*ev = (struct dtmf_event) { .code = 0, .ts = ts + media->monologue->dtmf_delay * clockrate / 1000,
			.volume = 0, .block_dtmf = media->monologue->block_dtmf };
		t_queue_push_tail(&media->dtmf_send, ev);
	}

	if (!dtmf_do_logging(media->call, injected))
		return;

	GString *buf = dtmf_json_print(media, event, volume, duration, fsin, clockrate);

	if (_log_facility_dtmf)
		dtmflog(buf);

	const endpoint_t *udp_dst = NULL;
	if (media->call->dtmf_log_dest.address.family)
		udp_dst = &media->call->dtmf_log_dest;
	else if (rtpe_config.dtmf_udp_ep.address.family)
		udp_dst = &rtpe_config.dtmf_udp_ep;

	if (udp_dst)
		if (socket_sendto(&dtmf_log_sock, buf->str, buf->len, udp_dst) < 0)
			ilog(LOG_ERR, "Error sending DTMF event info to UDP destination %s: %s",
					endpoint_print_buf(udp_dst),
					strerror(errno));

	if (rtpe_config.dtmf_via_ng)
		dtmf_bencode_and_notify(media, event, volume, duration, fsin, clockrate);
	g_string_free(buf, TRUE);
}

static struct dtmf_trigger_state *dtmf_get_trigger_state(struct call_monologue *ml, enum dtmf_trigger_type type)
{
	// Look up entry in ->dtmf_triger_state. If trigger is set already, its index
	// is stored in dtmf_trigger_index. If it isn't, grab a new entry.
	// The index must be less then num_triggers and the type of the entry pointed
	// to by the index must match the requested type. Everything else is invalid
	// and requires a new entry.
	// This keeps all set triggers at the front of the list and doesn't pollute
	// the list with unset entries, while still allowing quick lookup.
	// trigger_state[trigger_index[type]].type == type
	// trigger_index[trigger_state[idx].type] == idx

	unsigned int idx = ml->dtmf_trigger_index[type];
	if (idx >= ml->num_dtmf_triggers)
		return NULL;
	struct dtmf_trigger_state *state = &ml->dtmf_trigger_state[idx];
	if (state->type != type)
		return NULL;
	return state;
}

void dtmf_trigger_set(struct call_monologue *ml, enum dtmf_trigger_type trigger_type,
		const str *s, bool inactive)
{

	struct dtmf_trigger_state *state = dtmf_get_trigger_state(ml, trigger_type);

	if (!state) {
		// Trigger doesn't exist yet. Do we actually want to set a trigger?
		if (s->len == 0)
			return; // nothing to do

		// fill in a new entry
		assert(ml->num_dtmf_triggers < __NUM_DTMF_TRIGGERS);
		state = &ml->dtmf_trigger_state[ml->num_dtmf_triggers];
		ml->dtmf_trigger_index[trigger_type] = ml->num_dtmf_triggers;
		state->type = trigger_type;
		ml->num_dtmf_triggers++;

		// Trigger is set below
	}
	else {
		// Trigger is already set. Do we want to delete it?
		if (s->len == 0) {
			// Shift down remaining items and adjust indexes
			unsigned int idx = state - ml->dtmf_trigger_state;
			for (unsigned int i = idx; i < ml->num_dtmf_triggers - 1; i++) {
				assert(ml->dtmf_trigger_index[ml->dtmf_trigger_state[i].type] == i);
				assert(ml->dtmf_trigger_index[ml->dtmf_trigger_state[i + 1].type] == i + 1);
				ml->dtmf_trigger_state[i] = ml->dtmf_trigger_state[i + 1];
				ml->dtmf_trigger_index[ml->dtmf_trigger_state[i].type] = i;
			}
			ml->num_dtmf_triggers--;
			return;
		}

		// Replace existing trigger below
	}

	ilog(LOG_DEBUG, "Setting DTMF trigger '%s' (at idx %u) to '" STR_FORMAT "'",
			dtmf_trigger_types[trigger_type],
			(unsigned int) (state - ml->dtmf_trigger_state), STR_FMT(s));

	state->trigger = call_str_cpy(s);
	state->matched = 0;
	state->inactive = inactive;
}

static void dtmf_trigger_set_block(call_t *c, codec_timer_callback_arg_t a) {
	struct call_monologue *ml = a.ml;

	rwlock_lock_w(&c->master_lock);

	struct dtmf_trigger_state *end_trigger = dtmf_get_trigger_state(ml, DTMF_TRIGGER_UNBLOCK);

	if (end_trigger)
		ilog(LOG_INFO, "Setting DTMF block mode to %i and enabling end trigger '" STR_FORMAT "'",
				ml->block_dtmf_trigger, STR_FMT(&end_trigger->trigger));
	else
		ilog(LOG_INFO, "Setting DTMF block mode to %i",
				ml->block_dtmf_trigger);

	ml->block_dtmf = ml->block_dtmf_trigger;

	// enable end trigger
	if (end_trigger) {
		end_trigger->inactive = false;
		ml->dtmf_trigger_digits *= -1; // negative means it's active
	}

	codec_update_all_handlers(ml);

	rwlock_unlock_w(&c->master_lock);
}
static void dtmf_trigger_unset_block(call_t *c, codec_timer_callback_arg_t a) {
	struct call_monologue *ml = a.ml;

	ilog(LOG_INFO, "Setting DTMF block mode to %i", ml->block_dtmf_trigger_end);

	rwlock_lock_w(&c->master_lock);

	ml->block_dtmf = ml->block_dtmf_trigger_end;
	dtmf_trigger_set(ml, DTMF_TRIGGER_BLOCK, NULL, false);

	codec_update_all_handlers(ml);

	rwlock_unlock_w(&c->master_lock);
}

// dtmf_lock must be held
static void dtmf_trigger_block_digit(struct call_media *media, struct call_monologue *ml) {
	if (ml->dtmf_trigger_digits >= 0)
		return;

	// end trigger is active
	ml->dtmf_trigger_digits++;
	if (ml->dtmf_trigger_digits == 0) {
		// got all digits
		codec_timer_callback(ml->call, dtmf_trigger_unset_block, ml, 0);
	}
}

// dtmf_lock must be held
static void dtmf_trigger_block_action(struct call_media *media, struct call_monologue *ml) {
	ilog(LOG_INFO, "DTMF trigger matched, setting block mode to %i",
			ml->block_dtmf_trigger);

	// We only hold a read-lock on the call here and cannot switch to a write-lock
	// easily, which is needed to reset the codec handlers. Therefore we do this
	// asynchronously:
	codec_timer_callback(ml->call, dtmf_trigger_set_block, ml, 0);

	// set up unblock triggers
	if (ml->block_dtmf_trigger_end_ms)
		codec_timer_callback(ml->call, dtmf_trigger_unset_block, ml,
				ml->block_dtmf_trigger_end_ms * 1000);
}

// dtmf_lock must be held
static void dtmf_trigger_unblock_action(struct call_media *media, struct call_monologue *ml) {
	ilog(LOG_INFO, "DTMF trigger matched, setting block mode to %i",
			ml->block_dtmf_trigger);

	// We only hold a read-lock on the call here and cannot switch to a write-lock
	// easily, which is needed to reset the codec handlers. Therefore we do this
	// asynchronously:
	codec_timer_callback(ml->call, dtmf_trigger_unset_block, ml, 0);
}

// dtmf_lock must be held
static bool dtmf_check_1_trigger(struct call_media *media, struct call_monologue *ml,
		char event, uint64_t ts, int clockrate, unsigned int i)
{
	struct dtmf_trigger_state *state = &ml->dtmf_trigger_state[i];
	struct dtmf_trigger_action *action = &dtmf_trigger_actions[state->type];

	if (state->matched >= state->trigger.len) // is the trigger done already?
		return false;

	if (action->digit)
		action->digit(media, ml);

	// is the new event a match?
	if (state->trigger.s[state->matched] == event) {
		state->matched++;
		if (state->matched == state->trigger.len) {
			// trigger is finished
			state->matched = 0; // reset

			ilog(LOG_INFO, "DTMF VSC '%s' ('" STR_FORMAT "') triggered",
					dtmf_trigger_types[state->type], STR_FMT(&state->trigger));

			action->matched(media, ml);

			if (!action->repeatable)
				dtmf_trigger_set(ml, state->type, NULL, false);

			return true;
		}
		return false;
	}

	// can we do a partial match?
	for (size_t off = 1; off < state->matched; off++) {
		// look for repeating prefix: trigger "ABCABD", matched 5, prefix at offset 3: [AB]C[AB]
		if (memcmp(state->trigger.s + off, state->trigger.s, state->matched - off))
			continue;
		// is the new event a match?
		unsigned int next_match_idx = state->trigger.len - off;
		if (state->trigger.s[next_match_idx] == event) {
			// got a partial match
			state->matched = next_match_idx;
			return false;
		}
	}
	// no partial match... reset completely
	if (event == state->trigger.s[0])
		state->matched = 1;
	else
		state->matched = 0;

	return false;
}

// dtmf_lock must be held
static void dtmf_check_trigger(struct call_media *media, char event, uint64_t ts, int clockrate) {
	if (!clockrate)
		clockrate = 8000;

	struct call_monologue *ml = media->monologue;

	if (!ml->num_dtmf_triggers)
		return; // nothing to do

	// check delay from previous event
	bool reset = false;
	struct dtmf_event *last_ev = t_queue_peek_tail(&media->dtmf_recv);
	if (last_ev) {
		uint32_t ts_diff = ts - last_ev->ts;
		uint64_t ts_diff_ms = (uint64_t) ts_diff * 1000 / clockrate;
		if (ts_diff_ms > rtpe_config.dtmf_digit_delay) {
			// delay too long: restart event trigger
			reset = true;
		}
	}

	for (unsigned int i = 0; i < ml->num_dtmf_triggers; i++) {
		if (reset)
			ml->dtmf_trigger_state[i].matched = 0;

		if (dtmf_check_1_trigger(media, ml, event, ts, clockrate, i))
			break; // triggers should be unique, so only act on one
	}
}

// media->dtmf_lock must be held
static void dtmf_code_event(struct call_media *media, char event, uint64_t ts, int clockrate, int volume, bool injected) {
	struct dtmf_event *ev = t_queue_peek_tail(&media->dtmf_recv);
	if (ev && ev->code == event)
		return;

	// start of new event

	// check trigger before setting new dtmf_start
	dtmf_check_trigger(media, event, ts, clockrate);

	// don't add to recv list when it's injected, it can cause the list TS's to be out
	// of order breaking the dtmf-security and letting the generated PCM frames through
	if (!injected) {
		ev = g_new0(__typeof(*ev), 1);
		*ev = (struct dtmf_event) { .code = event, .ts = ts, .volume = volume,
			.rand_code = '0' + (ssl_random() % 10), .index = media->dtmf_count };
		t_queue_push_tail(&media->dtmf_recv, ev);
	}

	// only add to send list if injected, a delayed send, or not being blocked
	if (injected || !media->monologue->block_dtmf || media->monologue->dtmf_delay) {
		ev = g_new0(__typeof(*ev), 1);
		*ev = (struct dtmf_event) { .code = event, .ts = ts + media->monologue->dtmf_delay * clockrate / 1000,
			.volume = volume,
			.block_dtmf = media->monologue->block_dtmf };
		t_queue_push_tail(&media->dtmf_send, ev);
	}

	media->dtmf_count++;
}


struct dtmf_event *is_in_dtmf_event(dtmf_event_q *events, uint32_t this_ts, int clockrate, unsigned int head,
		unsigned int trail)
{
	if (!clockrate)
		clockrate = 8000;
	uint32_t cutoff = clockrate * 10;
	uint32_t neg = ~(clockrate * 100);

	uint32_t start_ts = this_ts + head * clockrate / 1000;
	uint32_t end_ts = this_ts - trail * clockrate / 1000;

	// go backwards through our list of DTMF events
	for (__auto_type l = events->tail; l; l = l->prev) {
		struct dtmf_event *ev = l->data;
		uint32_t ts = ev->ts; // truncate to 32 bits
		if (ev->code) {
			// start event: check TS against our shifted start TS.
			// start_ts must be larger than ts, but not much larger.
			uint32_t start_diff = start_ts - ts;
			// much too large? that means start_ts < ts. keep looking, we're close.
			if (start_diff >= neg)
				continue;
			// diff >= 0 and less than 10 seconds? that's a match.
			if (start_diff <= cutoff)
				return ev;
			// anything else is a bad/outdated TS. stop.
			break;
		}
		else {
			// stop event: check TS against our shifted end TS.
			uint32_t end_diff = end_ts - ts;
			if (end_diff >= neg)
				continue;
			if (end_diff == 0) // for end events, we wait until after the end
				continue;
			if (end_diff <= cutoff)
				return NULL;
			break;
		}
	}

	return NULL;
}


// media->dtmf_lock must be held
int dtmf_event_packet(struct media_packet *mp, str *payload, int clockrate, uint64_t ts) {
	struct telephone_event_payload *dtmf;
	if (payload->len < sizeof(*dtmf)) {
		ilog(LOG_WARN | LOG_FLAG_LIMIT, "Short DTMF event packet (len %zu)", payload->len);
		return -1;
	}
	dtmf = (void *) payload->s;
	uint16_t duration = ntohs(dtmf->duration);

	ilog(LOG_DEBUG, "DTMF event packet: event %u, volume %u, end %u, duration %u",
			dtmf->event, dtmf->volume, dtmf->end, duration);

	if (!dtmf->end) {
		dtmf_code_event(mp->media, dtmf_code_to_char(dtmf->event), ts, clockrate, dtmf->volume, false);
		return 0;
	}

	dtmf_end_event(mp->media, dtmf->event, dtmf->volume, duration,
			&mp->fsin, clockrate, true, ts + duration - 1, false);

	return 1;
}

void dtmf_dsp_event(const struct dtmf_event *new_event, struct dtmf_event *cur_event_p,
		struct call_media *media, int clockrate, uint64_t ts, bool injected)
{
	// update state tracker regardless of outcome
	struct dtmf_event cur_event = *cur_event_p;
	*cur_event_p = *new_event;

	if (!media)
		return;

	bool end_event;
	if (cur_event.code != 0 && new_event->code == 0)
		end_event = true;
	else if (cur_event.code == 0 && new_event->code != 0)
		end_event = false; // start of a new code
	else
		return; // don't care

	if (!media->streams.length)
		return;

	// we don't have a real fsin so just use the stream address
	struct packet_stream *ps = media->streams.head->data;


	LOCK(&media->dtmf_lock);

	if (end_event) {
		unsigned int duration = new_event->ts - cur_event.ts;

		ilog(LOG_DEBUG, "DTMF DSP end event: event %i, volume %i, duration %u",
				cur_event.code, cur_event.volume, duration);

		dtmf_end_event(media, dtmf_code_from_char(cur_event.code), dtmf_volume_from_dsp(cur_event.volume),
				duration, &ps->endpoint, clockrate, false, ts, injected);
	}
	else {
		ilog(LOG_DEBUG, "DTMF DSP code event: event %i, volume %i",
				new_event->code, new_event->volume);
		int code = dtmf_code_from_char(new_event->code); // for validation
		if (code != -1)
			dtmf_code_event(media, (char) new_event->code, ts, clockrate,
					dtmf_volume_from_dsp(new_event->volume), injected);
	}
}

void dtmf_event_free(struct dtmf_event *e) {
	g_free(e);
}

// returns: 0 = no DTMF. 1 = DTMF start event. 2 = DTMF in progress. 3 = DTMF end event.
int dtmf_event_payload(str *buf, uint64_t *pts, uint64_t duration, struct dtmf_event *cur_event,
		dtmf_event_q *events)
{
	// do we have a relevant state change?
	struct dtmf_event prev_event = *cur_event;
	struct dtmf_event *ev = t_queue_peek_head(events);
	while (events->length) {
		ilog(LOG_DEBUG, "Next DTMF event starts at %" PRIu64 ". PTS now %" PRIu64, ev->ts, *pts);
		if (ev->ts > *pts)
			break; // future event

		ilog(LOG_DEBUG, "DTMF state change at %" PRIu64 ": %i -> %i, duration %" PRIu64, ev->ts,
				cur_event->code, ev->code, duration);
		t_queue_pop_head(events);
		*cur_event = *ev;
		dtmf_event_free(ev);
		ev = t_queue_peek_head(events);
		if (ev && ev->code == 0 && cur_event->ts < *pts) {
			// if the start event ts was before *pts we need
			// to adjust the end event_ts to ensure we're not shortening
			// the event
			ilog(LOG_DEBUG, "Delayed send of DTMF, adjusting end event_ts by "
					"%" PRIu64 " - %" PRIu64 " = %" PRIu64,
					*pts, cur_event->ts, *pts - cur_event->ts);
			ev->ts += *pts - cur_event->ts;
		}
		cur_event->ts = *pts; // canonicalise start TS
	}

	int ret = 2; // normal: in progress
	if (cur_event->code == 0) {
		if (prev_event.code == 0)
			return 0;
		// state change from DTMF back to audio. send DTMF end code.
		ret = 3;
		cur_event = &prev_event;
	}
	else if (prev_event.code == 0)
		ret = 1; // start event

	int dtmf_code = dtmf_code_from_char(cur_event->code);
	if (dtmf_code == -1) {
		ilog(LOG_ERR | LOG_FLAG_LIMIT, "Unknown DTMF event code %i", cur_event->code);
		return 0;
	}

	// replace audio RTP frame with DTMF payload
	struct telephone_event_payload *ev_pt = (void *) buf->s;
	buf->len = sizeof(*ev_pt);
	ZERO(*ev_pt);

	ev_pt->event = dtmf_code;
	ev_pt->volume = dtmf_volume_from_dsp(cur_event->volume);
	ev_pt->end = (ret == 3) ? 1 : 0;
	ev_pt->duration = htons(*pts - cur_event->ts + duration);

	// fix up timestamp
	*pts = cur_event->ts;

	return ret;
}

int dtmf_code_from_char(char c) {
	if (c >= '0' && c <= '9')
		return c - '0';
	else if (c == '*')
		return 10;
	else if (c == '#')
		return 11;
	else if (c >= 'A' && c <= 'D')
		return c - 'A' + 12;
	return -1;
}

#ifdef WITH_TRANSCODING

// takes over the csh reference
static const char *dtmf_inject_pcm(struct call_media *media, struct call_media *sink,
		struct call_monologue *monologue,
		struct packet_stream *ps, struct ssrc_entry_call *ssrc_in, struct codec_handler *ch,
		struct codec_ssrc_handler *csh,
		int code, int volume, int duration, int pause)
{
	call_t *call = monologue->call;

	for (__auto_type l = ps->rtp_sinks.head; l; l = l->next) {
		struct sink_handler *sh = l->data;
		struct packet_stream *sink_ps = sh->sink;
		__auto_type sink_media = sink_ps->media;
		packet_sequencer_t *seq = g_hash_table_lookup(ssrc_in->sequencers, sink_ps->media);
		if (!seq)
			continue;

		struct ssrc_entry_call *ssrc_out = get_ssrc(sh->attrs.transcoding ?
					ssrc_in->ssrc_map_out : ssrc_in->h.ssrc,
				&sink_media->ssrc_hash_out);
		if (!ssrc_out)
			return "No output SSRC context present"; // XXX generate stream

		int duration_samples = duration * ch->dest_pt.clock_rate / 1000;
		int pause_samples = pause * ch->dest_pt.clock_rate / 1000;

		// we generate PCM DTMF by simulating a detected RFC event packet
		// XXX this shouldn't require faking an actual RTP packet
		struct telephone_event_payload tep = {
			.event = code,
			.volume = -1 * volume,
			.end = 1,
			.duration = htons(duration_samples),
		};
		struct rtp_header rtp = {
			.m_pt = 0xff,
			.timestamp = 0,
			.seq_num = htons(seq->seq),
			.ssrc = htonl(ssrc_in->h.ssrc),
		};
		struct media_packet packet = {
			.tv = rtpe_now,
			.call = call,
			.media = media,
			.media_out = sink,
			.rtp = &rtp,
			.ssrc_in = ssrc_in,
			.ssrc_out = ssrc_out,
			.raw = { (void *) &tep, sizeof(tep) },
			.payload = { (void *) &tep, sizeof(tep) },
		};

		// keep track of how much PCM we've generated
		uint64_t encoder_pts = codec_encoder_pts(csh, NULL);
		uint64_t skip_pts = codec_decoder_unskip_pts(csh); // reset to zero to take up our new samples

		ch->dtmf_injector->handler_func(ch->dtmf_injector, &packet);

		// insert pause
		tep.event = 0xff;
		tep.duration = htons(pause_samples);
		rtp.seq_num = htons(seq->seq);

		ch->dtmf_injector->handler_func(ch->dtmf_injector, &packet);

		// skip generated samples
		uint64_t pts_offset = codec_encoder_pts(csh, NULL) - encoder_pts;
		skip_pts += av_rescale(pts_offset, ch->dest_pt.clock_rate, ch->source_pt.clock_rate);
		codec_decoder_skip_pts(csh, skip_pts);

		// ready packets for send
		// XXX handle encryption?

		media_socket_dequeue(&packet, sink_ps);

		obj_put_o((struct obj *) csh);
		ssrc_entry_release(ssrc_out);
	}

	return 0;
}

const char *dtmf_inject(struct call_media *media, int code, int volume, int duration, int pause,
		struct call_media *sink)
{
	struct call_monologue *monologue = media->monologue;

	if (!media->streams.head)
		return "Media doesn't have an RTP stream";
	struct packet_stream *ps = media->streams.head->data;
	struct ssrc_entry_call *ssrc_in = call_get_first_ssrc(&media->ssrc_hash_in);
	if (!ssrc_in)
		return "No SSRC context present for DTMF injection"; // XXX fall back to generating stream

	// create RFC DTMF events. we do this by simulating a detected PCM DTMF event
	// find payload type to use
	struct codec_handler *ch = NULL;
	struct codec_ssrc_handler *csh = NULL;
	int pt = -1;
	int ch_pt = -1;
	for (int i = 0; i < ssrc_in->tracker.most_len; i++) {
		pt = ssrc_in->tracker.most[i];
		if (pt == 255)
			continue;

		ch = codec_handler_get(media, pt, sink, NULL);
		if (!ch)
			continue;
		// for DTMF delay, payload type will be -1 but the real payload type will be correct
		// and as we're specifically injecting we want to make sure we end up checking the right pt
		ch_pt = ch->real_dtmf_payload_type != -1 ? ch->real_dtmf_payload_type : ch->dtmf_payload_type;
		// skip DTMF PTs
		if (pt == ch_pt)
			continue;
		if (ch->output_handler) // context switch if we have multiple inputs going to one output
			ch = ch->output_handler;

		ilog(LOG_DEBUG, "DTMF injection: Using PT %i/%i -> %i (%i), SSRC %" PRIx32,
				pt,
				ch->source_pt.payload_type,
				ch->dest_pt.payload_type,
				ch_pt,
				ssrc_in->h.ssrc);

		csh = get_ssrc(ssrc_in->h.ssrc, &ch->ssrc_hash);
		if (!csh)
			continue;
		break;
	}

	if (pt < 0 || pt == 255)
		return "No RTP payload type found to be in use"; // XXX generate stream
	if (!ch)
		return "No matching codec handler";
	if (!csh)
		return "No matching codec SSRC handler";

	// if we don't have a DTMF payload type, we have to generate PCM
	if (ch_pt == -1 && ch->dtmf_injector)
		return dtmf_inject_pcm(media, sink, monologue, ps, ssrc_in, ch, csh, code, volume, duration,
				pause);

	ilog(LOG_DEBUG, "Injecting RFC DTMF event #%i for %i ms (vol %i) from '" STR_FORMAT "' (media #%u) "
			"into RTP PT %i, SSRC %" PRIx32,
			code, duration, volume, STR_FMT(&monologue->tag), media->index, pt,
			ssrc_in->h.ssrc);

	// synthesise start and stop events
	// the num_samples needs to be based on the the previous packet timestamp so we need to
	// reduce it by one packets worth or we'll generate one too many packets than requested
	uint64_t num_samples = (uint64_t) (duration - ch->dest_pt.ptime) * ch->dest_pt.clock_rate / 1000;
	uint64_t start_pts = codec_encoder_pts(csh, ssrc_in);
	// get the last event end time, and increase by the required pause
	// conversely to the above, we need to add the last packet num samples to its TS before adding
	// a pause so we dont generate one packet too few
	// if that's later than start_pts, we need to adjust it
	uint64_t last_end_pts = codec_last_dtmf_event(csh);
	if (last_end_pts) {
		last_end_pts += (pause + ch->dest_pt.ptime) * ch->dest_pt.clock_rate / 1000;
		if (last_end_pts > start_pts)
			start_pts = last_end_pts;
	}

	codec_add_dtmf_event(csh, dtmf_code_to_char(code), volume, start_pts, true);
	codec_add_dtmf_event(csh, 0, 0, start_pts + num_samples, true);

	obj_put_o((struct obj *) csh);
	return NULL;
}

#endif


enum block_dtmf_mode dtmf_get_block_mode(call_t *call, struct call_monologue *ml) {
	if (!call) {
		if (!ml)
			return BLOCK_DTMF_OFF;
		call = ml->call;
	}

	if (call && call->block_dtmf)
		return call->block_dtmf;
	if (!ml)
		return BLOCK_DTMF_OFF;
	return ml->block_dtmf;
}

bool is_pcm_dtmf_block_mode(enum block_dtmf_mode mode) {
	if (mode >= BLOCK_DTMF___PCM_REPLACE_START && mode <= BLOCK_DTMF___PCM_REPLACE_END)
		return true;
	return false;
}

bool is_dtmf_replace_mode(enum block_dtmf_mode mode) {
	if (mode >= BLOCK_DTMF___REPLACE_START && mode <= BLOCK_DTMF___REPLACE_END)
		return true;
	return false;
}

static void dtmf_trigger_do_start_rec(call_t *c, codec_timer_callback_arg_t a) {
	rwlock_lock_w(&c->master_lock);
	recording_start(c);
	rwlock_unlock_w(&c->master_lock);
}

// dtmf_lock must be held
static void dtmf_trigger_start_rec(struct call_media *media, struct call_monologue *ml) {
	codec_timer_callback(ml->call, dtmf_trigger_do_start_rec, ml, 0);
}

static void dtmf_trigger_do_stop_rec(call_t *c, codec_timer_callback_arg_t a) {
	rwlock_lock_w(&c->master_lock);
	recording_stop(c);
	rwlock_unlock_w(&c->master_lock);
}

// dtmf_lock must be held
static void dtmf_trigger_stop_rec(struct call_media *media, struct call_monologue *ml) {
	codec_timer_callback(ml->call, dtmf_trigger_do_stop_rec, ml, 0);
}

static void dtmf_trigger_do_start_stop_rec(call_t *c, codec_timer_callback_arg_t a) {
	rwlock_lock_w(&c->master_lock);
	if (c->recording)
		recording_stop(c);
	else
		recording_start(c);
	rwlock_unlock_w(&c->master_lock);
}

// dtmf_lock must be held
static void dtmf_trigger_start_stop_rec(struct call_media *media, struct call_monologue *ml) {
	codec_timer_callback(ml->call, dtmf_trigger_do_start_stop_rec, ml, 0);
}

static void dtmf_trigger_do_pause_rec(call_t *c, codec_timer_callback_arg_t a) {
	rwlock_lock_w(&c->master_lock);
	recording_pause(c);
	rwlock_unlock_w(&c->master_lock);
}

// dtmf_lock must be held
static void dtmf_trigger_pause_rec(struct call_media *media, struct call_monologue *ml) {
	codec_timer_callback(ml->call, dtmf_trigger_do_pause_rec, ml, 0);
}

static void dtmf_trigger_do_pause_resume_rec(call_t *c, codec_timer_callback_arg_t a) {
	rwlock_lock_w(&c->master_lock);
	if (!c->recording) {
		rwlock_unlock_w(&c->master_lock);
		return;
	}
	if (CALL_SET(c, RECORDING_ON))
		recording_pause(c);
	else
		recording_start(c);
	rwlock_unlock_w(&c->master_lock);
}

// dtmf_lock must be held
static void dtmf_trigger_pause_resume_rec(struct call_media *media, struct call_monologue *ml) {
	codec_timer_callback(ml->call, dtmf_trigger_do_pause_resume_rec, ml, 0);
}

static void dtmf_trigger_do_start_pause_resume_rec(call_t *c, codec_timer_callback_arg_t a) {
	rwlock_lock_w(&c->master_lock);
	if (CALL_SET(c, RECORDING_ON))
		recording_pause(c);
	else
		recording_start(c);
	rwlock_unlock_w(&c->master_lock);
}

// dtmf_lock must be held
static void dtmf_trigger_start_pause_resume_rec(struct call_media *media, struct call_monologue *ml) {
	codec_timer_callback(ml->call, dtmf_trigger_do_start_pause_resume_rec, ml, 0);
}