aboutsummaryrefslogtreecommitdiffstats
path: root/res/res_format_attr_celt.c
blob: de3de8c401debba777e7bffb7a4f5d09ccdd6aad (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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2011, 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 CELT format attribute interface
 *
 * \author David Vossel <dvossel@digium.com>
 */

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "asterisk/module.h"
#include "asterisk/format.h"

/*!
 * \brief CELT attribute structure.
 *
 * \note The only attribute that affects compatibility here is the sample rate.
 */
struct celt_attr {
	unsigned int samplerate;
	unsigned int maxbitrate;
	unsigned int framesize;
};

static enum ast_format_cmp_res celt_cmp(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2)
{
	struct celt_attr *attr1 = (struct celt_attr *) fattr1;
	struct celt_attr *attr2 = (struct celt_attr *) fattr2;

	if (attr1->samplerate == attr2->samplerate) {
		return AST_FORMAT_CMP_EQUAL;
	}
	return AST_FORMAT_CMP_NOT_EQUAL;
}

static int celt_get_val(const struct ast_format_attr *fattr, int key, void *result)
{
	const struct celt_attr *attr = (struct celt_attr *) fattr;
	int *val = result;

	switch (key) {
	case CELT_ATTR_KEY_SAMP_RATE:
		*val = attr->samplerate;
		break;
	case CELT_ATTR_KEY_MAX_BITRATE:
		*val = attr->maxbitrate;
		break;
	case CELT_ATTR_KEY_FRAME_SIZE:
		*val = attr->framesize;
		break;
	default:
		return -1;
		ast_log(LOG_WARNING, "unknown attribute type %d\n", key);
	}
	return 0;
}

static int celt_isset(const struct ast_format_attr *fattr, va_list ap)
{
	enum celt_attr_keys key;
	const struct celt_attr *attr = (struct celt_attr *) fattr;

	for (key = va_arg(ap, int);
		key != AST_FORMAT_ATTR_END;
		key = va_arg(ap, int))
	{
		switch (key) {
		case CELT_ATTR_KEY_SAMP_RATE:
			if (attr->samplerate != (va_arg(ap, int))) {
				return -1;
			}
			break;
		case CELT_ATTR_KEY_MAX_BITRATE:
			if (attr->maxbitrate != (va_arg(ap, int))) {
				return -1;
			}
			break;
		case CELT_ATTR_KEY_FRAME_SIZE:
			if (attr->framesize != (va_arg(ap, int))) {
				return -1;
			}
			break;
		default:
			return -1;
			ast_log(LOG_WARNING, "unknown attribute type %d\n", key);
		}
	}
	return 0;
}
static int celt_getjoint(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2, struct ast_format_attr *result)
{
	struct celt_attr *attr1 = (struct celt_attr *) fattr1;
	struct celt_attr *attr2 = (struct celt_attr *) fattr2;
	struct celt_attr *attr_res = (struct celt_attr *) result;

	/* sample rate is the only attribute that has any bearing on if joint capabilities exist or not */
	if (attr1->samplerate != attr2->samplerate) {
		return -1;
	}
	/* either would work, they are guaranteed the same at this point. */
	attr_res->samplerate = attr1->samplerate;
	/* Take the lowest max bitrate */
	attr_res->maxbitrate = MIN(attr1->maxbitrate, attr2->maxbitrate);

	attr_res->framesize = attr2->framesize; /* TODO figure out what joint framesize means */
	return 0;
}

static void celt_set(struct ast_format_attr *fattr, va_list ap)
{
	enum celt_attr_keys key;
	struct celt_attr *attr = (struct celt_attr *) fattr;

	for (key = va_arg(ap, int);
		key != AST_FORMAT_ATTR_END;
		key = va_arg(ap, int))
	{
		switch (key) {
		case CELT_ATTR_KEY_SAMP_RATE:
			attr->samplerate = (va_arg(ap, int));
			break;
		case CELT_ATTR_KEY_MAX_BITRATE:
			attr->maxbitrate = (va_arg(ap, int));
			break;
		case CELT_ATTR_KEY_FRAME_SIZE:
			attr->framesize = (va_arg(ap, int));
			break;
		default:
			ast_log(LOG_WARNING, "unknown attribute type %d\n", key);
		}
	}
}

static struct ast_format_attr_interface celt_interface = {
	.id = AST_FORMAT_CELT,
	.format_attr_cmp = celt_cmp,
	.format_attr_get_joint = celt_getjoint,
	.format_attr_set = celt_set,
	.format_attr_isset = celt_isset,
	.format_attr_get_val = celt_get_val,
};

static int load_module(void)
{
	if (ast_format_attr_reg_interface(&celt_interface)) {
		return AST_MODULE_LOAD_DECLINE;
	}

	return AST_MODULE_LOAD_SUCCESS;
}

static int unload_module(void)
{
	ast_format_attr_unreg_interface(&celt_interface);
	return 0;
}

AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "CELT Format Attribute Module",
	.load = load_module,
	.unload = unload_module,
	.load_pri = AST_MODPRI_CHANNEL_DEPEND,
);