Bug 59911 - Handle is not initialized when trying to create instance of InputAudioQueue
Summary: Handle is not initialized when trying to create instance of InputAudioQueue
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 11.0 (xcode9)
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: 15.6
Assignee: Bugzilla
: 60058 ()
Depends on:
Reported: 2017-10-03 08:58 UTC by Yaroslav
Modified: 2017-11-23 16:08 UTC (History)
8 users (show)

Is this bug a regression?: No
Last known good build:

InputAudioQueue issue project (18.14 KB, application/zip)
2017-10-05 10:57 UTC, Yaroslav
Super simple test case (11.39 KB, application/zip)
2017-10-06 00:04 UTC, Vincent Dondain [MSFT]

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Yaroslav 2017-10-03 08:58:21 UTC

I'm creating an Audio application, and one of the project's part is to analyze the input data.
So I'm using InputAudioQueue class to get the data from device's microphone. 
I'm using Xamarin.Forms. I've created a DependencyService class in iOS project with implementation of IAudioService interface. In that class I'm trying to create an instance of InputAudioQueue but every time I receive "Handle is not initialized" error right in the following line of code

inputQueue = new InputAudioQueue(audioFormat);
Comment 1 Yaroslav 2017-10-03 09:02:04 UTC
Here is the version of VS and Xamarin I'm using

=== Visual Studio Community 2017 for Mac ===

Version 7.1.5 (build 2)
Installation UUID: 96c15a93-db36-46d9-9b60-21b406f87d7f
	Mono (d15-3/14f2c81) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 502000224

=== NuGet ===


=== .NET Core ===

Runtime: Not installed
SDK: Not installed
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.2.0/lib/mono/msbuild/15.0/bin/Sdks

=== Xamarin.Profiler ===

'/Applications/Xamarin Profiler.app' not found

=== Apple Developer Tools ===

Xcode 9.0 (13247)
Build 9A235

=== Xamarin.iOS ===

Version: (Visual Studio Community)
Hash: 152b654a
Branch: xcode9
Build date: 2017-09-15 02:25:56-0400

=== Xamarin.Android ===

Version: (Visual Studio Community)
Android SDK: /Users/yaroslavpelyukh/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		6.0 (API level 23)
		7.1 (API level 25)

SDK Tools Version: 25.2.5
SDK Platform Tools Version: 25.0.5
SDK Build Tools Version: 25.0.3

Java SDK: /usr
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

Android Designer EPL code available here:

=== Xamarin.Mac ===

Version: (Visual Studio Community)

=== Xamarin Inspector ===

Version: 1.3.1
Hash: cbc48dd
Branch: 1.3-release
Build date: Thu, 21 Sep 2017 19:52:53 GMT
Client compatibility: 1

=== Build Information ===

Release ID: 701050002
Git revision: 7afedcaef8e7542e70e3cf8f9bdb26938b8c0876
Build date: 2017-09-15 08:39:58-04
Xamarin addins: 3262aadf811a18c12eac6742532d052b0139a808
Build lane: monodevelop-lion-d15-3-xcode9

=== Operating System ===

Mac OS X 10.12.6
Darwin 16.7.0 Darwin Kernel Version 16.7.0
    Thu Jun 15 17:36:27 PDT 2017
    root:xnu-3789.70.16~2/RELEASE_X86_64 x86_64
