summaryrefslogtreecommitdiffstats
path: root/branches/1.0/tinyRTP/src/rtp/trtp_rtp_header.c
blob: 65fffe35b7ac16279a4378fc2f03625ef3c2fd1e (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
/*
* Copyright (C) 2009-2010 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO 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 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO 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 DOUBANGO.
*
*/
/**@file trtp_rtp_header.c
 * @brief RTP header.
 *
 * @author Mamadou Diop <diopmamadou(at)doubango.org>
 *
 * @date Created: Sat Nov 8 16:54:58 2009 mdiop
 */
#include "tinyrtp/rtp/trtp_rtp_header.h"

#include "tnet_endianness.h"

#include "tsk_memory.h"
#include "tsk_debug.h"

	/* RFC 3550 section 5.1 - RTP Fixed Header Fields
		0                   1                   2                   3
		0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
	   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
	   |V=2|P|X|  CC   |M|     PT      |       sequence number         |
	   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
	   |                           timestamp                           |
	   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
	   |           synchronization source (SSRC) identifier            |
	   +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
	   |            contributing source (CSRC) identifiers             |
	   |                             ....                              |
	   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
	*/

/* Create new RTP header */
trtp_rtp_header_t* trtp_rtp_header_create_null()
{
	return tsk_object_new(trtp_rtp_header_def_t);
}

trtp_rtp_header_t* trtp_rtp_header_create(uint32_t ssrc, uint16_t seq_num, uint32_t timestamp, uint8_t payload_type, tsk_bool_t marker)
{
	trtp_rtp_header_t* header;
	if((header = tsk_object_new(trtp_rtp_header_def_t))){
		header->version = TRTP_RTP_VERSION;
		header->marker = marker ? 1 : 0;
		header->payload_type = payload_type;
		header->seq_num = seq_num;
		header->timestamp = timestamp;
		header->ssrc = ssrc;
	}
	return header;
}

/** Serialize rtp header object into binary buffer */
tsk_buffer_t* trtp_rtp_header_serialize(const trtp_rtp_header_t *self)
{
	tsk_buffer_t* buffer;
	uint8_t* data;
	tsk_size_t i;

	if(!self){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_null;
	}

	if(!(data = tsk_calloc(TRTP_RTP_HEADER_MIN_SIZE + (4 * self->csrc_count), 1))){
		TSK_DEBUG_ERROR("Failed to allocate buffer");
		return tsk_null;
	}

	/* Octet-0: version(2), Padding(1), Extension(1), CSRC Count(4) */
	data[0] = (((uint8_t)self->version)<< 6) |
		(((uint8_t)self->padding)<< 5) |
		(((uint8_t)self->extension)<< 4) |
		((uint8_t)self->csrc_count);
	/* Octet-1: Marker(1), Payload Type(7) */
	data[1] = (((uint8_t)self->marker)<< 7) |
		((uint8_t)self->payload_type);
	/* Octet-2-3: Sequence number (16) */
	*((uint16_t*)&data[2]) = tnet_htons(self->seq_num);
	/* Octet-4-5-6-7: timestamp (32) */
	*((uint32_t*)&data[4]) = tnet_htonl(self->timestamp);
	/* Octet-8-9-10-11: SSRC (32) */
	*((uint32_t*)&data[8]) = tnet_htonl(self->ssrc);

	/* Octet-12-13-14-15-****: CSRC */
	for(i=0; i<self->csrc_count; i++){
		*((uint32_t*)&data[12+i]) = tnet_htonl(self->csrc[i]);
	}

	if(!(buffer = tsk_buffer_create_null())){
		TSK_DEBUG_ERROR("Failed to create new buffer");
		TSK_FREE(data);
		return tsk_null;
	}
	else{
		tsk_buffer_takeownership(buffer, (void**)&data, TRTP_RTP_HEADER_MIN_SIZE + (4 * self->csrc_count));
	}

	return buffer;
}

/** Deserialize rtp header object from binary buffer */
trtp_rtp_header_t* trtp_rtp_header_deserialize(const void *data, tsk_size_t size)
{
	trtp_rtp_header_t* header = tsk_null;
	const uint8_t* pdata = (const uint8_t*)data;
	uint8_t csrc_count, i;

	if(!data){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_null;
	}

	if(size <TRTP_RTP_HEADER_MIN_SIZE){
		TSK_DEBUG_ERROR("Too short to contain RTP header");
		return tsk_null;
	}

	/* Before starting to deserialize, get the "csrc_count" and check the length validity
	* CSRC count (4 last bits)
	*/
	csrc_count = (*pdata & 0x0F);
	if(size <(tsk_size_t)TRTP_RTP_HEADER_MIN_SIZE + (4 * csrc_count)){
		TSK_DEBUG_ERROR("Too short to contain RTP header");
		return tsk_null;
	}

	if(!(header = trtp_rtp_header_create_null())){
		TSK_DEBUG_ERROR("Failed to create new RTP header");
		return tsk_null;
	}

	/* version (2bits) */
	header->version = (*pdata >> 6);
	/* Padding (1bit) */
	header->padding = ((*pdata >>5) & 0x01);
	/* Extension (1bit) */
	header->extension = ((*pdata >>4) & 0x01);
	/* CSRC Count (4bits) */
	header->csrc_count = csrc_count;
	// skip octet
	++pdata;

	/* Marker (1bit) */
	header->marker = (*pdata >> 7);
	/* Payload Type (7bits) */
	header->payload_type = (*pdata & 0x7F);
	// skip octet
	++pdata;

	/* Sequence Number (16bits) */
	header->seq_num = tnet_ntohs(*((uint16_t*)pdata));
	// skip octets
	pdata += 2;

	/* timestamp (32bits) */
	header->timestamp = tnet_ntohl(*((uint32_t*)pdata));
	// skip octets
	pdata += 4;

	/* synchronization source (SSRC) identifier (32bits) */
	header->ssrc = tnet_ntohl(*((uint32_t*)pdata));
	// skip octets
	pdata += 4;

	/* contributing source (CSRC) identifiers */
	for(i=0; i<csrc_count; i++, pdata += 4){
		header->csrc[i] = tnet_ntohl(*((uint32_t*)pdata));
	}

	return header;
}






//=================================================================================================
//	RTP header object definition
//
static tsk_object_t* trtp_rtp_header_ctor(tsk_object_t * self, va_list * app)
{
	trtp_rtp_header_t *header = self;
	if(header){
	}
	return self;
}

static tsk_object_t* trtp_rtp_header_dtor(tsk_object_t * self)
{ 
	trtp_rtp_header_t *header = self;
	if(header){
	}

	return self;
}

static const tsk_object_def_t trtp_rtp_header_def_s = 
{
	sizeof(trtp_rtp_header_t),
	trtp_rtp_header_ctor, 
	trtp_rtp_header_dtor,
	tsk_null, 
};
const tsk_object_def_t *trtp_rtp_header_def_t = &trtp_rtp_header_def_s;
OpenPOWER on IntegriCloud