annotate mgw/dtmf_timer.c @ 127:f062c32a5116

mgw: implement DTMF
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 01 Oct 2022 20:31:15 -0800
parents
children 529906fddcfa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
127
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * In this module we implement the timer function of DTMF generation.
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/socket.h>
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/time.h>
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <netinet/in.h>
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdint.h>
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdlib.h>
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <string.h>
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <strings.h>
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <syslog.h>
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include "struct.h"
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include "int_defs.h"
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 #include "dtmf_defs.h"
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 extern struct timeval cur_event_time;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 extern struct endpoint *dtmf_list_head;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 int dtmf_timer_running;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 struct timeval dtmf_next_time;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 void
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 start_dtmf_timer()
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 {
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 if (dtmf_timer_running)
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 return;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 dtmf_next_time = cur_event_time;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 dtmf_timer_running = 1;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 }
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 dtmf_timer_one(ep)
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 struct endpoint *ep;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 {
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 struct rtp_packet pkt;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 socklen_t addrlen;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 unsigned frame_limit;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 pkt.v_p_x_cc = 0x80;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 pkt.m_pt = ep->pstn_payload_type;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (ep->dtmf_m_bit) {
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 pkt.m_pt |= 0x80;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 ep->dtmf_m_bit = 0;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 }
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 pkt.seq = htons(++ep->g2p_out_seq);
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 ep->dtmf_last_ts += SAMPLES_PER_FRAME;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 pkt.tstamp = htonl(ep->dtmf_last_ts);
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 pkt.ssrc = ep->g2p_ssrc;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 g711_encode_frame(ep->dtmf_sample_ptr, pkt.payload,
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 ep->pstn_payload_type);
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 ep->dtmf_sample_ptr += SAMPLES_PER_FRAME;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 addrlen = sizeof(struct sockaddr_in);
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 sendto(ep->rtp_pstn.rtp_fd, &pkt, RTP_PACKET_SIZE_PSTN, 0,
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 (struct sockaddr *) &ep->rtp_pstn.remote_addr, addrlen);
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 ep->dtmf_frames_sent++;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 frame_limit = ep->dtmf_stop_req ? DTMF_MIN_FRAMES : DTMF_MAX_FRAMES;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 if (ep->dtmf_frames_sent >= frame_limit)
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 return 1;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 else
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 return 0;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 }
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 void
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 dtmf_timer_process()
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 {
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 struct endpoint *ep;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 int fin;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 for (ep = dtmf_list_head; ep; ep = ep->dtmf_next) {
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 fin = dtmf_timer_one(ep);
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 if (!fin)
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 continue;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 ep->dtmf_aftermath = 1;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 *ep->dtmf_pp = ep->dtmf_next;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 if (ep->dtmf_next)
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 ep->dtmf_next->dtmf_pp = ep->dtmf_pp;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 ep->dtmf_pp = 0;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 ep->dtmf_next = 0;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 }
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 if (dtmf_list_head) {
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 dtmf_next_time.tv_usec += 20000;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 if (dtmf_next_time.tv_usec >= 1000000) {
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 dtmf_next_time.tv_usec -= 1000000;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 dtmf_next_time.tv_sec++;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 }
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 } else
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 dtmf_timer_running = 0;
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 }