summaryrefslogtreecommitdiffstats
path: root/gst/rtp/rtp-packet.c
blob: f33772b521e61655d4640aec37b5caf7197f124b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
  Librtp - a library for the RTP/RTCP protocol
  Copyright (C) 2000  Roland Dreier
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  
  $Id$
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <glib.h>

#include "rtp-packet.h"

Rtp_Packet
rtp_packet_new_take_data (gpointer data, guint data_len)
{
  Rtp_Packet packet;

  //g_return_val_if_fail(data_len < RTP_MTU, NULL);

  packet = g_malloc (sizeof *packet);

  packet->data = data;
  packet->data_len = data_len;

  return packet;
}

Rtp_Packet
rtp_packet_new_copy_data (gpointer data, guint data_len)
{
  Rtp_Packet packet;

  //g_return_val_if_fail(data_len < RTP_MTU, NULL);

  packet = g_malloc (sizeof *packet);

  packet->data = g_memdup (data, data_len);
  packet->data_len = data_len;

  return packet;
}

Rtp_Packet
rtp_packet_new_allocate (guint payload_len, guint pad_len, guint csrc_count)
{
  guint len;
  Rtp_Packet packet;

  g_return_val_if_fail (csrc_count <= 15, NULL);

  len = RTP_HEADER_LEN + csrc_count * sizeof (guint32)
      + payload_len + pad_len;

  //g_return_val_if_fail(len < RTP_MTU, NULL);

  packet = g_malloc (sizeof *packet);

  packet->data_len = len;
  packet->data = g_malloc (len);

  return (packet);
}


void
rtp_packet_free (Rtp_Packet packet)
{
  g_return_if_fail (packet != NULL);

  g_free (packet->data);
  g_free (packet);
}

/*Rtp_Packet
rtp_packet_read(int fd, struct sockaddr *fromaddr, socklen_t *fromlen)
{
  int packlen;
  gpointer buf;

  buf = g_malloc(RTP_MTU);

  packlen = recvfrom(fd, buf, RTP_MTU, 0, fromaddr, fromlen);

  if (packlen < 0) {
    g_error("rtp_packet_read: recvfrom: %d %s", errno, strerror(errno));
    //exit(1);
    return NULL;
  }

  return rtp_packet_new_take_data(buf, packlen);
}*/

/*void
rtp_packet_send(Rtp_Packet packet, int fd, struct sockaddr *toaddr, socklen_t tolen)
{
  g_return_if_fail(packet != NULL);

  sendto(fd, (void *) packet -> data,
         packet -> data_len, 0,
	 toaddr, tolen);
}*/

guint8
rtp_packet_get_version (Rtp_Packet packet)
{
  g_return_val_if_fail (packet != NULL, 0);

  return ((Rtp_Header) packet->data)->version;
}

void
rtp_packet_set_version (Rtp_Packet packet, guint8 version)
{
  g_return_if_fail (packet != NULL);
  g_return_if_fail (version < 0x04);

  ((Rtp_Header) packet->data)->version = version;
}

guint8
rtp_packet_get_padding (Rtp_Packet packet)
{
  g_return_val_if_fail (packet != NULL, 0);

  return ((Rtp_Header) packet->data)->padding;
}

void
rtp_packet_set_padding (Rtp_Packet packet, guint8 padding)
{
  g_return_if_fail (packet != NULL);
  g_return_if_fail (padding < 0x02);

  ((Rtp_Header) packet->data)->padding = padding;
}

guint8
rtp_packet_get_csrc_count (Rtp_Packet packet)
{
  g_return_val_if_fail (packet != NULL, 0);

  return ((Rtp_Header) packet->data)->csrc_count;
}

guint8
rtp_packet_get_extension (Rtp_Packet packet)
{
  g_return_val_if_fail (packet != NULL, 0);

  return ((Rtp_Header) packet->data)->extension;
}

void
rtp_packet_set_extension (Rtp_Packet packet, guint8 extension)
{
  g_return_if_fail (packet != NULL);
  g_return_if_fail (extension < 0x02);

  ((Rtp_Header) packet->data)->extension = extension;
}

void
rtp_packet_set_csrc_count (Rtp_Packet packet, guint8 csrc_count)
{
  g_return_if_fail (packet != NULL);
  g_return_if_fail (csrc_count < 0x04);

  ((Rtp_Header) packet->data)->csrc_count = csrc_count;
}

guint8
rtp_packet_get_marker (Rtp_Packet packet)
{
  g_return_val_if_fail (packet != NULL, 0);

  return ((Rtp_Header) packet->data)->marker;
}

void
rtp_packet_set_marker (Rtp_Packet packet, guint8 marker)
{
  g_return_if_fail (packet != NULL);
  g_return_if_fail (marker < 0x02);

  ((Rtp_Header) packet->data)->marker = marker;
}

guint8
rtp_packet_get_payload_type (Rtp_Packet packet)
{
  g_return_val_if_fail (packet != NULL, 0);

  return ((Rtp_Header) packet->data)->payload_type;
}

void
rtp_packet_set_payload_type (Rtp_Packet packet, guint8 payload_type)
{
  g_return_if_fail (packet != NULL);
  g_return_if_fail (payload_type < 0x80);

  ((Rtp_Header) packet->data)->payload_type = payload_type;
}

guint16
rtp_packet_get_seq (Rtp_Packet packet)
{
  g_return_val_if_fail (packet != NULL, 0);

  return g_ntohs (((Rtp_Header) packet->data)->seq);
}

void
rtp_packet_set_seq (Rtp_Packet packet, guint16 seq)
{
  g_return_if_fail (packet != NULL);

  ((Rtp_Header) packet->data)->seq = g_htons (seq);
}

guint32
rtp_packet_get_timestamp (Rtp_Packet packet)
{
  g_return_val_if_fail (packet != NULL, 0);

  return g_ntohl (((Rtp_Header) packet->data)->timestamp);
}

void
rtp_packet_set_timestamp (Rtp_Packet packet, guint32 timestamp)
{
  g_return_if_fail (packet != NULL);

  ((Rtp_Header) packet->data)->timestamp = g_htonl (timestamp);
}

guint32
rtp_packet_get_ssrc (Rtp_Packet packet)
{
  g_return_val_if_fail (packet != NULL, 0);

  return g_ntohl (((Rtp_Header) packet->data)->ssrc);
}

void
rtp_packet_set_ssrc (Rtp_Packet packet, guint32 ssrc)
{
  g_return_if_fail (packet != NULL);

  ((Rtp_Header) packet->data)->ssrc = g_htonl (ssrc);
}

guint
rtp_packet_get_payload_len (Rtp_Packet packet)
{
  guint len;

  g_return_val_if_fail (packet != NULL, 0);

  len = packet->data_len
      - RTP_HEADER_LEN - rtp_packet_get_csrc_count (packet) * sizeof (guint32);

  if (rtp_packet_get_padding (packet)) {
    len -= ((guint8 *) packet->data)[packet->data_len - 1];
  }

  return len;
}

gpointer
rtp_packet_get_payload (Rtp_Packet packet)
{
  g_return_val_if_fail (packet != NULL, NULL);

  return ((char *) packet->data)
      + RTP_HEADER_LEN + rtp_packet_get_csrc_count (packet) * sizeof (guint32);
}

guint
rtp_packet_get_packet_len (Rtp_Packet packet)
{
  g_return_val_if_fail (packet != NULL, 0);

  return packet->data_len;
}