core: Fix unused variable error in handle_show_sysinfo.
[asterisk/asterisk.git] / main / sdp_translator.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2017, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 #include "asterisk.h"
20 #include "asterisk/sdp_options.h"
21 #include "asterisk/sdp_translator.h"
22 #include "asterisk/logger.h"
23 #include "asterisk/utils.h"
24 #include "asterisk/lock.h"
25
26 AST_RWLOCK_DEFINE_STATIC(registered_ops_lock);
27 static struct ast_sdp_translator_ops *registered_ops[AST_SDP_IMPL_END];
28
29 int ast_sdp_register_translator(struct ast_sdp_translator_ops *ops)
30 {
31         SCOPED_WRLOCK(lock, &registered_ops_lock);
32
33         if (ops->repr >= AST_SDP_IMPL_END) {
34                 ast_log(LOG_ERROR, "SDP translator has unrecognized representation\n");
35                 return -1;
36         }
37
38         if (registered_ops[ops->repr] != NULL) {
39                 ast_log(LOG_ERROR, "SDP_translator with this representation already registered\n");
40                 return -1;
41         }
42
43         registered_ops[ops->repr] = ops;
44         ast_log(LOG_NOTICE, "Placed ops %p at slot %d\n", ops, ops->repr);
45         return 0;
46 }
47
48 void ast_sdp_unregister_translator(struct ast_sdp_translator_ops *ops)
49 {
50         SCOPED_WRLOCK(lock, &registered_ops_lock);
51
52         if (ops->repr >= AST_SDP_IMPL_END) {
53                 return;
54         }
55
56         registered_ops[ops->repr] = NULL;
57 }
58
59 struct ast_sdp_translator *ast_sdp_translator_new(enum ast_sdp_options_impl repr)
60 {
61         struct ast_sdp_translator *translator;
62         SCOPED_RDLOCK(lock, &registered_ops_lock);
63
64         if (registered_ops[repr] == NULL) {
65                 ast_log(LOG_NOTICE, "No registered SDP translator with representation %d\n", repr);
66                 return NULL;
67         }
68
69         translator = ast_calloc(1, sizeof(*translator));
70         if (!translator) {
71                 return NULL;
72         }
73
74         translator->ops = registered_ops[repr];
75
76         translator->translator_priv = translator->ops->translator_new();
77         if (!translator->translator_priv) {
78                 ast_free(translator);
79                 return NULL;
80         }
81
82         return translator;
83 }
84
85 void ast_sdp_translator_free(struct ast_sdp_translator *translator)
86 {
87         if (!translator) {
88                 return;
89         }
90         translator->ops->translator_free(translator->translator_priv);
91         ast_free(translator);
92 }
93
94 struct ast_sdp *ast_sdp_translator_to_sdp(struct ast_sdp_translator *translator,
95         const void *native_sdp)
96 {
97         return translator->ops->to_sdp(native_sdp, translator->translator_priv);
98 }
99
100 const void *ast_sdp_translator_from_sdp(struct ast_sdp_translator *translator,
101         const struct ast_sdp *ast_sdp)
102 {
103         return translator->ops->from_sdp(ast_sdp, translator->translator_priv);
104 }