From 9075ba1bb221c0135d35151e7c163ce92356f6f8 Mon Sep 17 00:00:00 2001
From: Andrea Gianarda <andrea.gianarda@belledonne-communications.com>
Date: Wed, 20 Sep 2023 11:26:19 +0200
Subject: [PATCH] Delete hack when creating LocalConference in callbacks.c and
 RemoteConference in call.cpp

---
 coreapi/callbacks.c           |  15 ++---
 coreapi/local_conference.cpp  |  31 ++++++-----
 coreapi/local_conference.h    |   3 +
 coreapi/remote_conference.cpp | 100 +++++++++++++++++-----------------
 coreapi/remote_conference.h   |  20 ++++---
 src/call/call.cpp             |  26 +++++----
 src/core/core.cpp             |   4 +-
 src/core/core.h               |   2 +-
 8 files changed, 107 insertions(+), 94 deletions(-)

diff --git a/coreapi/callbacks.c b/coreapi/callbacks.c
index d4da2af869..49c0f0f9b8 100644
--- a/coreapi/callbacks.c
+++ b/coreapi/callbacks.c
@@ -223,6 +223,7 @@ static void call_received(SalCallOp *h) {
 				endTime = times.front().second;
 			}
 
+			std::shared_ptr<MediaConference::LocalConference> localConference = nullptr;
 			if (!conference) {
 #ifdef HAVE_DB_STORAGE
 				std::shared_ptr<ConferenceInfo> confInfo =
@@ -230,9 +231,9 @@ static void call_received(SalCallOp *h) {
 				        ? L_GET_PRIVATE_FROM_C_OBJECT(lc)->mainDb->getConferenceInfoFromURI(to)
 				        : nullptr;
 				if (confInfo) {
-					std::shared_ptr<MediaConference::LocalConference>(
-					    new MediaConference::LocalConference(L_GET_CPP_PTR_FROM_C_OBJECT(lc), h),
-					    [](MediaConference::LocalConference *c) { c->unref(); });
+					localConference = dynamic_pointer_cast<MediaConference::LocalConference>(
+					    (new MediaConference::LocalConference(L_GET_CPP_PTR_FROM_C_OBJECT(lc), h))->toSharedPtr());
+					localConference->initWithOp(h);
 				} else
 #endif // HAVE_DB_STORAGE
 				{
@@ -251,16 +252,16 @@ static void call_received(SalCallOp *h) {
 						return;
 					} else {
 
-						auto localConference = std::shared_ptr<MediaConference::LocalConference>(
-						    new MediaConference::LocalConference(L_GET_CPP_PTR_FROM_C_OBJECT(lc), h),
-						    [](MediaConference::LocalConference *c) { c->unref(); });
+						localConference = dynamic_pointer_cast<MediaConference::LocalConference>(
+						    (new MediaConference::LocalConference(L_GET_CPP_PTR_FROM_C_OBJECT(lc), h))->toSharedPtr());
+						localConference->initWithOp(h);
 						localConference->confirmCreation();
 						return;
 					}
 				}
 			} else if ((startTime != -1) || (endTime != -1)) {
 				// If start time or end time is not -1, then the client wants to update the conference
-				auto localConference = static_pointer_cast<MediaConference::LocalConference>(conference);
+				localConference = static_pointer_cast<MediaConference::LocalConference>(conference);
 				localConference->updateConferenceInformation(h);
 			}
 		}
