conference.cc 31.5 KB
Newer Older
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
/*******************************************************************************
 *            conference.cc
 *
 *  Thu Nov 26, 2015
 *  Copyright  2015  Belledonne Communications
 *  Author: Linphone's team
 *  Email info@belledonne-communications.com
 ******************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "private.h"
27
#include "conference_private.h"
28
#include <mediastreamer2/msvolume.h>
29
#include <typeinfo>
30 31
#include <list>
#include <algorithm>
32 33

namespace Linphone {
34

35 36
class Conference {
public:
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	class Participant {
	public:
		Participant(LinphoneCall *call);
		Participant(const Participant &src);
		~Participant();
		bool operator==(const Participant &src) const;
		const LinphoneAddress *getUri() const {return m_uri;}
		LinphoneCall *getCall() const {return m_call;}
		void setCall(LinphoneCall *call) {m_call = call;}
		
	private:
		LinphoneAddress *m_uri;
		LinphoneCall *m_call;
	};
	
52 53 54 55 56
	class Params {
	public:
		Params(const LinphoneCore *core = NULL);
		void enableVideo(bool enable) {m_enableVideo = enable;}
		bool videoRequested() const {return m_enableVideo;}
57 58 59 60
		void setStateChangedCallback(LinphoneConferenceStateChangedCb cb, void *user_data) {
			m_state_changed_cb=cb;
			m_user_data=user_data;
		}
61 62 63
		
	private:
		bool m_enableVideo;
64 65 66 67
		LinphoneConferenceStateChangedCb m_state_changed_cb;
		void *m_user_data;
		
		friend class Conference;
68 69 70
	};
	
	Conference(LinphoneCore *core, const Params *params = NULL);
71 72
	virtual ~Conference() {};
	
73 74
	const Params &getCurrentParams() const {return m_currentParams;}
	
75 76 77
	virtual int addParticipant(LinphoneCall *call) = 0;
	virtual int removeParticipant(LinphoneCall *call) = 0;
	virtual int removeParticipant(const LinphoneAddress *uri) = 0;
78 79
	virtual int terminate() = 0;
	
80 81 82 83 84 85 86 87
	virtual int enter() = 0;
	virtual int leave() = 0;
	virtual bool isIn() const = 0;
	
	AudioStream *getAudioStream() const {return m_localParticipantStream;}
	int muteMicrophone(bool val);
	bool microphoneIsMuted() const {return m_isMuted;}
	float getInputVolume() const;
88
	
89
	virtual int getSize() const {return m_participants.size() + (isIn()?1:0);}
90
	const std::list<Participant> &getParticipants() const {return m_participants;}
91 92 93 94 95 96 97 98
	
	virtual int startRecording(const char *path) = 0;
	virtual int stopRecording() = 0;
	
	virtual void onCallStreamStarting(LinphoneCall *call, bool isPausedByRemote) {};
	virtual void onCallStreamStopping(LinphoneCall *call) {};
	virtual void onCallTerminating(LinphoneCall *call) {};
	
99 100 101
	LinphoneConferenceState getState() const {return m_state;}
	static const char *stateToString(LinphoneConferenceState state);
	
102
protected:
103
	const Participant *findParticipant(const LinphoneAddress* uri) const;
104
	void setState(LinphoneConferenceState state);
105
	
106
	LinphoneCore *m_core;
107 108
	AudioStream *m_localParticipantStream;
	bool m_isMuted;
109
	std::list<Participant> m_participants;
110
	Params m_currentParams;
111
	LinphoneConferenceState m_state;
112 113 114 115
};

class LocalConference: public Conference {
public:
116
	LocalConference(LinphoneCore *core, const Params *params = NULL);
117 118 119 120 121 122 123 124 125 126
	virtual ~LocalConference();
	
	virtual int addParticipant(LinphoneCall *call);
	virtual int removeParticipant(LinphoneCall *call);
	virtual int removeParticipant(const LinphoneAddress *uri);
	virtual int terminate();
	
	virtual int enter();
	virtual int leave();
	virtual bool isIn() const {return m_localParticipantStream!=NULL;}
127
	virtual int getSize() const;
128 129 130 131
	
	virtual int startRecording(const char *path);
	virtual int stopRecording();
	
132 133 134
	virtual void onCallStreamStarting(LinphoneCall *call, bool isPausedByRemote);
	virtual void onCallStreamStopping(LinphoneCall *call);
	virtual void onCallTerminating(LinphoneCall *call);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	
private:
	void addLocalEndpoint();
	int remoteParticipantsCount();
	void removeLocalEndpoint();
	int removeFromConference(LinphoneCall *call, bool_t active);
	int convertConferenceToCall();
	static RtpProfile *sMakeDummyProfile(int samplerate);

	MSAudioConference *m_conf;
	MSAudioEndpoint *m_localEndpoint;
	MSAudioEndpoint *m_recordEndpoint;
	RtpProfile *m_localDummyProfile;
	bool_t m_terminated;
};

class RemoteConference: public Conference {
public:
153
	RemoteConference(LinphoneCore *core, const Params *params = NULL);
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	virtual ~RemoteConference();
	
	virtual int addParticipant(LinphoneCall *call);
	virtual int removeParticipant(LinphoneCall *call) {return -1;}
	virtual int removeParticipant(const LinphoneAddress *uri);
	virtual int terminate();
	
	virtual int enter();
	virtual int leave();
	virtual bool isIn() const;
	
	virtual int startRecording(const char *path) {return 0;}
	virtual int stopRecording() {return 0;}

private:
169 170 171
	bool focusIsReady() const;
	bool transferToFocus(LinphoneCall *call);
	void reset();
172 173 174
	
	void onFocusCallSateChanged(LinphoneCallState state);
	void onPendingCallStateChanged(LinphoneCall *call, LinphoneCallState state);
175
	void onTransferingCallStateChanged(LinphoneCall *transfered, LinphoneCallState newCallState);
176 177 178 179 180 181 182 183 184 185
	
	static void callStateChangedCb(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *message);
	static void transferStateChanged(LinphoneCore *lc, LinphoneCall *transfered, LinphoneCallState new_call_state);
	
	const char *m_focusAddr;
	char *m_focusContact;
	LinphoneCall *m_focusCall;
	LinphoneCoreVTable *m_vtable;
	MSList *m_pendingCalls;
	MSList *m_transferingCalls;
186
	bool m_isTerminating;
187
};
188

189 190
};

191

192
using namespace Linphone;
193 194 195
using namespace std;


196
Conference::Participant::Participant(LinphoneCall *call) {
197 198 199 200
	m_uri = linphone_address_clone(linphone_call_get_remote_address(call));
	m_call = linphone_call_ref(call);
}

201
Conference::Participant::Participant(const Participant &src) {
202 203 204 205
	m_uri = linphone_address_clone(src.m_uri);
	m_call = src.m_call ? linphone_call_ref(src.m_call) : NULL;
}

206
Conference::Participant::~Participant() {
207 208 209 210
	linphone_address_unref(m_uri);
	if(m_call) linphone_call_unref(m_call);
}

211
bool Conference::Participant::operator==(const Participant &src) const {
212 213 214 215
	return linphone_address_equal(m_uri, src.m_uri);
}


216

217 218 219 220 221 222 223 224 225 226
Conference::Params::Params(const LinphoneCore *core): m_enableVideo(false) {
	if(core) {
		const LinphoneVideoPolicy *policy = linphone_core_get_video_policy(core);
		if(policy->automatically_initiate) m_enableVideo = true;
	}
}



Conference::Conference(LinphoneCore *core, const Conference::Params *params):
227
	m_core(core),
228
	m_localParticipantStream(NULL),
229 230 231
	m_isMuted(false),
	m_currentParams(),
	m_state(LinphoneConferenceStopped) {
232
	if(params) m_currentParams = *params;
233 234
}

235 236 237
int Conference::addParticipant(LinphoneCall *call) {
	Participant participant(call);
	m_participants.push_back(participant);
238 239 240 241
	call->conf_ref = (LinphoneConference *)this;
	return 0;
}

242 243 244
int Conference::removeParticipant(LinphoneCall *call) {
	Participant participant(call);
	m_participants.remove(participant);
245 246 247 248
	call->conf_ref = NULL;
	return 0;
}

249 250 251 252 253 254 255 256
int Conference::terminate() {
	for(list<Participant>::iterator it = m_participants.begin(); it!=m_participants.end(); it++) {
		it->getCall()->conf_ref = NULL;
	}
	m_participants.clear();
	return 0;
}

257
int Conference::removeParticipant(const LinphoneAddress *uri) {
258
	const Participant *participant = findParticipant(uri);
259 260 261 262 263 264 265
	if(participant == NULL) return -1;
	LinphoneCall *call = participant->getCall();
	if(call) call->conf_ref = NULL;
	m_participants.remove(Participant(*participant));
	return 0;
}

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
int Conference::muteMicrophone(bool val)  {
	if (val) {
		audio_stream_set_mic_gain(m_localParticipantStream, 0);
	} else {
		audio_stream_set_mic_gain_db(m_localParticipantStream, m_core->sound_conf.soft_mic_lev);
	}
	if ( linphone_core_get_rtp_no_xmit_on_audio_mute(m_core) ){
		audio_stream_mute_rtp(m_localParticipantStream, val);
	}
	m_isMuted=val;
	return 0;
}

float Conference::getInputVolume() const {
	AudioStream *st=m_localParticipantStream;
	if (st && st->volsend && !m_isMuted){
		float vol=0;
		ms_filter_call_method(st->volsend,MS_VOLUME_GET,&vol);
		return vol;

	}
	return LINPHONE_VOLUME_DB_LOWEST;
288 289
}

290 291 292 293 294 295 296 297 298 299 300 301 302
const char *Conference::stateToString(LinphoneConferenceState state) {
	switch(state) {
		case LinphoneConferenceStopped: return "Stopped";
		case LinphoneConferenceStarting: return "Starting";
		case LinphoneConferenceReady: return "Ready";
		case LinphoneConferenceStartingFailed: return "Startig failed";
		default: return "Invalid state";
	}
}



const Conference::Participant *Conference::findParticipant(const LinphoneAddress *uri) const {
303
	list<Participant>::const_iterator it = m_participants.begin();
304 305 306 307 308 309 310
	while(it!=m_participants.end()) {
		if(linphone_address_equal(uri, it->getUri())) break;
		it++;
	}
	if(it == m_participants.end()) return NULL;
	else return &*it;
}
311

312 313 314 315 316 317 318 319 320 321 322
void Conference::setState(LinphoneConferenceState state) {
	if(m_state != state) {
		ms_message("Switching conference [%p] into state '%s'", this, stateToString(state));
		m_state = state;
		if(m_currentParams.m_state_changed_cb) {
			m_currentParams.m_state_changed_cb((LinphoneConference *)this, state, m_currentParams.m_user_data);
		}
	}
}


323 324


325 326
LocalConference::LocalConference(LinphoneCore *core, const Conference::Params *params):
	Conference(core, params),
327 328 329 330 331
	m_conf(NULL),
	m_localEndpoint(NULL),
	m_recordEndpoint(NULL),
	m_localDummyProfile(NULL),
	m_terminated(FALSE) {
332

333 334
	MSAudioConferenceParams ms_conf_params;
	ms_conf_params.samplerate = lp_config_get_int(m_core->config, "sound","conference_rate",16000);
335
	m_conf=ms_audio_conference_new(&ms_conf_params, core->factory);
336
	m_state=LinphoneConferenceReady;
337 338
}

339
LocalConference::~LocalConference() {
340 341
	terminate();
	ms_audio_conference_destroy(m_conf);
342 343
}

344
RtpProfile *LocalConference::sMakeDummyProfile(int samplerate){
345 346 347 348 349 350 351
	RtpProfile *prof=rtp_profile_new("dummy");
	PayloadType *pt=payload_type_clone(&payload_type_l16_mono);
	pt->clock_rate=samplerate;
	rtp_profile_set_payload(prof,0,pt);
	return prof;
}

352
void LocalConference::addLocalEndpoint() {
353 354
	/*create a dummy audiostream in order to extract the local part of it */
	/* network address and ports have no meaning and are not used here. */
