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,29 +211,34 @@ 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)
{ {
Log::warn("WasapiRecorder", "Failed to get next packet size");
}
if (packet_length == 0)
{
REFERENCE_TIME sleep_time = duration / 10000 / 2;
Sleep((uint32_t)sleep_time);
continue;
}
BYTE* data; BYTE* data;
uint32_t frame_size;
DWORD flags; DWORD flags;
hr = g_wasapi_data.m_capture_client->GetBuffer(&data, hr = g_wasapi_data.m_capture_client->GetBuffer(&data,
&frame_size, &flags, NULL, NULL); &packet_length, &flags, NULL, NULL);
if (FAILED(hr)) if (FAILED(hr))
return NULL; {
const unsigned bytes = ved.m_channels * Log::warn("WasapiRecorder", "Failed to get buffer");
(g_wasapi_data.m_wav_format->wBitsPerSample / 8) * }
frame_size; const unsigned bytes = ved.m_channels * (g_wasapi_data.m_wav_format
->wBitsPerSample / 8) * packet_length;
bool buf_full = readed + bytes > frag_size; bool buf_full = readed + bytes > frag_size;
unsigned copy_size = buf_full ? frag_size - readed : bytes; unsigned copy_size = buf_full ? frag_size - readed : bytes;
if (!(flags & AUDCLNT_BUFFERFLAGS_SILENT)) if (!(flags & AUDCLNT_BUFFERFLAGS_SILENT))
@ -241,37 +255,27 @@ namespace Recorder
readed = bytes - copy_size; readed = bytes - copy_size;
if (!(flags & AUDCLNT_BUFFERFLAGS_SILENT)) if (!(flags & AUDCLNT_BUFFERFLAGS_SILENT))
{ {
memcpy(each_audio_buf, (uint8_t*)data + copy_size, memcpy(each_audio_buf, (uint8_t*)data + copy_size, readed);
readed);
} }
} }
else else
{ {
readed += bytes; readed += bytes;
} }
hr = g_wasapi_data.m_capture_client->ReleaseBuffer(frame_size); hr = g_wasapi_data.m_capture_client->ReleaseBuffer(packet_length);
if (FAILED(hr)) if (FAILED(hr))
return NULL;
if (idle->getAtomic())
{ {
break; Log::warn("WasapiRecorder", "Failed to release buffer");
} }
hr = g_wasapi_data.m_capture_client->GetNextPacketSize( }
&frame_size); hr = g_wasapi_data.m_client->Stop();
if (FAILED(hr)) if (FAILED(hr))
return NULL; {
Log::warn("WasapiRecorder", "Failed to stop recorder");
} }
}
audio_data.lock();
audio_data.getData().push_back(NULL);
pthread_cond_signal(&enc_request);
audio_data.unlock();
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
} }