realtimetext_sender.c 4.23 KB
Newer Older
1 2 3

/*
linphone
jehan's avatar
jehan committed
4
Copyright (C) 2015  Belledonne Communications SARL
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

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
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

/**
jehan's avatar
jehan committed
22
 * @defgroup real_time_text Real Time Text Sender
23
 * @ingroup tutorials
jehan's avatar
jehan committed
24 25
 This program just send chat message in real time to dest uri. Use realtimetext_receiver to receive  message.
 usage: ./realtimetext_sender sip:localhost:5060
26

jehan's avatar
jehan committed
27 28

 @include realtimetext_sender.c
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
 */
#ifdef IN_LINPHONE
#include "linphonecore.h"
#else
#include "linphone/linphonecore.h"
#endif

#include <signal.h>

static bool_t running=TRUE;

static void stop(int signum){
	running=FALSE;
}



int main(int argc, char *argv[]){
	LinphoneCoreVTable vtable={0};
	LinphoneCore *lc;
	LinphoneCall *call=NULL;
	LinphoneChatRoom *chat_room;
	LinphoneChatMessage *chat_message=NULL;
	const char *dest=NULL;
	LCSipTransports tp;
	tp.udp_port=LC_SIP_TRANSPORT_RANDOM;
	tp.tcp_port=LC_SIP_TRANSPORT_RANDOM;
	tp.tls_port=LC_SIP_TRANSPORT_RANDOM;

	/* take the destination sip uri from the command line arguments */
	if (argc>1){
		dest=argv[1];
	}

	signal(SIGINT,stop);

#ifdef DEBUG
	linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
#endif

	/*
	 Instanciate a LinphoneCore object given the LinphoneCoreVTable
	*/
	lc=linphone_core_new(&vtable,NULL,NULL,NULL);


	linphone_core_set_sip_transports(lc,&tp); /*to avoid port colliding with receiver*/
	if (dest){
		/*
		 Place an outgoing call with rtt enabled
		*/
80
		LinphoneCallParams *cp = linphone_core_create_call_params(lc, NULL);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
		linphone_call_params_enable_realtime_text(cp,TRUE); /*enable real time text*/
		call=linphone_core_invite_with_params(lc,dest,cp);
		linphone_call_params_destroy(cp);
		if (call==NULL){
			printf("Could not place call to %s\n",dest);
			goto end;
		}else printf("Call to %s is in progress...",dest);
		linphone_call_ref(call);

	}
	/*wait for call to be established*/
	while 	(running && (linphone_call_get_state(call) == LinphoneCallOutgoingProgress
						|| linphone_call_get_state(call) == LinphoneCallOutgoingInit)) {
		linphone_core_iterate(lc);
		ms_usleep(50000);
	}
jehan's avatar
jehan committed
97
	/*check if call is established*/
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	switch (linphone_call_get_state(call)) {
	case LinphoneCallError:
	case LinphoneCallReleased:
	case LinphoneCallEnd:
		printf("Could not place call to %s\n",dest);
		goto end;
		break;
	default:
		break;
		/*continue*/
	}

	chat_room=linphone_call_get_chat_room(call); /*create a chat room associated to this call*/

	/* main loop for sending message and doing background linphonecore work: */
	while(running){
		char character;
Simon Morlat's avatar
Simon Morlat committed
115 116 117 118
		/*to disable terminal buffering*/
		if (system ("/bin/stty raw") == -1){
			ms_error("/bin/stty error");
		}
119
		character = getchar();
Simon Morlat's avatar
Simon Morlat committed
120 121 122
		if (system("/bin/stty cooked") == -1){
			ms_error("/bin/stty error");
		}
123 124 125 126 127 128 129 130 131 132 133
		if (character==0x03) {/*CTRL C*/
			running=0;
			break;
		}
		if (character != EOF) {
			/* user has typed something*/
			if (chat_message == NULL) {
				/*create a new message*/
				chat_message = linphone_chat_room_create_message(chat_room,""); /*create an empty message*/
			}
			if (character == '\r') {
jehan's avatar
jehan committed
134
				/*new line, committing message*/
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
				linphone_chat_room_send_chat_message(chat_room,chat_message);
				chat_message = NULL; /*reset message*/
			} else {
				linphone_chat_message_put_char(chat_message,character); /*send char in realtime*/
			}
		}
		linphone_core_iterate(lc);
		ms_usleep(50000);
	}
	if (call && linphone_call_get_state(call)!=LinphoneCallEnd){
		/* terminate the call */
		printf("Terminating the call...\n");
		linphone_core_terminate_call(lc,call);
		/*at this stage we don't need the call object */
		linphone_call_unref(call);
	}

end:
	printf("Shutting down...\n");
	linphone_core_destroy(lc);
	printf("Exited\n");
	return 0;
}