apple/Throw: new helper library replacing osx_os_status_to_cstring()
This commit is contained in:
		
							
								
								
									
										67
									
								
								src/apple/Throw.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								src/apple/Throw.cxx
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2020 Max Kellermann <max.kellermann@gmail.com>
 | 
			
		||||
 *
 | 
			
		||||
 * Redistribution and use in source and binary forms, with or without
 | 
			
		||||
 * modification, are permitted provided that the following conditions
 | 
			
		||||
 * are met:
 | 
			
		||||
 *
 | 
			
		||||
 * - Redistributions of source code must retain the above copyright
 | 
			
		||||
 * notice, this list of conditions and the following disclaimer.
 | 
			
		||||
 *
 | 
			
		||||
 * - Redistributions in binary form must reproduce the above copyright
 | 
			
		||||
 * notice, this list of conditions and the following disclaimer in the
 | 
			
		||||
 * documentation and/or other materials provided with the
 | 
			
		||||
 * distribution.
 | 
			
		||||
 *
 | 
			
		||||
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 | 
			
		||||
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 | 
			
		||||
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 | 
			
		||||
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 | 
			
		||||
 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
			
		||||
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 | 
			
		||||
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 | 
			
		||||
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 | 
			
		||||
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 | 
			
		||||
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 | 
			
		||||
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 | 
			
		||||
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "Throw.hxx"
 | 
			
		||||
#include "ErrorRef.hxx"
 | 
			
		||||
#include "StringRef.hxx"
 | 
			
		||||
 | 
			
		||||
#include <stdexcept>
 | 
			
		||||
 | 
			
		||||
namespace Apple {
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
ThrowOSStatus(OSStatus status)
 | 
			
