Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio stream manager no longer suspends audio when device app is inactive #1235

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion SmartDeviceLink/SDLStreamingAudioLifecycleManager.h
Expand Up @@ -31,7 +31,6 @@ NS_ASSUME_NONNULL_BEGIN
@property (strong, nonatomic, readonly) SDLAudioStreamManagerState *currentAudioStreamState;

@property (strong, nonatomic, readonly) SDLStateMachine *appStateMachine;
@property (strong, nonatomic, readonly) SDLAppState *currentAppState;

@property (copy, nonatomic, nullable) SDLHMILevel hmiLevel;

Expand Down
54 changes: 1 addition & 53 deletions SmartDeviceLink/SDLStreamingAudioLifecycleManager.m
Expand Up @@ -32,7 +32,6 @@

@interface SDLStreamingAudioLifecycleManager()

@property (strong, nonatomic, readwrite) SDLStateMachine *appStateMachine;
@property (strong, nonatomic, readwrite) SDLStateMachine *audioStreamStateMachine;
@property (assign, nonatomic, readonly, getter=isHmiStateAudioStreamCapable) BOOL hmiStateAudioStreamCapable;

Expand Down Expand Up @@ -66,27 +65,11 @@ - (instancetype)initWithConnectionManager:(id<SDLConnectionManagerType>)connecti
}
_secureMakes = [tempMakeArray copy];

SDLAppState *initialState = SDLAppStateInactive;
switch ([[UIApplication sharedApplication] applicationState]) {
case UIApplicationStateActive: {
initialState = SDLAppStateActive;
} break;
case UIApplicationStateInactive: // fallthrough
case UIApplicationStateBackground: {
initialState = SDLAppStateInactive;
} break;
default: break;
}

_appStateMachine = [[SDLStateMachine alloc] initWithTarget:self initialState:initialState states:[self.class sdl_appStateTransitionDictionary]];
_audioStreamStateMachine = [[SDLStateMachine alloc] initWithTarget:self initialState:SDLAudioStreamManagerStateStopped states:[self.class sdl_audioStreamingStateTransitionDictionary]];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sdl_didReceiveRegisterAppInterfaceResponse:) name:SDLDidReceiveRegisterAppInterfaceResponse object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sdl_hmiLevelDidChange:) name:SDLDidChangeHMIStatusNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sdl_appStateDidUpdate:) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sdl_appStateDidUpdate:) name:UIApplicationWillResignActiveNotification object:nil];

return self;
}

Expand Down Expand Up @@ -132,46 +115,11 @@ - (BOOL)isAudioConnected {
return [self.audioStreamStateMachine isCurrentState:SDLAudioStreamManagerStateReady];
}

- (SDLAppState *)currentAppState {
return self.appStateMachine.currentState;
}

- (SDLAudioStreamManagerState *)currentAudioStreamState {
return self.audioStreamStateMachine.currentState;
}

#pragma mark - State Machines
#pragma mark App State
+ (NSDictionary<SDLState *, SDLAllowableStateTransitions *> *)sdl_appStateTransitionDictionary {
return @{
// Will go from Inactive to Active if coming from a Phone Call.
// Will go from Inactive to IsRegainingActive if coming from Background.
SDLAppStateInactive : @[SDLAppStateActive],
SDLAppStateActive : @[SDLAppStateInactive]
};
}

- (void)sdl_appStateDidUpdate:(NSNotification*)notification {
if (notification.name == UIApplicationWillResignActiveNotification) {
[self.appStateMachine transitionToState:SDLAppStateInactive];
} else if (notification.name == UIApplicationDidBecomeActiveNotification) {
[self.appStateMachine transitionToState:SDLAppStateActive];
}
}

- (void)didEnterStateAppInactive {
SDLLogD(@"App became inactive");
[self sdl_stopAudioSession];
}

// Per Apple's guidelines: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StrategiesforHandlingAppStateTransitions/StrategiesforHandlingAppStateTransitions.html
// We should be waiting to start any OpenGL drawing until UIApplicationDidBecomeActive is called.
- (void)didEnterStateAppActive {
SDLLogD(@"App became active");
[self sdl_startAudioSession];
}

#pragma mark Audio
#pragma mark - State Machine
+ (NSDictionary<SDLState *, SDLAllowableStateTransitions *> *)sdl_audioStreamingStateTransitionDictionary {
return @{
SDLAudioStreamManagerStateStopped : @[SDLAudioStreamManagerStateStarting],
Expand Down
Expand Up @@ -46,7 +46,6 @@ __block void (^sendNotificationForHMILevel)(SDLHMILevel hmiLevel) = ^(SDLHMILeve
expect(@(streamingLifecycleManager.isAudioConnected)).to(equal(@NO));
expect(@(streamingLifecycleManager.isAudioEncrypted)).to(equal(@NO));
expect(@(streamingLifecycleManager.requestedEncryptionType)).to(equal(@(SDLStreamingEncryptionFlagNone)));
expect(streamingLifecycleManager.currentAppState).to(equal(SDLAppStateActive));
expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamManagerStateStopped));
});

Expand All @@ -67,7 +66,6 @@ __block void (^sendNotificationForHMILevel)(SDLHMILevel hmiLevel) = ^(SDLHMILeve
expect(@(streamingLifecycleManager.isStreamingSupported)).to(equal(@NO));
expect(@(streamingLifecycleManager.isAudioConnected)).to(equal(@NO));
expect(@(streamingLifecycleManager.isAudioEncrypted)).to(equal(@NO));
expect(streamingLifecycleManager.currentAppState).to(equal(SDLAppStateActive));
expect(streamingLifecycleManager.currentAudioStreamState).to(match(SDLAudioStreamManagerStateStopped));
});

Expand Down Expand Up @@ -196,8 +194,8 @@ __block void (^sendNotificationForHMILevel)(SDLHMILevel hmiLevel) = ^(SDLHMILeve
[streamingLifecycleManager.appStateMachine setToState:SDLAppStateInactive fromOldState:nil callEnterTransition:YES];
});

it(@"should suspend the video stream", ^{
expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamManagerStateShuttingDown));
it(@"should not close the stream", ^{
expect(streamingLifecycleManager.currentAudioStreamState).to(equal(SDLAudioStreamManagerStateReady));
});
});
});
Expand Down