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
|
/*
Copyright (C) 2005-2009 Michel de Boer <michel@twinklephone.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <cstring>
#include <sys/time.h>
#include "dtmf_player.h"
#include "audio_rx.h"
#include "line.h"
#include "rtp_telephone_event.h"
#include "log.h"
/////////////////////////////////////////
// class t_dtmf_player
/////////////////////////////////////////
t_dtmf_player::t_dtmf_player(t_audio_rx *audio_rx, t_audio_encoder *audio_encoder,
t_user *user_config, t_dtmf_ev dtmf_tone, uint32 dtmf_timestamp,
uint16 nsamples) :
_audio_rx(audio_rx),
_user_config(user_config),
_audio_encoder(audio_encoder),
_dtmf_pause(false),
_dtmf_stop(false),
_dtmf_current(dtmf_tone),
_dtmf_timestamp(dtmf_timestamp),
_dtmf_duration(0),
_nsamples(nsamples)
{}
uint32 t_dtmf_player::get_timestamp(void) {
return _dtmf_timestamp;
}
bool t_dtmf_player::finished(void) {
return _dtmf_stop;
}
/////////////////////////////////////////
// class t_rtp_event_dtmf_player
/////////////////////////////////////////
t_rtp_event_dtmf_player::t_rtp_event_dtmf_player(t_audio_rx *audio_rx,
t_audio_encoder *audio_encoder, t_user *user_config,
t_dtmf_ev dtmf_tone, uint32 dtmf_timestamp,
uint16 nsamples) :
t_dtmf_player(audio_rx, audio_encoder, user_config, dtmf_tone, dtmf_timestamp,
nsamples)
{
}
uint16 t_rtp_event_dtmf_player::get_payload(uint8 *payload,
uint16 payload_size, uint32 timestamp, uint32 &rtp_timestamp)
{
t_rtp_telephone_event *dtmf_payload = (t_rtp_telephone_event *)payload;
assert(sizeof(t_rtp_telephone_event) <= payload_size);
// RFC 2833 3.5, 3.6
dtmf_payload->set_event(_dtmf_current);
dtmf_payload->set_reserved(false);
dtmf_payload->set_volume(_user_config->get_dtmf_volume());
if (_dtmf_pause) {
// Trailing pause phase of a DTMF tone
// Repeat the last packet
dtmf_payload->set_end(true);
int pause_duration = timestamp - _dtmf_timestamp - _dtmf_duration +
_nsamples;
if (pause_duration / _nsamples * _audio_encoder->get_ptime() >=
_user_config->get_dtmf_pause())
{
// This is the last packet to be sent for the
// current DTMF tone.
_dtmf_stop = true;
log_file->write_header("t_rtp_event_dtmf_player::get_payload",
LOG_NORMAL);
log_file->write_raw("Audio rx line ");
log_file->write_raw(_audio_rx->get_line()->get_line_number()+1);
log_file->write_raw(": finish DTMF event - ");
log_file->write_raw(_dtmf_current);
log_file->write_endl();
log_file->write_footer();
}
} else {
// Play phase of a DTMF tone
// The duration counts from the start of the tone.
_dtmf_duration = timestamp - _dtmf_timestamp + _nsamples;
// Check if the tone must end
if (_dtmf_duration / _nsamples * _audio_encoder->get_ptime() >=
_user_config->get_dtmf_duration())
{
dtmf_payload->set_end(true);
_dtmf_pause = true;
} else {
dtmf_payload->set_end(false);
}
}
dtmf_payload->set_duration(_dtmf_duration);
rtp_timestamp = _dtmf_timestamp;
return sizeof(t_rtp_telephone_event);
}
/////////////////////////////////////////
// class t_inband_dtmf_player
/////////////////////////////////////////
t_inband_dtmf_player::t_inband_dtmf_player(t_audio_rx *audio_rx,
t_audio_encoder *audio_encoder, t_user *user_config,
t_dtmf_ev dtmf_tone, uint32 dtmf_timestamp,
uint16 nsamples) :
t_dtmf_player(audio_rx, audio_encoder, user_config, dtmf_tone, dtmf_timestamp,
nsamples),
_freq_gen(dtmf_tone, -(user_config->get_dtmf_volume()))
{
}
uint16 t_inband_dtmf_player::get_payload(uint8 *payload,
uint16 payload_size, uint32 timestamp, uint32 &rtp_timestamp)
{
int16 sample_buf[_nsamples];
if (_dtmf_pause) {
int pause_duration = timestamp - _dtmf_timestamp - _dtmf_duration +
_nsamples;
memset(sample_buf, 0, _nsamples * 2);
if (pause_duration / _nsamples * _audio_encoder->get_ptime() >=
_user_config->get_dtmf_pause())
{
// This is the last packet to be sent for the
// current DTMF tone.
_dtmf_stop = true;
log_file->write_header("t_inband_dtmf_player::get_payload", LOG_NORMAL);
log_file->write_raw("Audio rx line ");
log_file->write_raw(_audio_rx->get_line()->get_line_number()+1);
log_file->write_raw(": finish DTMF event - ");
log_file->write_raw(_dtmf_current);
log_file->write_endl();
log_file->write_footer();
}
} else {
// Timestamp and interval for _freq_gen must be in usec
uint32 ts_start = (timestamp - _dtmf_timestamp) * 1000000 /
_audio_encoder->get_sample_rate();
_freq_gen.get_samples(sample_buf, _nsamples, ts_start,
1000000.0 / _audio_encoder->get_sample_rate());
// The duration counts from the start of the tone.
_dtmf_duration = timestamp - _dtmf_timestamp + _nsamples;
// Check if the tone must end
if (_dtmf_duration / _nsamples * _audio_encoder->get_ptime() >=
_user_config->get_dtmf_duration())
{
_dtmf_pause = true;
}
}
// Encode audio samples
bool silence;
rtp_timestamp = timestamp;
return _audio_encoder->encode(sample_buf, _nsamples, payload, payload_size, silence);
}
|