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_rfc2833(struct ast_rtp *rtp, unsigned char *data, int len)
158 struct ast_frame *f = NULL;
159 event = ntohl(*((unsigned int *)(data)));
162 printf("Event: %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_rfc3389(struct ast_rtp *rtp, unsigned char *data, int len)
183 struct ast_frame *f = NULL;
184 /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
185 totally help us out becuase we don't have an engine to keep it going and we are not
186 guaranteed to have it every 20ms or anything */
188 printf("RFC3389: %d bytes, format is %d\n", len, rtp->lastrxformat);
190 ast_log(LOG_NOTICE, "RFC3389 support incomplete. Turn off on client if possible\n");
191 if (!rtp->lastrxformat)
193 switch(rtp->lastrxformat) {
194 case AST_FORMAT_ULAW:
195 rtp->f.frametype = AST_FRAME_VOICE;
196 rtp->f.subclass = AST_FORMAT_ULAW;
197 rtp->f.datalen = 160;
198 rtp->f.samples = 160;
199 memset(rtp->f.data, 0x7f, rtp->f.datalen);
202 case AST_FORMAT_ALAW:
203 rtp->f.frametype = AST_FRAME_VOICE;
204 rtp->f.subclass = AST_FORMAT_ALAW;
205 rtp->f.datalen = 160;
206 rtp->f.samples = 160;
207 memset(rtp->f.data, 0x7e, rtp->f.datalen); /* XXX Is this right? XXX */
210 case AST_FORMAT_SLINEAR:
211 rtp->f.frametype = AST_FRAME_VOICE;
212 rtp->f.subclass = AST_FORMAT_SLINEAR;
213 rtp->f.datalen = 320;
214 rtp->f.samples = 160;
215 memset(rtp->f.data, 0x00, rtp->f.datalen);
219 ast_log(LOG_NOTICE, "Don't know how to handle RFC3389 for receive codec %d\n", rtp->lastrxformat);
224 static int rtpread(int *id, int fd, short events, void *cbdata)
226 struct ast_rtp *rtp = cbdata;
228 f = ast_rtp_read(rtp);
231 rtp->callback(rtp, f, rtp->data);
236 struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
239 struct sockaddr_in sin;
244 unsigned int timestamp;
245 unsigned int *rtpheader;
246 static struct ast_frame *f, null_frame = { AST_FRAME_NULL, };
247 struct rtpPayloadType rtpPT;
251 res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
252 0, (struct sockaddr *)&sin, &len);
255 rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
257 ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
263 ast_log(LOG_WARNING, "RTP Read too short\n");
267 /* Send to whoever sent to us */
268 if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
269 (rtp->them.sin_port != sin.sin_port)) {
270 memcpy(&rtp->them, &sin, sizeof(rtp->them));
271 ast_log(LOG_DEBUG, "RTP NAT: Using address %s:%d\n", inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
275 seqno = ntohl(rtpheader[0]);
276 payloadtype = (seqno & 0x7f0000) >> 16;
278 timestamp = ntohl(rtpheader[1]);
280 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);
282 rtp->f.frametype = AST_FRAME_VOICE;
283 rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
284 if (!rtpPT.isAstFormat) {
285 // This is special in-band data that's not one of our codecs
286 if (rtpPT.code == AST_RTP_DTMF) {
287 /* It's special -- rfc2833 process it */
288 f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
289 if (f) return f; else return &null_frame;
290 } else if (rtpPT.code == AST_RTP_CN) {
292 f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
293 if (f) return f; else return &null_frame;
295 ast_log(LOG_NOTICE, "Unknown RTP codec %d received\n", payloadtype);
299 rtp->f.subclass = rtpPT.code;
300 rtp->lastrxformat = rtp->f.subclass;
303 rtp->lastrxts = timestamp;
305 if (rtp->dtmfcount) {
307 printf("dtmfcount was %d\n", rtp->dtmfcount);
309 rtp->dtmfcount -= (timestamp - rtp->lastrxts);
310 if (rtp->dtmfcount < 0)
313 if (dtmftimeout != rtp->dtmfcount)
314 printf("dtmfcount is %d\n", rtp->dtmfcount);
317 rtp->lastrxts = timestamp;
319 /* Send any pending DTMF */
320 if (rtp->resp && !rtp->dtmfcount) {
321 ast_log(LOG_DEBUG, "Sending pending DTMF\n");
322 return send_dtmf(rtp);
325 rtp->f.datalen = res - hdrlen;
326 rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
327 rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
328 switch(rtp->f.subclass) {
329 case AST_FORMAT_ULAW:
330 case AST_FORMAT_ALAW:
331 rtp->f.samples = rtp->f.datalen;
333 case AST_FORMAT_SLINEAR:
334 rtp->f.samples = rtp->f.datalen / 2;
337 rtp->f.samples = 160 * (rtp->f.datalen / 33);
339 case AST_FORMAT_ILBC:
340 rtp->f.samples = 240 * (rtp->f.datalen / 50);
342 case AST_FORMAT_ADPCM:
343 rtp->f.samples = rtp->f.datalen * 2;
345 case AST_FORMAT_G729A:
346 rtp->f.samples = rtp->f.datalen * 8;
348 case AST_FORMAT_G723_1:
349 rtp->f.samples = g723_samples(rtp->f.data, rtp->f.datalen);
351 case AST_FORMAT_SPEEX:
352 rtp->f.samples = 160;
353 // assumes that the RTP packet contained one Speex frame
356 ast_log(LOG_NOTICE, "Unable to calculate samples for format %d\n", rtp->f.subclass);
363 // The following array defines the MIME type (and subtype) for each
364 // of our codecs, or RTP-specific data type.
366 struct rtpPayloadType payloadType;
370 {{1, AST_FORMAT_G723_1}, "audio", "G723"},
371 {{1, AST_FORMAT_GSM}, "audio", "GSM"},
372 {{1, AST_FORMAT_ULAW}, "audio", "PCMU"},
373 {{1, AST_FORMAT_ALAW}, "audio", "PCMA"},
374 {{1, AST_FORMAT_MP3}, "audio", "MPA"},
375 {{1, AST_FORMAT_ADPCM}, "audio", "DVI4"},
376 {{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
377 {{1, AST_FORMAT_LPC10}, "audio", "LPC"},
378 {{1, AST_FORMAT_G729A}, "audio", "G729"},
379 {{1, AST_FORMAT_SPEEX}, "audio", "SPEEX"},
380 {{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
381 {{0, AST_RTP_DTMF}, "audio", "telephone-event"},
382 {{0, AST_RTP_CN}, "audio", "CN"},
383 {{1, AST_FORMAT_JPEG}, "video", "JPEG"},
384 {{1, AST_FORMAT_PNG}, "video", "PNG"},
385 {{1, AST_FORMAT_H261}, "video", "H261"},
386 {{1, AST_FORMAT_H263}, "video", "H263"},
389 /* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
390 also, our own choices for dynamic payload types. This is our master
391 table for transmission */
392 static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
393 [0] = {1, AST_FORMAT_ULAW},
394 [3] = {1, AST_FORMAT_GSM},
395 [4] = {1, AST_FORMAT_G723_1},
396 [5] = {1, AST_FORMAT_ADPCM}, // 8 kHz
397 [6] = {1, AST_FORMAT_ADPCM}, // 16 kHz
398 [7] = {1, AST_FORMAT_LPC10},
399 [8] = {1, AST_FORMAT_ALAW},
400 [10] = {1, AST_FORMAT_SLINEAR}, // 2 channels
401 [11] = {1, AST_FORMAT_SLINEAR}, // 1 channel
402 [13] = {0, AST_RTP_CN},
403 [14] = {1, AST_FORMAT_MP3},
404 [16] = {1, AST_FORMAT_ADPCM}, // 11.025 kHz
405 [17] = {1, AST_FORMAT_ADPCM}, // 22.050 kHz
406 [18] = {1, AST_FORMAT_G729A},
407 [26] = {1, AST_FORMAT_JPEG},
408 [31] = {1, AST_FORMAT_H261},
409 [34] = {1, AST_FORMAT_H263},
410 [97] = {1, AST_FORMAT_ILBC},
411 [101] = {0, AST_RTP_DTMF},
412 [110] = {1, AST_FORMAT_SPEEX},
415 void ast_rtp_pt_clear(struct ast_rtp* rtp)
419 for (i = 0; i < MAX_RTP_PT; ++i) {
420 rtp->current_RTP_PT[i].isAstFormat = 0;
421 rtp->current_RTP_PT[i].code = 0;
424 rtp->rtp_lookup_code_cache_isAstFormat = 0;
425 rtp->rtp_lookup_code_cache_code = 0;
426 rtp->rtp_lookup_code_cache_result = 0;
429 void ast_rtp_pt_default(struct ast_rtp* rtp)
432 /* Initialize to default payload types */
433 for (i = 0; i < MAX_RTP_PT; ++i) {
434 rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
435 rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
438 rtp->rtp_lookup_code_cache_isAstFormat = 0;
439 rtp->rtp_lookup_code_cache_code = 0;
440 rtp->rtp_lookup_code_cache_result = 0;
443 // Make a note of a RTP payload type that was seen in a SDP "m=" line.
444 // By default, use the well-known value for this type (although it may
445 // still be set to a different value by a subsequent "a=rtpmap:" line):
446 void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt) {
447 if (pt < 0 || pt > MAX_RTP_PT) return; // bogus payload type
449 if (static_RTP_PT[pt].code != 0) {
450 rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
454 // Make a note of a RTP payload type (with MIME type) that was seen in
455 // a SDP "a=rtpmap:" line.
456 void ast_rtp_set_rtpmap_type(struct ast_rtp* rtp, int pt,
457 char* mimeType, char* mimeSubtype) {
460 if (pt < 0 || pt > MAX_RTP_PT) return; // bogus payload type
462 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
463 if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
464 strcasecmp(mimeType, mimeTypes[i].type) == 0) {
465 rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
471 // Return the union of all of the codecs that were set by rtp_set...() calls
472 // They're returned as two distinct sets: AST_FORMATs, and AST_RTPs
473 void ast_rtp_get_current_formats(struct ast_rtp* rtp,
474 int* astFormats, int* nonAstFormats) {
477 *astFormats = *nonAstFormats = 0;
478 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
479 if (rtp->current_RTP_PT[pt].isAstFormat) {
480 *astFormats |= rtp->current_RTP_PT[pt].code;
482 *nonAstFormats |= rtp->current_RTP_PT[pt].code;
487 struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt) {
488 if (pt < 0 || pt > MAX_RTP_PT) {
489 struct rtpPayloadType result;
490 result.isAstFormat = result.code = 0;
491 return result; // bogus payload type
493 /* Gotta use our static one, since that's what we sent against */
494 return static_RTP_PT[pt];
497 int ast_rtp_lookup_code(struct ast_rtp* rtp, int isAstFormat, int code) {
500 /* Looks up an RTP code out of our *static* outbound list */
502 if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
503 code == rtp->rtp_lookup_code_cache_code) {
504 // Use our cached mapping, to avoid the overhead of the loop below
505 return rtp->rtp_lookup_code_cache_result;
508 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
509 if (static_RTP_PT[pt].code == code &&
510 static_RTP_PT[pt].isAstFormat == isAstFormat) {
511 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
512 rtp->rtp_lookup_code_cache_code = code;
513 rtp->rtp_lookup_code_cache_result = pt;
520 char* ast_rtp_lookup_mime_subtype(int isAstFormat, int code) {
523 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
524 if (mimeTypes[i].payloadType.code == code &&
525 mimeTypes[i].payloadType.isAstFormat == isAstFormat) {
526 return mimeTypes[i].subtype;
532 struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io)
537 rtp = malloc(sizeof(struct ast_rtp));
540 memset(rtp, 0, sizeof(struct ast_rtp));
541 rtp->them.sin_family = AF_INET;
542 rtp->us.sin_family = AF_INET;
543 rtp->s = socket(AF_INET, SOCK_DGRAM, 0);
545 rtp->seqno = rand() & 0xffff;
548 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
551 flags = fcntl(rtp->s, F_GETFL);
552 fcntl(rtp->s, F_SETFL, flags | O_NONBLOCK);
554 /* Find us a place */
555 x = (rand() % (65000-1025)) + 1025;
556 /* Must be an even port number by RTP spec */
558 rtp->us.sin_port = htons(x);
559 if (!bind(rtp->s, &rtp->us, sizeof(rtp->us)))
561 if (errno != EADDRINUSE) {
562 ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
569 /* Operate this one in a callback mode */
572 rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
574 ast_rtp_pt_default(rtp);
578 int ast_rtp_settos(struct ast_rtp *rtp, int tos)
581 if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
582 ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
586 void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
588 rtp->them.sin_port = them->sin_port;
589 rtp->them.sin_addr = them->sin_addr;
592 void ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
594 them->sin_family = AF_INET;
595 them->sin_port = rtp->them.sin_port;
596 them->sin_addr = rtp->them.sin_addr;
599 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
601 memcpy(us, &rtp->us, sizeof(rtp->us));
604 void ast_rtp_stop(struct ast_rtp *rtp)
606 memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
607 memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
610 void ast_rtp_destroy(struct ast_rtp *rtp)
613 ast_smoother_free(rtp->smoother);
615 ast_io_remove(rtp->io, rtp->ioid);
621 static unsigned int calc_txstamp(struct ast_rtp *rtp)
625 if (!rtp->txcore.tv_sec && !rtp->txcore.tv_usec) {
626 gettimeofday(&rtp->txcore, NULL);
628 gettimeofday(&now, NULL);
629 ms = (now.tv_sec - rtp->txcore.tv_sec) * 1000;
630 ms += (now.tv_usec - rtp->txcore.tv_usec) / 1000;
631 /* Use what we just got for next time */
632 rtp->txcore.tv_sec = now.tv_sec;
633 rtp->txcore.tv_usec = now.tv_usec;
637 int ast_rtp_senddigit(struct ast_rtp *rtp, char digit)
639 unsigned int *rtpheader;
647 if ((digit <= '9') && (digit >= '0'))
649 else if (digit == '*')
651 else if (digit == '#')
653 else if ((digit >= 'A') && (digit <= 'D'))
654 digit = digit - 'A' + 12;
655 else if ((digit >= 'a') && (digit <= 'd'))
656 digit = digit - 'a' + 12;
658 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
663 /* If we have no peer, return immediately */
664 if (!rtp->them.sin_addr.s_addr)
667 ms = calc_txstamp(rtp);
668 /* Default prediction */
669 pred = rtp->lastts + ms * 8;
671 /* Get a pointer to the header */
672 rtpheader = (unsigned int *)data;
673 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (101 << 16) | (rtp->seqno++));
674 rtpheader[1] = htonl(rtp->lastts);
675 rtpheader[2] = htonl(rtp->ssrc);
676 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (0));
678 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
679 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 4, 0, &rtp->them, sizeof(rtp->them));
681 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));
683 printf("Sent %d bytes of RTP data to %s:%d\n", res, inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
687 /* Clear marker bit and increment seqno */
688 rtpheader[0] = htonl((2 << 30) | (101 << 16) | (rtp->seqno++));
689 /* Make duration 240 */
690 rtpheader[3] |= htonl((240));
691 /* Set the End bit for the last 3 */
692 rtpheader[3] |= htonl((1 << 23));
698 static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec)
700 unsigned int *rtpheader;
706 ms = calc_txstamp(rtp);
707 /* Default prediction */
708 pred = rtp->lastts + ms * 8;
710 switch(f->subclass) {
711 case AST_FORMAT_ULAW:
712 case AST_FORMAT_ALAW:
713 /* If we're within +/- 20ms from when where we
714 predict we should be, use that */
715 pred = rtp->lastts + f->datalen;
717 case AST_FORMAT_G729A:
718 pred = rtp->lastts + f->datalen * 8;
721 pred = rtp->lastts + (f->datalen * 160 / 33);
723 case AST_FORMAT_ILBC:
724 pred = rtp->lastts + (f->datalen * 240 / 50);
726 case AST_FORMAT_G723_1:
727 pred = rtp->lastts + g723_samples(f->data, f->datalen);
729 case AST_FORMAT_SPEEX:
730 pred = rtp->lastts + 160;
731 // assumes that the RTP packet contains one Speex frame
734 ast_log(LOG_WARNING, "Not sure about timestamp format for codec format %d\n", f->subclass);
737 /* Re-calculate last TS */
738 rtp->lastts = rtp->lastts + ms * 8;
739 /* If it's close to ou prediction, go for it */
740 if (abs(rtp->lastts - pred) < 640)
743 ast_log(LOG_DEBUG, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
744 /* Get a pointer to the header */
745 rtpheader = (unsigned int *)(f->data - hdrlen);
746 rtpheader[0] = htonl((2 << 30) | (codec << 16) | (rtp->seqno++));
747 rtpheader[1] = htonl(rtp->lastts);
748 rtpheader[2] = htonl(rtp->ssrc);
749 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
750 res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, &rtp->them, sizeof(rtp->them));
752 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));
754 printf("Sent %d bytes of RTP data to %s:%d\n", res, inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
760 int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *_f)
767 /* If we have no peer, return immediately */
768 if (!rtp->them.sin_addr.s_addr)
771 /* If there is no data length, return immediately */
775 /* Make sure we have enough space for RTP header */
776 if (_f->frametype != AST_FRAME_VOICE) {
777 ast_log(LOG_WARNING, "RTP can only send voice\n");
782 codec = ast_rtp_lookup_code(rtp, 1, _f->subclass);
784 ast_log(LOG_WARNING, "Don't know how to send format %d packets with RTP\n", _f->subclass);
788 if (rtp->lasttxformat != _f->subclass) {
789 /* New format, reset the smoother */
790 ast_log(LOG_DEBUG, "Ooh, format changed from %d to %d\n", rtp->lasttxformat, _f->subclass);
791 rtp->lasttxformat = _f->subclass;
793 ast_smoother_free(rtp->smoother);
794 rtp->smoother = NULL;
798 switch(_f->subclass) {
799 case AST_FORMAT_ULAW:
800 case AST_FORMAT_ALAW:
801 if (!rtp->smoother) {
802 rtp->smoother = ast_smoother_new(160);
804 if (!rtp->smoother) {
805 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
808 ast_smoother_feed(rtp->smoother, _f);
810 while((f = ast_smoother_read(rtp->smoother)))
811 ast_rtp_raw_write(rtp, f, codec);
813 case AST_FORMAT_G729A:
814 if (!rtp->smoother) {
815 rtp->smoother = ast_smoother_new(20);
817 if (!rtp->smoother) {
818 ast_log(LOG_WARNING, "Unable to create g729 smoother :(\n");
821 ast_smoother_feed(rtp->smoother, _f);
823 while((f = ast_smoother_read(rtp->smoother)))
824 ast_rtp_raw_write(rtp, f, codec);
827 if (!rtp->smoother) {
828 rtp->smoother = ast_smoother_new(33);
830 if (!rtp->smoother) {
831 ast_log(LOG_WARNING, "Unable to create GSM smoother :(\n");
834 ast_smoother_feed(rtp->smoother, _f);
835 while((f = ast_smoother_read(rtp->smoother)))
836 ast_rtp_raw_write(rtp, f, codec);
838 case AST_FORMAT_ILBC:
839 if (!rtp->smoother) {
840 rtp->smoother = ast_smoother_new(50);
842 if (!rtp->smoother) {
843 ast_log(LOG_WARNING, "Unable to create ILBC smoother :(\n");
846 ast_smoother_feed(rtp->smoother, _f);
847 while((f = ast_smoother_read(rtp->smoother)))
848 ast_rtp_raw_write(rtp, f, codec);
851 ast_log(LOG_WARNING, "Not sure about sending format %d packets\n", _f->subclass);
852 // fall through to...
853 case AST_FORMAT_SPEEX:
854 // Don't buffer outgoing frames; send them one-per-packet:
855 if (_f->offset < hdrlen) {
860 ast_rtp_raw_write(rtp, f, codec);
866 void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto)
868 struct ast_rtp_protocol *cur, *prev;
874 prev->next = proto->next;
876 protos = proto->next;
884 int ast_rtp_proto_register(struct ast_rtp_protocol *proto)
886 struct ast_rtp_protocol *cur;
889 if (cur->type == proto->type) {
890 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
895 proto->next = protos;
900 static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
902 struct ast_rtp_protocol *cur;
905 if (cur->type == chan->type) {
913 int ast_rtp_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
916 struct ast_channel *who, *cs[3];
917 struct ast_rtp *p0, *p1;
918 struct ast_rtp_protocol *pr0, *pr1;
919 struct sockaddr_in ac0, ac1;
920 struct sockaddr_in t0, t1;
925 /* XXX Wait a half a second for things to settle up
926 this really should be fixed XXX */
927 ast_autoservice_start(c0);
928 ast_autoservice_start(c1);
930 ast_autoservice_stop(c0);
931 ast_autoservice_stop(c1);
933 /* if need DTMF, cant native bridge */
934 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
936 ast_pthread_mutex_lock(&c0->lock);
937 ast_pthread_mutex_lock(&c1->lock);
941 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
942 ast_pthread_mutex_unlock(&c0->lock);
943 ast_pthread_mutex_unlock(&c1->lock);
947 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
948 ast_pthread_mutex_unlock(&c0->lock);
949 ast_pthread_mutex_unlock(&c1->lock);
954 p0 = pr0->get_rtp_info(c0);
955 p1 = pr1->get_rtp_info(c1);
957 /* Somebody doesn't want to play... */
958 ast_pthread_mutex_unlock(&c0->lock);
959 ast_pthread_mutex_unlock(&c1->lock);
962 if (pr0->set_rtp_peer(c0, p1))
963 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
966 ast_rtp_get_peer(p1, &ac1);
968 if (pr1->set_rtp_peer(c1, p0))
969 ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
972 ast_rtp_get_peer(p0, &ac0);
974 ast_pthread_mutex_unlock(&c0->lock);
975 ast_pthread_mutex_unlock(&c1->lock);
980 if ((c0->pvt->pvt != pvt0) ||
981 (c1->pvt->pvt != pvt1) ||
982 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
983 ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
984 if (c0->pvt->pvt == pvt0) {
985 if (pr0->set_rtp_peer(c0, NULL))
986 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
988 if (c1->pvt->pvt == pvt1) {
989 if (pr1->set_rtp_peer(c1, NULL))
990 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
992 /* Tell it to try again later */
996 ast_rtp_get_peer(p1, &t1);
997 ast_rtp_get_peer(p0, &t0);
998 if (inaddrcmp(&t1, &ac1)) {
999 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address\n", c1->name);
1000 if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL))
1001 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
1002 memcpy(&ac1, &t1, sizeof(ac1));
1004 if (inaddrcmp(&t0, &ac0)) {
1005 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address\n", c0->name);
1006 if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL))
1007 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
1008 memcpy(&ac0, &t0, sizeof(ac0));
1010 who = ast_waitfor_n(cs, 2, &to);
1012 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
1016 if (!f || ((f->frametype == AST_FRAME_DTMF) &&
1017 (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1018 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1021 ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
1022 if ((c0->pvt->pvt == pvt0) && (!c0->_softhangup)) {
1023 if (pr0->set_rtp_peer(c0, NULL))
1024 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1026 if ((c1->pvt->pvt == pvt1) && (!c1->_softhangup)) {
1027 if (pr1->set_rtp_peer(c1, NULL))
1028 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1030 /* That's all we needed */
1033 if ((f->frametype == AST_FRAME_DTMF) || (f->frametype == AST_FRAME_VOICE)) {
1034 /* Forward voice or DTMF frames if they happen upon us */
1037 } else if (who == c1) {
1043 /* Swap priority not that it's a big deal at this point */