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"
23 using namespace tizen_media_audio;
27 * class CPulseAudioClient
29 const char* CPulseAudioClient::CLIENT_NAME = "AUDIO_IO_PA_CLIENT";
31 CPulseAudioClient::CPulseAudioClient(
32 EStreamDirection direction,
33 CPulseStreamSpec& spec,
34 IPulseStreamListener* listener) :
35 __mDirection(direction),
37 __mpListener(listener),
43 __mIsOperationSuccess(false),
44 __mpSyncReadDataPtr(NULL),
47 __mIsUsedSyncRead(false) {
50 CPulseAudioClient::~CPulseAudioClient() {
54 void CPulseAudioClient::__contextStateChangeCb(pa_context* c, void* user_data) {
55 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
59 switch (pa_context_get_state(c)) {
60 case PA_CONTEXT_READY:
61 AUDIO_IO_LOGD("The context is ready");
62 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
65 case PA_CONTEXT_FAILED:
66 case PA_CONTEXT_TERMINATED:
67 AUDIO_IO_LOGD("The context is lost");
68 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
71 case PA_CONTEXT_UNCONNECTED:
72 case PA_CONTEXT_CONNECTING:
73 case PA_CONTEXT_AUTHORIZING:
74 case PA_CONTEXT_SETTING_NAME:
79 void CPulseAudioClient::__successContextCb(pa_context* c, int success, void* user_data) {
80 AUDIO_IO_LOGD("pa_context[%p], success[%d], user_data[%p]", c, success, user_data);
84 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
85 pClient->__mIsOperationSuccess = static_cast<bool>(success);
87 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
90 void CPulseAudioClient::__streamStateChangeCb(pa_stream* s, void* user_data) {
94 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
96 switch (pa_stream_get_state(s)) {
98 AUDIO_IO_LOGD("The stream is ready");
99 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
100 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
103 case PA_STREAM_FAILED:
104 AUDIO_IO_LOGD("The stream is failed");
105 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
106 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
109 case PA_STREAM_TERMINATED:
110 AUDIO_IO_LOGD("The stream is terminated");
111 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
112 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
115 case PA_STREAM_UNCONNECTED:
116 case PA_STREAM_CREATING:
121 void CPulseAudioClient::__streamCaptureCb(pa_stream* s, size_t length, void* user_data) {
125 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
126 assert(pClient->__mpListener);
127 assert(pClient->__mpMainloop);
129 if (pClient->__mIsUsedSyncRead == true) {
130 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
133 pClient->__mpListener->onStream(pClient, length);
136 void CPulseAudioClient::__streamPlaybackCb(pa_stream* s, size_t length, void* user_data) {
140 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
141 assert(pClient->__mpListener);
143 if (pClient->__mIsInit == false) {
144 AUDIO_IO_LOGD("Occurred this listener when an out stream is on the way to create : Write dummy, length[%d]", length);
146 char* dummy = new char[length];
147 memset(dummy, 0, length);
148 pa_stream_write(s, dummy, length, NULL, 0LL, PA_SEEK_RELATIVE);
154 pClient->__mpListener->onStream(pClient, length);
157 void CPulseAudioClient::__streamLatencyUpdateCb(pa_stream* s, void* user_data) {
161 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
163 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
166 void CPulseAudioClient::__successStreamCb(pa_stream* s, int success, void* user_data) {
167 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
171 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
172 pClient->__mIsOperationSuccess = static_cast<bool>(success);
174 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
177 void CPulseAudioClient::initialize() throw(CAudioError) {
179 if (__mIsInit == true) {
187 // Allocates PA proplist
188 __mpPropList = pa_proplist_new();
189 if (__mpPropList == NULL) {
190 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_proplist_new()");
193 // Adds values on proplist for delivery to PULSEAUDIO
194 char *streamType = NULL;
195 CAudioInfo::EAudioType audioType = __mSpec.getAudioInfo().getAudioType();
196 __mSpec.getAudioInfo().convertAudioType2StreamType(audioType, &streamType);
197 pa_proplist_sets(__mpPropList, PA_PROP_MEDIA_ROLE, streamType);
199 int index = __mSpec.getAudioInfo().getAudioIndex();
201 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_PARENT_ID, "%u", (unsigned int) index);
204 // Adds latency on proplist for delivery to PULSEAUDIO
205 AUDIO_IO_LOGD("LATENCY : %s[%d]", __mSpec.getStreamLatencyToString(), __mSpec.getStreamLatency());
206 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_TIZEN_AUDIO_LATENCY, "%s", __mSpec.getStreamLatencyToString());
208 // Allocates PA mainloop
209 __mpMainloop = pa_threaded_mainloop_new();
210 if (__mpMainloop == NULL) {
211 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()");
214 // Allocates PA context
215 __mpContext = pa_context_new(pa_threaded_mainloop_get_api(__mpMainloop), CLIENT_NAME);
216 if (__mpContext == NULL) {
217 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()");
220 // Sets context state changed callback
221 pa_context_set_state_callback(__mpContext, __contextStateChangeCb, this);
223 // Connects this client with PA server
224 if (pa_context_connect(__mpContext, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) {
225 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_connect()");
228 // LOCK for synchronous connection
229 pa_threaded_mainloop_lock(__mpMainloop);
232 if (pa_threaded_mainloop_start(__mpMainloop) < 0) {
233 pa_threaded_mainloop_unlock(__mpMainloop);
234 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_threaded_mainloop_start()");
237 // Connection process is asynchronously
238 // So, this function will be waited when occurred context state change event
239 // If I got a signal, do next processing
241 pa_context_state_t state;
242 state = pa_context_get_state(__mpContext);
244 if (state == PA_CONTEXT_READY) {
248 if (!PA_CONTEXT_IS_GOOD(state)) {
249 err = pa_context_errno(__mpContext);
250 pa_threaded_mainloop_unlock(__mpMainloop);
251 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_context's state is not good : err[%d]", err);
254 /* Wait until the context is ready */
255 pa_threaded_mainloop_wait(__mpMainloop);
258 // Allocates PA stream
259 pa_sample_spec ss = __mSpec.getSampleSpec();
260 pa_channel_map map = __mSpec.getChannelMap();
262 __mpStream = pa_stream_new_with_proplist(__mpContext, __mSpec.getStreamName(), &ss, &map, __mpPropList);
263 if (__mpStream == NULL) {
264 pa_threaded_mainloop_unlock(__mpMainloop);
265 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_new_with_proplist()");
268 // Sets stream callbacks
269 pa_stream_set_state_callback(__mpStream, __streamStateChangeCb, this);
270 pa_stream_set_read_callback(__mpStream, __streamCaptureCb, this);
271 pa_stream_set_write_callback(__mpStream, __streamPlaybackCb, this);
272 pa_stream_set_latency_update_callback(__mpStream, __streamLatencyUpdateCb, this);
274 // Connect stream with PA Server
276 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
277 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
278 PA_STREAM_INTERPOLATE_TIMING |
279 PA_STREAM_ADJUST_LATENCY |
280 PA_STREAM_AUTO_TIMING_UPDATE);
282 ret = pa_stream_connect_playback(__mpStream, NULL, NULL, flags, NULL, NULL);
284 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
285 PA_STREAM_INTERPOLATE_TIMING |
286 PA_STREAM_ADJUST_LATENCY |
287 PA_STREAM_AUTO_TIMING_UPDATE);
289 ret = pa_stream_connect_record(__mpStream, NULL, NULL, flags);
293 err = pa_context_errno(__mpContext);
294 pa_threaded_mainloop_unlock(__mpMainloop);
295 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_connect() : err[%d]", err);
299 pa_stream_state_t state;
300 state = pa_stream_get_state(__mpStream);
302 if (state == PA_STREAM_READY) {
303 AUDIO_IO_LOGD("STREAM READY");
307 if (!PA_STREAM_IS_GOOD(state)) {
308 err = pa_context_errno(__mpContext);
309 pa_threaded_mainloop_unlock(__mpMainloop);
310 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_stream's state is not good : err[%d]", err);
313 /* Wait until the stream is ready */
314 pa_threaded_mainloop_wait(__mpMainloop);
317 // End of synchronous
318 pa_threaded_mainloop_unlock(__mpMainloop);
321 } catch (CAudioError e) {
327 void CPulseAudioClient::finalize() {
329 if (__mIsInit == false) {
333 if (__mpMainloop != NULL) {
334 pa_threaded_mainloop_stop(__mpMainloop);
337 if (__mpStream != NULL) {
338 pa_stream_disconnect(__mpStream);
339 pa_stream_unref(__mpStream);
343 if (__mpContext != NULL) {
344 pa_context_disconnect(__mpContext);
345 pa_context_unref(__mpContext);
349 if (__mpMainloop != NULL) {
350 pa_threaded_mainloop_free(__mpMainloop);
354 if (__mpPropList != NULL) {
355 pa_proplist_free(__mpPropList);
362 int CPulseAudioClient::read(void* buffer, size_t length) throw(CAudioError) {
363 if (__mIsInit == false) {
364 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
369 if (buffer == NULL) {
370 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p]", buffer);
373 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
374 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
377 size_t lengthIter = length;
380 __mIsUsedSyncRead = true;
383 pa_threaded_mainloop_lock(__mpMainloop);
385 while (lengthIter > 0) {
388 while (__mpSyncReadDataPtr == NULL) {
389 ret = pa_stream_peek(__mpStream, &__mpSyncReadDataPtr, &__mSyncReadLength);
391 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_peek() : ret[%d]", ret);
394 if (__mSyncReadLength <= 0) {
395 #ifdef _AUDIO_IO_DEBUG_TIMING_
396 AUDIO_IO_LOGD("readLength(%d byte) is not valid. wait...", __mSyncReadLength);
398 pa_threaded_mainloop_wait(__mpMainloop);
399 } else if (__mpSyncReadDataPtr == NULL) {
400 // Data peeked, but it doesn't have any data
401 ret = pa_stream_drop(__mpStream);
403 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
406 __mSyncReadIndex = 0;
410 if (__mSyncReadLength < lengthIter) {
411 l = __mSyncReadLength;
416 // Copy partial pcm data on out parameter
417 #ifdef _AUDIO_IO_DEBUG_TIMING_
418 AUDIO_IO_LOGD("memcpy() that a peeked buffer[%], index[%d], length[%d] on out buffer", (const uint8_t*)(__mpSyncReadDataPtr) + __mSyncReadIndex, __mSyncReadIndex, l);
420 memcpy(buffer, (const uint8_t*)__mpSyncReadDataPtr + __mSyncReadIndex, l);
422 // Move next position
423 buffer = (uint8_t*)buffer + l;
426 // Adjusts the rest length
427 __mSyncReadIndex += l;
428 __mSyncReadLength -= l;
430 if (__mSyncReadLength == 0) {
431 #ifdef _AUDIO_IO_DEBUG_TIMING_
432 AUDIO_IO_LOGD("__mSyncReadLength is zero, do drop()");
434 ret = pa_stream_drop(__mpStream);
436 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
439 // Reset the internal pointer
440 __mpSyncReadDataPtr = NULL;
441 __mSyncReadLength = 0;
442 __mSyncReadIndex = 0;
444 } // End of while (lengthIter > 0)
446 pa_threaded_mainloop_unlock(__mpMainloop);
447 __mIsUsedSyncRead = false;
448 } catch (CAudioError e) {
449 pa_threaded_mainloop_unlock(__mpMainloop);
450 __mIsUsedSyncRead = false;
457 int CPulseAudioClient::peek(const void** buffer, size_t* length) throw(CAudioError) {
458 if (__mIsInit == false) {
459 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
464 if (buffer == NULL || length == NULL) {
465 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p], length[%p]", buffer, length);
468 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
469 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
474 if (isInThread() == false) {
475 pa_threaded_mainloop_lock(__mpMainloop);
476 ret = pa_stream_peek(__mpStream, buffer, length);
477 pa_threaded_mainloop_unlock(__mpMainloop);
479 ret = pa_stream_peek(__mpStream, buffer, length);
482 #ifdef _AUDIO_IO_DEBUG_TIMING_
483 AUDIO_IO_LOGD("buffer[%p], length[%d]", *buffer, *length);
487 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_peek() : err[%d]", ret);
493 int CPulseAudioClient::drop() throw(CAudioError) {
494 if (__mIsInit == false) {
495 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
498 #ifdef _AUDIO_IO_DEBUG_TIMING_
504 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
505 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
510 if (isInThread() == false) {
511 pa_threaded_mainloop_lock(__mpMainloop);
512 ret = pa_stream_drop(__mpStream);
513 pa_threaded_mainloop_unlock(__mpMainloop);
515 ret = pa_stream_drop(__mpStream);
519 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_drop() : err[%d]", ret);
525 int CPulseAudioClient::write(const void* data, size_t length) throw(CAudioError) {
526 if (__mIsInit == false) {
527 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
533 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid");
536 if (__mDirection == EStreamDirection::STREAM_DIRECTION_RECORD) {
537 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
542 #ifdef _AUDIO_IO_DEBUG_TIMING_
543 AUDIO_IO_LOGD("data[%p], length[%d]", data, length);
546 if (isInThread() == false) {
547 pa_threaded_mainloop_lock(__mpMainloop);
548 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
549 pa_threaded_mainloop_unlock(__mpMainloop);
551 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
555 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_write() : err[%d]", ret);
561 void CPulseAudioClient::cork(bool cork) throw(CAudioError) {
562 AUDIO_IO_LOGD("cork[%d]", cork);
564 if (__mIsInit == false) {
565 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
568 if (isInThread() == true) {
569 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This operation is not supported in callback");
574 if (isInThread() == false) {
575 pa_threaded_mainloop_lock(__mpMainloop);
576 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
577 pa_threaded_mainloop_unlock(__mpMainloop);
579 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
585 bool CPulseAudioClient::isCorked() throw(CAudioError) {
586 if (__mIsInit == false) {
587 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
594 if (isInThread() == false) {
595 pa_threaded_mainloop_lock(__mpMainloop);
596 isCork = pa_stream_is_corked(__mpStream);
597 pa_threaded_mainloop_unlock(__mpMainloop);
599 isCork = pa_stream_is_corked(__mpStream);
602 AUDIO_IO_LOGD("isCork[%d]", isCork);
603 return static_cast<bool>(isCork);
606 bool CPulseAudioClient::drain() throw(CAudioError) {
607 AUDIO_IO_LOGD("drain");
609 if (__mIsInit == false) {
610 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
615 if (isInThread() == false) {
616 pa_threaded_mainloop_lock(__mpMainloop);
617 pa_operation_unref(pa_stream_drain(__mpStream, __successStreamCb, this));
618 pa_threaded_mainloop_unlock(__mpMainloop);
620 pa_operation_unref(pa_stream_drain(__mpStream, __successStreamCb, this));
626 bool CPulseAudioClient::flush() throw(CAudioError) {
627 AUDIO_IO_LOGD("flush");
629 if (__mIsInit == false) {
630 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
635 if (isInThread() == false) {
636 pa_threaded_mainloop_lock(__mpMainloop);
637 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
638 pa_threaded_mainloop_unlock(__mpMainloop);
640 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
646 size_t CPulseAudioClient::getWritableSize() throw(CAudioError) {
647 if (__mIsInit == false) {
648 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
653 if (__mDirection != EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
654 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Playback");
659 if (isInThread() == false) {
660 pa_threaded_mainloop_lock(__mpMainloop);
661 ret = pa_stream_writable_size(__mpStream);
662 pa_threaded_mainloop_unlock(__mpMainloop);
664 ret = pa_stream_writable_size(__mpStream);
670 void CPulseAudioClient::checkRunningState() throw(CAudioError) {
671 if (__mIsInit == false) {
672 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
675 if (__mpContext == NULL || PA_CONTEXT_IS_GOOD(pa_context_get_state(__mpContext)) == 0) {
676 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] is not created or not good state", __mpContext);
678 if (__mpStream == NULL || PA_STREAM_IS_GOOD(pa_stream_get_state(__mpStream)) == 0) {
679 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The stream[%p] is not created or not good state", __mpStream);
681 if (pa_context_get_state(__mpContext) != PA_CONTEXT_READY || pa_stream_get_state(__mpStream) != PA_STREAM_READY) {
682 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] or stream[%p] state is not ready", __mpContext, __mpStream);
685 #ifdef _AUDIO_IO_DEBUG_TIMING_
686 AUDIO_IO_LOGD("This client is running");
690 bool CPulseAudioClient::isInThread() throw(CAudioError) {
691 if (__mIsInit == false) {
692 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
695 int ret = pa_threaded_mainloop_in_thread(__mpMainloop);
697 #ifdef _AUDIO_IO_DEBUG_TIMING_
698 AUDIO_IO_LOGD("isInThread[%d]", ret);
700 return static_cast<bool>(ret);
703 size_t CPulseAudioClient::getReadableSize() throw(CAudioError) {
704 if (__mIsInit == false) {
705 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
710 if (__mDirection != EStreamDirection::STREAM_DIRECTION_RECORD) {
711 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Capture");
716 if (isInThread() == false) {
717 pa_threaded_mainloop_lock(__mpMainloop);
718 ret = pa_stream_writable_size(__mpStream);
719 pa_threaded_mainloop_unlock(__mpMainloop);
721 ret = pa_stream_writable_size(__mpStream);
727 size_t CPulseAudioClient::getBufferSize() throw(CAudioError) {
728 if (__mIsInit == false) {
729 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
737 if (isInThread() == false) {
738 pa_threaded_mainloop_lock(__mpMainloop);
741 const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
743 int _err = pa_context_errno(__mpContext);
744 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_buffer_attr() : err[%d]", _err);
747 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
749 AUDIO_IO_LOGD("PLAYBACK buffer size[%d]", ret);
751 ret = attr->fragsize;
752 AUDIO_IO_LOGD("RECORD buffer size[%d]", ret);
754 } catch (CAudioError err) {
755 if (isInThread() == false) {
756 pa_threaded_mainloop_unlock(__mpMainloop);
761 if (isInThread() == false) {
762 pa_threaded_mainloop_unlock(__mpMainloop);
768 pa_usec_t CPulseAudioClient::getLatency() throw(CAudioError) {
769 if (__mIsInit == false) {
770 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
778 if (isInThread() == false) {
779 if (pa_stream_get_latency(__mpStream, &ret, &negative) < 0) {
780 int _err = pa_context_errno(__mpContext);
781 if (_err != PA_ERR_NODATA) {
782 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
785 return negative ? 0 : ret;
788 pa_threaded_mainloop_lock(__mpMainloop);
792 if (pa_stream_get_latency(__mpStream, &ret, &negative) >= 0) {
796 int _err = pa_context_errno(__mpContext);
797 if (_err != PA_ERR_NODATA) {
798 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
801 /* Wait until latency data is available again */
802 pa_threaded_mainloop_wait(__mpMainloop);
804 } catch (CAudioError e) {
805 pa_threaded_mainloop_unlock(__mpMainloop);
809 pa_threaded_mainloop_unlock(__mpMainloop);
811 return negative ? 0 : ret;
814 pa_usec_t CPulseAudioClient::getFinalLatency() throw(CAudioError) {
815 if (__mIsInit == false) {
816 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
825 if (isInThread() == false) {
826 pa_threaded_mainloop_lock(__mpMainloop);
829 ver = pa_context_get_server_protocol_version(__mpContext);
831 const pa_buffer_attr* buffer_attr = pa_stream_get_buffer_attr(__mpStream);
832 const pa_sample_spec* sample_spec = pa_stream_get_sample_spec(__mpStream);
833 const pa_timing_info* timing_info = pa_stream_get_timing_info(__mpStream);
835 if (buffer_attr == NULL || sample_spec == NULL || timing_info == NULL) {
836 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",
837 buffer_attr, sample_spec, timing_info);
840 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
841 ret = (pa_bytes_to_usec(buffer_attr->tlength, sample_spec) + timing_info->configured_sink_usec);
842 AUDIO_IO_LOGD("FINAL PLAYBACK LATENCY[%d]", ret);
844 ret = (pa_bytes_to_usec(buffer_attr->fragsize, sample_spec) + timing_info->configured_source_usec);
845 AUDIO_IO_LOGD("FINAL RECORD LATENCY[%d]", ret);
848 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED, "This version(ver.%d) is not supported", ver);
851 if (isInThread() == false) {
852 pa_threaded_mainloop_unlock(__mpMainloop);
854 } catch (CAudioError e) {
855 if (isInThread() == false) {
856 pa_threaded_mainloop_unlock(__mpMainloop);
864 CPulseAudioClient::EStreamDirection CPulseAudioClient::getStreamDirection() {
868 CPulseStreamSpec CPulseAudioClient::getStreamSpec() {