Audio to bluetooth headset on iOS

For Apple specific issues

Audio to bluetooth headset on iOS

Postby P4tr3ck » Wed May 16, 2012 9:23 am

I got a bugreport (through a bad app store review, sigh) that bluetooth headsets / headphones do not playback audio on the iOS plattform. I researched a bit and it seems the fix should be as simple as adding the following lines to juce_ios_Audio.cpp around line 105:

Code: Select all
UInt32 audioCategory = audioInputIsAvailable ? kAudioSessionCategory_PlayAndRecord
                                                     : kAudioSessionCategory_MediaPlayback;

AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (audioCategory), &audioCategory);
      
UInt32 allowBluetoothInput = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryEnableBluetoothInput, sizeof(allowBluetoothInput), &allowBluetoothInput);
      
AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, routingChangedStatic, this);


kAudioSessionProperty_OverrideCategoryEnableBluetoothInput is available in in iOS 3.1 and later. While the name seems to imply that it only applies to audio input it affects audio output as well. This behavior is documented in the Audio Session Services Reference (http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html):

"This property affects the kAudioSessionCategory_PlayAndRecord category as follows: If the audio input to the device is coming from a Bluetooth headset, setting this property to TRUE results in audio output also going to the Bluetooth headset."

I can't test right now, my already ordered bluetooth headset is still on its way from Amazon. If someone could verify if this fixes bluetooth audio that would be great.

Patrick
P4tr3ck
JUCE UberWeenie
 
Posts: 136
Joined: Tue Mar 29, 2011 9:28 pm

Re: Audio to bluetooth headset on iOS

Postby jules » Thu May 17, 2012 10:37 am

Thanks for tracking that one down.. I also don't have a bluetooth headset, anyone tried it?
User avatar
jules
Fearless Leader
 
Posts: 17386
Joined: Mon Sep 06, 2004 9:03 am
Location: London, UK

Re: Audio to bluetooth headset on iOS

Postby P4tr3ck » Sat May 19, 2012 10:07 am

With the change I hear audio but with the wrong samplerate. The problem is when iOS enables the headset, the device samplerate changes. I dont see were the changed device samplerate is propagated to my code.

Patrick
P4tr3ck
JUCE UberWeenie
 
Posts: 136
Joined: Tue Mar 29, 2011 9:28 pm

Re: Audio to bluetooth headset on iOS

Postby TheVinn » Sat May 19, 2012 3:41 pm

P4tr3ck wrote:With the change I hear audio but with the wrong samplerate. The problem is when iOS enables the headset, the device samplerate changes. I dont see were the changed device samplerate is propagated to my code.


When a device sample rate changed you should get called with prepareToPlay().
Open Source: LayerEffects, VFLib, SimpleDJ, DSP Filters, LuaBridge, JUCE, FreeType, TagLib
"This isn't a big project, it shouldn't take long." - Jules
User avatar
TheVinn
JUCE UberWeenie
 
Posts: 2990
Joined: Sat Aug 29, 2009 11:31 am
Location: Marina del Rey, California

Re: Audio to bluetooth headset on iOS

Postby P4tr3ck » Sun May 20, 2012 1:55 pm

Alright, I hear audio with a correct samplerate now, its very low though only a samplerate of 8000.

I am sure the bluetooth headset supports a higher samplerate. I hear good quality audio when using the default music app on the iPhone with the same headset or if I pair the headset with my macbook. On the macbook I see two additional audio output devices when I connect the bluetooth headset. MM100 and MM100 Stereo. MM100 sounds just as bad as audio through my app on the iPhone, MM100 Stereo sounds great.

Maybe something is missing during audio device setup? I haven't seen anything obvious though.

Patrick
P4tr3ck
JUCE UberWeenie
 
Posts: 136
Joined: Tue Mar 29, 2011 9:28 pm

Re: Audio to bluetooth headset on iOS

Postby P4tr3ck » Sun May 20, 2012 11:18 pm

It seems I finally found the solution. To make the bluetooth headset work correctly the audio category must be set to kAudioSessionCategory_MediaPlayback. If it is set to kAudioSessionCategory_PlayAndRecord I was not able to get better samplerates than 8000.

To get best audio quality with kAudioSessionCategory_MediaPlayback and at least some audio with kAudioSessionCategory_PlayAndRecord setup of the audio unit should be changed like this:

Code: Select all
AudioSessionSetActive (true);

UInt32 audioCategory = (inputChannels > 0 && audioInputIsAvailable) ? kAudioSessionCategory_PlayAndRecord : kAudioSessionCategory_MediaPlayback;

if(audioCategory == kAudioSessionCategory_PlayAndRecord)
{
   UInt32 allowBluetoothInput = 1;
   AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryEnableBluetoothInput, sizeof(allowBluetoothInput), &allowBluetoothInput);
}

AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (audioCategory), &audioCategory);


Then for audio playback only initialize the device manager like this:

Code: Select all
_DeviceManager->initialise(0, 2, NULL, true);


Besides bluetooth audio this also makes the MPVolumeView work correctly. Using the MPVolumeView control the user can change the active audio output device including Airplay playback devices! I successfully tested these changes with two bluetooth headsets and an software Airplay receiver. Here is what the MPVolumeView looks like with these devices connected:

IMG_0758.PNG
MPVolumeView with two bluetooth headsets and an airplay receiver on the iPhone
IMG_0758.PNG (140.23 KiB) Viewed 813 times


Patrick
P4tr3ck
JUCE UberWeenie
 
Posts: 136
Joined: Tue Mar 29, 2011 9:28 pm

Re: Audio to bluetooth headset on iOS

Postby jules » Mon May 21, 2012 10:34 am

Excellent stuff, thanks Patrick, I'll get that sorted out!

Very annoying about the 8kHz thing, I guess it must be pushing bluetooth's bandwidth limit.
User avatar
jules
Fearless Leader
 
Posts: 17386
Joined: Mon Sep 06, 2004 9:03 am
Location: London, UK

Re: Audio to bluetooth headset on iOS

Postby P4tr3ck » Mon May 21, 2012 6:54 pm

My guess is, iOS sets the output samplerate to the same value as the maximum input samplerate because output and input samplerates where both 8000.

If you have code changes that need testing on bluethooth headsets / airplay receivers please let me know. I'll be happy to help.

Patrick
P4tr3ck
JUCE UberWeenie
 
Posts: 136
Joined: Tue Mar 29, 2011 9:28 pm

Re: Audio to bluetooth headset on iOS

Postby jules » Mon May 21, 2012 7:03 pm

If you have code changes that need testing on bluethooth headsets / airplay receivers please let me know. I'll be happy to help.


Thanks! I checked in some changes earlier today if you want to try them.
User avatar
jules
Fearless Leader
 
Posts: 17386
Joined: Mon Sep 06, 2004 9:03 am
Location: London, UK

Re: Audio to bluetooth headset on iOS

Postby P4tr3ck » Fri May 25, 2012 7:59 am

My tests were successful. I tested with a bluetooth headset and an airplay software receiver and both worked just fine.

Patrick
P4tr3ck
JUCE UberWeenie
 
Posts: 136
Joined: Tue Mar 29, 2011 9:28 pm


Return to MacOSX and iOS

Who is online

Users browsing this forum: No registered users and 1 guest