355
	AudioStream *st=audio_stream_new(m_core->factory, 65000,65001,FALSE);
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
	MSSndCard *playcard=m_core->sound_conf.lsd_card ?
			m_core->sound_conf.lsd_card : m_core->sound_conf.play_sndcard;
	MSSndCard *captcard=m_core->sound_conf.capt_sndcard;
	const MSAudioConferenceParams *params=ms_audio_conference_get_params(m_conf);
	m_localDummyProfile=sMakeDummyProfile(params->samplerate);

	audio_stream_start_full(st, m_localDummyProfile,
				"127.0.0.1",
				65000,
				"127.0.0.1",
				65001,
				0,
				40,
				NULL,
				NULL,
				playcard,
				captcard,
				linphone_core_echo_cancellation_enabled(m_core)
				);
	_post_configure_audio_stream(st,m_core,FALSE);
376
	m_localParticipantStream=st;
377 378 379 380
	m_localEndpoint=ms_audio_endpoint_get_from_stream(st,FALSE);
	ms_audio_conference_add_member(m_conf,m_localEndpoint);
}

381
int LocalConference::addParticipant(LinphoneCall *call) {
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
	if (call->current_params->in_conference){
		ms_error("Already in conference");
		return -1;
	}

	if (call->state==LinphoneCallPaused){
		call->params->in_conference=TRUE;
		call->params->has_video=FALSE;
		linphone_core_resume_call(m_core,call);
	}else if (call->state==LinphoneCallStreamsRunning){
		LinphoneCallParams *params=linphone_call_params_copy(linphone_call_get_current_params(call));
		params->in_conference=TRUE;
		params->has_video=FALSE;

		if (call->audiostream || call->videostream){
			linphone_call_stop_media_streams(call); /*free the audio & video local resources*/
			linphone_call_init_media_streams(call);
		}
		if (call==m_core->current_call){
			m_core->current_call=NULL;
		}
		/*this will trigger a reINVITE that will later redraw the streams */
		/*FIXME probably a bit too much to just redraw streams !*/
		linphone_core_update_call(m_core,call,params);
		linphone_call_params_destroy(params);
		addLocalEndpoint();
	}else{
		ms_error("Call is in state %s, it cannot be added to the conference.",linphone_call_state_to_string(call->state));
		return -1;
	}
412
	Conference::addParticipant(call);
413 414 415
	return 0;
}

