telephonyevents.c 12.9 KB
Newer Older
aymeric's avatar
aymeric committed
1
/*
2
 * Copyright (c) 2010-2019 Belledonne Communications SARL.
3
 *
4
 * This file is part of oRTP.
5
 *
6 7 8 9
 * 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 3 of the License, or
 * (at your option) any later version.
10
 *
11 12 13 14 15 16 17
 * 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/>.
18
 */
aymeric's avatar
aymeric committed
19 20 21 22 23

#include <ortp/telephonyevents.h>
#include "utils.h"
#include "rtpsession_priv.h"
#include <ortp/ortp.h>
24
#include <bctoolbox/port.h>
aymeric's avatar
aymeric committed
25 26 27 28 29 30 31 32 33

PayloadType	payload_type_telephone_event={
	PAYLOAD_AUDIO_PACKETIZED, /*type */
	8000,	/*clock rate */
	0,		/* bytes per sample N/A */
	NULL,	/* zero pattern N/A*/
	0,		/*pattern_length N/A */
	0,		/*	normal_bitrate */
	"telephone-event",	/* MIME subtype */
34
	1,		/* Audio Channels */
aymeric's avatar
aymeric committed
35 36 37 38
	0		/*flags */
};

/**
39
 * Tells whether telephony events payload type is supported within the
aymeric's avatar
aymeric committed
40
 * context of the rtp session.
41
 * @param session a rtp session
aymeric's avatar
aymeric committed
42 43 44 45 46 47
 *
 * @return the payload type number used for telephony events if found, -1 if not found.
**/
int rtp_session_telephone_events_supported(RtpSession *session)
{
	/* search for a telephony event payload in the current profile */
48 49 50 51 52 53
	return rtp_profile_get_payload_number_from_mime(session->snd.profile,"telephone-event");
}

bool_t rtp_profile_is_telephone_event(const RtpProfile *prof, int pt_num){
	PayloadType *pt=rtp_profile_get_payload(prof, pt_num);
	return pt && strcasecmp(pt->mime_type,"telephone-event")==0;
aymeric's avatar
aymeric committed
54 55 56 57
}


/**
58
 * Tells whether telephone event payload type is supported for send within the
aymeric's avatar
aymeric committed
59
 * context of the rtp session.
60
 * @param session a rtp session
aymeric's avatar
aymeric committed
61 62 63 64 65 66
 *
 * @return the payload type number used for telephony events if found, -1 if not found.
**/
int rtp_session_send_telephone_events_supported(RtpSession *session)
{
	/* search for a telephony event payload in the current profile */
67
	return rtp_profile_get_payload_number_from_mime(session->snd.profile,"telephone-event");
aymeric's avatar
aymeric committed
68 69 70
}

/**
71
 * Tells whether telephone event payload type is supported for receiving within the
aymeric's avatar
aymeric committed
72
 * context of the rtp session.
73
 * @param session a rtp session
aymeric's avatar
aymeric committed
74 75 76 77 78
 *
 * @return the payload type number used for telephony events if found, -1 if not found.
**/int rtp_session_recv_telephone_events_supported(RtpSession *session)
{
	/* search for a telephony event payload in the current profile */
79
	return rtp_profile_get_payload_number_from_mime(session->rcv.profile,"telephone-event");
aymeric's avatar
aymeric committed
80 81 82 83 84 85 86 87 88 89 90 91
}


