Skip to content

Commit

Permalink
Merge pull request #35359 from akien-mga/revert-30648-unsafe-audioserver
Browse files Browse the repository at this point in the history
Revert "Exposes capture methods to AudioServer + documentation" #30468
  • Loading branch information
akien-mga authored Jan 20, 2020
2 parents c3fd101 + 837adb3 commit cfeba2c
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 156 deletions.
53 changes: 11 additions & 42 deletions doc/classes/AudioServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,26 @@
Adds an [AudioEffect] effect to the bus [code]bus_idx[/code] at [code]at_position[/code].
</description>
</method>
<method name="capture_get_device_list">
<return type="Array">
<method name="capture_get_device">
<return type="String">
</return>
<description>
Returns the names of all audio input devices detected on the system.
Name of the current device for audio input (see [method capture_get_device_list]).
</description>
</method>
<method name="capture_start">
<return type="int" enum="Error">
<method name="capture_get_device_list">
<return type="Array">
</return>
<description>
Attempts to start recording from the audio driver's capture device. On success, the return value is [constant OK].
Returns the names of all audio input devices detected on the system.
</description>
</method>
<method name="capture_stop">
<return type="int" enum="Error">
<method name="capture_set_device">
<return type="void">
</return>
<argument index="0" name="name" type="String">
</argument>
<description>
Attempts to stop recording from the audio driver's capture device. On success, the return value is [constant OK].
</description>
</method>
<method name="generate_bus_layout" qualifiers="const">
Expand Down Expand Up @@ -159,32 +160,11 @@
Returns the volume of the bus at index [code]bus_idx[/code] in dB.
</description>
</method>
<method name="get_capture_buffer">
<return type="PoolIntArray">
</return>
<description>
Returns an [PoolIntArray] containing audio frames from the capture device.
</description>
</method>
<method name="get_capture_position">
<return type="int">
</return>
<description>
Returns the write position of the capture device buffer.
</description>
</method>
<method name="get_capture_size">
<return type="int">
</return>
<description>
Returns the size of the capture device buffer.
</description>
</method>
<method name="get_device_list">
<return type="Array">
</return>
<description>
Returns the names of all audio output devices detected on the system.
Returns the names of all audio devices detected on the system.
</description>
</method>
<method name="get_mix_rate" qualifiers="const">
Expand Down Expand Up @@ -409,9 +389,6 @@
<member name="bus_count" type="int" setter="set_bus_count" getter="get_bus_count" default="1">
Number of available audio buses.
</member>
<member name="capture_device" type="String" setter="capture_set_device" getter="capture_get_device" default="&quot;&quot;">
Name of the current device for audio input (see [method capture_get_device_list]).
</member>
<member name="device" type="String" setter="set_device" getter="get_device" default="&quot;Default&quot;">
Name of the current device for audio output (see [method get_device_list]).
</member>
Expand All @@ -420,14 +397,6 @@
</member>
</members>
<signals>
<signal name="audio_mix_callback">
<description>
</description>
</signal>
<signal name="audio_update_callback">
<description>
</description>
</signal>
<signal name="bus_layout_changed">
<description>
Emitted when the [AudioBusLayout] changes.
Expand Down
14 changes: 7 additions & 7 deletions drivers/coreaudio/audio_driver_coreaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ OSStatus AudioDriverCoreAudio::input_callback(void *inRefCon,
if (result == noErr) {
for (unsigned int i = 0; i < inNumberFrames * ad->capture_channels; i++) {
int32_t sample = ad->input_buf[i] << 16;
ad->capture_buffer_write(sample);
ad->input_buffer_write(sample);

if (ad->capture_channels == 1) {
// In case capture device is single channel convert it to Stereo
ad->capture_buffer_write(sample);
// In case input device is single channel convert it to Stereo
ad->input_buffer_write(sample);
}
}
} else {
Expand Down Expand Up @@ -487,7 +487,7 @@ void AudioDriverCoreAudio::capture_finish() {

Error AudioDriverCoreAudio::capture_start() {

capture_buffer_init(buffer_frames);
input_buffer_init(buffer_frames);

OSStatus result = AudioOutputUnitStart(input_unit);
if (result != noErr) {
Expand Down Expand Up @@ -642,9 +642,9 @@ void AudioDriverCoreAudio::_set_device(const String &device, bool capture) {
ERR_FAIL_COND(result != noErr);

if (capture) {
// Reset audio capture to keep synchronisation.
capture_position = 0;
capture_size = 0;
// Reset audio input to keep synchronisation.
input_position = 0;
input_size = 0;
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions drivers/pulseaudio/audio_driver_pulseaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ Error AudioDriverPulseAudio::init_device() {
samples_out.resize(pa_buffer_size);

// Reset audio input to keep synchronisation.
capture_position = 0;
capture_size = 0;
input_position = 0;
input_size = 0;

return OK;
}
Expand Down Expand Up @@ -465,7 +465,7 @@ void AudioDriverPulseAudio::thread_func(void *p_udata) {
size_t bytes = pa_stream_readable_size(ad->pa_rec_str);
if (bytes > 0) {
const void *ptr = NULL;
size_t maxbytes = ad->capture_buffer.size() * sizeof(int16_t);
size_t maxbytes = ad->input_buffer.size() * sizeof(int16_t);

bytes = MIN(bytes, maxbytes);
ret = pa_stream_peek(ad->pa_rec_str, &ptr, &bytes);
Expand All @@ -475,11 +475,11 @@ void AudioDriverPulseAudio::thread_func(void *p_udata) {
int16_t *srcptr = (int16_t *)ptr;
for (size_t i = bytes >> 1; i > 0; i--) {
int32_t sample = int32_t(*srcptr++) << 16;
ad->capture_buffer_write(sample);
ad->input_buffer_write(sample);

if (ad->pa_rec_map.channels == 1) {
// In case capture device is single channel convert it to Stereo
ad->capture_buffer_write(sample);
// In case input device is single channel convert it to Stereo
ad->input_buffer_write(sample);
}
}

Expand Down Expand Up @@ -666,7 +666,7 @@ Error AudioDriverPulseAudio::capture_init_device() {
break;

default:
WARN_PRINTS("PulseAudio: Unsupported number of capture channels: " + itos(pa_rec_map.channels));
WARN_PRINTS("PulseAudio: Unsupported number of input channels: " + itos(pa_rec_map.channels));
pa_channel_map_init_stereo(&pa_rec_map);
break;
}
Expand Down Expand Up @@ -698,10 +698,10 @@ Error AudioDriverPulseAudio::capture_init_device() {
ERR_FAIL_V(ERR_CANT_OPEN);
}

capture_buffer_init(input_buffer_frames);
input_buffer_init(input_buffer_frames);

print_verbose("PulseAudio: detected " + itos(pa_rec_map.channels) + " capture channels");
print_verbose("PulseAudio: capture buffer frames: " + itos(input_buffer_frames) + " calculated latency: " + itos(input_buffer_frames * 1000 / mix_rate) + "ms");
print_verbose("PulseAudio: detected " + itos(pa_rec_map.channels) + " input channels");
print_verbose("PulseAudio: input buffer frames: " + itos(input_buffer_frames) + " calculated latency: " + itos(input_buffer_frames * 1000 / mix_rate) + "ms");

return OK;
}
Expand Down
10 changes: 5 additions & 5 deletions drivers/wasapi/audio_driver_wasapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ Error AudioDriverWASAPI::init_render_device(bool reinit) {
// Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels)
samples_in.resize(buffer_frames * channels);

capture_position = 0;
capture_size = 0;
input_position = 0;
input_size = 0;

print_verbose("WASAPI: detected " + itos(channels) + " channels");
print_verbose("WASAPI: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms");
Expand All @@ -362,7 +362,7 @@ Error AudioDriverWASAPI::init_capture_device(bool reinit) {
HRESULT hr = audio_input.audio_client->GetBufferSize(&max_frames);
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);

capture_buffer_init(max_frames);
input_buffer_init(max_frames);

return OK;
}
Expand Down Expand Up @@ -715,8 +715,8 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
}
}

ad->capture_buffer_write(l);
ad->capture_buffer_write(r);
ad->input_buffer_write(l);
ad->input_buffer_write(r);
}

read_frames += num_frames_available;
Expand Down
6 changes: 3 additions & 3 deletions platform/android/audio_driver_opensl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ void AudioDriverOpenSL::_record_buffer_callback(SLAndroidSimpleBufferQueueItf qu

for (int i = 0; i < rec_buffer.size(); i++) {
int32_t sample = rec_buffer[i] << 16;
capture_buffer_write(sample);
capture_buffer_write(sample); // call twice to convert to Stereo
input_buffer_write(sample);
input_buffer_write(sample); // call twice to convert to Stereo
}

SLresult res = (*recordBufferQueueItf)->Enqueue(recordBufferQueueItf, rec_buffer.ptrw(), rec_buffer.size() * sizeof(int16_t));
Expand Down Expand Up @@ -280,7 +280,7 @@ Error AudioDriverOpenSL::capture_init_device() {

const int rec_buffer_frames = 2048;
rec_buffer.resize(rec_buffer_frames);
capture_buffer_init(rec_buffer_frames);
input_buffer_init(rec_buffer_frames);

res = (*recordBufferQueueItf)->Enqueue(recordBufferQueueItf, rec_buffer.ptrw(), rec_buffer.size() * sizeof(int16_t));
ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
Expand Down
42 changes: 21 additions & 21 deletions servers/audio/audio_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,31 +134,31 @@ AudioStreamMicrophone::AudioStreamMicrophone() {

void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_frames) {

AudioServer::get_singleton()->lock();
AudioDriver::get_singleton()->lock();

PoolVector<int32_t> capture_buffer = AudioServer::get_singleton()->get_capture_buffer();
unsigned int capture_size = AudioServer::get_singleton()->get_capture_size();
int mix_rate = AudioServer::get_singleton()->get_mix_rate();
unsigned int playback_delay = MIN(((50 * mix_rate) / 1000) * 2, capture_buffer.size() >> 1);
Vector<int32_t> buf = AudioDriver::get_singleton()->get_input_buffer();
unsigned int input_size = AudioDriver::get_singleton()->get_input_size();
int mix_rate = AudioDriver::get_singleton()->get_mix_rate();
unsigned int playback_delay = MIN(((50 * mix_rate) / 1000) * 2, buf.size() >> 1);
#ifdef DEBUG_ENABLED
unsigned int capture_position = AudioServer::get_singleton()->get_capture_position();
unsigned int input_position = AudioDriver::get_singleton()->get_input_position();
#endif

if (playback_delay > capture_size) {
if (playback_delay > input_size) {
for (int i = 0; i < p_frames; i++) {
p_buffer[i] = AudioFrame(0.0f, 0.0f);
}
capture_ofs = 0;
input_ofs = 0;
} else {
for (int i = 0; i < p_frames; i++) {
if (capture_size > capture_ofs && (int)capture_ofs < capture_buffer.size()) {
float l = (capture_buffer[capture_ofs++] >> 16) / 32768.f;
if ((int)capture_ofs >= capture_buffer.size()) {
capture_ofs = 0;
if (input_size > input_ofs && (int)input_ofs < buf.size()) {
float l = (buf[input_ofs++] >> 16) / 32768.f;
if ((int)input_ofs >= buf.size()) {
input_ofs = 0;
}
float r = (capture_buffer[capture_ofs++] >> 16) / 32768.f;
if ((int)capture_ofs >= capture_buffer.size()) {
capture_ofs = 0;
float r = (buf[input_ofs++] >> 16) / 32768.f;
if ((int)input_ofs >= buf.size()) {
input_ofs = 0;
}

p_buffer[i] = AudioFrame(l, r);
Expand All @@ -169,12 +169,12 @@ void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_fr
}

#ifdef DEBUG_ENABLED
if (capture_ofs > capture_position && (int)(capture_ofs - capture_position) < (p_frames * 2)) {
print_verbose(String(get_class_name()) + " buffer underrun: capture_position=" + itos(capture_position) + " capture_ofs=" + itos(capture_ofs) + " capture_size=" + itos(capture_size));
if (input_ofs > input_position && (int)(input_ofs - input_position) < (p_frames * 2)) {
print_verbose(String(get_class_name()) + " buffer underrun: input_position=" + itos(input_position) + " input_ofs=" + itos(input_ofs) + " input_size=" + itos(input_size));
}
#endif

AudioServer::get_singleton()->unlock();
AudioDriver::get_singleton()->unlock();
}

void AudioStreamPlaybackMicrophone::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
Expand All @@ -196,17 +196,17 @@ void AudioStreamPlaybackMicrophone::start(float p_from_pos) {
return;
}

capture_ofs = 0;
input_ofs = 0;

if (AudioServer::get_singleton()->capture_start() == OK) {
if (AudioDriver::get_singleton()->capture_start() == OK) {
active = true;
_begin_resample();
}
}

void AudioStreamPlaybackMicrophone::stop() {
if (active) {
AudioServer::get_singleton()->capture_stop();
AudioDriver::get_singleton()->capture_stop();
active = false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion servers/audio/audio_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class AudioStreamPlaybackMicrophone : public AudioStreamPlaybackResampled {
friend class AudioStreamMicrophone;

bool active;
unsigned int capture_ofs;
unsigned int input_ofs;

Ref<AudioStreamMicrophone> microphone;

Expand Down
Loading

0 comments on commit cfeba2c

Please sign in to comment.