summaryrefslogtreecommitdiffstats
path: root/tinyRTP/src/rtcp/trtp_rtcp_sdes_item.c
blob: 7f2253722826a25ecfeaac828f3409e1c7350957 (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
/*
* 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_item.h"
#include "tinyrtp/rtcp/trtp_rtcp_packet.h"

#include "tnet_endianness.h"

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

#include <string.h> /* strlen() */

static tsk_object_t* trtp_rtcp_sdes_item_ctor(tsk_object_t * self, va_list * app)
{
	trtp_rtcp_sdes_item_t *item = self;
	if(item){
	}
	return self;
}
static tsk_object_t* trtp_rtcp_sdes_item_dtor(tsk_object_t * self)
{ 
	trtp_rtcp_sdes_item_t *item = self;
	if(item){
		TSK_OBJECT_SAFE_FREE(item->data);
	}

	return self;
}
static const tsk_object_def_t trtp_rtcp_sdes_item_def_s = 
{
	sizeof(trtp_rtcp_sdes_item_t),
	trtp_rtcp_sdes_item_ctor, 
	trtp_rtcp_sdes_item_dtor,
	tsk_null, 
};
const tsk_object_def_t *trtp_rtcp_sdes_item_def_t = &trtp_rtcp_sdes_item_def_s;


trtp_rtcp_sdes_item_t* _trtp_rtcp_sdes_item_create_null(trtp_rtcp_sdes_item_type_t type)
{
	return tsk_object_new(trtp_rtcp_sdes_item_def_t);
}

trtp_rtcp_sdes_item_t* trtp_rtcp_sdes_item_create(trtp_rtcp_sdes_item_type_t type, const void* data, uint8_t length)
{
	trtp_rtcp_sdes_item_t* item;
	if(!(item = _trtp_rtcp_sdes_item_create_null(type))){
		TSK_DEBUG_ERROR("Failed to create new SDES item");
		return tsk_null;
	}
	item->type = type;
	if(data && length){
		item->data = tsk_buffer_create(data, length);
	}
	
	return item;
}

trtp_rtcp_sdes_item_t* trtp_rtcp_sdes_item_deserialize(const void* data, tsk_size_t size)
{
	const uint8_t* pdata = (const uint8_t*)data;
	
	if(!data || !size){
		TSK_DEBUG_ERROR("Invlaid parameter");
		return tsk_null;
	}

	if(pdata[0] == trtp_rtcp_sdes_item_type_end){
		return trtp_rtcp_sdes_item_create(trtp_rtcp_sdes_item_type_end, tsk_null, 0);
	}
	
	if(size < TRTP_RTCP_SDES_ITEM_MIN_SIZE || size < (tsk_size_t)(pdata[1] + 2)){
		TSK_DEBUG_ERROR("Too short");
		return tsk_null;
	}

	return trtp_rtcp_sdes_item_create((trtp_rtcp_sdes_item_type_t)pdata[0], &pdata[2], pdata[1]);
}

tsk_buffer_t* trtp_rtcp_sdes_item_serialize(const trtp_rtcp_sdes_item_t* self)
{
	tsk_buffer_t*buffer = tsk_null;
	if(!self){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_null;
	}
	if((buffer = tsk_buffer_create(tsk_null, trtp_rtcp_sdes_item_get_size(self)))){
		if(trtp_rtcp_sdes_item_serialize_to(self, buffer->data, buffer->size) != 0){
			TSK_OBJECT_SAFE_FREE(buffer);
		}
	}
	return buffer;
}

int trtp_rtcp_sdes_item_serialize_to(const trtp_rtcp_sdes_item_t* self, void* data, tsk_size_t size)
{
	if(!self || !data || (size < trtp_rtcp_sdes_item_get_size(self))){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}
	if(self->type == trtp_rtcp_sdes_item_type_end){
		((uint8_t*)data)[0] = trtp_rtcp_sdes_item_type_end;
	}
	else{
		((uint8_t*)data)[0] = self->type;
		if(self->data){
			((uint8_t*)data)[1] = (uint8_t)self->data->size;
			memcpy(&((uint8_t*)data)[2], self->data->data, self->data->size);
		}
		else{
			((uint8_t*)data)[1] = 0;
		}
	}
	return 0;
}

tsk_size_t trtp_rtcp_sdes_item_get_size(const trtp_rtcp_sdes_item_t* self)
{
	if(!self){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}
	switch(self->type){
		case trtp_rtcp_sdes_item_type_end: return 1;
		default: return 2 + (self->data ? self->data->size : 0);
	}
}

OpenPOWER on IntegriCloud