/**
 *	Allocates a new rtp packet to be used to add named telephony events. The application can use
 *	then rtp_session_add_telephone_event() to add named events to the packet.
 *	Finally the packet has to be sent with rtp_session_sendm_with_ts().
 *
 * @param session a rtp session.
 * @param start boolean to indicate if the marker bit should be set.
 *
 * @return a message block containing the rtp packet if successfull, NULL if the rtp session
92 93
 * cannot support telephony event (because the rtp profile it is bound to does not include
 * a telephony event payload type).
aymeric's avatar
aymeric committed
94 95 96 97 98
**/
mblk_t	*rtp_session_create_telephone_event_packet(RtpSession *session, int start)
{
	mblk_t *mp;
	rtp_header_t *rtp;
99
	PayloadType *cur_pt=rtp_profile_get_payload(session->snd.profile, rtp_session_get_send_payload_type(session));
100
	int tev_pt = session->tev_send_pt;
101

102 103 104 105 106 107
	if (tev_pt != -1){
		PayloadType *cur_tev_pt=rtp_profile_get_payload(session->snd.profile, tev_pt);
		if (!cur_tev_pt){
			ortp_error("Undefined telephone-event payload type %i choosen for sending telephone event", tev_pt);
			tev_pt = -1;
		}else if (cur_pt && cur_tev_pt->clock_rate != cur_pt->clock_rate){
108
			ortp_warning("Telephone-event payload type %i has clockrate %i while main audio codec has clockrate %i: this is not permitted.",
109 110 111
				tev_pt, cur_tev_pt->clock_rate, cur_pt->clock_rate);
		}
	}
112

113 114 115
	if (tev_pt == -1){
		tev_pt = rtp_profile_find_payload_number(session->snd.profile, "telephone-event", cur_pt ? cur_pt->clock_rate : 8000, 1);
	}
116
	return_val_if_fail(tev_pt!=-1,NULL);
117

aymeric's avatar
aymeric committed
118 119 120 121 122 123 124 125 126 127 128
	mp=allocb(RTP_FIXED_HEADER_SIZE+TELEPHONY_EVENTS_ALLOCATED_SIZE,BPRI_MED);
	if (mp==NULL) return NULL;
	rtp=(rtp_header_t*)mp->b_rptr;
	rtp->version = 2;
	rtp->markbit=start;
	rtp->padbit = 0;
	rtp->extbit = 0;
	rtp->cc = 0;
	rtp->ssrc = session->snd.ssrc;
	/* timestamp set later, when packet is sended */
	/*seq number set later, when packet is sended */
129

aymeric's avatar
aymeric committed
130
	/*set the payload type */
131
	rtp->paytype=tev_pt;
132

aymeric's avatar
aymeric committed
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	/*copy the payload */
	mp->b_wptr+=RTP_FIXED_HEADER_SIZE;
	return mp;
}


/**
 *@param session a rtp session.
 *@param packet a rtp packet as a mblk_t
 *@param event the event type as described in rfc2833, ie one of the TEV_* macros.
 *@param end a boolean to indicate if the end bit should be set. (end of tone)
 *@param volume the volume of the telephony tone, as described in rfc2833
 *@param duration the duration of the telephony tone, in timestamp unit.
 *
 * Adds a named telephony event to a rtp packet previously allocated using
 * rtp_session_create_telephone_event_packet().
 *
 *@return 0 on success.
**/
int rtp_session_add_telephone_event(RtpSession *session,
			mblk_t *packet, uint8_t event, int end, uint8_t volume, uint16_t duration)
{
	mblk_t *mp=packet;
	telephone_event_t *event_hdr;


	/* find the place where to add the new telephony event to the packet */
	while(mp->b_cont!=NULL) mp=mp->b_cont;
	/* see if we need to allocate a new mblk_t */
	if ( ( mp->b_wptr) >= (mp->b_datap->db_lim)){
		mblk_t *newm=allocb(TELEPHONY_EVENTS_ALLOCATED_SIZE,BPRI_MED);
		mp->b_cont=newm;
		mp=mp->b_cont;
	}
	if (mp==NULL) return -1;
	event_hdr=(telephone_event_t*)mp->b_wptr;
	event_hdr->event=event;
	event_hdr->R=0;
	event_hdr->E=end;
	event_hdr->volume=volume;
	event_hdr->duration=htons(duration);
	mp->b_wptr+=sizeof(telephone_event_t);
	return 0;
}
/**
 *	This functions creates telephony events packets for dtmf and sends them.
 *	It uses rtp_session_create_telephone_event_packet() and
 *	rtp_session_add_telephone_event() to create them and finally
 *	rtp_session_sendm_with_ts() to send them.
 *
 * @param session a rtp session
 * @param dtmf a character meaning the dtmf (ex: '1', '#' , '9' ...)
 * @param userts the timestamp
 * @return 0 if successfull, -1 if the session cannot support telephony events or if the dtmf given as argument is not valid.
**/
int rtp_session_send_dtmf(RtpSession *session, char dtmf, uint32_t userts)
{
  return rtp_session_send_dtmf2(session, dtmf, userts, 480);
}

