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 #include <dpm/restriction.h>
27 using namespace tizen_media_audio;
31 * class CPulseAudioClient
33 const char* CPulseAudioClient::CLIENT_NAME = "AUDIO_IO_PA_CLIENT";
34 static const unsigned int drain_wait_interval = 10000;
35 static const unsigned int drain_wait_max_count = 30;
37 CPulseAudioClient::CPulseAudioClient(
38 EStreamDirection direction,
39 CPulseStreamSpec& spec,
40 IPulseStreamListener* listener) :
41 __mDirection(direction),
43 __mpListener(listener),
49 __mIsOperationSuccess(false),
50 __mpSyncReadDataPtr(NULL),
53 __mIsUsedSyncRead(false),
54 __mIsFirstStream(false),
55 __mIsDraining(false) {
58 CPulseAudioClient::~CPulseAudioClient() {
62 void CPulseAudioClient::__contextStateChangeCb(pa_context* c, void* user_data) {
63 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
67 switch (pa_context_get_state(c)) {
68 case PA_CONTEXT_READY:
69 AUDIO_IO_LOGD("The context is ready");
70 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
73 case PA_CONTEXT_FAILED:
74 case PA_CONTEXT_TERMINATED:
75 AUDIO_IO_LOGD("The context is lost");
76 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
79 case PA_CONTEXT_UNCONNECTED:
80 case PA_CONTEXT_CONNECTING:
81 case PA_CONTEXT_AUTHORIZING:
82 case PA_CONTEXT_SETTING_NAME:
87 void CPulseAudioClient::__successContextCb(pa_context* c, int success, void* user_data) {
88 AUDIO_IO_LOGD("pa_context[%p], success[%d], user_data[%p]", c, success, user_data);
92 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
93 pClient->__mIsOperationSuccess = static_cast<bool>(success);
95 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
98 static bool __is_microphone_restricted(void) {
101 device_policy_manager_h dpm_h = NULL;
104 if ((dpm_h = dpm_manager_create())) {
105 /* state: 0(disallowed), 1(allowed) */
106 if ((ret = dpm_restriction_get_microphone_state(dpm_h, &state)))
107 AUDIO_IO_LOGE("Failed to dpm_restriction_get_microphone_state(), ret(0x%x)", ret);
108 dpm_manager_destroy(dpm_h);
109 AUDIO_IO_LOGD("microphone restriction state: %d(1:allowed, 0:disallowed)", state);
111 AUDIO_IO_LOGE("Failed to dpm_manager_create()");
114 return (state ? false : true);
117 void CPulseAudioClient::__streamStateChangeCb(pa_stream* s, void* user_data) {
121 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
123 switch (pa_stream_get_state(s)) {
124 case PA_STREAM_READY:
125 AUDIO_IO_LOGD("The stream is ready");
126 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
127 if (pClient->__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK)
128 pClient->__mIsFirstStream = true;
129 pClient->__mIsInit = true;
130 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
133 case PA_STREAM_FAILED:
134 AUDIO_IO_LOGD("The stream is failed");
135 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE,
136 __is_microphone_restricted());
137 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
140 case PA_STREAM_TERMINATED:
141 AUDIO_IO_LOGD("The stream is terminated");
142 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
143 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
146 case PA_STREAM_UNCONNECTED:
147 case PA_STREAM_CREATING:
152 void CPulseAudioClient::__streamCaptureCb(pa_stream* s, size_t length, void* user_data) {
156 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
157 assert(pClient->__mpListener);
158 assert(pClient->__mpMainloop);
160 if (pClient->__mIsUsedSyncRead == true) {
161 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
164 pClient->__mpListener->onStream(pClient, length);
167 #ifndef DISABLE_MOBILE_BACK_COMP
168 static void __dummy_write(pa_stream* s, size_t length) {
169 char* dummy = new char[length];
170 memset(dummy, 0, length);
171 pa_stream_write(s, dummy, length, NULL, 0LL, PA_SEEK_RELATIVE);
176 void CPulseAudioClient::__streamPlaybackCb(pa_stream* s, size_t length, void* user_data) {
180 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
181 assert(pClient->__mpListener);
183 #ifndef DISABLE_MOBILE_BACK_COMP
184 if (pClient->__mIsInit == false) {
185 AUDIO_IO_LOGD("Occurred this listener when an out stream is on the way to create : Write dummy, length[%d]", length);
186 __dummy_write(s, length);
189 if (pClient->isCorked()) {
190 AUDIO_IO_LOGD("Occurred this listener when an out stream is CORKED : Write dummy, length[%d]", length);
191 __dummy_write(s, length);
196 pClient->__mpListener->onStream(pClient, length);
198 /* If stream is not written in first callback during prepare,
199 then write dummy data to ensure the start */
200 if (pClient->__mIsFirstStream) {
201 AUDIO_IO_LOGD("Write dummy, length [%d]", length);
203 char* dummy = new char[length];
204 memset(dummy, 0, length);
205 pa_stream_write(s, dummy, length, NULL, 0LL, PA_SEEK_RELATIVE);
208 pClient->__mIsFirstStream = false;
212 void CPulseAudioClient::__streamLatencyUpdateCb(pa_stream* s, void* user_data) {
216 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
218 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
221 void CPulseAudioClient::__streamEventCb(pa_stream* s, const char *name, pa_proplist *pl, void *user_data) {
225 AUDIO_IO_LOGE("NAME : %s, Prop : %p", name, pl);
227 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
228 if (strcmp(name, PA_STREAM_EVENT_POP_TIMEOUT) == 0) {
229 pa_operation_unref(pa_stream_cork(pClient->__mpStream, 1, NULL, NULL));
234 void CPulseAudioClient::__successStreamCb(pa_stream* s, int success, void* user_data) {
235 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
239 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
240 pClient->__mIsOperationSuccess = static_cast<bool>(success);
242 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
245 void CPulseAudioClient::__successDrainCbInThread(pa_stream* s, int success, void* user_data) {
246 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
250 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
251 pClient->__mIsOperationSuccess = static_cast<bool>(success);
252 pClient->__mIsDraining = false;
255 void CPulseAudioClient::__successDrainCb(pa_stream* s, int success, void* user_data) {
256 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
260 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
261 pClient->__mIsOperationSuccess = static_cast<bool>(success);
262 pClient->__mIsDraining = false;
264 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
267 void CPulseAudioClient::initialize() {
268 if (__mIsInit == true) {
276 // Allocates PA proplist
277 __mpPropList = pa_proplist_new();
278 if (__mpPropList == NULL) {
279 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_proplist_new()");
282 // Adds values on proplist for delivery to PULSEAUDIO
283 char *streamType = NULL;
284 CAudioInfo::EAudioType audioType = __mSpec.getAudioInfo().getAudioType();
285 __mSpec.getAudioInfo().convertAudioType2StreamType(audioType, &streamType);
286 pa_proplist_sets(__mpPropList, PA_PROP_MEDIA_ROLE, streamType);
288 int index = __mSpec.getAudioInfo().getAudioIndex();
290 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_PARENT_ID, "%u", (unsigned int) index);
293 // Adds latency on proplist for delivery to PULSEAUDIO
294 AUDIO_IO_LOGD("LATENCY : %s[%d]", __mSpec.getStreamLatencyToString(), static_cast<int>(__mSpec.getStreamLatency()));
295 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_TIZEN_AUDIO_LATENCY, "%s", __mSpec.getStreamLatencyToString());
297 // Allocates PA mainloop
298 __mpMainloop = pa_threaded_mainloop_new();
299 if (__mpMainloop == NULL) {
300 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()");
303 // Allocates PA context
304 __mpContext = pa_context_new(pa_threaded_mainloop_get_api(__mpMainloop), CLIENT_NAME);
305 if (__mpContext == NULL) {
306 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()");
309 // Sets context state changed callback
310 pa_context_set_state_callback(__mpContext, __contextStateChangeCb, this);
312 // Connects this client with PA server
313 if (pa_context_connect(__mpContext, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) {
314 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_connect()");
317 // LOCK for synchronous connection
318 pa_threaded_mainloop_lock(__mpMainloop);
321 if (pa_threaded_mainloop_start(__mpMainloop) < 0) {
322 pa_threaded_mainloop_unlock(__mpMainloop);
323 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_threaded_mainloop_start()");
326 // Connection process is asynchronously
327 // So, this function will be waited when occurred context state change event
328 // If I got a signal, do next processing
330 pa_context_state_t state;
331 state = pa_context_get_state(__mpContext);
333 if (state == PA_CONTEXT_READY) {
337 if (!PA_CONTEXT_IS_GOOD(state)) {
338 err = pa_context_errno(__mpContext);
339 pa_threaded_mainloop_unlock(__mpMainloop);
340 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_context's state is not good : err[%d]", err);
343 /* Wait until the context is ready */
344 pa_threaded_mainloop_wait(__mpMainloop);
347 // Allocates PA stream
348 pa_sample_spec ss = __mSpec.getSampleSpec();
349 pa_channel_map map = __mSpec.getChannelMap();
351 __mpStream = pa_stream_new_with_proplist(__mpContext, __mSpec.getStreamName(), &ss, &map, __mpPropList);
352 if (__mpStream == NULL) {
353 pa_threaded_mainloop_unlock(__mpMainloop);
354 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_new_with_proplist()");
357 // Sets stream callbacks
358 pa_stream_set_state_callback(__mpStream, __streamStateChangeCb, this);
359 pa_stream_set_read_callback(__mpStream, __streamCaptureCb, this);
360 pa_stream_set_write_callback(__mpStream, __streamPlaybackCb, this);
361 pa_stream_set_latency_update_callback(__mpStream, __streamLatencyUpdateCb, this);
362 pa_stream_set_event_callback(__mpStream, __streamEventCb, this);
364 // Connect stream with PA Server
366 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
367 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
368 PA_STREAM_INTERPOLATE_TIMING |
369 PA_STREAM_ADJUST_LATENCY |
370 #ifndef DISABLE_MOBILE_BACK_COMP
371 PA_STREAM_START_CORKED |
373 PA_STREAM_AUTO_TIMING_UPDATE);
375 ret = pa_stream_connect_playback(__mpStream, NULL, NULL, flags, NULL, NULL);
377 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
378 PA_STREAM_INTERPOLATE_TIMING |
379 PA_STREAM_ADJUST_LATENCY |
380 #ifndef DISABLE_MOBILE_BACK_COMP
381 PA_STREAM_START_CORKED |
383 PA_STREAM_AUTO_TIMING_UPDATE);
385 ret = pa_stream_connect_record(__mpStream, NULL, NULL, flags);
389 err = pa_context_errno(__mpContext);
390 pa_threaded_mainloop_unlock(__mpMainloop);
391 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_connect() : err[%d]", err);
395 pa_stream_state_t state;
396 state = pa_stream_get_state(__mpStream);
398 if (state == PA_STREAM_READY) {
399 AUDIO_IO_LOGD("STREAM READY");
403 if (!PA_STREAM_IS_GOOD(state)) {
404 err = pa_context_errno(__mpContext);
405 pa_threaded_mainloop_unlock(__mpMainloop);
406 if (err == PA_ERR_ACCESS) {
407 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_DEVICE_POLICY_RESTRICTION, "pa_stream's state is not good : err[%d]", err);
409 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_stream's state is not good : err[%d]", err);
413 /* Wait until the stream is ready */
414 pa_threaded_mainloop_wait(__mpMainloop);
417 // End of synchronous
418 pa_threaded_mainloop_unlock(__mpMainloop);
420 // __mIsInit = true; // Moved to __streamStateChangeCb()
421 } catch (CAudioError& e) {
427 void CPulseAudioClient::finalize() {
429 if (__mIsInit == false) {
433 if (isInThread() == false)
434 pa_threaded_mainloop_lock(__mpMainloop);
436 /* clear callbacks */
437 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK)
438 pa_stream_set_write_callback(__mpStream, NULL, NULL);
440 pa_stream_set_read_callback(__mpStream, NULL, NULL);
441 pa_stream_set_latency_update_callback(__mpStream, NULL, NULL);
442 pa_stream_set_event_callback(__mpStream, NULL, NULL);
444 if (isInThread() == false)
445 pa_threaded_mainloop_unlock(__mpMainloop);
447 /* Wait for drain complete if draining before finalize */
449 unsigned int drain_wait_count = 0;
450 while (__mIsDraining && drain_wait_count++ < drain_wait_max_count) {
451 usleep(drain_wait_interval);
453 AUDIO_IO_LOGD("wait for drain complete!!!! [%d * %d usec]",
454 drain_wait_count, drain_wait_interval);
457 if (__mpMainloop != NULL) {
458 pa_threaded_mainloop_stop(__mpMainloop);
461 if (__mpStream != NULL) {
462 pa_stream_disconnect(__mpStream);
463 pa_stream_unref(__mpStream);
467 if (__mpContext != NULL) {
468 pa_context_disconnect(__mpContext);
469 pa_context_unref(__mpContext);
473 if (__mpMainloop != NULL) {
474 pa_threaded_mainloop_free(__mpMainloop);
478 if (__mpPropList != NULL) {
479 pa_proplist_free(__mpPropList);
486 int CPulseAudioClient::read(void* buffer, size_t length) {
487 if (__mIsInit == false) {
488 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
493 if (buffer == NULL) {
494 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p]", buffer);
497 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
498 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
501 size_t lengthIter = length;
504 __mIsUsedSyncRead = true;
507 pa_threaded_mainloop_lock(__mpMainloop);
509 while (lengthIter > 0) {
512 while (__mpSyncReadDataPtr == NULL) {
513 ret = pa_stream_peek(__mpStream, &__mpSyncReadDataPtr, &__mSyncReadLength);
515 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_peek() : ret[%d]", ret);
518 if (__mSyncReadLength <= 0) {
519 #ifdef _AUDIO_IO_DEBUG_TIMING_
520 AUDIO_IO_LOGD("readLength(%d byte) is not valid. wait...", __mSyncReadLength);
522 pa_threaded_mainloop_wait(__mpMainloop);
523 } else if (__mpSyncReadDataPtr == NULL) {
524 // Data peeked, but it doesn't have any data
525 ret = pa_stream_drop(__mpStream);
527 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
530 __mSyncReadIndex = 0;
534 if (__mSyncReadLength < lengthIter) {
535 l = __mSyncReadLength;
540 // Copy partial pcm data on out parameter
541 #ifdef _AUDIO_IO_DEBUG_TIMING_
542 AUDIO_IO_LOGD("memcpy() that a peeked buffer[0x%x], index[%d], length[%d] on out buffer", (const uint8_t*)(__mpSyncReadDataPtr) + __mSyncReadIndex, __mSyncReadIndex, l);
544 memcpy(buffer, (const uint8_t*)__mpSyncReadDataPtr + __mSyncReadIndex, l);
546 // Move next position
547 buffer = (uint8_t*)buffer + l;
550 // Adjusts the rest length
551 __mSyncReadIndex += l;
552 __mSyncReadLength -= l;
554 if (__mSyncReadLength == 0) {
555 #ifdef _AUDIO_IO_DEBUG_TIMING_
556 AUDIO_IO_LOGD("__mSyncReadLength is zero, do drop()");
558 ret = pa_stream_drop(__mpStream);
560 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
563 // Reset the internal pointer
564 __mpSyncReadDataPtr = NULL;
565 __mSyncReadLength = 0;
566 __mSyncReadIndex = 0;
568 } // End of while (lengthIter > 0)
570 pa_threaded_mainloop_unlock(__mpMainloop);
571 __mIsUsedSyncRead = false;
572 } catch (CAudioError& e) {
573 pa_threaded_mainloop_unlock(__mpMainloop);
574 __mIsUsedSyncRead = false;
581 int CPulseAudioClient::peek(const void** buffer, size_t* length) {
582 if (__mIsInit == false) {
583 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
588 if (buffer == NULL || length == NULL) {
589 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p], length[%p]", buffer, length);
592 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
593 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
598 if (isInThread() == false) {
599 pa_threaded_mainloop_lock(__mpMainloop);
600 ret = pa_stream_peek(__mpStream, buffer, length);
601 pa_threaded_mainloop_unlock(__mpMainloop);
603 ret = pa_stream_peek(__mpStream, buffer, length);
606 #ifdef _AUDIO_IO_DEBUG_TIMING_
607 AUDIO_IO_LOGD("buffer[%p], length[%d]", *buffer, *length);
611 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_peek() : err[%d]", ret);
617 int CPulseAudioClient::drop() {
618 if (__mIsInit == false) {
619 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
622 #ifdef _AUDIO_IO_DEBUG_TIMING_
628 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
629 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
634 if (isInThread() == false) {
635 pa_threaded_mainloop_lock(__mpMainloop);
636 ret = pa_stream_drop(__mpStream);
637 pa_threaded_mainloop_unlock(__mpMainloop);
639 ret = pa_stream_drop(__mpStream);
643 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_drop() : err[%d]", ret);
649 int CPulseAudioClient::write(const void* data, size_t length) {
651 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid");
654 if (__mDirection == EStreamDirection::STREAM_DIRECTION_RECORD) {
655 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
660 #ifdef _AUDIO_IO_DEBUG_TIMING_
661 AUDIO_IO_LOGD("data[%p], length[%d]", data, length);
663 if (pa_stream_is_corked(__mpStream)) {
664 AUDIO_IO_LOGW("stream is corked...do uncork here first!!!!");
665 pa_operation_unref(pa_stream_cork(__mpStream, 0, NULL, this));
668 if (isInThread() == false) {
669 pa_threaded_mainloop_lock(__mpMainloop);
670 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
671 pa_threaded_mainloop_unlock(__mpMainloop);
673 if (__mIsFirstStream) {
674 const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
675 uint32_t prebuf = attr->prebuf;
676 // Compensate amount of prebuf in first stream callback when audio-io use prebuf(-1)
677 // Because a stream will never start when an application wrote less than prebuf at first
678 if (length < prebuf) {
679 char* dummy = new char[prebuf - length];
680 memset(dummy, 0, prebuf - length);
681 pa_stream_write(__mpStream, dummy, prebuf - length, NULL, 0LL, PA_SEEK_RELATIVE);
684 __mIsFirstStream = false;
685 AUDIO_IO_LOGW("FIRST STREAM CALLBACK : length[%d], prebuf[%d], dummy[%d]",
686 length, prebuf, (length < prebuf) ? prebuf - length : 0);
688 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
692 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_write() : err[%d]", ret);
698 void CPulseAudioClient::cork(bool cork) {
699 AUDIO_IO_LOGD("cork[%d]", cork);
701 if (__mIsInit == false) {
702 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
705 if (isInThread() == true) {
706 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This operation is not supported in callback");
711 // Set __mIsFirstStream flag when uncork(resume) stream, because prebuf will be enable again
713 __mIsFirstStream = true;
715 if (isInThread() == false) {
716 pa_threaded_mainloop_lock(__mpMainloop);
717 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
718 pa_threaded_mainloop_unlock(__mpMainloop);
720 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
726 bool CPulseAudioClient::isCorked() {
727 if (__mIsInit == false) {
728 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
735 if (isInThread() == false) {
736 pa_threaded_mainloop_lock(__mpMainloop);
737 isCork = pa_stream_is_corked(__mpStream);
738 pa_threaded_mainloop_unlock(__mpMainloop);
740 isCork = pa_stream_is_corked(__mpStream);
743 #ifdef _AUDIO_IO_DEBUG_TIMING_
744 AUDIO_IO_LOGD("isCork[%d]", isCork);
746 return static_cast<bool>(isCork);
749 bool CPulseAudioClient::drain() {
750 if (__mIsInit == false) {
751 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
757 AUDIO_IO_LOGW("Corked...");
762 AUDIO_IO_LOGW("already draining...");
766 if (isInThread() == false) {
767 AUDIO_IO_LOGD("drain");
768 pa_threaded_mainloop_lock(__mpMainloop);
769 pa_operation* o = pa_stream_drain(__mpStream, __successDrainCb, this);
770 __mIsDraining = true;
771 while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
772 pa_threaded_mainloop_wait(__mpMainloop);
774 pa_operation_unref(o);
775 pa_threaded_mainloop_unlock(__mpMainloop);
777 AUDIO_IO_LOGD("drain in thread");
778 pa_operation_unref(pa_stream_drain(__mpStream, __successDrainCbInThread, this));
779 __mIsDraining = true;
785 bool CPulseAudioClient::flush() {
786 if (__mIsInit == false) {
787 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
792 if (isInThread() == false) {
793 AUDIO_IO_LOGD("flush");
794 pa_threaded_mainloop_lock(__mpMainloop);
795 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
796 pa_threaded_mainloop_unlock(__mpMainloop);
798 AUDIO_IO_LOGD("flush in thread");
799 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
805 size_t CPulseAudioClient::getWritableSize() {
806 if (__mIsInit == false) {
807 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
812 if (__mDirection != EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
813 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Playback");
818 if (isInThread() == false) {
819 pa_threaded_mainloop_lock(__mpMainloop);
820 ret = pa_stream_writable_size(__mpStream);
821 pa_threaded_mainloop_unlock(__mpMainloop);
823 ret = pa_stream_writable_size(__mpStream);
829 void CPulseAudioClient::checkRunningState() {
830 if (__mpContext == NULL || PA_CONTEXT_IS_GOOD(pa_context_get_state(__mpContext)) == 0) {
831 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] is not created or not good state", __mpContext);
833 if (__mpStream == NULL || PA_STREAM_IS_GOOD(pa_stream_get_state(__mpStream)) == 0) {
834 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The stream[%p] is not created or not good state", __mpStream);
836 if (pa_context_get_state(__mpContext) != PA_CONTEXT_READY || pa_stream_get_state(__mpStream) != PA_STREAM_READY) {
837 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] or stream[%p] state is not ready", __mpContext, __mpStream);
840 #ifdef _AUDIO_IO_DEBUG_TIMING_
841 AUDIO_IO_LOGD("This client is running");
845 bool CPulseAudioClient::isInThread() {
846 int ret = pa_threaded_mainloop_in_thread(__mpMainloop);
848 #ifdef _AUDIO_IO_DEBUG_TIMING_
849 AUDIO_IO_LOGD("isInThread[%d]", ret);
851 return static_cast<bool>(ret);
854 size_t CPulseAudioClient::getReadableSize() {
855 if (__mIsInit == false) {
856 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
861 if (__mDirection != EStreamDirection::STREAM_DIRECTION_RECORD) {
862 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Capture");
867 if (isInThread() == false) {
868 pa_threaded_mainloop_lock(__mpMainloop);
869 ret = pa_stream_writable_size(__mpStream);
870 pa_threaded_mainloop_unlock(__mpMainloop);
872 ret = pa_stream_writable_size(__mpStream);
878 size_t CPulseAudioClient::getBufferSize() {
879 if (__mIsInit == false) {
880 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
888 if (isInThread() == false) {
889 pa_threaded_mainloop_lock(__mpMainloop);
892 const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
894 int _err = pa_context_errno(__mpContext);
895 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_buffer_attr() : err[%d]", _err);
898 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
900 AUDIO_IO_LOGD("PLAYBACK buffer size[%d]", ret);
902 ret = attr->fragsize;
903 AUDIO_IO_LOGD("RECORD buffer size[%d]", ret);
905 } catch (CAudioError& e) {
906 if (isInThread() == false) {
907 pa_threaded_mainloop_unlock(__mpMainloop);
912 if (isInThread() == false) {
913 pa_threaded_mainloop_unlock(__mpMainloop);
919 pa_usec_t CPulseAudioClient::getLatency() {
920 if (__mIsInit == false) {
921 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
929 if (isInThread() == false) {
930 if (pa_stream_get_latency(__mpStream, &ret, &negative) < 0) {
931 int _err = pa_context_errno(__mpContext);
932 if (_err != PA_ERR_NODATA) {
933 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
936 return negative ? 0 : ret;
939 pa_threaded_mainloop_lock(__mpMainloop);
943 if (pa_stream_get_latency(__mpStream, &ret, &negative) >= 0) {
947 int _err = pa_context_errno(__mpContext);
948 if (_err != PA_ERR_NODATA) {
949 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
952 /* Wait until latency data is available again */
953 pa_threaded_mainloop_wait(__mpMainloop);
955 } catch (CAudioError& e) {
956 pa_threaded_mainloop_unlock(__mpMainloop);
960 pa_threaded_mainloop_unlock(__mpMainloop);
962 return negative ? 0 : ret;
965 pa_usec_t CPulseAudioClient::getFinalLatency() {
966 if (__mIsInit == false) {
967 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
976 if (isInThread() == false) {
977 pa_threaded_mainloop_lock(__mpMainloop);
980 ver = pa_context_get_server_protocol_version(__mpContext);
982 const pa_buffer_attr* buffer_attr = pa_stream_get_buffer_attr(__mpStream);
983 const pa_sample_spec* sample_spec = pa_stream_get_sample_spec(__mpStream);
984 const pa_timing_info* timing_info = pa_stream_get_timing_info(__mpStream);
986 if (buffer_attr == NULL || sample_spec == NULL || timing_info == NULL) {
987 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",
988 buffer_attr, sample_spec, timing_info);
991 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
992 ret = (pa_bytes_to_usec(buffer_attr->tlength, sample_spec) + timing_info->configured_sink_usec);
993 AUDIO_IO_LOGD("FINAL PLAYBACK LATENCY[%" PRIu64 "]", ret);
995 ret = (pa_bytes_to_usec(buffer_attr->fragsize, sample_spec) + timing_info->configured_source_usec);
996 AUDIO_IO_LOGD("FINAL RECORD LATENCY[%" PRIu64 "]", ret);
999 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED, "This version(ver.%d) is not supported", ver);
1002 if (isInThread() == false) {
1003 pa_threaded_mainloop_unlock(__mpMainloop);
1005 } catch (CAudioError& e) {
1006 if (isInThread() == false) {
1007 pa_threaded_mainloop_unlock(__mpMainloop);
1015 CPulseAudioClient::EStreamDirection CPulseAudioClient::getStreamDirection() {
1016 return __mDirection;
1019 CPulseStreamSpec CPulseAudioClient::getStreamSpec() {