auto_answer.c 7.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
linphone
Copyright (C) 2010  Belledonne Communications SARL
 (simon.morlat@linphone.org)

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, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 20 21
*/


22
#include "linphone/core.h"
23 24 25 26 27 28
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <signal.h>

static bool_t running=TRUE;
jehan's avatar
jehan committed
29
static bool_t print_stats=FALSE;
30
static bool_t dump_stats=FALSE;
31 32 33 34

static void stop(int signum){
	running=FALSE;
}
35
#ifndef _WIN32
jehan's avatar
jehan committed
36 37 38
static void stats(int signum){
	print_stats=TRUE;
}
39 40 41 42
static void dump_call_logs(int signum){
	dump_stats=TRUE;
}

43 44 45 46 47
#endif
/*
 * Call state notification callback
 */
static void call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *msg){
jehan's avatar
jehan committed
48
	LinphoneCallParams * call_params;
49 50
	switch(cstate){
		case LinphoneCallIncomingReceived:
51
			ms_message("Incoming call arriving !\n");
52
			/* accept the incoming call*/
53
			call_params = linphone_core_create_call_params(lc, call);
54
			linphone_call_params_enable_video(call_params,TRUE);
55 56 57
			linphone_call_params_set_audio_direction(call_params,LinphoneMediaDirectionSendOnly);
			linphone_call_params_set_video_direction(call_params,LinphoneMediaDirectionSendOnly);
			linphone_core_accept_call_with_params(lc,call,call_params);
Simon Morlat's avatar
Simon Morlat committed
58
			linphone_call_params_unref(call_params);
59 60
		break;
		default:
61
			break;
62 63 64
	}
}
extern MSWebCamDesc mire_desc;
65 66
static void helper(const char *progname) {
	printf("%s --help\n"
67
			"\t\t\t--listening-uri <uri> uri to listen on, default [sip:localhost:5060]\n"
jehan's avatar
jehan committed
68
			"\t\t\t--max-call-duration max duration of a call in seconds, default [3600]\n"
69
			"\t\t\t--media-file <wav or mkv file to play to caller>\n"
70
			"\t\t\t--verbose\n", progname);
71
	exit(0);
72 73 74 75 76 77 78 79 80 81
}

