dtmf_tester.c 6.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
	liblinphone_tester - liblinphone test suite
	Copyright (C) 2013  Belledonne Communications SARL

	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 <http://www.gnu.org/licenses/>.
*/

#include "liblinphone_tester.h"
Benjamin REIS's avatar
Benjamin REIS committed
20
#include "tester_utils.h"
21 22 23 24 25 26 27

void dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf) {
	stats* counters = get_stats(lc);
	char** dst = &counters->dtmf_list_received;
	*dst = *dst ?
				ms_strcat_printf(*dst, "%c", dtmf)
				: ms_strdup_printf("%c", dtmf);
28
	counters->dtmf_count++;
29 30
}

31
void send_dtmf_base(LinphoneCoreManager **pmarie, LinphoneCoreManager **ppauline, bool_t use_rfc2833, bool_t use_sipinfo, char dtmf, char* dtmf_seq, bool_t use_opus) {
32
	char* expected = NULL;
33
	int dtmf_count_prev;
34 35 36
	LinphoneCoreManager *marie = *pmarie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager *pauline = *ppauline = linphone_core_manager_new( "pauline_tcp_rc");
	LinphoneCall *marie_call = NULL;
37 38

	if (use_opus) {
39
		//if (!ms_filter_codec_supported("opus")) {
40
		if(!ms_factory_codec_supported(linphone_core_get_ms_factory(marie->lc), "opus") && !ms_factory_codec_supported(linphone_core_get_ms_factory(pauline->lc), "opus")){
41

42 43 44
			ms_warning("Opus not supported, skipping test.");
			return;
		}
45 46 47
		disable_all_audio_codecs_except_one(marie->lc, "opus", 48000);
		disable_all_audio_codecs_except_one(pauline->lc, "opus", 48000);
	}
48 49 50 51 52 53

	linphone_core_set_use_rfc2833_for_dtmf(marie->lc, use_rfc2833);
	linphone_core_set_use_info_for_dtmf(marie->lc, use_sipinfo);
	linphone_core_set_use_rfc2833_for_dtmf(pauline->lc, use_rfc2833);
	linphone_core_set_use_info_for_dtmf(pauline->lc, use_sipinfo);

54
	BC_ASSERT_TRUE(call(pauline,marie));
55 56

	marie_call = linphone_core_get_current_call(marie->lc);
57 58 59

	BC_ASSERT_PTR_NOT_NULL(marie_call);

60
	if (!marie_call) return;
61 62

	if (dtmf != '\0') {
63
		dtmf_count_prev = pauline->stat.dtmf_count;
64 65 66
		linphone_call_send_dtmf(marie_call, dtmf);

		/*wait for the DTMF to be received from pauline*/
67
		BC_ASSERT_TRUE(wait_for_until(marie->lc, pauline->lc, &pauline->stat.dtmf_count, dtmf_count_prev+1, 10000));
68 69 70 71
		expected = ms_strdup_printf("%c", dtmf);
	}

	if (dtmf_seq != NULL) {
72
		int dtmf_delay_ms = lp_config_get_int(linphone_core_get_config(linphone_call_get_core(marie_call)),"net","dtmf_delay_ms",200);
73
		dtmf_count_prev = pauline->stat.dtmf_count;
74 75 76
		linphone_call_send_dtmfs(marie_call, dtmf_seq);

		/*wait for the DTMF sequence to be received from pauline*/
77
		BC_ASSERT_TRUE(wait_for_until(marie->lc, pauline->lc, &pauline->stat.dtmf_count, (int)(dtmf_count_prev + strlen(dtmf_seq)), (int)(10000 + dtmf_delay_ms * strlen(dtmf_seq))));
78 79 80 81
		expected = (dtmf!='\0')?ms_strdup_printf("%c%s",dtmf,dtmf_seq):ms_strdup(dtmf_seq);
	}

	if (expected != NULL) {
82
		BC_ASSERT_PTR_NOT_NULL(pauline->stat.dtmf_list_received);
83
		if (pauline->stat.dtmf_list_received) {
84
			BC_ASSERT_STRING_EQUAL(pauline->stat.dtmf_list_received, expected);
85 86 87
		}
		ms_free(expected);
	} else {
88
		BC_ASSERT_PTR_NULL(pauline->stat.dtmf_list_received);
89 90 91
	}
}

92 93
void send_dtmf_cleanup(LinphoneCoreManager *marie, LinphoneCoreManager *pauline) {
	LinphoneCall *marie_call = linphone_core_get_current_call(marie->lc);
94
	if (marie_call) {
95 96
		BC_ASSERT_PTR_NULL(_linphone_call_get_dtmf_timer(marie_call));
		BC_ASSERT_FALSE(_linphone_call_has_dtmf_sequence(marie_call));
97 98 99 100 101 102

		/*just to sleep*/
		linphone_core_terminate_all_calls(pauline->lc);
		BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneCallEnd,1));
		BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneCallEnd,1));
	}