/**
 * A variation of rtp_session_send_dtmf() with duration specified.
 *
 * @param session a rtp session
 * @param dtmf a character meaning the dtmf (ex: '1', '#' , '9' ...)
 * @param userts the timestamp
 * @param duration duration of the dtmf in timestamp units
 * @return 0 if successfull, -1 if the session cannot support telephony events or if the dtmf given as argument is not valid.
**/
int rtp_session_send_dtmf2(RtpSession *session, char dtmf, uint32_t userts, int duration)
{
	mblk_t *m1,*m2,*m3;
	int tev_type;
	int durationtier = duration/3;

	/* create the first telephony event packet */
	switch (dtmf){
		case '1':
			tev_type=TEV_DTMF_1;
		break;
		case '2':
			tev_type=TEV_DTMF_2;
		break;
		case '3':
			tev_type=TEV_DTMF_3;
		break;
		case '4':
			tev_type=TEV_DTMF_4;
		break;
		case '5':
			tev_type=TEV_DTMF_5;
		break;
		case '6':
			tev_type=TEV_DTMF_6;
		break;
		case '7':
			tev_type=TEV_DTMF_7;
		break;
		case '8':
			tev_type=TEV_DTMF_8;
		break;
		case '9':
			tev_type=TEV_DTMF_9;
		break;
		case '*':
			tev_type=TEV_DTMF_STAR;
		break;
		case '0':
			tev_type=TEV_DTMF_0;
		break;
		case '#':
			tev_type=TEV_DTMF_POUND;
		break;

		case 'A':
		case 'a':
		  tev_type=TEV_DTMF_A;
		  break;


		case 'B':
		case 'b':
		  tev_type=TEV_DTMF_B;
		  break;

		case 'C':
		case 'c':
		  tev_type=TEV_DTMF_C;
		  break;

		case 'D':
		case 'd':
		  tev_type=TEV_DTMF_D;
		  break;

		case '!':
		  tev_type=TEV_FLASH;
		  break;


		default:
		ortp_warning("Bad dtmf: %c.",dtmf);
		return -1;
	}

	m1=rtp_session_create_telephone_event_packet(session,1);
	if (m1==NULL) return -1;
	rtp_session_add_telephone_event(session,m1,tev_type,0,10,durationtier);
	/* create a second packet */
	m2=rtp_session_create_telephone_event_packet(session,0);
	if (m2==NULL) return -1;
	rtp_session_add_telephone_event(session,m2,tev_type,0,10, durationtier+durationtier);
285

aymeric's avatar
aymeric committed
286 287 288 289
	/* create a third and final packet */
	m3=rtp_session_create_telephone_event_packet(session,0);
	if (m3==NULL) return -1;
	rtp_session_add_telephone_event(session,m3,tev_type,1,10,duration);
290

aymeric's avatar
aymeric committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	/* and now sends them */
	rtp_session_sendm_with_ts(session,m1,userts);
	rtp_session_sendm_with_ts(session,m2,userts);
	/* the last packet is sent three times in order to improve reliability*/
	m1=copymsg(m3);
	m2=copymsg(m3);
	/*			NOTE:			*/
	/* we need to copymsg() instead of dupmsg() because the buffers are modified when
	the packet is sended because of the host-to-network conversion of timestamp,ssrc, csrc, and
	seq number.
	*/
	rtp_session_sendm_with_ts(session,m3,userts);
	session->rtp.snd_seq--;
	rtp_session_sendm_with_ts(session,m1,userts);
	session->rtp.snd_seq--;
	rtp_session_sendm_with_ts(session,m2,userts);
	return 0;
}


