From bf187325f53e83e159a94f0fd96494de3abfdd75 Mon Sep 17 00:00:00 2001 From: Simon Morlat Date: Thu, 1 Mar 2018 16:04:22 +0100 Subject: [PATCH] add ios platform helpers to fix issue with background tasks. --- coreapi/linphonecore.c | 2 + .../platform-helpers/ios-platform-helpers.cpp | 119 ++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 src/core/platform-helpers/ios-platform-helpers.cpp diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index c879c99a9..29b36b5a0 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -2242,6 +2242,8 @@ static void linphone_core_init(LinphoneCore * lc, LinphoneCoreCbs *cbs, LpConfig #ifdef __ANDROID__ if (system_context) lc->platform_helper = LinphonePrivate::createAndroidPlatformHelpers(lc, system_context); +#elif TARGET_OS_IPHONE + lc->platform_helper = LinphonePrivate::createIosPlatformHelpers(lc, system_context); #endif if (lc->platform_helper == NULL) lc->platform_helper = new LinphonePrivate::StubbedPlatformHelpers(lc); diff --git a/src/core/platform-helpers/ios-platform-helpers.cpp b/src/core/platform-helpers/ios-platform-helpers.cpp new file mode 100644 index 000000000..f3671667c --- /dev/null +++ b/src/core/platform-helpers/ios-platform-helpers.cpp @@ -0,0 +1,119 @@ +/* +linphone +Copyright (C) 2017 Belledonne Communications SARL + +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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifdef __APPLE__ +#include "TargetConditionals.h" +#endif + +#if TARGET_OS_IPHONE + +#include "private.h" +#include "platform-helpers.h" + + + +namespace LinphonePrivate{ + +class IosPlatformHelpers : public PlatformHelpers{ +public: + IosPlatformHelpers(LinphoneCore *lc, void *system_context); + virtual void setDnsServers(); + virtual void acquireWifiLock(); + virtual void releaseWifiLock(); + virtual void acquireMcastLock(); + virtual void releaseMcastLock(); + virtual void acquireCpuLock(); + virtual void releaseCpuLock(); + ~IosPlatformHelpers(); +private: + void bgTaskTimeout(); + static void sBgTaskTimeout(void *data); + long int mCpuLockTaskId; + int mCpuLockCount; +}; + + +IosPlatformHelpers::IosPlatformHelpers(LinphoneCore *lc, void *system_context) : PlatformHelpers(lc) { + mCpuLockCount = 0; + mCpuLockTaskId = 0; + ms_message("IosPlatformHelpers is fully initialised"); +} + +IosPlatformHelpers::~IosPlatformHelpers(){ + +} + + +void IosPlatformHelpers::setDnsServers(){ +} + +void IosPlatformHelpers::acquireWifiLock(){ +} + +void IosPlatformHelpers::releaseWifiLock(){ +} + +void IosPlatformHelpers::acquireMcastLock(){ +} + +void IosPlatformHelpers::releaseMcastLock(){ +} + +void IosPlatformHelpers::bgTaskTimeout(){ + ms_error("IosPlatformHelpers: the system requests that the cpu lock is released now."); + if (mCpuLockTaskId != 0){ + belle_sip_end_background_task(mCpuLockTaskId); + mCpuLockTaskId = 0; + } +} + +void IosPlatformHelpers::sBgTaskTimeout(void *data){ + IosPlatformHelpers *zis = (IosPlatformHelpers*)data; + zis->bgTaskTimeout(); +} + +void IosPlatformHelpers::acquireCpuLock(){ + if (mCpuLockCount == 0){ + /* on iOS, cpu lock is implemented by a long running task (obcj-C) and it is abstracted by belle-sip, + so let's use belle-sip directly.*/ + mCpuLockTaskId = belle_sip_begin_background_task("Liblinphone cpu lock", sBgTaskTimeout, this); + } + mCpuLockCount++; +} + +void IosPlatformHelpers::releaseCpuLock(){ + mCpuLockCount--; + if (mCpuLockCount == 0){ + if (mCpuLockTaskId != 0){ + belle_sip_end_background_task(mCpuLockTaskId); + mCpuLockTaskId = 0; + }else{ + ms_error("IosPlatformHelpers::releaseCpuLock(): too late, the lock has been released already by the system."); + } + } +} + + +PlatformHelpers *createIosPlatformHelpers(LinphoneCore *lc, void *system_context){ + return new IosPlatformHelpers(lc, system_context); +} + +}//end of namespace + +#endif