more watchOS shenanigans

This commit is contained in:
Jesús A. Álvarez 2024-03-31 16:50:26 +02:00
parent bebdac3c71
commit d7712fabfa
1 changed files with 36 additions and 12 deletions

View File

@ -33,6 +33,7 @@ static NSObject<Emulator> *sharedEmulator = nil;
{ {
WKExtendedRuntimeSession *runtimeSession; WKExtendedRuntimeSession *runtimeSession;
BOOL hasStartedEmulator; BOOL hasStartedEmulator;
AVAudioEngine *audioEngine;
} }
+ (void)load { + (void)load {
@ -114,8 +115,7 @@ static NSObject<Emulator> *sharedEmulator = nil;
- (void)didDeactivate { - (void)didDeactivate {
// This method is called when watch view controller is no longer visible // This method is called when watch view controller is no longer visible
[super didDeactivate]; [super didDeactivate];
[runtimeSession invalidate]; //sharedEmulator.running = NO;
sharedEmulator.running = NO;
} }
- (void)sessionReachabilityDidChange:(WCSession *)session { - (void)sessionReachabilityDidChange:(WCSession *)session {
@ -123,20 +123,27 @@ static NSObject<Emulator> *sharedEmulator = nil;
} }
- (void)startRuntimeSession { - (void)startRuntimeSession {
runtimeSession = [WKExtendedRuntimeSession new]; if (runtimeSession == nil || runtimeSession.state == WKExtendedRuntimeSessionStateInvalid) {
runtimeSession.delegate = self; runtimeSession = [WKExtendedRuntimeSession new];
[runtimeSession start]; runtimeSession.delegate = self;
}
if (runtimeSession.state == WKExtendedRuntimeSessionStateNotStarted) {
[runtimeSession start];
}
} }
- (void)startOrResumeEmulator { - (void)startOrResumeEmulator {
if (!hasStartedEmulator) { if (!hasStartedEmulator) {
hasStartedEmulator = YES; hasStartedEmulator = YES;
[sharedEmulator performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:NO]; [sharedEmulator performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:NO];
[self startAudioEngine];
} else { } else {
sharedEmulator.running = YES; sharedEmulator.running = YES;
} }
} }
#define LTOVRTCP_SERVER "37.187.76.115:1984"
- (void)loadAndStartEmulator { - (void)loadAndStartEmulator {
#ifdef LTOVRTCP_SERVER #ifdef LTOVRTCP_SERVER
setenv("LTOVRTCP_SERVER", LTOVRTCP_SERVER, 1); setenv("LTOVRTCP_SERVER", LTOVRTCP_SERVER, 1);
@ -208,12 +215,13 @@ static NSObject<Emulator> *sharedEmulator = nil;
- (void)extendedRuntimeSessionDidStart:(WKExtendedRuntimeSession *)extendedRuntimeSession { - (void)extendedRuntimeSessionDidStart:(WKExtendedRuntimeSession *)extendedRuntimeSession {
#if TARGET_OS_SIMULATOR == 0 #if TARGET_OS_SIMULATOR == 0
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback AVAudioSession *audioSession = [AVAudioSession sharedInstance];
mode:AVAudioSessionModeDefault [audioSession setCategory:AVAudioSessionCategoryPlayback
routeSharingPolicy:AVAudioSessionRouteSharingPolicyLongFormAudio mode:AVAudioSessionModeDefault
options:0 routeSharingPolicy:AVAudioSessionRouteSharingPolicyLongFormAudio
error:NULL]; options:0
[[AVAudioSession sharedInstance] activateWithOptions:0 completionHandler:^(BOOL activated, NSError * _Nullable error) { error:NULL];
[audioSession activateWithOptions:0 completionHandler:^(BOOL activated, NSError * _Nullable error) {
// network only works on watchOS when there's an active audio session // network only works on watchOS when there's an active audio session
[self startOrResumeEmulator]; [self startOrResumeEmulator];
}]; }];
@ -223,11 +231,27 @@ static NSObject<Emulator> *sharedEmulator = nil;
} }
- (void)extendedRuntimeSession:(WKExtendedRuntimeSession *)extendedRuntimeSession didInvalidateWithReason:(WKExtendedRuntimeSessionInvalidationReason)reason error:(NSError *)error { - (void)extendedRuntimeSession:(WKExtendedRuntimeSession *)extendedRuntimeSession didInvalidateWithReason:(WKExtendedRuntimeSessionInvalidationReason)reason error:(NSError *)error {
NSLog(@"Runtime session invalidated: %@", error); NSLog(@"Runtime session inv alidated (%d): %@", (int)reason, error);
sharedEmulator.showAlert(@"Runtime session invalidated", [NSString stringWithFormat:@"reason=%d, error=%@", (int)reason, error]);
} }
- (void)extendedRuntimeSessionWillExpire:(WKExtendedRuntimeSession *)extendedRuntimeSession { - (void)extendedRuntimeSessionWillExpire:(WKExtendedRuntimeSession *)extendedRuntimeSession {
NSLog(@"Extended runtime session will expire"); NSLog(@"Extended runtime session will expire");
} }
- (void)startAudioEngine {
NSError *error = nil;
audioEngine = [AVAudioEngine new];
AVAudioFormat *audioFormat = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:22255 channels:1];
[audioEngine enableManualRenderingMode:AVAudioEngineManualRenderingModeRealtime
format:audioFormat
maximumFrameCount:3 // DesiredMinFilledSoundBuffs
error:&error];
if (error == nil) {
[audioEngine startAndReturnError:&error];
}
if (error != nil) {
NSLog(@"Error starting audioEngine: %@", error);
}
}
@end @end