1 /*
2  * Copyright (C) 2010 Stefan Sayer
3  * Copyright (C) 2011 Raphael Coeffic
4  *
5  * This file is part of SEMS, a free SIP media server.
6  *
7  * SEMS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version. This program is released under
11  * the GPL with the additional exemption that compiling, linking,
12  * and/or using OpenSSL is allowed.
13  *
14  * For a license to use the SEMS software under conditions
15  * other than those described here, or to purchase support for this
16  * software, please contact iptel.org by e-mail at the following addresses:
17  *    info@iptel.org
18  *
19  * SEMS is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28 
29 #include "AmDtmfSender.h"
30 #include "AmRtpStream.h"
31 
32 #include "rtp/telephone_event.h"
33 
AmDtmfSender()34 AmDtmfSender::AmDtmfSender()
35   : sending_state(DTMF_SEND_NONE)
36 {
37 }
38 
39 /** Add a DTMF event to the send queue */
queueEvent(int event,unsigned int duration_ms,unsigned int sample_rate)40 void AmDtmfSender::queueEvent(int event, unsigned int duration_ms, unsigned int sample_rate)
41 {
42   send_queue_mut.lock();
43   send_queue.push(std::make_pair(event, duration_ms * sample_rate / 1000));
44   send_queue_mut.unlock();
45   DBG("enqueued DTMF event %i duration %u\n", event, duration_ms);
46 }
47 
48 /** Processes the send queue according to the timestamp */
sendPacket(unsigned int ts,unsigned int remote_pt,AmRtpStream * stream)49 void AmDtmfSender::sendPacket(unsigned int ts, unsigned int remote_pt, AmRtpStream* stream)
50 {
51   while (true) {
52     switch(sending_state) {
53     case DTMF_SEND_NONE: {
54       send_queue_mut.lock();
55       if (send_queue.empty()) {
56 	send_queue_mut.unlock();
57 	return;
58       }
59       current_send_dtmf = send_queue.front();
60       current_send_dtmf_ts = ts;
61       send_queue.pop();
62       send_queue_mut.unlock();
63       sending_state = DTMF_SEND_SENDING;
64       current_send_dtmf_ts = ts;
65       DBG("starting to send DTMF\n");
66     } break;
67 
68     case DTMF_SEND_SENDING: {
69       if (ts_less()(ts, current_send_dtmf_ts + current_send_dtmf.second)) {
70 	// send packet
71 	//if (!remote_telephone_event_pt.get())
72 	//return;
73 
74 	dtmf_payload_t dtmf;
75 	dtmf.event = current_send_dtmf.first;
76 	dtmf.e = dtmf.r = 0;
77 	dtmf.duration = htons(ts - current_send_dtmf_ts);
78 	dtmf.volume = 20;
79 
80 	DBG("sending DTMF: event=%i; e=%i; r=%i; volume=%i; duration=%i; ts=%u\n",
81 	    dtmf.event,dtmf.e,dtmf.r,dtmf.volume,ntohs(dtmf.duration),current_send_dtmf_ts);
82 
83 	stream->compile_and_send(remote_pt, dtmf.duration == 0,
84 				 current_send_dtmf_ts,
85 				 (unsigned char*)&dtmf, sizeof(dtmf_payload_t));
86 	return;
87 
88       } else {
89 	DBG("ending DTMF\n");
90 	sending_state = DTMF_SEND_ENDING;
91 	send_dtmf_end_repeat = 0;
92       }
93     } break;
94 
95     case DTMF_SEND_ENDING:  {
96       if (send_dtmf_end_repeat >= 3) {
97 	DBG("DTMF send complete\n");
98 	sending_state = DTMF_SEND_NONE;
99       } else {
100 	send_dtmf_end_repeat++;
101 	// send packet with end bit set, duration = event duration
102 	//if (!remote_telephone_event_pt.get())
103 	//return;
104 
105 	dtmf_payload_t dtmf;
106 	dtmf.event = current_send_dtmf.first;
107 	dtmf.e = 1;
108 	dtmf.r = 0;
109 	dtmf.duration = htons(current_send_dtmf.second);
110 	dtmf.volume = 20;
111 
112 	DBG("sending DTMF: event=%i; e=%i; r=%i; volume=%i; duration=%i; ts=%u\n",
113 	    dtmf.event,dtmf.e,dtmf.r,dtmf.volume,ntohs(dtmf.duration),current_send_dtmf_ts);
114 
115 	stream->compile_and_send(remote_pt, false, current_send_dtmf_ts,
116 				 (unsigned char*)&dtmf, sizeof(dtmf_payload_t));
117 	return;
118       }
119     } break;
120     };
121   }
122 }
123 
124