2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "CAudioIODef.h"
22 #include <dpm/restriction.h>
26 using namespace tizen_media_audio;
30 * class CPulseAudioClient
32 const char* CPulseAudioClient::CLIENT_NAME = "AUDIO_IO_PA_CLIENT";
33 static const unsigned int drain_wait_interval = 10000;
34 static const unsigned int drain_wait_max_count = 30;
36 CPulseAudioClient::CPulseAudioClient(
37 EStreamDirection direction,
38 CPulseStreamSpec& spec,
39 IPulseStreamListener* listener) :
40 __mDirection(direction),
42 __mpListener(listener),
48 __mIsOperationSuccess(false),
49 __mpSyncReadDataPtr(NULL),
52 __mIsUsedSyncRead(false),
53 __mIsFirstStream(false),
54 __mIsDraining(false) {
57 CPulseAudioClient::~CPulseAudioClient() {
61 void CPulseAudioClient::__contextStateChangeCb(pa_context* c, void* user_data) {
62 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
66 switch (pa_context_get_state(c)) {
67 case PA_CONTEXT_READY:
68 AUDIO_IO_LOGD("The context is ready");
69 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
72 case PA_CONTEXT_FAILED:
73 case PA_CONTEXT_TERMINATED:
74 AUDIO_IO_LOGD("The context is lost");
75 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
78 case PA_CONTEXT_UNCONNECTED:
79 case PA_CONTEXT_CONNECTING:
80 case PA_CONTEXT_AUTHORIZING:
81 case PA_CONTEXT_SETTING_NAME:
86 void CPulseAudioClient::__successContextCb(pa_context* c, int success, void* user_data) {
87 AUDIO_IO_LOGD("pa_context[%p], success[%d], user_data[%p]", c, success, user_data);
91 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
92 pClient->__mIsOperationSuccess = static_cast<bool>(success);
94 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
97 static bool __is_microphone_restricted(void) {
100 device_policy_manager_h dpm_h = NULL;
103 if ((dpm_h = dpm_manager_create())) {
104 /* state: 0(disallowed), 1(allowed) */
105 if ((ret = dpm_restriction_get_microphone_state(dpm_h, &state)))
106 AUDIO_IO_LOGE("Failed to dpm_restriction_get_microphone_state(), ret(0x%x)", ret);
107 dpm_manager_destroy(dpm_h);
108 AUDIO_IO_LOGD("microphone restriction state: %d(1:allowed, 0:disallowed)", state);
110 AUDIO_IO_LOGE("Failed to dpm_manager_create()");
113 return (state ? false : true);
116 void CPulseAudioClient::__streamStateChangeCb(pa_stream* s, void* user_data) {
120 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
122 switch (pa_stream_get_state(s)) {
123 case PA_STREAM_READY:
124 AUDIO_IO_LOGD("The stream is ready");
125 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
126 if (pClient->__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK)
127 pClient->__mIsFirstStream = true;
128 pClient->__mIsInit = true;
129 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
132 case PA_STREAM_FAILED:
133 AUDIO_IO_LOGD("The stream is failed");
134 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE,
135 __is_microphone_restricted());
136 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
139 case PA_STREAM_TERMINATED:
140 AUDIO_IO_LOGD("The stream is terminated");
141 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
142 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
145 case PA_STREAM_UNCONNECTED:
146 case PA_STREAM_CREATING:
151 void CPulseAudioClient::__streamCaptureCb(pa_stream* s, size_t length, void* user_data) {
155 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
156 assert(pClient->__mpListener);
157 assert(pClient->__mpMainloop);
159 if (pClient->__mIsUsedSyncRead == true) {
160 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
163 pClient->__mpListener->onStream(pClient, length);
166 void CPulseAudioClient::__streamPlaybackCb(pa_stream* s, size_t length, void* user_data) {
170 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
171 assert(pClient->__mpListener);
173 pClient->__mpListener->onStream(pClient, length);
175 /* If stream is not written in first callback during prepare,
176 then write dummy data to ensure the start */
177 if (pClient->__mIsFirstStream) {
178 AUDIO_IO_LOGD("Write dummy, length [%d]", length);
180 char* dummy = new char[length];
181 memset(dummy, 0, length);
182 pa_stream_write(s, dummy, length, NULL, 0LL, PA_SEEK_RELATIVE);
185 pClient->__mIsFirstStream = false;
189 void CPulseAudioClient::__streamLatencyUpdateCb(pa_stream* s, void* user_data) {
193 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
195 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
198 void CPulseAudioClient::__streamEventCb(pa_stream* s, const char *name, pa_proplist *pl, void *user_data) {
202 AUDIO_IO_LOGE("NAME : %s, Prop : %p", name, pl);
204 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
205 if (strcmp(name, PA_STREAM_EVENT_POP_TIMEOUT) == 0) {
206 pa_operation_unref(pa_stream_cork(pClient->__mpStream, 1, NULL, NULL));
211 void CPulseAudioClient::__successStreamCb(pa_stream* s, int success, void* user_data) {
212 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
216 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
217 pClient->__mIsOperationSuccess = static_cast<bool>(success);
219 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
222 void CPulseAudioClient::__successDrainCbInThread(pa_stream* s, int success, void* user_data) {
223 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
227 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
228 pClient->__mIsOperationSuccess = static_cast<bool>(success);
229 pClient->__mIsDraining = false;
232 void CPulseAudioClient::__successDrainCb(pa_stream* s, int success, void* user_data) {
233 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
237 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
238 pClient->__mIsOperationSuccess = static_cast<bool>(success);
239 pClient->__mIsDraining = false;
241 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
244 void CPulseAudioClient::initialize() throw(CAudioError) {
245 if (__mIsInit == true) {
253 // Allocates PA proplist
254 __mpPropList = pa_proplist_new();
255 if (__mpPropList == NULL) {
256 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_proplist_new()");
259 // Adds values on proplist for delivery to PULSEAUDIO
260 char *streamType = NULL;
261 CAudioInfo::EAudioType audioType = __mSpec.getAudioInfo().getAudioType();
262 __mSpec.getAudioInfo().convertAudioType2StreamType(audioType, &streamType);
263 pa_proplist_sets(__mpPropList, PA_PROP_MEDIA_ROLE, streamType);
265 int index = __mSpec.getAudioInfo().getAudioIndex();
267 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_PARENT_ID, "%u", (unsigned int) index);
270 // Adds latency on proplist for delivery to PULSEAUDIO
271 AUDIO_IO_LOGD("LATENCY : %s[%d]", __mSpec.getStreamLatencyToString(), __mSpec.getStreamLatency());
272 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_TIZEN_AUDIO_LATENCY, "%s", __mSpec.getStreamLatencyToString());
274 // Allocates PA mainloop
275 __mpMainloop = pa_threaded_mainloop_new();
276 if (__mpMainloop == NULL) {
277 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()");
280 // Allocates PA context
281 __mpContext = pa_context_new(pa_threaded_mainloop_get_api(__mpMainloop), CLIENT_NAME);
282 if (__mpContext == NULL) {
283 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()");
286 // Sets context state changed callback
287 pa_context_set_state_callback(__mpContext, __contextStateChangeCb, this);
289 // Connects this client with PA server
290 if (pa_context_connect(__mpContext, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) {
291 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_connect()");
294 // LOCK for synchronous connection
295 pa_threaded_mainloop_lock(__mpMainloop);
298 if (pa_threaded_mainloop_start(__mpMainloop) < 0) {
299 pa_threaded_mainloop_unlock(__mpMainloop);
300 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_threaded_mainloop_start()");
303 // Connection process is asynchronously
304 // So, this function will be waited when occurred context state change event
305 // If I got a signal, do next processing
307 pa_context_state_t state;
308 state = pa_context_get_state(__mpContext);
310 if (state == PA_CONTEXT_READY) {
314 if (!PA_CONTEXT_IS_GOOD(state)) {
315 err = pa_context_errno(__mpContext);
316 pa_threaded_mainloop_unlock(__mpMainloop);
317 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_context's state is not good : err[%d]", err);
320 /* Wait until the context is ready */
321 pa_threaded_mainloop_wait(__mpMainloop);
324 // Allocates PA stream
325 pa_sample_spec ss = __mSpec.getSampleSpec();
326 pa_channel_map map = __mSpec.getChannelMap();
328 __mpStream = pa_stream_new_with_proplist(__mpContext, __mSpec.getStreamName(), &ss, &map, __mpPropList);
329 if (__mpStream == NULL) {
330 pa_threaded_mainloop_unlock(__mpMainloop);
331 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_new_with_proplist()");
334 // Sets stream callbacks
335 pa_stream_set_state_callback(__mpStream, __streamStateChangeCb, this);
336 pa_stream_set_read_callback(__mpStream, __streamCaptureCb, this);
337 pa_stream_set_write_callback(__mpStream, __streamPlaybackCb, this);
338 pa_stream_set_latency_update_callback(__mpStream, __streamLatencyUpdateCb, this);
339 pa_stream_set_event_callback(__mpStream, __streamEventCb, this);
341 // Connect stream with PA Server
343 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
344 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
345 PA_STREAM_INTERPOLATE_TIMING |
346 PA_STREAM_ADJUST_LATENCY |
347 PA_STREAM_AUTO_TIMING_UPDATE);
349 ret = pa_stream_connect_playback(__mpStream, NULL, NULL, flags, NULL, NULL);
351 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
352 PA_STREAM_INTERPOLATE_TIMING |
353 PA_STREAM_ADJUST_LATENCY |
354 PA_STREAM_AUTO_TIMING_UPDATE);
356 ret = pa_stream_connect_record(__mpStream, NULL, NULL, flags);
360 err = pa_context_errno(__mpContext);
361 pa_threaded_mainloop_unlock(__mpMainloop);
362 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_connect() : err[%d]", err);
366 pa_stream_state_t state;
367 state = pa_stream_get_state(__mpStream);
369 if (state == PA_STREAM_READY) {
370 AUDIO_IO_LOGD("STREAM READY");
374 if (!PA_STREAM_IS_GOOD(state)) {
375 err = pa_context_errno(__mpContext);
376 pa_threaded_mainloop_unlock(__mpMainloop);
377 if (err == PA_ERR_ACCESS) {
378 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_DEVICE_POLICY_RESTRICTION, "pa_stream's state is not good : err[%d]", err);
380 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_stream's state is not good : err[%d]", err);
384 /* Wait until the stream is ready */
385 pa_threaded_mainloop_wait(__mpMainloop);
388 // End of synchronous
389 pa_threaded_mainloop_unlock(__mpMainloop);
391 // __mIsInit = true; // Moved to __streamStateChangeCb()
392 } catch (CAudioError e) {
398 void CPulseAudioClient::finalize() {
400 if (__mIsInit == false) {
404 if (isInThread() == false)
405 pa_threaded_mainloop_lock(__mpMainloop);
407 /* clear callbacks */
408 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK)
409 pa_stream_set_write_callback(__mpStream, NULL, NULL);
411 pa_stream_set_read_callback(__mpStream, NULL, NULL);
412 pa_stream_set_latency_update_callback(__mpStream, NULL, NULL);
413 pa_stream_set_event_callback(__mpStream, NULL, NULL);
415 if (isInThread() == false)
416 pa_threaded_mainloop_unlock(__mpMainloop);
418 /* Wait for drain complete if draining before finalize */
420 unsigned int drain_wait_count = 0;
421 while (__mIsDraining && drain_wait_count++ < drain_wait_max_count) {
422 usleep(drain_wait_interval);
424 AUDIO_IO_LOGD("wait for drain complete!!!! [%d * %d usec]",
425 drain_wait_count, drain_wait_interval);
428 if (__mpMainloop != NULL) {
429 pa_threaded_mainloop_stop(__mpMainloop);
432 if (__mpStream != NULL) {
433 pa_stream_disconnect(__mpStream);
434 pa_stream_unref(__mpStream);
438 if (__mpContext != NULL) {
439 pa_context_disconnect(__mpContext);
440 pa_context_unref(__mpContext);
444 if (__mpMainloop != NULL) {
445 pa_threaded_mainloop_free(__mpMainloop);
449 if (__mpPropList != NULL) {
450 pa_proplist_free(__mpPropList);
457 int CPulseAudioClient::read(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) {
465 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p]", buffer);
468 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
469 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
472 size_t lengthIter = length;
475 __mIsUsedSyncRead = true;
478 pa_threaded_mainloop_lock(__mpMainloop);
480 while (lengthIter > 0) {
483 while (__mpSyncReadDataPtr == NULL) {
484 ret = pa_stream_peek(__mpStream, &__mpSyncReadDataPtr, &__mSyncReadLength);
486 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_peek() : ret[%d]", ret);
489 if (__mSyncReadLength <= 0) {
490 #ifdef _AUDIO_IO_DEBUG_TIMING_
491 AUDIO_IO_LOGD("readLength(%d byte) is not valid. wait...", __mSyncReadLength);
493 pa_threaded_mainloop_wait(__mpMainloop);
494 } else if (__mpSyncReadDataPtr == NULL) {
495 // Data peeked, but it doesn't have any data
496 ret = pa_stream_drop(__mpStream);
498 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
501 __mSyncReadIndex = 0;
505 if (__mSyncReadLength < lengthIter) {
506 l = __mSyncReadLength;
511 // Copy partial pcm data on out parameter
512 #ifdef _AUDIO_IO_DEBUG_TIMING_
513 AUDIO_IO_LOGD("memcpy() that a peeked buffer[0x%x], index[%d], length[%d] on out buffer", (const uint8_t*)(__mpSyncReadDataPtr) + __mSyncReadIndex, __mSyncReadIndex, l);
515 memcpy(buffer, (const uint8_t*)__mpSyncReadDataPtr + __mSyncReadIndex, l);
517 // Move next position
518 buffer = (uint8_t*)buffer + l;
521 // Adjusts the rest length
522 __mSyncReadIndex += l;
523 __mSyncReadLength -= l;
525 if (__mSyncReadLength == 0) {
526 #ifdef _AUDIO_IO_DEBUG_TIMING_
527 AUDIO_IO_LOGD("__mSyncReadLength is zero, do drop()");
529 ret = pa_stream_drop(__mpStream);
531 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
534 // Reset the internal pointer
535 __mpSyncReadDataPtr = NULL;
536 __mSyncReadLength = 0;
537 __mSyncReadIndex = 0;
539 } // End of while (lengthIter > 0)
541 pa_threaded_mainloop_unlock(__mpMainloop);
542 __mIsUsedSyncRead = false;
543 } catch (CAudioError e) {
544 pa_threaded_mainloop_unlock(__mpMainloop);
545 __mIsUsedSyncRead = false;
552 int CPulseAudioClient::peek(const void** buffer, size_t* length) throw(CAudioError) {
553 if (__mIsInit == false) {
554 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
559 if (buffer == NULL || length == NULL) {
560 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p], length[%p]", buffer, length);
563 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
564 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
569 if (isInThread() == false) {
570 pa_threaded_mainloop_lock(__mpMainloop);
571 ret = pa_stream_peek(__mpStream, buffer, length);
572 pa_threaded_mainloop_unlock(__mpMainloop);
574 ret = pa_stream_peek(__mpStream, buffer, length);
577 #ifdef _AUDIO_IO_DEBUG_TIMING_
578 AUDIO_IO_LOGD("buffer[%p], length[%d]", *buffer, *length);
582 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_peek() : err[%d]", ret);
588 int CPulseAudioClient::drop() throw(CAudioError) {
589 if (__mIsInit == false) {
590 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
593 #ifdef _AUDIO_IO_DEBUG_TIMING_
599 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
600 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
605 if (isInThread() == false) {
606 pa_threaded_mainloop_lock(__mpMainloop);
607 ret = pa_stream_drop(__mpStream);
608 pa_threaded_mainloop_unlock(__mpMainloop);
610 ret = pa_stream_drop(__mpStream);
614 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_drop() : err[%d]", ret);
620 int CPulseAudioClient::write(const void* data, size_t length) throw(CAudioError) {
622 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid");
625 if (__mDirection == EStreamDirection::STREAM_DIRECTION_RECORD) {
626 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
631 #ifdef _AUDIO_IO_DEBUG_TIMING_
632 AUDIO_IO_LOGD("data[%p], length[%d]", data, length);
634 if (pa_stream_is_corked(__mpStream)) {
635 AUDIO_IO_LOGW("stream is corked...do uncork here first!!!!");
636 pa_operation_unref(pa_stream_cork(__mpStream, 0, NULL, this));
639 if (isInThread() == false) {
640 pa_threaded_mainloop_lock(__mpMainloop);
641 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
642 pa_threaded_mainloop_unlock(__mpMainloop);
644 if (__mIsFirstStream) {
645 const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
646 uint32_t prebuf = attr->prebuf;
647 // Compensate amount of prebuf in first stream callback when audio-io use prebuf(-1)
648 // Because a stream will never start when an application wrote less than prebuf at first
649 if (length < prebuf) {
650 char* dummy = new char[prebuf - length];
651 memset(dummy, 0, prebuf - length);
652 pa_stream_write(__mpStream, dummy, prebuf - length, NULL, 0LL, PA_SEEK_RELATIVE);
655 __mIsFirstStream = false;
656 AUDIO_IO_LOGW("FIRST STREAM CALLBACK : length[%d], prebuf[%d], dummy[%d]",
657 length, prebuf, (length < prebuf) ? prebuf - length : 0);
659 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
663 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_write() : err[%d]", ret);
669 void CPulseAudioClient::cork(bool cork) throw(CAudioError) {
670 AUDIO_IO_LOGD("cork[%d]", cork);
672 if (__mIsInit == false) {
673 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
676 if (isInThread() == true) {
677 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This operation is not supported in callback");
682 // Set __mIsFirstStream flag when uncork(resume) stream, because prebuf will be enable again
684 __mIsFirstStream = true;
686 if (isInThread() == false) {
687 pa_threaded_mainloop_lock(__mpMainloop);
688 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
689 pa_threaded_mainloop_unlock(__mpMainloop);
691 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
697 bool CPulseAudioClient::isCorked() throw(CAudioError) {
698 if (__mIsInit == false) {
699 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
706 if (isInThread() == false) {
707 pa_threaded_mainloop_lock(__mpMainloop);
708 isCork = pa_stream_is_corked(__mpStream);
709 pa_threaded_mainloop_unlock(__mpMainloop);
711 isCork = pa_stream_is_corked(__mpStream);
714 AUDIO_IO_LOGD("isCork[%d]", isCork);
715 return static_cast<bool>(isCork);
718 bool CPulseAudioClient::drain() throw(CAudioError) {
719 if (__mIsInit == false) {
720 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
726 AUDIO_IO_LOGW("already draining...");
728 if (isInThread() == false) {
729 AUDIO_IO_LOGD("drain");
730 pa_threaded_mainloop_lock(__mpMainloop);
731 pa_operation* o = pa_stream_drain(__mpStream, __successDrainCb, this);
732 __mIsDraining = true;
733 while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
734 pa_threaded_mainloop_wait(__mpMainloop);
736 pa_operation_unref(o);
737 pa_threaded_mainloop_unlock(__mpMainloop);
739 AUDIO_IO_LOGD("drain in thread");
740 pa_operation_unref(pa_stream_drain(__mpStream, __successDrainCbInThread, this));
741 __mIsDraining = true;
747 bool CPulseAudioClient::flush() throw(CAudioError) {
748 if (__mIsInit == false) {
749 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
754 if (isInThread() == false) {
755 AUDIO_IO_LOGD("flush");
756 pa_threaded_mainloop_lock(__mpMainloop);
757 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
758 pa_threaded_mainloop_unlock(__mpMainloop);
760 AUDIO_IO_LOGD("flush in thread");
761 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
767 size_t CPulseAudioClient::getWritableSize() throw(CAudioError) {
768 if (__mIsInit == false) {
769 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
774 if (__mDirection != EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
775 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Playback");
780 if (isInThread() == false) {
781 pa_threaded_mainloop_lock(__mpMainloop);
782 ret = pa_stream_writable_size(__mpStream);
783 pa_threaded_mainloop_unlock(__mpMainloop);
785 ret = pa_stream_writable_size(__mpStream);
791 void CPulseAudioClient::checkRunningState() throw(CAudioError) {
792 if (__mpContext == NULL || PA_CONTEXT_IS_GOOD(pa_context_get_state(__mpContext)) == 0) {
793 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] is not created or not good state", __mpContext);
795 if (__mpStream == NULL || PA_STREAM_IS_GOOD(pa_stream_get_state(__mpStream)) == 0) {
796 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The stream[%p] is not created or not good state", __mpStream);
798 if (pa_context_get_state(__mpContext) != PA_CONTEXT_READY || pa_stream_get_state(__mpStream) != PA_STREAM_READY) {
799 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] or stream[%p] state is not ready", __mpContext, __mpStream);
802 #ifdef _AUDIO_IO_DEBUG_TIMING_
803 AUDIO_IO_LOGD("This client is running");
807 bool CPulseAudioClient::isInThread() throw(CAudioError) {
808 int ret = pa_threaded_mainloop_in_thread(__mpMainloop);
810 #ifdef _AUDIO_IO_DEBUG_TIMING_
811 AUDIO_IO_LOGD("isInThread[%d]", ret);
813 return static_cast<bool>(ret);
816 size_t CPulseAudioClient::getReadableSize() throw(CAudioError) {
817 if (__mIsInit == false) {
818 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
823 if (__mDirection != EStreamDirection::STREAM_DIRECTION_RECORD) {
824 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Capture");
829 if (isInThread() == false) {
830 pa_threaded_mainloop_lock(__mpMainloop);
831 ret = pa_stream_writable_size(__mpStream);
832 pa_threaded_mainloop_unlock(__mpMainloop);
834 ret = pa_stream_writable_size(__mpStream);
840 size_t CPulseAudioClient::getBufferSize() throw(CAudioError) {
841 if (__mIsInit == false) {
842 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
850 if (isInThread() == false) {
851 pa_threaded_mainloop_lock(__mpMainloop);
854 const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
856 int _err = pa_context_errno(__mpContext);
857 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_buffer_attr() : err[%d]", _err);
860 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
862 AUDIO_IO_LOGD("PLAYBACK buffer size[%d]", ret);
864 ret = attr->fragsize;
865 AUDIO_IO_LOGD("RECORD buffer size[%d]", ret);
867 } catch (CAudioError err) {
868 if (isInThread() == false) {
869 pa_threaded_mainloop_unlock(__mpMainloop);
874 if (isInThread() == false) {
875 pa_threaded_mainloop_unlock(__mpMainloop);
881 pa_usec_t CPulseAudioClient::getLatency() throw(CAudioError) {
882 if (__mIsInit == false) {
883 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
891 if (isInThread() == false) {
892 if (pa_stream_get_latency(__mpStream, &ret, &negative) < 0) {
893 int _err = pa_context_errno(__mpContext);
894 if (_err != PA_ERR_NODATA) {
895 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
898 return negative ? 0 : ret;
901 pa_threaded_mainloop_lock(__mpMainloop);
905 if (pa_stream_get_latency(__mpStream, &ret, &negative) >= 0) {
909 int _err = pa_context_errno(__mpContext);
910 if (_err != PA_ERR_NODATA) {
911 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
914 /* Wait until latency data is available again */
915 pa_threaded_mainloop_wait(__mpMainloop);
917 } catch (CAudioError e) {
918 pa_threaded_mainloop_unlock(__mpMainloop);
922 pa_threaded_mainloop_unlock(__mpMainloop);
924 return negative ? 0 : ret;
927 pa_usec_t CPulseAudioClient::getFinalLatency() throw(CAudioError) {
928 if (__mIsInit == false) {
929 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
938 if (isInThread() == false) {
939 pa_threaded_mainloop_lock(__mpMainloop);
942 ver = pa_context_get_server_protocol_version(__mpContext);
944 const pa_buffer_attr* buffer_attr = pa_stream_get_buffer_attr(__mpStream);
945 const pa_sample_spec* sample_spec = pa_stream_get_sample_spec(__mpStream);
946 const pa_timing_info* timing_info = pa_stream_get_timing_info(__mpStream);
948 if (buffer_attr == NULL || sample_spec == NULL || timing_info == NULL) {
949 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",
950 buffer_attr, sample_spec, timing_info);
953 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
954 ret = (pa_bytes_to_usec(buffer_attr->tlength, sample_spec) + timing_info->configured_sink_usec);
955 AUDIO_IO_LOGD("FINAL PLAYBACK LATENCY[%d]", ret);
957 ret = (pa_bytes_to_usec(buffer_attr->fragsize, sample_spec) + timing_info->configured_source_usec);
958 AUDIO_IO_LOGD("FINAL RECORD LATENCY[%d]", ret);
961 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED, "This version(ver.%d) is not supported", ver);
964 if (isInThread() == false) {
965 pa_threaded_mainloop_unlock(__mpMainloop);
967 } catch (CAudioError e) {
968 if (isInThread() == false) {
969 pa_threaded_mainloop_unlock(__mpMainloop);
977 CPulseAudioClient::EStreamDirection CPulseAudioClient::getStreamDirection() {
981 CPulseStreamSpec CPulseAudioClient::getStreamSpec() {