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"
21 #include <dpm/context.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";
34 CPulseAudioClient::CPulseAudioClient(
35 EStreamDirection direction,
36 CPulseStreamSpec& spec,
37 IPulseStreamListener* listener) :
38 __mDirection(direction),
40 __mpListener(listener),
46 __mIsOperationSuccess(false),
47 __mpSyncReadDataPtr(NULL),
50 __mIsUsedSyncRead(false) {
53 CPulseAudioClient::~CPulseAudioClient() {
57 void CPulseAudioClient::__contextStateChangeCb(pa_context* c, void* user_data) {
58 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
62 switch (pa_context_get_state(c)) {
63 case PA_CONTEXT_READY:
64 AUDIO_IO_LOGD("The context is ready");
65 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
68 case PA_CONTEXT_FAILED:
69 case PA_CONTEXT_TERMINATED:
70 AUDIO_IO_LOGD("The context is lost");
71 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
74 case PA_CONTEXT_UNCONNECTED:
75 case PA_CONTEXT_CONNECTING:
76 case PA_CONTEXT_AUTHORIZING:
77 case PA_CONTEXT_SETTING_NAME:
82 void CPulseAudioClient::__successContextCb(pa_context* c, int success, void* user_data) {
83 AUDIO_IO_LOGD("pa_context[%p], success[%d], user_data[%p]", c, success, user_data);
87 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
88 pClient->__mIsOperationSuccess = static_cast<bool>(success);
90 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
93 static bool __is_microphone_restricted(void) {
96 dpm_context_h dpm_ctx_h;
97 dpm_restriction_policy_h dpm_policy_h;
100 if ((dpm_ctx_h = dpm_context_create())) {
101 if ((dpm_policy_h = dpm_context_acquire_restriction_policy(dpm_ctx_h))) {
102 /* state: 0(disallowed), 1(allowed) */
103 if ((ret = dpm_restriction_get_microphone_state(dpm_policy_h, &state)))
104 AUDIO_IO_LOGE("Failed to dpm_restriction_get_microphone_state(), ret(0x%x)", ret);
105 dpm_context_release_restriction_policy(dpm_ctx_h, dpm_policy_h);
107 AUDIO_IO_LOGE("Failed to dpm_context_acquire_restriction_policy()");
108 dpm_context_destroy(dpm_ctx_h);
110 AUDIO_IO_LOGE("Failed to dpm_context_create()");
112 return (state ? false : true);
115 void CPulseAudioClient::__streamStateChangeCb(pa_stream* s, void* user_data) {
119 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
121 switch (pa_stream_get_state(s)) {
122 case PA_STREAM_READY:
123 AUDIO_IO_LOGD("The stream is ready");
124 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
125 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
128 case PA_STREAM_FAILED:
129 AUDIO_IO_LOGD("The stream is failed");
130 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE,
131 __is_microphone_restricted());
132 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
135 case PA_STREAM_TERMINATED:
136 AUDIO_IO_LOGD("The stream is terminated");
137 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
138 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
141 case PA_STREAM_UNCONNECTED:
142 case PA_STREAM_CREATING:
147 void CPulseAudioClient::__streamCaptureCb(pa_stream* s, size_t length, void* user_data) {
151 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
152 assert(pClient->__mpListener);
153 assert(pClient->__mpMainloop);
155 if (pClient->__mIsUsedSyncRead == true) {
156 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
159 pClient->__mpListener->onStream(pClient, length);
162 void CPulseAudioClient::__streamPlaybackCb(pa_stream* s, size_t length, void* user_data) {
166 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
167 assert(pClient->__mpListener);
169 if (pClient->__mIsInit == false) {
170 AUDIO_IO_LOGD("Occurred this listener when an out stream is on the way to create : Write dummy, length[%d]", length);
172 char* dummy = new char[length];
173 memset(dummy, 0, length);
174 pa_stream_write(s, dummy, length, NULL, 0LL, PA_SEEK_RELATIVE);
180 pClient->__mpListener->onStream(pClient, length);
183 void CPulseAudioClient::__streamLatencyUpdateCb(pa_stream* s, void* user_data) {
187 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
189 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
192 void CPulseAudioClient::__successStreamCb(pa_stream* s, int success, void* user_data) {
193 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
197 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
198 pClient->__mIsOperationSuccess = static_cast<bool>(success);
200 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
203 void CPulseAudioClient::initialize() throw(CAudioError) {
205 if (__mIsInit == true) {
213 // Allocates PA proplist
214 __mpPropList = pa_proplist_new();
215 if (__mpPropList == NULL) {
216 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_proplist_new()");
219 // Adds values on proplist for delivery to PULSEAUDIO
220 char *streamType = NULL;
221 CAudioInfo::EAudioType audioType = __mSpec.getAudioInfo().getAudioType();
222 __mSpec.getAudioInfo().convertAudioType2StreamType(audioType, &streamType);
223 pa_proplist_sets(__mpPropList, PA_PROP_MEDIA_ROLE, streamType);
225 int index = __mSpec.getAudioInfo().getAudioIndex();
227 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_PARENT_ID, "%u", (unsigned int) index);
230 // Adds latency on proplist for delivery to PULSEAUDIO
231 AUDIO_IO_LOGD("LATENCY : %s[%d]", __mSpec.getStreamLatencyToString(), __mSpec.getStreamLatency());
232 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_TIZEN_AUDIO_LATENCY, "%s", __mSpec.getStreamLatencyToString());
234 // Allocates PA mainloop
235 __mpMainloop = pa_threaded_mainloop_new();
236 if (__mpMainloop == NULL) {
237 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()");
240 // Allocates PA context
241 __mpContext = pa_context_new(pa_threaded_mainloop_get_api(__mpMainloop), CLIENT_NAME);
242 if (__mpContext == NULL) {
243 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()");
246 // Sets context state changed callback
247 pa_context_set_state_callback(__mpContext, __contextStateChangeCb, this);
249 // Connects this client with PA server
250 if (pa_context_connect(__mpContext, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) {
251 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_connect()");
254 // LOCK for synchronous connection
255 pa_threaded_mainloop_lock(__mpMainloop);
258 if (pa_threaded_mainloop_start(__mpMainloop) < 0) {
259 pa_threaded_mainloop_unlock(__mpMainloop);
260 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_threaded_mainloop_start()");
263 // Connection process is asynchronously
264 // So, this function will be waited when occurred context state change event
265 // If I got a signal, do next processing
267 pa_context_state_t state;
268 state = pa_context_get_state(__mpContext);
270 if (state == PA_CONTEXT_READY) {
274 if (!PA_CONTEXT_IS_GOOD(state)) {
275 err = pa_context_errno(__mpContext);
276 pa_threaded_mainloop_unlock(__mpMainloop);
277 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_context's state is not good : err[%d]", err);
280 /* Wait until the context is ready */
281 pa_threaded_mainloop_wait(__mpMainloop);
284 // Allocates PA stream
285 pa_sample_spec ss = __mSpec.getSampleSpec();
286 pa_channel_map map = __mSpec.getChannelMap();
288 __mpStream = pa_stream_new_with_proplist(__mpContext, __mSpec.getStreamName(), &ss, &map, __mpPropList);
289 if (__mpStream == NULL) {
290 pa_threaded_mainloop_unlock(__mpMainloop);
291 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_new_with_proplist()");
294 // Sets stream callbacks
295 pa_stream_set_state_callback(__mpStream, __streamStateChangeCb, this);
296 pa_stream_set_read_callback(__mpStream, __streamCaptureCb, this);
297 pa_stream_set_write_callback(__mpStream, __streamPlaybackCb, this);
298 pa_stream_set_latency_update_callback(__mpStream, __streamLatencyUpdateCb, this);
300 // Connect stream with PA Server
302 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
303 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
304 PA_STREAM_INTERPOLATE_TIMING |
305 PA_STREAM_ADJUST_LATENCY |
306 PA_STREAM_AUTO_TIMING_UPDATE);
308 ret = pa_stream_connect_playback(__mpStream, NULL, NULL, flags, NULL, NULL);
310 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
311 PA_STREAM_INTERPOLATE_TIMING |
312 PA_STREAM_ADJUST_LATENCY |
313 PA_STREAM_AUTO_TIMING_UPDATE);
315 ret = pa_stream_connect_record(__mpStream, NULL, NULL, flags);
319 err = pa_context_errno(__mpContext);
320 pa_threaded_mainloop_unlock(__mpMainloop);
321 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_connect() : err[%d]", err);
325 pa_stream_state_t state;
326 state = pa_stream_get_state(__mpStream);
328 if (state == PA_STREAM_READY) {
329 AUDIO_IO_LOGD("STREAM READY");
333 if (!PA_STREAM_IS_GOOD(state)) {
334 err = pa_context_errno(__mpContext);
335 pa_threaded_mainloop_unlock(__mpMainloop);
336 if (err == PA_ERR_ACCESS) {
337 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_DEVICE_POLICY_RESTRICTION, "pa_stream's state is not good : err[%d]", err);
339 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_stream's state is not good : err[%d]", err);
343 /* Wait until the stream is ready */
344 pa_threaded_mainloop_wait(__mpMainloop);
347 // End of synchronous
348 pa_threaded_mainloop_unlock(__mpMainloop);
351 } catch (CAudioError e) {
357 void CPulseAudioClient::finalize() {
359 if (__mIsInit == false) {
363 if (__mpMainloop != NULL) {
364 pa_threaded_mainloop_stop(__mpMainloop);
367 if (__mpStream != NULL) {
368 pa_stream_disconnect(__mpStream);
369 pa_stream_unref(__mpStream);
373 if (__mpContext != NULL) {
374 pa_context_disconnect(__mpContext);
375 pa_context_unref(__mpContext);
379 if (__mpMainloop != NULL) {
380 pa_threaded_mainloop_free(__mpMainloop);
384 if (__mpPropList != NULL) {
385 pa_proplist_free(__mpPropList);
392 int CPulseAudioClient::read(void* buffer, size_t length) throw(CAudioError) {
393 if (__mIsInit == false) {
394 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
399 if (buffer == NULL) {
400 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p]", buffer);
403 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
404 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
407 size_t lengthIter = length;
410 __mIsUsedSyncRead = true;
413 pa_threaded_mainloop_lock(__mpMainloop);
415 while (lengthIter > 0) {
418 while (__mpSyncReadDataPtr == NULL) {
419 ret = pa_stream_peek(__mpStream, &__mpSyncReadDataPtr, &__mSyncReadLength);
421 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_peek() : ret[%d]", ret);
424 if (__mSyncReadLength <= 0) {
425 #ifdef _AUDIO_IO_DEBUG_TIMING_
426 AUDIO_IO_LOGD("readLength(%d byte) is not valid. wait...", __mSyncReadLength);
428 pa_threaded_mainloop_wait(__mpMainloop);
429 } else if (__mpSyncReadDataPtr == NULL) {
430 // Data peeked, but it doesn't have any data
431 ret = pa_stream_drop(__mpStream);
433 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
436 __mSyncReadIndex = 0;
440 if (__mSyncReadLength < lengthIter) {
441 l = __mSyncReadLength;
446 // Copy partial pcm data on out parameter
447 #ifdef _AUDIO_IO_DEBUG_TIMING_
448 AUDIO_IO_LOGD("memcpy() that a peeked buffer[%], index[%d], length[%d] on out buffer", (const uint8_t*)(__mpSyncReadDataPtr) + __mSyncReadIndex, __mSyncReadIndex, l);
450 memcpy(buffer, (const uint8_t*)__mpSyncReadDataPtr + __mSyncReadIndex, l);
452 // Move next position
453 buffer = (uint8_t*)buffer + l;
456 // Adjusts the rest length
457 __mSyncReadIndex += l;
458 __mSyncReadLength -= l;
460 if (__mSyncReadLength == 0) {
461 #ifdef _AUDIO_IO_DEBUG_TIMING_
462 AUDIO_IO_LOGD("__mSyncReadLength is zero, do drop()");
464 ret = pa_stream_drop(__mpStream);
466 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
469 // Reset the internal pointer
470 __mpSyncReadDataPtr = NULL;
471 __mSyncReadLength = 0;
472 __mSyncReadIndex = 0;
474 } // End of while (lengthIter > 0)
476 pa_threaded_mainloop_unlock(__mpMainloop);
477 __mIsUsedSyncRead = false;
478 } catch (CAudioError e) {
479 pa_threaded_mainloop_unlock(__mpMainloop);
480 __mIsUsedSyncRead = false;
487 int CPulseAudioClient::peek(const void** buffer, size_t* length) throw(CAudioError) {
488 if (__mIsInit == false) {
489 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
494 if (buffer == NULL || length == NULL) {
495 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p], length[%p]", buffer, length);
498 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
499 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
504 if (isInThread() == false) {
505 pa_threaded_mainloop_lock(__mpMainloop);
506 ret = pa_stream_peek(__mpStream, buffer, length);
507 pa_threaded_mainloop_unlock(__mpMainloop);
509 ret = pa_stream_peek(__mpStream, buffer, length);
512 #ifdef _AUDIO_IO_DEBUG_TIMING_
513 AUDIO_IO_LOGD("buffer[%p], length[%d]", *buffer, *length);
517 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_peek() : err[%d]", ret);
523 int CPulseAudioClient::drop() throw(CAudioError) {
524 if (__mIsInit == false) {
525 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
528 #ifdef _AUDIO_IO_DEBUG_TIMING_
534 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
535 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
540 if (isInThread() == false) {
541 pa_threaded_mainloop_lock(__mpMainloop);
542 ret = pa_stream_drop(__mpStream);
543 pa_threaded_mainloop_unlock(__mpMainloop);
545 ret = pa_stream_drop(__mpStream);
549 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_drop() : err[%d]", ret);
555 int CPulseAudioClient::write(const void* data, size_t length) throw(CAudioError) {
556 if (__mIsInit == false) {
557 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
563 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid");
566 if (__mDirection == EStreamDirection::STREAM_DIRECTION_RECORD) {
567 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
572 #ifdef _AUDIO_IO_DEBUG_TIMING_
573 AUDIO_IO_LOGD("data[%p], length[%d]", data, length);
576 if (isInThread() == false) {
577 pa_threaded_mainloop_lock(__mpMainloop);
578 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
579 pa_threaded_mainloop_unlock(__mpMainloop);
581 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
585 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_write() : err[%d]", ret);
591 void CPulseAudioClient::cork(bool cork) throw(CAudioError) {
592 AUDIO_IO_LOGD("cork[%d]", cork);
594 if (__mIsInit == false) {
595 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
598 if (isInThread() == true) {
599 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This operation is not supported in callback");
604 if (isInThread() == false) {
605 pa_threaded_mainloop_lock(__mpMainloop);
606 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
607 pa_threaded_mainloop_unlock(__mpMainloop);
609 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
615 bool CPulseAudioClient::isCorked() throw(CAudioError) {
616 if (__mIsInit == false) {
617 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
624 if (isInThread() == false) {
625 pa_threaded_mainloop_lock(__mpMainloop);
626 isCork = pa_stream_is_corked(__mpStream);
627 pa_threaded_mainloop_unlock(__mpMainloop);
629 isCork = pa_stream_is_corked(__mpStream);
632 AUDIO_IO_LOGD("isCork[%d]", isCork);
633 return static_cast<bool>(isCork);
636 bool CPulseAudioClient::drain() throw(CAudioError) {
637 AUDIO_IO_LOGD("drain");
639 if (__mIsInit == false) {
640 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
645 if (isInThread() == false) {
646 pa_threaded_mainloop_lock(__mpMainloop);
647 pa_operation_unref(pa_stream_drain(__mpStream, __successStreamCb, this));
648 pa_threaded_mainloop_unlock(__mpMainloop);
650 pa_operation_unref(pa_stream_drain(__mpStream, __successStreamCb, this));
656 bool CPulseAudioClient::flush() throw(CAudioError) {
657 AUDIO_IO_LOGD("flush");
659 if (__mIsInit == false) {
660 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
665 if (isInThread() == false) {
666 pa_threaded_mainloop_lock(__mpMainloop);
667 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
668 pa_threaded_mainloop_unlock(__mpMainloop);
670 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
676 size_t CPulseAudioClient::getWritableSize() throw(CAudioError) {
677 if (__mIsInit == false) {
678 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
683 if (__mDirection != EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
684 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Playback");
689 if (isInThread() == false) {
690 pa_threaded_mainloop_lock(__mpMainloop);
691 ret = pa_stream_writable_size(__mpStream);
692 pa_threaded_mainloop_unlock(__mpMainloop);
694 ret = pa_stream_writable_size(__mpStream);
700 void CPulseAudioClient::checkRunningState() throw(CAudioError) {
701 if (__mIsInit == false) {
702 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
705 if (__mpContext == NULL || PA_CONTEXT_IS_GOOD(pa_context_get_state(__mpContext)) == 0) {
706 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] is not created or not good state", __mpContext);
708 if (__mpStream == NULL || PA_STREAM_IS_GOOD(pa_stream_get_state(__mpStream)) == 0) {
709 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The stream[%p] is not created or not good state", __mpStream);
711 if (pa_context_get_state(__mpContext) != PA_CONTEXT_READY || pa_stream_get_state(__mpStream) != PA_STREAM_READY) {
712 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] or stream[%p] state is not ready", __mpContext, __mpStream);
715 #ifdef _AUDIO_IO_DEBUG_TIMING_
716 AUDIO_IO_LOGD("This client is running");
720 bool CPulseAudioClient::isInThread() throw(CAudioError) {
721 if (__mIsInit == false) {
722 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
725 int ret = pa_threaded_mainloop_in_thread(__mpMainloop);
727 #ifdef _AUDIO_IO_DEBUG_TIMING_
728 AUDIO_IO_LOGD("isInThread[%d]", ret);
730 return static_cast<bool>(ret);
733 size_t CPulseAudioClient::getReadableSize() throw(CAudioError) {
734 if (__mIsInit == false) {
735 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
740 if (__mDirection != EStreamDirection::STREAM_DIRECTION_RECORD) {
741 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Capture");
746 if (isInThread() == false) {
747 pa_threaded_mainloop_lock(__mpMainloop);
748 ret = pa_stream_writable_size(__mpStream);
749 pa_threaded_mainloop_unlock(__mpMainloop);
751 ret = pa_stream_writable_size(__mpStream);
757 size_t CPulseAudioClient::getBufferSize() throw(CAudioError) {
758 if (__mIsInit == false) {
759 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
767 if (isInThread() == false) {
768 pa_threaded_mainloop_lock(__mpMainloop);
771 const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
773 int _err = pa_context_errno(__mpContext);
774 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_buffer_attr() : err[%d]", _err);
777 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
779 AUDIO_IO_LOGD("PLAYBACK buffer size[%d]", ret);
781 ret = attr->fragsize;
782 AUDIO_IO_LOGD("RECORD buffer size[%d]", ret);
784 } catch (CAudioError err) {
785 if (isInThread() == false) {
786 pa_threaded_mainloop_unlock(__mpMainloop);
791 if (isInThread() == false) {
792 pa_threaded_mainloop_unlock(__mpMainloop);
798 pa_usec_t CPulseAudioClient::getLatency() throw(CAudioError) {
799 if (__mIsInit == false) {
800 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
808 if (isInThread() == false) {
809 if (pa_stream_get_latency(__mpStream, &ret, &negative) < 0) {
810 int _err = pa_context_errno(__mpContext);
811 if (_err != PA_ERR_NODATA) {
812 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
815 return negative ? 0 : ret;
818 pa_threaded_mainloop_lock(__mpMainloop);
822 if (pa_stream_get_latency(__mpStream, &ret, &negative) >= 0) {
826 int _err = pa_context_errno(__mpContext);
827 if (_err != PA_ERR_NODATA) {
828 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
831 /* Wait until latency data is available again */
832 pa_threaded_mainloop_wait(__mpMainloop);
834 } catch (CAudioError e) {
835 pa_threaded_mainloop_unlock(__mpMainloop);
839 pa_threaded_mainloop_unlock(__mpMainloop);
841 return negative ? 0 : ret;
844 pa_usec_t CPulseAudioClient::getFinalLatency() throw(CAudioError) {
845 if (__mIsInit == false) {
846 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
855 if (isInThread() == false) {
856 pa_threaded_mainloop_lock(__mpMainloop);
859 ver = pa_context_get_server_protocol_version(__mpContext);
861 const pa_buffer_attr* buffer_attr = pa_stream_get_buffer_attr(__mpStream);
862 const pa_sample_spec* sample_spec = pa_stream_get_sample_spec(__mpStream);
863 const pa_timing_info* timing_info = pa_stream_get_timing_info(__mpStream);
865 if (buffer_attr == NULL || sample_spec == NULL || timing_info == NULL) {
866 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",
867 buffer_attr, sample_spec, timing_info);
870 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
871 ret = (pa_bytes_to_usec(buffer_attr->tlength, sample_spec) + timing_info->configured_sink_usec);
872 AUDIO_IO_LOGD("FINAL PLAYBACK LATENCY[%d]", ret);
874 ret = (pa_bytes_to_usec(buffer_attr->fragsize, sample_spec) + timing_info->configured_source_usec);
875 AUDIO_IO_LOGD("FINAL RECORD LATENCY[%d]", ret);
878 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED, "This version(ver.%d) is not supported", ver);
881 if (isInThread() == false) {
882 pa_threaded_mainloop_unlock(__mpMainloop);
884 } catch (CAudioError e) {
885 if (isInThread() == false) {
886 pa_threaded_mainloop_unlock(__mpMainloop);
894 CPulseAudioClient::EStreamDirection CPulseAudioClient::getStreamDirection() {
898 CPulseStreamSpec CPulseAudioClient::getStreamSpec() {