summaryrefslogtreecommitdiffstats
path: root/tinyMEDIA/src/tmedia_common.c
blob: 1f20845b9d678739d66b4274371f94f794654020 (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]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 tmedia_common.c
 * @brief Common functions and definitions.
 *
 * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
 *

 */
#include "tinymedia/tmedia_common.h"

#include "tinymedia/tmedia_consumer.h"
#include "tinymedia/tmedia_producer.h"
#include "tinymedia/tmedia_session.h"
#include "tinymedia/tmedia_converter_video.h"
#include "tinymedia/tmedia_resampler.h"
#include "tinymedia/tmedia_denoise.h"
#include "tinymedia/tmedia_imageattr.h"
#include "tinymedia/tmedia_defaults.h"

#include "tsk_params.h"
#include "tsk_debug.h"

#include <stdlib.h> /* atoi() */

typedef struct fmtp_size_s {
    const char* name;
    tmedia_pref_video_size_t pref_vs;
    tsk_bool_t cif_family;
    unsigned width;
    unsigned height;
} fmtp_size_t;
static const fmtp_size_t fmtp_sizes[] = {
    /* must be sorted like this */
    {"2160P", tmedia_pref_video_size_2160p, tsk_false, 3840, 2160},
    {"1080P", tmedia_pref_video_size_1080p, tsk_false, 1920, 1080},
    {"16CIF", tmedia_pref_video_size_16cif, tsk_true, 1408, 1152},
    {"720P", tmedia_pref_video_size_720p, tsk_false, 1280, 720},
    {"XGA", tmedia_pref_video_size_xga, tsk_false, 1024, 768},
    {"480P", tmedia_pref_video_size_480p, tsk_false, 852, 480},
    {"WVGA", tmedia_pref_video_size_wvga, tsk_false, 800, 480},
    {"SVGA", tmedia_pref_video_size_svga, tsk_false, 800, 600},
    {"4CIF", tmedia_pref_video_size_4cif, tsk_true, 704, 576},
    {"VGA", tmedia_pref_video_size_vga, tsk_false, 640, 480},
    {"HVGA", tmedia_pref_video_size_hvga, tsk_false, 480, 320},
    {"CIF", tmedia_pref_video_size_cif, tsk_true, 352, 288},
    {"QVGA", tmedia_pref_video_size_qvga, tsk_false, 320, 240},
    {"QCIF", tmedia_pref_video_size_qcif, tsk_true, 176, 144},
    {"SQCIF", tmedia_pref_video_size_sqcif, tsk_true, 128, 96}
};

typedef int (*plugin_register)(const void* plugin_def);
typedef int (*plugin_unregister)(const void* plugin_def);

typedef struct plugin_decl_s {
    tsk_plugin_def_type_t type;
    plugin_register fn_register;
    plugin_unregister fn_unregister;
}
plugin_decl_t;

static const struct plugin_decl_s __plugin_def_types[] = {
    {tsk_plugin_def_type_consumer, tmedia_consumer_plugin_register, tmedia_consumer_plugin_unregister },
    {tsk_plugin_def_type_producer, tmedia_producer_plugin_register, tmedia_producer_plugin_unregister },
    {tsk_plugin_def_type_session, tmedia_session_plugin_register, tmedia_session_plugin_unregister },
    {tsk_plugin_def_type_codec, tmedia_codec_plugin_register, tmedia_codec_plugin_unregister },
    {tsk_plugin_def_type_converter, tmedia_converter_video_plugin_register, tmedia_converter_video_plugin_unregister },
    {tsk_plugin_def_type_resampler, tmedia_resampler_plugin_register, tmedia_resampler_plugin_unregister },
    {tsk_plugin_def_type_denoiser, tmedia_denoise_plugin_register, tmedia_denoise_plugin_unregister },
};
static const tsk_size_t __plugin_def_types_count = sizeof(__plugin_def_types)/sizeof(__plugin_def_types[0]);
static const tsk_plugin_def_media_type_t __plugin_def_media_types[] = {
    tsk_plugin_def_media_type_audio,
    tsk_plugin_def_media_type_video,
    tsk_plugin_def_media_type_screencast
};
static const tsk_size_t __plugin_def_media_types_count = sizeof(__plugin_def_media_types)/sizeof(__plugin_def_media_types[0]);

