Reduce cpu usage

This commit is contained in:
Benau 2017-04-02 10:28:46 +08:00
parent 9bfe4200c4
commit ab2ce24914
2 changed files with 60 additions and 59 deletions

View File

@ -460,6 +460,7 @@ namespace Recorder
{ {
pcm_data.lock(); pcm_data.lock();
pcm_data.getData().push_back(each_pcm_buf); pcm_data.getData().push_back(each_pcm_buf);
pcm_data.getData().push_back(NULL);
pthread_cond_signal(&enc_request); pthread_cond_signal(&enc_request);
pcm_data.unlock(); pcm_data.unlock();
break; break;
@ -498,10 +499,6 @@ namespace Recorder
} }
g_pa_data.pa_stream_drop(stream); g_pa_data.pa_stream_drop(stream);
} }
pcm_data.lock();
pcm_data.getData().push_back(NULL);
pthread_cond_signal(&enc_request);
pcm_data.unlock();
pthread_join(vorbis_enc, NULL); pthread_join(vorbis_enc, NULL);
pthread_cond_destroy(&enc_request); pthread_cond_destroy(&enc_request);
g_pa_data.pa_stream_disconnect(stream); g_pa_data.pa_stream_disconnect(stream);

View File

@ -177,9 +177,18 @@ namespace Recorder
Log::error("WasapiRecorder", "Unsupported audio input format"); Log::error("WasapiRecorder", "Unsupported audio input format");
return NULL; return NULL;
} }
HRESULT hr = g_wasapi_data.m_client->Start(); HRESULT hr = g_wasapi_data.m_client->Reset();
if (FAILED(hr)) if (FAILED(hr))
{
Log::error("WasapiRecorder", "Failed to reset recorder");
return NULL; return NULL;
}
hr = g_wasapi_data.m_client->Start();
if (FAILED(hr))
{
Log::error("WasapiRecorder", "Failed to start recorder");
return NULL;
}
REFERENCE_TIME duration = REFTIMES_PER_SEC * REFERENCE_TIME duration = REFTIMES_PER_SEC *
g_wasapi_data.m_buffer_size / g_wasapi_data.m_wav_format g_wasapi_data.m_buffer_size / g_wasapi_data.m_wav_format
->nSamplesPerSec; ->nSamplesPerSec;
@ -202,76 +211,71 @@ namespace Recorder
{ {
audio_data.lock(); audio_data.lock();
audio_data.getData().push_back(each_audio_buf); audio_data.getData().push_back(each_audio_buf);
audio_data.getData().push_back(NULL);
pthread_cond_signal(&enc_request); pthread_cond_signal(&enc_request);
audio_data.unlock(); audio_data.unlock();
break; break;
} }
REFERENCE_TIME sleep_time = duration / 10000 / 2; uint32_t packet_length = 0;
Sleep((uint32_t)sleep_time);
uint32_t packet_length;
hr = g_wasapi_data.m_capture_client->GetNextPacketSize( hr = g_wasapi_data.m_capture_client->GetNextPacketSize(
&packet_length); &packet_length);
if (FAILED(hr)) if (FAILED(hr))
return NULL;
while (packet_length != 0)
{ {
BYTE* data; Log::warn("WasapiRecorder", "Failed to get next packet size");
uint32_t frame_size; }
DWORD flags; if (packet_length == 0)
hr = g_wasapi_data.m_capture_client->GetBuffer(&data, {
&frame_size, &flags, NULL, NULL); REFERENCE_TIME sleep_time = duration / 10000 / 2;
if (FAILED(hr)) Sleep((uint32_t)sleep_time);
return NULL; continue;
const unsigned bytes = ved.m_channels * }
(g_wasapi_data.m_wav_format->wBitsPerSample / 8) * BYTE* data;
frame_size; DWORD flags;
bool buf_full = readed + bytes > frag_size; hr = g_wasapi_data.m_capture_client->GetBuffer(&data,
unsigned copy_size = buf_full ? frag_size - readed : bytes; &packet_length, &flags, NULL, NULL);
if (FAILED(hr))
{
Log::warn("WasapiRecorder", "Failed to get buffer");
}
const unsigned bytes = ved.m_channels * (g_wasapi_data.m_wav_format
->wBitsPerSample / 8) * packet_length;
bool buf_full = readed + bytes > frag_size;
unsigned copy_size = buf_full ? frag_size - readed : bytes;
if (!(flags & AUDCLNT_BUFFERFLAGS_SILENT))
{
memcpy(each_audio_buf + readed, data, copy_size);
}
if (buf_full)
{
audio_data.lock();
audio_data.getData().push_back(each_audio_buf);
pthread_cond_signal(&enc_request);
audio_data.unlock();
each_audio_buf = new int8_t[frag_size]();
readed = bytes - copy_size;
if (!(flags & AUDCLNT_BUFFERFLAGS_SILENT)) if (!(flags & AUDCLNT_BUFFERFLAGS_SILENT))
{ {
memcpy(each_audio_buf + readed, data, copy_size); memcpy(each_audio_buf, (uint8_t*)data + copy_size, readed);
} }
if (buf_full) }
{ else
audio_data.lock(); {
audio_data.getData().push_back(each_audio_buf); readed += bytes;
pthread_cond_signal(&enc_request); }
audio_data.unlock(); hr = g_wasapi_data.m_capture_client->ReleaseBuffer(packet_length);
each_audio_buf = new int8_t[frag_size](); if (FAILED(hr))
readed = bytes - copy_size; {
if (!(flags & AUDCLNT_BUFFERFLAGS_SILENT)) Log::warn("WasapiRecorder", "Failed to release buffer");
{
memcpy(each_audio_buf, (uint8_t*)data + copy_size,
readed);
}
}
else
{
readed += bytes;
}
hr = g_wasapi_data.m_capture_client->ReleaseBuffer(frame_size);
if (FAILED(hr))
return NULL;
if (idle->getAtomic())
{
break;
}
hr = g_wasapi_data.m_capture_client->GetNextPacketSize(
&frame_size);
if (FAILED(hr))
return NULL;
} }
} }
audio_data.lock(); hr = g_wasapi_data.m_client->Stop();
audio_data.getData().push_back(NULL); if (FAILED(hr))
pthread_cond_signal(&enc_request); {
audio_data.unlock(); Log::warn("WasapiRecorder", "Failed to stop recorder");
}
pthread_join(vorbis_enc, NULL); pthread_join(vorbis_enc, NULL);
pthread_cond_destroy(&enc_request); pthread_cond_destroy(&enc_request);
hr = g_wasapi_data.m_client->Stop();
if (FAILED(hr))
return NULL;
return NULL; return NULL;
} // audioRecorder } // audioRecorder
} }