416
int LocalConference::removeFromConference(LinphoneCall *call, bool_t active){
417 418 419 420 421 422 423 424 425 426 427 428 429
	int err=0;
	char *str;

	if (!call->current_params->in_conference){
		if (call->params->in_conference){
			ms_warning("Not (yet) in conference, be patient");
			return -1;
		}else{
			ms_error("Not in a conference.");
			return -1;
		}
	}
	call->params->in_conference=FALSE;
430
	Conference::removeParticipant(call);
431 432 433 434 435 436 437 438

	str=linphone_call_get_remote_address_as_string(call);
	ms_message("%s will be removed from conference", str);
	ms_free(str);
	if (active){
		LinphoneCallParams *params=linphone_call_params_copy(linphone_call_get_current_params(call));
		params->in_conference=FALSE;
		// reconnect local audio with this call
439
		if (isIn()){
440
			ms_message("Leaving conference for reconnecting with unique call.");
441
			leave();
442 443 444 445 446 447 448 449 450 451 452
		}
		ms_message("Updating call to actually remove from conference");
		err=linphone_core_update_call(m_core,call,params);
		linphone_call_params_destroy(params);
	} else{
		ms_message("Pausing call to actually remove from conference");
		err=_linphone_core_pause_call(m_core,call);
	}
	return err;
}

