From 80e8338014ab3d1bd01823e078c9a49d4cff4652 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 28 May 2020 13:39:58 +0200 Subject: [PATCH] output/osx: make variables more local --- src/output/plugins/OSXOutputPlugin.cxx | 34 ++++++++++++++------------ 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/output/plugins/OSXOutputPlugin.cxx b/src/output/plugins/OSXOutputPlugin.cxx index afc14b47c..0c6f0c5f5 100644 --- a/src/output/plugins/OSXOutputPlugin.cxx +++ b/src/output/plugins/OSXOutputPlugin.cxx @@ -161,8 +161,6 @@ AudioOutput * OSXOutput::Create(EventLoop &, const ConfigBlock &block) { OSXOutput *oo = new OSXOutput(block); - AudioDeviceID dev_id = kAudioDeviceUnknown; - UInt32 dev_id_size = sizeof(dev_id); static constexpr AudioObjectPropertyAddress default_system_output_device{ kAudioHardwarePropertyDefaultSystemOutputDevice, @@ -184,6 +182,8 @@ OSXOutput::Create(EventLoop &, const ConfigBlock &block) changed by osx_output_set_device) */ : default_output_device; + AudioDeviceID dev_id = kAudioDeviceUnknown; + UInt32 dev_id_size = sizeof(dev_id); AudioObjectGetPropertyData(kAudioObjectSystemObject, &aopa, 0, @@ -253,7 +253,6 @@ osx_output_parse_channel_map(const char *device_name, SInt32 channel_map[], UInt32 num_channels) { - char *endptr; unsigned int inserted_channels = 0; bool want_number = true; @@ -271,6 +270,7 @@ osx_output_parse_channel_map(const char *device_name, if (want_number && (std::isdigit(*channel_map_str) || *channel_map_str == '-') ) { + char *endptr; channel_map[inserted_channels] = strtol(channel_map_str, &endptr, 10); if (channel_map[inserted_channels] < -1) throw FormatRuntimeError("%s: channel map value %d not allowed (must be -1 or greater)", @@ -297,12 +297,10 @@ osx_output_parse_channel_map(const char *device_name, static void osx_output_set_channel_map(OSXOutput *oo) { - AudioStreamBasicDescription desc; OSStatus status; - UInt32 size, num_channels; - char errormsg[1024]; - size = sizeof(desc); + AudioStreamBasicDescription desc; + UInt32 size = sizeof(desc); memset(&desc, 0, size); status = AudioUnitGetProperty(oo->au, kAudioUnitProperty_StreamFormat, @@ -311,12 +309,13 @@ osx_output_set_channel_map(OSXOutput *oo) &desc, &size); if (status != noErr) { + char errormsg[1024]; osx_os_status_to_cstring(status, errormsg, sizeof(errormsg)); throw FormatRuntimeError("%s: unable to get number of output device channels: %s", oo->device_name, errormsg); } - num_channels = desc.mChannelsPerFrame; + UInt32 num_channels = desc.mChannelsPerFrame; std::unique_ptr channel_map(new SInt32[num_channels]); osx_output_parse_channel_map(oo->device_name, oo->channel_map, @@ -331,6 +330,7 @@ osx_output_set_channel_map(OSXOutput *oo) channel_map.get(), size); if (status != noErr) { + char errormsg[1024]; osx_os_status_to_cstring(status, errormsg, sizeof(errormsg)); throw FormatRuntimeError("%s: unable to set channel map: %s", oo->device_name, errormsg); } @@ -631,9 +631,6 @@ osx_output_set_device(OSXOutput *oo) OSStatus status; UInt32 size, numdevices; CFStringRef cfname = nullptr; - char errormsg[1024]; - char name[256]; - unsigned int i; AtScopeExit(&cfname) { if (cfname) @@ -653,6 +650,7 @@ osx_output_set_device(OSXOutput *oo) status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &aopa_hw_devices, 0, nullptr, &size); if (status != noErr) { + char errormsg[1024]; osx_os_status_to_cstring(status, errormsg, sizeof(errormsg)); throw FormatRuntimeError("Unable to determine number of OS X audio devices: %s", errormsg); @@ -665,6 +663,7 @@ osx_output_set_device(OSXOutput *oo) &aopa_hw_devices, 0, nullptr, &size, deviceids.get()); if (status != noErr) { + char errormsg[1024]; osx_os_status_to_cstring(status, errormsg, sizeof(errormsg)); throw FormatRuntimeError("Unable to determine OS X audio device IDs: %s", errormsg); @@ -677,12 +676,14 @@ osx_output_set_device(OSXOutput *oo) kAudioObjectPropertyElementMaster, }; + unsigned i; size = sizeof(CFStringRef); for (i = 0; i < numdevices; i++) { status = AudioObjectGetPropertyData(deviceids[i], &aopa_name, 0, nullptr, &size, &cfname); if (status != noErr) { + char errormsg[1024]; osx_os_status_to_cstring(status, errormsg, sizeof(errormsg)); throw FormatRuntimeError("Unable to determine OS X device name " "(device %u): %s", @@ -690,6 +691,7 @@ osx_output_set_device(OSXOutput *oo) errormsg); } + char name[256]; if (!CFStringGetCString(cfname, name, sizeof(name), kCFStringEncodingUTF8)) throw std::runtime_error("Unable to convert device name from CFStringRef to char*"); @@ -713,6 +715,7 @@ osx_output_set_device(OSXOutput *oo) &(deviceids[i]), sizeof(AudioDeviceID)); if (status != noErr) { + char errormsg[1024]; osx_os_status_to_cstring(status, errormsg, sizeof(errormsg)); throw FormatRuntimeError("Unable to set OS X audio output device: %s", errormsg); @@ -721,7 +724,7 @@ osx_output_set_device(OSXOutput *oo) oo->dev_id = deviceids[i]; FormatDebug(osx_output_domain, "set OS X audio output device ID=%u, name=%s", - (unsigned)deviceids[i], name); + (unsigned)deviceids[i], oo->device_name); if (oo->channel_map) osx_output_set_channel_map(oo); @@ -755,8 +758,6 @@ osx_render(void *vdata, void OSXOutput::Enable() { - char errormsg[1024]; - AudioComponentDescription desc; desc.componentType = kAudioUnitType_Output; desc.componentSubType = component_subtype; @@ -770,6 +771,7 @@ OSXOutput::Enable() OSStatus status = AudioComponentInstanceNew(comp, &au); if (status != noErr) { + char errormsg[1024]; osx_os_status_to_cstring(status, errormsg, sizeof(errormsg)); throw FormatRuntimeError("Unable to open OS X component: %s", errormsg); @@ -815,7 +817,6 @@ OSXOutput::Close() noexcept void OSXOutput::Open(AudioFormat &audio_format) { - char errormsg[1024]; #ifdef ENABLE_DSD PcmExport::Params params; params.alsa_channel_order = true; @@ -895,6 +896,7 @@ OSXOutput::Open(AudioFormat &audio_format) status = AudioUnitInitialize(au); if (status != noErr) { + char errormsg[1024]; osx_os_status_to_cstring(status, errormsg, sizeof(errormsg)); throw FormatRuntimeError("Unable to initialize OS X audio unit: %s", errormsg); @@ -903,6 +905,7 @@ OSXOutput::Open(AudioFormat &audio_format) UInt32 buffer_frame_size = 1; status = osx_output_set_buffer_size(au, asbd, &buffer_frame_size); if (status != noErr) { + char errormsg[1024]; osx_os_status_to_cstring(status, errormsg, sizeof(errormsg)); throw FormatRuntimeError("Unable to set frame size: %s", errormsg); @@ -923,6 +926,7 @@ OSXOutput::Open(AudioFormat &audio_format) status = AudioOutputUnitStart(au); if (status != 0) { AudioUnitUninitialize(au); + char errormsg[1024]; osx_os_status_to_cstring(status, errormsg, sizeof(errormsg)); throw FormatRuntimeError("Unable to start audio output: %s", errormsg);