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.
18 #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),
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:
88 void CPulseAudioClient::__successContextCb(pa_context* c, int success, void* user_data) {
89 AUDIO_IO_LOGD("pa_context[%p], success[%d], user_data[%p]", c, success, user_data);
93 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
94 pClient->__mIsOperationSuccess = static_cast<bool>(success);
96 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
99 static bool __is_microphone_restricted(void) {
102 device_policy_manager_h dpm_h = NULL;
105 if ((dpm_h = dpm_manager_create())) {
106 /* state: 0(disallowed), 1(allowed) */
107 if ((ret = dpm_restriction_get_microphone_state(dpm_h, &state)))
108 AUDIO_IO_LOGE("Failed to dpm_restriction_get_microphone_state(), ret(0x%x)", ret);
109 dpm_manager_destroy(dpm_h);
110 AUDIO_IO_LOGD("microphone restriction state: %d(1:allowed, 0:disallowed)", state);
112 AUDIO_IO_LOGE("Failed to dpm_manager_create()");
115 return (state ? false : true);
119 void CPulseAudioClient::__streamStateChangeCb(pa_stream* s, void* user_data) {
123 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
125 switch (pa_stream_get_state(s)) {
126 case PA_STREAM_READY:
127 AUDIO_IO_LOGD("The stream is ready");
128 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
129 if (pClient->__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK)
130 pClient->__mIsFirstStream = true;
131 pClient->__mIsInit = true;
132 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
135 case PA_STREAM_FAILED:
137 AUDIO_IO_LOGD("The stream is failed");
138 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE,
139 __is_microphone_restricted());
140 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
144 case PA_STREAM_TERMINATED:
145 AUDIO_IO_LOGD("The stream is terminated");
146 pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
147 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
150 case PA_STREAM_UNCONNECTED:
151 case PA_STREAM_CREATING:
156 void CPulseAudioClient::__streamCaptureCb(pa_stream* s, size_t length, void* user_data) {
160 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
161 assert(pClient->__mpListener);
162 assert(pClient->__mpMainloop);
164 if (pClient->__mIsUsedSyncRead == true) {
165 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
168 pClient->__mpListener->onStream(pClient, length);
171 #ifndef DISABLE_MOBILE_BACK_COMP
172 static void __dummy_write(pa_stream* s, size_t length) {
173 char* dummy = new char[length];
174 memset(dummy, 0, length);
175 pa_stream_write(s, dummy, length, NULL, 0LL, PA_SEEK_RELATIVE);
180 void CPulseAudioClient::__streamPlaybackCb(pa_stream* s, size_t length, void* user_data) {
184 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
185 assert(pClient->__mpListener);
187 #ifndef DISABLE_MOBILE_BACK_COMP
188 if (pClient->__mIsInit == false) {
189 AUDIO_IO_LOGD("Occurred this listener when an out stream is on the way to create : Write dummy, length[%zu]", length);
190 __dummy_write(s, length);
193 if (pClient->isCorked()) {
194 AUDIO_IO_LOGD("Occurred this listener when an out stream is CORKED : Write dummy, length[%zu]", length);
195 __dummy_write(s, length);
200 pClient->__mpListener->onStream(pClient, length);
202 #ifndef DISABLE_MOBILE_BACK_COMP
203 /* If stream is not written in first callback during prepare,
204 then write dummy data to ensure the start */
205 if (pClient->__mSpec.getStreamLatency() == CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_OUTPUT_DEFAULT_ASYNC &&
206 pClient->__mIsFirstStream) {
207 AUDIO_IO_LOGW("[async] Write dummy of length[%zu] since not written in first callback during prepare", length);
208 __dummy_write(s, length);
209 pClient->__mIsFirstStream = false;
214 void CPulseAudioClient::__streamLatencyUpdateCb(pa_stream* s, void* user_data) {
218 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
220 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
223 void CPulseAudioClient::__streamStartedCb(pa_stream* s, void* user_data) {
227 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
229 AUDIO_IO_LOGD("stream %p started.", pClient);
231 pClient->__mIsStarted = true;
234 void CPulseAudioClient::__streamUnderflowCb(pa_stream* s, void* user_data) {
238 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
240 AUDIO_IO_LOGD("stream %p UnderFlow...", pClient);
242 pClient->__mIsStarted = false;
246 void CPulseAudioClient::__streamEventCb(pa_stream* s, const char *name, pa_proplist *pl, void *user_data) {
250 AUDIO_IO_LOGE("NAME : %s, Prop : %p", name, pl);
252 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
253 if (strcmp(name, PA_STREAM_EVENT_POP_TIMEOUT) == 0) {
254 pa_operation_unref(pa_stream_cork(pClient->__mpStream, 1, NULL, NULL));
259 void CPulseAudioClient::__successStreamCb(pa_stream* s, int success, void* user_data) {
260 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
264 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
265 pClient->__mIsOperationSuccess = static_cast<bool>(success);
267 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
271 void CPulseAudioClient::__successDrainCbInThread(pa_stream* s, int success, void* user_data) {
272 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
276 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
277 pClient->__mIsOperationSuccess = static_cast<bool>(success);
278 pClient->__mIsDraining = false;
282 void CPulseAudioClient::__successDrainCb(pa_stream* s, int success, void* user_data) {
283 AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
287 CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
288 pClient->__mIsOperationSuccess = static_cast<bool>(success);
289 pClient->__mIsDraining = false;
291 pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
294 void CPulseAudioClient::initialize() {
295 if (__mIsInit == true) {
303 // Allocates PA proplist
304 __mpPropList = pa_proplist_new();
305 if (__mpPropList == NULL) {
306 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_proplist_new()"); //LCOV_EXCL_LINE
309 // Adds values on proplist for delivery to PULSEAUDIO
310 char *streamType = NULL;
311 CAudioInfo::EAudioType audioType = __mSpec.getAudioInfo().getAudioType();
312 __mSpec.getAudioInfo().convertAudioType2StreamType(audioType, &streamType);
313 pa_proplist_sets(__mpPropList, PA_PROP_MEDIA_ROLE, streamType);
315 int index = __mSpec.getAudioInfo().getAudioIndex();
317 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_PARENT_ID, "%u", (unsigned int) index);
320 // Adds latency on proplist for delivery to PULSEAUDIO
321 AUDIO_IO_LOGD("LATENCY : %s[%d]", __mSpec.getStreamLatencyToString(), static_cast<int>(__mSpec.getStreamLatency()));
322 pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_TIZEN_AUDIO_LATENCY, "%s", __mSpec.getStreamLatencyToString());
324 // Allocates PA mainloop
325 __mpMainloop = pa_threaded_mainloop_new();
326 if (__mpMainloop == NULL) {
327 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()"); //LCOV_EXCL_LINE
330 // Allocates PA context
331 __mpContext = pa_context_new(pa_threaded_mainloop_get_api(__mpMainloop), CLIENT_NAME);
332 if (__mpContext == NULL) {
333 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()"); //LCOV_EXCL_LINE
336 // Sets context state changed callback
337 pa_context_set_state_callback(__mpContext, __contextStateChangeCb, this);
339 // Connects this client with PA server
340 if (pa_context_connect(__mpContext, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) {
341 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_connect()"); //LCOV_EXCL_LINE
344 // LOCK for synchronous connection
345 pa_threaded_mainloop_lock(__mpMainloop);
348 if (pa_threaded_mainloop_start(__mpMainloop) < 0) {
349 pa_threaded_mainloop_unlock(__mpMainloop);
350 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_threaded_mainloop_start()"); //LCOV_EXCL_LINE
353 // Connection process is asynchronously
354 // So, this function will be waited when occurred context state change event
355 // If I got a signal, do next processing
357 pa_context_state_t state;
358 state = pa_context_get_state(__mpContext);
360 if (state == PA_CONTEXT_READY) {
364 if (!PA_CONTEXT_IS_GOOD(state)) {
366 err = pa_context_errno(__mpContext);
367 pa_threaded_mainloop_unlock(__mpMainloop);
368 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_context's state is not good : err[%d]", err);
372 /* Wait until the context is ready */
373 pa_threaded_mainloop_wait(__mpMainloop);
376 // Allocates PA stream
377 pa_sample_spec ss = __mSpec.getSampleSpec();
378 pa_channel_map map = __mSpec.getChannelMap();
380 __mpStream = pa_stream_new_with_proplist(__mpContext, __mSpec.getStreamName(), &ss, &map, __mpPropList);
381 if (__mpStream == NULL) {
383 pa_threaded_mainloop_unlock(__mpMainloop);
384 THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_new_with_proplist()");
388 // Sets stream callbacks
389 pa_stream_set_state_callback(__mpStream, __streamStateChangeCb, this);
390 pa_stream_set_read_callback(__mpStream, __streamCaptureCb, this);
391 pa_stream_set_write_callback(__mpStream, __streamPlaybackCb, this);
392 pa_stream_set_latency_update_callback(__mpStream, __streamLatencyUpdateCb, this);
393 pa_stream_set_event_callback(__mpStream, __streamEventCb, this);
394 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
395 pa_stream_set_started_callback(__mpStream, __streamStartedCb, this);
396 pa_stream_set_underflow_callback(__mpStream, __streamUnderflowCb, this);
399 // Connect stream with PA Server
401 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
402 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
403 PA_STREAM_INTERPOLATE_TIMING |
404 PA_STREAM_ADJUST_LATENCY |
405 #ifndef DISABLE_MOBILE_BACK_COMP
406 PA_STREAM_START_CORKED |
408 PA_STREAM_AUTO_TIMING_UPDATE);
410 ret = pa_stream_connect_playback(__mpStream, NULL, NULL, flags, NULL, NULL);
412 pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
413 PA_STREAM_INTERPOLATE_TIMING |
414 PA_STREAM_ADJUST_LATENCY |
415 #ifndef DISABLE_MOBILE_BACK_COMP
416 PA_STREAM_START_CORKED |
418 PA_STREAM_AUTO_TIMING_UPDATE);
420 ret = pa_stream_connect_record(__mpStream, NULL, NULL, flags);
425 err = pa_context_errno(__mpContext);
426 pa_threaded_mainloop_unlock(__mpMainloop);
427 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_connect() : err[%d]", err);
432 pa_stream_state_t state;
433 state = pa_stream_get_state(__mpStream);
435 if (state == PA_STREAM_READY) {
436 AUDIO_IO_LOGD("STREAM READY");
440 if (!PA_STREAM_IS_GOOD(state)) {
441 err = pa_context_errno(__mpContext);
442 pa_threaded_mainloop_unlock(__mpMainloop);
443 if (err == PA_ERR_ACCESS) {
444 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_DEVICE_POLICY_RESTRICTION, "pa_stream's state is not good : err[%d]", err);
446 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_stream's state is not good : err[%d]", err); //LCOV_EXCL_LINE
450 /* Wait until the stream is ready */
451 pa_threaded_mainloop_wait(__mpMainloop);
454 // End of synchronous
455 pa_threaded_mainloop_unlock(__mpMainloop);
457 // __mIsInit = true; // Moved to __streamStateChangeCb()
458 } catch (CAudioError& e) {
464 void CPulseAudioClient::finalize() {
467 if (__mpMainloop && isInThread() == false)
468 pa_threaded_mainloop_lock(__mpMainloop);
470 /* clear callbacks */
472 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK)
473 pa_stream_set_write_callback(__mpStream, NULL, NULL);
475 pa_stream_set_read_callback(__mpStream, NULL, NULL);
476 pa_stream_set_latency_update_callback(__mpStream, NULL, NULL);
477 pa_stream_set_event_callback(__mpStream, NULL, NULL);
480 if (__mpMainloop && isInThread() == false)
481 pa_threaded_mainloop_unlock(__mpMainloop);
483 /* Wait for drain complete if draining before finalize */
485 unsigned int drain_wait_count = 0;
486 while (__mIsDraining && drain_wait_count++ < drain_wait_max_count) {
487 usleep(drain_wait_interval);
489 AUDIO_IO_LOGD("wait for drain complete!!!! [%d * %d usec]",
490 drain_wait_count, drain_wait_interval);
493 if (__mpMainloop != NULL) {
494 pa_threaded_mainloop_stop(__mpMainloop);
497 if (__mpStream != NULL) {
498 pa_stream_disconnect(__mpStream);
499 pa_stream_unref(__mpStream);
503 if (__mpContext != NULL) {
504 pa_context_disconnect(__mpContext);
505 pa_context_unref(__mpContext);
509 if (__mpMainloop != NULL) {
510 pa_threaded_mainloop_free(__mpMainloop);
514 if (__mpPropList != NULL) {
515 pa_proplist_free(__mpPropList);
522 int CPulseAudioClient::read(void* buffer, size_t length) {
523 if (__mIsInit == false) {
524 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
529 if (buffer == NULL) {
530 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p]", buffer);
533 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
534 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
537 size_t lengthIter = length;
540 __mIsUsedSyncRead = true;
543 pa_threaded_mainloop_lock(__mpMainloop);
545 while (lengthIter > 0) {
548 while (__mpSyncReadDataPtr == NULL) {
549 ret = pa_stream_peek(__mpStream, &__mpSyncReadDataPtr, &__mSyncReadLength);
551 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_peek() : ret[%d]", ret);
554 if (__mSyncReadLength <= 0) {
555 #ifdef _AUDIO_IO_DEBUG_TIMING_
556 AUDIO_IO_LOGD("readLength(%zu byte) is not valid. wait...", __mSyncReadLength);
558 pa_threaded_mainloop_wait(__mpMainloop);
559 } else if (__mpSyncReadDataPtr == NULL) {
560 // Data peeked, but it doesn't have any data
561 ret = pa_stream_drop(__mpStream);
563 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret); //LCOV_EXCL_LINE
566 __mSyncReadIndex = 0;
570 if (__mSyncReadLength < lengthIter) {
571 l = __mSyncReadLength;
576 // Copy partial pcm data on out parameter
577 #ifdef _AUDIO_IO_DEBUG_TIMING_
578 AUDIO_IO_LOGD("memcpy() that a peeked buffer[%p], index[%zu], length[%zu] on out buffer", (const uint8_t*)(__mpSyncReadDataPtr) + __mSyncReadIndex, __mSyncReadIndex, l);
580 memcpy(buffer, (const uint8_t*)__mpSyncReadDataPtr + __mSyncReadIndex, l);
582 // Move next position
583 buffer = (uint8_t*)buffer + l;
586 // Adjusts the rest length
587 __mSyncReadIndex += l;
588 __mSyncReadLength -= l;
590 if (__mSyncReadLength == 0) {
591 #ifdef _AUDIO_IO_DEBUG_TIMING_
592 AUDIO_IO_LOGD("__mSyncReadLength is zero, do drop()");
594 ret = pa_stream_drop(__mpStream);
596 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret); //LCOV_EXCL_LINE
599 // Reset the internal pointer
600 __mpSyncReadDataPtr = NULL;
601 __mSyncReadLength = 0;
602 __mSyncReadIndex = 0;
604 } // End of while (lengthIter > 0)
606 pa_threaded_mainloop_unlock(__mpMainloop);
607 __mIsUsedSyncRead = false;
608 } catch (CAudioError& e) {
609 pa_threaded_mainloop_unlock(__mpMainloop);
610 __mIsUsedSyncRead = false;
617 int CPulseAudioClient::peek(const void** buffer, size_t* length) {
618 if (__mIsInit == false) {
619 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
624 if (buffer == NULL || length == NULL) {
625 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p], length[%p]", buffer, length);
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_peek(__mpStream, buffer, length);
637 pa_threaded_mainloop_unlock(__mpMainloop);
639 ret = pa_stream_peek(__mpStream, buffer, length);
642 #ifdef _AUDIO_IO_DEBUG_TIMING_
643 AUDIO_IO_LOGD("buffer[%p], length[%zu]", *buffer, *length);
647 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_peek() : err[%d]", ret); //LCOV_EXCL_LINE
653 int CPulseAudioClient::drop() {
654 if (__mIsInit == false) {
655 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
658 #ifdef _AUDIO_IO_DEBUG_TIMING_
664 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
665 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
670 if (isInThread() == false) {
671 pa_threaded_mainloop_lock(__mpMainloop);
672 ret = pa_stream_drop(__mpStream);
673 pa_threaded_mainloop_unlock(__mpMainloop);
675 ret = pa_stream_drop(__mpStream);
679 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_drop() : err[%d]", ret); //LCOV_EXCL_LINE
685 int CPulseAudioClient::write(const void* data, size_t length) {
687 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid");
690 if (__mDirection == EStreamDirection::STREAM_DIRECTION_RECORD) {
691 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
696 #ifdef _AUDIO_IO_DEBUG_TIMING_
697 AUDIO_IO_LOGD("data[%p], length[%zu], First[%d]", data, length, __mIsFirstStream);
700 if (isInThread() == false) {
701 pa_threaded_mainloop_lock(__mpMainloop);
702 if (pa_stream_is_corked(__mpStream)) {
703 AUDIO_IO_LOGW("stream is corked...do uncork here first!!!!");
704 pa_operation_unref(pa_stream_cork(__mpStream, 0, NULL, this));
707 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
708 pa_threaded_mainloop_unlock(__mpMainloop);
710 if (pa_stream_is_corked(__mpStream)) {
711 AUDIO_IO_LOGW("stream is corked...do uncork here first!!!!");
712 pa_operation_unref(pa_stream_cork(__mpStream, 0, NULL, this));
715 if (__mIsFirstStream) {
716 const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
717 uint32_t prebuf = attr->prebuf;
718 // Compensate amount of prebuf in first stream callback when audio-io use prebuf(-1)
719 // Because a stream will never start when an application wrote less than prebuf at first
720 if (length < prebuf) {
721 char* dummy = new char[prebuf - length];
722 memset(dummy, 0, prebuf - length);
723 pa_stream_write(__mpStream, dummy, prebuf - length, NULL, 0LL, PA_SEEK_RELATIVE);
726 __mIsFirstStream = false;
727 AUDIO_IO_LOGW("FIRST STREAM CALLBACK : length[%zu], prebuf[%d], dummy[%zu]",
728 length, prebuf, (length < prebuf) ? prebuf - length : 0);
730 ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
734 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_write() : err[%d]", ret); //LCOV_EXCL_LINE
740 void CPulseAudioClient::cork(bool cork) {
741 AUDIO_IO_LOGD("cork[%d]", cork);
743 if (__mIsInit == false) {
744 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
747 if (isInThread() == true) {
748 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This operation is not supported in callback");
753 // Set __mIsFirstStream flag when uncork(resume) stream, because prebuf will be enable again
755 __mIsFirstStream = true;
757 if (isInThread() == false) {
758 pa_threaded_mainloop_lock(__mpMainloop);
759 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
760 pa_threaded_mainloop_unlock(__mpMainloop);
762 pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
768 bool CPulseAudioClient::isCorked() {
769 if (__mIsInit == false) {
770 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
777 if (isInThread() == false) {
778 pa_threaded_mainloop_lock(__mpMainloop);
779 isCork = pa_stream_is_corked(__mpStream);
780 pa_threaded_mainloop_unlock(__mpMainloop);
782 isCork = pa_stream_is_corked(__mpStream);
785 #ifdef _AUDIO_IO_DEBUG_TIMING_
786 AUDIO_IO_LOGD("isCork[%d]", isCork);
788 return static_cast<bool>(isCork);
791 bool CPulseAudioClient::drain() {
792 if (__mIsInit == false) {
793 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
799 AUDIO_IO_LOGW("Corked...");
804 AUDIO_IO_LOGW("already draining...");
809 AUDIO_IO_LOGW("stream not started yet...skip drain");
813 if (isInThread() == false) {
814 AUDIO_IO_LOGD("drain");
815 pa_threaded_mainloop_lock(__mpMainloop);
816 pa_operation* o = pa_stream_drain(__mpStream, __successDrainCb, this);
817 __mIsDraining = true;
818 while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
819 pa_threaded_mainloop_wait(__mpMainloop);
821 pa_operation_unref(o);
822 pa_threaded_mainloop_unlock(__mpMainloop);
823 AUDIO_IO_LOGD("drain done");
825 AUDIO_IO_LOGD("drain in thread");
826 pa_operation_unref(pa_stream_drain(__mpStream, __successDrainCbInThread, this));
827 __mIsDraining = true;
833 bool CPulseAudioClient::flush() {
834 if (__mIsInit == false) {
835 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
840 if (isInThread() == false) {
841 AUDIO_IO_LOGD("flush");
842 pa_threaded_mainloop_lock(__mpMainloop);
843 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
844 pa_threaded_mainloop_unlock(__mpMainloop);
846 AUDIO_IO_LOGD("flush in thread");
847 pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
853 size_t CPulseAudioClient::getWritableSize() {
854 if (__mIsInit == false) {
855 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
860 if (__mDirection != EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
861 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Playback");
866 if (isInThread() == false) {
867 pa_threaded_mainloop_lock(__mpMainloop);
868 ret = pa_stream_writable_size(__mpStream);
869 pa_threaded_mainloop_unlock(__mpMainloop);
871 ret = pa_stream_writable_size(__mpStream);
877 void CPulseAudioClient::checkRunningState() {
878 if (__mpContext == NULL || PA_CONTEXT_IS_GOOD(pa_context_get_state(__mpContext)) == 0) {
879 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] is not created or not good state", __mpContext);
881 if (__mpStream == NULL || PA_STREAM_IS_GOOD(pa_stream_get_state(__mpStream)) == 0) {
882 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The stream[%p] is not created or not good state", __mpStream);
884 if (pa_context_get_state(__mpContext) != PA_CONTEXT_READY || pa_stream_get_state(__mpStream) != PA_STREAM_READY) {
885 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] or stream[%p] state is not ready", __mpContext, __mpStream);
888 #ifdef _AUDIO_IO_DEBUG_TIMING_
889 AUDIO_IO_LOGD("This client is running");
893 bool CPulseAudioClient::isInThread() {
894 int ret = pa_threaded_mainloop_in_thread(__mpMainloop);
896 #ifdef _AUDIO_IO_DEBUG_TIMING_
897 AUDIO_IO_LOGD("isInThread[%d]", ret);
899 return static_cast<bool>(ret);
903 size_t CPulseAudioClient::getReadableSize() {
904 if (__mIsInit == false) {
905 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
910 if (__mDirection != EStreamDirection::STREAM_DIRECTION_RECORD) {
911 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Capture");
916 if (isInThread() == false) {
917 pa_threaded_mainloop_lock(__mpMainloop);
918 ret = pa_stream_writable_size(__mpStream);
919 pa_threaded_mainloop_unlock(__mpMainloop);
921 ret = pa_stream_writable_size(__mpStream);
927 size_t CPulseAudioClient::getBufferSize() {
928 if (__mIsInit == false) {
929 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
937 if (isInThread() == false) {
938 pa_threaded_mainloop_lock(__mpMainloop);
941 const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
943 int _err = pa_context_errno(__mpContext);
944 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_buffer_attr() : err[%d]", _err);
947 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
949 AUDIO_IO_LOGD("PLAYBACK buffer size[%zu]", ret);
951 ret = attr->fragsize;
952 AUDIO_IO_LOGD("RECORD buffer size[%zu]", ret);
954 } catch (CAudioError& e) {
955 if (isInThread() == false) {
956 pa_threaded_mainloop_unlock(__mpMainloop);
961 if (isInThread() == false) {
962 pa_threaded_mainloop_unlock(__mpMainloop);
968 pa_usec_t CPulseAudioClient::getLatency() {
969 if (__mIsInit == false) {
970 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
978 if (isInThread() == false) {
979 if (pa_stream_get_latency(__mpStream, &ret, &negative) < 0) {
980 int _err = pa_context_errno(__mpContext);
981 if (_err != PA_ERR_NODATA) {
982 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
985 return negative ? 0 : ret;
988 pa_threaded_mainloop_lock(__mpMainloop);
992 if (pa_stream_get_latency(__mpStream, &ret, &negative) >= 0) {
996 int _err = pa_context_errno(__mpContext);
997 if (_err != PA_ERR_NODATA) {
998 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
1001 /* Wait until latency data is available again */
1002 pa_threaded_mainloop_wait(__mpMainloop);
1004 } catch (CAudioError& e) {
1005 pa_threaded_mainloop_unlock(__mpMainloop);
1009 pa_threaded_mainloop_unlock(__mpMainloop);
1011 return negative ? 0 : ret;
1014 pa_usec_t CPulseAudioClient::getFinalLatency() {
1015 if (__mIsInit == false) {
1016 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
1019 checkRunningState();
1025 if (isInThread() == false) {
1026 pa_threaded_mainloop_lock(__mpMainloop);
1029 ver = pa_context_get_server_protocol_version(__mpContext);
1031 const pa_buffer_attr* buffer_attr = pa_stream_get_buffer_attr(__mpStream);
1032 const pa_sample_spec* sample_spec = pa_stream_get_sample_spec(__mpStream);
1033 const pa_timing_info* timing_info = pa_stream_get_timing_info(__mpStream);
1035 if (buffer_attr == NULL || sample_spec == NULL || timing_info == NULL) {
1036 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",
1037 buffer_attr, sample_spec, timing_info);
1040 if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
1041 ret = (pa_bytes_to_usec(buffer_attr->tlength, sample_spec) + timing_info->configured_sink_usec);
1042 AUDIO_IO_LOGD("FINAL PLAYBACK LATENCY[%" PRIu64 "]", ret);
1044 ret = (pa_bytes_to_usec(buffer_attr->fragsize, sample_spec) + timing_info->configured_source_usec);
1045 AUDIO_IO_LOGD("FINAL RECORD LATENCY[%" PRIu64 "]", ret);
1048 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED, "This version(ver.%d) is not supported", ver);
1051 if (isInThread() == false) {
1052 pa_threaded_mainloop_unlock(__mpMainloop);
1054 } catch (CAudioError& e) {
1055 if (isInThread() == false) {
1056 pa_threaded_mainloop_unlock(__mpMainloop);
1064 CPulseAudioClient::EStreamDirection CPulseAudioClient::getStreamDirection() {
1065 return __mDirection;
1068 CPulseStreamSpec CPulseAudioClient::getStreamSpec() {