static tsk_size_t _tmedia_plugin_register_or_unregister(struct tsk_plugin_s* plugin, enum tsk_plugin_def_type_e type, enum tsk_plugin_def_media_type_e media, tsk_bool_t _register)
{
    tsk_size_t ret = 0, i, j, index;
    tsk_plugin_def_ptr_const_t plugin_def_ptr_const;
    if(!plugin) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return 0;
    }

    for(i = 0; i < __plugin_def_types_count; ++i) {
        for(j = 0; j < __plugin_def_media_types_count; ++j) {
            if((_register ? __plugin_def_types[i].fn_register : __plugin_def_types[i].fn_unregister) && (type & __plugin_def_types[i].type) == __plugin_def_types[i].type && (media & __plugin_def_media_types[j]) == __plugin_def_media_types[j]) {
                for(index = 0; (plugin_def_ptr_const = tsk_plugin_get_def_2(plugin, __plugin_def_types[i].type, __plugin_def_media_types[j], index)); ++index) {
                    if((_register ? __plugin_def_types[i].fn_register : __plugin_def_types[i].fn_unregister)(plugin_def_ptr_const) == 0) {
                        ++ret;
                    }
                }
            }
        }
    }

    return ret;
}

tsk_size_t tmedia_plugin_register(struct tsk_plugin_s* plugin, enum tsk_plugin_def_type_e type, enum tsk_plugin_def_media_type_e media)
{
    return _tmedia_plugin_register_or_unregister(plugin, type, media, tsk_true);
}

tsk_size_t tmedia_plugin_unregister(struct tsk_plugin_s* plugin, enum tsk_plugin_def_type_e type, enum tsk_plugin_def_media_type_e media)
{
    return _tmedia_plugin_register_or_unregister(plugin, type, media, tsk_false);
}

tmedia_type_t tmedia_type_from_sdp(const tsdp_message_t* sdp)
{
    tmedia_type_t type = tmedia_none;
    const tsdp_header_M_t* M;
    tsk_size_t index = 0;

    if (!sdp) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return tmedia_none;
    }

    while ((M = (const tsdp_header_M_t*)tsdp_message_get_headerAt(sdp, tsdp_htype_M, index++))) {
        type |= tmedia_type_from_sdp_headerM(M);
    }
    return type;
}

tmedia_type_t tmedia_type_from_sdp_headerM(const tsdp_header_M_t* M)
{
    const tmedia_session_plugin_def_t* plugin;
    const tsdp_header_A_t* A;
    if (!M) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return tmedia_none;
    }
    if (M->port && (plugin = tmedia_session_plugin_find_by_media(M->media))) {
        if (plugin->type == tmedia_audio || plugin->type == tmedia_video) {
            // check if it's BFCP audio/video session
            // content attribute described in http://tools.ietf.org/html/rfc4796
            if ((A = tsdp_header_M_findA(M, "content")) && (!tsk_striequals(A->value, "main"))) {
                return plugin->type == tmedia_audio ? tmedia_bfcp_audio : tmedia_bfcp_video;
            }
        }
        return plugin->type;
    }
    return tmedia_none;
}


int tmedia_parse_rtpmap(const char* rtpmap, char** name, int32_t* rate, int32_t* channels)
{
    /* e.g. AMR-WB/16000/2 */

    int len;
    int index, pos = 0;

    if(tsk_strnullORempty(rtpmap)) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }

    *name = tsk_null;
    *rate = *channels = 0;
    len = (int)tsk_strlen(rtpmap);

    /* name */
    if((index = tsk_strindexOf(rtpmap, len, "/")) != -1) {
        *name = tsk_strndup(rtpmap, index);
        len -= (index + 1), pos = (index + 1);
        /* rate */
        if(len>0) {
            if((index = tsk_strindexOf((rtpmap + pos), len, "/")) != -1) {
                *rate = atoi(&rtpmap[pos]);
                len -= (index + 1), pos += (index + 1);
                /* channels */
                if(len>0) {
                    *channels = atoi(&rtpmap[pos]);
                }
            }
            else {
                *rate = atoi(&rtpmap[pos]);
            }
        }
    }
    else {
        *name = tsk_strdup(rtpmap);
    }

    return 0;

    ///* e.g. AMR-WB/16000/2 */
    //if(sscanf(rtpmap, "%*s/%*d/%*d") != EOF){
    //	int index = tsk_strindexOf(rtpmap, len, "/");
    //	*name = tsk_strndup(rtpmap, index);
    //	sscanf(&rtpmap[index+1], "%d/%d", rate, channels);
    //	return 0;
    //}
    ///* e.g. AMR-WB/16000 */
    //else if(sscanf(rtpmap, "%*s/%*d") != EOF){
    //	int index = tsk_strindexOf(rtpmap, len, "/");
    //	*name = tsk_strndup(rtpmap, index);
    //	*rate = atoi(&rtpmap[index+1]);
    //	return 0;
    //}
    ///* e.g. AMR-WB */
    //else if(sscanf(rtpmap, "%*s") != EOF){
    //	*name = tsk_strdup(rtpmap);
    //	return 0;
    //}
    //else{
    //	TSK_DEBUG_ERROR("%s is not a valid rtpmap value", rtpmap);
    //	return -2;
    //}
}

