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.h>
35 #include <asterisk/channel_pvt.h>
36 #include <asterisk/config.h>
42 #define TYPE_SILENCE 0x2
43 #define TYPE_DONTSEND 0x3
46 static int dtmftimeout = 3000; /* 3000 samples */
48 static int rtpstart = 0;
49 static int rtpend = 0;
51 // The value of each payload format mapping:
52 struct rtpPayloadType {
53 int isAstFormat; // whether the following code is an AST_FORMAT
57 #define MAX_RTP_PT 256
59 #define FLAG_3389_WARNING (1 << 0)
65 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
68 unsigned int lastrxts;
69 unsigned int lastividtimestamp;
70 unsigned int lastovidtimestamp;
71 unsigned int lasteventseqn;
75 unsigned int dtmfduration;
78 struct sockaddr_in us;
79 struct sockaddr_in them;
80 struct timeval rxcore;
81 struct timeval txcore;
82 struct timeval dtmfmute;
83 struct ast_smoother *smoother;
86 struct sched_context *sched;
87 struct io_context *io;
89 ast_rtp_callback callback;
90 struct rtpPayloadType current_RTP_PT[MAX_RTP_PT];
91 // a cache for the result of rtp_lookup_code():
92 int rtp_lookup_code_cache_isAstFormat;
93 int rtp_lookup_code_cache_code;
94 int rtp_lookup_code_cache_result;
95 struct ast_rtcp *rtcp;
100 struct sockaddr_in us;
101 struct sockaddr_in them;
104 static struct ast_rtp_protocol *protos = NULL;
106 int ast_rtp_fd(struct ast_rtp *rtp)
111 int ast_rtcp_fd(struct ast_rtp *rtp)
118 static int g723_len(unsigned char buf)
120 switch(buf & TYPE_MASK) {
134 ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", buf & TYPE_MASK);
139 static int g723_samples(unsigned char *buf, int maxlen)
144 while(pos < maxlen) {
145 res = g723_len(buf[pos]);
154 void ast_rtp_set_data(struct ast_rtp *rtp, void *data)
159 void ast_rtp_set_callback(struct ast_rtp *rtp, ast_rtp_callback callback)
161 rtp->callback = callback;
164 void ast_rtp_setnat(struct ast_rtp *rtp, int nat)
169 static struct ast_frame *send_dtmf(struct ast_rtp *rtp)
172 static struct ast_frame null_frame = { AST_FRAME_NULL, };
173 gettimeofday(&tv, NULL);
174 if ((tv.tv_sec < rtp->dtmfmute.tv_sec) ||
175 ((tv.tv_sec == rtp->dtmfmute.tv_sec) && (tv.tv_usec < rtp->dtmfmute.tv_usec))) {
176 ast_log(LOG_DEBUG, "Ignore potential DTMF echo from '%s'\n", inet_ntoa(rtp->them.sin_addr));
178 rtp->dtmfduration = 0;
181 ast_log(LOG_DEBUG, "Sending dtmf: %d (%c), at %s\n", rtp->resp, rtp->resp, inet_ntoa(rtp->them.sin_addr));
182 rtp->f.frametype = AST_FRAME_DTMF;
183 rtp->f.subclass = rtp->resp;
189 rtp->dtmfduration = 0;
194 static struct ast_frame *process_cisco_dtmf(struct ast_rtp *rtp, unsigned char *data, int len)
198 struct ast_frame *f = NULL;
199 event = ntohl(*((unsigned int *)(data)));
202 printf("Cisco Digit: %08x (len = %d)\n", event, len);
206 } else if (event < 11) {
208 } else if (event < 12) {
210 } else if (event < 16) {
211 resp = 'A' + (event - 12);
213 if (rtp->resp && (rtp->resp != resp)) {
217 rtp->dtmfcount = dtmftimeout;
221 static struct ast_frame *process_rfc2833(struct ast_rtp *rtp, unsigned char *data, int len)
224 unsigned int event_end;
225 unsigned int duration;
227 struct ast_frame *f = NULL;
228 event = ntohl(*((unsigned int *)(data)));
230 event_end = ntohl(*((unsigned int *)(data)));
233 duration = ntohl(*((unsigned int *)(data)));
236 printf("Event: %08x (len = %d)\n", event, len);
240 } else if (event < 11) {
242 } else if (event < 12) {
244 } else if (event < 16) {
245 resp = 'A' + (event - 12);
247 if (rtp->resp && (rtp->resp != resp)) {
250 else if(event_end & 0x80)
259 else if(rtp->dtmfduration && (duration < rtp->dtmfduration))
263 if (!(event_end & 0x80))
265 rtp->dtmfcount = dtmftimeout;
266 rtp->dtmfduration = duration;
270 static struct ast_frame *process_rfc3389(struct ast_rtp *rtp, unsigned char *data, int len)
272 struct ast_frame *f = NULL;
273 /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
274 totally help us out becuase we don't have an engine to keep it going and we are not
275 guaranteed to have it every 20ms or anything */
277 printf("RFC3389: %d bytes, format is %d\n", len, rtp->lastrxformat);
279 if (!(rtp->flags & FLAG_3389_WARNING)) {
280 ast_log(LOG_NOTICE, "RFC3389 support incomplete. Turn off on client if possible\n");
281 rtp->flags |= FLAG_3389_WARNING;
283 if (!rtp->lastrxformat)
285 switch(rtp->lastrxformat) {
286 case AST_FORMAT_ULAW:
287 rtp->f.frametype = AST_FRAME_VOICE;
288 rtp->f.subclass = AST_FORMAT_ULAW;
289 rtp->f.datalen = 160;
290 rtp->f.samples = 160;
291 memset(rtp->f.data, 0x7f, rtp->f.datalen);
294 case AST_FORMAT_ALAW:
295 rtp->f.frametype = AST_FRAME_VOICE;
296 rtp->f.subclass = AST_FORMAT_ALAW;
297 rtp->f.datalen = 160;
298 rtp->f.samples = 160;
299 memset(rtp->f.data, 0x7e, rtp->f.datalen); /* XXX Is this right? XXX */
302 case AST_FORMAT_SLINEAR:
303 rtp->f.frametype = AST_FRAME_VOICE;
304 rtp->f.subclass = AST_FORMAT_SLINEAR;
305 rtp->f.datalen = 320;
306 rtp->f.samples = 160;
307 memset(rtp->f.data, 0x00, rtp->f.datalen);
311 ast_log(LOG_NOTICE, "Don't know how to handle RFC3389 for receive codec %d\n", rtp->lastrxformat);
316 static int rtpread(int *id, int fd, short events, void *cbdata)
318 struct ast_rtp *rtp = cbdata;
320 f = ast_rtp_read(rtp);
323 rtp->callback(rtp, f, rtp->data);
328 struct ast_frame *ast_rtcp_read(struct ast_rtp *rtp)
330 static struct ast_frame null_frame = { AST_FRAME_NULL, };
334 struct sockaddr_in sin;
335 unsigned int rtcpdata[1024];
342 res = recvfrom(rtp->rtcp->s, rtcpdata, sizeof(rtcpdata),
343 0, (struct sockaddr *)&sin, &len);
346 ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
353 ast_log(LOG_WARNING, "RTP Read too short\n");
358 /* Send to whoever sent to us */
359 if ((rtp->rtcp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
360 (rtp->rtcp->them.sin_port != sin.sin_port)) {
361 memcpy(&rtp->them, &sin, sizeof(rtp->them));
362 ast_log(LOG_DEBUG, "RTP NAT: Using address %s:%d\n", inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
366 ast_log(LOG_DEBUG, "Got RTCP report of %d bytes\n", res);
370 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
372 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
373 gettimeofday(&rtp->rxcore, NULL);
374 rtp->rxcore.tv_sec -= timestamp / 8000;
375 rtp->rxcore.tv_usec -= (timestamp % 8000) * 125;
376 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 20000;
377 if (rtp->rxcore.tv_usec < 0) {
378 /* Adjust appropriately if necessary */
379 rtp->rxcore.tv_usec += 1000000;
380 rtp->rxcore.tv_sec -= 1;
383 tv->tv_sec = rtp->rxcore.tv_sec + timestamp / 8000;
384 tv->tv_usec = rtp->rxcore.tv_usec + (timestamp % 8000) * 125;
385 if (tv->tv_usec >= 1000000) {
386 tv->tv_usec -= 1000000;
391 struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
394 struct sockaddr_in sin;
400 unsigned int timestamp;
401 unsigned int *rtpheader;
402 static struct ast_frame *f, null_frame = { AST_FRAME_NULL, };
403 struct rtpPayloadType rtpPT;
407 /* Cache where the header will go */
408 res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
409 0, (struct sockaddr *)&sin, &len);
412 rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
414 ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
420 ast_log(LOG_WARNING, "RTP Read too short\n");
424 /* Ignore if the other side hasn't been given an address
426 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
430 /* Send to whoever sent to us */
431 if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
432 (rtp->them.sin_port != sin.sin_port)) {
433 memcpy(&rtp->them, &sin, sizeof(rtp->them));
434 ast_log(LOG_DEBUG, "RTP NAT: Using address %s:%d\n", inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
439 seqno = ntohl(rtpheader[0]);
440 payloadtype = (seqno & 0x7f0000) >> 16;
441 mark = seqno & (1 << 23);
443 timestamp = ntohl(rtpheader[1]);
446 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);
448 rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
449 if (!rtpPT.isAstFormat) {
450 // This is special in-band data that's not one of our codecs
451 if (rtpPT.code == AST_RTP_DTMF) {
452 /* It's special -- rfc2833 process it */
453 if (rtp->lasteventseqn <= seqno) {
454 f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
455 rtp->lasteventseqn = seqno;
457 if (f) return f; else return &null_frame;
458 } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
459 /* It's really special -- process it the Cisco way */
460 if (rtp->lasteventseqn <= seqno) {
461 f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
462 rtp->lasteventseqn = seqno;
464 if (f) return f; else return &null_frame;
465 } else if (rtpPT.code == AST_RTP_CN) {
467 f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
468 if (f) return f; else return &null_frame;
470 ast_log(LOG_NOTICE, "Unknown RTP codec %d received\n", payloadtype);
474 rtp->f.subclass = rtpPT.code;
475 if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO)
476 rtp->f.frametype = AST_FRAME_VOICE;
478 rtp->f.frametype = AST_FRAME_VIDEO;
479 rtp->lastrxformat = rtp->f.subclass;
482 rtp->lastrxts = timestamp;
484 if (rtp->dtmfcount) {
486 printf("dtmfcount was %d\n", rtp->dtmfcount);
488 rtp->dtmfcount -= (timestamp - rtp->lastrxts);
489 if (rtp->dtmfcount < 0)
492 if (dtmftimeout != rtp->dtmfcount)
493 printf("dtmfcount is %d\n", rtp->dtmfcount);
496 rtp->lastrxts = timestamp;
498 /* Send any pending DTMF */
499 if (rtp->resp && !rtp->dtmfcount) {
500 ast_log(LOG_DEBUG, "Sending pending DTMF\n");
501 return send_dtmf(rtp);
504 rtp->f.datalen = res - hdrlen;
505 rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
506 rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
507 if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) {
508 switch(rtp->f.subclass) {
509 case AST_FORMAT_ULAW:
510 case AST_FORMAT_ALAW:
511 rtp->f.samples = rtp->f.datalen;
513 case AST_FORMAT_SLINEAR:
514 rtp->f.samples = rtp->f.datalen / 2;
517 rtp->f.samples = 160 * (rtp->f.datalen / 33);
519 case AST_FORMAT_ILBC:
520 rtp->f.samples = 240 * (rtp->f.datalen / 50);
522 case AST_FORMAT_ADPCM:
523 case AST_FORMAT_G726:
524 rtp->f.samples = rtp->f.datalen * 2;
526 case AST_FORMAT_G729A:
527 rtp->f.samples = rtp->f.datalen * 8;
529 case AST_FORMAT_G723_1:
530 rtp->f.samples = g723_samples(rtp->f.data, rtp->f.datalen);
532 case AST_FORMAT_SPEEX:
533 rtp->f.samples = 160;
534 // assumes that the RTP packet contained one Speex frame
537 ast_log(LOG_NOTICE, "Unable to calculate samples for format %s\n", ast_getformatname(rtp->f.subclass));
540 calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
542 /* Video -- samples is # of samples vs. 90000 */
543 if (!rtp->lastividtimestamp)
544 rtp->lastividtimestamp = timestamp;
545 rtp->f.samples = timestamp - rtp->lastividtimestamp;
546 rtp->lastividtimestamp = timestamp;
547 rtp->f.delivery.tv_sec = 0;
548 rtp->f.delivery.tv_usec = 0;
550 rtp->f.subclass |= 0x1;
557 // The following array defines the MIME type (and subtype) for each
558 // of our codecs, or RTP-specific data type.
560 struct rtpPayloadType payloadType;
564 {{1, AST_FORMAT_G723_1}, "audio", "G723"},
565 {{1, AST_FORMAT_GSM}, "audio", "GSM"},
566 {{1, AST_FORMAT_ULAW}, "audio", "PCMU"},
567 {{1, AST_FORMAT_ALAW}, "audio", "PCMA"},
568 {{1, AST_FORMAT_G726}, "audio", "G726-32"},
569 {{1, AST_FORMAT_ADPCM}, "audio", "DVI4"},
570 {{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
571 {{1, AST_FORMAT_LPC10}, "audio", "LPC"},
572 {{1, AST_FORMAT_G729A}, "audio", "G729"},
573 {{1, AST_FORMAT_SPEEX}, "audio", "SPEEX"},
574 {{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
575 {{0, AST_RTP_DTMF}, "audio", "telephone-event"},
576 {{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event"},
577 {{0, AST_RTP_CN}, "audio", "CN"},
578 {{1, AST_FORMAT_JPEG}, "video", "JPEG"},
579 {{1, AST_FORMAT_PNG}, "video", "PNG"},
580 {{1, AST_FORMAT_H261}, "video", "H261"},
581 {{1, AST_FORMAT_H263}, "video", "H263"},
584 /* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
585 also, our own choices for dynamic payload types. This is our master
586 table for transmission */
587 static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
588 [0] = {1, AST_FORMAT_ULAW},
589 [2] = {1, AST_FORMAT_G726}, // Technically this is G.721, but if Cisco can do it, so can we...
590 [3] = {1, AST_FORMAT_GSM},
591 [4] = {1, AST_FORMAT_G723_1},
592 [5] = {1, AST_FORMAT_ADPCM}, // 8 kHz
593 [6] = {1, AST_FORMAT_ADPCM}, // 16 kHz
594 [7] = {1, AST_FORMAT_LPC10},
595 [8] = {1, AST_FORMAT_ALAW},
596 [10] = {1, AST_FORMAT_SLINEAR}, // 2 channels
597 [11] = {1, AST_FORMAT_SLINEAR}, // 1 channel
598 [13] = {0, AST_RTP_CN},
599 [16] = {1, AST_FORMAT_ADPCM}, // 11.025 kHz
600 [17] = {1, AST_FORMAT_ADPCM}, // 22.050 kHz
601 [18] = {1, AST_FORMAT_G729A},
602 [26] = {1, AST_FORMAT_JPEG},
603 [31] = {1, AST_FORMAT_H261},
604 [34] = {1, AST_FORMAT_H263},
605 [97] = {1, AST_FORMAT_ILBC},
606 [101] = {0, AST_RTP_DTMF},
607 [110] = {1, AST_FORMAT_SPEEX},
608 [121] = {0, AST_RTP_CISCO_DTMF}, // Must be type 121
611 void ast_rtp_pt_clear(struct ast_rtp* rtp)
615 for (i = 0; i < MAX_RTP_PT; ++i) {
616 rtp->current_RTP_PT[i].isAstFormat = 0;
617 rtp->current_RTP_PT[i].code = 0;
620 rtp->rtp_lookup_code_cache_isAstFormat = 0;
621 rtp->rtp_lookup_code_cache_code = 0;
622 rtp->rtp_lookup_code_cache_result = 0;
625 void ast_rtp_pt_default(struct ast_rtp* rtp)
628 /* Initialize to default payload types */
629 for (i = 0; i < MAX_RTP_PT; ++i) {
630 rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
631 rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
634 rtp->rtp_lookup_code_cache_isAstFormat = 0;
635 rtp->rtp_lookup_code_cache_code = 0;
636 rtp->rtp_lookup_code_cache_result = 0;
639 // Make a note of a RTP payload type that was seen in a SDP "m=" line.
640 // By default, use the well-known value for this type (although it may
641 // still be set to a different value by a subsequent "a=rtpmap:" line):
642 void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt) {
643 if (pt < 0 || pt > MAX_RTP_PT) return; // bogus payload type
645 if (static_RTP_PT[pt].code != 0) {
646 rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
650 // Make a note of a RTP payload type (with MIME type) that was seen in
651 // a SDP "a=rtpmap:" line.
652 void ast_rtp_set_rtpmap_type(struct ast_rtp* rtp, int pt,
653 char* mimeType, char* mimeSubtype) {
656 if (pt < 0 || pt > MAX_RTP_PT) return; // bogus payload type
658 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
659 if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
660 strcasecmp(mimeType, mimeTypes[i].type) == 0) {
661 rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
667 // Return the union of all of the codecs that were set by rtp_set...() calls
668 // They're returned as two distinct sets: AST_FORMATs, and AST_RTPs
669 void ast_rtp_get_current_formats(struct ast_rtp* rtp,
670 int* astFormats, int* nonAstFormats) {
673 *astFormats = *nonAstFormats = 0;
674 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
675 if (rtp->current_RTP_PT[pt].isAstFormat) {
676 *astFormats |= rtp->current_RTP_PT[pt].code;
678 *nonAstFormats |= rtp->current_RTP_PT[pt].code;
683 struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt)
685 struct rtpPayloadType result;
686 if (pt < 0 || pt > MAX_RTP_PT) {
687 result.isAstFormat = result.code = 0;
688 return result; // bogus payload type
690 /* Start with the negotiated codecs */
691 result = rtp->current_RTP_PT[pt];
692 /* If it doesn't exist, check our static RTP type list, just in case */
694 result = static_RTP_PT[pt];
698 int ast_rtp_lookup_code(struct ast_rtp* rtp, int isAstFormat, int code) {
701 /* Looks up an RTP code out of our *static* outbound list */
703 if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
704 code == rtp->rtp_lookup_code_cache_code) {
705 // Use our cached mapping, to avoid the overhead of the loop below
706 return rtp->rtp_lookup_code_cache_result;
709 /* Check the dynamic list first */
710 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
711 if (rtp->current_RTP_PT[pt].code == code &&
712 rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
713 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
714 rtp->rtp_lookup_code_cache_code = code;
715 rtp->rtp_lookup_code_cache_result = pt;
720 /* Then the static list */
721 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
722 if (static_RTP_PT[pt].code == code &&
723 static_RTP_PT[pt].isAstFormat == isAstFormat) {
724 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
725 rtp->rtp_lookup_code_cache_code = code;
726 rtp->rtp_lookup_code_cache_result = pt;
733 char* ast_rtp_lookup_mime_subtype(int isAstFormat, int code) {
736 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
737 if (mimeTypes[i].payloadType.code == code &&
738 mimeTypes[i].payloadType.isAstFormat == isAstFormat) {
739 return mimeTypes[i].subtype;
745 static struct ast_rtcp *ast_rtcp_new(void)
747 struct ast_rtcp *rtcp;
749 rtcp = malloc(sizeof(struct ast_rtcp));
752 memset(rtcp, 0, sizeof(struct ast_rtcp));
753 rtcp->s = socket(AF_INET, SOCK_DGRAM, 0);
754 rtcp->us.sin_family = AF_INET;
757 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
760 flags = fcntl(rtcp->s, F_GETFL);
761 fcntl(rtcp->s, F_SETFL, flags | O_NONBLOCK);
765 struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
771 rtp = malloc(sizeof(struct ast_rtp));
774 memset(rtp, 0, sizeof(struct ast_rtp));
775 rtp->them.sin_family = AF_INET;
776 rtp->us.sin_family = AF_INET;
777 rtp->s = socket(AF_INET, SOCK_DGRAM, 0);
779 rtp->seqno = rand() & 0xffff;
782 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
785 if (sched && rtcpenable) {
787 rtp->rtcp = ast_rtcp_new();
789 flags = fcntl(rtp->s, F_GETFL);
790 fcntl(rtp->s, F_SETFL, flags | O_NONBLOCK);
791 /* Find us a place */
792 x = (rand() % (rtpend-rtpstart)) + rtpstart;
796 /* Must be an even port number by RTP spec */
797 rtp->us.sin_port = htons(x);
799 rtp->rtcp->us.sin_port = htons(x + 1);
800 if (!bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us)) &&
801 (!rtp->rtcp || !bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us))))
803 if (errno != EADDRINUSE) {
804 ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
815 x = (rtpstart + 1) & ~1;
816 if (x == startplace) {
817 ast_log(LOG_WARNING, "No RTP ports remaining\n");
827 if (io && sched && callbackmode) {
828 /* Operate this one in a callback mode */
831 rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
833 ast_rtp_pt_default(rtp);
837 int ast_rtp_settos(struct ast_rtp *rtp, int tos)
840 if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
841 ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
845 void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
847 rtp->them.sin_port = them->sin_port;
848 rtp->them.sin_addr = them->sin_addr;
850 rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
851 rtp->rtcp->them.sin_addr = them->sin_addr;
855 void ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
857 them->sin_family = AF_INET;
858 them->sin_port = rtp->them.sin_port;
859 them->sin_addr = rtp->them.sin_addr;
862 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
864 memcpy(us, &rtp->us, sizeof(rtp->us));
867 void ast_rtp_stop(struct ast_rtp *rtp)
869 memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
870 memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
872 memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
873 memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->them.sin_port));
877 void ast_rtp_destroy(struct ast_rtp *rtp)
880 ast_smoother_free(rtp->smoother);
882 ast_io_remove(rtp->io, rtp->ioid);
892 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
896 if (!rtp->txcore.tv_sec && !rtp->txcore.tv_usec) {
897 gettimeofday(&rtp->txcore, NULL);
898 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
900 if (delivery && (delivery->tv_sec || delivery->tv_usec)) {
901 /* Use previous txcore */
902 ms = (delivery->tv_sec - rtp->txcore.tv_sec) * 1000;
903 ms += (delivery->tv_usec - rtp->txcore.tv_usec) / 1000;
904 rtp->txcore.tv_sec = delivery->tv_sec;
905 rtp->txcore.tv_usec = delivery->tv_usec;
907 gettimeofday(&now, NULL);
908 ms = (now.tv_sec - rtp->txcore.tv_sec) * 1000;
909 ms += (now.tv_usec - rtp->txcore.tv_usec) / 1000;
910 /* Use what we just got for next time */
911 rtp->txcore.tv_sec = now.tv_sec;
912 rtp->txcore.tv_usec = now.tv_usec;
917 int ast_rtp_senddigit(struct ast_rtp *rtp, char digit)
919 unsigned int *rtpheader;
926 if ((digit <= '9') && (digit >= '0'))
928 else if (digit == '*')
930 else if (digit == '#')
932 else if ((digit >= 'A') && (digit <= 'D'))
933 digit = digit - 'A' + 12;
934 else if ((digit >= 'a') && (digit <= 'd'))
935 digit = digit - 'a' + 12;
937 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
942 /* If we have no peer, return immediately */
943 if (!rtp->them.sin_addr.s_addr)
946 gettimeofday(&rtp->dtmfmute, NULL);
947 rtp->dtmfmute.tv_usec += (500 * 1000);
948 if (rtp->dtmfmute.tv_usec > 1000000) {
949 rtp->dtmfmute.tv_usec -= 1000000;
950 rtp->dtmfmute.tv_sec += 1;
953 ms = calc_txstamp(rtp, NULL);
954 /* Default prediction */
955 rtp->lastts = rtp->lastts + ms * 8;
957 /* Get a pointer to the header */
958 rtpheader = (unsigned int *)data;
959 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (101 << 16) | (rtp->seqno++));
960 rtpheader[1] = htonl(rtp->lastts);
961 rtpheader[2] = htonl(rtp->ssrc);
962 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (0));
964 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
965 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 4, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
967 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));
969 printf("Sent %d bytes of RTP data to %s:%d\n", res, inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
973 /* Clear marker bit and increment seqno */
974 rtpheader[0] = htonl((2 << 30) | (101 << 16) | (rtp->seqno++));
975 /* Make duration 800 (100ms) */
976 rtpheader[3] |= htonl((800));
977 /* Set the End bit for the last 3 */
978 rtpheader[3] |= htonl((1 << 23));
984 static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec)
986 unsigned int *rtpheader;
993 ms = calc_txstamp(rtp, &f->delivery);
994 /* Default prediction */
995 if (f->subclass < AST_FORMAT_MAX_AUDIO) {
996 pred = rtp->lastts + ms * 8;
998 switch(f->subclass) {
999 case AST_FORMAT_ULAW:
1000 case AST_FORMAT_ALAW:
1001 /* If we're within +/- 20ms from when where we
1002 predict we should be, use that */
1003 pred = rtp->lastts + f->datalen;
1005 case AST_FORMAT_ADPCM:
1006 case AST_FORMAT_G726:
1007 /* If we're within +/- 20ms from when where we
1008 predict we should be, use that */
1009 pred = rtp->lastts + f->datalen * 2;
1011 case AST_FORMAT_G729A:
1012 pred = rtp->lastts + f->datalen * 8;
1014 case AST_FORMAT_GSM:
1015 pred = rtp->lastts + (f->datalen * 160 / 33);
1017 case AST_FORMAT_ILBC:
1018 pred = rtp->lastts + (f->datalen * 240 / 50);
1020 case AST_FORMAT_G723_1:
1021 pred = rtp->lastts + g723_samples(f->data, f->datalen);
1023 case AST_FORMAT_SPEEX:
1024 pred = rtp->lastts + 160;
1025 // assumes that the RTP packet contains one Speex frame
1028 ast_log(LOG_WARNING, "Not sure about timestamp format for codec format %s\n", ast_getformatname(f->subclass));
1030 /* Re-calculate last TS */
1031 rtp->lastts = rtp->lastts + ms * 8;
1032 if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
1033 /* If this isn't an absolute delivery time, Check if it is close to our prediction,
1034 and if so, go with our prediction */
1035 if (abs(rtp->lastts - pred) < 640)
1038 ast_log(LOG_DEBUG, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
1043 mark = f->subclass & 0x1;
1044 pred = rtp->lastovidtimestamp + f->samples;
1045 /* Re-calculate last TS */
1046 rtp->lastts = rtp->lastts + ms * 90;
1047 /* If it's close to our prediction, go for it */
1048 if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
1049 if (abs(rtp->lastts - pred) < 7200) {
1051 rtp->lastovidtimestamp += f->samples;
1053 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);
1054 rtp->lastovidtimestamp = rtp->lastts;
1058 /* Get a pointer to the header */
1059 rtpheader = (unsigned int *)(f->data - hdrlen);
1060 rtpheader[0] = htonl((2 << 30) | (codec << 16) | (rtp->seqno++) | (mark << 23));
1061 rtpheader[1] = htonl(rtp->lastts);
1062 rtpheader[2] = htonl(rtp->ssrc);
1063 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1064 res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
1066 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));
1068 printf("Sent %d bytes of RTP data to %s:%d\n", res, inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
1074 int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *_f)
1076 struct ast_frame *f;
1082 /* If we have no peer, return immediately */
1083 if (!rtp->them.sin_addr.s_addr)
1086 /* If there is no data length, return immediately */
1090 /* Make sure we have enough space for RTP header */
1091 if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO)) {
1092 ast_log(LOG_WARNING, "RTP can only send voice\n");
1096 subclass = _f->subclass;
1097 if (_f->frametype == AST_FRAME_VIDEO)
1100 codec = ast_rtp_lookup_code(rtp, 1, subclass);
1102 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
1106 if (rtp->lasttxformat != subclass) {
1107 /* New format, reset the smoother */
1108 ast_log(LOG_DEBUG, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
1109 rtp->lasttxformat = subclass;
1111 ast_smoother_free(rtp->smoother);
1112 rtp->smoother = NULL;
1117 case AST_FORMAT_ULAW:
1118 case AST_FORMAT_ALAW:
1119 if (!rtp->smoother) {
1120 rtp->smoother = ast_smoother_new(160);
1122 if (!rtp->smoother) {
1123 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1126 ast_smoother_feed(rtp->smoother, _f);
1128 while((f = ast_smoother_read(rtp->smoother)))
1129 ast_rtp_raw_write(rtp, f, codec);
1131 case AST_FORMAT_ADPCM:
1132 case AST_FORMAT_G726:
1133 if (!rtp->smoother) {
1134 rtp->smoother = ast_smoother_new(80);
1136 if (!rtp->smoother) {
1137 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1140 ast_smoother_feed(rtp->smoother, _f);
1142 while((f = ast_smoother_read(rtp->smoother)))
1143 ast_rtp_raw_write(rtp, f, codec);
1145 case AST_FORMAT_G729A:
1146 if (!rtp->smoother) {
1147 rtp->smoother = ast_smoother_new(20);
1149 ast_smoother_set_flags(rtp->smoother, AST_SMOOTHER_FLAG_G729);
1151 if (!rtp->smoother) {
1152 ast_log(LOG_WARNING, "Unable to create g729 smoother :(\n");
1155 ast_smoother_feed(rtp->smoother, _f);
1157 while((f = ast_smoother_read(rtp->smoother)))
1158 ast_rtp_raw_write(rtp, f, codec);
1160 case AST_FORMAT_GSM:
1161 if (!rtp->smoother) {
1162 rtp->smoother = ast_smoother_new(33);
1164 if (!rtp->smoother) {
1165 ast_log(LOG_WARNING, "Unable to create GSM smoother :(\n");
1168 ast_smoother_feed(rtp->smoother, _f);
1169 while((f = ast_smoother_read(rtp->smoother)))
1170 ast_rtp_raw_write(rtp, f, codec);
1172 case AST_FORMAT_ILBC:
1173 if (!rtp->smoother) {
1174 rtp->smoother = ast_smoother_new(50);
1176 if (!rtp->smoother) {
1177 ast_log(LOG_WARNING, "Unable to create ILBC smoother :(\n");
1180 ast_smoother_feed(rtp->smoother, _f);
1181 while((f = ast_smoother_read(rtp->smoother)))
1182 ast_rtp_raw_write(rtp, f, codec);
1185 ast_log(LOG_WARNING, "Not sure about sending format %s packets\n", ast_getformatname(subclass));
1186 // fall through to...
1187 case AST_FORMAT_H261:
1188 case AST_FORMAT_H263:
1189 case AST_FORMAT_G723_1:
1190 case AST_FORMAT_SPEEX:
1191 // Don't buffer outgoing frames; send them one-per-packet:
1192 if (_f->offset < hdrlen) {
1197 ast_rtp_raw_write(rtp, f, codec);
1203 void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto)
1205 struct ast_rtp_protocol *cur, *prev;
1211 prev->next = proto->next;
1213 protos = proto->next;
1221 int ast_rtp_proto_register(struct ast_rtp_protocol *proto)
1223 struct ast_rtp_protocol *cur;
1226 if (cur->type == proto->type) {
1227 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
1232 proto->next = protos;
1237 static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
1239 struct ast_rtp_protocol *cur;
1242 if (cur->type == chan->type) {
1250 int ast_rtp_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
1252 struct ast_frame *f;
1253 struct ast_channel *who, *cs[3];
1254 struct ast_rtp *p0, *p1;
1255 struct ast_rtp *vp0, *vp1;
1256 struct ast_rtp_protocol *pr0, *pr1;
1257 struct sockaddr_in ac0, ac1;
1258 struct sockaddr_in vac0, vac1;
1259 struct sockaddr_in t0, t1;
1260 struct sockaddr_in vt0, vt1;
1264 int codec0,codec1, oldcodec0, oldcodec1;
1266 memset(&vt0, 0, sizeof(vt0));
1267 memset(&vt1, 0, sizeof(vt1));
1268 memset(&vac0, 0, sizeof(vac0));
1269 memset(&vac1, 0, sizeof(vac1));
1271 /* if need DTMF, cant native bridge */
1272 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
1274 ast_mutex_lock(&c0->lock);
1275 ast_mutex_lock(&c1->lock);
1276 pr0 = get_proto(c0);
1277 pr1 = get_proto(c1);
1279 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
1280 ast_mutex_unlock(&c0->lock);
1281 ast_mutex_unlock(&c1->lock);
1285 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
1286 ast_mutex_unlock(&c0->lock);
1287 ast_mutex_unlock(&c1->lock);
1290 pvt0 = c0->pvt->pvt;
1291 pvt1 = c1->pvt->pvt;
1292 p0 = pr0->get_rtp_info(c0);
1293 if (pr0->get_vrtp_info)
1294 vp0 = pr0->get_vrtp_info(c0);
1297 p1 = pr1->get_rtp_info(c1);
1298 if (pr1->get_vrtp_info)
1299 vp1 = pr1->get_vrtp_info(c1);
1303 /* Somebody doesn't want to play... */
1304 ast_mutex_unlock(&c0->lock);
1305 ast_mutex_unlock(&c1->lock);
1309 codec0 = pr0->get_codec(c0);
1313 codec1 = pr1->get_codec(c1);
1316 if (pr0->get_codec && pr1->get_codec) {
1317 /* Hey, we can't do reinvite if both parties speak diffrent codecs */
1318 if (!(codec0 & codec1)) {
1319 ast_log(LOG_WARNING, "codec0 = %d is not codec1 = %d, cannot native bridge.\n",codec0,codec1);
1320 ast_mutex_unlock(&c0->lock);
1321 ast_mutex_unlock(&c1->lock);
1325 if (pr0->set_rtp_peer(c0, p1, vp1, codec1))
1326 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
1328 /* Store RTP peer */
1329 ast_rtp_get_peer(p1, &ac1);
1331 ast_rtp_get_peer(vp1, &vac1);
1333 if (pr1->set_rtp_peer(c1, p0, vp0, codec0))
1334 ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
1336 /* Store RTP peer */
1337 ast_rtp_get_peer(p0, &ac0);
1339 ast_rtp_get_peer(vp0, &vac0);
1341 ast_mutex_unlock(&c0->lock);
1342 ast_mutex_unlock(&c1->lock);
1349 if ((c0->pvt->pvt != pvt0) ||
1350 (c1->pvt->pvt != pvt1) ||
1351 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
1352 ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
1353 if (c0->pvt->pvt == pvt0) {
1354 if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
1355 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1357 if (c1->pvt->pvt == pvt1) {
1358 if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
1359 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1361 /* Tell it to try again later */
1365 ast_rtp_get_peer(p1, &t1);
1366 ast_rtp_get_peer(p0, &t0);
1368 codec0 = pr0->get_codec(c0);
1370 codec1 = pr1->get_codec(c1);
1372 ast_rtp_get_peer(vp1, &vt1);
1374 ast_rtp_get_peer(vp0, &vt0);
1375 if (inaddrcmp(&t1, &ac1) || (vp1 && inaddrcmp(&vt1, &vac1)) || (codec1 != oldcodec1)) {
1376 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1377 c1->name, inet_ntoa(t1.sin_addr), ntohs(t1.sin_port), codec1);
1378 ast_log(LOG_DEBUG, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
1379 c1->name, inet_ntoa(vt1.sin_addr), ntohs(vt1.sin_port), codec1);
1380 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
1381 c1->name, inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port), oldcodec1);
1382 ast_log(LOG_DEBUG, "Oooh, '%s' wasv %s:%d/(format %d)\n",
1383 c1->name, inet_ntoa(vac1.sin_addr), ntohs(vac1.sin_port), oldcodec1);
1384 if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL, vt1.sin_addr.s_addr ? vp1 : NULL, codec1))
1385 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
1386 memcpy(&ac1, &t1, sizeof(ac1));
1387 memcpy(&vac1, &vt1, sizeof(vac1));
1390 if (inaddrcmp(&t0, &ac0) || (vp0 && inaddrcmp(&vt0, &vac0))) {
1391 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1392 c0->name, inet_ntoa(t0.sin_addr), ntohs(t0.sin_port), codec0);
1393 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
1394 c0->name, inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
1395 if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL, vt0.sin_addr.s_addr ? vp0 : NULL, codec0))
1396 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
1397 memcpy(&ac0, &t0, sizeof(ac0));
1398 memcpy(&vac0, &vt0, sizeof(vac0));
1401 who = ast_waitfor_n(cs, 2, &to);
1403 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
1404 /* check for hagnup / whentohangup */
1405 if (ast_check_hangup(c0) || ast_check_hangup(c1))
1410 if (!f || ((f->frametype == AST_FRAME_DTMF) &&
1411 (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1412 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1415 ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
1416 if ((c0->pvt->pvt == pvt0) && (!c0->_softhangup)) {
1417 if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
1418 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1420 if ((c1->pvt->pvt == pvt1) && (!c1->_softhangup)) {
1421 if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
1422 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1424 /* That's all we needed */
1427 if ((f->frametype == AST_FRAME_DTMF) ||
1428 (f->frametype == AST_FRAME_VOICE) ||
1429 (f->frametype == AST_FRAME_VIDEO)) {
1430 /* Forward voice or DTMF frames if they happen upon us */
1433 } else if (who == c1) {
1439 /* Swap priority not that it's a big deal at this point */
1448 void ast_rtp_reload(void)
1450 struct ast_config *cfg;
1454 cfg = ast_load("rtp.conf");
1456 if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
1458 if (rtpstart < 1024)
1460 if (rtpstart > 65535)
1463 if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
1472 if (rtpstart >= rtpend) {
1473 ast_log(LOG_WARNING, "Unreasonable values for RTP start/end\n");
1477 if (option_verbose > 1)
1478 ast_verbose(VERBOSE_PREFIX_2 "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
1481 void ast_rtp_init(void)