diff --git a/coreapi/local_conference.cpp b/coreapi/local_conference.cpp
index 71d73d7889..bb9a08b346 100644
--- a/coreapi/local_conference.cpp
+++ b/coreapi/local_conference.cpp
@@ -101,9 +101,21 @@ LocalConference::LocalConference(const shared_ptr<Core> &core,
 
 LocalConference::LocalConference(const std::shared_ptr<Core> &core, SalCallOp *op)
     : Conference(core, Address::create(op->getTo()), nullptr, ConferenceParams::create(core->getCCore())) {
+}
 
+LocalConference::~LocalConference() {
+	if ((state != ConferenceInterface::State::Terminated) && (state != ConferenceInterface::State::Deleted)) {
+		terminate();
+	}
 #ifdef HAVE_ADVANCED_IM
-	LinphoneCore *lc = core->getCCore();
+	eventHandler.reset();
+#endif // HAVE_ADVANCED_IM
+	mMixerSession.reset();
+}
+
+void LocalConference::createEventHandler() {
+#ifdef HAVE_ADVANCED_IM
+	LinphoneCore *lc = getCore()->getCCore();
 	bool_t eventLogEnabled =
 	    linphone_config_get_bool(linphone_core_get_config(lc), "misc", "conference_event_log_enabled", TRUE);
 	if (eventLogEnabled) {
@@ -116,24 +128,15 @@ LocalConference::LocalConference(const std::shared_ptr<Core> &core, SalCallOp *o
 #ifdef HAVE_ADVANCED_IM
 	}
 #endif // HAVE_ADVANCED_IM
+}
 
-	mMixerSession.reset(new MixerSession(*core.get()));
-
+void LocalConference::initWithOp(SalCallOp *op) {
+	mMixerSession.reset(new MixerSession(*getCore().get()));
 	setState(ConferenceInterface::State::Instantiated);
-
+	createEventHandler();
 	configure(op);
 }
 
-LocalConference::~LocalConference() {
-	if ((state != ConferenceInterface::State::Terminated) && (state != ConferenceInterface::State::Deleted)) {
-		terminate();
-	}
-#ifdef HAVE_ADVANCED_IM
-	eventHandler.reset();
-#endif // HAVE_ADVANCED_IM
-	mMixerSession.reset();
-}
-
 void LocalConference::updateConferenceInformation(SalCallOp *op) {
 	auto remoteContact = op->getRemoteContactAddress();
 	if (remoteContact) {
diff --git a/coreapi/local_conference.h b/coreapi/local_conference.h
index 1462b0c3f2..5985c90617 100644
--- a/coreapi/local_conference.h
+++ b/coreapi/local_conference.h
@@ -158,6 +158,7 @@ public:
 
 	virtual void notifyStateChanged(LinphonePrivate::ConferenceInterface::State state) override;
 
+	void initWithOp(SalCallOp *op);
 	void confirmCreation();
 	void updateConferenceInformation(SalCallOp *op);
 	virtual std::shared_ptr<Call> getCall() const override;
@@ -195,6 +196,8 @@ private:
 
 	virtual std::shared_ptr<ConferenceInfo> createConferenceInfo() const override;
 	bool tryAddMeDevice();
+
+	void createEventHandler();
 };
 
 } // end of namespace MediaConference
diff --git a/coreapi/remote_conference.cpp b/coreapi/remote_conference.cpp
index eac19f4c0b..175e8c4067 100644
--- a/coreapi/remote_conference.cpp
+++ b/coreapi/remote_conference.cpp
@@ -42,39 +42,10 @@ LINPHONE_BEGIN_NAMESPACE
 namespace MediaConference {
 
 RemoteConference::RemoteConference(const shared_ptr<Core> &core,
-                                   const std::shared_ptr<LinphonePrivate::CallSession> &focusSession,
-                                   const std::shared_ptr<Address> &confAddr,
-                                   const ConferenceId &conferenceId,
-                                   const ConferenceInfo::participant_list_t &invitees,
+                                   const std::shared_ptr<Address> &meAddr,
                                    CallSessionListener *listener,
                                    const std::shared_ptr<LinphonePrivate::ConferenceParams> params)
-    : Conference(core, conferenceId.getLocalAddress(), listener, params) {
-
-	focus = Participant::create(this, confAddr, focusSession);
-	lInfo() << "Create focus '" << *focus->getAddress() << "' from address : " << *confAddr;
-	confParams->enableLocalParticipant(false);
-	pendingSubject = confParams->getSubject();
-
-	std::shared_ptr<Address> organizer;
-#ifdef HAVE_DB_STORAGE
-	auto &mainDb = getCore()->getPrivate()->mainDb;
-	if (mainDb) {
-		const auto &confInfo = mainDb->getConferenceInfoFromURI(confAddr);
-		// me is admin if the organizer is the same as me
-		if (confInfo) {
-			organizer = confInfo->getOrganizerAddress();
-		}
-	}
-#endif
-	getMe()->setAdmin((organizer == nullptr) || organizer->weakEqual(*(getMe()->getAddress())));
-
-	mInvitedParticipants = invitees;
-
-	setState(ConferenceInterface::State::Instantiated);
-
-	setConferenceId(conferenceId);
-	setConferenceAddress(confAddr);
-	finalizeCreation();
+    : Conference(core, meAddr, listener, params) {
 }
 
 RemoteConference::RemoteConference(const shared_ptr<Core> &core,
@@ -84,8 +55,7 @@ RemoteConference::RemoteConference(const shared_ptr<Core> &core,
                                    const std::shared_ptr<LinphonePrivate::ConferenceParams> params)
     : Conference(core, conferenceId.getLocalAddress(), listener, params) {
 
-	focus = Participant::create(this, focusAddr);
-	lInfo() << "Create focus '" << focus->getAddress() << "' from address : " << focusAddr;
+	createFocus(focusAddr);
 	pendingSubject = confParams->getSubject();
 
 	getMe()->setAdmin(true);
@@ -98,20 +68,28 @@ RemoteConference::RemoteConference(const shared_ptr<Core> &core,
 	setState(ConferenceInterface::State::Instantiated);
 }
 
-RemoteConference::RemoteConference(const shared_ptr<Core> &core,
-                                   const std::shared_ptr<LinphonePrivate::Call> &focusCall,
-                                   const ConferenceId &conferenceId,
-                                   CallSessionListener *listener,
-                                   const std::shared_ptr<LinphonePrivate::ConferenceParams> params)
-    : Conference(core, conferenceId.getLocalAddress(), listener, params) {
+RemoteConference::~RemoteConference() {
+	terminate();
+#ifdef HAVE_ADVANCED_IM
+	eventHandler.reset();
+#endif // HAVE_ADVANCED_IM
+}
 
-	focus = Participant::create(this, focusCall->getRemoteContactAddress(), focusCall->getActiveSession());
-	lInfo() << "Create focus '" << *focus->getAddress() << "' from address : " << focusCall->getRemoteContact();
+void RemoteConference::createFocus(const std::shared_ptr<Address> focusAddr,
+                                   const std::shared_ptr<LinphonePrivate::CallSession> focusSession) {
+	focus = Participant::create(this, focusAddr, focusSession);
+	lInfo() << "Create focus '" << *focus->getAddress() << "' from address : " << *focusAddr;
+}
+
+void RemoteConference::initWithInvitees(const std::shared_ptr<LinphonePrivate::Call> &focusCall,
+                                        const ConferenceInfo::participant_list_t &invitees,
+                                        const ConferenceId &conferenceId) {
+	const auto &focusSession = focusCall->getActiveSession();
+	const auto &conferenceAddress = focusSession->getRemoteContactAddress();
+	createFocus(conferenceAddress, focusSession);
 	pendingSubject = confParams->getSubject();
 	setConferenceId(conferenceId);
 
-	const auto &conferenceAddress = focus->getSession()->getRemoteContactAddress();
-
 #ifdef HAVE_DB_STORAGE
 	auto &mainDb = getCore()->getPrivate()->mainDb;
 	if (mainDb) {
@@ -120,6 +98,7 @@ RemoteConference::RemoteConference(const shared_ptr<Core> &core,
 		getMe()->setAdmin((confInfo && (confInfo->getOrganizerAddress()->weakEqual(*getMe()->getAddress()))));
 	}
 #endif
+	mInvitedParticipants = invitees;
 
 	setState(ConferenceInterface::State::Instantiated);
 
@@ -127,13 +106,36 @@ RemoteConference::RemoteConference(const shared_ptr<Core> &core,
 	finalizeCreation();
 }
 
-RemoteConference::~RemoteConference() {
-	terminate();
-#ifdef HAVE_ADVANCED_IM
-	eventHandler.reset();
-#endif // HAVE_ADVANCED_IM
-}
+void RemoteConference::initWithInvitees(const std::shared_ptr<Address> confAddr,
+                                        const std::shared_ptr<Address> focusAddr,
+                                        const std::shared_ptr<LinphonePrivate::CallSession> focusSession,
+                                        const ConferenceInfo::participant_list_t &invitees,
+                                        const ConferenceId &conferenceId) {
+	createFocus(focusAddr, focusSession);
+	confParams->enableLocalParticipant(false);
+	pendingSubject = confParams->getSubject();
 
+	std::shared_ptr<Address> organizer;
+#ifdef HAVE_DB_STORAGE
+	auto &mainDb = getCore()->getPrivate()->mainDb;
+	if (mainDb) {
+		const auto &confInfo = mainDb->getConferenceInfoFromURI(confAddr);
+		// me is admin if the organizer is the same as me
+		if (confInfo) {
+			organizer = confInfo->getOrganizerAddress();
+		}
+	}
+#endif
+	getMe()->setAdmin((organizer == nullptr) || organizer->weakEqual(*(getMe()->getAddress())));
+
+	mInvitedParticipants = invitees;
+
+	setState(ConferenceInterface::State::Instantiated);
+
+	setConferenceId(conferenceId);
+	setConferenceAddress(confAddr);
+	finalizeCreation();
+}
 void RemoteConference::finalizeCreation() {
 
 	if (getState() == ConferenceInterface::State::CreationPending) {
diff --git a/coreapi/remote_conference.h b/coreapi/remote_conference.h
index 4bc2a3d795..2f594d26c7 100644
--- a/coreapi/remote_conference.h
+++ b/coreapi/remote_conference.h
@@ -45,19 +45,19 @@ public:
 	                 CallSessionListener *listener,
 	                 const std::shared_ptr<ConferenceParams> params);
 	RemoteConference(const std::shared_ptr<Core> &core,
-	                 const std::shared_ptr<LinphonePrivate::Call> &focusCall,
-	                 const ConferenceId &conferenceId,
-	                 CallSessionListener *listener,
-	                 const std::shared_ptr<ConferenceParams> params);
-	RemoteConference(const std::shared_ptr<Core> &core,
-	                 const std::shared_ptr<LinphonePrivate::CallSession> &focusSession,
-	                 const std::shared_ptr<Address> &confAddr,
-	                 const ConferenceId &conferenceId,
-	                 const ConferenceInfo::participant_list_t &invitees,
+	                 const std::shared_ptr<Address> &meAddr,
 	                 CallSessionListener *listener,
 	                 const std::shared_ptr<LinphonePrivate::ConferenceParams> params);
 	virtual ~RemoteConference();
 
+	void initWithInvitees(const std::shared_ptr<LinphonePrivate::Call> &focusCall,
+	                      const ConferenceInfo::participant_list_t &invitees,
+	                      const ConferenceId &conferenceId);
+	void initWithInvitees(const std::shared_ptr<Address> confAddr,
+	                      const std::shared_ptr<Address> focusAddr,
+	                      const std::shared_ptr<LinphonePrivate::CallSession> focusSession,
+	                      const ConferenceInfo::participant_list_t &invitees,
+	                      const ConferenceId &conferenceId);
 	virtual int inviteAddresses(const std::list<std::shared_ptr<Address>> &addresses,
 	                            const LinphoneCallParams *params) override;
 	virtual bool dialOutAddresses(const std::list<std::shared_ptr<Address>> &addressList) override;
@@ -183,6 +183,8 @@ private:
 	void onTransferingCallStateChanged(std::shared_ptr<LinphonePrivate::Call> transfered,
 	                                   LinphoneCallState newCallState);
 	std::list<std::shared_ptr<Address>> cleanAddressesList(const std::list<std::shared_ptr<Address>> &addresses) const;
+	void createFocus(const std::shared_ptr<Address> focusAddr,
+	                 const std::shared_ptr<LinphonePrivate::CallSession> focusSession = nullptr);
 
 	bool finalized = false;
 	bool scheduleUpdate = false;
diff --git a/src/call/call.cpp b/src/call/call.cpp
index bb317ed944..b4ee2e5b48 100644
--- a/src/call/call.cpp
+++ b/src/call/call.cpp
@@ -549,10 +549,10 @@ void Call::createRemoteConference(const shared_ptr<CallSession> &session) {
 
 			const std::shared_ptr<Address> confAddr = conferenceInfo->getUri();
 			const ConferenceId confId(confAddr, session->getLocalAddress());
-			remoteConference = std::shared_ptr<MediaConference::RemoteConference>(
-			    new MediaConference::RemoteConference(getCore(), session, confAddr, confId, invitees, nullptr,
-			                                          confParams),
-			    [](MediaConference::RemoteConference *c) { c->unref(); });
+			remoteConference = dynamic_pointer_cast<MediaConference::RemoteConference>(
+			    (new MediaConference::RemoteConference(getCore(), confId.getLocalAddress(), nullptr, confParams))
+			        ->toSharedPtr());
+			remoteConference->initWithInvitees(confAddr, confAddr, session, invitees, confId);
 #ifdef HAVE_ADVANCED_IM
 		} else if (!resourceList.isEmpty() || !sipfrag.isEmpty()) {
 			const auto &remoteParams = static_pointer_cast<MediaSession>(session)->getRemoteParams();
@@ -562,20 +562,22 @@ void Call::createRemoteConference(const shared_ptr<CallSession> &session) {
 			const auto organizer = Utils::getSipFragAddress(sipfrag);
 			auto organizerInfo = Factory::get()->createParticipantInfo(Address::create(organizer));
 			invitees.push_back(organizerInfo);
-			remoteConference = std::shared_ptr<MediaConference::RemoteConference>(
-			    new MediaConference::RemoteConference(getCore(), session, remoteContactAddress, conferenceId, invitees,
-			                                          nullptr, confParams),
-			    [](MediaConference::RemoteConference *c) { c->unref(); });
+			remoteConference = dynamic_pointer_cast<MediaConference::RemoteConference>(
+			    (new MediaConference::RemoteConference(getCore(), conferenceId.getLocalAddress(), nullptr, confParams))
+			        ->toSharedPtr());
+			remoteConference->initWithInvitees(remoteContactAddress, remoteContactAddress, session, invitees,
+			                                   conferenceId);
 #endif // HAVE_ADVANCED_IM
 		} else {
 			const auto &remoteParams = static_pointer_cast<MediaSession>(session)->getRemoteParams();
 			confParams->setStartTime(remoteParams->getPrivate()->getStartTime());
 			confParams->setEndTime(remoteParams->getPrivate()->getEndTime());
+			ConferenceInfo::participant_list_t invitees;
 			// It is expected that the core of the remote conference is the participant one
-			remoteConference = std::shared_ptr<MediaConference::RemoteConference>(
-			    new MediaConference::RemoteConference(getCore(), getSharedFromThis(), conferenceId, nullptr,
-			                                          confParams),
-			    [](MediaConference::RemoteConference *c) { c->unref(); });
+			remoteConference = dynamic_pointer_cast<MediaConference::RemoteConference>(
+			    (new MediaConference::RemoteConference(getCore(), conferenceId.getLocalAddress(), nullptr, confParams))
+			        ->toSharedPtr());
+			remoteConference->initWithInvitees(getSharedFromThis(), invitees, conferenceId);
 		}
 	}
 
diff --git a/src/core/core.cpp b/src/core/core.cpp
index c9c2eef7f7..9be7a0830c 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -1602,7 +1602,7 @@ std::shared_ptr<MediaConference::Conference> Core::findAudioVideoConference(cons
 	return nullptr;
 }
 
-void Core::insertAudioVideoConference(const shared_ptr<MediaConference::Conference> &audioVideoConference) {
+void Core::insertAudioVideoConference(const shared_ptr<MediaConference::Conference> audioVideoConference) {
 	L_ASSERT(audioVideoConference);
 
 	const ConferenceId &conferenceId = audioVideoConference->getConferenceId();
@@ -1615,7 +1615,7 @@ void Core::insertAudioVideoConference(const shared_ptr<MediaConference::Conferen
 	if (conf == nullptr) {
 		lInfo() << "Insert audio video conference " << audioVideoConference << " in RAM with conference ID "
 		        << conferenceId << ".";
-		audioVideoConferenceById[prunedConferenceId] = audioVideoConference;
+		audioVideoConferenceById.insert(std::make_pair(prunedConferenceId, audioVideoConference));
 	}
 }
 
diff --git a/src/core/core.h b/src/core/core.h
index c9443ad588..68e6918bdb 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -226,7 +226,7 @@ public:
 	// Audio Video Conference.
 	// ---------------------------------------------------------------------------
 
-	void insertAudioVideoConference(const std::shared_ptr<MediaConference::Conference> &audioVideoConference);
+	void insertAudioVideoConference(const std::shared_ptr<MediaConference::Conference> audioVideoConference);
 	std::shared_ptr<MediaConference::Conference> findAudioVideoConference(const ConferenceId &conferenceId,
 	                                                                      bool logIfNotFound = true) const;
 	void deleteAudioVideoConference(const std::shared_ptr<const MediaConference::Conference> &audioVideoConference);
-- 
GitLab