2 Librtp - a library for the RTP/RTCP protocol
3 Copyright (C) 2000 Roland Dreier
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <sys/types.h>
30 #include <netinet/in.h>
33 #include "rtp-packet.h"
36 rtp_packet_new_take_data (gpointer data, guint data_len)
40 //g_return_val_if_fail(data_len < RTP_MTU, NULL);
42 packet = g_malloc (sizeof *packet);
45 packet->data_len = data_len;
51 rtp_packet_new_copy_data (gpointer data, guint data_len)
55 //g_return_val_if_fail(data_len < RTP_MTU, NULL);
57 packet = g_malloc (sizeof *packet);
59 packet->data = g_memdup (data, data_len);
60 packet->data_len = data_len;
66 rtp_packet_new_allocate (guint payload_len, guint pad_len, guint csrc_count)
71 g_return_val_if_fail (csrc_count <= 15, NULL);
73 len = RTP_HEADER_LEN + csrc_count * sizeof (guint32)
74 + payload_len + pad_len;
76 //g_return_val_if_fail(len < RTP_MTU, NULL);
78 packet = g_malloc (sizeof *packet);
80 packet->data_len = len;
81 packet->data = g_malloc (len);
88 rtp_packet_free (Rtp_Packet packet)
90 g_return_if_fail (packet != NULL);
92 g_free (packet->data);
97 rtp_packet_read(int fd, struct sockaddr *fromaddr, socklen_t *fromlen)
102 buf = g_malloc(RTP_MTU);
104 packlen = recvfrom(fd, buf, RTP_MTU, 0, fromaddr, fromlen);
107 g_error("rtp_packet_read: recvfrom: %d %s", errno, strerror(errno));
112 return rtp_packet_new_take_data(buf, packlen);
116 rtp_packet_send(Rtp_Packet packet, int fd, struct sockaddr *toaddr, socklen_t tolen)
118 g_return_if_fail(packet != NULL);
120 sendto(fd, (void *) packet -> data,
121 packet -> data_len, 0,
126 rtp_packet_get_version (Rtp_Packet packet)
128 g_return_val_if_fail (packet != NULL, 0);
130 return ((Rtp_Header) packet->data)->version;
134 rtp_packet_set_version (Rtp_Packet packet, guint8 version)
136 g_return_if_fail (packet != NULL);
137 g_return_if_fail (version < 0x04);
139 ((Rtp_Header) packet->data)->version = version;
143 rtp_packet_get_padding (Rtp_Packet packet)
145 g_return_val_if_fail (packet != NULL, 0);
147 return ((Rtp_Header) packet->data)->padding;
151 rtp_packet_set_padding (Rtp_Packet packet, guint8 padding)
153 g_return_if_fail (packet != NULL);
154 g_return_if_fail (padding < 0x02);
156 ((Rtp_Header) packet->data)->padding = padding;
160 rtp_packet_get_csrc_count (Rtp_Packet packet)
162 g_return_val_if_fail (packet != NULL, 0);
164 return ((Rtp_Header) packet->data)->csrc_count;
168 rtp_packet_get_extension (Rtp_Packet packet)
170 g_return_val_if_fail (packet != NULL, 0);
172 return ((Rtp_Header) packet->data)->extension;
176 rtp_packet_set_extension (Rtp_Packet packet, guint8 extension)
178 g_return_if_fail (packet != NULL);
179 g_return_if_fail (extension < 0x02);
181 ((Rtp_Header) packet->data)->extension = extension;
185 rtp_packet_set_csrc_count (Rtp_Packet packet, guint8 csrc_count)
187 g_return_if_fail (packet != NULL);
188 g_return_if_fail (csrc_count < 0x04);
190 ((Rtp_Header) packet->data)->csrc_count = csrc_count;
194 rtp_packet_get_marker (Rtp_Packet packet)
196 g_return_val_if_fail (packet != NULL, 0);
198 return ((Rtp_Header) packet->data)->marker;
202 rtp_packet_set_marker (Rtp_Packet packet, guint8 marker)
204 g_return_if_fail (packet != NULL);
205 g_return_if_fail (marker < 0x02);
207 ((Rtp_Header) packet->data)->marker = marker;
211 rtp_packet_get_payload_type (Rtp_Packet packet)
213 g_return_val_if_fail (packet != NULL, 0);
215 return ((Rtp_Header) packet->data)->payload_type;
219 rtp_packet_set_payload_type (Rtp_Packet packet, guint8 payload_type)
221 g_return_if_fail (packet != NULL);
222 g_return_if_fail (payload_type < 0x80);
224 ((Rtp_Header) packet->data)->payload_type = payload_type;
228 rtp_packet_get_seq (Rtp_Packet packet)
230 g_return_val_if_fail (packet != NULL, 0);
232 return g_ntohs (((Rtp_Header) packet->data)->seq);
236 rtp_packet_set_seq (Rtp_Packet packet, guint16 seq)
238 g_return_if_fail (packet != NULL);
240 ((Rtp_Header) packet->data)->seq = g_htons (seq);
244 rtp_packet_get_timestamp (Rtp_Packet packet)
246 g_return_val_if_fail (packet != NULL, 0);
248 return g_ntohl (((Rtp_Header) packet->data)->timestamp);
252 rtp_packet_set_timestamp (Rtp_Packet packet, guint32 timestamp)
254 g_return_if_fail (packet != NULL);
256 ((Rtp_Header) packet->data)->timestamp = g_htonl (timestamp);
260 rtp_packet_get_ssrc (Rtp_Packet packet)
262 g_return_val_if_fail (packet != NULL, 0);
264 return g_ntohl (((Rtp_Header) packet->data)->ssrc);
268 rtp_packet_set_ssrc (Rtp_Packet packet, guint32 ssrc)
270 g_return_if_fail (packet != NULL);
272 ((Rtp_Header) packet->data)->ssrc = g_htonl (ssrc);
276 rtp_packet_get_payload_len (Rtp_Packet packet)
280 g_return_val_if_fail (packet != NULL, 0);
282 len = packet->data_len
283 - RTP_HEADER_LEN - rtp_packet_get_csrc_count (packet) * sizeof (guint32);
285 if (rtp_packet_get_padding (packet)) {
286 len -= ((guint8 *) packet->data)[packet->data_len - 1];
293 rtp_packet_get_payload (Rtp_Packet packet)
295 g_return_val_if_fail (packet != NULL, NULL);
297 return ((char *) packet->data)
298 + RTP_HEADER_LEN + rtp_packet_get_csrc_count (packet) * sizeof (guint32);
302 rtp_packet_get_packet_len (Rtp_Packet packet)
304 g_return_val_if_fail (packet != NULL, 0);
306 return packet->data_len;