summaryrefslogtreecommitdiffstats
path: root/tinyMEDIA/src/content/tmedia_content.c
blob: bcdf2750ec5524d17297265f625b7a2f242b7e0d (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright (C) 2010-2015 Mamadou Diop.
*
* 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 tmedia_content.c
 * @brief Base content object.
 */
#include "tinymedia/content/tmedia_content.h"

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

#include <string.h>

/**@defgroup tmedia_content_group Contents
*/

typedef struct tmedia_content_plugin_entry_s
{
	const char* type;
	const tmedia_content_plugin_def_t* plugin;
}
tmedia_content_plugin_entry_t;

/* pointer to all registered contents */
tmedia_content_plugin_entry_t __tmedia_content_plugin_entries[TMEDIA_CONTENT_MAX_PLUGINS][1] = { tsk_null };


int tmedia_content_plugin_register(const char* type, const tmedia_content_plugin_def_t* plugin)
{
	tsk_size_t i;
	//-- int a = sizeof(__tmedia_content_plugin_entries);
	if (!plugin || !plugin){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	/* add or replace the plugin */
	for (i = 0; i < TMEDIA_CONTENT_MAX_PLUGINS; i++){
		if (!__tmedia_content_plugin_entries[i]->plugin || (__tmedia_content_plugin_entries[i]->plugin == plugin && tsk_striequals(type, __tmedia_content_plugin_entries[i]->type))){
			__tmedia_content_plugin_entries[i]->type = type;
			__tmedia_content_plugin_entries[i]->plugin = plugin;
			return 0;
		}
	}


	TSK_DEBUG_ERROR("There are already %d plugins.", TMEDIA_CONTENT_MAX_PLUGINS);
	return -2;
}

int tmedia_content_plugin_unregister(const char* type, const tmedia_content_plugin_def_t* plugin)
{
	tsk_size_t i;
	tsk_bool_t found = tsk_false;
	if (!plugin){
		TSK_DEBUG_ERROR("Invalid Parameter");
		return -1;
	}

	/* find the plugin to unregister */
	for (i = 0; i < TMEDIA_CONTENT_MAX_PLUGINS && __tmedia_content_plugin_entries[i]->plugin; i++){
		if (__tmedia_content_plugin_entries[i]->plugin == plugin && tsk_striequals(type, __tmedia_content_plugin_entries[i]->type)){
			__tmedia_content_plugin_entries[i]->type = tsk_null,
				__tmedia_content_plugin_entries[i]->plugin = tsk_null;
			found = tsk_true;
			break;
		}
	}

	/* compact */
	if (found){
		for (; i < (TMEDIA_CONTENT_MAX_PLUGINS - 1); i++){
			if (__tmedia_content_plugin_entries[i + 1]->plugin){
				__tmedia_content_plugin_entries[i]->type = __tmedia_content_plugin_entries[i + 1]->type,
					__tmedia_content_plugin_entries[i]->plugin = __tmedia_content_plugin_entries[i + 1]->plugin;
			}
			else{
				break;
			}
		}
		__tmedia_content_plugin_entries[i]->type = tsk_null,
			__tmedia_content_plugin_entries[i]->plugin = tsk_null;
	}
	return (found ? 0 : -2);
}

int tmedia_content_plugin_unregister_all()
{
	tsk_size_t i;
	for (i = 0; i < TMEDIA_CONTENT_MAX_PLUGINS && __tmedia_content_plugin_entries[i]->plugin; i++){
		__tmedia_content_plugin_entries[i]->type = tsk_null,
			__tmedia_content_plugin_entries[i]->plugin = tsk_null;
	}
	return 0;
}

tmedia_content_t* tmedia_content_create(const char* type)
{
	tmedia_content_t* content = tsk_null;
	const tmedia_content_plugin_entry_t* entry;
	tsk_size_t i = 0;

	while (i < TMEDIA_CONTENT_MAX_PLUGINS){
		entry = __tmedia_content_plugin_entries[i];
		if (!entry->plugin || !entry->type){
			break;
		}
		if (entry->plugin->objdef && tsk_striequals(entry->type, type)){
			if ((content = tsk_object_new(entry->plugin->objdef))){
				content->plugin = entry->plugin;
				content->type = entry->type;
				return content;
			}
		}
		++i;
	}

	TSK_DEBUG_WARN("Failed to find content type (%s) will be added as dummy", type);
	if (tmedia_content_dummy_plugin_def_t){
		content = tsk_object_new(tmedia_content_dummy_plugin_def_t->objdef);
		content->plugin = tmedia_content_dummy_plugin_def_t;
		content->type = type;
	}

	return content;
}

tmedia_content_t* tmedia_content_parse(const void* data, tsk_size_t size, const char* type)
{
	tmedia_content_t* content = tmedia_content_create(type);
	if (content){
		if (content->plugin->parse){
			int ret;
			if ((ret = content->plugin->parse(content, data, size))){
				TSK_DEBUG_ERROR("Failed to parse the content(%d)", ret);
				TSK_OBJECT_SAFE_FREE(content);
				return tsk_null;
			}
			return content;
		}
		else{
			TSK_DEBUG_ERROR("No parser function for this content (%s)", type);
			TSK_OBJECT_SAFE_FREE(content);
			return tsk_null;
		}
	}
	else{
		TSK_DEBUG_ERROR("Failed to to find content(%s)", type);
		return tsk_null;
	}
}

int tmedia_content_init(tmedia_content_t* self)
{
	if (!self || !self->plugin){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	return 0;
}

int tmedia_content_deinit(tmedia_content_t* self)
{
	if (!self || !self->plugin){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	return 0;
}

tsk_buffer_t* tmedia_content_get_data(tmedia_content_t* self)
{
	if (!self || !self->plugin){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_null;
	}
	return self->plugin->get_data(self);
}




static int tmedia_content_dummy_parse(tmedia_content_t* self, const void* in_data, tsk_size_t in_size)
{
	tmedia_content_dummy_t *dummy = TMEDIA_CONTENT_DUMMY(self);
	if (!dummy || dummy->data){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	dummy->data = tsk_buffer_create(in_data, in_size);
	return 0;
}

static tsk_buffer_t* tmedia_content_dummy_get_data(tmedia_content_t* self)
{
	return tsk_object_ref(TMEDIA_CONTENT_DUMMY(self)->data);
}






//=================================================================================================
//	object/plugin definitions
//
/* constructor */
static tsk_object_t* tmedia_content_dummy_ctor(tsk_object_t * self, va_list * app)
{
	tmedia_content_dummy_t *dummy = self;
	if (dummy){
		/* init base: called by tmedia_content_create() */
		/* init self */
	}
	return self;
}
/* destructor */
static tsk_object_t* tmedia_content_dummy_dtor(tsk_object_t * self)
{
	tmedia_content_dummy_t *dummy = self;
	if (dummy){
		/* deinit base */
		tmedia_content_deinit(TMEDIA_CONTENT(dummy));
		/* deinit self */
		TSK_OBJECT_SAFE_FREE(dummy->data);
	}

	return self;
}
/* object definition */
static const tsk_object_def_t tmedia_content_dummy_def_s =
{
	sizeof(tmedia_content_dummy_t),
	tmedia_content_dummy_ctor,
	tmedia_content_dummy_dtor,
	tsk_null,
};
/* plugin definition*/
static const tmedia_content_plugin_def_t tmedia_content_dummy_plugin_def_s =
{
	&tmedia_content_dummy_def_s,

	"dummy",
	tmedia_content_dummy_parse,
	tmedia_content_dummy_get_data
};
const tmedia_content_plugin_def_t *tmedia_content_dummy_plugin_def_t = &tmedia_content_dummy_plugin_def_s;



//=================================================================================================
//	media content header
//

tmedia_content_header_t* tmedia_content_header_create(const char* name, const char* value)
{
	tmedia_content_header_t* header = tsk_object_new(tmedia_content_header_def_t);
	const char* str;

	if (!header){
		TSK_DEBUG_ERROR("Failed to create new header object");
		return tsk_null;
	}
	header->name = tsk_strdup(name);
	if (value && (str = strstr(value, ";"))){
		header->value = tsk_strndup(value, (tsk_size_t)(str - value));
		header->params = tsk_params_fromstring((str + 1), ";", tsk_true);
	}
	else{
		header->value = tsk_strdup(value);
	}

	return header;
}

int tmedia_content_header_deinit(tmedia_content_header_t* self)
{
	if (!self){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	TSK_FREE(self->name);
	TSK_FREE(self->value);
	TSK_OBJECT_SAFE_FREE(self->params);

	return 0;
}

char* tmedia_content_header_tostring(const tmedia_content_header_t* self)
{
	char* string = tsk_null;

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

	tsk_sprintf(&string, "%s: %s", self->name, self->value);
	if (self->params){
		const tsk_list_item_t* item;
		tsk_list_foreach(item, self->params){
			tsk_strcat_2(&string, ";%s=%s", TSK_PARAM(item->data)->name, TSK_PARAM(item->data)->value);
		}
	}

	return string;
}

/* constructor */
static tsk_object_t* tmedia_content_header_ctor(tsk_object_t * self, va_list * app)
{
	tmedia_content_header_t *header = self;
	if (header){
	}
	return self;
}
/* destructor */
static tsk_object_t* tmedia_content_header_dtor(tsk_object_t * self)
{
	tmedia_content_header_t *header = self;
	if (header){
		tmedia_content_header_deinit(header);
	}

	return self;
}
/* object definition */
static const tsk_object_def_t tmedia_content_header_def_s =
{
	sizeof(tmedia_content_header_t),
	tmedia_content_header_ctor,
	tmedia_content_header_dtor,
	tsk_null,
};
const tsk_object_def_t *tmedia_content_header_def_t = &tmedia_content_header_def_s;
OpenPOWER on IntegriCloud