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/restriction.h>
25 using namespace tizen_media_audio;
29 * class CPulseAudioClient
31 const char* CPulseAudioClient::CLIENT_NAME = "AUDIO_IO_PA_CLIENT";
33 CPulseAudioClient::CPulseAudioClient(
34 EStreamDirection direction,
35 CPulseStreamSpec& spec,
36 IPulseStreamListener* listener) :
37 __mDirection(direction),
39 __mpListener(listener),
45 __mIsOperationSuccess(false),
46 __mpSyncReadDataPtr(NULL),
49 __mIsUsedSyncRead(false) {
52 CPulseAudioClient::~CPulseAudioClient() {
56 void CPulseAudioClient::__contextStateChangeCb(pa_context* c, void* user_data) {
57 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
61 switch (pa_context_get_state(c)) {
62 case PA_CONTEXT_READY:
63 AUDIO_IO_LOGD("The context is ready");
64 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
67 case PA_CONTEXT_FAILED:
68 case PA_CONTEXT_TERMINATED:
69 AUDIO_IO_LOGD("The context is lost");
70 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
73 case PA_CONTEXT_UNCONNECTED:
74 case PA_CONTEXT_CONNECTING:
75 case PA_CONTEXT_AUTHORIZING:
76 case PA_CONTEXT_SETTING_NAME:
81 void CPulseAudioClient::__successContextCb(pa_context* c, int success, void* user_data) {
82 AUDIO_IO_LOGD("pa_context[%p], success[%d], user_data[%p]", c, success, user_data);
86 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
87 pClient->__mIsOperationSuccess = static_cast<bool>(success);
89 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
92 static bool __is_microphone_restricted(void) {
95 device_policy_manager_h dpm_h = NULL;
98 if ((dpm_h = dpm_manager_create())) {
99 /* state: 0(disallowed), 1(allowed) */
100 if ((ret = dpm_restriction_get_microphone_state(dpm_h, &state)))
101 AUDIO_IO_LOGE("Failed to dpm_restriction_get_microphone_state(), ret(0x%x)", ret);
102 dpm_manager_destroy(dpm_h);
103 AUDIO_IO_LOGD("microphone restriction state: %d(1:allowed, 0:disallowed)", state);
105 AUDIO_IO_LOGE("Failed to dpm_manager_create()");
108 return (state ? false : true);
111 void CPulseAudioClient::__streamStateChangeCb(pa_stream* s, void* user_data) {
115 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
117 switch (pa_stream_get_state(s)) {
118 case PA_STREAM_READY:
119 AUDIO_IO_LOGD("The stream is ready");
120 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
121 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
124 case PA_STREAM_FAILED:
125 AUDIO_IO_LOGD("The stream is failed");
126 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE,
127 __is_microphone_restricted());
128 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
131 case PA_STREAM_TERMINATED:
132 AUDIO_IO_LOGD("The stream is terminated");
133 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
134 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
137 case PA_STREAM_UNCONNECTED:
138 case PA_STREAM_CREATING:
143 void CPulseAudioClient::__streamCaptureCb(pa_stream* s, size_t length, void* user_data) {
147 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
148 assert(pClient->__mpListener);
149 assert(pClient->__mpMainloop);
151 if (pClient->__mIsUsedSyncRead == true) {
152 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
155 pClient->__mpListener->onStream(pClient, length);
158 void CPulseAudioClient::__streamPlaybackCb(pa_stream* s, size_t length, void* user_data) {
162 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
163 assert(pClient->__mpListener);
165 if (pClient->__mIsInit == false) {
166 AUDIO_IO_LOGD("Occurred this listener when an out stream is on the way to create : Write dummy, length[%d]", length);
168 char* dummy = new char[length];
169 memset(dummy, 0, length);
170 pa_stream_write(s, dummy, length, NULL, 0LL, PA_SEEK_RELATIVE);
176 pClient->__mpListener->onStream(pClient, length);
179 void CPulseAudioClient::__streamLatencyUpdateCb(pa_stream* s, void* user_data) {
183 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
185 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
188 void CPulseAudioClient::__streamEventCb(pa_stream* s, const char *name, pa_proplist *pl, void *user_data) {
192 AUDIO_IO_LOGE("NAME : %s, Prop : %p", name, pl);
194 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
195 if (strcmp(name, PA_STREAM_EVENT_POP_TIMEOUT) == 0) {
196 pa_operation_unref(pa_stream_cork(pClient->__mpStream, 1, NULL, NULL));
201 void CPulseAudioClient::__successStreamCb(pa_stream* s, int success, void* user_data) {
202 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
206 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
207 pClient->__mIsOperationSuccess = static_cast<bool>(success);
209 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
212 void CPulseAudioClient::initialize() throw(CAudioError) {
214 if (__mIsInit == true) {
222 // Allocates PA proplist
223 __mpPropList = pa_proplist_new();
224 if (__mpPropList == NULL) {
225 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_proplist_new()");
228 // Adds values on proplist for delivery to PULSEAUDIO
229 char *streamType = NULL;
230 CAudioInfo::EAudioType audioType = __mSpec.getAudioInfo().getAudioType();
231 __mSpec.getAudioInfo().convertAudioType2StreamType(audioType, &streamType);
232 pa_proplist_sets(__mpPropList, PA_PROP_MEDIA_ROLE, streamType);
234 int index = __mSpec.getAudioInfo().getAudioIndex();
236 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_PARENT_ID, "%u", (unsigned int) index);
239 // Adds latency on proplist for delivery to PULSEAUDIO
240 AUDIO_IO_LOGD("LATENCY : %s[%d]", __mSpec.getStreamLatencyToString(), __mSpec.getStreamLatency());
241 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_TIZEN_AUDIO_LATENCY, "%s", __mSpec.getStreamLatencyToString());
243 // Allocates PA mainloop
244 __mpMainloop = pa_threaded_mainloop_new();
245 if (__mpMainloop == NULL) {
246 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()");
249 // Allocates PA context
250 __mpContext = pa_context_new(pa_threaded_mainloop_get_api(__mpMainloop), CLIENT_NAME);
251 if (__mpContext == NULL) {
252 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()");
255 // Sets context state changed callback
256 pa_context_set_state_callback(__mpContext, __contextStateChangeCb, this);
258 // Connects this client with PA server
259 if (pa_context_connect(__mpContext, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) {
260 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_connect()");
263 // LOCK for synchronous connection
264 pa_threaded_mainloop_lock(__mpMainloop);
267 if (pa_threaded_mainloop_start(__mpMainloop) < 0) {
268 pa_threaded_mainloop_unlock(__mpMainloop);
269 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_threaded_mainloop_start()");
272 // Connection process is asynchronously
273 // So, this function will be waited when occurred context state change event
274 // If I got a signal, do next processing
276 pa_context_state_t state;
277 state = pa_context_get_state(__mpContext);
279 if (state == PA_CONTEXT_READY) {
283 if (!PA_CONTEXT_IS_GOOD(state)) {
284 err = pa_context_errno(__mpContext);
285 pa_threaded_mainloop_unlock(__mpMainloop);
286 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_context's state is not good : err[%d]", err);
289 /* Wait until the context is ready */
290 pa_threaded_mainloop_wait(__mpMainloop);
293 // Allocates PA stream
294 pa_sample_spec ss = __mSpec.getSampleSpec();
295 pa_channel_map map = __mSpec.getChannelMap();
297 __mpStream = pa_stream_new_with_proplist(__mpContext, __mSpec.getStreamName(), &ss, &map, __mpPropList);
298 if (__mpStream == NULL) {
299 pa_threaded_mainloop_unlock(__mpMainloop);
300 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_new_with_proplist()");
303 // Sets stream callbacks
304 pa_stream_set_state_callback(__mpStream, __streamStateChangeCb, this);
305 pa_stream_set_read_callback(__mpStream, __streamCaptureCb, this);
306 pa_stream_set_write_callback(__mpStream, __streamPlaybackCb, this);
307 pa_stream_set_latency_update_callback(__mpStream, __streamLatencyUpdateCb, this);
308 pa_stream_set_event_callback(__mpStream, __streamEventCb, this);
310 // Connect stream with PA Server
312 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
313 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
314 PA_STREAM_INTERPOLATE_TIMING |
315 PA_STREAM_ADJUST_LATENCY |
316 PA_STREAM_AUTO_TIMING_UPDATE);
318 ret = pa_stream_connect_playback(__mpStream, NULL, NULL, flags, NULL, NULL);
320 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
321 PA_STREAM_INTERPOLATE_TIMING |
322 PA_STREAM_ADJUST_LATENCY |
323 PA_STREAM_AUTO_TIMING_UPDATE);
325 ret = pa_stream_connect_record(__mpStream, NULL, NULL, flags);
329 err = pa_context_errno(__mpContext);
330 pa_threaded_mainloop_unlock(__mpMainloop);
331 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_connect() : err[%d]", err);
335 pa_stream_state_t state;
336 state = pa_stream_get_state(__mpStream);
338 if (state == PA_STREAM_READY) {
339 AUDIO_IO_LOGD("STREAM READY");
343 if (!PA_STREAM_IS_GOOD(state)) {
344 err = pa_context_errno(__mpContext);
345 pa_threaded_mainloop_unlock(__mpMainloop);
346 if (err == PA_ERR_ACCESS) {
347 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_DEVICE_POLICY_RESTRICTION, "pa_stream's state is not good : err[%d]", err);
349 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_stream's state is not good : err[%d]", err);
353 /* Wait until the stream is ready */
354 pa_threaded_mainloop_wait(__mpMainloop);
357 // End of synchronous
358 pa_threaded_mainloop_unlock(__mpMainloop);
361 } catch (CAudioError e) {
367 void CPulseAudioClient::finalize() {
369 if (__mIsInit == false) {
373 if (__mpMainloop != NULL) {
374 pa_threaded_mainloop_stop(__mpMainloop);
377 if (__mpStream != NULL) {
378 pa_stream_disconnect(__mpStream);
379 pa_stream_unref(__mpStream);
383 if (__mpContext != NULL) {
384 pa_context_disconnect(__mpContext);
385 pa_context_unref(__mpContext);
389 if (__mpMainloop != NULL) {
390 pa_threaded_mainloop_free(__mpMainloop);
394 if (__mpPropList != NULL) {
395 pa_proplist_free(__mpPropList);
402 int CPulseAudioClient::read(void* buffer, size_t length) throw(CAudioError) {
403 if (__mIsInit == false) {
404 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
409 if (buffer == NULL) {
410 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p]", buffer);
413 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
414 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
417 size_t lengthIter = length;
420 __mIsUsedSyncRead = true;
423 pa_threaded_mainloop_lock(__mpMainloop);
425 while (lengthIter > 0) {
428 while (__mpSyncReadDataPtr == NULL) {
429 ret = pa_stream_peek(__mpStream, &__mpSyncReadDataPtr, &__mSyncReadLength);
431 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_peek() : ret[%d]", ret);
434 if (__mSyncReadLength <= 0) {
435 #ifdef _AUDIO_IO_DEBUG_TIMING_
436 AUDIO_IO_LOGD("readLength(%d byte) is not valid. wait...", __mSyncReadLength);
438 pa_threaded_mainloop_wait(__mpMainloop);
439 } else if (__mpSyncReadDataPtr == NULL) {
440 // Data peeked, but it doesn't have any data
441 ret = pa_stream_drop(__mpStream);
443 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
446 __mSyncReadIndex = 0;
450 if (__mSyncReadLength < lengthIter) {
451 l = __mSyncReadLength;
456 // Copy partial pcm data on out parameter
457 #ifdef _AUDIO_IO_DEBUG_TIMING_
458 AUDIO_IO_LOGD("memcpy() that a peeked buffer[%], index[%d], length[%d] on out buffer", (const uint8_t*)(__mpSyncReadDataPtr) + __mSyncReadIndex, __mSyncReadIndex, l);
460 memcpy(buffer, (const uint8_t*)__mpSyncReadDataPtr + __mSyncReadIndex, l);
462 // Move next position
463 buffer = (uint8_t*)buffer + l;
466 // Adjusts the rest length
467 __mSyncReadIndex += l;
468 __mSyncReadLength -= l;
470 if (__mSyncReadLength == 0) {
471 #ifdef _AUDIO_IO_DEBUG_TIMING_
472 AUDIO_IO_LOGD("__mSyncReadLength is zero, do drop()");
474 ret = pa_stream_drop(__mpStream);
476 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
479 // Reset the internal pointer
480 __mpSyncReadDataPtr = NULL;
481 __mSyncReadLength = 0;
482 __mSyncReadIndex = 0;
484 } // End of while (lengthIter > 0)
486 pa_threaded_mainloop_unlock(__mpMainloop);
487 __mIsUsedSyncRead = false;
488 } catch (CAudioError e) {
489 pa_threaded_mainloop_unlock(__mpMainloop);
490 __mIsUsedSyncRead = false;
497 int CPulseAudioClient::peek(const void** buffer, size_t* length) throw(CAudioError) {
498 if (__mIsInit == false) {
499 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
504 if (buffer == NULL || length == NULL) {
505 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p], length[%p]", buffer, length);
508 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
509 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
514 if (isInThread() == false) {
515 pa_threaded_mainloop_lock(__mpMainloop);
516 ret = pa_stream_peek(__mpStream, buffer, length);
517 pa_threaded_mainloop_unlock(__mpMainloop);
519 ret = pa_stream_peek(__mpStream, buffer, length);
522 #ifdef _AUDIO_IO_DEBUG_TIMING_
523 AUDIO_IO_LOGD("buffer[%p], length[%d]", *buffer, *length);
527 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_peek() : err[%d]", ret);
533 int CPulseAudioClient::drop() throw(CAudioError) {
534 if (__mIsInit == false) {
535 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
538 #ifdef _AUDIO_IO_DEBUG_TIMING_
544 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
545 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
550 if (isInThread() == false) {
551 pa_threaded_mainloop_lock(__mpMainloop);
552 ret = pa_stream_drop(__mpStream);
553 pa_threaded_mainloop_unlock(__mpMainloop);
555 ret = pa_stream_drop(__mpStream);
559 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_drop() : err[%d]", ret);
565 int CPulseAudioClient::write(const void* data, size_t length) throw(CAudioError) {
566 if (__mIsInit == false) {
567 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
573 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid");
576 if (__mDirection == EStreamDirection::STREAM_DIRECTION_RECORD) {
577 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
582 #ifdef _AUDIO_IO_DEBUG_TIMING_
583 AUDIO_IO_LOGD("data[%p], length[%d]", data, length);
585 if (pa_stream_is_corked(__mpStream)) {
586 AUDIO_IO_LOGW("stream is corked...do uncork here first!!!!");
587 pa_operation_unref(pa_stream_cork(__mpStream, 0, NULL, this));
590 if (isInThread() == false) {
591 pa_threaded_mainloop_lock(__mpMainloop);
592 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
593 pa_threaded_mainloop_unlock(__mpMainloop);
595 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
599 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_write() : err[%d]", ret);
605 void CPulseAudioClient::cork(bool cork) throw(CAudioError) {
606 AUDIO_IO_LOGD("cork[%d]", cork);
608 if (__mIsInit == false) {
609 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
612 if (isInThread() == true) {
613 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This operation is not supported in callback");
618 if (isInThread() == false) {
619 pa_threaded_mainloop_lock(__mpMainloop);
620 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
621 pa_threaded_mainloop_unlock(__mpMainloop);
623 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
629 bool CPulseAudioClient::isCorked() throw(CAudioError) {
630 if (__mIsInit == false) {
631 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
638 if (isInThread() == false) {
639 pa_threaded_mainloop_lock(__mpMainloop);
640 isCork = pa_stream_is_corked(__mpStream);
641 pa_threaded_mainloop_unlock(__mpMainloop);
643 isCork = pa_stream_is_corked(__mpStream);
646 AUDIO_IO_LOGD("isCork[%d]", isCork);
647 return static_cast<bool>(isCork);
650 bool CPulseAudioClient::drain() throw(CAudioError) {
651 AUDIO_IO_LOGD("drain");
653 if (__mIsInit == false) {
654 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
659 if (isInThread() == false) {
660 pa_threaded_mainloop_lock(__mpMainloop);
661 pa_operation_unref(pa_stream_drain(__mpStream, __successStreamCb, this));
662 pa_threaded_mainloop_unlock(__mpMainloop);
664 pa_operation_unref(pa_stream_drain(__mpStream, __successStreamCb, this));
670 bool CPulseAudioClient::flush() throw(CAudioError) {
671 AUDIO_IO_LOGD("flush");
673 if (__mIsInit == false) {
674 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
679 if (isInThread() == false) {
680 pa_threaded_mainloop_lock(__mpMainloop);
681 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
682 pa_threaded_mainloop_unlock(__mpMainloop);
684 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
690 size_t CPulseAudioClient::getWritableSize() throw(CAudioError) {
691 if (__mIsInit == false) {
692 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
697 if (__mDirection != EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
698 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Playback");
703 if (isInThread() == false) {
704 pa_threaded_mainloop_lock(__mpMainloop);
705 ret = pa_stream_writable_size(__mpStream);
706 pa_threaded_mainloop_unlock(__mpMainloop);
708 ret = pa_stream_writable_size(__mpStream);
714 void CPulseAudioClient::checkRunningState() throw(CAudioError) {
715 if (__mIsInit == false) {
716 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
719 if (__mpContext == NULL || PA_CONTEXT_IS_GOOD(pa_context_get_state(__mpContext)) == 0) {
720 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] is not created or not good state", __mpContext);
722 if (__mpStream == NULL || PA_STREAM_IS_GOOD(pa_stream_get_state(__mpStream)) == 0) {
723 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The stream[%p] is not created or not good state", __mpStream);
725 if (pa_context_get_state(__mpContext) != PA_CONTEXT_READY || pa_stream_get_state(__mpStream) != PA_STREAM_READY) {
726 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] or stream[%p] state is not ready", __mpContext, __mpStream);
729 #ifdef _AUDIO_IO_DEBUG_TIMING_
730 AUDIO_IO_LOGD("This client is running");
734 bool CPulseAudioClient::isInThread() throw(CAudioError) {
735 if (__mIsInit == false) {
736 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
739 int ret = pa_threaded_mainloop_in_thread(__mpMainloop);
741 #ifdef _AUDIO_IO_DEBUG_TIMING_
742 AUDIO_IO_LOGD("isInThread[%d]", ret);
744 return static_cast<bool>(ret);
747 size_t CPulseAudioClient::getReadableSize() throw(CAudioError) {
748 if (__mIsInit == false) {
749 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
754 if (__mDirection != EStreamDirection::STREAM_DIRECTION_RECORD) {
755 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Capture");
760 if (isInThread() == false) {
761 pa_threaded_mainloop_lock(__mpMainloop);
762 ret = pa_stream_writable_size(__mpStream);
763 pa_threaded_mainloop_unlock(__mpMainloop);
765 ret = pa_stream_writable_size(__mpStream);
771 size_t CPulseAudioClient::getBufferSize() throw(CAudioError) {
772 if (__mIsInit == false) {
773 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
781 if (isInThread() == false) {
782 pa_threaded_mainloop_lock(__mpMainloop);
785 const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
787 int _err = pa_context_errno(__mpContext);
788 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_buffer_attr() : err[%d]", _err);
791 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
793 AUDIO_IO_LOGD("PLAYBACK buffer size[%d]", ret);
795 ret = attr->fragsize;
796 AUDIO_IO_LOGD("RECORD buffer size[%d]", ret);
798 } catch (CAudioError err) {
799 if (isInThread() == false) {
800 pa_threaded_mainloop_unlock(__mpMainloop);
805 if (isInThread() == false) {
806 pa_threaded_mainloop_unlock(__mpMainloop);
812 pa_usec_t CPulseAudioClient::getLatency() throw(CAudioError) {
813 if (__mIsInit == false) {
814 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
822 if (isInThread() == false) {
823 if (pa_stream_get_latency(__mpStream, &ret, &negative) < 0) {
824 int _err = pa_context_errno(__mpContext);
825 if (_err != PA_ERR_NODATA) {
826 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
829 return negative ? 0 : ret;
832 pa_threaded_mainloop_lock(__mpMainloop);
836 if (pa_stream_get_latency(__mpStream, &ret, &negative) >= 0) {
840 int _err = pa_context_errno(__mpContext);
841 if (_err != PA_ERR_NODATA) {
842 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
845 /* Wait until latency data is available again */
846 pa_threaded_mainloop_wait(__mpMainloop);
848 } catch (CAudioError e) {
849 pa_threaded_mainloop_unlock(__mpMainloop);
853 pa_threaded_mainloop_unlock(__mpMainloop);
855 return negative ? 0 : ret;
858 pa_usec_t CPulseAudioClient::getFinalLatency() throw(CAudioError) {
859 if (__mIsInit == false) {
860 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
869 if (isInThread() == false) {
870 pa_threaded_mainloop_lock(__mpMainloop);
873 ver = pa_context_get_server_protocol_version(__mpContext);
875 const pa_buffer_attr* buffer_attr = pa_stream_get_buffer_attr(__mpStream);
876 const pa_sample_spec* sample_spec = pa_stream_get_sample_spec(__mpStream);
877 const pa_timing_info* timing_info = pa_stream_get_timing_info(__mpStream);
879 if (buffer_attr == NULL || sample_spec == NULL || timing_info == NULL) {
880 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",
881 buffer_attr, sample_spec, timing_info);
884 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
885 ret = (pa_bytes_to_usec(buffer_attr->tlength, sample_spec) + timing_info->configured_sink_usec);
886 AUDIO_IO_LOGD("FINAL PLAYBACK LATENCY[%d]", ret);
888 ret = (pa_bytes_to_usec(buffer_attr->fragsize, sample_spec) + timing_info->configured_source_usec);
889 AUDIO_IO_LOGD("FINAL RECORD LATENCY[%d]", ret);
892 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED, "This version(ver.%d) is not supported", ver);
895 if (isInThread() == false) {
896 pa_threaded_mainloop_unlock(__mpMainloop);
898 } catch (CAudioError e) {
899 if (isInThread() == false) {
900 pa_threaded_mainloop_unlock(__mpMainloop);
908 CPulseAudioClient::EStreamDirection CPulseAudioClient::getStreamDirection() {
912 CPulseStreamSpec CPulseAudioClient::getStreamSpec() {