dtmf_tester.c 5.67 KB
Newer Older
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
/*
	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"
#include "private.h"

LinphoneCoreManager* marie;
LinphoneCoreManager* pauline;
LinphoneCall *marie_call;

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);
32
	counters->dtmf_count++;
33 34
}

35
void send_dtmf_base(bool_t use_rfc2833, bool_t use_sipinfo, char dtmf, char* dtmf_seq, bool_t use_opus) {
36
	char* expected = NULL;
37
	int dtmf_count_prev;
38
	marie = linphone_core_manager_new( "marie_rc");
39 40 41
	pauline = linphone_core_manager_new( "pauline_tcp_rc");

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

	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);

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

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

	BC_ASSERT_PTR_NOT_NULL(marie_call);

Simon Morlat's avatar
Simon Morlat committed
61
	if (!marie_call) return;
62 63

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

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

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

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

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

void send_dtmf_cleanup() {
94 95
	BC_ASSERT_PTR_NULL(marie_call->dtmfs_timer);
	BC_ASSERT_PTR_NULL(marie_call->dtmf_sequence);
96 97 98

	/*just to sleep*/
	linphone_core_terminate_all_calls(pauline->lc);
99 100
	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));
101 102 103 104 105 106

	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

static void send_dtmf_rfc2833() {
107
	send_dtmf_base(TRUE,FALSE,'1',NULL,FALSE);
108 109 110 111
	send_dtmf_cleanup();
}

static void send_dtmf_sip_info() {
112
	send_dtmf_base(FALSE,TRUE,'#',NULL,FALSE);
113 114 115 116
	send_dtmf_cleanup();
}

static void send_dtmfs_sequence_rfc2833() {
117
	send_dtmf_base(TRUE,FALSE,'\0',"1230#",FALSE);
118 119 120 121
	send_dtmf_cleanup();
}

static void send_dtmfs_sequence_sip_info() {
122
	send_dtmf_base(FALSE,TRUE,'\0',"1230#",FALSE);
123 124 125 126 127
	send_dtmf_cleanup();
}

static void send_dtmfs_sequence_not_ready() {
	marie = linphone_core_manager_new( "marie_rc");
128
	BC_ASSERT_EQUAL(linphone_call_send_dtmfs(linphone_core_get_current_call(marie->lc), "123"), -1, int, "%d");
129 130 131 132
	linphone_core_manager_destroy(marie);
}

static void send_dtmfs_sequence_call_state_changed() {
133
	send_dtmf_base(FALSE,TRUE,'\0',NULL,FALSE);
134 135 136 137 138

	/*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*/
	linphone_core_pause_call(marie_call->core,marie_call);
139 140
	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));
141 142 143 144

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

145
	BC_ASSERT_PTR_NULL(pauline->stat.dtmf_list_received);
146 147 148 149

	send_dtmf_cleanup();
}

150 151 152 153 154
static void send_dtmf_rfc2833_opus() {
	send_dtmf_base(TRUE,FALSE,'1',NULL,TRUE);
	send_dtmf_cleanup();
}

155 156 157 158 159 160 161
test_t dtmf_tests[] = {
	{ "Send DTMF using RFC2833",send_dtmf_rfc2833},
	{ "Send DTMF using SIP INFO",send_dtmf_sip_info},
	{ "Send DTMF sequence using RFC2833",send_dtmfs_sequence_rfc2833},
	{ "Send DTMF sequence using SIP INFO",send_dtmfs_sequence_sip_info},
	{ "DTMF sequence not sent if invalid call",send_dtmfs_sequence_not_ready},
	{ "DTMF sequence canceled if call state changed",send_dtmfs_sequence_call_state_changed},
162
	{ "Send DTMF using RFC2833 using Opus",send_dtmf_rfc2833_opus},
163 164 165 166 167 168 169 170 171
};

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