]> sjero.net Git - linphone/blob - coreapi/sal.c
Merge branch 'master' of git.linphone.org:linphone-private
[linphone] / coreapi / sal.c
1 /*
2 linphone
3 Copyright (C) 2010  Simon MORLAT (simon.morlat@free.fr)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20 /** 
21  This header files defines the Signaling Abstraction Layer.
22  The purpose of this layer is too allow experiment different call signaling 
23  protocols and implementations under linphone, for example SIP, JINGLE...
24 **/
25
26 #include "sal.h"
27
28 SalMediaDescription *sal_media_description_new(){
29         SalMediaDescription *md=ms_new0(SalMediaDescription,1);
30         md->refcount=1;
31         return md;
32 }
33
34 static void sal_media_description_destroy(SalMediaDescription *md){
35         int i;
36         for(i=0;i<SAL_MEDIA_DESCRIPTION_MAX_STREAMS;i++){
37                 ms_list_for_each(md->streams[i].payloads,(void (*)(void *))payload_type_destroy);
38                 ms_list_free(md->streams[i].payloads);
39                 md->streams[i].payloads=NULL;
40         }
41         ms_free(md);
42 }
43
44 void sal_media_description_ref(SalMediaDescription *md){
45         md->refcount++;
46 }
47
48 void sal_media_description_unref(SalMediaDescription *md){
49         md->refcount--;
50         if (md->refcount==0){
51                 sal_media_description_destroy (md);
52         }
53 }
54
55 SalStreamDescription *sal_media_description_find_stream(SalMediaDescription *md,
56     SalMediaProto proto, SalStreamType type){
57         int i;
58         for(i=0;i<md->nstreams;++i){
59                 SalStreamDescription *ss=&md->streams[i];
60                 if (ss->proto==proto && ss->type==type) return ss;
61         }
62         return NULL;
63 }
64
65 bool_t sal_media_description_empty(const SalMediaDescription *md){
66         int i;
67         for(i=0;i<md->nstreams;++i){
68                 const SalStreamDescription *ss=&md->streams[i];
69                 if (ss->port!=0) return FALSE;
70         }
71         return TRUE;
72 }
73
74 void sal_media_description_set_dir(SalMediaDescription *md, SalStreamDir stream_dir){
75         int i;
76         for(i=0;i<md->nstreams;++i){
77                 SalStreamDescription *ss=&md->streams[i];
78                 ss->dir=stream_dir;
79         }
80 }
81
82 bool_t sal_media_description_has_dir(const SalMediaDescription *md, SalStreamDir stream_dir){
83         int i;
84         bool_t found=FALSE;
85
86         /* we are looking for at least one stream with requested direction, inactive streams are ignored*/
87         for(i=0;i<md->nstreams;++i){
88                 const SalStreamDescription *ss=&md->streams[i];
89                 if (ss->dir==stream_dir) found=TRUE;
90                 else{
91                         if (ss->dir!=SalStreamInactive) return FALSE;
92                 }
93         }
94         return found;
95 }
96
97 static void assign_string(char **str, const char *arg){
98         if (*str){
99                 ms_free(*str);
100                 *str=NULL;
101         }
102         if (arg)
103                 *str=ms_strdup(arg);
104 }
105
106 void sal_op_set_contact(SalOp *op, const char *contact){
107         assign_string(&((SalOpBase*)op)->contact,contact);
108 }
109
110 void sal_op_set_route(SalOp *op, const char *route){
111         assign_string(&((SalOpBase*)op)->route,route);
112 }
113
114 void sal_op_set_from(SalOp *op, const char *from){
115         assign_string(&((SalOpBase*)op)->from,from);
116 }
117
118 void sal_op_set_to(SalOp *op, const char *to){
119         assign_string(&((SalOpBase*)op)->to,to);
120 }
121
122 void sal_op_set_user_pointer(SalOp *op, void *up){
123         ((SalOpBase*)op)->user_pointer=up;
124 }
125
126 Sal *sal_op_get_sal(const SalOp *op){
127         return ((SalOpBase*)op)->root;
128 }
129
130 const char *sal_op_get_from(const SalOp *op){
131         return ((SalOpBase*)op)->from;
132 }
133
134 const char *sal_op_get_to(const SalOp *op){
135         return ((SalOpBase*)op)->to;
136 }
137
138 const char *sal_op_get_contact(const SalOp *op){
139         return ((SalOpBase*)op)->contact;
140 }
141
142 const char *sal_op_get_route(const SalOp *op){
143         return ((SalOpBase*)op)->route;
144 }
145
146 const char *sal_op_get_remote_ua(const SalOp *op){
147         return ((SalOpBase*)op)->remote_ua;
148 }
149
150 void *sal_op_get_user_pointer(const SalOp *op){
151         return ((SalOpBase*)op)->user_pointer;
152 }
153
154 const char *sal_op_get_proxy(const SalOp *op){
155         return ((SalOpBase*)op)->route;
156 }
157
158 const char *sal_op_get_network_origin(const SalOp *op){
159         return ((SalOpBase*)op)->origin;
160 }
161
162 void __sal_op_init(SalOp *b, Sal *sal){
163         memset(b,0,sizeof(SalOpBase));
164         ((SalOpBase*)b)->root=sal;
165 }
166
167 void __sal_op_set_network_origin(SalOp *op, const char *origin){
168         assign_string(&((SalOpBase*)op)->origin,origin);
169 }
170
171
172 void __sal_op_free(SalOp *op){
173         SalOpBase *b=(SalOpBase *)op;
174         if (b->from) {
175                 ms_free(b->from);
176                 b->from=NULL;
177         }
178         if (b->to) {
179                 ms_free(b->to);
180                 b->to=NULL;
181         }
182         if (b->route) {
183                 ms_free(b->route);
184                 b->route=NULL;
185         }
186         if (b->contact) {
187                 ms_free(b->contact);
188                 b->contact=NULL;
189         }
190         if (b->origin){
191                 ms_free(b->origin);
192                 b->origin=NULL;
193         }
194         if (b->remote_ua){
195                 ms_free(b->remote_ua);
196                 b->remote_ua=NULL;
197         }
198         if (b->local_media)
199                 sal_media_description_unref(b->local_media);
200         if (b->remote_media)
201                 sal_media_description_unref(b->remote_media);
202         ms_free(op);
203 }