453
int LocalConference::remoteParticipantsCount() {
454
	int count=getSize();
455
	if (count==0) return 0;
456
	if (!m_localParticipantStream) return count;
457 458 459
	return count -1;
}

460
int LocalConference::convertConferenceToCall(){
461 462 463 464 465 466 467 468 469 470 471 472
	int err=0;
	MSList *calls=m_core->calls;

	if (remoteParticipantsCount()!=1){
		ms_error("No unique call remaining in conference.");
		return -1;
	}

	while (calls) {
		LinphoneCall *rc=(LinphoneCall*)calls->data;
		calls=calls->next;
		if (rc->params->in_conference) { // not using current_param
473
			bool_t active_after_removed=isIn();
474 475 476 477 478 479 480
			err=removeFromConference(rc, active_after_removed);
			break;
		}
	}
	return err;
}

481
int LocalConference::removeParticipant(LinphoneCall *call) {
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	int err;
	char * str=linphone_call_get_remote_address_as_string(call);
	ms_message("Removing call %s from the conference", str);
	ms_free(str);
	err=removeFromConference(call, FALSE);
	if (err){
		ms_error("Error removing participant from conference.");
		return err;
	}

	if (remoteParticipantsCount()==1){
		ms_message("conference size is 1: need to be converted to plain call");
		err=convertConferenceToCall();
	} else {
		ms_message("the conference need not to be converted as size is %i", remoteParticipantsCount());
	}
	return err;
}

501
int LocalConference::removeParticipant(const LinphoneAddress *uri) {
502
	const Participant *participant = findParticipant(uri);
503 504 505 506 507 508
	if(participant == NULL) return -1;
	LinphoneCall *call = participant->getCall();
	if(call == NULL) return -1;
	return removeParticipant(call);
}

509
int LocalConference::terminate() {
510 511 512 513 514 515 516 517 518 519
	MSList *calls=m_core->calls;
	m_terminated=TRUE;

	while (calls) {
		LinphoneCall *call=(LinphoneCall*)calls->data;
		calls=calls->next;
		if (call->current_params->in_conference) {
			linphone_core_terminate_call(m_core, call);
		}
	}
520 521 522
	
	Conference::terminate();
	
523 524 525
	return 0;
}

526
int LocalConference::enter() {
527 528 529 530 531 532
	if (linphone_core_sound_resources_locked(m_core)) {
		return -1;
	}
	if (m_core->current_call != NULL) {
		_linphone_core_pause_call(m_core, m_core->current_call);
	}
533
	if (m_localParticipantStream==NULL) addLocalEndpoint();
534 535 536
	return 0;
}

537
void LocalConference::removeLocalEndpoint(){
538 539 540 541
	if (m_localEndpoint){
		ms_audio_conference_remove_member(m_conf,m_localEndpoint);
		ms_audio_endpoint_release_from_stream(m_localEndpoint);
		m_localEndpoint=NULL;
542 543
		audio_stream_stop(m_localParticipantStream);
		m_localParticipantStream=NULL;
544 545 546 547
		rtp_profile_destroy(m_localDummyProfile);
	}
}

548
int LocalConference::leave() {
549
	if (isIn())
550 551 552 553
		removeLocalEndpoint();
	return 0;
}

554
int LocalConference::getSize() const {
555 556 557 558 559 560
	if (m_conf == NULL) {
		return 0;
	}
	return ms_audio_conference_get_size(m_conf) - (m_recordEndpoint ? 1 : 0);
}

