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

Crash on startup when using ExampeAVAudioEngineDevice as AudioDevice #592

Open
dericpr opened this issue Feb 22, 2021 · 2 comments
Open

Comments

@dericpr
Copy link

dericpr commented Feb 22, 2021

I've been working with @piyushtank on integrating the ExampleAVAudioEngineDevice into our Twilio based app and it's working great from the mixed audio standpoint, the major issue I currently have is that the application will randomly crash on startup sometimes when it creates an instance of ExampleAVAudioEngineDevice. It is not reproducible every time, but it happens much more frequently on the simulator then on an actual device, although it's happening on about 25% of launches on our testing hardware.

The crash occurs in the ExampleAVAudioEngineDevicePlayoutCallback and it ALWAYS happens when the system goes through the else path in the startRendering function below. The crash doesn't happen when the startup doesn't fall through this else path. I'm wondering if anyone has any insight into how we can lock down the startup sequence so it's not random in how it starts up so we don't end up crashing on startup.

My current version of the device file ( minimal changes made to export the AudioEngines )

`- (BOOL)startRendering:(nonnull TVIAudioDeviceContext)context {
@synchronized(self) {
NSLog(@"START RENDERING");
/*
* In this example, the app always publishes an audio track. So we will start the audio unit from the capturer
* call backs. We will restart the audio unit if a remote participant adds an audio track after the audio graph is
* established. Also we will re-establish the audio graph in case the format changes.
*/
if (_audioUnit) {
[self stopAudioUnit];
[self teardownAudioUnit];
}

    // If music is being played then we have already setup the engine
    if (!self.continuousMusic) {
        // We will make sure AVAudioEngine and AVAudioPlayerNode is accessed on the main queue.
        dispatch_async(dispatch_get_main_queue(), ^{
            AVAudioFormat *manualRenderingFormat  = self.playoutEngine.manualRenderingFormat;
            TVIAudioFormat *engineFormat = [[TVIAudioFormat alloc] initWithChannels:manualRenderingFormat.channelCount
                                                                         sampleRate:manualRenderingFormat.sampleRate
                                                                    framesPerBuffer:kMaximumFramesPerBuffer];
            if ([engineFormat isEqual:[[self class] activeFormat]]) {
                if (self.playoutEngine.isRunning) {
                    [self.playoutEngine stop];
                }
                
                NSError *error = nil;
                if (![self.playoutEngine startAndReturnError:&error]) {
                    NSLog(@"Failed to start AVAudioEngine, error = %@", error);
                }
            } else {
                NSLog(@"TEARING DOWN AND RESTARTING PLAYBACK ENGINE");
                [self teardownPlayoutFilePlayer];
                [self teardownPlayoutAudioEngine];
                [self setupPlayoutAudioEngine];

            }
        });
    }`
@dericpr
Copy link
Author

dericpr commented Feb 25, 2021

Just as a further piece of information it seems like if the device has a 48mhz microphone we end up in this crash scenario as the call to check if our current engineFormat matches the activeFormat fails ( engineFormat is 44100 ). On some devices these values match and on others they do not which causes the crash.

Any thoughts on how we can work around this?

@spalmertwilio
Copy link

@piyushtank this issue is linked to support request 5979254 and engineering VIDEO-4533.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants