2 * Asterisk -- A telephony toolkit for Linux.
4 * Real-time Protocol Support
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
22 #include <netinet/in.h>
24 #include <sys/socket.h>
25 #include <arpa/inet.h>
28 #include <asterisk/rtp.h>
29 #include <asterisk/frame.h>
30 #include <asterisk/logger.h>
31 #include <asterisk/options.h>
32 #include <asterisk/channel.h>
33 #include <asterisk/acl.h>
34 #include <asterisk/channel_pvt.h>
38 #define TYPE_SILENCE 0x2
39 #define TYPE_DONTSEND 0x3
42 static int dtmftimeout = 300; /* 300 samples */
44 // The value of each payload format mapping:
45 struct rtpPayloadType {
46 int isAstFormat; // whether the following code is an AST_FORMAT
50 #define MAX_RTP_PT 256
56 unsigned char rawdata[1024 + AST_FRIENDLY_OFFSET];
59 unsigned int lastrxts;
64 struct sockaddr_in us;
65 struct sockaddr_in them;
66 struct timeval rxcore;
67 struct timeval txcore;
68 struct ast_smoother *smoother;
71 struct sched_context *sched;
72 struct io_context *io;
74 ast_rtp_callback callback;
75 struct rtpPayloadType current_RTP_PT[MAX_RTP_PT];
76 // a cache for the result of rtp_lookup_code():
77 int rtp_lookup_code_cache_isAstFormat;
78 int rtp_lookup_code_cache_code;
79 int rtp_lookup_code_cache_result;
82 static struct ast_rtp_protocol *protos = NULL;
84 int ast_rtp_fd(struct ast_rtp *rtp)
89 static int g723_len(unsigned char buf)
91 switch(buf & TYPE_MASK) {
105 ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", buf & TYPE_MASK);
110 static int g723_samples(unsigned char *buf, int maxlen)
115 while(pos < maxlen) {
116 res = g723_len(buf[pos]);
125 void ast_rtp_set_data(struct ast_rtp *rtp, void *data)
130 void ast_rtp_set_callback(struct ast_rtp *rtp, ast_rtp_callback callback)
132 rtp->callback = callback;
135 void ast_rtp_setnat(struct ast_rtp *rtp, int nat)
140 static struct ast_frame *send_dtmf(struct ast_rtp *rtp)
142 ast_log(LOG_DEBUG, "Sending dtmf: %d (%c)\n", rtp->resp, rtp->resp);
143 rtp->f.frametype = AST_FRAME_DTMF;
144 rtp->f.subclass = rtp->resp;
154 static struct ast_frame *process_cisco_dtmf(struct ast_rtp *rtp, unsigned char *data, int len)
158 struct ast_frame *f = NULL;
159 event = ntohl(*((unsigned int *)(data)));
162 printf("Cisco Digit: %08x (len = %d)\n", event, len);
166 } else if (event < 11) {
168 } else if (event < 12) {
170 } else if (event < 16) {
171 resp = 'A' + (event - 12);
173 if (rtp->resp && (rtp->resp != resp)) {
177 rtp->dtmfcount = dtmftimeout;
181 static struct ast_frame *process_rfc2833(struct ast_rtp *rtp, unsigned char *data, int len)
185 struct ast_frame *f = NULL;
186 event = ntohl(*((unsigned int *)(data)));
189 printf("Event: %08x (len = %d)\n", event, len);
193 } else if (event < 11) {
195 } else if (event < 12) {
197 } else if (event < 16) {
198 resp = 'A' + (event - 12);
200 if (rtp->resp && (rtp->resp != resp)) {
204 rtp->dtmfcount = dtmftimeout;
208 static struct ast_frame *process_rfc3389(struct ast_rtp *rtp, unsigned char *data, int len)
210 struct ast_frame *f = NULL;
211 /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
212 totally help us out becuase we don't have an engine to keep it going and we are not
213 guaranteed to have it every 20ms or anything */
215 printf("RFC3389: %d bytes, format is %d\n", len, rtp->lastrxformat);
217 ast_log(LOG_NOTICE, "RFC3389 support incomplete. Turn off on client if possible\n");
218 if (!rtp->lastrxformat)
220 switch(rtp->lastrxformat) {
221 case AST_FORMAT_ULAW:
222 rtp->f.frametype = AST_FRAME_VOICE;
223 rtp->f.subclass = AST_FORMAT_ULAW;
224 rtp->f.datalen = 160;
225 rtp->f.samples = 160;
226 memset(rtp->f.data, 0x7f, rtp->f.datalen);
229 case AST_FORMAT_ALAW:
230 rtp->f.frametype = AST_FRAME_VOICE;
231 rtp->f.subclass = AST_FORMAT_ALAW;
232 rtp->f.datalen = 160;
233 rtp->f.samples = 160;
234 memset(rtp->f.data, 0x7e, rtp->f.datalen); /* XXX Is this right? XXX */
237 case AST_FORMAT_SLINEAR:
238 rtp->f.frametype = AST_FRAME_VOICE;
239 rtp->f.subclass = AST_FORMAT_SLINEAR;
240 rtp->f.datalen = 320;
241 rtp->f.samples = 160;
242 memset(rtp->f.data, 0x00, rtp->f.datalen);
246 ast_log(LOG_NOTICE, "Don't know how to handle RFC3389 for receive codec %d\n", rtp->lastrxformat);
251 static int rtpread(int *id, int fd, short events, void *cbdata)
253 struct ast_rtp *rtp = cbdata;
255 f = ast_rtp_read(rtp);
258 rtp->callback(rtp, f, rtp->data);
263 struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
266 struct sockaddr_in sin;
271 unsigned int timestamp;
272 unsigned int *rtpheader;
273 static struct ast_frame *f, null_frame = { AST_FRAME_NULL, };
274 struct rtpPayloadType rtpPT;
278 res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
279 0, (struct sockaddr *)&sin, &len);
282 rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
284 ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
290 ast_log(LOG_WARNING, "RTP Read too short\n");
294 /* Send to whoever sent to us */
295 if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
296 (rtp->them.sin_port != sin.sin_port)) {
297 memcpy(&rtp->them, &sin, sizeof(rtp->them));
298 ast_log(LOG_DEBUG, "RTP NAT: Using address %s:%d\n", inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
302 seqno = ntohl(rtpheader[0]);
303 payloadtype = (seqno & 0x7f0000) >> 16;
305 timestamp = ntohl(rtpheader[1]);
307 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);
309 rtp->f.frametype = AST_FRAME_VOICE;
310 rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
311 if (!rtpPT.isAstFormat) {
312 // This is special in-band data that's not one of our codecs
313 if (rtpPT.code == AST_RTP_DTMF) {
314 /* It's special -- rfc2833 process it */
315 f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
316 if (f) return f; else return &null_frame;
317 } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
318 /* It's really special -- process it the Cisco way */
319 f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
320 if (f) return f; else return &null_frame;
321 } else if (rtpPT.code == AST_RTP_CN) {
323 f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
324 if (f) return f; else return &null_frame;
326 ast_log(LOG_NOTICE, "Unknown RTP codec %d received\n", payloadtype);
330 rtp->f.subclass = rtpPT.code;
331 rtp->lastrxformat = rtp->f.subclass;
334 rtp->lastrxts = timestamp;
336 if (rtp->dtmfcount) {
338 printf("dtmfcount was %d\n", rtp->dtmfcount);
340 rtp->dtmfcount -= (timestamp - rtp->lastrxts);
341 if (rtp->dtmfcount < 0)
344 if (dtmftimeout != rtp->dtmfcount)
345 printf("dtmfcount is %d\n", rtp->dtmfcount);
348 rtp->lastrxts = timestamp;
350 /* Send any pending DTMF */
351 if (rtp->resp && !rtp->dtmfcount) {
352 ast_log(LOG_DEBUG, "Sending pending DTMF\n");
353 return send_dtmf(rtp);
356 rtp->f.datalen = res - hdrlen;
357 rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
358 rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
359 switch(rtp->f.subclass) {
360 case AST_FORMAT_ULAW:
361 case AST_FORMAT_ALAW:
362 rtp->f.samples = rtp->f.datalen;
364 case AST_FORMAT_SLINEAR:
365 rtp->f.samples = rtp->f.datalen / 2;
368 rtp->f.samples = 160 * (rtp->f.datalen / 33);
370 case AST_FORMAT_ILBC:
371 rtp->f.samples = 240 * (rtp->f.datalen / 50);
373 case AST_FORMAT_ADPCM:
374 rtp->f.samples = rtp->f.datalen * 2;
376 case AST_FORMAT_G729A:
377 rtp->f.samples = rtp->f.datalen * 8;
379 case AST_FORMAT_G723_1:
380 rtp->f.samples = g723_samples(rtp->f.data, rtp->f.datalen);
382 case AST_FORMAT_SPEEX:
383 rtp->f.samples = 160;
384 // assumes that the RTP packet contained one Speex frame
387 ast_log(LOG_NOTICE, "Unable to calculate samples for format %d\n", rtp->f.subclass);
394 // The following array defines the MIME type (and subtype) for each
395 // of our codecs, or RTP-specific data type.
397 struct rtpPayloadType payloadType;
401 {{1, AST_FORMAT_G723_1}, "audio", "G723"},
402 {{1, AST_FORMAT_GSM}, "audio", "GSM"},
403 {{1, AST_FORMAT_ULAW}, "audio", "PCMU"},
404 {{1, AST_FORMAT_ALAW}, "audio", "PCMA"},
405 {{1, AST_FORMAT_MP3}, "audio", "MPA"},
406 {{1, AST_FORMAT_ADPCM}, "audio", "DVI4"},
407 {{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
408 {{1, AST_FORMAT_LPC10}, "audio", "LPC"},
409 {{1, AST_FORMAT_G729A}, "audio", "G729"},
410 {{1, AST_FORMAT_SPEEX}, "audio", "SPEEX"},
411 {{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
412 {{0, AST_RTP_DTMF}, "audio", "telephone-event"},
413 {{0, AST_RTP_CISCO_DTMF}, "audio", "bastard-telephone-event"},
414 {{0, AST_RTP_CN}, "audio", "CN"},
415 {{1, AST_FORMAT_JPEG}, "video", "JPEG"},
416 {{1, AST_FORMAT_PNG}, "video", "PNG"},
417 {{1, AST_FORMAT_H261}, "video", "H261"},
418 {{1, AST_FORMAT_H263}, "video", "H263"},
421 /* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
422 also, our own choices for dynamic payload types. This is our master
423 table for transmission */
424 static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
425 [0] = {1, AST_FORMAT_ULAW},
426 [3] = {1, AST_FORMAT_GSM},
427 [4] = {1, AST_FORMAT_G723_1},
428 [5] = {1, AST_FORMAT_ADPCM}, // 8 kHz
429 [6] = {1, AST_FORMAT_ADPCM}, // 16 kHz
430 [7] = {1, AST_FORMAT_LPC10},
431 [8] = {1, AST_FORMAT_ALAW},
432 [10] = {1, AST_FORMAT_SLINEAR}, // 2 channels
433 [11] = {1, AST_FORMAT_SLINEAR}, // 1 channel
434 [13] = {0, AST_RTP_CN},
435 [14] = {1, AST_FORMAT_MP3},
436 [16] = {1, AST_FORMAT_ADPCM}, // 11.025 kHz
437 [17] = {1, AST_FORMAT_ADPCM}, // 22.050 kHz
438 [18] = {1, AST_FORMAT_G729A},
439 [26] = {1, AST_FORMAT_JPEG},
440 [31] = {1, AST_FORMAT_H261},
441 [34] = {1, AST_FORMAT_H263},
442 [97] = {1, AST_FORMAT_ILBC},
443 [101] = {0, AST_RTP_DTMF},
444 [110] = {1, AST_FORMAT_SPEEX},
445 [121] = {0, AST_RTP_CISCO_DTMF}, // Must be type 121
448 void ast_rtp_pt_clear(struct ast_rtp* rtp)
452 for (i = 0; i < MAX_RTP_PT; ++i) {
453 rtp->current_RTP_PT[i].isAstFormat = 0;
454 rtp->current_RTP_PT[i].code = 0;
457 rtp->rtp_lookup_code_cache_isAstFormat = 0;
458 rtp->rtp_lookup_code_cache_code = 0;
459 rtp->rtp_lookup_code_cache_result = 0;
462 void ast_rtp_pt_default(struct ast_rtp* rtp)
465 /* Initialize to default payload types */
466 for (i = 0; i < MAX_RTP_PT; ++i) {
467 rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
468 rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
471 rtp->rtp_lookup_code_cache_isAstFormat = 0;
472 rtp->rtp_lookup_code_cache_code = 0;
473 rtp->rtp_lookup_code_cache_result = 0;
476 // Make a note of a RTP payload type that was seen in a SDP "m=" line.
477 // By default, use the well-known value for this type (although it may
478 // still be set to a different value by a subsequent "a=rtpmap:" line):
479 void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt) {
480 if (pt < 0 || pt > MAX_RTP_PT) return; // bogus payload type
482 if (static_RTP_PT[pt].code != 0) {
483 rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
487 // Make a note of a RTP payload type (with MIME type) that was seen in
488 // a SDP "a=rtpmap:" line.
489 void ast_rtp_set_rtpmap_type(struct ast_rtp* rtp, int pt,
490 char* mimeType, char* mimeSubtype) {
493 if (pt < 0 || pt > MAX_RTP_PT) return; // bogus payload type
495 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
496 if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
497 strcasecmp(mimeType, mimeTypes[i].type) == 0) {
498 rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
504 // Return the union of all of the codecs that were set by rtp_set...() calls
505 // They're returned as two distinct sets: AST_FORMATs, and AST_RTPs
506 void ast_rtp_get_current_formats(struct ast_rtp* rtp,
507 int* astFormats, int* nonAstFormats) {
510 *astFormats = *nonAstFormats = 0;
511 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
512 if (rtp->current_RTP_PT[pt].isAstFormat) {
513 *astFormats |= rtp->current_RTP_PT[pt].code;
515 *nonAstFormats |= rtp->current_RTP_PT[pt].code;
520 struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt) {
521 if (pt < 0 || pt > MAX_RTP_PT) {
522 struct rtpPayloadType result;
523 result.isAstFormat = result.code = 0;
524 return result; // bogus payload type
526 /* Gotta use our static one, since that's what we sent against */
527 return static_RTP_PT[pt];
530 int ast_rtp_lookup_code(struct ast_rtp* rtp, int isAstFormat, int code) {
533 /* Looks up an RTP code out of our *static* outbound list */
535 if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
536 code == rtp->rtp_lookup_code_cache_code) {
537 // Use our cached mapping, to avoid the overhead of the loop below
538 return rtp->rtp_lookup_code_cache_result;
541 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
542 if (static_RTP_PT[pt].code == code &&
543 static_RTP_PT[pt].isAstFormat == isAstFormat) {
544 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
545 rtp->rtp_lookup_code_cache_code = code;
546 rtp->rtp_lookup_code_cache_result = pt;
553 char* ast_rtp_lookup_mime_subtype(int isAstFormat, int code) {
556 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
557 if (mimeTypes[i].payloadType.code == code &&
558 mimeTypes[i].payloadType.isAstFormat == isAstFormat) {
559 return mimeTypes[i].subtype;
565 struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io)
570 rtp = malloc(sizeof(struct ast_rtp));
573 memset(rtp, 0, sizeof(struct ast_rtp));
574 rtp->them.sin_family = AF_INET;
575 rtp->us.sin_family = AF_INET;
576 rtp->s = socket(AF_INET, SOCK_DGRAM, 0);
578 rtp->seqno = rand() & 0xffff;
581 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
584 flags = fcntl(rtp->s, F_GETFL);
585 fcntl(rtp->s, F_SETFL, flags | O_NONBLOCK);
587 /* Find us a place */
588 x = (rand() % (65000-1025)) + 1025;
589 /* Must be an even port number by RTP spec */
591 rtp->us.sin_port = htons(x);
592 if (!bind(rtp->s, &rtp->us, sizeof(rtp->us)))
594 if (errno != EADDRINUSE) {
595 ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
602 /* Operate this one in a callback mode */
605 rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
607 ast_rtp_pt_default(rtp);
611 int ast_rtp_settos(struct ast_rtp *rtp, int tos)
614 if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
615 ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
619 void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
621 rtp->them.sin_port = them->sin_port;
622 rtp->them.sin_addr = them->sin_addr;
625 void ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
627 them->sin_family = AF_INET;
628 them->sin_port = rtp->them.sin_port;
629 them->sin_addr = rtp->them.sin_addr;
632 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
634 memcpy(us, &rtp->us, sizeof(rtp->us));
637 void ast_rtp_stop(struct ast_rtp *rtp)
639 memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
640 memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
643 void ast_rtp_destroy(struct ast_rtp *rtp)
646 ast_smoother_free(rtp->smoother);
648 ast_io_remove(rtp->io, rtp->ioid);
654 static unsigned int calc_txstamp(struct ast_rtp *rtp)
658 if (!rtp->txcore.tv_sec && !rtp->txcore.tv_usec) {
659 gettimeofday(&rtp->txcore, NULL);
661 gettimeofday(&now, NULL);
662 ms = (now.tv_sec - rtp->txcore.tv_sec) * 1000;
663 ms += (now.tv_usec - rtp->txcore.tv_usec) / 1000;
664 /* Use what we just got for next time */
665 rtp->txcore.tv_sec = now.tv_sec;
666 rtp->txcore.tv_usec = now.tv_usec;
670 int ast_rtp_senddigit(struct ast_rtp *rtp, char digit)
672 unsigned int *rtpheader;
680 if ((digit <= '9') && (digit >= '0'))
682 else if (digit == '*')
684 else if (digit == '#')
686 else if ((digit >= 'A') && (digit <= 'D'))
687 digit = digit - 'A' + 12;
688 else if ((digit >= 'a') && (digit <= 'd'))
689 digit = digit - 'a' + 12;
691 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
696 /* If we have no peer, return immediately */
697 if (!rtp->them.sin_addr.s_addr)
700 ms = calc_txstamp(rtp);
701 /* Default prediction */
702 pred = rtp->lastts + ms * 8;
704 /* Get a pointer to the header */
705 rtpheader = (unsigned int *)data;
706 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (101 << 16) | (rtp->seqno++));
707 rtpheader[1] = htonl(rtp->lastts);
708 rtpheader[2] = htonl(rtp->ssrc);
709 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (0));
711 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
712 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 4, 0, &rtp->them, sizeof(rtp->them));
714 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));
716 printf("Sent %d bytes of RTP data to %s:%d\n", res, inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
720 /* Clear marker bit and increment seqno */
721 rtpheader[0] = htonl((2 << 30) | (101 << 16) | (rtp->seqno++));
722 /* Make duration 240 */
723 rtpheader[3] |= htonl((240));
724 /* Set the End bit for the last 3 */
725 rtpheader[3] |= htonl((1 << 23));
731 static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec)
733 unsigned int *rtpheader;
739 ms = calc_txstamp(rtp);
740 /* Default prediction */
741 pred = rtp->lastts + ms * 8;
743 switch(f->subclass) {
744 case AST_FORMAT_ULAW:
745 case AST_FORMAT_ALAW:
746 /* If we're within +/- 20ms from when where we
747 predict we should be, use that */
748 pred = rtp->lastts + f->datalen;
750 case AST_FORMAT_G729A:
751 pred = rtp->lastts + f->datalen * 8;
754 pred = rtp->lastts + (f->datalen * 160 / 33);
756 case AST_FORMAT_ILBC:
757 pred = rtp->lastts + (f->datalen * 240 / 50);
759 case AST_FORMAT_G723_1:
760 pred = rtp->lastts + g723_samples(f->data, f->datalen);
762 case AST_FORMAT_SPEEX:
763 pred = rtp->lastts + 160;
764 // assumes that the RTP packet contains one Speex frame
767 ast_log(LOG_WARNING, "Not sure about timestamp format for codec format %d\n", f->subclass);
770 /* Re-calculate last TS */
771 rtp->lastts = rtp->lastts + ms * 8;
772 /* If it's close to ou prediction, go for it */
773 if (abs(rtp->lastts - pred) < 640)
776 ast_log(LOG_DEBUG, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
777 /* Get a pointer to the header */
778 rtpheader = (unsigned int *)(f->data - hdrlen);
779 rtpheader[0] = htonl((2 << 30) | (codec << 16) | (rtp->seqno++));
780 rtpheader[1] = htonl(rtp->lastts);
781 rtpheader[2] = htonl(rtp->ssrc);
782 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
783 res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, &rtp->them, sizeof(rtp->them));
785 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));
787 printf("Sent %d bytes of RTP data to %s:%d\n", res, inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
793 int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *_f)
800 /* If we have no peer, return immediately */
801 if (!rtp->them.sin_addr.s_addr)
804 /* If there is no data length, return immediately */
808 /* Make sure we have enough space for RTP header */
809 if (_f->frametype != AST_FRAME_VOICE) {
810 ast_log(LOG_WARNING, "RTP can only send voice\n");
815 codec = ast_rtp_lookup_code(rtp, 1, _f->subclass);
817 ast_log(LOG_WARNING, "Don't know how to send format %d packets with RTP\n", _f->subclass);
821 if (rtp->lasttxformat != _f->subclass) {
822 /* New format, reset the smoother */
823 ast_log(LOG_DEBUG, "Ooh, format changed from %d to %d\n", rtp->lasttxformat, _f->subclass);
824 rtp->lasttxformat = _f->subclass;
826 ast_smoother_free(rtp->smoother);
827 rtp->smoother = NULL;
831 switch(_f->subclass) {
832 case AST_FORMAT_ULAW:
833 case AST_FORMAT_ALAW:
834 if (!rtp->smoother) {
835 rtp->smoother = ast_smoother_new(160);
837 if (!rtp->smoother) {
838 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
841 ast_smoother_feed(rtp->smoother, _f);
843 while((f = ast_smoother_read(rtp->smoother)))
844 ast_rtp_raw_write(rtp, f, codec);
846 case AST_FORMAT_G729A:
847 if (!rtp->smoother) {
848 rtp->smoother = ast_smoother_new(20);
850 if (!rtp->smoother) {
851 ast_log(LOG_WARNING, "Unable to create g729 smoother :(\n");
854 ast_smoother_feed(rtp->smoother, _f);
856 while((f = ast_smoother_read(rtp->smoother)))
857 ast_rtp_raw_write(rtp, f, codec);
860 if (!rtp->smoother) {
861 rtp->smoother = ast_smoother_new(33);
863 if (!rtp->smoother) {
864 ast_log(LOG_WARNING, "Unable to create GSM smoother :(\n");
867 ast_smoother_feed(rtp->smoother, _f);
868 while((f = ast_smoother_read(rtp->smoother)))
869 ast_rtp_raw_write(rtp, f, codec);
871 case AST_FORMAT_ILBC:
872 if (!rtp->smoother) {
873 rtp->smoother = ast_smoother_new(50);
875 if (!rtp->smoother) {
876 ast_log(LOG_WARNING, "Unable to create ILBC smoother :(\n");
879 ast_smoother_feed(rtp->smoother, _f);
880 while((f = ast_smoother_read(rtp->smoother)))
881 ast_rtp_raw_write(rtp, f, codec);
884 ast_log(LOG_WARNING, "Not sure about sending format %d packets\n", _f->subclass);
885 // fall through to...
886 case AST_FORMAT_SPEEX:
887 // Don't buffer outgoing frames; send them one-per-packet:
888 if (_f->offset < hdrlen) {
893 ast_rtp_raw_write(rtp, f, codec);
899 void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto)
901 struct ast_rtp_protocol *cur, *prev;
907 prev->next = proto->next;
909 protos = proto->next;
917 int ast_rtp_proto_register(struct ast_rtp_protocol *proto)
919 struct ast_rtp_protocol *cur;
922 if (cur->type == proto->type) {
923 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
928 proto->next = protos;
933 static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
935 struct ast_rtp_protocol *cur;
938 if (cur->type == chan->type) {
946 int ast_rtp_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
949 struct ast_channel *who, *cs[3];
950 struct ast_rtp *p0, *p1;
951 struct ast_rtp_protocol *pr0, *pr1;
952 struct sockaddr_in ac0, ac1;
953 struct sockaddr_in t0, t1;
958 /* XXX Wait a half a second for things to settle up
959 this really should be fixed XXX */
960 ast_autoservice_start(c0);
961 ast_autoservice_start(c1);
963 ast_autoservice_stop(c0);
964 ast_autoservice_stop(c1);
966 /* if need DTMF, cant native bridge */
967 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
969 ast_pthread_mutex_lock(&c0->lock);
970 ast_pthread_mutex_lock(&c1->lock);
974 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
975 ast_pthread_mutex_unlock(&c0->lock);
976 ast_pthread_mutex_unlock(&c1->lock);
980 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
981 ast_pthread_mutex_unlock(&c0->lock);
982 ast_pthread_mutex_unlock(&c1->lock);
987 p0 = pr0->get_rtp_info(c0);
988 p1 = pr1->get_rtp_info(c1);
990 /* Somebody doesn't want to play... */
991 ast_pthread_mutex_unlock(&c0->lock);
992 ast_pthread_mutex_unlock(&c1->lock);
995 if (pr0->set_rtp_peer(c0, p1))
996 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
999 ast_rtp_get_peer(p1, &ac1);
1001 if (pr1->set_rtp_peer(c1, p0))
1002 ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
1004 /* Store RTP peer */
1005 ast_rtp_get_peer(p0, &ac0);
1007 ast_pthread_mutex_unlock(&c0->lock);
1008 ast_pthread_mutex_unlock(&c1->lock);
1013 if ((c0->pvt->pvt != pvt0) ||
1014 (c1->pvt->pvt != pvt1) ||
1015 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
1016 ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
1017 if (c0->pvt->pvt == pvt0) {
1018 if (pr0->set_rtp_peer(c0, NULL))
1019 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1021 if (c1->pvt->pvt == pvt1) {
1022 if (pr1->set_rtp_peer(c1, NULL))
1023 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1025 /* Tell it to try again later */
1029 ast_rtp_get_peer(p1, &t1);
1030 ast_rtp_get_peer(p0, &t0);
1031 if (inaddrcmp(&t1, &ac1)) {
1032 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address\n", c1->name);
1033 if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL))
1034 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
1035 memcpy(&ac1, &t1, sizeof(ac1));
1037 if (inaddrcmp(&t0, &ac0)) {
1038 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address\n", c0->name);
1039 if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL))
1040 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
1041 memcpy(&ac0, &t0, sizeof(ac0));
1043 who = ast_waitfor_n(cs, 2, &to);
1045 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
1049 if (!f || ((f->frametype == AST_FRAME_DTMF) &&
1050 (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1051 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1054 ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
1055 if ((c0->pvt->pvt == pvt0) && (!c0->_softhangup)) {
1056 if (pr0->set_rtp_peer(c0, NULL))
1057 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1059 if ((c1->pvt->pvt == pvt1) && (!c1->_softhangup)) {
1060 if (pr1->set_rtp_peer(c1, NULL))
1061 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1063 /* That's all we needed */
1066 if ((f->frametype == AST_FRAME_DTMF) || (f->frametype == AST_FRAME_VOICE)) {
1067 /* Forward voice or DTMF frames if they happen upon us */
1070 } else if (who == c1) {
1076 /* Swap priority not that it's a big deal at this point */