		||||
{
 | 
			
		||||
	const Apple::ErrorRef cferr(nullptr, kCFErrorDomainOSStatus,
 | 
			
		||||
				    status, nullptr);
 | 
			
		||||
	const Apple::StringRef cfstr(cferr.CopyDescription());
 | 
			
		||||
 | 
			
		||||
	char msg[1024];
 | 
			
		||||
	if (!cfstr.GetCString(msg, sizeof(msg)))
 | 
			
		||||
		throw std::runtime_error("Unknown OSStatus");
 | 
			
		||||
 | 
			
		||||
	throw std::runtime_error(msg);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
ThrowOSStatus(OSStatus status, const char *_msg)
 | 
			
		||||
{
 | 
			
		||||
	const Apple::ErrorRef cferr(nullptr, kCFErrorDomainOSStatus,
 | 
			
		||||
				    status, nullptr);
 | 
			
		||||
	const Apple::StringRef cfstr(cferr.CopyDescription());
 | 
			
		||||
 | 
			
		||||
	char msg[1024];
 | 
			
		||||
	strcpy(msg, _msg);
 | 
			
		||||
	size_t length = strlen(msg);
 | 
			
		||||
 | 
			
		||||
	cfstr.GetCString(msg + length, sizeof(msg) - length);
 | 
			
		||||
	throw std::runtime_error(msg);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace Apple
 | 
			
		||||
							
								
								
									
										45
									
								
								src/apple/Throw.hxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/apple/Throw.hxx
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright 2020 Max Kellermann <max.kellermann@gmail.com>
 | 
			
		||||
 *
 | 
			
		||||
 * Redistribution and use in source and binary forms, with or without
 | 
			
		||||
 * modification, are permitted provided that the following conditions
 | 
			
		||||
 * are met:
 | 
			
		||||
 *
 | 
			
		||||
 * - Redistributions of source code must retain the above copyright
 | 
			
		||||
 * notice, this list of conditions and the following disclaimer.
 | 
			
		||||
 *
 | 
			
		||||
 * - Redistributions in binary form must reproduce the above copyright
 | 
			
		||||
 * notice, this list of conditions and the following disclaimer in the
 | 
			
		||||
 * documentation and/or other materials provided with the
 | 
			
		||||
 * distribution.
 | 
			
		||||
 *
 | 
			
		||||
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 | 
			
		||||
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 | 
			
		||||
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 | 
			
		||||
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 | 
			
		||||
 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
			
		||||
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 | 
			
		||||
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 | 
			
		||||
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 | 
			
		||||
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 | 
			
		||||
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 | 
			
		||||
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 | 
			
		||||
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef APPLE_THROW_HXX
 | 
			
		||||
#define APPLE_THROW_HXX
 | 
			
		||||
 | 
			
		||||
#include <CoreFoundation/CFBase.h>
 | 
			
		||||
 | 
			
		||||
namespace Apple {
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
ThrowOSStatus(OSStatus status);
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
ThrowOSStatus(OSStatus status, const char *msg);
 | 
			
		||||
 | 
			
		||||
} // namespace Apple
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
@@ -19,8 +19,8 @@
 | 
			
		||||
 | 
			
		||||
#include "config.h"
 | 
			
		||||
#include "OSXOutputPlugin.hxx"
 | 
			
		||||
#include "apple/ErrorRef.hxx"
 | 
			
		||||
#include "apple/StringRef.hxx"
 | 
			
		||||
#include "apple/Throw.hxx"
 | 
			
		||||
#include "../OutputAPI.hxx"
 | 
			
		||||
#include "mixer/MixerList.hxx"
 | 
			
		||||
#include "util/RuntimeError.hxx"
 | 
			
		||||
@@ -109,17 +109,6 @@ private:
 | 
			
		||||
 | 
			
		||||
static constexpr Domain osx_output_domain("osx_output");
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
osx_os_status_to_cstring(OSStatus status, char *str, size_t size)
 | 
			
		||||
{
 | 
			
		||||
	Apple::ErrorRef cferr(nullptr, kCFErrorDomainOSStatus, status, nullptr);
 | 
			
		||||
	const Apple::StringRef cfstr(cferr.CopyDescription());
 | 
			
		||||
	if (!cfstr.GetCString(str, size)) {
 | 
			
		||||
		/* conversion failed, return empty string */
 | 
			
		||||
		*str = '\0';
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool
 | 
			
		||||
osx_output_test_default_device()
 | 
			
		||||
{
 | 
			
		||||
@@ -209,11 +198,8 @@ OSXOutput::GetVolume()
 | 
			
		||||
						     &size,
 | 
			
		||||
						     &vol);
 | 
			
		||||
 | 
			
		||||
	if (status != noErr) {
 | 
			
		||||
		char errormsg[1024];
 | 
			
		||||
		osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
 | 
			
		||||
		throw FormatRuntimeError("unable to get volume: %s", errormsg);
 | 
			
		||||
	}
 | 
			
		||||
	if (status != noErr)
 | 
			
		||||
		Apple::ThrowOSStatus(status);
 | 
			
		||||
 | 
			
		||||
	return static_cast<int>(vol * 100.0);
 | 
			
		||||
}
 | 
			
		||||
@@ -235,12 +221,8 @@ OSXOutput::SetVolume(unsigned new_volume)
 | 
			
		||||
						     size,
 | 
			
		||||
						     &vol);
 | 
			
		||||
 | 
			
		||||
	if (status != noErr) {
 | 
			
		||||
		char errormsg[1024];
 | 
			
		||||
		osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
 | 
			
		||||
		throw FormatRuntimeError( "unable to set new volume %u: %s",
 | 
			
		||||
				new_volume, errormsg);
 | 
			
		||||
	}
 | 
			
		||||
	if (status != noErr)
 | 
			
		||||
		Apple::ThrowOSStatus(status);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -304,12 +286,9 @@ osx_output_set_channel_map(OSXOutput *oo)
 | 
			
		||||
		0,
 | 
			
		||||
		&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);
 | 
			
		||||
	}
 | 
			
		||||
	if (status != noErr)
 | 
			
		||||
		Apple::ThrowOSStatus(status,
 | 
			
		||||
				     "unable to get number of output device channels");
 | 
			
		||||
 | 
			
		||||
	UInt32 num_channels = desc.mChannelsPerFrame;
 | 
			
		||||
	std::unique_ptr<SInt32[]> channel_map(new SInt32[num_channels]);
 | 
			
		||||
@@ -325,11 +304,8 @@ osx_output_set_channel_map(OSXOutput *oo)
 | 
			
		||||
		0,
 | 
			
