msrtp.c 13.7 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (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
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

Simon Morlat's avatar
Simon Morlat committed
20

aymeric's avatar
aymeric committed
21 22 23 24
#include "mediastreamer2/msrtp.h"
#include "mediastreamer2/msticker.h"

#include "ortp/telephonyevents.h"
aymeric's avatar
aymeric committed
25 26 27
#if defined(__cplusplus)
#define B64_NO_NAMESPACE
#endif
aymeric's avatar
aymeric committed
28 29
#include "ortp/b64.h"

30
static const int default_dtmf_duration_ms=100; /*in milliseconds*/
aymeric's avatar
aymeric committed
31

aymeric's avatar
aymeric committed
32 33 34
struct SenderData {
	RtpSession *session;
	uint32_t tsoff;
35 36
	uint32_t last_ts;
	int64_t last_sent_time;
aymeric's avatar
aymeric committed
37 38
	uint32_t skip_until;
	int rate;
39
	int nchannels;
40
	int dtmf_duration;
41 42
	int dtmf_ts_step;
	uint32_t dtmf_ts_cur;
aymeric's avatar
aymeric committed
43 44
	char relay_session_id[64];
	int relay_session_id_size;
aymeric's avatar
aymeric committed
45
	uint64_t last_rsi_time;
46 47
	char dtmf;
	bool_t dtmf_start;
aymeric's avatar
aymeric committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	bool_t skip;
	bool_t mute_mic;
};

typedef struct SenderData SenderData;

static void sender_init(MSFilter * f)
{
	SenderData *d = (SenderData *)ms_new(SenderData, 1);

	d->session = NULL;
	d->tsoff = 0;
	d->skip_until = 0;
	d->skip = FALSE;
	d->rate = 8000;
63
	d->nchannels = 1;
aymeric's avatar
aymeric committed
64
	d->dtmf = 0;
65
	d->dtmf_start = FALSE;
66
	d->dtmf_duration = 800;
67
	d->dtmf_ts_step=160;
aymeric's avatar
aymeric committed
68 69 70
	d->mute_mic=FALSE;
	d->relay_session_id_size=0;
	d->last_rsi_time=0;
71 72
	d->last_sent_time=-1;
	d->last_ts=0;
aymeric's avatar
aymeric committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	f->data = d;
}

static void sender_uninit(MSFilter * f)
{
	SenderData *d = (SenderData *) f->data;

	ms_free(d);
}

static int sender_send_dtmf(MSFilter * f, void *arg)
{
	const char *dtmf = (const char *) arg;
	SenderData *d = (SenderData *) f->data;

aymeric's avatar
aymeric committed
88
	ms_filter_lock(f);
89 90 91
	if (d->skip==TRUE)
	{
		ms_filter_unlock(f);
92
		ms_warning("MSRtpSend: already sending a dtmf.");
93 94
		return -1;
	}
aymeric's avatar
aymeric committed
95
	d->dtmf = dtmf[0];
aymeric's avatar
aymeric committed
96
	ms_filter_unlock(f);
aymeric's avatar
aymeric committed
97 98 99
	return 0;
}

100 101 102 103 104 105 106
static int sender_set_dtmf_duration(MSFilter * f, void *arg)
{
	SenderData *d = (SenderData *) f->data;
	d->dtmf_duration = *((int*)arg);
	return 0;
}

aymeric's avatar
aymeric committed
107 108 109 110 111 112 113 114
static int sender_set_session(MSFilter * f, void *arg)
{
	SenderData *d = (SenderData *) f->data;
	RtpSession *s = (RtpSession *) arg;
	PayloadType *pt =
		rtp_profile_get_payload(rtp_session_get_profile(s),
								rtp_session_get_send_payload_type(s));
	if (pt != NULL) {
115
		d->rate = pt->clock_rate;
116 117
		d->dtmf_duration=(default_dtmf_duration_ms*d->rate)/1000;
		d->dtmf_ts_step=(20*d->rate)/1000;
aymeric's avatar
aymeric committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	} else {
		ms_warning("Sending undefined payload type ?");
	}
	d->session = s;
	return 0;
}

static int sender_mute_mic(MSFilter * f, void *arg)
{
	SenderData *d = (SenderData *) f->data;
	ms_filter_lock(f);
	d->mute_mic=TRUE;
	ms_filter_unlock(f);
	return 0;
}

static int sender_unmute_mic(MSFilter * f, void *arg)
{
	SenderData *d = (SenderData *) f->data;
	ms_filter_lock(f);
	d->mute_mic=FALSE;
	ms_filter_unlock(f);
	return 0;
}

static int sender_set_relay_session_id(MSFilter *f, void*arg){
	SenderData *d = (SenderData *) f->data;
	const char *tmp=(const char *)arg;
aymeric's avatar
aymeric committed
146
	d->relay_session_id_size=b64_decode(tmp, strlen(tmp), (void*)d->relay_session_id, (unsigned int)sizeof(d->relay_session_id));
aymeric's avatar
aymeric committed
147 148 149
	return 0;
}

150 151
static int sender_get_sr(MSFilter *f, void *arg){
	SenderData *d = (SenderData *) f->data;
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	PayloadType *pt;
	if (d->session==NULL) {
		ms_warning("Could not obtain sample rate, session is not set.");
		return -1;
	}
	pt=rtp_profile_get_payload(rtp_session_get_profile(d->session),
									rtp_session_get_recv_payload_type(d->session));
	if (pt != NULL) {
		if (strcasecmp(pt->mime_type,"G722")==0)
			*(int*)arg=16000;
		else
			*(int*)arg=pt->clock_rate;
	}else{
		ms_warning("MSRtpSend: Could not obtain sample rate, payload type is unknown.");
		return -1;
	}
168 169 170
	return 0;
}

171 172 173 174 175 176 177 178 179 180 181 182
static int sender_get_ch(MSFilter *f, void *arg) {
	SenderData *d = (SenderData *)f->data;
	*(int *)arg = d->nchannels;
	return 0;
}

static int sender_set_ch(MSFilter *f, void *arg) {
	SenderData *d = (SenderData *)f->data;
	d->nchannels = *(int *)arg;
	return 0;
}

aymeric's avatar
aymeric committed
183
/* the goal of that function is to return a absolute timestamp closest to real time, with respect of given packet_ts, which is a relative to an undefined origin*/
184
static uint32_t get_cur_timestamp(MSFilter * f, mblk_t *im)
aymeric's avatar
aymeric committed
185 186
{
	SenderData *d = (SenderData *) f->data;
smorlat's avatar
smorlat committed
187
	uint32_t curts = (uint32_t)( (f->ticker->time*(uint64_t)d->rate)/(uint64_t)1000) ;
188
	int diffts;
aymeric's avatar
aymeric committed
189
	uint32_t netts;
190
	int difftime_ts;
aymeric's avatar
aymeric committed
191

192
	if (im && d->dtmf==0){ /*do not perform timestamp adjustment while a dtmf is being sent, otherwise durations are erroneous */
193 194 195 196 197 198 199 200 201 202 203 204
		uint32_t packet_ts=mblk_get_timestamp_info(im);
		if (d->last_sent_time==-1){
			d->tsoff = curts - packet_ts;
		}else{
			diffts=packet_ts-d->last_ts;
			difftime_ts=((f->ticker->time-d->last_sent_time)*d->rate)/1000;
			/* detect timestamp jump in the stream and adjust so that they become continuous on the network*/
			if (abs(diffts-difftime_ts)>(d->rate/5)){
				uint32_t tsoff=curts - packet_ts;
				ms_message("Adjusting output timestamp by %i",(tsoff-d->tsoff));
				d->tsoff = tsoff;
			}
205
		}
206 207 208 209
		netts = packet_ts + d->tsoff;
		d->last_sent_time=f->ticker->time;
		d->last_ts=packet_ts;
	}else netts=curts;
aymeric's avatar
aymeric committed
210 211 212
	return netts;
}

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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
static int send_dtmf(MSFilter * f, uint32_t timestamp_start, uint32_t current_timestamp)
{
	SenderData *d = (SenderData *) f->data;
	mblk_t *m1;
	int tev_type;

	/* create the first telephony event packet */
	switch (d->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:
		ms_warning("Bad dtmf: %c.",d->dtmf);
		return -1;
	}


	if (d->dtmf_start == TRUE)
		m1=rtp_session_create_telephone_event_packet(d->session,1);
	else
		m1=rtp_session_create_telephone_event_packet(d->session,0);
	if (m1==NULL) return -1;


	if (RTP_TIMESTAMP_IS_NEWER_THAN(current_timestamp, d->skip_until)) {
		//retransmit end of rtp dtmf event
		mblk_t *tmp;
300
		rtp_session_add_telephone_event(d->session,m1,tev_type,1,10, d->dtmf_ts_step+ (current_timestamp-timestamp_start));
301 302 303 304 305 306 307
		tmp=copymsg(m1);
		rtp_session_sendm_with_ts(d->session,tmp,timestamp_start);
		d->session->rtp.snd_seq--;
		tmp=copymsg(m1);
		rtp_session_sendm_with_ts(d->session,tmp,timestamp_start);
		d->session->rtp.snd_seq--;
		rtp_session_sendm_with_ts(d->session,m1,timestamp_start);
308
	}else {
309
		rtp_session_add_telephone_event(d->session,m1,tev_type,0,10, d->dtmf_ts_step +(current_timestamp-timestamp_start));
310 311 312 313 314
		rtp_session_sendm_with_ts(d->session,m1,timestamp_start);
	}
	return 0;
}

aymeric's avatar
aymeric committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
static void sender_process(MSFilter * f)
{
	SenderData *d = (SenderData *) f->data;
	RtpSession *s = d->session;

	mblk_t *im;
	uint32_t timestamp;

	if (s == NULL){
		ms_queue_flush(f->inputs[0]);
		return;
	}

	if (d->relay_session_id_size>0 && 
		( (f->ticker->time-d->last_rsi_time)>5000 || d->last_rsi_time==0) ) {
		ms_message("relay session id sent in RTCP APP");
aymeric's avatar
aymeric committed
331
		rtp_session_send_rtcp_APP(s,0,"RSID",(const uint8_t *)d->relay_session_id,d->relay_session_id_size);
aymeric's avatar
aymeric committed
332 333
		d->last_rsi_time=f->ticker->time;
	}
334 335 336
	ms_filter_lock(f);
	im = ms_queue_get(f->inputs[0]);
	do {
aymeric's avatar
aymeric committed
337 338
		mblk_t *header;

339 340
		timestamp = get_cur_timestamp(f, im);
		
341 342 343
		if (d->dtmf != 0 && !d->skip) {
			ms_debug("prepare to send RFC2833 dtmf.");
			d->skip_until = timestamp + d->dtmf_duration;
344
			d->dtmf_ts_cur=timestamp;
345 346 347
			d->skip = TRUE;
			d->dtmf_start = TRUE;
		}
348
		if (d->skip) {
349 350 351 352 353 354 355 356 357 358
			uint32_t origin_ts=d->skip_until-d->dtmf_duration;
			if (d->dtmf_start || ((timestamp-d->dtmf_ts_cur) >= d->dtmf_ts_step)){
				ms_debug("Sending RFC2833 packet, start_timestamp=%u, timestamp=%u",origin_ts,timestamp);
				send_dtmf(f, origin_ts, timestamp);
				d->dtmf_ts_cur=timestamp;
				d->dtmf_start = FALSE;
				if (RTP_TIMESTAMP_IS_NEWER_THAN(timestamp, d->skip_until)) {
					d->skip = FALSE;
					d->dtmf = 0;
				}
smorlat's avatar
smorlat committed
359
			}
360
		}
361 362 363 364 365 366 367 368 369
		if (im){
			if (d->skip == FALSE && d->mute_mic==FALSE){
				header = rtp_session_create_packet(s, 12, NULL, 0);
				rtp_set_markbit(header, mblk_get_marker_info(im));
				header->b_cont = im;
				rtp_session_sendm_with_ts(s, header, timestamp);
			}else{
				freemsg(im);
			}
370
		}
371 372
	}while ((im = ms_queue_get(f->inputs[0])) != NULL);
	ms_filter_unlock(f);
aymeric's avatar
aymeric committed
373 374 375 376 377 378 379 380
}

static MSFilterMethod sender_methods[] = {
	{MS_RTP_SEND_MUTE_MIC, sender_mute_mic},
	{MS_RTP_SEND_UNMUTE_MIC, sender_unmute_mic},
	{MS_RTP_SEND_SET_SESSION, sender_set_session},
	{MS_RTP_SEND_SEND_DTMF, sender_send_dtmf},
	{MS_RTP_SEND_SET_RELAY_SESSION_ID, sender_set_relay_session_id},
381
	{MS_FILTER_GET_SAMPLE_RATE, sender_get_sr },
382 383
	{MS_FILTER_GET_NCHANNELS, sender_get_ch },
	{MS_FILTER_SET_NCHANNELS, sender_set_ch },
384
	{MS_RTP_SEND_SET_DTMF_DURATION, sender_set_dtmf_duration },
aymeric's avatar
aymeric committed
385 386 387 388 389 390 391 392
	{0, NULL}
};

#ifdef _MSC_VER

MSFilterDesc ms_rtp_send_desc = {
	MS_RTP_SEND_ID,
	"MSRtpSend",
393
	N_("RTP output filter"),
aymeric's avatar
aymeric committed
394 395 396 397 398 399 400 401 402
	MS_FILTER_OTHER,
	NULL,
	1,
	0,
	sender_init,
	NULL,
	sender_process,
	NULL,
	sender_uninit,
403 404
	sender_methods,
	MS_FILTER_IS_PUMP
aymeric's avatar
aymeric committed
405 406 407 408 409 410 411
};

#else

MSFilterDesc ms_rtp_send_desc = {
	.id = MS_RTP_SEND_ID,
	.name = "MSRtpSend",
412
	.text = N_("RTP output filter"),
aymeric's avatar
aymeric committed
413 414 415 416 417 418
	.category = MS_FILTER_OTHER,
	.ninputs = 1,
	.noutputs = 0,
	.init = sender_init,
	.process = sender_process,
	.uninit = sender_uninit,
419 420
	.methods = sender_methods,
	.flags=MS_FILTER_IS_PUMP
aymeric's avatar
aymeric committed
421 422 423 424 425 426 427
};

#endif

struct ReceiverData {
	RtpSession *session;
	int rate;
428
	int nchannels;
Simon Morlat's avatar
Simon Morlat committed
429
	bool_t starting;
aymeric's avatar
aymeric committed
430 431 432 433 434 435 436 437 438
};

typedef struct ReceiverData ReceiverData;

static void receiver_init(MSFilter * f)
{
	ReceiverData *d = (ReceiverData *)ms_new(ReceiverData, 1);
	d->session = NULL;
	d->rate = 8000;
439
	d->nchannels = 1;
aymeric's avatar
aymeric committed
440 441 442
	f->data = d;
}

443 444
static void receiver_postprocess(MSFilter * f){
	/*ReceiverData *d = (ReceiverData *) f->data;*/
aymeric's avatar
aymeric committed
445 446
}

447
static void receiver_uninit(MSFilter * f){
aymeric's avatar
aymeric committed
448
	ReceiverData *d = (ReceiverData *) f->data;
449
	ms_free(d);
aymeric's avatar
aymeric committed
450 451 452 453 454 455 456 457 458 459
}

static int receiver_set_session(MSFilter * f, void *arg)
{
	ReceiverData *d = (ReceiverData *) f->data;
	RtpSession *s = (RtpSession *) arg;
	PayloadType *pt = rtp_profile_get_payload(rtp_session_get_profile(s),
											  rtp_session_get_recv_payload_type
											  (s));
	if (pt != NULL) {
460
		d->rate = pt->clock_rate;
aymeric's avatar
aymeric committed
461
	} else {
462 463
		ms_warning("Receiving undefined payload type %i ?",
		    rtp_session_get_recv_payload_type(s));
aymeric's avatar
aymeric committed
464 465 466 467 468 469
	}
	d->session = s;

	return 0;
}

smorlat's avatar
smorlat committed
470 471 472 473 474 475 476 477 478 479
static int receiver_get_sr(MSFilter *f, void *arg){
	ReceiverData *d = (ReceiverData *) f->data;
	PayloadType *pt;
	if (d->session==NULL) {
		ms_warning("Could not obtain sample rate, session is not set.");
		return -1;
	}
	pt=rtp_profile_get_payload(rtp_session_get_profile(d->session),
									rtp_session_get_recv_payload_type(d->session));
	if (pt != NULL) {
480 481 482 483
		if (strcasecmp(pt->mime_type,"G722")==0)
			*(int*)arg=16000;
		else
			*(int*)arg=pt->clock_rate;
smorlat's avatar
smorlat committed
484 485 486 487 488 489 490
	}else{
		ms_warning("Could not obtain sample rate, payload type is unknown.");
		return -1;
	}
	return 0;
}

491 492 493 494 495 496 497 498 499 500 501 502
static int receiver_get_ch(MSFilter *f, void *arg) {
	ReceiverData *d = (ReceiverData *)f->data;
	*(int *)arg = d->nchannels;
	return 0;
}

static int receiver_set_ch(MSFilter *f, void *arg) {
	ReceiverData *d = (ReceiverData *)f->data;
	d->nchannels = *(int *)arg;
	return 0;
}

aymeric's avatar
aymeric committed
503 504
static void receiver_preprocess(MSFilter * f){
	ReceiverData *d = (ReceiverData *) f->data;
Simon Morlat's avatar
Simon Morlat committed
505
	d->starting=TRUE;
aymeric's avatar
aymeric committed
506 507 508 509 510 511 512 513 514 515 516
}

static void receiver_process(MSFilter * f)
{
	ReceiverData *d = (ReceiverData *) f->data;
	mblk_t *m;
	uint32_t timestamp;

	if (d->session == NULL)
		return;

Simon Morlat's avatar
Simon Morlat committed
517 518 519 520 521 522 523 524 525
	if (d->starting){
		PayloadType *pt=rtp_profile_get_payload(
			rtp_session_get_profile(d->session),
			rtp_session_get_recv_payload_type(d->session));
		if (pt && pt->type!=PAYLOAD_VIDEO)
			rtp_session_flush_sockets(d->session);
		d->starting=FALSE;
	}

aymeric's avatar
aymeric committed
526
	timestamp = (uint32_t) (f->ticker->time * (d->rate/1000));
aymeric's avatar
aymeric committed
527 528 529
	while ((m = rtp_session_recvm_with_ts(d->session, timestamp)) != NULL) {
		mblk_set_timestamp_info(m, rtp_get_timestamp(m));
		mblk_set_marker_info(m, rtp_get_markbit(m));
jehan's avatar
jehan committed
530
		mblk_set_cseq(m, rtp_get_seqnumber(m));
aymeric's avatar
aymeric committed
531 532 533 534 535 536
		rtp_get_payload(m,&m->b_rptr);
		ms_queue_put(f->outputs[0], m);
	}
}

static MSFilterMethod receiver_methods[] = {
smorlat's avatar
smorlat committed
537 538
	{	MS_RTP_RECV_SET_SESSION	, receiver_set_session	},
	{	MS_FILTER_GET_SAMPLE_RATE	, receiver_get_sr		},
539 540
	{	MS_FILTER_GET_NCHANNELS	,	receiver_get_ch	},
	{	MS_FILTER_SET_NCHANNELS	,	receiver_set_ch	},
smorlat's avatar
smorlat committed
541
	{	0, NULL}
aymeric's avatar
aymeric committed
542 543 544 545 546 547 548
};

#ifdef _MSC_VER

MSFilterDesc ms_rtp_recv_desc = {
	MS_RTP_RECV_ID,
	"MSRtpRecv",
549
	N_("RTP input filter"),
aymeric's avatar
aymeric committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
	MS_FILTER_OTHER,
	NULL,
	0,
	1,
	receiver_init,
	receiver_preprocess,
	receiver_process,
	receiver_postprocess,
	receiver_uninit,
	receiver_methods
};

#else

MSFilterDesc ms_rtp_recv_desc = {
	.id = MS_RTP_RECV_ID,
	.name = "MSRtpRecv",
567
	.text = N_("RTP input filter"),
aymeric's avatar
aymeric committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
	.category = MS_FILTER_OTHER,
	.ninputs = 0,
	.noutputs = 1,
	.init = receiver_init,
	.preprocess = receiver_preprocess,
	.process = receiver_process,
	.postprocess=receiver_postprocess,
	.uninit = receiver_uninit,
	.methods = receiver_methods
};

#endif

MS_FILTER_DESC_EXPORT(ms_rtp_send_desc)
MS_FILTER_DESC_EXPORT(ms_rtp_recv_desc)