int tmedia_video_get_size(tmedia_pref_video_size_t pref_vs, unsigned *width, unsigned *height)
{
    int i;
    for(i=0; i<sizeof(fmtp_sizes)/sizeof(fmtp_sizes[0]); i++) {
        if(fmtp_sizes[i].pref_vs == pref_vs) {
            if(width) {
                *width = fmtp_sizes[i].width;
            }
            if(height) {
                *height = fmtp_sizes[i].height;
            }
            return 0;
        }
    }
    return -1;
}

int tmedia_video_get_closest_cif_size(tmedia_pref_video_size_t pref_vs, tmedia_pref_video_size_t *cif_vs)
{
    int i;
    if(!cif_vs) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    for(i=0; i<sizeof(fmtp_sizes)/sizeof(fmtp_sizes[0]); i++) {
        if(fmtp_sizes[i].pref_vs <= pref_vs && fmtp_sizes[i].cif_family) {
            *cif_vs = fmtp_sizes[i].pref_vs;
            return 0;
        }
    }
    return -2;
}

int tmedia_video_get_closest_pref_size(unsigned width, unsigned height, tmedia_pref_video_size_t *pref_vs)
{
    int i;
    unsigned size;
    if (!pref_vs) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    size = width * height;
    for (i = 0; i<sizeof(fmtp_sizes) / sizeof(fmtp_sizes[0]); i++) {
        if (size >= fmtp_sizes[i].height * fmtp_sizes[i].width) {
            *pref_vs = fmtp_sizes[i].pref_vs;
            return 0;
        }
    }
    return -2;
}

int tmedia_parse_video_fmtp(const char* fmtp, tmedia_pref_video_size_t pref_vs, unsigned* width, unsigned* height, unsigned* fps)
{
    int ret = -2;
    tsk_params_L_t* params = tsk_null;
    const tsk_param_t* param = tsk_null;
    const tsk_list_item_t* item;
    int i;
    tmedia_pref_video_size_t best_vs;

    if(!fmtp || !width || !height || !fps) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }

    // set default values
    best_vs = fmtp_sizes[(sizeof(fmtp_sizes)/sizeof(fmtp_sizes[0])) - 1].pref_vs /* last = lowest resolution */;
    ret = tmedia_video_get_size(pref_vs, width, height);
    *fps = 15;

    if((params = tsk_params_fromstring(fmtp, ";", tsk_true))) {
        // set real values
        tsk_list_foreach(item, params) {
            if(!(param = TSK_PARAM(item->data)) || !param->name || !param->value) {
                continue;
            }
            for(i=0; i<sizeof(fmtp_sizes)/sizeof(fmtp_sizes[0]); i++) {
                if((int)pref_vs >= (int)fmtp_sizes[i].pref_vs && tsk_striequals(fmtp_sizes[i].name, param->name) && ((int)best_vs <= (int)fmtp_sizes[i].pref_vs)) {
                    *width= fmtp_sizes[i].width;
                    *height = fmtp_sizes[i].height;
                    *fps = atoi(param->value);
                    *fps = *fps ? 30/(*fps) : 15;
                    ret = 0;
                    best_vs = fmtp_sizes[i].pref_vs;
                    // rfc 4629 section 8.2.1: Parameters offered first are the most preferred picture mode to be received.
                    // /!\ asterisk do not respect this :)
                    /* goto done */;
                }
            }
        }
        /* done: */
        TSK_OBJECT_SAFE_FREE(params);
    }

    return ret;
}

static void _imageattr_get_best_size(const tmedia_imageattr_set_xt* set, xyvalue_t *width, xyvalue_t *height)
{
    tsk_size_t i;
    if(set->xrange.is_range) {
        *width = TSK_MIN(set->xrange.range.end, *width);
    }
    else {
        for(i = 0; i < set->xrange.array.count; ++i) {
            *width = TSK_MIN(set->xrange.array.values[i], *width);
        }
    }
    if(set->yrange.is_range) {
        *height = TSK_MIN(set->yrange.range.end, *height);
    }
    else {
        for(i = 0; i < set->yrange.array.count; ++i) {
            *height = TSK_MIN(set->yrange.array.values[i], *height);
        }
    }
}