Comment 2 Yaroslav 2017-10-03 09:03:03 UTC
Also, I've tried to switch to the Beta and Alpha channels just to check this issue is fixed there, but as far as I've noticed - no(
Comment 3 Paul DiPietro [MSFT] 2017-10-03 14:57:36 UTC
Can you upload a reproduction project?
Comment 4 Yaroslav 2017-10-05 10:57:05 UTC
Created attachment 25120 [details]
InputAudioQueue issue project

Hello, Paul!
Please check out the attached archive
Comment 5 Paul DiPietro [MSFT] 2017-10-05 16:34:46 UTC
I'd like to get some feedback on this from the XI team; moving the code from the dependency service into the AppDelegate causes the same crash, so I'm wondering if something's going on in that end of things.
Comment 6 Rolf Bjarne Kvinge [MSFT] 2017-10-05 16:48:02 UTC
Reopening for triaging.
Comment 7 Vincent Dondain [MSFT] 2017-10-06 00:04:15 UTC
I could reproduce the issue and made a super simple test case:

var audioFormat = new AudioStreamBasicDescription ();
var inputQueue = new InputAudioQueue (audioFormat);

^ that fails in the same way (with XI, XI and XI so not a regression).
Comment 8 Vincent Dondain [MSFT] 2017-10-06 00:04:43 UTC
Created attachment 25136 [details]
Super simple test case
Comment 9 Rolf Bjarne Kvinge [MSFT] 2017-10-06 08:00:30 UTC
The problem is that the empty AudioStreamBasicDescription is invalid.

Then there's a problem in our error path that ends up throwing the "Handle is not initialized" exception, but once that's fixed you'd get an "Invalid parameter" exception.

Something like this works fine:

    var audioFormat = new AudioStreamBasicDescription {
        SampleRate = 44100,
        Format = AudioFormatType.LinearPCM,
        FormatFlags = AudioFormatFlags.IsSignedInteger | AudioFormatFlags.IsNonInterleaved,
        BytesPerPacket = 4,
        FramesPerPacket = 1,
        BytesPerFrame = 4,
        ChannelsPerFrame = 2,
        BitsPerChannel = 16

    var inputQueue = new InputAudioQueue (audioFormat);
Comment 10 Rolf Bjarne Kvinge [MSFT] 2017-10-06 08:17:26 UTC
Fix for the problem in our error path: https://github.com/xamarin/xamarin-macios/pull/2852
Comment 12 Vincent Dondain [MSFT] 2017-10-06 19:35:19 UTC
So Rolf's fix addresses the fact that we should get a clearer exception but what's important to know for you @Yaroslav is that some of the values in your `AudioStreamBasicDescription` initialization are wrong.

Rolf's initialization works because the values are slightly different.

We won't be able to help a lot with **how** exactly `AudioStreamBasicDescription` works (in this bug) but here's how you could learn more about it:

1. We have 2 samples using `AudioStreamBasicDescription `, https://developer.xamarin.com/samples/monotouch/AudioConverterFileConverter/ and https://developer.xamarin.com/samples/monotouch/AudioGenerator/

2. You might want to check out https://developer.apple.com/documentation/coreaudio/audiostreambasicdescription

3. The broader developer community would be the best and quickest place for additional troubleshooting help on this.
Posting a question on Stack Overflow [0] or the Xamarin Forums [1][2] would be the best next step.

[0] https://stackoverflow.com/questions/tagged/xamarin
[1] https://forums.xamarin.com/categories/ios
[2] https://forums.xamarin.com/categories/mac
Comment 13 Alex Soto [MSFT] 2017-10-09 17:34:18 UTC
*** Bug 60058 has been marked as a duplicate of this bug. ***
Comment 14 Yaroslav 2017-10-12 08:54:25 UTC
I tried the initialization of AudioStreamBasicDescription class you suggested but in that case, the InputCompleted event won't fire. Here is initialization I used, InputAudioQueue is not crashing and InputCompleted event is fired:

var audioFormat = new AudioStreamBasicDescription
                SampleRate = 44100,
                Format = AudioFormatType.LinearPCM,
                FormatFlags =
                                          | AudioFormatFlags.LinearPCMIsPacked,
                BytesPerPacket = 4,
                FramesPerPacket = 1,
                BytesPerFrame = 4,
                ChannelsPerFrame = 2,
                BitsPerChannel = 16

inputQueue = new InputAudioQueue(audioFormat);
Comment 15 Yaroslav 2017-11-23 16:08:48 UTC
Also, the default initializer works fine

var audioFormat = AudioStreamBasicDescription.CreateLinearPCM();