diff options
author | Liong Sauw Ming <ming@teluu.com> | 2013-10-09 03:34:26 +0000 |
---|---|---|
committer | Liong Sauw Ming <ming@teluu.com> | 2013-10-09 03:34:26 +0000 |
commit | d8b5dd7e1cce29801971e99377da09c2aa9d2d0a (patch) | |
tree | f72999a3a235c9d4dd2824ebc16ed4099bf1b810 | |
parent | 813db6d4094126dadf3c53ea346b08f04d223528 (diff) |
Re #1697: Use AVAudioSession to set audio session category and activate/deactivate audio session
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@4617 74dad513-b988-da41-8d7b-12977e46ad98
-rw-r--r-- | pjmedia/src/pjmedia-audiodev/coreaudio_dev.m (renamed from pjmedia/src/pjmedia-audiodev/coreaudio_dev.c) | 137 | ||||
-rw-r--r-- | pjsip-apps/src/pjsua/ios/ipjsua.xcodeproj/project.pbxproj | 4 |
2 files changed, 63 insertions, 78 deletions
diff --git a/pjmedia/src/pjmedia-audiodev/coreaudio_dev.c b/pjmedia/src/pjmedia-audiodev/coreaudio_dev.m index 3248f616..b2206824 100644 --- a/pjmedia/src/pjmedia-audiodev/coreaudio_dev.c +++ b/pjmedia/src/pjmedia-audiodev/coreaudio_dev.m @@ -35,7 +35,7 @@ #if COREAUDIO_MAC #include <CoreAudio/CoreAudio.h> #else - #include <AudioToolbox/AudioServices.h> + #include <AVFoundation/AVAudioSession.h> #define AudioDeviceID unsigned @@ -51,11 +51,7 @@ #define USE_AUDIO_ROUTE_CHANGE_PROP_LISTENER 0 /* Starting iOS SDK 7, Audio Session API is deprecated. */ - #ifdef __IPHONE_7_0 - #define USE_AUDIO_SESSION_API 0 - #else - #define USE_AUDIO_SESSION_API 1 - #endif + #define USE_AUDIO_SESSION_API 0 #endif /* For Mac OS 10.5.x and earlier */ @@ -143,6 +139,10 @@ struct coreaudio_stream void *resample_buf_ptr; unsigned resample_buf_count; unsigned resample_buf_size; + +#if !COREAUDIO_MAC + AVAudioSession *sess; +#endif }; /* Static variable */ @@ -288,19 +288,17 @@ static pj_status_t ca_factory_init(pjmedia_aud_dev_factory *f) cdi->info.default_samples_per_sec = 8000; /* Set the device capabilities here */ -#if USE_AUDIO_SESSION_API != 0 cdi->info.caps = PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY | PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY | PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING | +#if USE_AUDIO_SESSION_API != 0 PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE | PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE | +#endif PJMEDIA_AUD_DEV_CAP_EC; cdi->info.routes = PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER | PJMEDIA_AUD_DEV_ROUTE_EARPIECE | PJMEDIA_AUD_DEV_ROUTE_BLUETOOTH; -#else - cdi->info.caps = PJMEDIA_AUD_DEV_CAP_EC; -#endif PJ_LOG(4, (THIS_FILE, " dev_id %d: %s (in=%d, out=%d) %dHz", i, @@ -1239,16 +1237,14 @@ static pj_status_t create_audio_unit(AudioComponent io_comp, AudioUnit *io_unit) { OSStatus ostatus; -#if !COREAUDIO_MAC && USE_AUDIO_SESSION_API != 0 - UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord; +#if !COREAUDIO_MAC /* We want to be able to open playback and recording streams */ - ostatus = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, - sizeof(audioCategory), - &audioCategory); - if (ostatus != kAudioSessionNoError) { + strm->sess = [AVAudioSession sharedInstance]; + if ([strm->sess setCategory:AVAudioSessionCategoryPlayAndRecord + error:nil] != YES) + { PJ_LOG(4, (THIS_FILE, - "Warning: cannot set the audio session category (%i)", - ostatus)); + "Warning: cannot set the audio session category")); } #endif @@ -1715,21 +1711,14 @@ static pj_status_t ca_stream_get_cap(pjmedia_aud_stream *s, strm->param.input_latency_ms++; } } -#elif USE_AUDIO_SESSION_API != 0 - Float32 latency, latency2; - UInt32 size = sizeof(Float32); - - if ((AudioSessionGetProperty( - kAudioSessionProperty_CurrentHardwareInputLatency, - &size, &latency) == kAudioSessionNoError) && - (AudioSessionGetProperty( - kAudioSessionProperty_CurrentHardwareIOBufferDuration, - &size, &latency2) == kAudioSessionNoError)) - { - strm->param.input_latency_ms = (unsigned) - ((latency + latency2) * 1000); +#else + if ([strm->sess respondsToSelector:@selector(inputLatency)]) { + strm->param.input_latency_ms = + (unsigned)(([strm->sess inputLatency] + + [strm->sess IOBufferDuration]) * 1000); strm->param.input_latency_ms++; - } + } else + return PJMEDIA_EAUD_INVCAP; #endif *(unsigned*)pval = strm->param.input_latency_ms; @@ -1763,34 +1752,26 @@ static pj_status_t ca_stream_get_cap(pjmedia_aud_stream *s, strm->param.output_latency_ms++; } } -#elif USE_AUDIO_SESSION_API != 0 - Float32 latency, latency2; - UInt32 size = sizeof(Float32); - - if ((AudioSessionGetProperty( - kAudioSessionProperty_CurrentHardwareOutputLatency, - &size, &latency) == kAudioSessionNoError) && - (AudioSessionGetProperty( - kAudioSessionProperty_CurrentHardwareIOBufferDuration, - &size, &latency2) == kAudioSessionNoError)) - { - strm->param.output_latency_ms = (unsigned) - ((latency + latency2) * 1000); +#else + if ([strm->sess respondsToSelector:@selector(outputLatency)]) { + strm->param.output_latency_ms = + (unsigned)(([strm->sess outputLatency] + + [strm->sess IOBufferDuration]) * 1000); strm->param.output_latency_ms++; - } + } else + return PJMEDIA_EAUD_INVCAP; #endif *(unsigned*)pval = (++strm->param.output_latency_ms * 2); return PJ_SUCCESS; -#if COREAUDIO_MAC || USE_AUDIO_SESSION_API != 0 } else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING && (strm->param.dir & PJMEDIA_DIR_PLAYBACK)) { +#if COREAUDIO_MAC OSStatus ostatus; Float32 volume; UInt32 size = sizeof(Float32); /* Output volume setting */ -#if COREAUDIO_MAC ostatus = AudioUnitGetProperty (strm->io_units[1] ? strm->io_units[1] : strm->io_units[0], kAudioDevicePropertyVolumeScalar, @@ -1800,17 +1781,15 @@ static pj_status_t ca_stream_get_cap(pjmedia_aud_stream *s, &size); if (ostatus != noErr) return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); -#elif USE_AUDIO_SESSION_API != 0 - ostatus = AudioSessionGetProperty( - kAudioSessionProperty_CurrentHardwareOutputVolume, - &size, &volume); - if (ostatus != kAudioSessionNoError) { - return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); - } -#endif *(unsigned*)pval = (unsigned)(volume * 100); return PJ_SUCCESS; +#else + if ([strm->sess respondsToSelector:@selector(outputVolume)]) { + *(unsigned*)pval = (unsigned)([strm->sess outputVolume] * 100); + return PJ_SUCCESS; + } else + return PJMEDIA_EAUD_INVCAP; #endif #if !COREAUDIO_MAC @@ -1932,39 +1911,36 @@ static pj_status_t ca_stream_set_cap(pjmedia_aud_stream *s, "VoiceProcessingIO"))); return PJ_SUCCESS; - } - -#if USE_AUDIO_SESSION_API != 0 - - else if ((cap==PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY && + } else if ((cap==PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY && (strm->param.dir & PJMEDIA_DIR_CAPTURE)) || (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY && (strm->param.dir & PJMEDIA_DIR_PLAYBACK))) { - Float32 bufferDuration = *(unsigned *)pval; - OSStatus ostatus; + NSTimeInterval duration = *(unsigned *)pval; unsigned latency; - + /* For low-latency audio streaming, you can set this value to * as low as 5 ms (the default is 23ms). However, lowering the * latency may cause a decrease in audio quality. */ - bufferDuration /= 1000; - ostatus = AudioSessionSetProperty( - kAudioSessionProperty_PreferredHardwareIOBufferDuration, - sizeof(bufferDuration), &bufferDuration); - if (ostatus != kAudioSessionNoError) { + duration /= 1000; + if ([strm->sess setPreferredIOBufferDuration:duration error:nil] + != YES) + { PJ_LOG(4, (THIS_FILE, - "Error: cannot set the preferred buffer duration (%i)", - ostatus)); - return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); + "Error: cannot set the preferred buffer duration")); + return PJMEDIA_EAUD_INVOP; } ca_stream_get_cap(s, PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY, &latency); ca_stream_get_cap(s, PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY, &latency); return PJ_SUCCESS; - } else if (cap==PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE && + } + +#if USE_AUDIO_SESSION_API != 0 + + else if (cap==PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE && (strm->param.dir & PJMEDIA_DIR_CAPTURE)) { UInt32 btooth = *(pjmedia_aud_dev_route*)pval == @@ -2025,8 +2001,10 @@ static pj_status_t ca_stream_start(pjmedia_aud_stream *strm) return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); } -#if !COREAUDIO_MAC && USE_AUDIO_SESSION_API != 0 - AudioSessionSetActive(true); +#if !COREAUDIO_MAC + if ([stream->sess setActive:true error:nil] != YES) { + PJ_LOG(4, (THIS_FILE, "Warning: cannot activate audio session")); + } #endif for (i = 0; i < 2; i++) { @@ -2083,9 +2061,12 @@ static pj_status_t ca_stream_stop(pjmedia_aud_stream *strm) } pj_mutex_unlock(stream->cf->mutex); -#if !COREAUDIO_MAC && USE_AUDIO_SESSION_API != 0 - if (should_deactivate) - AudioSessionSetActive(false); +#if !COREAUDIO_MAC + if (should_deactivate) { + if ([stream->sess setActive:false error:nil] != YES) { + PJ_LOG(4, (THIS_FILE, "Warning: cannot deactivate audio session")); + } + } #endif stream->quit_flag = 1; diff --git a/pjsip-apps/src/pjsua/ios/ipjsua.xcodeproj/project.pbxproj b/pjsip-apps/src/pjsua/ios/ipjsua.xcodeproj/project.pbxproj index 5c0b4c6e..1c6b0bd5 100644 --- a/pjsip-apps/src/pjsua/ios/ipjsua.xcodeproj/project.pbxproj +++ b/pjsip-apps/src/pjsua/ios/ipjsua.xcodeproj/project.pbxproj @@ -15,6 +15,7 @@ 3ADCCD2F172E40120007BE8E /* pjsua_app_config.c in Sources */ = {isa = PBXBuildFile; fileRef = 3ADCCD2A172E40120007BE8E /* pjsua_app_config.c */; }; 3ADCCD30172E40120007BE8E /* pjsua_app_legacy.c in Sources */ = {isa = PBXBuildFile; fileRef = 3ADCCD2B172E40120007BE8E /* pjsua_app_legacy.c */; }; 3ADCCD31172E40120007BE8E /* pjsua_app.c in Sources */ = {isa = PBXBuildFile; fileRef = 3ADCCD2C172E40120007BE8E /* pjsua_app.c */; }; + 3ADFCCFD1803B00600B0A097 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3ADFCCFC1803B00600B0A097 /* AVFoundation.framework */; }; 3AF0580916F050770046B835 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3AF0580816F050770046B835 /* UIKit.framework */; }; 3AF0580B16F050770046B835 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3AF0580A16F050770046B835 /* Foundation.framework */; }; 3AF0580D16F050770046B835 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3AF0580C16F050770046B835 /* CoreGraphics.framework */; }; @@ -57,6 +58,7 @@ 3ADCCD2A172E40120007BE8E /* pjsua_app_config.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pjsua_app_config.c; path = ../../pjsua_app_config.c; sourceTree = "<group>"; }; 3ADCCD2B172E40120007BE8E /* pjsua_app_legacy.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pjsua_app_legacy.c; path = ../../pjsua_app_legacy.c; sourceTree = "<group>"; }; 3ADCCD2C172E40120007BE8E /* pjsua_app.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pjsua_app.c; path = ../../pjsua_app.c; sourceTree = "<group>"; }; + 3ADFCCFC1803B00600B0A097 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; }; 3AF0580416F050770046B835 /* ipjsua.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ipjsua.app; sourceTree = BUILT_PRODUCTS_DIR; }; 3AF0580816F050770046B835 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; 3AF0580A16F050770046B835 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; @@ -100,6 +102,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 3ADFCCFD1803B00600B0A097 /* AVFoundation.framework in Frameworks */, 3A92069316F1DEA500D49F96 /* CFNetwork.framework in Frameworks */, 3A92068F16F1DE7100D49F96 /* AudioToolbox.framework in Frameworks */, 3AF0580916F050770046B835 /* UIKit.framework in Frameworks */, @@ -163,6 +166,7 @@ 3AF0580716F050770046B835 /* Frameworks */ = { isa = PBXGroup; children = ( + 3ADFCCFC1803B00600B0A097 /* AVFoundation.framework */, 3A92069216F1DEA500D49F96 /* CFNetwork.framework */, 3A92068E16F1DE7100D49F96 /* AudioToolbox.framework */, 3AF0580816F050770046B835 /* UIKit.framework */, |