1 /***************************************************************************
5 * Copyright 2011 Belledonne Communications
7 * Email simon dot morlat at linphone dot org
8 ****************************************************************************/
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.
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.
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.
29 #include "mediastreamer2/msvolume.h"
32 * @addtogroup conferencing
37 static int convert_conference_to_call(LinphoneCore *lc);
39 static void conference_check_init(LinphoneConference *ctx, int samplerate){
41 MSAudioConferenceParams params;
42 params.samplerate=samplerate;
43 ctx->conf=ms_audio_conference_new(¶ms);
47 static void remove_local_endpoint(LinphoneConference *ctx){
48 if (ctx->local_endpoint){
49 ms_audio_conference_remove_member(ctx->conf,ctx->local_endpoint);
50 ms_audio_endpoint_release_from_stream(ctx->local_endpoint);
51 ctx->local_endpoint=NULL;
52 audio_stream_stop(ctx->local_participant);
53 ctx->local_participant=NULL;
54 rtp_profile_destroy(ctx->local_dummy_profile);
58 static int remote_participants_count(LinphoneConference *ctx) {
59 if (!ctx->conf || ms_audio_conference_get_size(ctx->conf)==0) return 0;
60 if (!ctx->local_participant) return ms_audio_conference_get_size(ctx->conf);
61 return ms_audio_conference_get_size(ctx->conf) -1;
64 void linphone_core_conference_check_uninit(LinphoneCore *lc){
65 LinphoneConference *ctx=&lc->conf_ctx;
67 ms_message("conference_check_uninit(): nmembers=%i",ms_audio_conference_get_size(ctx->conf));
68 if (remote_participants_count(ctx)==1){
69 convert_conference_to_call(lc);
71 if (ms_audio_conference_get_size(ctx->conf)==1 && ctx->local_participant!=NULL){
72 remove_local_endpoint(ctx);
74 if (ms_audio_conference_get_size(ctx->conf)==0){
75 ms_audio_conference_destroy(ctx->conf);
81 void linphone_call_add_to_conf(LinphoneCall *call, bool_t muted){
82 LinphoneCore *lc=call->core;
83 LinphoneConference *conf=&lc->conf_ctx;
85 call->params.has_video = FALSE;
86 call->camera_active = FALSE;
87 ep=ms_audio_endpoint_get_from_stream(call->audiostream,TRUE);
88 ms_audio_conference_add_member(conf->conf,ep);
89 ms_audio_conference_mute_member(conf->conf,ep,muted);
93 void linphone_call_remove_from_conf(LinphoneCall *call){
94 LinphoneCore *lc=call->core;
95 LinphoneConference *conf=&lc->conf_ctx;
97 ms_audio_conference_remove_member(conf->conf,call->endpoint);
98 ms_audio_endpoint_release_from_stream(call->endpoint);
102 static RtpProfile *make_dummy_profile(int samplerate){
103 RtpProfile *prof=rtp_profile_new("dummy");
104 PayloadType *pt=payload_type_clone(&payload_type_l16_mono);
105 pt->clock_rate=samplerate;
106 rtp_profile_set_payload(prof,0,pt);
110 static void add_local_endpoint(LinphoneConference *conf,LinphoneCore *lc){
111 /*create a dummy audiostream in order to extract the local part of it */
112 /* network address and ports have no meaning and are not used here. */
113 AudioStream *st=audio_stream_new(65000,65001,FALSE);
114 MSSndCard *playcard=lc->sound_conf.lsd_card ?
115 lc->sound_conf.lsd_card : lc->sound_conf.play_sndcard;
116 MSSndCard *captcard=lc->sound_conf.capt_sndcard;
117 const MSAudioConferenceParams *params=ms_audio_conference_get_params(conf->conf);
118 conf->local_dummy_profile=make_dummy_profile(params->samplerate);
120 audio_stream_start_full(st, conf->local_dummy_profile,
131 linphone_core_echo_cancellation_enabled(lc)
133 _post_configure_audio_stream(st,lc,FALSE);
134 conf->local_participant=st;
135 conf->local_endpoint=ms_audio_endpoint_get_from_stream(st,FALSE);
136 ms_audio_conference_add_member(conf->conf,conf->local_endpoint);
141 * Returns the sound volume (mic input) of the local participant of the conference.
142 * @param lc the linphone core
143 * @returns the measured input volume expressed in dbm0.
145 float linphone_core_get_conference_local_input_volume(LinphoneCore *lc){
146 LinphoneConference *conf=&lc->conf_ctx;
147 AudioStream *st=conf->local_participant;
148 if (st && st->volsend && !conf->local_muted){
150 ms_filter_call_method(st->volsend,MS_VOLUME_GET,&vol);
154 return LINPHONE_VOLUME_DB_LOWEST;
158 * Merge a call into a conference.
159 * @param lc the linphone core
160 * @param call an established call, either in LinphoneCallStreamsRunning or LinphoneCallPaused state.
162 * If this is the first call that enters the conference, the virtual conference will be created automatically.
163 * If the local user was actively part of the call (ie not in paused state), then the local user is automatically entered into the conference.
165 * @returns 0 if successful, -1 otherwise.
167 int linphone_core_add_to_conference(LinphoneCore *lc, LinphoneCall *call){
168 LinphoneCallParams params;
169 LinphoneConference *conf=&lc->conf_ctx;
171 if (call->current_params.in_conference){
172 ms_error("Already in conference");
175 conference_check_init(&lc->conf_ctx, lp_config_get_int(lc->config, "sound","conference_rate",16000));
176 call->params.in_conference=TRUE;
177 call->params.has_video=FALSE;
178 call->params.media_encryption=LinphoneMediaEncryptionNone;
180 if (call->state==LinphoneCallPaused)
181 linphone_core_resume_call(lc,call);
182 else if (call->state==LinphoneCallStreamsRunning){
183 /*this will trigger a reINVITE that will later redraw the streams */
184 if (call->audiostream || call->videostream){
185 linphone_call_stop_media_streams (call); /*free the audio & video local resources*/
187 if (call==lc->current_call){
188 lc->current_call=NULL;
190 linphone_core_update_call(lc,call,¶ms);
191 add_local_endpoint(conf,lc);
193 ms_error("Call is in state %s, it cannot be added to the conference.",linphone_call_state_to_string(call->state));
199 static int remove_from_conference(LinphoneCore *lc, LinphoneCall *call, bool_t active){
202 if (!call->current_params.in_conference){
203 if (call->params.in_conference){
204 ms_warning("Not (yet) in conference, be patient");
207 ms_error("Not in a conference.");
211 call->params.in_conference=FALSE;
213 char *str=linphone_call_get_remote_address_as_string(call);
214 ms_message("%s will be removed from conference", str);
217 // reconnect local audio with this call
218 if (linphone_core_is_in_conference(lc)){
219 ms_message("Leaving conference for reconnecting with unique call.");
220 linphone_core_leave_conference(lc);
222 ms_message("Updating call to actually remove from conference");
223 err=linphone_core_update_call(lc,call,&call->params);
225 ms_message("Pausing call to actually remove from conference");
226 err=linphone_core_pause_call(lc,call);
232 static int convert_conference_to_call(LinphoneCore *lc){
234 MSList *calls=lc->calls;
236 if (remote_participants_count(&lc->conf_ctx)!=1){
237 ms_error("No unique call remaining in conference.");
242 LinphoneCall *rc=(LinphoneCall*)calls->data;
244 if (rc->params.in_conference) { // not using current_param
245 bool_t active_after_removed=linphone_core_is_in_conference(lc);
246 err=remove_from_conference(lc, rc, active_after_removed);
254 * Remove a call from the conference.
255 * @param lc the linphone core
256 * @param call a call that has been previously merged into the conference.
258 * After removing the remote participant belonging to the supplied call, the call becomes a normal call in paused state.
259 * If one single remote participant is left alone in the conference after the removal, then it is
260 * automatically removed from the conference and put into a simple call, like before entering the conference.
261 * The conference's resources are then automatically destroyed.
263 * @returns 0 if successful, -1 otherwise.
265 int linphone_core_remove_from_conference(LinphoneCore *lc, LinphoneCall *call){
266 char * str=linphone_call_get_remote_address_as_string(call);
267 ms_message("Removing call %s from the conference", str);
269 int err=remove_from_conference(lc,call, FALSE);
271 ms_error("Error removing participant from conference.");
275 if (remote_participants_count(&lc->conf_ctx)==1){
276 ms_message("conference size is 1: need to be converted to plain call");
277 err=convert_conference_to_call(lc);
279 ms_message("the conference need not to be converted as size is %i", remote_participants_count(&lc->conf_ctx));
285 * Indicates whether the local participant is part of the conference.
286 * @param lc the linphone core
287 * @returns TRUE if the local participant is in the conference, FALSE otherwise.
289 bool_t linphone_core_is_in_conference(const LinphoneCore *lc){
290 return lc->conf_ctx.local_participant!=NULL;
294 * Moves the local participant out of the conference.
295 * @param lc the linphone core
296 * When the local participant is out of the conference, the remote participants can continue to talk normally.
297 * @returns 0 if successful, -1 otherwise.
299 int linphone_core_leave_conference(LinphoneCore *lc){
300 LinphoneConference *conf=&lc->conf_ctx;
301 if (linphone_core_is_in_conference(lc))
302 remove_local_endpoint(conf);
307 * Moves the local participant inside the conference.
308 * @param lc the linphone core
310 * Makes the local participant to join the conference.
311 * Typically, the local participant is by default always part of the conference when joining an active call into a conference.
312 * However, by calling linphone_core_leave_conference() and linphone_core_enter_conference() the application can decide to temporarily
313 * move out and in the local participant from the conference.
315 * @returns 0 if successful, -1 otherwise
317 int linphone_core_enter_conference(LinphoneCore *lc){
318 if (linphone_core_sound_resources_locked(lc)) {
321 if (lc->current_call != NULL) {
322 linphone_core_pause_call(lc, lc->current_call);
324 LinphoneConference *conf=&lc->conf_ctx;
325 if (conf->local_participant==NULL) add_local_endpoint(conf,lc);
330 * Add all calls into a conference.
331 * @param lc the linphone core
333 * Merge all established calls (either in LinphoneCallStreamsRunning or LinphoneCallPaused) into a conference.
335 * @returns 0 if successful, -1 otherwise
337 int linphone_core_add_all_to_conference(LinphoneCore *lc) {
338 MSList *calls=lc->calls;
340 LinphoneCall *call=(LinphoneCall*)calls->data;
342 if (!call->current_params.in_conference) {
343 linphone_core_add_to_conference(lc, call);
346 linphone_core_enter_conference(lc);
351 * Terminates the conference and the calls associated with it.
352 * @param lc the linphone core
354 * All the calls that were merged to the conference are terminated, and the conference resources are destroyed.
356 * @returns 0 if successful, -1 otherwise
358 int linphone_core_terminate_conference(LinphoneCore *lc) {
359 MSList *calls=lc->calls;
361 LinphoneCall *call=(LinphoneCall*)calls->data;
363 if (call->current_params.in_conference) {
364 linphone_core_terminate_call(lc, call);
371 * Returns the number of participants to the conference, including the local participant.
372 * @param lc the linphone core
374 * Typically, after merging two calls into the conference, there is total of 3 participants:
375 * the local participant (or local user), and two remote participants that were the destinations of the two previously establised calls.
377 * @returns the number of participants to the conference
379 int linphone_core_get_conference_size(LinphoneCore *lc) {
380 if (lc->conf_ctx.conf == NULL) {
383 return ms_audio_conference_get_size(lc->conf_ctx.conf);