103 104 105 106
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

107
static void send_dtmf_rfc2833(void) {
108 109 110
	LinphoneCoreManager *marie, *pauline;
	send_dtmf_base(&marie, &pauline, TRUE,FALSE,'1',NULL,FALSE);
	send_dtmf_cleanup(marie, pauline);
111 112
}

113
static void send_dtmf_sip_info(void) {
114 115 116
	LinphoneCoreManager *marie, *pauline;
	send_dtmf_base(&marie, &pauline, FALSE,TRUE,'#',NULL,FALSE);
	send_dtmf_cleanup(marie, pauline);
117 118
}

119
static void send_dtmfs_sequence_rfc2833(void) {
120 121 122
	LinphoneCoreManager *marie, *pauline;
	send_dtmf_base(&marie, &pauline, TRUE,FALSE,'\0',"1230#",FALSE);
	send_dtmf_cleanup(marie, pauline);
123 124
}

125
static void send_dtmfs_sequence_sip_info(void) {
126 127 128
	LinphoneCoreManager *marie, *pauline;
	send_dtmf_base(&marie, &pauline, FALSE,TRUE,'\0',"1230#",FALSE);
	send_dtmf_cleanup(marie, pauline);
129 130
}

131
static void send_dtmfs_sequence_not_ready(void) {
132
	LinphoneCoreManager *marie;
133
	marie = linphone_core_manager_new( "marie_rc");
134
	BC_ASSERT_EQUAL(linphone_call_send_dtmfs(linphone_core_get_current_call(marie->lc), "123"), -1, int, "%d");
135 136 137
	linphone_core_manager_destroy(marie);
}

138
static void send_dtmfs_sequence_call_state_changed(void) {
139 140 141
	LinphoneCoreManager *marie, *pauline;
	LinphoneCall *marie_call = NULL;
	send_dtmf_base(&marie, &pauline, FALSE,TRUE,'\0',NULL,FALSE);
142

143 144 145 146 147
	marie_call = linphone_core_get_current_call(marie->lc);
	if (marie_call) {
		/*very long DTMF(around 4 sec to be sent)*/
		linphone_call_send_dtmfs(marie_call, "123456789123456789");
		/*just after, change call state, and expect DTMF to be canceled*/
148
		linphone_call_pause(marie_call);
149 150
		BC_ASSERT_TRUE(wait_for(marie->lc,pauline->lc,&marie->stat.number_of_LinphoneCallPausing,1));
		BC_ASSERT_TRUE(wait_for(marie->lc,pauline->lc,&marie->stat.number_of_LinphoneCallPaused,1));
151

152 153
		/*wait a few time to ensure that no DTMF are received*/
		wait_for_until(marie->lc, pauline->lc, NULL, 0, 1000);
154

155 156
		BC_ASSERT_PTR_NULL(pauline->stat.dtmf_list_received);
	}
157
	end_call(marie, pauline);
158
	send_dtmf_cleanup(marie, pauline);
159 160
}

161
static void send_dtmf_rfc2833_opus(void) {
162 163 164
	LinphoneCoreManager *marie, *pauline;
	send_dtmf_base(&marie, &pauline, TRUE,FALSE,'1',NULL,TRUE);
	send_dtmf_cleanup(marie, pauline);
165 166
}

167
test_t dtmf_tests[] = {
168 169 170 171 172 173 174
	TEST_NO_TAG("Send DTMF using RFC2833",send_dtmf_rfc2833),
	TEST_NO_TAG("Send DTMF using SIP INFO",send_dtmf_sip_info),
	TEST_NO_TAG("Send DTMF sequence using RFC2833",send_dtmfs_sequence_rfc2833),
	TEST_NO_TAG("Send DTMF sequence using SIP INFO",send_dtmfs_sequence_sip_info),
	TEST_NO_TAG("DTMF sequence not sent if invalid call",send_dtmfs_sequence_not_ready),
	TEST_NO_TAG("DTMF sequence canceled if call state changed",send_dtmfs_sequence_call_state_changed),
	TEST_NO_TAG("Send DTMF using RFC2833 using Opus",send_dtmf_rfc2833_opus)
175 176
};

177
test_suite_t dtmf_test_suite = {"DTMF", NULL, NULL, liblinphone_tester_before_each, liblinphone_tester_after_each,
178
								sizeof(dtmf_tests) / sizeof(dtmf_tests[0]), dtmf_tests};