		||||
		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);
 | 
			
		||||
	}
 | 
			
		||||
	if (status != noErr)
 | 
			
		||||
		Apple::ThrowOSStatus(status, "unable to set channel map");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -640,12 +616,8 @@ 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);
 | 
			
		||||
	}
 | 
			
		||||
	if (status != noErr)
 | 
			
		||||
		Apple::ThrowOSStatus(status);
 | 
			
		||||
 | 
			
		||||
	/* what are the available audio device IDs? */
 | 
			
		||||
	numdevices = size / sizeof(AudioDeviceID);
 | 
			
		||||
@@ -653,12 +625,8 @@ osx_output_set_device(OSXOutput *oo)
 | 
			
		||||
	status = AudioObjectGetPropertyData(kAudioObjectSystemObject,
 | 
			
		||||
					    &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);
 | 
			
		||||
	}
 | 
			
		||||
	if (status != noErr)
 | 
			
		||||
		Apple::ThrowOSStatus(status);
 | 
			
		||||
 | 
			
		||||
	/* which audio device matches oo->device_name? */
 | 
			
		||||
	static constexpr AudioObjectPropertyAddress aopa_name{
 | 
			
		||||
@@ -701,12 +669,9 @@ osx_output_set_device(OSXOutput *oo)
 | 
			
		||||
				      0,
 | 
			
		||||
				      &(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);
 | 
			
		||||
	}
 | 
			
		||||
	if (status != noErr)
 | 
			
		||||
		Apple::ThrowOSStatus(status,
 | 
			
		||||
				     "Unable to set OS X audio output device");
 | 
			
		||||
 | 
			
		||||
	oo->dev_id = deviceids[i];
 | 
			
		||||
	FormatDebug(osx_output_domain,
 | 
			
		||||
@@ -757,12 +722,9 @@ OSXOutput::Enable()
 | 
			
		||||
		throw std::runtime_error("Error finding OS X component");
 | 
			
		||||
 | 
			
		||||
	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);
 | 
			
		||||
	}
 | 
			
		||||
	if (status != noErr)
 | 
			
		||||
		Apple::ThrowOSStatus(status, "Unable to open OS X component");
 | 
			
		||||
 | 
			
		||||
#ifdef ENABLE_DSD
 | 
			
		||||
	pcm_export.Construct();
 | 
			
		||||
#endif
 | 
			
		||||
@@ -883,21 +845,13 @@ 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);
 | 
			
		||||
	}
 | 
			
		||||
	if (status != noErr)
 | 
			
		||||
		Apple::ThrowOSStatus(status, "Unable to initialize OS X audio unit");
 | 
			
		||||
 | 
			
		||||
	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);
 | 
			
		||||
	}
 | 
			
		||||
	if (status != noErr)
 | 
			
		||||
		Apple::ThrowOSStatus(status, "Unable to set frame size");
 | 
			
		||||
 | 
			
		||||
	size_t ring_buffer_size = std::max<size_t>(buffer_frame_size,
 | 
			
		||||
						   MPD_OSX_BUFFER_TIME_MS * audio_format.GetFrameSize() * audio_format.sample_rate / 1000);
 | 
			
		||||
@@ -912,13 +866,9 @@ OSXOutput::Open(AudioFormat &audio_format)
 | 
			
		||||
	ring_buffer = new boost::lockfree::spsc_queue<uint8_t>(ring_buffer_size);
 | 
			
		||||
 | 
			
		||||
	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);
 | 
			
		||||
	}
 | 
			
		||||
	if (status != 0)
 | 
			
		||||
		Apple::ThrowOSStatus(status, "Unable to start audio output");
 | 
			
		||||
 | 
			
		||||
	pause = false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -72,7 +72,10 @@ if enable_oss
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
if is_darwin
 | 
			
		||||
  output_plugins_sources += 'OSXOutputPlugin.cxx'
 | 
			
		||||
  output_plugins_sources += [
 | 
			
		||||
    'OSXOutputPlugin.cxx',
 | 
			
		||||
    '../../apple/Throw.cxx',
 | 
			
		||||
  ]
 | 
			
		||||
  audiounit_dep = declare_dependency(
 | 
			
		||||
    link_args: [
 | 
			
		||||
      '-framework', 'AudioUnit', '-framework', 'CoreAudio', '-framework', 'CoreServices',
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user