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