shogo4405 / HaishinKit.swift

Camera and Microphone streaming library via RTMP and SRT for iOS, macOS, tvOS and visionOS.
https://docs.haishinkit.com/swift/latest
BSD 3-Clause "New" or "Revised" License
2.78k stars 618 forks source link

CRASH: Mac OS Example + AudioConverterNewSpecific #296

Closed ghost closed 6 years ago

ghost commented 7 years ago

I have this log line, File [Warn] [com.haishinkit.HaishinKit] [153] converter > 1718449215

After this log line the app crashes. Screenshot attached. So in short the "AudioConverterNewSpecific()" is returning null

If I modify in "AudioIOComponent.swift" the "extension AudioIOComponent: AVCaptureAudioDataOutputSampleBufferDelegate" and comment out the line "encoder.encodeSampleBuffer(sampleBuffer)"

then it does not crash but of course there is no audio in the published video

screen shot 2017-09-10 at 11 24 50 pm
shogo4405 commented 7 years ago

Thank for your issue. Please tell me your Mac model name.

Best regards.

ghost commented 7 years ago
screen shot 2017-09-11 at 9 13 47 am
ghost commented 7 years ago

This happens with the sample app. I have tried to use it in a custom app and I can repro the issue. It goes away if I say this (copy-paste from the README)

                    self.rtmpStream.recorderSettings = [
                        AVMediaTypeAudio: [
                            AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                            AVSampleRateKey: 0,
                            AVNumberOfChannelsKey: 0,
                        ],
                        AVMediaTypeVideo: [
                            AVVideoCodecKey: AVVideoCodecH264,
                            AVVideoHeightKey: 0,
                            AVVideoWidthKey: 0
                        ],
                    ]
ghost commented 7 years ago

Sorry, this is what actually did it

        self.rtmpStream.audioSettings = [
            "muted": false,
            "bitrate": 32 * 1024,
         // "profile",
            "sampleRate": 44_100 // down,up sampleRate not supported yet #58
        ]
shogo4405 commented 6 years ago

Closed cannot reproduce.

RufusMall commented 4 years ago

FYI I managed to reproduce this on my machine.

I managed to track this down to it failing when I use my virtual audio device which has 16 channels (it is called "blackhole", similar to the old "soundflower" tool). I believe the system libraries cannot create a converter from the 16 channels.

I temporarily solved this in a local build by: -modifying ChannelConfiguration enum, as it only supports up to 7 channels up to: "frontCenterAndFrontLeftAndFrontRightAndSideLeftAndSideRightAndBackLeftAndBackRightLFE" -In AudioConverter.swift, I forced the destination format to use 2 channels (instead of the 16 it was going to use based on the import source).

_inDestinationFormat = destination.audioStreamBasicDescription(inSourceFormat, sampleRate: sampleRate, channels: 2)

This obviously isn't an ideal solution. Maybe if the audio converter fails to convert, it should fallback to creating a format we know it can always convert to, or at least show some sort of error or something. I don't have much audio or audio dev knowledge - so I don't yet know if that is easily possible... but will do some reading!

mmhmmh commented 4 years ago

I also ran into this crash and it can be reproduced by using Audio MIDI Setup and set the Built-in Microphone's format to 2 ch 16-bit Integer 96.0 kHz.

I think it will also crash for other formats that are 96.0 kHz.

Setting the rtmpStream.audioSettings sampleRate to 44_100 seems to fix it.

Ideally, it shouldn't crash the app since this error can be detected.

alexisPonce98 commented 3 years ago

Hello i was wondering if you guys have found more info on this, i am trying to use this framework on iOS on an iPhone XR and am having the same problem, when i change the channels in _inDestinationFormat to 2 it works but if i leave it alone the app crashes with a failed to unwrap optional variable from _converter. I have also tried to make the rtmpStream.audioSettings: sampleRate to 44_100 with no luck.