int tmedia_parse_video_imageattr(const char* imageattr, tmedia_pref_video_size_t pref_vs, unsigned* in_width, unsigned* in_height, unsigned* out_width, unsigned* out_height)
{
    tmedia_imageattr_xt attr;
    int ret;
    tsk_size_t i;

    if(!imageattr || !in_width || !in_height || !out_width || !out_height) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    // set default values
    ret = tmedia_video_get_size(pref_vs, in_width, in_height);
    if(ret != 0) {
        TSK_DEBUG_ERROR("tmedia_video_get_size() failed with error code=%d", ret);
        return ret;
    }
    *out_width = *in_width, *out_height = *in_height;

    if((ret = tmedia_imageattr_parse(&attr, imageattr, (tsk_size_t)tsk_strlen(imageattr)))) {
        TSK_DEBUG_ERROR("Failed to parse");
        return 0; // use default values
    }


    for(i = 0; i < attr.send.count; ++i) {
        _imageattr_get_best_size(&attr.send.sets[i], (xyvalue_t*)out_width, (xyvalue_t*)out_height);
    }
    for(i = 0; i < attr.recv.count; ++i) {
        _imageattr_get_best_size(&attr.recv.sets[i], (xyvalue_t*)in_width, (xyvalue_t*)in_height);
    }

    return 0;
}

char* tmedia_get_video_fmtp(tmedia_pref_video_size_t pref_vs)
{
    tsk_size_t i;
    char* fmtp = tsk_null;


    for(i = 0; i < sizeof(fmtp_sizes)/sizeof(fmtp_sizes[0]); ++i) {
        if(fmtp_sizes[i].cif_family && fmtp_sizes[i].pref_vs <= pref_vs) {
            if(!fmtp) {
                tsk_strcat_2(&fmtp, "%s=2", fmtp_sizes[i].name);
            }
            else {
                tsk_strcat_2(&fmtp, ";%s=2", fmtp_sizes[i].name);
            }
        }
    }
    return fmtp;
}

char* tmedia_get_video_imageattr(tmedia_pref_video_size_t pref_vs, unsigned in_width, unsigned in_height, unsigned out_width, unsigned out_height)
{
    unsigned width, height;
    const fmtp_size_t* size_min = &fmtp_sizes[(sizeof(fmtp_sizes) / sizeof(fmtp_sizes[0]))-1];
    char* ret = tsk_null;
    if(tmedia_video_get_size(pref_vs, &width, &height) == 0) {
        tsk_sprintf(&ret, "recv [x=[%u:16:%u],y=[%u:16:%u]] send [x=[%u:16:%u],y=[%u:16:%u]]",
                    size_min->width, in_width, size_min->height, in_height,
                    size_min->width, out_width, size_min->height, out_height);
    }
    return ret;
}


// #retval: 1(best)-31(worst) */
int tmedia_get_video_quality(tmedia_bandwidth_level_t bl)
{
    switch(bl) {
    case tmedia_bl_low:
    default:
        return 13;
    case tmedia_bl_medium:
        return 9;
    case tmedia_bl_hight:
        return 5;
    case tmedia_bl_unrestricted:
        return 1;
    }
}

int32_t tmedia_get_video_bandwidth_kbps(unsigned width, unsigned height, unsigned fps, unsigned motion_rank)
{
    return (int32_t)((width * height * fps * motion_rank * 0.07) / 1024);
}

int32_t tmedia_get_video_bandwidth_kbps_2(unsigned width, unsigned height, unsigned fps)
{
    return tmedia_get_video_bandwidth_kbps(width, height, fps, tmedia_defaults_get_video_motion_rank());
}
int32_t tmedia_get_video_bandwidth_kbps_3()
{
    unsigned width = 3840;
    unsigned height = 2160;
    tmedia_video_get_size(tmedia_defaults_get_pref_video_size(), &width, &height);
    return tmedia_get_video_bandwidth_kbps(width, height, tmedia_defaults_get_video_fps(), tmedia_defaults_get_video_motion_rank());
}

int32_t tmedia_get_video_motion_rank(unsigned width, unsigned height, unsigned fps, int32_t bw_kbps)
{
	if (!width || !height || !fps || bw_kbps <= 0) {
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}
	else {
		float mr = (bw_kbps * 1024.f) / (width * height * fps * 0.07f);
		return (int32_t)(TSK_MAX(1.f, (mr + 0.5f)));
	}	
}
OpenPOWER on IntegriCloud