summaryrefslogtreecommitdiffstats
path: root/tinyRTP/src/rtcp/trtp_rtcp_sdes_chunck.c
blob: 9104580addb08452684c4977bd90472b49fac6a6 (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
/*
* Copyright (C) 2012 Doubango Telecom <http://www.doubango.org>
*
* 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.
*
*/
#include "tinyrtp/rtcp/trtp_rtcp_sdes_chunck.h"

#include "tnet_endianness.h"

#include "tsk_debug.h"

/*
Each chunk consists of an SSRC/CSRC identifier followed by a list of
   zero or more items, which carry information about the SSRC/CSRC.
   Each chunk starts on a 32-bit boundary.  Each item consists of an 8-
   bit type field, an 8-bit octet count describing the length of the
   text (thus, not including this two-octet header), and the text
   itself.  Note that the text can be no longer than 255 octets, but
   this is consistent with the need to limit RTCP bandwidth consumption.

+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
|                          SSRC/CSRC_1                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                           SDES items                          |
|                              ...                              |
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*/

static tsk_object_t* trtp_rtcp_sdes_chunck_ctor(tsk_object_t * self, va_list * app)
{
	trtp_rtcp_sdes_chunck_t *chunck = self;
	if(chunck){
		chunck->items = tsk_list_create();
	}
	return self;
}
static tsk_object_t* trtp_rtcp_sdes_chunck_dtor(tsk_object_t * self)
{ 
	trtp_rtcp_sdes_chunck_t *chunck = self;
	if(chunck){
		TSK_OBJECT_SAFE_FREE(chunck->items);
	}

	return self;
}
static const tsk_object_def_t trtp_rtcp_sdes_chunck_def_s = 
{
	sizeof(trtp_rtcp_sdes_chunck_t),
	trtp_rtcp_sdes_chunck_ctor, 
	trtp_rtcp_sdes_chunck_dtor,
	tsk_null, 
};
const tsk_object_def_t *trtp_rtcp_sdes_chunck_def_t = &trtp_rtcp_sdes_chunck_def_s;



trtp_rtcp_sdes_chunck_t* trtp_rtcp_sdes_chunck_create_null()
{
	return tsk_object_new(trtp_rtcp_sdes_chunck_def_t);
}

trtp_rtcp_sdes_chunck_t* trtp_rtcp_sdes_chunck_create(uint32_t ssrc)
{
	trtp_rtcp_sdes_chunck_t* chunck;
	if((chunck = trtp_rtcp_sdes_chunck_create_null())){
		chunck->ssrc = ssrc;
	}
	return chunck;
}

trtp_rtcp_sdes_chunck_t* trtp_rtcp_sdes_chunck_deserialize(const void* data, tsk_size_t size)
{
	trtp_rtcp_sdes_chunck_t* chunck = tsk_null;
	const uint8_t* pdata = (uint8_t*)data;
	const uint8_t* pend;
	if(!data || size < TRTP_RTCP_SDES_CHUNCK_MIN_SIZE){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_null;
	}
	
	pend = (pdata + size);
	if((chunck = trtp_rtcp_sdes_chunck_create_null())){
		trtp_rtcp_sdes_item_t* item;
		tsk_bool_t is_last_item = tsk_false;
		// SSRC/CSRC
		chunck->ssrc = (uint32_t)tnet_ntohl_2(pdata);
		pdata += TRTP_RTCP_SDES_CHUNCK_SSRC_OR_CSRC_SIZE;
		while((pdata < pend) && !is_last_item){
			if((item = trtp_rtcp_sdes_item_deserialize(pdata, (pend-pdata)))){
				is_last_item = (item->type == trtp_rtcp_sdes_item_type_end);
				pdata += trtp_rtcp_sdes_item_get_size(item);
				tsk_list_push_back_data(chunck->items, (void**)&item);
			}
			else{
				TSK_DEBUG_ERROR("Failed to deserialize sdes item");
				break;
			}
		}
	}
	else{
		TSK_DEBUG_ERROR("Failed to create new sdes_chunck object");
		return tsk_null;
	}

	return chunck;
}

int trtp_rtcp_sdes_chunck_serialize_to(const trtp_rtcp_sdes_chunck_t* self, void* data, tsk_size_t size)
{
	uint8_t* pdata = (uint8_t*)data;
	const tsk_list_item_t* item;
	const trtp_rtcp_sdes_item_t* sdes_item;
	tsk_size_t sdes_item_size;
	int ret = 0;

	if(!self || !data || size < trtp_rtcp_sdes_chunck_get_size(self)){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	pdata[0] = self->ssrc >> 24;
	pdata[1] = (self->ssrc >> 16) & 0xFF;
	pdata[2] = (self->ssrc >> 8) & 0xFF;
	pdata[3] = (self->ssrc & 0xFF);
	pdata += 4;

	tsk_list_foreach(item, self->items){
		if(!(sdes_item = TRTP_RTCP_SDES_ITEM(item->data))){
			continue;
		}
		if((ret = trtp_rtcp_sdes_item_serialize_to(sdes_item, pdata, size))){
			TSK_DEBUG_ERROR("SDES item serialization failed");
			goto bail;
		}
		sdes_item_size = trtp_rtcp_sdes_item_get_size(sdes_item);
		pdata += sdes_item_size; size -= sdes_item_size;
	}

bail:
	return ret;
}

int trtp_rtcp_sdes_chunck_add_item(trtp_rtcp_sdes_chunck_t* self, trtp_rtcp_sdes_item_type_t type, const void* data, uint8_t length)
{
	trtp_rtcp_sdes_item_t *item;
	if(!self || !self->items){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}
	if((item = trtp_rtcp_sdes_item_create(type, data, length))){
		tsk_list_push_back_data(self->items, (void**)&item);
	}
	return 0;
}

tsk_size_t trtp_rtcp_sdes_chunck_get_size(const trtp_rtcp_sdes_chunck_t* self)
{
	const tsk_list_item_t* item;
	if(!self){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}
	else{
		tsk_size_t size = TRTP_RTCP_SDES_CHUNCK_SSRC_OR_CSRC_SIZE;
		tsk_list_foreach(item, self->items){
			size += trtp_rtcp_sdes_item_get_size(TRTP_RTCP_SDES_ITEM(item->data));
		}
		if(size & 0x03){//Each chunk starts on a 32-bit boundary 
			size += (4 - (size & 0x03));
		}
		return size;
	}
}

OpenPOWER on IntegriCloud