shogo4405 / HaishinKit.swift

Camera and Microphone streaming library via RTMP and SRT for iOS, macOS, tvOS and visionOS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

4 is not a valid channel count for Format ID 'aac '.

mkrn opened this issue · comments

Describe the bug

Crash from Sentry:

NSInvalidArgumentException: *** -[AVAssetWriterInput initWithMediaType:outputSettings:sourceFormatHint:] 4 is not a valid channel count for Format ID 'aac '.  Use kAudioFormatProperty_AvailableEncodeNumberChannels (<AudioToolbox/AudioFormat.h>) to enumerate available channel counts for a given format.
  ?, in __exceptionPreprocess
  ?, in objc_exception_throw
  ?, in -[AVAssetWriterInput initWithMediaType:outputSettings:sourceFormatHint:]
  File "<compiler-generated>", in AVAssetWriterInput.init
  File "<compiler-generated>", in AVAssetWriterInput.__allocating_init
...

To Reproduce

device = [iPhone13,4]
os = [iOS 16.3.1]

Expected behavior

App should not not crash

Version

Latest (main)

Smartphone info.

device = [iPhone13,4]
os = [iOS 16.3.1]

Additional context

Not sure. Captured in the wild

Screenshots

No response

Relevant log output

No response

Indeed, it doesn't support 3-channel or 4-channel audio. While it's not causing a crash, I believe it should display an error for unsupported cases. Do you know how to capture in 3 channels or more on an iPhone or iPad?

Sorry, no idea yet. It may be invalid.. maybe some special microphone

It seems that AVAssetWriterInput doesn't support 4 channels. Even when forcibly setting it to 2 channels, recording didn't work. Therefore, I made adjustments to prevent crashes. Now, you can handle it using delegate?.recorder(self, errorOccurred: .failedToCreateAssetWriterInput). #1268

We have a subset of users who use external audio devices. Mostly musicians and djs. They use audio mixers that they connect to the phones using DIN (MIDI) cables with lightning or USB-C adapters. MIDI supports up to 16 channels. This way, the input audio format looks like this:
Screenshot 2023-08-24 at 3 37 22 PM
It has 4 channels and while it doesn't crash, AV can't instantiate AVAudioFormat with this ASBD. I have EvermixBox4 (evermix does no longer exist) and was able to get the above format. Some users have iRig Pro Duo, PreSonus Audiobox USB96, Focusrite Clarett2Pro USB. I think any mixer with DIN output should reproduce the issue.

I'm wondering if we could to use Audio Toolbox in this case and discard extra channels with kAudioConverterChannelMap. Ideally could let the user to select what channels to discard. Still looking into this, would be happy to help to debug this.

I've found a solution for this issue, it's to provide correct channelLayout to AVAudioFormat.init in case number of channels is more than 2 (in AudioCodec.swift). For example, for 4 channels it's AVAudioChannelLayout(layoutTag: kAudioChannelLayoutTag_AudioUnit_4). I also tried to create AudioChannelLayout using custom mChannelDescriptions to discard selected channels, but AVAudioFormat was always nil. Next need to limit number of channels for outputFormat to min(inSourceFormat.mChannelsPerFrame, 2) (in AudioCodecSettings.swift). After that connecting an external mixer and streaming works fine. Will prepare a PR.