561
int LocalConference::startRecording(const char *path) {
562 563 564 565 566
	if (m_conf == NULL) {
		ms_warning("linphone_core_start_conference_recording(): no conference now.");
		return -1;
	}
	if (m_recordEndpoint==NULL){
567
		m_recordEndpoint=ms_audio_endpoint_new_recorder(m_core->factory);
568 569 570 571 572 573
		ms_audio_conference_add_member(m_conf,m_recordEndpoint);
	}
	ms_audio_recorder_endpoint_start(m_recordEndpoint,path);
	return 0;
}

574
int LocalConference::stopRecording() {
575 576 577 578 579 580 581 582 583 584 585 586
	if (m_conf == NULL) {
		ms_warning("linphone_core_stop_conference_recording(): no conference now.");
		return -1;
	}
	if (m_recordEndpoint==NULL){
		ms_warning("linphone_core_stop_conference_recording(): no record active.");
		return -1;
	}
	ms_audio_recorder_endpoint_stop(m_recordEndpoint);
	return 0;
}

587
void LocalConference::onCallStreamStarting(LinphoneCall *call, bool isPausedByRemote) {
588 589 590 591 592 593 594 595
	call->params->has_video = FALSE;
	call->camera_enabled = FALSE;
	MSAudioEndpoint *ep=ms_audio_endpoint_get_from_stream(call->audiostream,TRUE);
	ms_audio_conference_add_member(m_conf,ep);
	ms_audio_conference_mute_member(m_conf,ep,isPausedByRemote);
	call->endpoint=ep;
}

596
void LocalConference::onCallStreamStopping(LinphoneCall *call) {
597 598 599 600 601
	ms_audio_conference_remove_member(m_conf,call->endpoint);
	ms_audio_endpoint_release_from_stream(call->endpoint);
	call->endpoint=NULL;
}

602
void LocalConference::onCallTerminating(LinphoneCall *call) {
603
	int remote_count=remoteParticipantsCount();
604
	ms_message("conference_check_uninit(): size=%i", getSize());
605 606 607 608
	if (remote_count==1 && !m_terminated){
		convertConferenceToCall();
	}
	if (remote_count==0){
609
		if (m_localParticipantStream)
610 611 612 613 614 615 616 617
			removeLocalEndpoint();
		if (m_recordEndpoint){
			ms_audio_conference_remove_member(m_conf, m_recordEndpoint);
			ms_audio_endpoint_destroy(m_recordEndpoint);
		}
	}
}

618 619


620 621
RemoteConference::RemoteConference(LinphoneCore *core, const Conference::Params *params):
	Conference(core, params),
622 623 624 625 626
	m_focusAddr(NULL),
	m_focusContact(NULL),
	m_focusCall(NULL),
	m_vtable(NULL),
	m_pendingCalls(NULL),
627 628
	m_transferingCalls(NULL),
	m_isTerminating(false) {
629 630 631 632 633
	m_focusAddr = lp_config_get_string(m_core->config, "misc", "conference_focus_addr", "");
	m_vtable = linphone_core_v_table_new();
	m_vtable->call_state_changed = callStateChangedCb;
	m_vtable->transfer_state_changed = transferStateChanged;
	linphone_core_v_table_set_user_data(m_vtable, this);
634
	_linphone_core_add_listener(m_core, m_vtable, FALSE, TRUE);
635 636
}

637
RemoteConference::~RemoteConference() {
638
	terminate();
639 640 641 642
	linphone_core_remove_listener(m_core, m_vtable);
	linphone_core_v_table_destroy(m_vtable);
}

643
int RemoteConference::addParticipant(LinphoneCall *call) {
644
	LinphoneAddress *addr;
645
	LinphoneCallParams *params;
646 647
	
	switch(m_state) {
648 649
		case LinphoneConferenceStopped:
		case LinphoneConferenceStartingFailed:
650
			Conference::addParticipant(call);
651 652 653
			ms_message("Calling the conference focus (%s)", m_focusAddr);
			addr = linphone_address_new(m_focusAddr);
			if(addr) {
654 655
				params = linphone_core_create_call_params(m_core, NULL);
				linphone_call_params_enable_video(params, m_currentParams.videoRequested());
656
				m_focusCall = linphone_core_invite_address_with_params(m_core, addr, params);
657
				m_focusCall->conf_ref = (LinphoneConference *)this;
658
				m_localParticipantStream = m_focusCall->audiostream;
659
				m_pendingCalls = ms_list_append(m_pendingCalls, call);
660 661 662
				LinphoneCallLog *callLog = linphone_call_get_call_log(m_focusCall);
				callLog->was_conference = TRUE;
				linphone_address_unref(addr);
663
				linphone_call_params_unref(params);
664
				setState(LinphoneConferenceStarting);
665 666 667
				return 0;
			} else return -1;

668
		case LinphoneConferenceStarting:
669
			Conference::addParticipant(call);
670 671 672 673 674
			if(focusIsReady()) {
				transferToFocus(call);
			} else {
				m_pendingCalls = ms_list_append(m_pendingCalls, call);
			}
675 676
			return 0;
			
677
		case LinphoneConferenceReady:
678
			Conference::addParticipant(call);
679
			transferToFocus(call);
680 681 682 683 684 685 686 687
			return 0;

		default:
			ms_error("Could not add call %p to the conference. Bad conference state (%s)", call, stateToString(m_state));
			return -1;
	}
}

