Commit 3c5f7194 authored by DanmeiChen's avatar DanmeiChen
Browse files

fix crash of audio session route change notification and improve code

parent 0481a2de
......@@ -21,11 +21,36 @@
#include "private.h"
#import <UserNotifications/UserNotifications.h>
@interface IosAppDelegate : NSObject {
@interface IosObject : NSObject {
std::shared_ptr<LinphonePrivate::Core> pcore;
}
- (void)configure:(std::shared_ptr<LinphonePrivate::Core>)core;
- (id)initWithCore:(std::shared_ptr<LinphonePrivate::Core>)core;
@end
/*
Used only by mais core.
IosAppDelegate is an object taking care of all application delegate's notifications and iteartion:
UIApplicationDidEnterBackgroundNotification
UIApplicationWillEnterForegroundNotification
iteration
Its lifecicle is the same as the one from linphonecore init to destroy.
*/
@interface IosAppDelegate : IosObject
- (id)initWithCore:(std::shared_ptr<LinphonePrivate::Core>)core;
@end
/*
IosHandler is an object taking cahrge of all ios system's notifications:
AVAudioSessionRouteChangeNotification
Its lifecicle is the same as the one from core start to stop.
*/
@interface IosHandler : IosObject
- (id)initWithCore:(std::shared_ptr<LinphonePrivate::Core>)core;
- (void)reloadDeviceOnRouteChangeCallback: (NSNotification *) notif;
@end
......@@ -20,10 +20,19 @@
#include "ios-app-delegate.h"
#import <AVFoundation/AVFoundation.h>
@implementation IosObject
- (id)initWithCore:(std::shared_ptr<LinphonePrivate::Core>)core {
pcore = core;
return self;
}
@end
@implementation IosAppDelegate
- (id)init {
self = [super init];
- (id)initWithCore:(std::shared_ptr<LinphonePrivate::Core>)core {
self = [super initWithCore:core];
if (self != nil) {
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(didEnterBackground:)
......@@ -35,6 +44,7 @@
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
return self;
}
......@@ -61,6 +71,26 @@
linphone_core_iterate(pcore->getCCore());
}
@end
@implementation IosHandler
- (id)initWithCore:(std::shared_ptr<LinphonePrivate::Core>)core {
self = [super initWithCore:core];
if (self != nil) {
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(reloadDeviceOnRouteChangeCallback:)
name:AVAudioSessionRouteChangeNotification
object:nil];
}
return self;
}
- (void)dealloc {
[super dealloc];
[NSNotificationCenter.defaultCenter removeObserver:self];
}
- (void)reloadDeviceOnRouteChangeCallback: (NSNotification *) notif
{
......
......@@ -115,6 +115,7 @@ private:
SCNetworkReachabilityFlags mCurrentFlags = 0;
bool mNetworkMonitoringEnabled = false;
static const string Framework;
IosHandler *mHandler = NULL;
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*/
......@@ -130,8 +131,7 @@ const string IosPlatformHelpers::Framework = "org.linphone.linphone";
IosPlatformHelpers::IosPlatformHelpers (std::shared_ptr<LinphonePrivate::Core> core, void *systemContext) : GenericPlatformHelpers(core) {
mUseAppDelgate = core->getCCore()->is_main_core && !linphone_config_get_int(core->getCCore()->config, "tester", "test_env", false);
if (mUseAppDelgate) {
mAppDelegate = [[IosAppDelegate alloc] init];
[mAppDelegate configure:core];
mAppDelegate = [[IosAppDelegate alloc] initWithCore:core];
}
ms_message("IosPlatformHelpers is fully initialised");
}
......@@ -141,6 +141,7 @@ void IosPlatformHelpers::start (std::shared_ptr<LinphonePrivate::Core> core) {
mCpuLockTaskId = 0;
mNetworkReachable = 0; // wait until monitor to give a status;
mSharedCoreHelpers = createIosSharedCoreHelpers(core);
mHandler = [[IosHandler alloc] initWithCore:core];
string cpimPath = getResourceDirPath(Framework, "cpim_grammar");
if (!cpimPath.empty())
......@@ -148,8 +149,6 @@ void IosPlatformHelpers::start (std::shared_ptr<LinphonePrivate::Core> core) {
else
ms_error("IosPlatformHelpers did not find cpim grammar resource directory...");
// mSharedCoreHelpers->setupSharedCore(core->getCCore()->config);
string identityPath = getResourceDirPath(Framework, "identity_grammar");
if (!identityPath.empty())
belr::GrammarLoader::get().addPath(identityPath);
......@@ -163,18 +162,14 @@ void IosPlatformHelpers::start (std::shared_ptr<LinphonePrivate::Core> core) {
else
ms_message("IosPlatformHelpers did not find vcard grammar resource directory...");
#endif
[NSNotificationCenter.defaultCenter addObserver:mAppDelegate
selector:@selector(reloadDeviceOnRouteChangeCallback:)
name:AVAudioSessionRouteChangeNotification
object:nil];
ms_message("IosPlatformHelpers is fully started");
mStart = true;
}
void IosPlatformHelpers::stop () {
mStart = false;
[mHandler dealloc];
ms_message("IosPlatformHelpers is fully stopped");
}
......@@ -297,19 +292,17 @@ void IosPlatformHelpers::onLinphoneCoreStart(bool monitoringEnabled) {
startNetworkMonitoring();
}
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!");
if (mUseAppDelgate && 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!");
}
}
......
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