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