688
int RemoteConference::removeParticipant(const LinphoneAddress *uri) {
689 690
	char *refer_to;
	LinphoneAddress *refer_to_addr;
691 692 693
	int res;
	
	switch(m_state) {
694
		case LinphoneConferenceReady:
695 696 697 698 699 700 701 702 703 704 705 706
			if(findParticipant(uri) == NULL) {
				char *tmp = linphone_address_as_string(uri);
				ms_error("Conference: could not remove participant '%s': not in the participants list", tmp);
				ms_free(tmp);
				return -1;
			}
			
// 			refer_to = ms_strdup_printf("%s;method=BYE", tmp);
			refer_to_addr = linphone_address_clone(uri);
			linphone_address_set_method_param(refer_to_addr, "BYE");
			refer_to = linphone_address_as_string(refer_to_addr);
			linphone_address_unref(refer_to_addr);
707
			res = sal_call_refer(m_focusCall->op, refer_to);
708
			ms_free(refer_to);
709
			
710 711 712 713 714 715 716 717
			if(res == 0) {
				return Conference::removeParticipant(uri);
			} else {
				char *tmp = linphone_address_as_string(uri);
				ms_error("Conference: could not remove participant '%s': REFER with BYE has failed", tmp);
				ms_free(tmp);
				return -1;
			}
718 719 720 721 722 723 724
			
		default:
			ms_error("Cannot remove %s from conference: Bad conference state (%s)", linphone_address_as_string(uri), stateToString(m_state));
			return -1;
	}
}

725
int RemoteConference::terminate() {
726
	m_isTerminating = true;
727
	switch(m_state) {
728 729
		case LinphoneConferenceReady:
		case LinphoneConferenceStarting:
730
			linphone_core_terminate_call(m_core, m_focusCall);
731 732
			reset();
			Conference::terminate();
Simon Morlat's avatar
Simon Morlat committed
733
			m_isTerminating = false;
734
			setState(LinphoneConferenceStopped);
735
			break;
736
			
737
		default:
Simon Morlat's avatar
Simon Morlat committed
738
			m_isTerminating = false;
739 740
			break;
	}
Simon Morlat's avatar
Simon Morlat committed
741
	
742 743 744
	return 0;
}

745
int RemoteConference::enter() {
746
	if(m_state != LinphoneConferenceReady) {
François Grisez's avatar
François Grisez committed
747
		ms_error("Could not enter in the conference: bad conference state (%s)", stateToString(m_state));
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
		return -1;
	}
	LinphoneCallState callState = linphone_call_get_state(m_focusCall);
	switch(callState) {
		case LinphoneCallStreamsRunning: break;
		case LinphoneCallPaused:
			linphone_core_resume_call(m_core, m_focusCall);
			break;
		default:
			ms_error("Could not join the conference: bad focus call state (%s)", linphone_call_state_to_string(callState));
			return -1;
	}
	return 0;
}

763
int RemoteConference::leave() {
764
	if(m_state != LinphoneConferenceReady) {
François Grisez's avatar
François Grisez committed
765
		ms_error("Could not leave the conference: bad conference state (%s)", stateToString(m_state));
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
		return -1;
	}
	LinphoneCallState callState = linphone_call_get_state(m_focusCall);
	switch(callState) {
		case LinphoneCallPaused: break;
		case LinphoneCallStreamsRunning:
			linphone_core_pause_call(m_core, m_focusCall);
			break;
		default:
			ms_error("Could not leave the conference: bad focus call state (%s)", linphone_call_state_to_string(callState));
			return -1;
	}
	return 0;
}

781
bool RemoteConference::isIn() const {
782
	if(m_state != LinphoneConferenceReady) return false;
783 784 785 786
	LinphoneCallState callState = linphone_call_get_state(m_focusCall);
	return callState == LinphoneCallStreamsRunning;
}

787 788 789 790 791 792 793 794 795 796 797 798 799 800
bool RemoteConference::focusIsReady() const {
	LinphoneCallState focusState;
	if(m_focusCall == NULL) return false;
	focusState = linphone_call_get_state(m_focusCall);
	return focusState == LinphoneCallStreamsRunning || focusState == LinphoneCallPaused;
}

bool RemoteConference::transferToFocus(LinphoneCall *call) {
	if(linphone_core_transfer_call(m_core, call, m_focusContact) == 0) {
		m_transferingCalls = ms_list_append(m_transferingCalls, call);
		return true;
	} else {
		ms_error("Conference: could not transfer call [%p] to %s", call, m_focusContact);
		return false;
801 802 803
	}
}

