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),
50 __mIsFirstStream(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 device_policy_manager_h dpm_h = NULL;
99 if ((dpm_h = dpm_manager_create())) {
100 /* state: 0(disallowed), 1(allowed) */
101 if ((ret = dpm_restriction_get_microphone_state(dpm_h, &state)))
102 AUDIO_IO_LOGE("Failed to dpm_restriction_get_microphone_state(), ret(0x%x)", ret);
103 dpm_manager_destroy(dpm_h);
104 AUDIO_IO_LOGD("microphone restriction state: %d(1:allowed, 0:disallowed)", state);
106 AUDIO_IO_LOGE("Failed to dpm_manager_create()");
109 return (state ? false : true);
112 void CPulseAudioClient::__streamStateChangeCb(pa_stream* s, void* user_data) {
116 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
118 switch (pa_stream_get_state(s)) {
119 case PA_STREAM_READY:
120 AUDIO_IO_LOGD("The stream is ready");
121 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
122 if (pClient->__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK)
123 pClient->__mIsFirstStream = true;
124 pClient->__mIsInit = true;
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 pClient->__mpListener->onStream(pClient, length);
172 void CPulseAudioClient::__streamLatencyUpdateCb(pa_stream* s, void* user_data) {
176 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
178 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
181 void CPulseAudioClient::__streamEventCb(pa_stream* s, const char *name, pa_proplist *pl, void *user_data) {
185 AUDIO_IO_LOGE("NAME : %s, Prop : %p", name, pl);
187 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
188 if (strcmp(name, PA_STREAM_EVENT_POP_TIMEOUT) == 0) {
189 pa_operation_unref(pa_stream_cork(pClient->__mpStream, 1, NULL, NULL));
194 void CPulseAudioClient::__successStreamCb(pa_stream* s, int success, void* user_data) {
195 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
199 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
200 pClient->__mIsOperationSuccess = static_cast<bool>(success);
202 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
205 void CPulseAudioClient::initialize() throw(CAudioError) {
207 if (__mIsInit == true) {
215 // Allocates PA proplist
216 __mpPropList = pa_proplist_new();
217 if (__mpPropList == NULL) {
218 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_proplist_new()");
221 // Adds values on proplist for delivery to PULSEAUDIO
222 char *streamType = NULL;
223 CAudioInfo::EAudioType audioType = __mSpec.getAudioInfo().getAudioType();
224 __mSpec.getAudioInfo().convertAudioType2StreamType(audioType, &streamType);
225 pa_proplist_sets(__mpPropList, PA_PROP_MEDIA_ROLE, streamType);
227 int index = __mSpec.getAudioInfo().getAudioIndex();
229 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_PARENT_ID, "%u", (unsigned int) index);
232 // Adds latency on proplist for delivery to PULSEAUDIO
233 AUDIO_IO_LOGD("LATENCY : %s[%d]", __mSpec.getStreamLatencyToString(), __mSpec.getStreamLatency());
234 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_TIZEN_AUDIO_LATENCY, "%s", __mSpec.getStreamLatencyToString());
236 // Allocates PA mainloop
237 __mpMainloop = pa_threaded_mainloop_new();
238 if (__mpMainloop == NULL) {
239 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()");
242 // Allocates PA context
243 __mpContext = pa_context_new(pa_threaded_mainloop_get_api(__mpMainloop), CLIENT_NAME);
244 if (__mpContext == NULL) {
245 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()");
248 // Sets context state changed callback
249 pa_context_set_state_callback(__mpContext, __contextStateChangeCb, this);
251 // Connects this client with PA server
252 if (pa_context_connect(__mpContext, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) {
253 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_connect()");
256 // LOCK for synchronous connection
257 pa_threaded_mainloop_lock(__mpMainloop);
260 if (pa_threaded_mainloop_start(__mpMainloop) < 0) {
261 pa_threaded_mainloop_unlock(__mpMainloop);
262 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_threaded_mainloop_start()");
265 // Connection process is asynchronously
266 // So, this function will be waited when occurred context state change event
267 // If I got a signal, do next processing
269 pa_context_state_t state;
270 state = pa_context_get_state(__mpContext);
272 if (state == PA_CONTEXT_READY) {
276 if (!PA_CONTEXT_IS_GOOD(state)) {
277 err = pa_context_errno(__mpContext);
278 pa_threaded_mainloop_unlock(__mpMainloop);
279 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_context's state is not good : err[%d]", err);
282 /* Wait until the context is ready */
283 pa_threaded_mainloop_wait(__mpMainloop);
286 // Allocates PA stream
287 pa_sample_spec ss = __mSpec.getSampleSpec();
288 pa_channel_map map = __mSpec.getChannelMap();
290 __mpStream = pa_stream_new_with_proplist(__mpContext, __mSpec.getStreamName(), &ss, &map, __mpPropList);
291 if (__mpStream == NULL) {
292 pa_threaded_mainloop_unlock(__mpMainloop);
293 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_new_with_proplist()");
296 // Sets stream callbacks
297 pa_stream_set_state_callback(__mpStream, __streamStateChangeCb, this);
298 pa_stream_set_read_callback(__mpStream, __streamCaptureCb, this);
299 pa_stream_set_write_callback(__mpStream, __streamPlaybackCb, this);
300 pa_stream_set_latency_update_callback(__mpStream, __streamLatencyUpdateCb, this);
301 pa_stream_set_event_callback(__mpStream, __streamEventCb, this);
303 // Connect stream with PA Server
305 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
306 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
307 PA_STREAM_INTERPOLATE_TIMING |
308 PA_STREAM_ADJUST_LATENCY |
309 PA_STREAM_AUTO_TIMING_UPDATE);
311 ret = pa_stream_connect_playback(__mpStream, NULL, NULL, flags, NULL, NULL);
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_record(__mpStream, NULL, NULL, flags);
322 err = pa_context_errno(__mpContext);
323 pa_threaded_mainloop_unlock(__mpMainloop);
324 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_connect() : err[%d]", err);
328 pa_stream_state_t state;
329 state = pa_stream_get_state(__mpStream);
331 if (state == PA_STREAM_READY) {
332 AUDIO_IO_LOGD("STREAM READY");
336 if (!PA_STREAM_IS_GOOD(state)) {
337 err = pa_context_errno(__mpContext);
338 pa_threaded_mainloop_unlock(__mpMainloop);
339 if (err == PA_ERR_ACCESS) {
340 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_DEVICE_POLICY_RESTRICTION, "pa_stream's state is not good : err[%d]", err);
342 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_stream's state is not good : err[%d]", err);
346 /* Wait until the stream is ready */
347 pa_threaded_mainloop_wait(__mpMainloop);
350 // End of synchronous
351 pa_threaded_mainloop_unlock(__mpMainloop);
353 // __mIsInit = true; // Moved to __streamStateChangeCb()
354 } catch (CAudioError e) {
360 void CPulseAudioClient::finalize() {
362 if (__mIsInit == false) {
366 if (__mpMainloop != NULL) {
367 pa_threaded_mainloop_stop(__mpMainloop);
370 if (__mpStream != NULL) {
371 pa_stream_disconnect(__mpStream);
372 pa_stream_unref(__mpStream);
376 if (__mpContext != NULL) {
377 pa_context_disconnect(__mpContext);
378 pa_context_unref(__mpContext);
382 if (__mpMainloop != NULL) {
383 pa_threaded_mainloop_free(__mpMainloop);
387 if (__mpPropList != NULL) {
388 pa_proplist_free(__mpPropList);
395 int CPulseAudioClient::read(void* buffer, size_t length) throw(CAudioError) {
396 if (__mIsInit == false) {
397 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
402 if (buffer == NULL) {
403 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p]", buffer);
406 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
407 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
410 size_t lengthIter = length;
413 __mIsUsedSyncRead = true;
416 pa_threaded_mainloop_lock(__mpMainloop);
418 while (lengthIter > 0) {
421 while (__mpSyncReadDataPtr == NULL) {
422 ret = pa_stream_peek(__mpStream, &__mpSyncReadDataPtr, &__mSyncReadLength);
424 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_peek() : ret[%d]", ret);
427 if (__mSyncReadLength <= 0) {
428 #ifdef _AUDIO_IO_DEBUG_TIMING_
429 AUDIO_IO_LOGD("readLength(%d byte) is not valid. wait...", __mSyncReadLength);
431 pa_threaded_mainloop_wait(__mpMainloop);
432 } else if (__mpSyncReadDataPtr == NULL) {
433 // Data peeked, but it doesn't have any data
434 ret = pa_stream_drop(__mpStream);
436 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
439 __mSyncReadIndex = 0;
443 if (__mSyncReadLength < lengthIter) {
444 l = __mSyncReadLength;
449 // Copy partial pcm data on out parameter
450 #ifdef _AUDIO_IO_DEBUG_TIMING_
451 AUDIO_IO_LOGD("memcpy() that a peeked buffer[0x%x], index[%d], length[%d] on out buffer", (const uint8_t*)(__mpSyncReadDataPtr) + __mSyncReadIndex, __mSyncReadIndex, l);
453 memcpy(buffer, (const uint8_t*)__mpSyncReadDataPtr + __mSyncReadIndex, l);
455 // Move next position
456 buffer = (uint8_t*)buffer + l;
459 // Adjusts the rest length
460 __mSyncReadIndex += l;
461 __mSyncReadLength -= l;
463 if (__mSyncReadLength == 0) {
464 #ifdef _AUDIO_IO_DEBUG_TIMING_
465 AUDIO_IO_LOGD("__mSyncReadLength is zero, do drop()");
467 ret = pa_stream_drop(__mpStream);
469 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
472 // Reset the internal pointer
473 __mpSyncReadDataPtr = NULL;
474 __mSyncReadLength = 0;
475 __mSyncReadIndex = 0;
477 } // End of while (lengthIter > 0)
479 pa_threaded_mainloop_unlock(__mpMainloop);
480 __mIsUsedSyncRead = false;
481 } catch (CAudioError e) {
482 pa_threaded_mainloop_unlock(__mpMainloop);
483 __mIsUsedSyncRead = false;
490 int CPulseAudioClient::peek(const void** buffer, size_t* length) throw(CAudioError) {
491 if (__mIsInit == false) {
492 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
497 if (buffer == NULL || length == NULL) {
498 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p], length[%p]", buffer, length);
501 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
502 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
507 if (isInThread() == false) {
508 pa_threaded_mainloop_lock(__mpMainloop);
509 ret = pa_stream_peek(__mpStream, buffer, length);
510 pa_threaded_mainloop_unlock(__mpMainloop);
512 ret = pa_stream_peek(__mpStream, buffer, length);
515 #ifdef _AUDIO_IO_DEBUG_TIMING_
516 AUDIO_IO_LOGD("buffer[%p], length[%d]", *buffer, *length);
520 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_peek() : err[%d]", ret);
526 int CPulseAudioClient::drop() throw(CAudioError) {
527 if (__mIsInit == false) {
528 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
531 #ifdef _AUDIO_IO_DEBUG_TIMING_
537 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
538 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
543 if (isInThread() == false) {
544 pa_threaded_mainloop_lock(__mpMainloop);
545 ret = pa_stream_drop(__mpStream);
546 pa_threaded_mainloop_unlock(__mpMainloop);
548 ret = pa_stream_drop(__mpStream);
552 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_drop() : err[%d]", ret);
558 int CPulseAudioClient::write(const void* data, size_t length) throw(CAudioError) {
560 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid");
563 if (__mDirection == EStreamDirection::STREAM_DIRECTION_RECORD) {
564 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
569 #ifdef _AUDIO_IO_DEBUG_TIMING_
570 AUDIO_IO_LOGD("data[%p], length[%d]", data, length);
572 if (pa_stream_is_corked(__mpStream)) {
573 AUDIO_IO_LOGW("stream is corked...do uncork here first!!!!");
574 pa_operation_unref(pa_stream_cork(__mpStream, 0, NULL, this));
577 if (isInThread() == false) {
578 pa_threaded_mainloop_lock(__mpMainloop);
579 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
580 pa_threaded_mainloop_unlock(__mpMainloop);
582 if (__mIsFirstStream) {
583 const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
584 uint32_t prebuf = attr->prebuf;
585 // Compensate amount of prebuf in first stream callback when audio-io use prebuf(-1)
586 // Because a stream will never start when an application wrote less than prebuf at first
587 if (length < prebuf) {
588 char* dummy = new char[prebuf - length];
589 memset(dummy, 0, prebuf - length);
590 pa_stream_write(__mpStream, dummy, prebuf - length, NULL, 0LL, PA_SEEK_RELATIVE);
593 __mIsFirstStream = false;
594 AUDIO_IO_LOGD("FIRST STREAM CALLBACK : length[%d], prebuf[%d], dummy[%d]", length, prebuf, (length < prebuf) ? prebuf - length : 0);
596 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
600 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_write() : err[%d]", ret);
606 void CPulseAudioClient::cork(bool cork) throw(CAudioError) {
607 AUDIO_IO_LOGD("cork[%d]", cork);
609 if (__mIsInit == false) {
610 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
613 if (isInThread() == true) {
614 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This operation is not supported in callback");
619 // Set __mIsFirstStream flag when uncork(resume) stream, because prebuf will be enable again
621 __mIsFirstStream = true;
623 if (isInThread() == false) {
624 pa_threaded_mainloop_lock(__mpMainloop);
625 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
626 pa_threaded_mainloop_unlock(__mpMainloop);
628 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
634 bool CPulseAudioClient::isCorked() throw(CAudioError) {
635 if (__mIsInit == false) {
636 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
643 if (isInThread() == false) {
644 pa_threaded_mainloop_lock(__mpMainloop);
645 isCork = pa_stream_is_corked(__mpStream);
646 pa_threaded_mainloop_unlock(__mpMainloop);
648 isCork = pa_stream_is_corked(__mpStream);
651 AUDIO_IO_LOGD("isCork[%d]", isCork);
652 return static_cast<bool>(isCork);
655 bool CPulseAudioClient::drain() throw(CAudioError) {
656 AUDIO_IO_LOGD("drain");
658 if (__mIsInit == false) {
659 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
664 if (isInThread() == false) {
665 pa_threaded_mainloop_lock(__mpMainloop);
666 pa_operation_unref(pa_stream_drain(__mpStream, __successStreamCb, this));
667 pa_threaded_mainloop_unlock(__mpMainloop);
669 pa_operation_unref(pa_stream_drain(__mpStream, __successStreamCb, this));
675 bool CPulseAudioClient::flush() throw(CAudioError) {
676 AUDIO_IO_LOGD("flush");
678 if (__mIsInit == false) {
679 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
684 if (isInThread() == false) {
685 pa_threaded_mainloop_lock(__mpMainloop);
686 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
687 pa_threaded_mainloop_unlock(__mpMainloop);
689 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
695 size_t CPulseAudioClient::getWritableSize() throw(CAudioError) {
696 if (__mIsInit == false) {
697 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
702 if (__mDirection != EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
703 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Playback");
708 if (isInThread() == false) {
709 pa_threaded_mainloop_lock(__mpMainloop);
710 ret = pa_stream_writable_size(__mpStream);
711 pa_threaded_mainloop_unlock(__mpMainloop);
713 ret = pa_stream_writable_size(__mpStream);
719 void CPulseAudioClient::checkRunningState() throw(CAudioError) {
720 if (__mpContext == NULL || PA_CONTEXT_IS_GOOD(pa_context_get_state(__mpContext)) == 0) {
721 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] is not created or not good state", __mpContext);
723 if (__mpStream == NULL || PA_STREAM_IS_GOOD(pa_stream_get_state(__mpStream)) == 0) {
724 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The stream[%p] is not created or not good state", __mpStream);
726 if (pa_context_get_state(__mpContext) != PA_CONTEXT_READY || pa_stream_get_state(__mpStream) != PA_STREAM_READY) {
727 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] or stream[%p] state is not ready", __mpContext, __mpStream);
730 #ifdef _AUDIO_IO_DEBUG_TIMING_
731 AUDIO_IO_LOGD("This client is running");
735 bool CPulseAudioClient::isInThread() throw(CAudioError) {
736 int ret = pa_threaded_mainloop_in_thread(__mpMainloop);
738 #ifdef _AUDIO_IO_DEBUG_TIMING_
739 AUDIO_IO_LOGD("isInThread[%d]", ret);
741 return static_cast<bool>(ret);
744 size_t CPulseAudioClient::getReadableSize() throw(CAudioError) {
745 if (__mIsInit == false) {
746 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
751 if (__mDirection != EStreamDirection::STREAM_DIRECTION_RECORD) {
752 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Capture");
757 if (isInThread() == false) {
758 pa_threaded_mainloop_lock(__mpMainloop);
759 ret = pa_stream_writable_size(__mpStream);
760 pa_threaded_mainloop_unlock(__mpMainloop);
762 ret = pa_stream_writable_size(__mpStream);
768 size_t CPulseAudioClient::getBufferSize() throw(CAudioError) {
769 if (__mIsInit == false) {
770 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
778 if (isInThread() == false) {
779 pa_threaded_mainloop_lock(__mpMainloop);
782 const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
784 int _err = pa_context_errno(__mpContext);
785 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_buffer_attr() : err[%d]", _err);
788 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
790 AUDIO_IO_LOGD("PLAYBACK buffer size[%d]", ret);
792 ret = attr->fragsize;
793 AUDIO_IO_LOGD("RECORD buffer size[%d]", ret);
795 } catch (CAudioError err) {
796 if (isInThread() == false) {
797 pa_threaded_mainloop_unlock(__mpMainloop);
802 if (isInThread() == false) {
803 pa_threaded_mainloop_unlock(__mpMainloop);
809 pa_usec_t CPulseAudioClient::getLatency() throw(CAudioError) {
810 if (__mIsInit == false) {
811 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
819 if (isInThread() == false) {
820 if (pa_stream_get_latency(__mpStream, &ret, &negative) < 0) {
821 int _err = pa_context_errno(__mpContext);
822 if (_err != PA_ERR_NODATA) {
823 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
826 return negative ? 0 : ret;
829 pa_threaded_mainloop_lock(__mpMainloop);
833 if (pa_stream_get_latency(__mpStream, &ret, &negative) >= 0) {
837 int _err = pa_context_errno(__mpContext);
838 if (_err != PA_ERR_NODATA) {
839 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
842 /* Wait until latency data is available again */
843 pa_threaded_mainloop_wait(__mpMainloop);
845 } catch (CAudioError e) {
846 pa_threaded_mainloop_unlock(__mpMainloop);
850 pa_threaded_mainloop_unlock(__mpMainloop);
852 return negative ? 0 : ret;
855 pa_usec_t CPulseAudioClient::getFinalLatency() throw(CAudioError) {
856 if (__mIsInit == false) {
857 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
866 if (isInThread() == false) {
867 pa_threaded_mainloop_lock(__mpMainloop);
870 ver = pa_context_get_server_protocol_version(__mpContext);
872 const pa_buffer_attr* buffer_attr = pa_stream_get_buffer_attr(__mpStream);
873 const pa_sample_spec* sample_spec = pa_stream_get_sample_spec(__mpStream);
874 const pa_timing_info* timing_info = pa_stream_get_timing_info(__mpStream);
876 if (buffer_attr == NULL || sample_spec == NULL || timing_info == NULL) {
877 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",
878 buffer_attr, sample_spec, timing_info);
881 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
882 ret = (pa_bytes_to_usec(buffer_attr->tlength, sample_spec) + timing_info->configured_sink_usec);
883 AUDIO_IO_LOGD("FINAL PLAYBACK LATENCY[%d]", ret);
885 ret = (pa_bytes_to_usec(buffer_attr->fragsize, sample_spec) + timing_info->configured_source_usec);
886 AUDIO_IO_LOGD("FINAL RECORD LATENCY[%d]", ret);
889 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED, "This version(ver.%d) is not supported", ver);
892 if (isInThread() == false) {
893 pa_threaded_mainloop_unlock(__mpMainloop);
895 } catch (CAudioError e) {
896 if (isInThread() == false) {
897 pa_threaded_mainloop_unlock(__mpMainloop);
905 CPulseAudioClient::EStreamDirection CPulseAudioClient::getStreamDirection() {
909 CPulseStreamSpec CPulseAudioClient::getStreamSpec() {