Path: blob/master/drivers/coreaudio/audio_driver_coreaudio.h
9898 views
/**************************************************************************/1/* audio_driver_coreaudio.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#ifdef COREAUDIO_ENABLED3334#include "servers/audio_server.h"3536#import <AudioUnit/AudioUnit.h>37#ifdef MACOS_ENABLED38#import <CoreAudio/AudioHardware.h>39#else40#import <AVFoundation/AVFoundation.h>41#endif4243class AudioDriverCoreAudio : public AudioDriver {44AudioComponentInstance audio_unit = nullptr;45AudioComponentInstance input_unit = nullptr;4647bool active = false;48Mutex mutex;4950String output_device_name = "Default";51String input_device_name = "Default";5253int mix_rate = 0;54int capture_mix_rate = 0;55unsigned int channels = 2;56unsigned int capture_channels = 2;57unsigned int buffer_frames = 0;58unsigned int capture_buffer_frames = 0;5960Vector<int32_t> samples_in;61unsigned int buffer_size = 0;6263#ifdef MACOS_ENABLED64PackedStringArray _get_device_list(bool capture = false);65void _set_device(const String &output_device, bool capture = false);6667static OSStatus input_device_address_cb(AudioObjectID inObjectID,68UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses,69void *inClientData);7071static OSStatus output_device_address_cb(AudioObjectID inObjectID,72UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses,73void *inClientData);74#endif7576static OSStatus output_callback(void *inRefCon,77AudioUnitRenderActionFlags *ioActionFlags,78const AudioTimeStamp *inTimeStamp,79UInt32 inBusNumber, UInt32 inNumberFrames,80AudioBufferList *ioData);8182static OSStatus input_callback(void *inRefCon,83AudioUnitRenderActionFlags *ioActionFlags,84const AudioTimeStamp *inTimeStamp,85UInt32 inBusNumber, UInt32 inNumberFrames,86AudioBufferList *ioData);8788Error init_input_device();89void finish_input_device();9091public:92virtual const char *get_name() const override {93return "CoreAudio";94}9596virtual Error init() override;97virtual void start() override;98virtual int get_mix_rate() const override;99virtual int get_input_mix_rate() const override;100virtual SpeakerMode get_speaker_mode() const override;101102virtual void lock() override;103virtual void unlock() override;104virtual void finish() override;105106#ifdef MACOS_ENABLED107virtual PackedStringArray get_output_device_list() override;108virtual String get_output_device() override;109virtual void set_output_device(const String &p_name) override;110111virtual PackedStringArray get_input_device_list() override;112virtual String get_input_device() override;113virtual void set_input_device(const String &p_name) override;114#endif115116virtual Error input_start() override;117virtual Error input_stop() override;118119bool try_lock();120void stop();121122AudioDriverCoreAudio();123~AudioDriverCoreAudio() {}124};125126#endif // COREAUDIO_ENABLED127128129