167
179
struct timeval tv;
168
180
static struct ast_frame null_frame = { AST_FRAME_NULL, };
181
char iabuf[INET_ADDRSTRLEN];
169
182
gettimeofday(&tv, NULL);
170
183
if ((tv.tv_sec < rtp->dtmfmute.tv_sec) ||
171
184
((tv.tv_sec == rtp->dtmfmute.tv_sec) && (tv.tv_usec < rtp->dtmfmute.tv_usec))) {
172
ast_log(LOG_DEBUG, "Ignore potential DTMF echo from '%s'\n", inet_ntoa(rtp->them.sin_addr));
185
ast_log(LOG_DEBUG, "Ignore potential DTMF echo from '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr));
174
187
rtp->dtmfduration = 0;
175
188
return &null_frame;
177
ast_log(LOG_DEBUG, "Sending dtmf: %d (%c), at %s\n", rtp->resp, rtp->resp, inet_ntoa(rtp->them.sin_addr));
178
rtp->f.frametype = AST_FRAME_DTMF;
179
rtp->f.subclass = rtp->resp;
190
ast_log(LOG_DEBUG, "Sending dtmf: %d (%c), at %s\n", rtp->resp, rtp->resp, ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr));
191
if (rtp->resp == 'X') {
192
rtp->f.frametype = AST_FRAME_CONTROL;
193
rtp->f.subclass = AST_CONTROL_FLASH;
195
rtp->f.frametype = AST_FRAME_DTMF;
196
rtp->f.subclass = rtp->resp;
180
198
rtp->f.datalen = 0;
181
199
rtp->f.samples = 0;
182
200
rtp->f.mallocd = 0;
269
291
/* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
270
292
totally help us out becuase we don't have an engine to keep it going and we are not
271
293
guaranteed to have it every 20ms or anything */
273
printf("RFC3389: %d bytes, format is %d\n", len, rtp->lastrxformat);
295
printf("RFC3389: %d bytes, level %d...\n", len, rtp->lastrxformat);
275
ast_log(LOG_NOTICE, "RFC3389 support incomplete. Turn off on client if possible\n");
276
if (!rtp->lastrxformat)
278
switch(rtp->lastrxformat) {
279
case AST_FORMAT_ULAW:
280
rtp->f.frametype = AST_FRAME_VOICE;
281
rtp->f.subclass = AST_FORMAT_ULAW;
282
rtp->f.datalen = 160;
283
rtp->f.samples = 160;
284
memset(rtp->f.data, 0x7f, rtp->f.datalen);
287
case AST_FORMAT_ALAW:
288
rtp->f.frametype = AST_FRAME_VOICE;
289
rtp->f.subclass = AST_FORMAT_ALAW;
290
rtp->f.datalen = 160;
291
rtp->f.samples = 160;
292
memset(rtp->f.data, 0x7e, rtp->f.datalen); /* XXX Is this right? XXX */
295
case AST_FORMAT_SLINEAR:
296
rtp->f.frametype = AST_FRAME_VOICE;
297
rtp->f.subclass = AST_FORMAT_SLINEAR;
298
rtp->f.datalen = 320;
299
rtp->f.samples = 160;
300
memset(rtp->f.data, 0x00, rtp->f.datalen);
304
ast_log(LOG_NOTICE, "Don't know how to handle RFC3389 for receive codec %d\n", rtp->lastrxformat);
297
if (!(rtp->flags & FLAG_3389_WARNING)) {
298
ast_log(LOG_NOTICE, "RFC3389 support incomplete. Turn off on client if possible\n");
299
rtp->flags |= FLAG_3389_WARNING;
301
/* Must have at least one byte */
305
rtp->f.data = rtp->rawdata + AST_FRIENDLY_OFFSET;
306
rtp->f.datalen = len - 1;
307
rtp->f.offset = AST_FRIENDLY_OFFSET;
308
memcpy(rtp->f.data, data + 1, len - 1);
314
rtp->f.frametype = AST_FRAME_CNG;
315
rtp->f.subclass = data[0] & 0x7f;
316
rtp->f.datalen = len - 1;
318
rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
360
378
return &null_frame;
381
static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
383
if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
384
gettimeofday(&rtp->rxcore, NULL);
385
rtp->rxcore.tv_sec -= timestamp / 8000;
386
rtp->rxcore.tv_usec -= (timestamp % 8000) * 125;
387
/* Round to 20ms for nice, pretty timestamps */
388
rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 20000;
389
if (rtp->rxcore.tv_usec < 0) {
390
/* Adjust appropriately if necessary */
391
rtp->rxcore.tv_usec += 1000000;
392
rtp->rxcore.tv_sec -= 1;
395
tv->tv_sec = rtp->rxcore.tv_sec + timestamp / 8000;
396
tv->tv_usec = rtp->rxcore.tv_usec + (timestamp % 8000) * 125;
397
if (tv->tv_usec >= 1000000) {
398
tv->tv_usec -= 1000000;
363
403
struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
366
406
struct sockaddr_in sin;
368
408
unsigned int seqno;
414
char iabuf[INET_ADDRSTRLEN];
372
415
unsigned int timestamp;
373
416
unsigned int *rtpheader;
374
417
static struct ast_frame *f, null_frame = { AST_FRAME_NULL, };
392
438
ast_log(LOG_WARNING, "RTP Read too short\n");
393
439
return &null_frame;
396
/* Send to whoever sent to us */
397
if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
398
(rtp->them.sin_port != sin.sin_port)) {
399
memcpy(&rtp->them, &sin, sizeof(rtp->them));
400
ast_log(LOG_DEBUG, "RTP NAT: Using address %s:%d\n", inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
403
442
/* Ignore if the other side hasn't been given an address
405
444
if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
406
445
return &null_frame;
448
/* Send to whoever sent to us */
449
if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
450
(rtp->them.sin_port != sin.sin_port)) {
451
memcpy(&rtp->them, &sin, sizeof(rtp->them));
452
ast_log(LOG_DEBUG, "RTP NAT: Using address %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port));
409
457
seqno = ntohl(rtpheader[0]);
459
/* Check RTP version */
460
version = (seqno & 0xC0000000) >> 30;
410
464
payloadtype = (seqno & 0x7f0000) >> 16;
411
465
mark = seqno & (1 << 23);
466
ext = seqno & (1 << 28);
413
468
timestamp = ntohl(rtpheader[1]);
470
/* RTP Extension present */
472
hdrlen += (ntohl(rtpheader[3]) & 0xffff) << 2;
476
ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d)\n", res, hdrlen);
416
printf("Got RTP packet from %s:%d (type %d, seq %d, ts %d, len = %d)\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
481
printf("Got RTP packet from %s:%d (type %d, seq %d, ts %d, len = %d)\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
418
483
rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
419
484
if (!rtpPT.isAstFormat) {
420
// This is special in-band data that's not one of our codecs
485
/* This is special in-band data that's not one of our codecs */
421
486
if (rtpPT.code == AST_RTP_DTMF) {
422
487
/* It's special -- rfc2833 process it */
423
f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
488
if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
489
f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
490
rtp->lasteventseqn = seqno;
424
492
if (f) return f; else return &null_frame;
425
493
} else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
426
494
/* It's really special -- process it the Cisco way */
427
f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
495
if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
496
f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
497
rtp->lasteventseqn = seqno;
428
499
if (f) return f; else return &null_frame;
429
500
} else if (rtpPT.code == AST_RTP_CN) {
430
501
/* Comfort Noise */
493
565
rtp->f.samples = g723_samples(rtp->f.data, rtp->f.datalen);
495
567
case AST_FORMAT_SPEEX:
496
rtp->f.samples = 160;
497
// assumes that the RTP packet contained one Speex frame
568
/* assumes that the RTP packet contained one Speex frame */
569
rtp->f.samples = 160;
571
case AST_FORMAT_LPC10:
572
rtp->f.samples = 22 * 8;
573
rtp->f.samples += (((char *)(rtp->f.data))[7] & 0x1) * 8;
500
576
ast_log(LOG_NOTICE, "Unable to calculate samples for format %s\n", ast_getformatname(rtp->f.subclass));
579
calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
504
581
/* Video -- samples is # of samples vs. 90000 */
505
582
if (!rtp->lastividtimestamp)
506
583
rtp->lastividtimestamp = timestamp;
507
584
rtp->f.samples = timestamp - rtp->lastividtimestamp;
508
585
rtp->lastividtimestamp = timestamp;
586
rtp->f.delivery.tv_sec = 0;
587
rtp->f.delivery.tv_usec = 0;
510
589
rtp->f.subclass |= 0x1;
530
609
{{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
531
610
{{1, AST_FORMAT_LPC10}, "audio", "LPC"},
532
611
{{1, AST_FORMAT_G729A}, "audio", "G729"},
533
{{1, AST_FORMAT_SPEEX}, "audio", "SPEEX"},
612
{{1, AST_FORMAT_SPEEX}, "audio", "speex"},
534
613
{{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
535
614
{{0, AST_RTP_DTMF}, "audio", "telephone-event"},
536
615
{{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event"},
546
625
table for transmission */
547
626
static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
548
627
[0] = {1, AST_FORMAT_ULAW},
549
[2] = {1, AST_FORMAT_G726}, // Technically this is G.721, but if Cisco can do it, so can we...
628
[2] = {1, AST_FORMAT_G726}, /* Technically this is G.721, but if Cisco can do it, so can we... */
550
629
[3] = {1, AST_FORMAT_GSM},
551
630
[4] = {1, AST_FORMAT_G723_1},
552
[5] = {1, AST_FORMAT_ADPCM}, // 8 kHz
553
[6] = {1, AST_FORMAT_ADPCM}, // 16 kHz
631
[5] = {1, AST_FORMAT_ADPCM}, /* 8 kHz */
632
[6] = {1, AST_FORMAT_ADPCM}, /* 16 kHz */
554
633
[7] = {1, AST_FORMAT_LPC10},
555
634
[8] = {1, AST_FORMAT_ALAW},
556
[10] = {1, AST_FORMAT_SLINEAR}, // 2 channels
557
[11] = {1, AST_FORMAT_SLINEAR}, // 1 channel
635
[10] = {1, AST_FORMAT_SLINEAR}, /* 2 channels */
636
[11] = {1, AST_FORMAT_SLINEAR}, /* 1 channel */
558
637
[13] = {0, AST_RTP_CN},
559
[16] = {1, AST_FORMAT_ADPCM}, // 11.025 kHz
560
[17] = {1, AST_FORMAT_ADPCM}, // 22.050 kHz
638
[16] = {1, AST_FORMAT_ADPCM}, /* 11.025 kHz */
639
[17] = {1, AST_FORMAT_ADPCM}, /* 22.050 kHz */
561
640
[18] = {1, AST_FORMAT_G729A},
641
[19] = {0, AST_RTP_CN}, /* Also used for CN */
562
642
[26] = {1, AST_FORMAT_JPEG},
563
643
[31] = {1, AST_FORMAT_H261},
564
644
[34] = {1, AST_FORMAT_H263},
565
645
[97] = {1, AST_FORMAT_ILBC},
566
646
[101] = {0, AST_RTP_DTMF},
567
647
[110] = {1, AST_FORMAT_SPEEX},
568
[121] = {0, AST_RTP_CISCO_DTMF}, // Must be type 121
648
[121] = {0, AST_RTP_CISCO_DTMF}, /* Must be type 121 */
571
651
void ast_rtp_pt_clear(struct ast_rtp* rtp)
596
676
rtp->rtp_lookup_code_cache_result = 0;
599
// Make a note of a RTP payload type that was seen in a SDP "m=" line.
600
// By default, use the well-known value for this type (although it may
601
// still be set to a different value by a subsequent "a=rtpmap:" line):
679
/* Make a note of a RTP payload type that was seen in a SDP "m=" line. */
680
/* By default, use the well-known value for this type (although it may */
681
/* still be set to a different value by a subsequent "a=rtpmap:" line): */
602
682
void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt) {
603
if (pt < 0 || pt > MAX_RTP_PT) return; // bogus payload type
683
if (pt < 0 || pt > MAX_RTP_PT) return; /* bogus payload type */
605
685
if (static_RTP_PT[pt].code != 0) {
606
686
rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
610
// Make a note of a RTP payload type (with MIME type) that was seen in
611
// a SDP "a=rtpmap:" line.
690
/* Make a note of a RTP payload type (with MIME type) that was seen in */
691
/* a SDP "a=rtpmap:" line. */
612
692
void ast_rtp_set_rtpmap_type(struct ast_rtp* rtp, int pt,
613
693
char* mimeType, char* mimeSubtype) {
616
if (pt < 0 || pt > MAX_RTP_PT) return; // bogus payload type
696
if (pt < 0 || pt > MAX_RTP_PT) return; /* bogus payload type */
618
698
for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
619
699
if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
643
struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt) {
723
void ast_rtp_offered_from_local(struct ast_rtp* rtp, int local) {
725
rtp->rtp_offered_from_local = local;
727
ast_log(LOG_WARNING, "rtp structure is null\n");
730
struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt)
732
struct rtpPayloadType result;
734
result.isAstFormat = result.code = 0;
644
735
if (pt < 0 || pt > MAX_RTP_PT) {
645
struct rtpPayloadType result;
646
result.isAstFormat = result.code = 0;
647
return result; // bogus payload type
736
return result; /* bogus payload type */
649
/* Gotta use our static one, since that's what we sent against */
650
return static_RTP_PT[pt];
738
/* Start with the negotiated codecs */
739
if (!rtp->rtp_offered_from_local)
740
result = rtp->current_RTP_PT[pt];
741
/* If it doesn't exist, check our static RTP type list, just in case */
743
result = static_RTP_PT[pt];
747
/* Looks up an RTP code out of our *static* outbound list */
653
748
int ast_rtp_lookup_code(struct ast_rtp* rtp, int isAstFormat, int code) {
656
/* Looks up an RTP code out of our *static* outbound list */
658
752
if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
659
753
code == rtp->rtp_lookup_code_cache_code) {
660
// Use our cached mapping, to avoid the overhead of the loop below
754
/* Use our cached mapping, to avoid the overhead of the loop below */
661
755
return rtp->rtp_lookup_code_cache_result;
758
/* Check the dynamic list first */
759
for (pt = 0; pt < MAX_RTP_PT; ++pt) {
760
if (rtp->current_RTP_PT[pt].code == code &&
761
rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
762
rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
763
rtp->rtp_lookup_code_cache_code = code;
764
rtp->rtp_lookup_code_cache_result = pt;
769
/* Then the static list */
664
770
for (pt = 0; pt < MAX_RTP_PT; ++pt) {
665
771
if (static_RTP_PT[pt].code == code &&
666
772
static_RTP_PT[pt].isAstFormat == isAstFormat) {
794
static int rtp_socket(void)
798
s = socket(AF_INET, SOCK_DGRAM, 0);
800
flags = fcntl(s, F_GETFL);
801
fcntl(s, F_SETFL, flags | O_NONBLOCK);
804
setsockopt(s, SOL_SOCKET, SO_NO_CHECK, &checksums, sizeof(checksums));
688
811
static struct ast_rtcp *ast_rtcp_new(void)
690
813
struct ast_rtcp *rtcp;
692
814
rtcp = malloc(sizeof(struct ast_rtcp));
695
817
memset(rtcp, 0, sizeof(struct ast_rtcp));
696
rtcp->s = socket(AF_INET, SOCK_DGRAM, 0);
818
rtcp->s = rtp_socket();
697
819
rtcp->us.sin_family = AF_INET;
698
820
if (rtcp->s < 0) {
700
822
ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
703
flags = fcntl(rtcp->s, F_GETFL);
704
fcntl(rtcp->s, F_SETFL, flags | O_NONBLOCK);
708
struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
828
struct ast_rtp *ast_rtp_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr addr)
710
830
struct ast_rtp *rtp;
714
834
rtp = malloc(sizeof(struct ast_rtp));
739
857
/* Must be an even port number by RTP spec */
740
858
rtp->us.sin_port = htons(x);
859
rtp->us.sin_addr = addr;
742
861
rtp->rtcp->us.sin_port = htons(x + 1);
743
if (!bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us)) &&
862
if (!(first = bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) &&
744
863
(!rtp->rtcp || !bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us))))
866
/* Primary bind succeeded! Gotta recreate it */
868
rtp->s = rtp_socket();
746
870
if (errno != EADDRINUSE) {
747
871
ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
835
static unsigned int calc_txstamp(struct ast_rtp *rtp)
966
static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
837
968
struct timeval now;
839
970
if (!rtp->txcore.tv_sec && !rtp->txcore.tv_usec) {
840
971
gettimeofday(&rtp->txcore, NULL);
842
gettimeofday(&now, NULL);
843
ms = (now.tv_sec - rtp->txcore.tv_sec) * 1000;
844
ms += (now.tv_usec - rtp->txcore.tv_usec) / 1000;
845
/* Use what we just got for next time */
846
rtp->txcore.tv_sec = now.tv_sec;
847
rtp->txcore.tv_usec = now.tv_usec;
972
/* Round to 20ms for nice, pretty timestamps */
973
rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
975
if (delivery && (delivery->tv_sec || delivery->tv_usec)) {
976
/* Use previous txcore */
977
ms = (delivery->tv_sec - rtp->txcore.tv_sec) * 1000;
978
ms += (1000000 + delivery->tv_usec - rtp->txcore.tv_usec) / 1000 - 1000;
979
rtp->txcore.tv_sec = delivery->tv_sec;
980
rtp->txcore.tv_usec = delivery->tv_usec;
982
gettimeofday(&now, NULL);
983
ms = (now.tv_sec - rtp->txcore.tv_sec) * 1000;
984
ms += (1000000 + now.tv_usec - rtp->txcore.tv_usec) / 1000 - 1000;
985
/* Use what we just got for next time */
986
rtp->txcore.tv_sec = now.tv_sec;
987
rtp->txcore.tv_usec = now.tv_usec;
884
1025
rtp->dtmfmute.tv_usec -= 1000000;
885
1026
rtp->dtmfmute.tv_sec += 1;
888
ms = calc_txstamp(rtp);
889
/* Default prediction */
890
pred = rtp->lastts + ms * 8;
892
1029
/* Get a pointer to the header */
893
1030
rtpheader = (unsigned int *)data;
894
rtpheader[0] = htonl((2 << 30) | (1 << 23) | (101 << 16) | (rtp->seqno++));
1031
rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
895
1032
rtpheader[1] = htonl(rtp->lastts);
896
1033
rtpheader[2] = htonl(rtp->ssrc);
897
1034
rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (0));
899
1036
if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
900
1037
res = sendto(rtp->s, (void *)rtpheader, hdrlen + 4, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
902
ast_log(LOG_NOTICE, "RTP Transmission error to %s:%d: %s\n", inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), strerror(errno));
1039
ast_log(LOG_NOTICE, "RTP Transmission error to %s:%d: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), strerror(errno));
904
printf("Sent %d bytes of RTP data to %s:%d\n", res, inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
1041
printf("Sent %d bytes of RTP data to %s:%d\n", res, ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port));
908
1045
/* Clear marker bit and increment seqno */
909
rtpheader[0] = htonl((2 << 30) | (101 << 16) | (rtp->seqno++));
1046
rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno++));
910
1047
/* Make duration 800 (100ms) */
911
1048
rtpheader[3] |= htonl((800));
912
1049
/* Set the End bit for the last 3 */
913
1050
rtpheader[3] |= htonl((1 << 23));
1051
} else if ( x < 5) {
1052
rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno++));
950
1096
pred = rtp->lastts + g723_samples(f->data, f->datalen);
952
1098
case AST_FORMAT_SPEEX:
953
pred = rtp->lastts + 160;
954
// assumes that the RTP packet contains one Speex frame
1099
pred = rtp->lastts + 160;
1100
/* assumes that the RTP packet contains one Speex frame */
1102
case AST_FORMAT_LPC10:
1103
/* assumes that the RTP packet contains one LPC10 frame */
1104
pred = rtp->lastts + 22 * 8;
1105
pred += (((char *)(f->data))[7] & 0x1) * 8;
957
1108
ast_log(LOG_WARNING, "Not sure about timestamp format for codec format %s\n", ast_getformatname(f->subclass));
960
1110
/* Re-calculate last TS */
961
1111
rtp->lastts = rtp->lastts + ms * 8;
962
/* If it's close to our prediction, go for it */
963
if (abs(rtp->lastts - pred) < 640)
966
ast_log(LOG_DEBUG, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
1112
if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
1113
/* If this isn't an absolute delivery time, Check if it is close to our prediction,
1114
and if so, go with our prediction */
1115
if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW)
1118
ast_log(LOG_DEBUG, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
968
1123
mark = f->subclass & 0x1;
969
1124
pred = rtp->lastovidtimestamp + f->samples;
970
1125
/* Re-calculate last TS */
971
1126
rtp->lastts = rtp->lastts + ms * 90;
972
1127
/* If it's close to our prediction, go for it */
973
if (abs(rtp->lastts - pred) < 7200) {
975
rtp->lastovidtimestamp += f->samples;
977
ast_log(LOG_DEBUG, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
978
rtp->lastovidtimestamp = rtp->lastts;
1128
if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
1129
if (abs(rtp->lastts - pred) < 7200) {
1131
rtp->lastovidtimestamp += f->samples;
1133
ast_log(LOG_DEBUG, "Difference is %d, ms is %d (%d), pred/ts/samples %d/%d/%d\n", abs(rtp->lastts - pred), ms, ms * 90, rtp->lastts, pred, f->samples);
1134
rtp->lastovidtimestamp = rtp->lastts;
981
1138
/* Get a pointer to the header */
986
1143
if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
987
1144
res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
989
ast_log(LOG_NOTICE, "RTP Transmission error to %s:%d: %s\n", inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), strerror(errno));
1146
ast_log(LOG_NOTICE, "RTP Transmission error to %s:%d: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), strerror(errno));
991
printf("Sent %d bytes of RTP data to %s:%d\n", res, inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
1148
printf("Sent %d bytes of RTP data to %s:%d\n", res, ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port));
1051
1208
while((f = ast_smoother_read(rtp->smoother)))
1052
1209
ast_rtp_raw_write(rtp, f, codec);
1211
case AST_FORMAT_ADPCM:
1212
case AST_FORMAT_G726:
1213
if (!rtp->smoother) {
1214
rtp->smoother = ast_smoother_new(80);
1216
if (!rtp->smoother) {
1217
ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1220
ast_smoother_feed(rtp->smoother, _f);
1222
while((f = ast_smoother_read(rtp->smoother)))
1223
ast_rtp_raw_write(rtp, f, codec);
1054
1225
case AST_FORMAT_G729A:
1055
1226
if (!rtp->smoother) {
1056
1227
rtp->smoother = ast_smoother_new(20);
1229
ast_smoother_set_flags(rtp->smoother, AST_SMOOTHER_FLAG_G729);
1058
1231
if (!rtp->smoother) {
1059
1232
ast_log(LOG_WARNING, "Unable to create g729 smoother :(\n");
1165
1339
struct sockaddr_in vac0, vac1;
1166
1340
struct sockaddr_in t0, t1;
1167
1341
struct sockaddr_in vt0, vt1;
1342
char iabuf[INET_ADDRSTRLEN];
1169
1344
void *pvt0, *pvt1;
1346
int codec0,codec1, oldcodec0, oldcodec1;
1171
1348
memset(&vt0, 0, sizeof(vt0));
1172
1349
memset(&vt1, 0, sizeof(vt1));
1173
1350
memset(&vac0, 0, sizeof(vac0));
1174
1351
memset(&vac1, 0, sizeof(vac1));
1176
/* XXX Wait a half a second for things to settle up
1177
this really should be fixed XXX */
1178
ast_autoservice_start(c0);
1179
ast_autoservice_start(c1);
1181
ast_autoservice_stop(c0);
1182
ast_autoservice_stop(c1);
1184
1353
/* if need DTMF, cant native bridge */
1185
1354
if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
1187
1356
ast_mutex_lock(&c0->lock);
1188
ast_mutex_lock(&c1->lock);
1357
while(ast_mutex_trylock(&c1->lock)) {
1358
ast_mutex_unlock(&c0->lock);
1360
ast_mutex_lock(&c0->lock);
1189
1362
pr0 = get_proto(c0);
1190
1363
pr1 = get_proto(c1);
1233
if (pr0->set_rtp_peer(c0, p1, vp1))
1411
if (pr0->set_rtp_peer(c0, p1, vp1, codec1))
1234
1412
ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
1236
1414
/* Store RTP peer */
1237
1415
ast_rtp_get_peer(p1, &ac1);
1239
ast_rtp_get_peer(p1, &vac1);
1417
ast_rtp_get_peer(vp1, &vac1);
1241
if (pr1->set_rtp_peer(c1, p0, vp0))
1419
if (pr1->set_rtp_peer(c1, p0, vp0, codec0))
1242
1420
ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
1244
1422
/* Store RTP peer */
1245
1423
ast_rtp_get_peer(p0, &ac0);
1247
ast_rtp_get_peer(p0, &vac0);
1425
ast_rtp_get_peer(vp0, &vac0);
1249
1427
ast_mutex_unlock(&c0->lock);
1250
1428
ast_mutex_unlock(&c1->lock);
1255
1435
if ((c0->pvt->pvt != pvt0) ||
1256
1436
(c1->pvt->pvt != pvt1) ||
1257
1437
(c0->masq || c0->masqr || c1->masq || c1->masqr)) {
1258
1438
ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
1259
1439
if (c0->pvt->pvt == pvt0) {
1260
if (pr0->set_rtp_peer(c0, NULL, NULL))
1440
if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
1261
1441
ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1263
1443
if (c1->pvt->pvt == pvt1) {
1264
if (pr1->set_rtp_peer(c1, NULL, NULL))
1444
if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
1265
1445
ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1267
1447
/* Tell it to try again later */
1271
1451
ast_rtp_get_peer(p1, &t1);
1272
1452
ast_rtp_get_peer(p0, &t0);
1454
codec0 = pr0->get_codec(c0);
1456
codec1 = pr1->get_codec(c1);
1274
1458
ast_rtp_get_peer(vp1, &vt1);
1276
1460
ast_rtp_get_peer(vp0, &vt0);
1277
if (inaddrcmp(&t1, &ac1) || (vp1 && inaddrcmp(&vt1, &vac1))) {
1278
ast_log(LOG_DEBUG, "Oooh, '%s' changed end address\n", c1->name);
1279
if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL, vt1.sin_addr.s_addr ? vp1 : NULL))
1461
if (inaddrcmp(&t1, &ac1) || (vp1 && inaddrcmp(&vt1, &vac1)) || (codec1 != oldcodec1)) {
1462
ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1463
c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t1.sin_addr), ntohs(t1.sin_port), codec1);
1464
ast_log(LOG_DEBUG, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
1465
c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vt1.sin_addr), ntohs(vt1.sin_port), codec1);
1466
ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
1467
c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac1.sin_addr), ntohs(ac1.sin_port), oldcodec1);
1468
ast_log(LOG_DEBUG, "Oooh, '%s' wasv %s:%d/(format %d)\n",
1469
c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vac1.sin_addr), ntohs(vac1.sin_port), oldcodec1);
1470
if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL, vt1.sin_addr.s_addr ? vp1 : NULL, codec1))
1280
1471
ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
1281
1472
memcpy(&ac1, &t1, sizeof(ac1));
1282
1473
memcpy(&vac1, &vt1, sizeof(vac1));
1284
1476
if (inaddrcmp(&t0, &ac0) || (vp0 && inaddrcmp(&vt0, &vac0))) {
1285
ast_log(LOG_DEBUG, "Oooh, '%s' changed end address\n", c0->name);
1286
if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL, vt0.sin_addr.s_addr ? vp0 : NULL))
1477
ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1478
c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t0.sin_addr), ntohs(t0.sin_port), codec0);
1479
ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
1480
c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
1481
if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL, vt0.sin_addr.s_addr ? vp0 : NULL, codec0))
1287
1482
ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
1288
1483
memcpy(&ac0, &t0, sizeof(ac0));
1289
1484
memcpy(&vac0, &vt0, sizeof(vac0));
1291
1487
who = ast_waitfor_n(cs, 2, &to);
1305
1501
ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
1306
1502
if ((c0->pvt->pvt == pvt0) && (!c0->_softhangup)) {
1307
if (pr0->set_rtp_peer(c0, NULL, NULL))
1503
if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
1308
1504
ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1310
1506
if ((c1->pvt->pvt == pvt1) && (!c1->_softhangup)) {
1311
if (pr1->set_rtp_peer(c1, NULL, NULL))
1507
if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
1312
1508
ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1314
1510
/* That's all we needed */