]> sjero.net Git - linphone/blob - coreapi/conference.c
14867f72ac14fe8bbbe6909459dce427a39c6871
[linphone] / coreapi / conference.c
1 /***************************************************************************
2  *            conference.c
3  *
4  *  Mon Sep 12, 2011
5  *  Copyright  2011  Belledonne Communications
6  *  Author: Simon Morlat
7  *  Email simon dot morlat at linphone dot org
8  ****************************************************************************/
9
10 /*
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25  
26 #include "private.h"
27
28 #include "mediastreamer2/msvolume.h"
29
30 static void conference_check_init(LinphoneConference *ctx){
31         if (ctx->conf==NULL){
32                 ctx->conf=ms_audio_conference_new();
33         }
34 }
35
36 static void remove_local_endpoint(LinphoneConference *ctx){
37         if (ctx->local_endpoint){
38                 ms_audio_conference_remove_member(ctx->conf,ctx->local_endpoint);
39                 ms_audio_endpoint_release_from_stream(ctx->local_endpoint);
40                 ctx->local_endpoint=NULL;
41                 audio_stream_stop(ctx->local_participant);
42                 ctx->local_participant=NULL;
43         }
44 }
45
46 static void conference_check_uninit(LinphoneConference *ctx){
47         if (ctx->conf){
48                 if (ctx->conf->nmembers==0){
49                         remove_local_endpoint(ctx);
50                         ms_audio_conference_destroy(ctx->conf);
51                         ctx->conf=NULL;
52                 }
53         }
54 }
55
56
57 void linphone_call_add_to_conf(LinphoneCall *call){
58         LinphoneCore *lc=call->core;
59         LinphoneConference *conf=&lc->conf_ctx;
60         MSAudioEndpoint *ep;
61         ep=ms_audio_endpoint_get_from_stream(call->audiostream,TRUE);
62         ms_audio_conference_add_member(conf->conf,ep);
63         call->endpoint=ep;
64 }
65
66 void linphone_call_remove_from_conf(LinphoneCall *call){
67         LinphoneCore *lc=call->core;
68         LinphoneConference *conf=&lc->conf_ctx;
69         
70         ms_audio_conference_remove_member(conf->conf,call->endpoint);
71         ms_audio_endpoint_release_from_stream(call->endpoint);
72         call->endpoint=NULL;
73         conference_check_uninit(conf);
74 }
75
76 static void add_local_endpoint(LinphoneConference *conf,LinphoneCore *lc){
77         /*create a dummy audiostream in order to extract the local part of it */
78         /* network address and ports have no meaning and are not used here. */
79         AudioStream *st=audio_stream_new(65000,FALSE);
80         MSSndCard *playcard=lc->sound_conf.lsd_card ? 
81                         lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
82         MSSndCard *captcard=lc->sound_conf.capt_sndcard;
83         
84         audio_stream_start_full(st, &av_profile,
85                                 "127.0.0.1",
86                                 65000,
87                                 65001,
88                                 0,
89                                 40,
90                                 NULL,
91                                 NULL,
92                                 playcard,
93                                 captcard,
94                                 linphone_core_echo_cancellation_enabled(lc)
95                                 );
96         _post_configure_audio_stream(st,lc,FALSE);
97         conf->local_participant=st;
98         conf->local_endpoint=ms_audio_endpoint_get_from_stream(st,FALSE);
99         ms_audio_conference_add_member(conf->conf,conf->local_endpoint);
100 }
101
102 float linphone_core_get_conference_local_input_volume(LinphoneCore *lc){
103         LinphoneConference *conf=&lc->conf_ctx;
104         AudioStream *st=conf->local_participant;
105         if (st && st->volsend){
106                 float vol=0;
107                 ms_filter_call_method(st->volsend,MS_VOLUME_GET,&vol);
108                 return vol;
109                 
110         }
111         return LINPHONE_VOLUME_DB_LOWEST;
112 }
113
114 int linphone_core_add_to_conference(LinphoneCore *lc, LinphoneCall *call){
115         LinphoneCallParams params;
116         LinphoneConference *conf=&lc->conf_ctx;
117         
118         if (call->current_params.in_conference){
119                 ms_error("Already in conference");
120                 return -1;
121         }
122         conference_check_init(&lc->conf_ctx);
123         call->params.in_conference=TRUE;
124         call->params.has_video=FALSE;
125         params=call->params;
126         if (call->state==LinphoneCallPaused)
127                 linphone_core_resume_call(lc,call);
128         else if (call->state==LinphoneCallStreamsRunning){
129                 /*this will trigger a reINVITE that will later redraw the streams */
130                 if (call->audiostream || call->videostream)
131                         linphone_call_stop_media_streams (call); /*free the audio & video local resources*/
132                 linphone_core_update_call(lc,call,&params);
133                 add_local_endpoint(conf,lc);
134         }else{
135                 ms_error("Call is in state %s, it cannot be added to the conference.",linphone_call_state_to_string(call->state));
136                 return -1;
137         }
138         return 0;
139 }
140
141 int linphone_core_remove_from_conference(LinphoneCore *lc, LinphoneCall *call){
142         if (!call->current_params.in_conference){
143                 if (call->params.in_conference){
144                         ms_warning("Not (yet) in conference, be patient");
145                         return -1;
146                 }else{
147                         ms_error("Not in a conference.");
148                         return -1;
149                 }
150         }
151         call->params.in_conference=FALSE;
152         return linphone_core_pause_call(lc,call);
153 }
154
155 bool_t linphone_core_is_in_conference(const LinphoneCore *lc){
156         return lc->conf_ctx.local_participant!=NULL;
157 }
158
159 int linphone_core_leave_conference(LinphoneCore *lc){
160         LinphoneConference *conf=&lc->conf_ctx;
161         if (linphone_core_is_in_conference(lc))
162                 remove_local_endpoint(conf);
163         return 0;
164 }
165
166
167 int linphone_core_enter_conference(LinphoneCore *lc){
168         LinphoneConference *conf=&lc->conf_ctx;
169         if (conf->local_participant==NULL) add_local_endpoint(conf,lc);
170         return 0;
171 }
172
173 int linphone_core_add_all_to_conference(LinphoneCore *lc) {
174         MSList *calls=lc->calls;
175         while (calls) {
176                 LinphoneCall *call=(LinphoneCall*)calls->data;
177                 calls=calls->next;
178                 if (!call->current_params.in_conference) {
179                         linphone_core_add_to_conference(lc, call);
180                 }
181         }
182         return 0;
183 }
184
185 int linphone_core_terminate_conference(LinphoneCore *lc) {
186         MSList *calls=lc->calls;
187         while (calls) {
188                 LinphoneCall *call=(LinphoneCall*)calls->data;
189                 calls=calls->next;
190                 if (call->current_params.in_conference) {
191                         linphone_core_terminate_call(lc, call);
192                 }
193         }
194         return 0;
195 }
196
197 int linphone_core_get_conference_size(LinphoneCore *lc) {
198         return ms_audio_conference_size(lc->conf_ctx.conf);
199 }