aboutsummaryrefslogtreecommitdiffstats
path: root/main/format_cap.c
blob: 3ef0e74d344001a147d870c89b386b4a57a0adb3 (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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2010, Digium, Inc.
 *
 * David Vossel <dvossel@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*!
 * \file
 * \brief Format Capability API
 *
 * \author David Vossel <dvossel@digium.com>
 */

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$");

#include "asterisk/_private.h"
#include "asterisk/version.h"
#include "asterisk/format.h"
#include "asterisk/format_cap.h"
#include "asterisk/frame.h"
#include "asterisk/astobj2.h"
#include "asterisk/utils.h"


struct ast_format_cap {
	/* The capabilities structure is just an ao2 container of ast_formats */
	struct ao2_container *formats;
	struct ao2_iterator it;
	int nolock;
};

/*! format exists within capabilities structure if it is identical to
 * another format, or if the format is a proper subset of another format. */
static int cmp_cb(void *obj, void *arg, int flags)
{
	struct ast_format *format1 = arg;
	struct ast_format *format2 = obj;
	enum ast_format_cmp_res res = ast_format_cmp(format1, format2);

	return ((res == AST_FORMAT_CMP_EQUAL) ||
			(res == AST_FORMAT_CMP_SUBSET)) ?
				CMP_MATCH | CMP_STOP :
				0;
}

static int hash_cb(const void *obj, const int flags)
{
	const struct ast_format *format = obj;
	return format->id;
}

static struct ast_format_cap *cap_alloc_helper(int nolock)
{
	struct ast_format_cap *cap = ast_calloc(1, sizeof(*cap));

	if (!cap) {
		return NULL;
	}
	cap->nolock = nolock ? OBJ_NOLOCK : 0;
	if (!(cap->formats = ao2_container_alloc(283, hash_cb, cmp_cb))) {
		ast_free(cap);
		return NULL;
	}

	return cap;
}

struct ast_format_cap *ast_format_cap_alloc_nolock(void)
{
	return cap_alloc_helper(1);
}

struct ast_format_cap *ast_format_cap_alloc(void)
{
	return cap_alloc_helper(0);
}

void *ast_format_cap_destroy(struct ast_format_cap *cap)
{
	if (!cap) {
		return NULL;
	}
	ao2_ref(cap->formats, -1);
	ast_free(cap);
	return NULL;
}

void ast_format_cap_add(struct ast_format_cap *cap, const struct ast_format *format)
{
	struct ast_format *fnew;

	if (!format || !format->id) {
		return;
	}
	if (!(fnew = ao2_alloc(sizeof(struct ast_format), NULL))) {
		return;
	}
	ast_format_copy(fnew, format);
	if (cap->nolock) {
		ao2_link_nolock(cap->formats, fnew);
	} else {
		ao2_link(cap->formats, fnew);
	}
	ao2_ref(fnew, -1);
}

void ast_format_cap_add_all_by_type(struct ast_format_cap *cap, enum ast_format_type type)
{
	int x;
	size_t f_len = 0;
	const struct ast_format_list *f_list = ast_format_list_get(&f_len);

	for (x = 0; x < f_len; x++) {
		if (AST_FORMAT_GET_TYPE(f_list[x].format.id) == type) {
			ast_format_cap_add(cap, &f_list[x].format);
		}
	}
	ast_format_list_destroy(f_list);
}

void ast_format_cap_add_all(struct ast_format_cap *cap)
{
	int x;
	size_t f_len = 0;
	const struct ast_format_list *f_list = ast_format_list_get(&f_len);

	for (x = 0; x < f_len; x++) {
		ast_format_cap_add(cap, &f_list[x].format);
	}
	ast_format_list_destroy(f_list);
}

static int append_cb(void *obj, void *arg, int flag)
{
	struct ast_format_cap *result = (struct ast_format_cap *) arg;
	struct ast_format *format = (struct ast_format *) obj;

	if (!ast_format_cap_iscompatible(result, format)) {
		ast_format_cap_add(result, format);
	}

	return 0;
}

void ast_format_cap_append(struct ast_format_cap *dst, const struct ast_format_cap *src)
{
	ao2_callback(src->formats, OBJ_NODATA | src->nolock, append_cb, dst);
}

static int copy_cb(void *obj, void *arg, int flag)
{
	struct ast_format_cap *result = (struct ast_format_cap *) arg;
	struct ast_format *format = (struct ast_format *) obj;

	ast_format_cap_add(result, format);
	return 0;
}

void ast_format_cap_copy(struct ast_format_cap *dst, const struct ast_format_cap *src)
{
	ast_format_cap_remove_all(dst);
	ao2_callback(src->formats, OBJ_NODATA | src->nolock, copy_cb, dst);
}

struct ast_format_cap *ast_format_cap_dup(const struct ast_format_cap *cap)
{
	struct ast_format_cap *dst;
	if (cap->nolock) {
		dst = ast_format_cap_alloc_nolock();
	} else {
		dst = ast_format_cap_alloc();
	}
	if (!dst) {
		return NULL;
	}
	ao2_callback(cap->formats, OBJ_NODATA | cap->nolock, copy_cb, dst);
	return dst;
}

int ast_format_cap_is_empty(const struct ast_format_cap *cap)
{
	if (!cap) {
		return 1;
	}
	return ao2_container_count(cap->formats) == 0 ? 1 : 0;
}

static int find_exact_cb(void *obj, void *arg, int flag)
{
	struct ast_format *format1 = (struct ast_format *) arg;
	struct ast_format *format2 = (struct ast_format *) obj;

	return (ast_format_cmp(format1, format2) == AST_FORMAT_CMP_EQUAL) ? CMP_MATCH : 0;
}

int ast_format_cap_remove(struct ast_format_cap *cap, struct ast_format *format)
{
	struct ast_format *fremove;
	fremove = ao2_callback(cap->formats, OBJ_POINTER | OBJ_UNLINK | cap->nolock, find_exact_cb, format);

	if (fremove) {
		ao2_ref(fremove, -1);
		return 0;
	}

	return -1;
}

struct multiple_by_id_data {
	struct ast_format *format;
	int match_found;
};

static int multiple_by_id_cb(void *obj, void *arg, int flag)
{
	struct multiple_by_id_data *data = arg;
	struct ast_format *format = obj;
	int res;

	res = (format->id == data->format->id) ? CMP_MATCH : 0;
	if (res) {
		data->match_found = 1;
	}

	return res;
}

int ast_format_cap_remove_byid(struct ast_format_cap *cap, enum ast_format_id id)
{
	struct ast_format format = {
		.id = id,
	};
	struct multiple_by_id_data data = {
		.format = &format,
		.match_found = 0,
	};

	ao2_callback(cap->formats,
		OBJ_NODATA | cap->nolock | OBJ_MULTIPLE | OBJ_UNLINK,
		multiple_by_id_cb,
		&data);

	/* match_found will be set if at least one item was removed */
	if (data.match_found) {
		return 0;
	}

	return -1;
}

static int multiple_by_type_cb(void *obj, void *arg, int flag)
{
	int *type = arg;
	struct ast_format *format = obj;
	return ((AST_FORMAT_GET_TYPE(format->id)) == *type) ? CMP_MATCH : 0;
}

void ast_format_cap_remove_bytype(struct ast_format_cap *cap, enum ast_format_type type)
{
	ao2_callback(cap->formats,
		OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE | cap->nolock,
		multiple_by_type_cb,
		&type);
}

void ast_format_cap_remove_all(struct ast_format_cap *cap)
{
	ao2_callback(cap->formats, OBJ_NODATA | cap->nolock | OBJ_MULTIPLE | OBJ_UNLINK, NULL, NULL);
}

void ast_format_cap_set(struct ast_format_cap *cap, struct ast_format *format)
{
	ast_format_cap_remove_all(cap);
	ast_format_cap_add(cap, format);
}

int ast_format_cap_get_compatible_format(const struct ast_format_cap *cap, const struct ast_format *format, struct ast_format *result)
{
	struct ast_format *f;
	struct ast_format_cap *tmp_cap = (struct ast_format_cap *) cap;
	f = ao2_find(tmp_cap->formats, (struct ast_format *) format, OBJ_POINTER | tmp_cap->nolock);

	if (f) {
		ast_format_copy(result, f);
		ao2_ref(f, -1);
		return 1;
	}
	ast_format_clear(result);
	return 0;
}

int ast_format_cap_iscompatible(const struct ast_format_cap *cap, const struct ast_format *format)
{
	struct ast_format *f;
	struct ast_format_cap *tmp_cap = (struct ast_format_cap *) cap;
	f = ao2_find(tmp_cap->formats, (struct ast_format *) format, OBJ_POINTER | tmp_cap->nolock);

	if (f) {
		ao2_ref(f, -1);
		return 1;
	}

	return 0;
}

struct byid_data {
	struct ast_format *result;
	enum ast_format_id id;
};
static int find_best_byid_cb(void *obj, void *arg, int flag)
{
	struct ast_format *format = obj;
	struct byid_data *data = arg;

	if (data->id != format->id) {
		return 0;
	}
	if (!data->result->id || (ast_format_rate(data->result) < ast_format_rate(format))) {
		ast_format_copy(data->result, format);
	}
	return 0;
}

int ast_format_cap_best_byid(const struct ast_format_cap *cap, enum ast_format_id id, struct ast_format *result)
{
	struct byid_data data;
	data.result = result;
	data.id = id;

	ast_format_clear(result);
	ao2_callback(cap->formats,
		OBJ_MULTIPLE | OBJ_NODATA | cap->nolock,
		find_best_byid_cb,
		&data);
	return result->id ? 1 : 0;
}

/*! \internal
 * \brief this struct is just used for the ast_format_cap_joint function so we can provide
 * both a format and a result ast_format_cap structure as arguments to the find_joint_cb
 * ao2 callback function.
 */
struct find_joint_data {
	/*! format to compare to for joint capabilities */
	struct ast_format *format;
	/*! if joint formmat exists with above format, add it to the result container */
	struct ast_format_cap *joint_cap;
	int joint_found;
};

static int find_joint_cb(void *obj, void *arg, int flag)
{
	struct ast_format *format = obj;
	struct find_joint_data *data = arg;

	struct ast_format tmp = { 0, };
	if (!ast_format_joint(format, data->format, &tmp)) {
		if (data->joint_cap) {
			ast_format_cap_add(data->joint_cap, &tmp);
		}
		data->joint_found++;
	}

	return 0;
}

int ast_format_cap_has_joint(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
{
	struct ao2_iterator it;
	struct ast_format *tmp;
	struct find_joint_data data = {
		.joint_found = 0,
		.joint_cap = NULL,
	};

	it = ao2_iterator_init(cap1->formats, cap1->nolock ? AO2_ITERATOR_DONTLOCK : 0);
	while ((tmp = ao2_iterator_next(&it))) {
		data.format = tmp;
		ao2_callback(cap2->formats,
			OBJ_MULTIPLE | OBJ_NODATA | cap2->nolock,
			find_joint_cb,
			&data);
		ao2_ref(tmp, -1);
	}
	ao2_iterator_destroy(&it);

	return data.joint_found ? 1 : 0;
}

int ast_format_cap_identical(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
{
	struct ao2_iterator it;
	struct ast_format *tmp;

	if (ao2_container_count(cap1->formats) != ao2_container_count(cap2->formats)) {
		return 0; /* if they are not the same size, they are not identical */
	}

	it = ao2_iterator_init(cap1->formats, cap1->nolock ? AO2_ITERATOR_DONTLOCK : 0);
	while ((tmp = ao2_iterator_next(&it))) {
		if (!ast_format_cap_iscompatible(cap2, tmp)) {
			ao2_ref(tmp, -1);
			ao2_iterator_destroy(&it);
			return 0;
		}
		ao2_ref(tmp, -1);
	}
	ao2_iterator_destroy(&it);

	return 1;
}

struct ast_format_cap *ast_format_cap_joint(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
{
	struct ao2_iterator it;
	struct ast_format_cap *result = ast_format_cap_alloc_nolock();
	struct ast_format *tmp;
	struct find_joint_data data = {
		.joint_found = 0,
		.joint_cap = result,
	};
	if (!result) {
		return NULL;
	}

	it = ao2_iterator_init(cap1->formats, cap1->nolock ? AO2_ITERATOR_DONTLOCK : 0);
	while ((tmp = ao2_iterator_next(&it))) {
		data.format = tmp;
		ao2_callback(cap2->formats,
			OBJ_MULTIPLE | OBJ_NODATA | cap2->nolock,
			find_joint_cb,
			&data);
		ao2_ref(tmp, -1);
	}
	ao2_iterator_destroy(&it);

	if (ao2_container_count(result->formats)) {
		return result;
	}

	result = ast_format_cap_destroy(result);
	return NULL;
}

static int joint_copy_helper(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2, struct ast_format_cap *result, int append)
{
	struct ao2_iterator it;
	struct ast_format *tmp;
	struct find_joint_data data = {
		.joint_cap = result,
		.joint_found = 0,
	};
	if (!append) {
		ast_format_cap_remove_all(result);
	}
	it = ao2_iterator_init(cap1->formats, cap2->nolock ? AO2_ITERATOR_DONTLOCK : 0);
	while ((tmp = ao2_iterator_next(&it))) {
		data.format = tmp;
		ao2_callback(cap2->formats,
			OBJ_MULTIPLE | OBJ_NODATA | cap2->nolock,
			find_joint_cb,
			&data);
		ao2_ref(tmp, -1);
	}
	ao2_iterator_destroy(&it);

	return ao2_container_count(result->formats) ? 1 : 0;
}

int ast_format_cap_joint_append(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2, struct ast_format_cap *result)
{
	return joint_copy_helper(cap1, cap2, result, 1);
}

int ast_format_cap_joint_copy(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2, struct ast_format_cap *result)
{
	return joint_copy_helper(cap1, cap2, result, 0);
}

struct ast_format_cap *ast_format_cap_get_type(const struct ast_format_cap *cap, enum ast_format_type ftype)
{
	struct ao2_iterator it;
	struct ast_format_cap *result = ast_format_cap_alloc_nolock();
	struct ast_format *tmp;

	if (!result) {
		return NULL;
	}

	/* for each format in cap1, see if that format is
	 * compatible with cap2. If so copy it to the result */
	it = ao2_iterator_init(cap->formats, cap->nolock ? AO2_ITERATOR_DONTLOCK : 0);
	while ((tmp = ao2_iterator_next(&it))) {
		if (AST_FORMAT_GET_TYPE(tmp->id) == ftype) {
			/* copy format */
			ast_format_cap_add(result, tmp);
		}
		ao2_ref(tmp, -1);
	}
	ao2_iterator_destroy(&it);

	if (ao2_container_count(result->formats)) {
		return result;
	}
	result = ast_format_cap_destroy(result);

	/* Remember to always free the NULL before returning it. */
	ast_free(NULL);
	return NULL;
}


int ast_format_cap_has_type(const struct ast_format_cap *cap, enum ast_format_type type)
{
	struct ao2_iterator it;
	struct ast_format *tmp;

	it = ao2_iterator_init(cap->formats, cap->nolock ? AO2_ITERATOR_DONTLOCK : 0);
	while ((tmp = ao2_iterator_next(&it))) {
		if (AST_FORMAT_GET_TYPE(tmp->id) == type) {
			ao2_ref(tmp, -1);
			ao2_iterator_destroy(&it);
			return 1;
		}
		ao2_ref(tmp, -1);
	}
	ao2_iterator_destroy(&it);

	return 0;
}

void ast_format_cap_iter_start(struct ast_format_cap *cap)
{
	if (!cap->nolock) {
		ao2_lock(cap->formats);
	}
	cap->it = ao2_iterator_init(cap->formats, cap->nolock ? AO2_ITERATOR_DONTLOCK : 0);
}

void ast_format_cap_iter_end(struct ast_format_cap *cap)
{
	ao2_iterator_destroy(&cap->it);
	if (!cap->nolock) {
		ao2_unlock(cap->formats);
	}
}

int ast_format_cap_iter_next(struct ast_format_cap *cap, struct ast_format *format)
{
	struct ast_format *tmp = ao2_iterator_next(&cap->it);

	if (!tmp) {
		return -1;
	}
	ast_format_copy(format, tmp);
	ao2_ref(tmp, -1);

	return 0;
}

char *ast_getformatname_multiple(char *buf, size_t size, struct ast_format_cap *cap)
{
	int x;
	unsigned len;
	char *start, *end = buf;
	struct ast_format tmp_fmt;
	size_t f_len;
	const struct ast_format_list *f_list = ast_format_list_get(&f_len);

	if (!size) {
		f_list = ast_format_list_destroy(f_list);
		return buf;
	}
	snprintf(end, size, "(");
	len = strlen(end);
	end += len;
	size -= len;
	start = end;
	for (x = 0; x < f_len; x++) {
		ast_format_copy(&tmp_fmt, &f_list[x].format);
		if (ast_format_cap_iscompatible(cap, &tmp_fmt)) {
			snprintf(end, size, "%s|", f_list[x].name);
			len = strlen(end);
			end += len;
			size -= len;
		}
	}
	if (start == end) {
		ast_copy_string(start, "nothing)", size);
	} else if (size > 1) {
		*(end - 1) = ')';
	}
	f_list = ast_format_list_destroy(f_list);
	return buf;
}

uint64_t ast_format_cap_to_old_bitfield(const struct ast_format_cap *cap)
{
	uint64_t res = 0;
	struct ao2_iterator it;
	struct ast_format *tmp;

	it = ao2_iterator_init(cap->formats, cap->nolock ? AO2_ITERATOR_DONTLOCK : 0);
	while ((tmp = ao2_iterator_next(&it))) {
		res |= ast_format_to_old_bitfield(tmp);
		ao2_ref(tmp, -1);
	}
	ao2_iterator_destroy(&it);
	return res;
}

void ast_format_cap_from_old_bitfield(struct ast_format_cap *dst, uint64_t src)
{
	uint64_t tmp = 0;
	int x;
	struct ast_format tmp_format = { 0, };

	ast_format_cap_remove_all(dst);
	for (x = 0; x < 64; x++) {
		tmp = (1ULL << x);
		if (tmp & src) {
			ast_format_cap_add(dst, ast_format_from_old_bitfield(&tmp_format, tmp));
		}
	}
}