Skip to content

Commit

Permalink
feat(AudioRender): support set CicadaAudioSessionDelegate
Browse files Browse the repository at this point in the history
  • Loading branch information
skufly authored and pingkai committed Apr 30, 2020
1 parent 6c0e424 commit bcbaae7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 45 deletions.
1 change: 1 addition & 0 deletions framework/render/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ if (IOS)
audio/Apple/AFAudioSession.h
audio/Apple/AFAudioSessionWrapper.mm
audio/Apple/AFAudioSessionWrapper.h
audio/Apple/CicadaAudioSessionDelegate.h
)
endif ()

Expand Down
10 changes: 7 additions & 3 deletions framework/render/audio/Apple/AFAudioSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
//

#import <Foundation/Foundation.h>
#include "AudioRenderType.h"
#include <functional>
#import "AudioRenderType.h"
#import "CicadaAudioSessionDelegate.h"
#import <functional>

using namespace Cicada;

Expand All @@ -17,7 +18,10 @@ using namespace Cicada;
std::function<void(AF_AUDIO_SESSION_STATUS)> mFun;
}

-(id) init:(std::function<void(AF_AUDIO_SESSION_STATUS)>)func;
@property (nullable, nonatomic, strong) id <CicadaAudioSessionDelegate> delegate;

+(instancetype _Nonnull) sharedInstance;
-(void) setCallback:(std::function<void(AF_AUDIO_SESSION_STATUS)>)func;
-(int) activeAudio;

@end
50 changes: 45 additions & 5 deletions framework/render/audio/Apple/AFAudioSession.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@

#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
#import "AFAudioSession.h"
#import "utils/frame_work_log.h"
#import "codec/utils_ios.h"

@implementation AFAudioSession

- (id) init
+(instancetype) sharedInstance
{
return [self init:nil];
static AFAudioSession *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[AFAudioSession alloc] init];
});
return instance;
}

- (id) init:(std::function<void(AF_AUDIO_SESSION_STATUS)>)func
- (instancetype) init
{
self = [super init];

if (nil != self) {
mFun = func;
mFun = nullptr;

AVAudioSession *sessionInstance = [AVAudioSession sharedInstance];

Expand Down Expand Up @@ -117,6 +122,41 @@ -(void) handleMediaServicesWereReset:(NSNotification *)notification
}
}

-(int) activeAudio
{
NSError *err = nil;
bool active = IOSNotificationManager::Instance()->GetActiveStatus() != 0;
AF_LOGI("setActive when app acitve is :%d", active);

AVAudioSessionCategoryOptions options = 0;
if (self.delegate && [self.delegate respondsToSelector:@selector(setCategory:withOptions:error:)]) {
[self.delegate setCategory:AVAudioSessionCategoryPlayback withOptions:options error:&err];
} else {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:options error:&err];
}

// Airplay 2 feature
if (@available(iOS 11.0, tvOS 11.0, *)) {
if (self.delegate && [self.delegate respondsToSelector:@selector(setCategory:mode:routeSharingPolicy:options:error:)]) {
[self.delegate setCategory:AVAudioSessionCategoryPlayback mode:AVAudioSessionModeDefault routeSharingPolicy:AVAudioSessionRouteSharingPolicyLongForm options:options error:&err];
} else {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback mode:AVAudioSessionModeDefault routeSharingPolicy:AVAudioSessionRouteSharingPolicyLongForm options:options error:&err];
}
}

if (self.delegate && [self.delegate respondsToSelector:@selector(setActive:error:)]) {
if (![self.delegate setActive:YES error:&err]) {
AF_LOGE("setActive error:%d", err.code);
}
} else {
if (![[AVAudioSession sharedInstance] setActive:YES error:&err]) {
AF_LOGE("setActive error:%d", err.code);
}
}

return static_cast<int>(err.code);
}

@end


42 changes: 5 additions & 37 deletions framework/render/audio/Apple/AFAudioSessionWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,18 @@
#import <list>
#import "AFAudioSessionWrapper.h"
#import "AFAudioSession.h"
#import <AVFoundation/AVFoundation.h>
#import "codec/utils_ios.h"
#import <UIKit/UIKit.h>

AFAudioSession *gAFAudioSession = [[AFAudioSession alloc] init];
std::mutex gAFRenderMutex;

std::list<AFAudioSessionWrapper::listener *> AFAudioSessionWrapper::mListenerList;

void AFAudioSessionWrapper::init()
{
// gAFAudioSession = [[AFAudioSession alloc] init];
std::function<void(AF_AUDIO_SESSION_STATUS)> fun = [](AF_AUDIO_SESSION_STATUS state) -> void {
return onInterruption(state);
};

if (nil != gAFAudioSession) {
std::function<void(AF_AUDIO_SESSION_STATUS)> fun = [](AF_AUDIO_SESSION_STATUS state) -> void {
return onInterruption(state);
};

[gAFAudioSession setCallback:fun];
}
[[AFAudioSession sharedInstance] setCallback:fun];
}

void AFAudioSessionWrapper::addObserver(listener *li)
Expand All @@ -49,29 +41,5 @@
// call from main thread
int AFAudioSessionWrapper::activeAudio()
{
NSError *err = nil;
AVAudioSession *sessionInstance = [AVAudioSession sharedInstance];

bool active = IOSNotificationManager::Instance()->GetActiveStatus() != 0;
if (!active) {
// need ReceivingRemoteControlEvents or use mix module to start in background
// otherwise will lead to 560557684 error
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
// [sessionInstance setCategory:AVAudioSessionCategoryPlayback
// withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDuckOthers
// error:nil];
}
AF_LOGI("setActive when app acitve is :%d", active);
[sessionInstance setCategory:AVAudioSessionCategoryPlayback withOptions:0 error:nil];
// Airplay 2 feature
if (@available(iOS 11.0, tvOS 11.0, *)) {
AVAudioSessionCategoryOptions options = 0;
[sessionInstance setCategory:AVAudioSessionCategoryPlayback mode:AVAudioSessionModeDefault routeSharingPolicy:AVAudioSessionRouteSharingPolicyLongForm options:options error:&err];
}

if (![sessionInstance setActive:YES error:&err]) {
AF_LOGE("setActive error:%d", err.code);
}

return static_cast<int>(err.code);
return [[AFAudioSession sharedInstance] activeAudio];
}
20 changes: 20 additions & 0 deletions framework/render/audio/Apple/CicadaAudioSessionDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef CicadaAudioSessionDelegate_h
#define CicadaAudioSessionDelegate_h

#import <AVFoundation/AVFoundation.h>
#import <TargetConditionals.h>

@protocol CicadaAudioSessionDelegate <NSObject>
#if TARGET_OS_IPHONE
@optional
- (BOOL)setActive:(BOOL)active error:(NSError **)outError;

@optional
- (BOOL)setCategory:(NSString *)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError;

@optional
- (BOOL)setCategory:(AVAudioSessionCategory)category mode:(AVAudioSessionMode)mode routeSharingPolicy:(AVAudioSessionRouteSharingPolicy)policy options:(AVAudioSessionCategoryOptions)options error:(NSError **)outError;
#endif
@end

#endif /* CicadaAudioSessionDelegate_h */

0 comments on commit bcbaae7

Please sign in to comment.