804 805 806 807 808 809 810 811 812 813 814 815
void RemoteConference::reset() {
	m_localParticipantStream = NULL;
	m_focusAddr = NULL;
	if(m_focusContact) {
		ms_free(m_focusContact);
		m_focusContact = NULL;
	}
	m_focusCall = NULL;
	m_pendingCalls = ms_list_free(m_pendingCalls);
	m_transferingCalls = ms_list_free(m_transferingCalls);
}

816
void RemoteConference::onFocusCallSateChanged(LinphoneCallState state) {
817 818
	MSList *it;
	
819
	switch (state) {
820 821 822 823 824 825 826 827 828 829
		case LinphoneCallConnected:
			m_focusContact = ms_strdup(linphone_call_get_remote_contact(m_focusCall));
			it = m_pendingCalls;
			while (it) {
				LinphoneCall *pendingCall = (LinphoneCall *)it->data;
				LinphoneCallState pendingCallState = linphone_call_get_state(pendingCall);
				if(pendingCallState == LinphoneCallStreamsRunning || pendingCallState == LinphoneCallPaused) {
					MSList *current_elem = it;
					it = it->next;
					m_pendingCalls = ms_list_remove_link(m_pendingCalls, current_elem);
830
					transferToFocus(pendingCall);
831 832
				} else {
					it = it->next;
833
				}
834
			}
835
			setState(LinphoneConferenceReady);
836
			break;
837

838
		case LinphoneCallError:
839 840 841 842
			reset();
			setState(LinphoneConferenceStartingFailed);
			break;
			
843
		case LinphoneCallEnd:
844
			if(!m_isTerminating) terminate();
845
			break;
846

847 848
		default: break;
	}
849 850
}

851
void RemoteConference::onPendingCallStateChanged(LinphoneCall *call, LinphoneCallState state) {
852 853 854
	switch(state) {
			case LinphoneCallStreamsRunning:
			case LinphoneCallPaused:
855
				if(m_state == LinphoneConferenceReady) {
François Grisez's avatar
François Grisez committed
856 857
					m_pendingCalls = ms_list_remove(m_pendingCalls, call);
					m_transferingCalls = ms_list_append(m_transferingCalls, call);
858
					linphone_core_transfer_call(m_core, call, m_focusContact);
859 860 861 862 863
				}
				break;
				
			case LinphoneCallError:
			case LinphoneCallEnd:
François Grisez's avatar
François Grisez committed
864
				m_pendingCalls = ms_list_remove(m_pendingCalls, call);
865
				Conference::removeParticipant(call);
866 867 868 869 870 871
				break;
				
			default: break;
		}
}

872 873 874 875 876 877 878 879 880 881 882 883 884
void RemoteConference::onTransferingCallStateChanged(LinphoneCall* transfered, LinphoneCallState newCallState) {
	switch (newCallState) {
		case LinphoneCallConnected:
		case LinphoneCallError:
			m_transferingCalls = ms_list_remove(m_transferingCalls, transfered);
			if(newCallState == LinphoneCallError) Conference::removeParticipant(transfered);
			break;

		default:
			break;
	}
}

885
void RemoteConference::callStateChangedCb(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *message) {
886
	LinphoneCoreVTable *vtable = linphone_core_get_current_vtable(lc);
887
	RemoteConference *conf = (RemoteConference *)linphone_core_v_table_get_user_data(vtable);
888 889 890 891 892 893 894
	if (call == conf->m_focusCall) {
		conf->onFocusCallSateChanged(cstate);
	} else if(ms_list_find(conf->m_pendingCalls, call)) {
		conf->onPendingCallStateChanged(call, cstate);
	}
}

895
void RemoteConference::transferStateChanged(LinphoneCore *lc, LinphoneCall *transfered, LinphoneCallState new_call_state) {
896
	LinphoneCoreVTable *vtable = linphone_core_get_current_vtable(lc);
897
	RemoteConference *conf = (RemoteConference *)linphone_core_v_table_get_user_data(vtable);
898
	if (ms_list_find(conf->m_transferingCalls, transfered)) {
899
		conf->onTransferingCallStateChanged(transfered, new_call_state);
900 901 902 903 904
	}
}



905 906 907 908
const char *linphone_conference_state_to_string(LinphoneConferenceState state) {
	return Conference::stateToString(state);
}

909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
LinphoneConferenceParams *linphone_conference_params_new(const LinphoneCore *core) {
	return (LinphoneConferenceParams *)new Conference::Params(core);
}

void linphone_conference_params_free(LinphoneConferenceParams *params) {
	delete (Conference::Params *)params;
}

LinphoneConferenceParams *linphone_conference_params_clone(const LinphoneConferenceParams *params) {
	return (LinphoneConferenceParams *)new Conference::Params(*(Conference::Params *)params);
}

void linphone_conference_params_enable_video(LinphoneConferenceParams *params, bool_t enable) {
	((Conference::Params *)params)->enableVideo(enable);
}

bool_t linphone_conference_params_video_requested(const LinphoneConferenceParams *params) {
	return ((Conference::Params *)params)->videoRequested();
}

