*/
-#include <mm.h>
#include "CAudioIODef.h"
+#include <unistd.h>
+#include <inttypes.h>
#ifdef ENABLE_DPM
#include <dpm/restriction.h>
#endif
* class CPulseAudioClient
*/
const char* CPulseAudioClient::CLIENT_NAME = "AUDIO_IO_PA_CLIENT";
+static const unsigned int drain_wait_interval = 10000;
+static const unsigned int drain_wait_max_count = 30;
CPulseAudioClient::CPulseAudioClient(
EStreamDirection direction,
__mpSyncReadDataPtr(NULL),
__mSyncReadIndex(0),
__mSyncReadLength(0),
- __mIsUsedSyncRead(false) {
+ __mIsUsedSyncRead(false),
+ __mIsFirstStream(false),
+ __mIsDraining(false),
+ __mIsStarted(false) {
}
CPulseAudioClient::~CPulseAudioClient() {
case PA_STREAM_READY:
AUDIO_IO_LOGD("The stream is ready");
pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
+ if (pClient->__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK)
+ pClient->__mIsFirstStream = true;
+ pClient->__mIsInit = true;
pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
break;
pClient->__mpListener->onStream(pClient, length);
}
+#ifndef DISABLE_MOBILE_BACK_COMP
+static void __dummy_write(pa_stream* s, size_t length) {
+ char* dummy = new char[length];
+ memset(dummy, 0, length);
+ pa_stream_write(s, dummy, length, NULL, 0LL, PA_SEEK_RELATIVE);
+ delete [] dummy;
+}
+#endif
+
void CPulseAudioClient::__streamPlaybackCb(pa_stream* s, size_t length, void* user_data) {
assert(s);
assert(user_data);
CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
assert(pClient->__mpListener);
+#ifndef DISABLE_MOBILE_BACK_COMP
if (pClient->__mIsInit == false) {
AUDIO_IO_LOGD("Occurred this listener when an out stream is on the way to create : Write dummy, length[%d]", length);
-
- char* dummy = new char[length];
- memset(dummy, 0, length);
- pa_stream_write(s, dummy, length, NULL, 0LL, PA_SEEK_RELATIVE);
- delete [] dummy;
-
+ __dummy_write(s, length);
+ return;
+ }
+ if (pClient->isCorked()) {
+ AUDIO_IO_LOGD("Occurred this listener when an out stream is CORKED : Write dummy, length[%d]", length);
+ __dummy_write(s, length);
return;
}
+#endif
pClient->__mpListener->onStream(pClient, length);
+
+#ifndef DISABLE_MOBILE_BACK_COMP
+ /* If stream is not written in first callback during prepare,
+ then write dummy data to ensure the start */
+ if (pClient->__mSpec.getStreamLatency() == CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_OUTPUT_DEFAULT_ASYNC &&
+ pClient->__mIsFirstStream) {
+ AUDIO_IO_LOGW("[async] Write dummy of length[%d] since not written in first callback during prepare", length);
+ __dummy_write(s, length);
+ pClient->__mIsFirstStream = false;
+ }
+#endif
}
void CPulseAudioClient::__streamLatencyUpdateCb(pa_stream* s, void* user_data) {
pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
}
+void CPulseAudioClient::__streamStartedCb(pa_stream* s, void* user_data) {
+ assert(s);
+ assert(user_data);
+
+ CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
+
+ AUDIO_IO_LOGD("stream %p started.", pClient);
+
+ pClient->__mIsStarted = true;
+}
+
+void CPulseAudioClient::__streamUnderflowCb(pa_stream* s, void* user_data) {
+ assert(s);
+ assert(user_data);
+
+ CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
+
+ AUDIO_IO_LOGD("stream %p UnderFlow...", pClient);
+
+ pClient->__mIsStarted = false;
+}
+
void CPulseAudioClient::__streamEventCb(pa_stream* s, const char *name, pa_proplist *pl, void *user_data) {
assert(s);
assert(user_data);
pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
}
-void CPulseAudioClient::initialize() throw(CAudioError) {
- AUDIO_IO_LOGD("");
+void CPulseAudioClient::__successDrainCbInThread(pa_stream* s, int success, void* user_data) {
+ AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
+ assert(s);
+ assert(user_data);
+
+ CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
+ pClient->__mIsOperationSuccess = static_cast<bool>(success);
+ pClient->__mIsDraining = false;
+}
+
+void CPulseAudioClient::__successDrainCb(pa_stream* s, int success, void* user_data) {
+ AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
+ assert(s);
+ assert(user_data);
+
+ CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
+ pClient->__mIsOperationSuccess = static_cast<bool>(success);
+ pClient->__mIsDraining = false;
+
+ pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
+}
+
+void CPulseAudioClient::initialize() {
if (__mIsInit == true) {
return;
}
}
// Adds latency on proplist for delivery to PULSEAUDIO
- AUDIO_IO_LOGD("LATENCY : %s[%d]", __mSpec.getStreamLatencyToString(), __mSpec.getStreamLatency());
+ AUDIO_IO_LOGD("LATENCY : %s[%d]", __mSpec.getStreamLatencyToString(), static_cast<int>(__mSpec.getStreamLatency()));
pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_TIZEN_AUDIO_LATENCY, "%s", __mSpec.getStreamLatencyToString());
// Allocates PA mainloop
pa_stream_set_write_callback(__mpStream, __streamPlaybackCb, this);
pa_stream_set_latency_update_callback(__mpStream, __streamLatencyUpdateCb, this);
pa_stream_set_event_callback(__mpStream, __streamEventCb, this);
+ if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
+ pa_stream_set_started_callback(__mpStream, __streamStartedCb, this);
+ pa_stream_set_underflow_callback(__mpStream, __streamUnderflowCb, this);
+ }
// Connect stream with PA Server
pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
PA_STREAM_INTERPOLATE_TIMING |
PA_STREAM_ADJUST_LATENCY |
+#ifndef DISABLE_MOBILE_BACK_COMP
+ PA_STREAM_START_CORKED |
+#endif
PA_STREAM_AUTO_TIMING_UPDATE);
ret = pa_stream_connect_playback(__mpStream, NULL, NULL, flags, NULL, NULL);
pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
PA_STREAM_INTERPOLATE_TIMING |
PA_STREAM_ADJUST_LATENCY |
+#ifndef DISABLE_MOBILE_BACK_COMP
+ PA_STREAM_START_CORKED |
+#endif
PA_STREAM_AUTO_TIMING_UPDATE);
ret = pa_stream_connect_record(__mpStream, NULL, NULL, flags);
// End of synchronous
pa_threaded_mainloop_unlock(__mpMainloop);
- __mIsInit = true;
- } catch (CAudioError e) {
+ // __mIsInit = true; // Moved to __streamStateChangeCb()
+ } catch (CAudioError& e) {
finalize();
- throw e;
+ throw;
}
}
void CPulseAudioClient::finalize() {
AUDIO_IO_LOGD("");
- if (__mIsInit == false) {
- return;
+
+ if (__mpMainloop && isInThread() == false)
+ pa_threaded_mainloop_lock(__mpMainloop);
+
+ /* clear callbacks */
+ if (__mpStream) {
+ if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK)
+ pa_stream_set_write_callback(__mpStream, NULL, NULL);
+ else
+ pa_stream_set_read_callback(__mpStream, NULL, NULL);
+ pa_stream_set_latency_update_callback(__mpStream, NULL, NULL);
+ pa_stream_set_event_callback(__mpStream, NULL, NULL);
+ }
+
+ if (__mpMainloop && isInThread() == false)
+ pa_threaded_mainloop_unlock(__mpMainloop);
+
+ /* Wait for drain complete if draining before finalize */
+ if (__mIsDraining) {
+ unsigned int drain_wait_count = 0;
+ while (__mIsDraining && drain_wait_count++ < drain_wait_max_count) {
+ usleep(drain_wait_interval);
+ }
+ AUDIO_IO_LOGD("wait for drain complete!!!! [%d * %d usec]",
+ drain_wait_count, drain_wait_interval);
}
if (__mpMainloop != NULL) {
__mIsInit = false;
}
-int CPulseAudioClient::read(void* buffer, size_t length) throw(CAudioError) {
+int CPulseAudioClient::read(void* buffer, size_t length) {
if (__mIsInit == false) {
THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
}
pa_threaded_mainloop_unlock(__mpMainloop);
__mIsUsedSyncRead = false;
- } catch (CAudioError e) {
+ } catch (CAudioError& e) {
pa_threaded_mainloop_unlock(__mpMainloop);
__mIsUsedSyncRead = false;
- throw e;
+ throw;
}
return length;
}
-int CPulseAudioClient::peek(const void** buffer, size_t* length) throw(CAudioError) {
+int CPulseAudioClient::peek(const void** buffer, size_t* length) {
if (__mIsInit == false) {
THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
}
return ret;
}
-int CPulseAudioClient::drop() throw(CAudioError) {
+int CPulseAudioClient::drop() {
if (__mIsInit == false) {
THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
}
return ret;
}
-int CPulseAudioClient::write(const void* data, size_t length) throw(CAudioError) {
- if (__mIsInit == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
- }
-
- checkRunningState();
-
+int CPulseAudioClient::write(const void* data, size_t length) {
if (data == NULL) {
THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid");
}
int ret = 0;
#ifdef _AUDIO_IO_DEBUG_TIMING_
- AUDIO_IO_LOGD("data[%p], length[%d]", data, length);
+ AUDIO_IO_LOGD("data[%p], length[%d], First[%d]", data, length, __mIsFirstStream);
#endif
if (pa_stream_is_corked(__mpStream)) {
AUDIO_IO_LOGW("stream is corked...do uncork here first!!!!");
ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
pa_threaded_mainloop_unlock(__mpMainloop);
} else {
+ if (__mIsFirstStream) {
+ const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
+ uint32_t prebuf = attr->prebuf;
+ // Compensate amount of prebuf in first stream callback when audio-io use prebuf(-1)
+ // Because a stream will never start when an application wrote less than prebuf at first
+ if (length < prebuf) {
+ char* dummy = new char[prebuf - length];
+ memset(dummy, 0, prebuf - length);
+ pa_stream_write(__mpStream, dummy, prebuf - length, NULL, 0LL, PA_SEEK_RELATIVE);
+ delete [] dummy;
+ }
+ __mIsFirstStream = false;
+ AUDIO_IO_LOGW("FIRST STREAM CALLBACK : length[%d], prebuf[%d], dummy[%d]",
+ length, prebuf, (length < prebuf) ? prebuf - length : 0);
+ }
ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
}
return ret;
}
-void CPulseAudioClient::cork(bool cork) throw(CAudioError) {
+void CPulseAudioClient::cork(bool cork) {
AUDIO_IO_LOGD("cork[%d]", cork);
if (__mIsInit == false) {
checkRunningState();
+ // Set __mIsFirstStream flag when uncork(resume) stream, because prebuf will be enable again
+ if (cork == false)
+ __mIsFirstStream = true;
+
if (isInThread() == false) {
pa_threaded_mainloop_lock(__mpMainloop);
pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
return;
}
-bool CPulseAudioClient::isCorked() throw(CAudioError) {
+bool CPulseAudioClient::isCorked() {
if (__mIsInit == false) {
THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
}
isCork = pa_stream_is_corked(__mpStream);
}
+#ifdef _AUDIO_IO_DEBUG_TIMING_
AUDIO_IO_LOGD("isCork[%d]", isCork);
+#endif
return static_cast<bool>(isCork);
}
-bool CPulseAudioClient::drain() throw(CAudioError) {
- AUDIO_IO_LOGD("drain");
-
+bool CPulseAudioClient::drain() {
if (__mIsInit == false) {
THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
}
checkRunningState();
+ if (isCorked()) {
+ AUDIO_IO_LOGW("Corked...");
+ return true;
+ }
+
+ if (__mIsDraining) {
+ AUDIO_IO_LOGW("already draining...");
+ return true;
+ }
+
+ if (!__mIsStarted) {
+ AUDIO_IO_LOGW("stream not started yet...skip drain");
+ return true;
+ }
+
if (isInThread() == false) {
+ AUDIO_IO_LOGD("drain");
pa_threaded_mainloop_lock(__mpMainloop);
- pa_operation_unref(pa_stream_drain(__mpStream, __successStreamCb, this));
+ pa_operation* o = pa_stream_drain(__mpStream, __successDrainCb, this);
+ __mIsDraining = true;
+ while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
+ pa_threaded_mainloop_wait(__mpMainloop);
+ }
+ pa_operation_unref(o);
pa_threaded_mainloop_unlock(__mpMainloop);
+ AUDIO_IO_LOGD("drain done");
} else {
- pa_operation_unref(pa_stream_drain(__mpStream, __successStreamCb, this));
+ AUDIO_IO_LOGD("drain in thread");
+ pa_operation_unref(pa_stream_drain(__mpStream, __successDrainCbInThread, this));
+ __mIsDraining = true;
}
return true;
}
-bool CPulseAudioClient::flush() throw(CAudioError) {
- AUDIO_IO_LOGD("flush");
-
+bool CPulseAudioClient::flush() {
if (__mIsInit == false) {
THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
}
checkRunningState();
if (isInThread() == false) {
+ AUDIO_IO_LOGD("flush");
pa_threaded_mainloop_lock(__mpMainloop);
pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
pa_threaded_mainloop_unlock(__mpMainloop);
} else {
+ AUDIO_IO_LOGD("flush in thread");
pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
}
return true;
}
-size_t CPulseAudioClient::getWritableSize() throw(CAudioError) {
+size_t CPulseAudioClient::getWritableSize() {
if (__mIsInit == false) {
THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
}
return ret;
}
-void CPulseAudioClient::checkRunningState() throw(CAudioError) {
- if (__mIsInit == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
- }
-
+void CPulseAudioClient::checkRunningState() {
if (__mpContext == NULL || PA_CONTEXT_IS_GOOD(pa_context_get_state(__mpContext)) == 0) {
THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] is not created or not good state", __mpContext);
}
if (__mpStream == NULL || PA_STREAM_IS_GOOD(pa_stream_get_state(__mpStream)) == 0) {
THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The stream[%p] is not created or not good state", __mpStream);
}
- if (pa_context_get_state(__mpContext) != PA_CONTEXT_READY || pa_stream_get_state(__mpStream) != PA_STREAM_READY) {
+ if (pa_context_get_state(__mpContext) != PA_CONTEXT_READY || pa_stream_get_state(__mpStream) != PA_STREAM_READY) {
THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] or stream[%p] state is not ready", __mpContext, __mpStream);
}
#endif
}
-bool CPulseAudioClient::isInThread() throw(CAudioError) {
- if (__mIsInit == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
- }
-
+bool CPulseAudioClient::isInThread() {
int ret = pa_threaded_mainloop_in_thread(__mpMainloop);
#ifdef _AUDIO_IO_DEBUG_TIMING_
return static_cast<bool>(ret);
}
-size_t CPulseAudioClient::getReadableSize() throw(CAudioError) {
+size_t CPulseAudioClient::getReadableSize() {
if (__mIsInit == false) {
THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
}
return ret;
}
-size_t CPulseAudioClient::getBufferSize() throw(CAudioError) {
+size_t CPulseAudioClient::getBufferSize() {
if (__mIsInit == false) {
THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
}
ret = attr->fragsize;
AUDIO_IO_LOGD("RECORD buffer size[%d]", ret);
}
- } catch (CAudioError err) {
+ } catch (CAudioError& e) {
if (isInThread() == false) {
pa_threaded_mainloop_unlock(__mpMainloop);
}
- throw err;
+ throw;
}
if (isInThread() == false) {
return ret;
}
-pa_usec_t CPulseAudioClient::getLatency() throw(CAudioError) {
+pa_usec_t CPulseAudioClient::getLatency() {
if (__mIsInit == false) {
THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
}
/* Wait until latency data is available again */
pa_threaded_mainloop_wait(__mpMainloop);
}
- } catch (CAudioError e) {
+ } catch (CAudioError& e) {
pa_threaded_mainloop_unlock(__mpMainloop);
- throw e;
+ throw;
}
pa_threaded_mainloop_unlock(__mpMainloop);
return negative ? 0 : ret;
}
-pa_usec_t CPulseAudioClient::getFinalLatency() throw(CAudioError) {
+pa_usec_t CPulseAudioClient::getFinalLatency() {
if (__mIsInit == false) {
THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
}
if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
ret = (pa_bytes_to_usec(buffer_attr->tlength, sample_spec) + timing_info->configured_sink_usec);
- AUDIO_IO_LOGD("FINAL PLAYBACK LATENCY[%d]", ret);
+ AUDIO_IO_LOGD("FINAL PLAYBACK LATENCY[%" PRIu64 "]", ret);
} else {
ret = (pa_bytes_to_usec(buffer_attr->fragsize, sample_spec) + timing_info->configured_source_usec);
- AUDIO_IO_LOGD("FINAL RECORD LATENCY[%d]", ret);
+ AUDIO_IO_LOGD("FINAL RECORD LATENCY[%" PRIu64 "]", ret);
}
} else {
THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED, "This version(ver.%d) is not supported", ver);
if (isInThread() == false) {
pa_threaded_mainloop_unlock(__mpMainloop);
}
- } catch (CAudioError e) {
+ } catch (CAudioError& e) {
if (isInThread() == false) {
pa_threaded_mainloop_unlock(__mpMainloop);
}
- throw e;
+ throw;
}
return ret;