/**
312
 *	Reads telephony events from a rtp packet. \a *tab points to the beginning of the event buffer.
aymeric's avatar
aymeric committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
 *
 * @param session a rtp session from which telephony events are received.
 * @param packet a rtp packet as a mblk_t.
 * @param tab the address of a pointer.
 * @return the number of events in the packet if successfull, 0 if the packet did not contain telephony events.
**/
int rtp_session_read_telephone_event(RtpSession *session,
		mblk_t *packet,telephone_event_t **tab)
{
	int datasize;
	int num;
	int i;
	telephone_event_t *tev;
	rtp_header_t *hdr=(rtp_header_t*)packet->b_rptr;
	unsigned char *payload;
328
	if (!rtp_profile_is_telephone_event(session->rcv.profile, hdr->paytype)) return 0;  /* this is not tel ev.*/
aymeric's avatar
aymeric committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342
	datasize=rtp_get_payload(packet,&payload);
	tev=*tab=(telephone_event_t*)payload;
	/* convert from network to host order what should be */
	num=datasize/sizeof(telephone_event_t);
	for (i=0;i<num;i++)
	{
		tev[i].duration=ntohs(tev[i].duration);
	}
	return num;
}

static void notify_tev(RtpSession *session, telephone_event_t *event){
	OrtpEvent *ev;
	OrtpEventData *evd;
343
	rtp_signal_table_emit2(&session->on_telephone_event,ORTP_INT_TO_POINTER(event[0].event));
aymeric's avatar
aymeric committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
	if (session->eventqs!=NULL){
		ev=ortp_event_new(ORTP_EVENT_TELEPHONE_EVENT);
		evd=ortp_event_get_data(ev);
		evd->packet=dupmsg(session->current_tev);
		evd->info.telephone_event=event[0].event;
		rtp_session_dispatch_event(session,ev);
	}
}

static void notify_events_ended(RtpSession *session, telephone_event_t *events, int num){
	int i;
	for (i=0;i<num;i++){
		if (events[i].E==1){
			notify_tev(session, &events[i]);
		}
	}
}

/* for high level telephony event callback */
void rtp_session_check_telephone_events(RtpSession *session, mblk_t *m0)
{
	telephone_event_t *events,*evbuf;
	int num,num2;
	int i;
	rtp_header_t *hdr;
	mblk_t *cur_tev;
	unsigned char *payload;
	int datasize;
372

aymeric's avatar
aymeric committed
373
	hdr=(rtp_header_t*)m0->b_rptr;
374

aymeric's avatar
aymeric committed
375 376 377 378
	datasize=rtp_get_payload(m0,&payload);

	num=datasize/sizeof(telephone_event_t);
	events=(telephone_event_t*)payload;
379 380


aymeric's avatar
aymeric committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	if (hdr->markbit==1)
	{
		/* this is a start of new events. Store the event buffer for later use*/
		if (session->current_tev!=NULL) {
			freemsg(session->current_tev);
			session->current_tev=NULL;
		}
		session->current_tev=copymsg(m0);
		/* handle the case where the events are short enough to end within the packet that has the marker bit*/
		notify_events_ended(session,events,num);
	}
	/* whatever there is a markbit set or not, we parse the packet and compare it to previously received one */
	cur_tev=session->current_tev;
	if (cur_tev!=NULL)
	{
		/* first compare timestamp, they must be identical */
		if (((rtp_header_t*)cur_tev->b_rptr)->timestamp==
			((rtp_header_t*)m0->b_rptr)->timestamp)
		{
			datasize=rtp_get_payload(cur_tev,&payload);
			num2=datasize/sizeof(telephone_event_t);
			evbuf=(telephone_event_t*)payload;
			for (i=0;i<MIN(num,num2);i++)
			{
				if (events[i].E==1)
				{
					/* update events that have ended */
					if (evbuf[i].E==0){
						evbuf[i].E=1;
						/* this is a end of event, report it */
						notify_tev(session,&events[i]);
					}
				}
			}
		}
		else
		{
			/* timestamp are not identical: this is not the same events*/
			if (session->current_tev!=NULL) {
				freemsg(session->current_tev);
				session->current_tev=NULL;
			}
			session->current_tev=copymsg(m0);
			notify_events_ended(session,events,num);
		}
	}
	else
	{
		/* there is no pending events, but we did not received marked bit packet
430
		either the sending implementation is not compliant, either it has been lost,
aymeric's avatar
aymeric committed
431 432 433 434 435 436
		we must deal with it anyway.*/
		session->current_tev=copymsg(m0);
		/* inform the application if there are tone ends */
		notify_events_ended(session,events,num);
	}
}