929 930 931 932
void linphone_conference_params_set_state_changed_callback(LinphoneConferenceParams *params, LinphoneConferenceStateChangedCb cb, void *user_data) {
	((Conference::Params *)params)->setStateChangedCallback(cb, user_data);
}

933 934


935 936
LinphoneConference *linphone_local_conference_new(LinphoneCore *core) {
	return (LinphoneConference *) new LocalConference(core);
937 938
}

939 940 941 942
LinphoneConference *linphone_local_conference_new_with_params(LinphoneCore *core, const LinphoneConferenceParams *params) {
	return (LinphoneConference *) new LocalConference(core, (Conference::Params *)params);
}

943 944
LinphoneConference *linphone_remote_conference_new(LinphoneCore *core) {
	return (LinphoneConference *) new RemoteConference(core);
945 946
}

947 948 949 950
LinphoneConference *linphone_remote_conference_new_with_params(LinphoneCore *core, const LinphoneConferenceParams *params) {
	return (LinphoneConference *) new RemoteConference(core, (Conference::Params *)params);
}

951 952 953 954
void linphone_conference_free(LinphoneConference *obj) {
	delete (Conference *)obj;
}

955 956 957 958
LinphoneConferenceState linphone_conference_get_state(const LinphoneConference *obj) {
	return ((Conference *)obj)->getState();
}

959 960
int linphone_conference_add_participant(LinphoneConference *obj, LinphoneCall *call) {
	return ((Conference *)obj)->addParticipant(call);
961 962
}

963 964 965 966 967 968
int linphone_conference_remove_participant(LinphoneConference *obj, const LinphoneAddress *uri) {
	return ((Conference *)obj)->removeParticipant(uri);
}

int linphone_conference_remove_participant_with_call(LinphoneConference *obj, LinphoneCall *call) {
	return ((Conference *)obj)->removeParticipant(call);
969 970 971 972 973 974
}

int linphone_conference_terminate(LinphoneConference *obj) {
	return ((Conference *)obj)->terminate();
}

975
int linphone_conference_enter(LinphoneConference *obj) {
976
	return ((Conference *)obj)->enter();
977 978
}

979
int linphone_conference_leave(LinphoneConference *obj) {
980
	return ((Conference *)obj)->leave();
981 982
}

983
bool_t linphone_conference_is_in(const LinphoneConference *obj) {
984
	return ((Conference *)obj)->isIn();
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
}

AudioStream *linphone_conference_get_audio_stream(const LinphoneConference *obj) {
	return ((Conference *)obj)->getAudioStream();
}

int linphone_conference_mute_microphone(LinphoneConference *obj, bool_t val) {
	return ((Conference *)obj)->muteMicrophone(val);
}

bool_t linphone_conference_microphone_is_muted(const LinphoneConference *obj) {
	return ((Conference *)obj)->microphoneIsMuted();
}

float linphone_conference_get_input_volume(const LinphoneConference *obj) {
	return ((Conference *)obj)->getInputVolume();
}

1003 1004
int linphone_conference_get_size(const LinphoneConference *obj) {
	return ((Conference *)obj)->getSize();
1005 1006
}

1007
MSList *linphone_conference_get_participants(const LinphoneConference *obj) {
1008
	const list<Conference::Participant> &participants = ((Conference *)obj)->getParticipants();
1009
	MSList *participants_list = NULL;
1010
	for(list<Conference::Participant>::const_iterator it=participants.begin();it!=participants.end();it++) {
1011 1012 1013 1014 1015 1016
		LinphoneAddress *uri = linphone_address_clone(it->getUri());
		participants_list = ms_list_append(participants_list, uri);
	}
	return participants_list;
}

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
int linphone_conference_start_recording(LinphoneConference *obj, const char *path) {
	return ((Conference *)obj)->startRecording(path);
}

int linphone_conference_stop_recording(LinphoneConference *obj) {
	return ((Conference *)obj)->stopRecording();
}

void linphone_conference_on_call_stream_starting(LinphoneConference *obj, LinphoneCall *call, bool_t is_paused_by_remote) {
	((Conference *)obj)->onCallStreamStarting(call, is_paused_by_remote);
}

void linphone_conference_on_call_stream_stopping(LinphoneConference *obj, LinphoneCall *call) {
	((Conference *)obj)->onCallStreamStopping(call);
}

void linphone_conference_on_call_terminating(LinphoneConference *obj, LinphoneCall *call) {
	((Conference *)obj)->onCallTerminating(call);
}
1036 1037 1038

bool_t linphone_conference_check_class(LinphoneConference *obj, LinphoneConferenceClass _class) {
	switch(_class) {
1039 1040
		case LinphoneConferenceClassLocal: return typeid(obj) == typeid(LocalConference);
		case LinphoneConferenceClassRemote: return typeid(obj) == typeid(RemoteConference);
1041 1042 1043
		default: return FALSE;
	}
}