[asterisk-commits] file: branch file/issue11797 r187639 - /team/file/issue11797/res/
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Fri Apr 10 10:13:22 CDT 2009
Author: file
Date: Fri Apr 10 10:13:19 2009
New Revision: 187639
URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=187639
Log:
Add the rest of the needed bits. This should now send the control packets for Linksys type paging and should
also send the actual RTP packets.
Modified:
team/file/issue11797/res/res_rtp_multicast.c
Modified: team/file/issue11797/res/res_rtp_multicast.c
URL: http://svn.digium.com/svn-view/asterisk/team/file/issue11797/res/res_rtp_multicast.c?view=diff&rev=187639&r1=187638&r2=187639
==============================================================================
--- team/file/issue11797/res/res_rtp_multicast.c (original)
+++ team/file/issue11797/res/res_rtp_multicast.c Fri Apr 10 10:13:19 2009
@@ -81,8 +81,10 @@
enum multicast_type type;
/*! Socket used for sending the audio on */
int socket;
- /*! Control address used for Linksys paging */
- struct sockaddr_in control_address;
+ /*! Synchronization source value, used when creating/sending the RTP packet */
+ unsigned int ssrc;
+ /*! Sequence number, used when creating/sending the RTP packet */
+ unsigned int seqno;
};
/* Forward Declarations */
@@ -126,7 +128,35 @@
return -1;
}
+ multicast->ssrc = ast_random();
+
ast_rtp_instance_set_data(instance, multicast);
+
+ return 0;
+}
+
+/*! \brief Helper function which populates a control packet with useful information and sends it */
+static int multicast_send_control_packet(struct ast_rtp_instance *instance, struct multicast_rtp *multicast, int command)
+{
+ struct multicast_control_packet control_packet = { .unique_id = htonl((u_long)time(NULL)),
+ .command = htonl(command),
+ };
+ struct sockaddr_in control_address, remote_address;
+
+ ast_rtp_instance_get_local_address(instance, &control_address);
+ ast_rtp_instance_get_remote_address(instance, &remote_address);
+
+ /* Ensure the user of us have given us both the control address and destination address */
+ if (!control_address.sin_addr.s_addr || !remote_address.sin_addr.s_addr) {
+ return -1;
+ }
+
+ memcpy(&control_packet.ip, &remote_address.sin_addr, sizeof(control_packet.ip));
+ control_packet.port = htonl(ntohs(remote_address.sin_port));
+
+ /* Based on a recommendation by Brian West who did the FreeSWITCH implementation we send control packets twice */
+ sendto(multicast->socket, &control_packet, sizeof(control_packet), 0, (struct sockaddr *)&control_address, sizeof(control_address));
+ sendto(multicast->socket, &control_packet, sizeof(control_packet), 0, (struct sockaddr *)&control_address, sizeof(control_address));
return 0;
}
@@ -136,11 +166,11 @@
{
struct multicast_rtp *multicast = ast_rtp_instance_get_data(instance);
- if (multicast->type == MULTICAST_TYPE_LINKSYS) {
- struct multicast_control_packet control_packet = { 0, };
- }
-
- return 0;
+ if (multicast->type != MULTICAST_TYPE_LINKSYS) {
+ return 0;
+ }
+
+ return multicast_send_control_packet(instance, multicast, LINKSYS_MCAST_STARTCMD);
}
/*! \brief Function called to destroy a multicast instance */
@@ -149,7 +179,7 @@
struct multicast_rtp *multicast = ast_rtp_instance_get_data(instance);
if (multicast->type == MULTICAST_TYPE_LINKSYS) {
- struct multicast_control_packet control_packet = { 0, };
+ multicast_send_control_packet(instance, multicast, LINKSYS_MCAST_STOPCMD);
}
close(multicast->socket);
@@ -163,18 +193,42 @@
static int multicast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
{
struct multicast_rtp *multicast = ast_rtp_instance_get_data(instance);
+ struct ast_frame *f = frame;
struct sockaddr_in remote_address;
- int hdrlen = 12, res;
- unsigned char *rtpheader = (unsigned char *)(frame->data.ptr - hdrlen);
+ int hdrlen = 12, res, codec;
+ unsigned char *rtpheader;
/* We only accept audio, nothing else */
if (frame->frametype != AST_FRAME_VOICE) {
return 0;
}
+ /* Grab the actual payload number for when we create the RTP packet */
+ if ((codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 1, frame->subclass)) < 0) {
+ return -1;
+ }
+
+ /* If we do not have space to construct an RTP header duplicate the frame so we get some */
+ if (frame->offset < hdrlen) {
+ f = ast_frdup(frame);
+ }
+
+ /* Construct an RTP header for our packet */
+ rtpheader = (unsigned char *)(f->data.ptr - hdrlen);
+ put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (multicast->seqno++) | (0 << 23)));
+ put_unaligned_uint32(rtpheader + 4, htonl(f->ts * 8));
+ put_unaligned_uint32(rtpheader + 8, htonl(multicast->ssrc));
+
+ /* Finally send it out to the eager phones listening for us */
ast_rtp_instance_get_remote_address(instance, &remote_address);
-
- return -1;
+ res = sendto(multicast->socket, (void *)rtpheader, f->datalen + hdrlen, 0, (struct sockaddr *)&remote_address, sizeof(remote_address));
+
+ /* If we were forced to duplicate the frame free the new one */
+ if (frame != f) {
+ ast_frfree(f);
+ }
+
+ return res;
}
/*! \brief Function called to read from a multicast instance */
More information about the asterisk-commits
mailing list