Browse Source

Keep voice recording from opening an already owned microphone

master
evgeny 5 days ago
parent
commit
ce46677dfb
  1. 6
      tools/chatgui/src/audiorecorder.cpp
  2. 2
      tools/chatgui/src/audiorecorder.h

6
tools/chatgui/src/audiorecorder.cpp

@ -114,13 +114,14 @@ void AudioRecorder::shutdown() {
void AudioRecorder::startRecording(bool meterOnly) {
if (m_recording) return;
if (!m_initialized) { DEBUG_ERROR(DEBUG_CATEGORY_CALL, "recorder: not initialized"); return; }
if (!m_microphone.acquire()) { DEBUG_WARN(DEBUG_CATEGORY_CALL, "recorder: microphone owned by another operation"); return; }
m_pcmBuffer.clear(); m_meterOnly = meterOnly;
m_captureQueue.reset(); m_captureFailed = false;
m_recordedFrames = 0; m_elapsedMs = 0; m_peakLevel = 0;
m_captureCompressorEnabled = !meterOnly && m_compressorEnabled;
if (m_compressor) {
if (audio_compressor_configure(m_compressor, &m_compressorConfig) != 0) {
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "recorder: cannot configure compressor"); return;
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "recorder: cannot configure compressor"); m_microphone.release(); return;
}
audio_compressor_set_enabled(m_compressor, m_captureCompressorEnabled);
audio_compressor_reset(m_compressor);
@ -129,7 +130,7 @@ void AudioRecorder::startRecording(bool meterOnly) {
try { m_recordWorker = std::thread(&AudioRecorder::recordLoop, this); }
catch (const std::system_error& error) {
m_workerRunning = false;
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "recorder: worker start failed reason=%s", error.what()); return;
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "recorder: worker start failed reason=%s", error.what()); m_microphone.release(); return;
}
m_recording = true;
m_capture.retryNow();
@ -173,6 +174,7 @@ void AudioRecorder::stopRecording() {
m_capture.close();
m_workerRunning = false; m_wake.notify_one();
if (m_recordWorker.joinable()) m_recordWorker.join();
m_microphone.release();
m_elapsedMs = (int)(m_recordedFrames.load() * 1000 / m_sampleRate);
int duration = m_elapsedMs;
size_t samples = m_compressor && m_captureCompressorEnabled ? audio_compressor_output_size(m_compressor) : m_pcmBuffer.size();

2
tools/chatgui/src/audiorecorder.h

@ -9,6 +9,7 @@
#include <vector>
#include <cstdint>
#include "audio_device.h"
#include "audio_capture_lease.h"
#include "audio_event_queue.h"
#include <thread>
#include <mutex>
@ -53,6 +54,7 @@ signals:
private:
AudioDevice m_capture;
AudioCaptureLease m_microphone;
QTimer m_recoveryTimer;
void recoverCapture();
void captureCallback(const void* input, unsigned int frames);

Loading…
Cancel
Save