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.
21 #include "CAudioIODef.h"
24 using namespace tizen_media_audio;
30 CAudioIO::CAudioIO() :
31 mpAudioSessionHandler(NULL),
32 mpPulseAudioClient(NULL),
33 __mMutex(PTHREAD_MUTEX_INITIALIZER),
34 __mCond(PTHREAD_COND_INITIALIZER),
36 __mForceIgnore(false) {
37 mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_MAX;
38 mState = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
39 mStatePrev = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
43 CAudioIO::CAudioIO(CAudioInfo& audioInfo) :
44 mpAudioSessionHandler(NULL),
45 mpPulseAudioClient(NULL),
46 __mMutex(PTHREAD_MUTEX_INITIALIZER),
47 __mCond(PTHREAD_COND_INITIALIZER),
49 __mForceIgnore(false) {
50 mAudioInfo = audioInfo;
51 mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_MAX;
52 mState = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
53 mStatePrev = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
57 CAudioIO::~CAudioIO() {
60 void CAudioIO::setInit(bool flag) {
64 bool CAudioIO::isInit() {
68 bool CAudioIO::IsReady() {
69 return ((mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING || mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED)? true : false);
72 void CAudioIO::internalLock() throw(CAudioError) {
73 if (__mIsInit == false) {
74 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
77 if (pthread_mutex_lock(&__mMutex) != 0) {
78 THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_lock()");
80 #ifdef _AUDIO_IO_DEBUG_TIMING_
81 AUDIO_IO_LOGD(COLOR_RED "LOCK" COLOR_END);
85 void CAudioIO::internalUnlock() throw(CAudioError) {
86 if (__mIsInit == false) {
87 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
90 if (pthread_mutex_unlock(&__mMutex) != 0) {
91 THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_lock()");
93 #ifdef _AUDIO_IO_DEBUG_TIMING_
94 AUDIO_IO_LOGD(COLOR_GREEN "UNLOCK" COLOR_END);
98 void CAudioIO::internalWait() throw(CAudioError) {
99 if (__mIsInit == false) {
100 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
103 #ifdef _AUDIO_IO_DEBUG_TIMING_
104 AUDIO_IO_LOGD(COLOR_RED "WAIT" COLOR_END);
107 pthread_cond_wait(&__mCond, &__mMutex);
110 void CAudioIO::internalSignal() throw(CAudioError) {
111 if (__mIsInit == false) {
112 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
115 #ifdef _AUDIO_IO_DEBUG_TIMING_
116 AUDIO_IO_LOGD(COLOR_GREEN "SIGNAL" COLOR_END);
119 pthread_cond_signal(&__mCond);
122 bool CAudioIO::isForceIgnore() {
123 return __mForceIgnore;
126 void CAudioIO::initialize() throw(CAudioError) {
127 if (__mIsInit == true) {
131 AUDIO_IO_LOGD("initialize");
133 int ret = pthread_mutex_init(&__mMutex, NULL);
135 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pthread_mutex_init()");
138 ret = pthread_cond_init(&__mCond, NULL);
140 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pthread_cond_init()");
146 void CAudioIO::finalize() {
147 if (__mIsInit == false) {
151 AUDIO_IO_LOGD("finalize");
153 int ret = pthread_mutex_destroy(&__mMutex);
155 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pthread_mutex_destroy() ret:%d", ret);
158 ret = pthread_cond_destroy(&__mCond);
160 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pthread_cond_destroy() ret:%d", ret);
166 void CAudioIO::onStream(CPulseAudioClient* pClient, size_t length) {
167 assert(__mIsInit == true);
168 assert(pClient != NULL);
171 #ifdef _AUDIO_IO_DEBUG_TIMING_
172 AUDIO_IO_LOGD("mStreamCallback.onStream(%p), pClient(%p), length(%zu)", mStreamCallback.onStream, pClient, length);
175 if (mStreamCallback.onStream != NULL) {
176 mStreamCallback.onStream(length, mStreamCallback.mUserData);
180 void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state, bool byPolicy) {
181 assert(__mIsInit == true);
182 assert(state >= CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE && state < CAudioInfo::EAudioIOState::AUDIO_IO_STATE_MAX);
186 mByPolicy = byPolicy;
188 if (mState == mStatePrev)
191 const char* state_string[] = { "NONE", "IDLE", "RUNNING", "PAUSED" };
193 AUDIO_IO_LOGD("previous(%s,%d) ===> current(%s,%d), by_policy(%d)",
194 state_string[(int)mStatePrev], mStatePrev, state_string[(int)mState], mState, mByPolicy);
196 if (mStateChangedCallback.onStateChanged != NULL) {
197 mStateChangedCallback.onStateChanged(mState, mStatePrev, mByPolicy, mStateChangedCallback.mUserData);
201 void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state) {
202 onStateChanged(state, false);
205 CAudioInfo::EAudioIOState CAudioIO::getState() {
209 void CAudioIO::onInterrupt(CAudioSessionHandler* pHandler, int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state, const char *reason_for_change, const char *additional_info) {
212 int session_option = pHandler->getOptions();
215 ///////////////////////////////////////
216 // Triggered by 'focus watch callback'
217 ///////////////////////////////////////
219 if (session_option & (MM_SESSION_OPTION_PAUSE_OTHERS | MM_SESSION_OPTION_UNINTERRUPTIBLE)) {
220 AUDIO_IO_LOGD("Session option is pausing others or uninterruptible, skip...");
224 if (state == FOCUS_IS_RELEASED) {
225 // Focus handle(id) of the other application was released, do resume if possible
227 if (mpPulseAudioClient) {
228 mpPulseAudioClient->cork(false);
229 onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
233 // Focus watch callback doesn't have focus handle, but it need to convert & report to application for convenience
234 state = FOCUS_IS_ACQUIRED;
235 } else if (state == FOCUS_IS_ACQUIRED) {
236 // Focus handle(id) of the other application was acquired, do pause if possible
238 if (mpPulseAudioClient) {
239 /* FIXME: Skip this codes due to the blocking of drain() function
240 if (mpPulseAudioClient->getStreamDirection() == CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
241 if (mpPulseAudioClient->drain() == false) {
242 AUDIO_IO_LOGE("Failed CPulseAudioClient::drain()");
246 mpPulseAudioClient->cork(true);
247 onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
251 // Focus watch callback doesn't have focus handle, but it need to convert & report to application for convenience
252 state = FOCUS_IS_RELEASED;
255 ///////////////////////////////////////
256 // Triggered by 'focus callback'
257 ///////////////////////////////////////
259 if (pHandler->getId() != id) {
260 AUDIO_IO_LOGW("Id is different, why? [mId : %d]", pHandler->getId());
263 if (session_option & MM_SESSION_OPTION_UNINTERRUPTIBLE) {
264 AUDIO_IO_LOGD("Session option is uninterruptible, skip...");
268 if (state == FOCUS_IS_RELEASED) {
269 // Focus handle(id) was released, do pause here
271 if (mpPulseAudioClient) {
272 /* FIXME: Skip this codes due to the blocking of drain() function
273 if (mpPulseAudioClient->getStreamDirection() == CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
274 if (mpPulseAudioClient->drain() == false) {
275 AUDIO_IO_LOGE("Failed CPulseAudioClient::drain()");
279 mpPulseAudioClient->cork(true);
280 onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
283 } else if (state == FOCUS_IS_ACQUIRED) {
284 // Focus handle(id) was acquired again,
285 // check reason_for_change ("call-voice","call-video","voip","alarm","notification", ...)
286 // do resume here and call interrupt completed callback to application.
288 if (mpPulseAudioClient) {
289 mpPulseAudioClient->cork(false);
290 onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
296 if (mInterruptCallback.onInterrupt != NULL) {
297 IAudioSessionEventListener::EInterruptCode e = IAudioSessionEventListener::EInterruptCode::INTERRUPT_COMPLETED;
298 e = IAudioSessionEventListener::convertInterruptedCode(state, reason_for_change);
299 mInterruptCallback.onInterrupt(e, mInterruptCallback.mUserData);
303 void CAudioIO::onSignal(CAudioSessionHandler* pHandler, mm_sound_signal_name_t signal, int value) {
306 if (signal == MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS) {
307 if (value == 1 && pHandler->getSubscribeId() > 0) {
308 // Unregister focus watch callback & disable session handler
309 pHandler->disableSessionHandler();
310 AUDIO_IO_LOGD("Session handler disabled by signal");
311 } else if (value == 0) {
312 // Currently do nothing...
317 void CAudioIO::prepare() throw(CAudioError) {
318 if (__mIsInit == false) {
319 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
323 AUDIO_IO_LOGD("------> prepare done");
325 } catch (CAudioError e) {
330 void CAudioIO::unprepare() throw(CAudioError) {
331 if (__mIsInit == false) {
332 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
336 AUDIO_IO_LOGD("unprepare ----->");
338 } catch (CAudioError e) {
343 void CAudioIO::pause() throw(CAudioError) {
344 if (__mIsInit == false || IsReady() == false) {
345 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
350 AUDIO_IO_LOGD("pause");
351 mpPulseAudioClient->cork(true);
353 } catch (CAudioError e) {
359 void CAudioIO::resume() throw(CAudioError) {
360 if (__mIsInit == false || IsReady() == false) {
361 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
366 AUDIO_IO_LOGD("resume");
367 mpPulseAudioClient->cork(false);
369 } catch (CAudioError e) {
375 void CAudioIO::drain() throw(CAudioError) {
376 if (__mIsInit == false || IsReady() == false) {
377 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
381 if (mpPulseAudioClient->isInThread()) {
382 mpPulseAudioClient->drain();
385 mpPulseAudioClient->drain();
388 } catch (CAudioError e) {
389 if (!mpPulseAudioClient->isInThread()) {
396 void CAudioIO::flush() throw(CAudioError) {
397 if (__mIsInit == false || IsReady() == false) {
398 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
402 if (mpPulseAudioClient->isInThread()) {
403 mpPulseAudioClient->flush();
406 mpPulseAudioClient->flush();
409 } catch (CAudioError e) {
410 if (!mpPulseAudioClient->isInThread()) {
417 CAudioInfo& CAudioIO::getAudioInfo() throw(CAudioError) {
418 if (__mIsInit == false) {
419 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
425 void CAudioIO::setStreamCallback(SStreamCallback callback) throw(CAudioError) {
426 if (__mIsInit == false) {
427 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
430 mStreamCallback = callback;
433 CAudioIO::SStreamCallback CAudioIO::getStreamCallback() throw(CAudioError) {
434 if (__mIsInit == false) {
435 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
438 return mStreamCallback;
441 void CAudioIO::setStateChangedCallback(SStateChangedCallback callback) throw(CAudioError) {
442 if (__mIsInit == false) {
443 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
446 mStateChangedCallback = callback;
449 CAudioIO::SStateChangedCallback CAudioIO::getStateChangedCallback() throw(CAudioError) {
450 if (__mIsInit == false) {
451 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
454 return mStateChangedCallback;
457 void CAudioIO::setInterruptCallback(SInterruptCallback callback) throw(CAudioError) {
458 if (__mIsInit == false) {
459 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
462 mInterruptCallback = callback;
465 CAudioIO::SInterruptCallback CAudioIO::getInterruptCallback() throw(CAudioError) {
466 if (__mIsInit == false) {
467 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
470 return mInterruptCallback;
473 void CAudioIO::ignoreSession() throw(CAudioError) {
474 if (__mIsInit == false)
475 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
480 if (mpPulseAudioClient != NULL && mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING)
481 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "An Operation is not permitted while started");
483 abandonInternalFocus();
486 } catch (CAudioError e) {
492 void CAudioIO::setStreamInfo(sound_stream_info_h stream_info) throw(CAudioError) {
493 if (stream_info == NULL)
494 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "stream_info is NULL");
496 if (__mIsInit == false)
497 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
500 if (mState != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE)
501 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "it is not permitted while started");
503 abandonInternalFocus();
505 int errorCode = SOUND_MANAGER_ERROR_NONE;
506 CAudioInfo::EAudioType audioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
511 if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE)
512 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode);
515 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Input stream is not supported");
517 if ((errorCode = sound_manager_get_type_from_stream_information(stream_info, &type)) != SOUND_MANAGER_ERROR_NONE)
518 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode);
520 if (mDirection == CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN)
521 getAudioInfo().convertInputStreamType2AudioType(type, &audioType);
523 getAudioInfo().convertOutputStreamType2AudioType(type, &audioType);
524 getAudioInfo().setAudioType(audioType);
526 if ((errorCode = sound_manager_get_index_from_stream_information(stream_info, &index)) != SOUND_MANAGER_ERROR_NONE)
527 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode);
529 getAudioInfo().setAudioIndex(index);
531 AUDIO_IO_LOGD("stream info(%p) is set", stream_info);
532 } catch (CAudioError e) {
537 void CAudioIO::setInternalStreamInfo() throw(CAudioError) {
538 if (__mIsInit == false)
539 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
542 if (mState != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE)
543 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "it is not permitted while started");
545 if (mpAudioSessionHandler &&
546 mpAudioSessionHandler->getMultimediaSession() == MM_SESSION_TYPE_VOIP) {
547 sound_stream_info_h stream_info = NULL;
548 mpAudioSessionHandler->getInternalVoipStreamInfo(&stream_info);
549 AUDIO_IO_LOGD("get internal VOIP stream info(%p)", stream_info);
550 setStreamInfo(stream_info);
552 } catch (CAudioError e) {
557 void CAudioIO::abandonInternalFocus() throw(CAudioError) {
558 bool isSkip = mpAudioSessionHandler->isSkipSession();
559 int id = mpAudioSessionHandler->getId();
562 if (isSkip == false && id >= 0)
563 mpAudioSessionHandler->unregisterSound();
565 mpAudioSessionHandler->finalize();
566 __mForceIgnore = true;
567 } catch (CAudioError e) {