int main(int argc, char *argv[]){
	LinphoneCoreVTable vtable={0};
	LinphoneCore *lc;
	LinphoneVideoPolicy policy;
	int i;
	LinphoneAddress *addr=NULL;
	LCSipTransports tp;
	char * tmp = NULL;
82
	LpConfig * lp_config = lp_config_new(NULL);
jehan's avatar
jehan committed
83
	int max_call_duration=3600;
84
	static const char *media_file = NULL;
jehan's avatar
jehan committed
85 86

	policy.automatically_accept=TRUE;
87
	signal(SIGINT,stop);
88
#ifndef _WIN32
jehan's avatar
jehan committed
89
	signal(SIGUSR1,stats);
90
	signal(SIGUSR2,dump_call_logs);
jehan's avatar
jehan committed
91
#endif
92 93 94
	for(i = 1; i < argc; ++i) {
		if (strcmp(argv[i], "--verbose") == 0) {
			linphone_core_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
jehan's avatar
jehan committed
95 96
		} else if (strcmp(argv[i], "--max-call-duration") == 0){
			max_call_duration = atoi(argv[++i]);
97 98 99 100
		} else if (strcmp(argv[i], "--listening-uri") == 0){
			addr = linphone_address_new(argv[++i]);
			if (!addr) {
				printf("Error, bad sip uri");
101
				helper(argv[0]);
102
			}
103
/*			switch(linphone_address_get_transport(addr)) {
104 105 106 107
			case LinphoneTransportUdp:
			case LinphoneTransportTcp:
				break;
			default:
108
				ms_error("Error, bad sip uri [%s] transport, should be udp | tcp",argv[i]);
109 110
				helper();
				break;
111
			}*/
112 113 114 115 116
		} else if (strcmp(argv[i], "--media-file") == 0){
			i++;
			if (i<argc){
				media_file = argv[i];
			}else helper(argv[0]);
117
		} else {
118
			helper(argv[0]);
119 120 121
		}
	}

122 123 124 125 126 127
	if (!addr) {
		addr = linphone_address_new("sip:bot@0.0.0.0:5060");
	}

	lp_config_set_string(lp_config,"sip","bind_address",linphone_address_get_domain(addr));
	lp_config_set_string(lp_config,"rtp","bind_address",linphone_address_get_domain(addr));
128
	lp_config_set_int(lp_config,"misc","history_max_size",100000);
129 130 131

	vtable.call_state_changed=call_state_changed;

132
	lc=linphone_core_new_with_config(&vtable,lp_config,NULL);
133 134 135
	linphone_core_enable_video_capture(lc,TRUE);
	linphone_core_enable_video_display(lc,FALSE);
	linphone_core_set_video_policy(lc,&policy);
jehan's avatar
jehan committed
136
	linphone_core_enable_keep_alive(lc,FALSE);
137 138 139 140


	/*instead of using sound capture card, a file is played to the calling party*/
	linphone_core_set_use_files(lc,TRUE);
141 142
	linphone_core_enable_echo_cancellation(lc, FALSE); /*no need for local echo cancellation when playing files*/
	if (!media_file){
143
		LinphoneFactory *factory = linphone_factory_get();
144
		const char *sound_resources_dir = linphone_factory_get_sound_resources_dir(factory);
145 146 147
		char *play_file = bctbx_strdup_printf("%s/hello16000.wav", sound_resources_dir);
		linphone_core_set_play_file(lc, play_file);
		bctbx_free(play_file);
148 149 150 151 152 153 154 155 156 157 158 159
		linphone_core_set_preferred_framerate(lc,5);
	}else{
		PayloadType *pt = linphone_core_find_payload_type(lc, "opus", 48000, -1);
		/*if opus is present, give it a bitrate for good quality with music, and stereo enabled*/
		if (pt){
			linphone_core_set_payload_type_bitrate(lc, pt, 150);
			payload_type_set_send_fmtp(pt, "stereo=1");
			payload_type_set_recv_fmtp(pt, "stereo=1");
		}
		linphone_core_set_play_file(lc, media_file);
		linphone_core_set_preferred_video_size_by_name(lc, "720p");
	}
160

Simon Morlat's avatar
Simon Morlat committed
161 162 163
	{
		MSWebCamDesc *desc = ms_mire_webcam_desc_get();
		if (desc){
164
			ms_web_cam_manager_add_cam(ms_factory_get_web_cam_manager(linphone_core_get_ms_factory(lc)),ms_web_cam_new(desc));
Simon Morlat's avatar
Simon Morlat committed
165 166 167
			linphone_core_set_video_device(lc,"Mire: Mire (synthetic moving picture)");
		}
	}
168

169

170 171

	memset(&tp,0,sizeof(LCSipTransports));
172 173 174 175

	tp.udp_port = linphone_address_get_port(addr);
	tp.tcp_port = linphone_address_get_port(addr);

176
	linphone_core_set_sip_transports(lc,&tp);
177
	linphone_core_set_audio_port_range(lc,1024,65000);
178
	linphone_core_set_video_port_range(lc,1024,65000);
179 180 181 182 183
	linphone_core_set_primary_contact(lc,tmp=linphone_address_as_string(addr));
	ms_free(tmp);

	/* main loop for receiving notifications and doing background linphonecore work: */
	while(running){
184
		const bctbx_list_t * iterator;
185 186
		linphone_core_iterate(lc);
		ms_usleep(50000);
jehan's avatar
jehan committed
187 188
		if (print_stats) {
			ms_message("*********************************");
189 190
			ms_message("*Current number of calls   [%10u]  *", (unsigned int)bctbx_list_size(linphone_core_get_calls(lc)));
			ms_message("*Number of calls until now [%10u]  *", (unsigned int)bctbx_list_size(linphone_core_get_call_logs(lc)));
jehan's avatar
jehan committed
191 192 193
			ms_message("*********************************");
			print_stats=FALSE;
		}
194 195 196 197 198 199 200 201 202 203 204
		if (dump_stats) {
			ms_message("*********************************");
			for (iterator=linphone_core_get_call_logs(lc);iterator!=NULL;iterator=iterator->next) {
				LinphoneCallLog *call_log=(LinphoneCallLog *)iterator->data;
				char * tmp_str = linphone_call_log_to_str(call_log);
				ms_message("\n%s",tmp_str);
				ms_free(tmp_str);
			}
			dump_stats=FALSE;
			ms_message("*********************************");
		}
jehan's avatar
jehan committed
205 206 207 208 209 210 211 212 213
		for (iterator=linphone_core_get_calls(lc);iterator!=NULL;iterator=iterator->next) {
			LinphoneCall *call=(LinphoneCall *)iterator->data;
			if (linphone_call_get_duration(call) > max_call_duration) {
				ms_message("Terminating call [%p] after [%i] s",call,linphone_call_get_duration(call));
				linphone_core_terminate_call(lc,call);
				break;
			}
		}

214 215
	}

216
	ms_message("Shutting down...\n");
217
	linphone_core_destroy(lc);
218
	ms_message("Exited\n");
219 220 221 222 223 224
	return 0;
}