2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "CAudioIODef.h"
22 #include <dpm/restriction.h>
26 using namespace tizen_media_audio;
30 * class CPulseAudioClient
32 const char* CPulseAudioClient::CLIENT_NAME = "AUDIO_IO_PA_CLIENT";
33 static const unsigned int drain_wait_interval = 10000;
34 static const unsigned int drain_wait_max_count = 30;
36 CPulseAudioClient::CPulseAudioClient(
37 EStreamDirection direction,
38 CPulseStreamSpec& spec,
39 IPulseStreamListener* listener) :
40 __mDirection(direction),
42 __mpListener(listener),
48 __mIsOperationSuccess(false),
49 __mpSyncReadDataPtr(NULL),
52 __mIsUsedSyncRead(false),
53 __mIsFirstStream(false),
54 __mIsDraining(false) {
57 CPulseAudioClient::~CPulseAudioClient() {
61 void CPulseAudioClient::__contextStateChangeCb(pa_context* c, void* user_data) {
62 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
66 switch (pa_context_get_state(c)) {
67 case PA_CONTEXT_READY:
68 AUDIO_IO_LOGD("The context is ready");
69 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
72 case PA_CONTEXT_FAILED:
73 case PA_CONTEXT_TERMINATED:
74 AUDIO_IO_LOGD("The context is lost");
75 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
78 case PA_CONTEXT_UNCONNECTED:
79 case PA_CONTEXT_CONNECTING:
80 case PA_CONTEXT_AUTHORIZING:
81 case PA_CONTEXT_SETTING_NAME:
86 void CPulseAudioClient::__successContextCb(pa_context* c, int success, void* user_data) {
87 AUDIO_IO_LOGD("pa_context[%p], success[%d], user_data[%p]", c, success, user_data);
91 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
92 pClient->__mIsOperationSuccess = static_cast<bool>(success);
94 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
97 static bool __is_microphone_restricted(void) {
100 device_policy_manager_h dpm_h = NULL;
103 if ((dpm_h = dpm_manager_create())) {
104 /* state: 0(disallowed), 1(allowed) */
105 if ((ret = dpm_restriction_get_microphone_state(dpm_h, &state)))
106 AUDIO_IO_LOGE("Failed to dpm_restriction_get_microphone_state(), ret(0x%x)", ret);
107 dpm_manager_destroy(dpm_h);
108 AUDIO_IO_LOGD("microphone restriction state: %d(1:allowed, 0:disallowed)", state);
110 AUDIO_IO_LOGE("Failed to dpm_manager_create()");
113 return (state ? false : true);
116 void CPulseAudioClient::__streamStateChangeCb(pa_stream* s, void* user_data) {
120 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
122 switch (pa_stream_get_state(s)) {
123 case PA_STREAM_READY:
124 AUDIO_IO_LOGD("The stream is ready");
125 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
126 if (pClient->__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK)
127 pClient->__mIsFirstStream = true;
128 pClient->__mIsInit = true;
129 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
132 case PA_STREAM_FAILED:
133 AUDIO_IO_LOGD("The stream is failed");
134 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE,
135 __is_microphone_restricted());
136 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
139 case PA_STREAM_TERMINATED:
140 AUDIO_IO_LOGD("The stream is terminated");
141 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
142 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
145 case PA_STREAM_UNCONNECTED:
146 case PA_STREAM_CREATING:
151 void CPulseAudioClient::__streamCaptureCb(pa_stream* s, size_t length, void* user_data) {
155 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
156 assert(pClient->__mpListener);
157 assert(pClient->__mpMainloop);
159 if (pClient->__mIsUsedSyncRead == true) {
160 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
163 pClient->__mpListener->onStream(pClient, length);
166 void CPulseAudioClient::__streamPlaybackCb(pa_stream* s, size_t length, void* user_data) {
170 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
171 assert(pClient->__mpListener);
173 pClient->__mpListener->onStream(pClient, length);
175 /* If stream is not written in first callback during prepare,
176 then write dummy data to ensure the start */
177 if (pClient->__mIsFirstStream) {
178 AUDIO_IO_LOGD("Write dummy, length [%d]", length);
180 char* dummy = new char[length];
181 memset(dummy, 0, length);
182 pa_stream_write(s, dummy, length, NULL, 0LL, PA_SEEK_RELATIVE);
185 pClient->__mIsFirstStream = false;
189 void CPulseAudioClient::__streamLatencyUpdateCb(pa_stream* s, void* user_data) {
193 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
195 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
198 void CPulseAudioClient::__streamEventCb(pa_stream* s, const char *name, pa_proplist *pl, void *user_data) {
202 AUDIO_IO_LOGE("NAME : %s, Prop : %p", name, pl);
204 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
205 if (strcmp(name, PA_STREAM_EVENT_POP_TIMEOUT) == 0) {
206 pa_operation_unref(pa_stream_cork(pClient->__mpStream, 1, NULL, NULL));
211 void CPulseAudioClient::__successStreamCb(pa_stream* s, int success, void* user_data) {
212 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
216 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
217 pClient->__mIsOperationSuccess = static_cast<bool>(success);
219 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
222 void CPulseAudioClient::__successDrainCbInThread(pa_stream* s, int success, void* user_data) {
223 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
227 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
228 pClient->__mIsOperationSuccess = static_cast<bool>(success);
229 pClient->__mIsDraining = false;
232 void CPulseAudioClient::__successDrainCb(pa_stream* s, int success, void* user_data) {
233 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
237 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
238 pClient->__mIsOperationSuccess = static_cast<bool>(success);
239 pClient->__mIsDraining = false;
241 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
244 void CPulseAudioClient::initialize() throw(CAudioError) {
245 if (__mIsInit == true) {
253 // Allocates PA proplist
254 __mpPropList = pa_proplist_new();
255 if (__mpPropList == NULL) {
256 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_proplist_new()");
259 // Adds values on proplist for delivery to PULSEAUDIO
260 char *streamType = NULL;
261 CAudioInfo::EAudioType audioType = __mSpec.getAudioInfo().getAudioType();
262 __mSpec.getAudioInfo().convertAudioType2StreamType(audioType, &streamType);
263 pa_proplist_sets(__mpPropList, PA_PROP_MEDIA_ROLE, streamType);
265 int index = __mSpec.getAudioInfo().getAudioIndex();
267 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_PARENT_ID, "%u", (unsigned int) index);
270 // Adds latency on proplist for delivery to PULSEAUDIO
271 AUDIO_IO_LOGD("LATENCY : %s[%d]", __mSpec.getStreamLatencyToString(), __mSpec.getStreamLatency());
272 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_TIZEN_AUDIO_LATENCY, "%s", __mSpec.getStreamLatencyToString());
274 // Allocates PA mainloop
275 __mpMainloop = pa_threaded_mainloop_new();
276 if (__mpMainloop == NULL) {
277 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()");
280 // Allocates PA context
281 __mpContext = pa_context_new(pa_threaded_mainloop_get_api(__mpMainloop), CLIENT_NAME);
282 if (__mpContext == NULL) {
283 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()");
286 // Sets context state changed callback
287 pa_context_set_state_callback(__mpContext, __contextStateChangeCb, this);
289 // Connects this client with PA server
290 if (pa_context_connect(__mpContext, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) {
291 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_connect()");
294 // LOCK for synchronous connection
295 pa_threaded_mainloop_lock(__mpMainloop);
298 if (pa_threaded_mainloop_start(__mpMainloop) < 0) {
299 pa_threaded_mainloop_unlock(__mpMainloop);
300 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_threaded_mainloop_start()");
303 // Connection process is asynchronously
304 // So, this function will be waited when occurred context state change event
305 // If I got a signal, do next processing
307 pa_context_state_t state;
308 state = pa_context_get_state(__mpContext);
310 if (state == PA_CONTEXT_READY) {
314 if (!PA_CONTEXT_IS_GOOD(state)) {
315 err = pa_context_errno(__mpContext);
316 pa_threaded_mainloop_unlock(__mpMainloop);
317 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_context's state is not good : err[%d]", err);
320 /* Wait until the context is ready */
321 pa_threaded_mainloop_wait(__mpMainloop);
324 // Allocates PA stream
325 pa_sample_spec ss = __mSpec.getSampleSpec();
326 pa_channel_map map = __mSpec.getChannelMap();
328 __mpStream = pa_stream_new_with_proplist(__mpContext, __mSpec.getStreamName(), &ss, &map, __mpPropList);
329 if (__mpStream == NULL) {
330 pa_threaded_mainloop_unlock(__mpMainloop);
331 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_new_with_proplist()");
334 // Sets stream callbacks
335 pa_stream_set_state_callback(__mpStream, __streamStateChangeCb, this);
336 pa_stream_set_read_callback(__mpStream, __streamCaptureCb, this);
337 pa_stream_set_write_callback(__mpStream, __streamPlaybackCb, this);
338 pa_stream_set_latency_update_callback(__mpStream, __streamLatencyUpdateCb, this);
339 pa_stream_set_event_callback(__mpStream, __streamEventCb, this);
341 // Connect stream with PA Server
343 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
344 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
345 PA_STREAM_INTERPOLATE_TIMING |
346 PA_STREAM_ADJUST_LATENCY |
347 PA_STREAM_AUTO_TIMING_UPDATE);
349 ret = pa_stream_connect_playback(__mpStream, NULL, NULL, flags, NULL, NULL);
351 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
352 PA_STREAM_INTERPOLATE_TIMING |
353 PA_STREAM_ADJUST_LATENCY |
354 PA_STREAM_AUTO_TIMING_UPDATE);
356 ret = pa_stream_connect_record(__mpStream, NULL, NULL, flags);
360 err = pa_context_errno(__mpContext);
361 pa_threaded_mainloop_unlock(__mpMainloop);
362 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_connect() : err[%d]", err);
366 pa_stream_state_t state;
367 state = pa_stream_get_state(__mpStream);
369 if (state == PA_STREAM_READY) {
370 AUDIO_IO_LOGD("STREAM READY");
374 if (!PA_STREAM_IS_GOOD(state)) {
375 err = pa_context_errno(__mpContext);
376 pa_threaded_mainloop_unlock(__mpMainloop);
377 if (err == PA_ERR_ACCESS) {
378 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_DEVICE_POLICY_RESTRICTION, "pa_stream's state is not good : err[%d]", err);
380 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_stream's state is not good : err[%d]", err);
384 /* Wait until the stream is ready */
385 pa_threaded_mainloop_wait(__mpMainloop);
388 // End of synchronous
389 pa_threaded_mainloop_unlock(__mpMainloop);
391 // __mIsInit = true; // Moved to __streamStateChangeCb()
392 } catch (CAudioError e) {
398 void CPulseAudioClient::finalize() {
400 if (__mIsInit == false) {
404 /* clear callbacks */
405 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK)
406 pa_stream_set_write_callback(__mpStream, NULL, NULL);
408 pa_stream_set_read_callback(__mpStream, NULL, NULL);
409 pa_stream_set_latency_update_callback(__mpStream, NULL, NULL);
410 pa_stream_set_event_callback(__mpStream, NULL, NULL);
412 /* Wait for drain complete if draining before finalize */
414 unsigned int drain_wait_count = 0;
415 while (__mIsDraining && drain_wait_count++ < drain_wait_max_count) {
416 usleep(drain_wait_interval);
418 AUDIO_IO_LOGD("wait for drain complete!!!! [%d * %d usec]",
419 drain_wait_count, drain_wait_interval);
422 if (__mpMainloop != NULL) {
423 pa_threaded_mainloop_stop(__mpMainloop);
426 if (__mpStream != NULL) {
427 pa_stream_disconnect(__mpStream);
428 pa_stream_unref(__mpStream);
432 if (__mpContext != NULL) {
433 pa_context_disconnect(__mpContext);
434 pa_context_unref(__mpContext);
438 if (__mpMainloop != NULL) {
439 pa_threaded_mainloop_free(__mpMainloop);
443 if (__mpPropList != NULL) {
444 pa_proplist_free(__mpPropList);
451 int CPulseAudioClient::read(void* buffer, size_t length) throw(CAudioError) {
452 if (__mIsInit == false) {
453 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
458 if (buffer == NULL) {
459 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p]", buffer);
462 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
463 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
466 size_t lengthIter = length;
469 __mIsUsedSyncRead = true;
472 pa_threaded_mainloop_lock(__mpMainloop);
474 while (lengthIter > 0) {
477 while (__mpSyncReadDataPtr == NULL) {
478 ret = pa_stream_peek(__mpStream, &__mpSyncReadDataPtr, &__mSyncReadLength);
480 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_peek() : ret[%d]", ret);
483 if (__mSyncReadLength <= 0) {
484 #ifdef _AUDIO_IO_DEBUG_TIMING_
485 AUDIO_IO_LOGD("readLength(%d byte) is not valid. wait...", __mSyncReadLength);
487 pa_threaded_mainloop_wait(__mpMainloop);
488 } else if (__mpSyncReadDataPtr == NULL) {
489 // Data peeked, but it doesn't have any data
490 ret = pa_stream_drop(__mpStream);
492 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
495 __mSyncReadIndex = 0;
499 if (__mSyncReadLength < lengthIter) {
500 l = __mSyncReadLength;
505 // Copy partial pcm data on out parameter
506 #ifdef _AUDIO_IO_DEBUG_TIMING_
507 AUDIO_IO_LOGD("memcpy() that a peeked buffer[0x%x], index[%d], length[%d] on out buffer", (const uint8_t*)(__mpSyncReadDataPtr) + __mSyncReadIndex, __mSyncReadIndex, l);
509 memcpy(buffer, (const uint8_t*)__mpSyncReadDataPtr + __mSyncReadIndex, l);
511 // Move next position
512 buffer = (uint8_t*)buffer + l;
515 // Adjusts the rest length
516 __mSyncReadIndex += l;
517 __mSyncReadLength -= l;
519 if (__mSyncReadLength == 0) {
520 #ifdef _AUDIO_IO_DEBUG_TIMING_
521 AUDIO_IO_LOGD("__mSyncReadLength is zero, do drop()");
523 ret = pa_stream_drop(__mpStream);
525 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
528 // Reset the internal pointer
529 __mpSyncReadDataPtr = NULL;
530 __mSyncReadLength = 0;
531 __mSyncReadIndex = 0;
533 } // End of while (lengthIter > 0)
535 pa_threaded_mainloop_unlock(__mpMainloop);
536 __mIsUsedSyncRead = false;
537 } catch (CAudioError e) {
538 pa_threaded_mainloop_unlock(__mpMainloop);
539 __mIsUsedSyncRead = false;
546 int CPulseAudioClient::peek(const void** buffer, size_t* length) throw(CAudioError) {
547 if (__mIsInit == false) {
548 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
553 if (buffer == NULL || length == NULL) {
554 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p], length[%p]", buffer, length);
557 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
558 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
563 if (isInThread() == false) {
564 pa_threaded_mainloop_lock(__mpMainloop);
565 ret = pa_stream_peek(__mpStream, buffer, length);
566 pa_threaded_mainloop_unlock(__mpMainloop);
568 ret = pa_stream_peek(__mpStream, buffer, length);
571 #ifdef _AUDIO_IO_DEBUG_TIMING_
572 AUDIO_IO_LOGD("buffer[%p], length[%d]", *buffer, *length);
576 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_peek() : err[%d]", ret);
582 int CPulseAudioClient::drop() throw(CAudioError) {
583 if (__mIsInit == false) {
584 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
587 #ifdef _AUDIO_IO_DEBUG_TIMING_
593 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
594 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
599 if (isInThread() == false) {
600 pa_threaded_mainloop_lock(__mpMainloop);
601 ret = pa_stream_drop(__mpStream);
602 pa_threaded_mainloop_unlock(__mpMainloop);
604 ret = pa_stream_drop(__mpStream);
608 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_drop() : err[%d]", ret);
614 int CPulseAudioClient::write(const void* data, size_t length) throw(CAudioError) {
616 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid");
619 if (__mDirection == EStreamDirection::STREAM_DIRECTION_RECORD) {
620 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
625 #ifdef _AUDIO_IO_DEBUG_TIMING_
626 AUDIO_IO_LOGD("data[%p], length[%d]", data, length);
628 if (pa_stream_is_corked(__mpStream)) {
629 AUDIO_IO_LOGW("stream is corked...do uncork here first!!!!");
630 pa_operation_unref(pa_stream_cork(__mpStream, 0, NULL, this));
633 if (isInThread() == false) {
634 pa_threaded_mainloop_lock(__mpMainloop);
635 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
636 pa_threaded_mainloop_unlock(__mpMainloop);
638 if (__mIsFirstStream) {
639 const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
640 uint32_t prebuf = attr->prebuf;
641 // Compensate amount of prebuf in first stream callback when audio-io use prebuf(-1)
642 // Because a stream will never start when an application wrote less than prebuf at first
643 if (length < prebuf) {
644 char* dummy = new char[prebuf - length];
645 memset(dummy, 0, prebuf - length);
646 pa_stream_write(__mpStream, dummy, prebuf - length, NULL, 0LL, PA_SEEK_RELATIVE);
649 __mIsFirstStream = false;
650 AUDIO_IO_LOGW("FIRST STREAM CALLBACK : length[%d], prebuf[%d], dummy[%d]",
651 length, prebuf, (length < prebuf) ? prebuf - length : 0);
653 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
657 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_write() : err[%d]", ret);
663 void CPulseAudioClient::cork(bool cork) throw(CAudioError) {
664 AUDIO_IO_LOGD("cork[%d]", cork);
666 if (__mIsInit == false) {
667 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
670 if (isInThread() == true) {
671 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This operation is not supported in callback");
676 // Set __mIsFirstStream flag when uncork(resume) stream, because prebuf will be enable again
678 __mIsFirstStream = true;
680 if (isInThread() == false) {
681 pa_threaded_mainloop_lock(__mpMainloop);
682 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
683 pa_threaded_mainloop_unlock(__mpMainloop);
685 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
691 bool CPulseAudioClient::isCorked() throw(CAudioError) {
692 if (__mIsInit == false) {
693 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
700 if (isInThread() == false) {
701 pa_threaded_mainloop_lock(__mpMainloop);
702 isCork = pa_stream_is_corked(__mpStream);
703 pa_threaded_mainloop_unlock(__mpMainloop);
705 isCork = pa_stream_is_corked(__mpStream);
708 AUDIO_IO_LOGD("isCork[%d]", isCork);
709 return static_cast<bool>(isCork);
712 bool CPulseAudioClient::drain() throw(CAudioError) {
713 if (__mIsInit == false) {
714 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
720 AUDIO_IO_LOGW("already draining...");
722 if (isInThread() == false) {
723 AUDIO_IO_LOGD("drain");
724 pa_threaded_mainloop_lock(__mpMainloop);
725 pa_operation* o = pa_stream_drain(__mpStream, __successDrainCb, this);
726 __mIsDraining = true;
727 while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
728 pa_threaded_mainloop_wait(__mpMainloop);
730 pa_operation_unref(o);
731 pa_threaded_mainloop_unlock(__mpMainloop);
733 AUDIO_IO_LOGD("drain in thread");
734 pa_operation_unref(pa_stream_drain(__mpStream, __successDrainCbInThread, this));
735 __mIsDraining = true;
741 bool CPulseAudioClient::flush() throw(CAudioError) {
742 if (__mIsInit == false) {
743 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
748 if (isInThread() == false) {
749 AUDIO_IO_LOGD("flush");
750 pa_threaded_mainloop_lock(__mpMainloop);
751 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
752 pa_threaded_mainloop_unlock(__mpMainloop);
754 AUDIO_IO_LOGD("flush in thread");
755 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
761 size_t CPulseAudioClient::getWritableSize() throw(CAudioError) {
762 if (__mIsInit == false) {
763 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
768 if (__mDirection != EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
769 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Playback");
774 if (isInThread() == false) {
775 pa_threaded_mainloop_lock(__mpMainloop);
776 ret = pa_stream_writable_size(__mpStream);
777 pa_threaded_mainloop_unlock(__mpMainloop);
779 ret = pa_stream_writable_size(__mpStream);
785 void CPulseAudioClient::checkRunningState() throw(CAudioError) {
786 if (__mpContext == NULL || PA_CONTEXT_IS_GOOD(pa_context_get_state(__mpContext)) == 0) {
787 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] is not created or not good state", __mpContext);
789 if (__mpStream == NULL || PA_STREAM_IS_GOOD(pa_stream_get_state(__mpStream)) == 0) {
790 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The stream[%p] is not created or not good state", __mpStream);
792 if (pa_context_get_state(__mpContext) != PA_CONTEXT_READY || pa_stream_get_state(__mpStream) != PA_STREAM_READY) {
793 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] or stream[%p] state is not ready", __mpContext, __mpStream);
796 #ifdef _AUDIO_IO_DEBUG_TIMING_
797 AUDIO_IO_LOGD("This client is running");
801 bool CPulseAudioClient::isInThread() throw(CAudioError) {
802 int ret = pa_threaded_mainloop_in_thread(__mpMainloop);
804 #ifdef _AUDIO_IO_DEBUG_TIMING_
805 AUDIO_IO_LOGD("isInThread[%d]", ret);
807 return static_cast<bool>(ret);
810 size_t CPulseAudioClient::getReadableSize() throw(CAudioError) {
811 if (__mIsInit == false) {
812 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
817 if (__mDirection != EStreamDirection::STREAM_DIRECTION_RECORD) {
818 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Capture");
823 if (isInThread() == false) {
824 pa_threaded_mainloop_lock(__mpMainloop);
825 ret = pa_stream_writable_size(__mpStream);
826 pa_threaded_mainloop_unlock(__mpMainloop);
828 ret = pa_stream_writable_size(__mpStream);
834 size_t CPulseAudioClient::getBufferSize() throw(CAudioError) {
835 if (__mIsInit == false) {
836 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
844 if (isInThread() == false) {
845 pa_threaded_mainloop_lock(__mpMainloop);
848 const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
850 int _err = pa_context_errno(__mpContext);
851 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_buffer_attr() : err[%d]", _err);
854 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
856 AUDIO_IO_LOGD("PLAYBACK buffer size[%d]", ret);
858 ret = attr->fragsize;
859 AUDIO_IO_LOGD("RECORD buffer size[%d]", ret);
861 } catch (CAudioError err) {
862 if (isInThread() == false) {
863 pa_threaded_mainloop_unlock(__mpMainloop);
868 if (isInThread() == false) {
869 pa_threaded_mainloop_unlock(__mpMainloop);
875 pa_usec_t CPulseAudioClient::getLatency() throw(CAudioError) {
876 if (__mIsInit == false) {
877 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
885 if (isInThread() == false) {
886 if (pa_stream_get_latency(__mpStream, &ret, &negative) < 0) {
887 int _err = pa_context_errno(__mpContext);
888 if (_err != PA_ERR_NODATA) {
889 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
892 return negative ? 0 : ret;
895 pa_threaded_mainloop_lock(__mpMainloop);
899 if (pa_stream_get_latency(__mpStream, &ret, &negative) >= 0) {
903 int _err = pa_context_errno(__mpContext);
904 if (_err != PA_ERR_NODATA) {
905 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
908 /* Wait until latency data is available again */
909 pa_threaded_mainloop_wait(__mpMainloop);
911 } catch (CAudioError e) {
912 pa_threaded_mainloop_unlock(__mpMainloop);
916 pa_threaded_mainloop_unlock(__mpMainloop);
918 return negative ? 0 : ret;
921 pa_usec_t CPulseAudioClient::getFinalLatency() throw(CAudioError) {
922 if (__mIsInit == false) {
923 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
932 if (isInThread() == false) {
933 pa_threaded_mainloop_lock(__mpMainloop);
936 ver = pa_context_get_server_protocol_version(__mpContext);
938 const pa_buffer_attr* buffer_attr = pa_stream_get_buffer_attr(__mpStream);
939 const pa_sample_spec* sample_spec = pa_stream_get_sample_spec(__mpStream);
940 const pa_timing_info* timing_info = pa_stream_get_timing_info(__mpStream);
942 if (buffer_attr == NULL || sample_spec == NULL || timing_info == NULL) {
943 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to get buffer_attr[%p] or sample_spec[%p] or timing_info[%p] from a pa_stream",
944 buffer_attr, sample_spec, timing_info);
947 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
948 ret = (pa_bytes_to_usec(buffer_attr->tlength, sample_spec) + timing_info->configured_sink_usec);
949 AUDIO_IO_LOGD("FINAL PLAYBACK LATENCY[%d]", ret);
951 ret = (pa_bytes_to_usec(buffer_attr->fragsize, sample_spec) + timing_info->configured_source_usec);
952 AUDIO_IO_LOGD("FINAL RECORD LATENCY[%d]", ret);
955 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED, "This version(ver.%d) is not supported", ver);
958 if (isInThread() == false) {
959 pa_threaded_mainloop_unlock(__mpMainloop);
961 } catch (CAudioError e) {
962 if (isInThread() == false) {
963 pa_threaded_mainloop_unlock(__mpMainloop);
971 CPulseAudioClient::EStreamDirection CPulseAudioClient::getStreamDirection() {
975 CPulseStreamSpec CPulseAudioClient::getStreamSpec() {