Commit 66eccbbc authored by DanmeiChen's avatar DanmeiChen
Browse files

improve ios platform helpers

parent 82967def
......@@ -2803,7 +2803,10 @@ static void linphone_core_init(LinphoneCore * lc, LinphoneCoreCbs *cbs, LpConfig
if (system_context) {
lc->system_context = system_context;
}
lc->platform_helper = LinphonePrivate::createIosPlatformHelpers(lc->cppPtr, lc->system_context);
if (lc->platform_helper == NULL) {
lc->platform_helper = LinphonePrivate::createIosPlatformHelpers(lc->cppPtr, lc->system_context);
}
getPlatformHelpers(lc)->start(lc->cppPtr);
#endif
if (lc->platform_helper == NULL)
lc->platform_helper = new LinphonePrivate::GenericPlatformHelpers(lc->cppPtr);
......@@ -2974,6 +2977,7 @@ LinphoneCore *_linphone_core_new_with_config(LinphoneCoreCbs *cbs, struct _LpCon
LinphoneCore *_linphone_core_new_shared_with_config(LinphoneCoreCbs *cbs, struct _LpConfig *config, void *userdata, void *system_context, bool_t automatically_start, const char *app_group_id, bool_t main_core) {
bctbx_message("[SHARED] Creating %s Shared Core", main_core ? "Main" : "Executor");
linphone_config_set_string(config, "shared_core", "app_group_id", app_group_id);
linphone_config_set_bool(config, "shared_core", "is_main_core", main_core);
LinphoneCore *core = _linphone_core_new_with_config(cbs, config, userdata, system_context, automatically_start);
core->is_main_core = main_core;
// allow ios app extension to mark msg as read without being registered
......@@ -3016,6 +3020,12 @@ LinphoneCore *linphone_core_ref(LinphoneCore *lc) {
}
void linphone_core_unref(LinphoneCore *lc) {
#if TARGET_OS_IPHONE
if (lc->platform_helper) {
delete getPlatformHelpers(lc);
}
lc->platform_helper = NULL;
#endif
belle_sip_object_unref(BELLE_SIP_OBJECT(lc));
}
......@@ -6929,9 +6939,11 @@ static void _linphone_core_stop_async_end(LinphoneCore *lc) {
#if TARGET_OS_IPHONE
bool_t is_shared_core = getPlatformHelpers(lc)->getSharedCoreHelpers()->isCoreShared();
#endif
getPlatformHelpers(lc)->stop();
#else
if (lc->platform_helper) delete getPlatformHelpers(lc);
lc->platform_helper = NULL;
#endif
#if TARGET_OS_IPHONE
/* this will unlock the other Linphone Shared Core that are waiting to start (if any).
......
......@@ -23,12 +23,9 @@
@interface IosAppDelegate : NSObject {
std::shared_ptr<LinphonePrivate::Core> pcore;
NSTimer* mIterateTimer;
}
- (void)setCore:(std::shared_ptr<LinphonePrivate::Core>)core;
- (void)onLinphoneCoreStart;
- (void)onLinphoneCoreStop;
- (void)configure:(std::shared_ptr<LinphonePrivate::Core>)core;
- (void)reloadDeviceOnRouteChangeCallback: (NSNotification *) notif;
@end
......@@ -43,7 +43,7 @@
[NSNotificationCenter.defaultCenter removeObserver:self];
}
- (void)setCore:(std::shared_ptr<LinphonePrivate::Core>)core {
- (void)configure:(std::shared_ptr<LinphonePrivate::Core>)core{
pcore = core;
}
......@@ -61,31 +61,6 @@
linphone_core_iterate(pcore->getCCore());
}
- (void)onLinphoneCoreStart {
if (linphone_core_is_auto_iterate_enabled(pcore->getCCore())) {
if (mIterateTimer.valid) {
ms_message("[Ios App] core.iterate() is already scheduled");
return;
}
mIterateTimer = [NSTimer timerWithTimeInterval:0.02 target:self selector:@selector(iterate) userInfo:nil repeats:YES];
// NSTimer runs only in the main thread correctly. Since there may not be a current thread loop.
[[NSRunLoop mainRunLoop] addTimer:mIterateTimer forMode:NSDefaultRunLoopMode];
ms_message("[Ios App] Call to core.iterate() scheduled every 20ms");
} else {
ms_warning("[Ios App] Auto core.iterate() isn't enabled, ensure you do it in your application!");
}
}
- (void)onLinphoneCoreStop {
if (linphone_core_is_auto_iterate_enabled(pcore->getCCore())) {
if (mIterateTimer) {
[mIterateTimer invalidate];
mIterateTimer = nil;
}
ms_message("[Ios App] Auto core.iterate() stopped");
}
}
- (void)reloadDeviceOnRouteChangeCallback: (NSNotification *) notif
{
......
......@@ -90,6 +90,9 @@ public:
void startAudioForEchoTestOrCalibration () override;
void stopAudioForEchoTestOrCalibration () override;
void start (std::shared_ptr<LinphonePrivate::Core> core) override;
void stop (void) override;
//IosHelper specific
bool isReachable(SCNetworkReachabilityFlags flags);
......@@ -112,7 +115,10 @@ private:
SCNetworkReachabilityFlags mCurrentFlags = 0;
bool mNetworkMonitoringEnabled = false;
static const string Framework;
IosAppDelegate *mAppDelegate;
IosAppDelegate *mAppDelegate = NULL; /* auto didEnterBackground/didEnterForeground and other callbacks */
bool mStart = false; /* generic platformhelper's funcs only work when mStart is true */
bool mUseAppDelgate = false; /* app delegate is only used by main core*/
NSTimer* mIterateTimer = NULL;
};
static void sNetworkChangeCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo);
......@@ -122,14 +128,20 @@ static void sNetworkChangeCallback(CFNotificationCenterRef center, void *observe
const string IosPlatformHelpers::Framework = "org.linphone.linphone";
IosPlatformHelpers::IosPlatformHelpers (std::shared_ptr<LinphonePrivate::Core> core, void *systemContext) : GenericPlatformHelpers(core) {
mUseAppDelgate = (!getSharedCoreHelpers()->isCoreShared() || linphone_config_get_bool(core->getCCore()->config, "shared_core", "is_main_core", false)) && !linphone_config_get_int(core->getCCore()->config, "tester", "test_env", false);
if (mUseAppDelgate) {
mAppDelegate = [[IosAppDelegate alloc] init];
[mAppDelegate configure:core];
}
ms_message("IosPlatformHelpers is fully initialised");
}
void IosPlatformHelpers::start (std::shared_ptr<LinphonePrivate::Core> core) {
mCpuLockCount = 0;
mCpuLockTaskId = 0;
mNetworkReachable = 0; // wait until monitor to give a status;
mSharedCoreHelpers = createIosSharedCoreHelpers(core);
mAppDelegate = [[IosAppDelegate alloc] init];
[mAppDelegate setCore:core];
string cpimPath = getResourceDirPath(Framework, "cpim_grammar");
if (!cpimPath.empty())
belr::GrammarLoader::get().addPath(cpimPath);
......@@ -157,7 +169,13 @@ IosPlatformHelpers::IosPlatformHelpers (std::shared_ptr<LinphonePrivate::Core> c
name:AVAudioSessionRouteChangeNotification
object:nil];
ms_message("IosPlatformHelpers is fully initialised");
ms_message("IosPlatformHelpers is fully started");
mStart = true;
}
void IosPlatformHelpers::stop () {
mStart = false;
ms_message("IosPlatformHelpers is fully stopped");
}
//Safely get an UTF-8 string from the given CFStringRef
......@@ -197,6 +215,8 @@ void IosPlatformHelpers::sBgTaskTimeout (void *data) {
// -----------------------------------------------------------------------------
void IosPlatformHelpers::acquireCpuLock () {
if (!mStart) return;
// on iOS, cpu lock is implemented by a long running task and it is abstracted by belle-sip, so let's use belle-sip directly.
if (mCpuLockCount == 0)
mCpuLockTaskId = static_cast<long>(belle_sip_begin_background_task("Liblinphone cpu lock", sBgTaskTimeout, this));
......@@ -205,6 +225,8 @@ void IosPlatformHelpers::acquireCpuLock () {
}
void IosPlatformHelpers::releaseCpuLock () {
if (!mStart) return;
mCpuLockCount--;
if (mCpuLockCount != 0)
return;
......@@ -268,17 +290,42 @@ void *IosPlatformHelpers::getPathContext () {
}
void IosPlatformHelpers::onLinphoneCoreStart(bool monitoringEnabled) {
if (!mStart) return;
mNetworkMonitoringEnabled = monitoringEnabled;
if (monitoringEnabled) {
startNetworkMonitoring();
[mAppDelegate onLinphoneCoreStart];
}
if (mUseAppDelgate) {
if (linphone_core_is_auto_iterate_enabled(getCore()->getCCore())) {
if (mIterateTimer && mIterateTimer.valid) {
ms_message("[IosPlatformHelpers] core.iterate() is already scheduled");
return;
}
mIterateTimer = [NSTimer timerWithTimeInterval:0.02 target:mAppDelegate selector:@selector(iterate) userInfo:nil repeats:YES];
// NSTimer runs only in the main thread correctly. Since there may not be a current thread loop.
[[NSRunLoop mainRunLoop] addTimer:mIterateTimer forMode:NSDefaultRunLoopMode];
ms_message("[IosPlatformHelpers] Call to core.iterate() scheduled every 20ms");
} else {
ms_warning("[IosPlatformHelpers] Auto core.iterate() isn't enabled, ensure you do it in your application!");
}
}
}
void IosPlatformHelpers::onLinphoneCoreStop() {
if (!mStart) return;
if (mNetworkMonitoringEnabled) {
stopNetworkMonitoring();
[mAppDelegate onLinphoneCoreStop];
}
if (mUseAppDelgate && linphone_core_is_auto_iterate_enabled(getCore()->getCCore())) {
if (mIterateTimer) {
[mIterateTimer invalidate];
mIterateTimer = NULL;
}
ms_message("[IosPlatformHelpers] Auto core.iterate() stopped");
}
getSharedCoreHelpers()->onLinphoneCoreStop();
......@@ -293,6 +340,8 @@ void IosPlatformHelpers::stopAudioForEchoTestOrCalibration () {
}
void IosPlatformHelpers::onWifiOnlyEnabled(bool enabled) {
if (!mStart) return;
mWifiOnly = enabled;
if (isNetworkReachable()) {
//Nothing to do if we have no connection
......@@ -308,6 +357,8 @@ void IosPlatformHelpers::setDnsServers () {
//Set proxy settings on core
void IosPlatformHelpers::setHttpProxy (const string &host, int port) {
if (!mStart) return;
linphone_core_set_http_proxy_host(getCore()->getCCore(), host.c_str());
linphone_core_set_http_proxy_port(getCore()->getCCore(), port);
}
......@@ -377,6 +428,8 @@ static void showNetworkFlags(SCNetworkReachabilityFlags flags) {
}
bool IosPlatformHelpers::startNetworkMonitoring(void) {
if (!mStart) return false;
if (reachabilityRef != NULL) {
stopNetworkMonitoring();
}
......@@ -402,6 +455,8 @@ bool IosPlatformHelpers::startNetworkMonitoring(void) {
}
void IosPlatformHelpers::stopNetworkMonitoring(void) {
if (!mStart) return;
if (reachabilityRef) {
CFRelease(reachabilityRef);
reachabilityRef = NULL;
......@@ -422,6 +477,8 @@ static void sNetworkChangeCallback(CFNotificationCenterRef center, void *observe
}
void IosPlatformHelpers::networkChangeCallback() {
if (!mStart) return;
//We receive this notification multiple time for possibly unknown reasons
//So take actions only if state changed of our internal network-related properties
......
......@@ -86,6 +86,8 @@ public:
virtual std::shared_ptr<SharedCoreHelpers> getSharedCoreHelpers() = 0;
virtual void startAudioForEchoTestOrCalibration () = 0;
virtual void stopAudioForEchoTestOrCalibration () = 0;
virtual void start (std::shared_ptr<LinphonePrivate::Core> core) = 0;
virtual void stop (void) = 0;
protected:
inline explicit PlatformHelpers (std::shared_ptr<LinphonePrivate::Core> core) : CoreAccessor(core) {}
......@@ -140,6 +142,9 @@ public:
std::shared_ptr<SharedCoreHelpers> getSharedCoreHelpers() override;
void startAudioForEchoTestOrCalibration () override;
void stopAudioForEchoTestOrCalibration () override;
void start (std::shared_ptr<LinphonePrivate::Core> core) override {};
void stop (void) override {};
protected:
bool checkIpAddressChanged();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment