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 if (mpPulseAudioClient->getStreamDirection() == CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
240 if (mpPulseAudioClient->drain() == false) {
241 AUDIO_IO_LOGE("Failed CPulseAudioClient::drain()");
244 mpPulseAudioClient->cork(true);
245 onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
249 // Focus watch callback doesn't have focus handle, but it need to convert & report to application for convenience
250 state = FOCUS_IS_RELEASED;
253 ///////////////////////////////////////
254 // Triggered by 'focus callback'
255 ///////////////////////////////////////
257 if (pHandler->getId() != id) {
258 AUDIO_IO_LOGW("Id is different, why? [mId : %d]", pHandler->getId());
261 if (session_option & MM_SESSION_OPTION_UNINTERRUPTIBLE) {
262 AUDIO_IO_LOGD("Session option is uninterruptible, skip...");
266 if (state == FOCUS_IS_RELEASED) {
267 // Focus handle(id) was released, do pause here
269 if (mpPulseAudioClient) {
270 if (mpPulseAudioClient->getStreamDirection() == CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
271 if (mpPulseAudioClient->drain() == false) {
272 AUDIO_IO_LOGE("Failed CPulseAudioClient::drain()");
275 mpPulseAudioClient->cork(true);
276 onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
279 } else if (state == FOCUS_IS_ACQUIRED) {
280 // Focus handle(id) was acquired again,
281 // check reason_for_change ("call-voice","call-video","voip","alarm","notification", ...)
282 // do resume here and call interrupt completed callback to application.
284 if (mpPulseAudioClient) {
285 mpPulseAudioClient->cork(false);
286 onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
292 if (mInterruptCallback.onInterrupt != NULL) {
293 IAudioSessionEventListener::EInterruptCode e = IAudioSessionEventListener::EInterruptCode::INTERRUPT_COMPLETED;
294 e = IAudioSessionEventListener::convertInterruptedCode(state, reason_for_change);
295 mInterruptCallback.onInterrupt(e, mInterruptCallback.mUserData);
299 void CAudioIO::onSignal(CAudioSessionHandler* pHandler, mm_sound_signal_name_t signal, int value) {
302 if (signal == MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS) {
303 if (value == 1 && pHandler->getSubscribeId() > 0) {
304 // Unregister focus watch callback & disable session handler
305 pHandler->disableSessionHandler();
306 AUDIO_IO_LOGD("Session handler disabled by signal");
307 } else if (value == 0) {
308 // Currently do nothing...
313 void CAudioIO::prepare() throw(CAudioError) {
314 if (__mIsInit == false) {
315 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
319 AUDIO_IO_LOGD("------> prepare done");
321 } catch (CAudioError e) {
326 void CAudioIO::unprepare() throw(CAudioError) {
327 if (__mIsInit == false) {
328 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
332 AUDIO_IO_LOGD("unprepare ----->");
334 } catch (CAudioError e) {
339 void CAudioIO::pause() throw(CAudioError) {
340 if (__mIsInit == false || IsReady() == false) {
341 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
346 AUDIO_IO_LOGD("pause");
347 mpPulseAudioClient->cork(true);
349 } catch (CAudioError e) {
355 void CAudioIO::resume() throw(CAudioError) {
356 if (__mIsInit == false || IsReady() == false) {
357 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
362 AUDIO_IO_LOGD("resume");
363 mpPulseAudioClient->cork(false);
365 } catch (CAudioError e) {
371 void CAudioIO::drain() throw(CAudioError) {
372 if (__mIsInit == false || IsReady() == false) {
373 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
377 if (mpPulseAudioClient->isInThread()) {
378 mpPulseAudioClient->drain();
381 mpPulseAudioClient->drain();
384 } catch (CAudioError e) {
385 if (!mpPulseAudioClient->isInThread()) {
392 void CAudioIO::flush() throw(CAudioError) {
393 if (__mIsInit == false || IsReady() == false) {
394 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
398 if (mpPulseAudioClient->isInThread()) {
399 mpPulseAudioClient->flush();
402 mpPulseAudioClient->flush();
405 } catch (CAudioError e) {
406 if (!mpPulseAudioClient->isInThread()) {
413 CAudioInfo& CAudioIO::getAudioInfo() throw(CAudioError) {
414 if (__mIsInit == false) {
415 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
421 void CAudioIO::setStreamCallback(SStreamCallback callback) throw(CAudioError) {
422 if (__mIsInit == false) {
423 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
426 mStreamCallback = callback;
429 CAudioIO::SStreamCallback CAudioIO::getStreamCallback() throw(CAudioError) {
430 if (__mIsInit == false) {
431 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
434 return mStreamCallback;
437 void CAudioIO::setStateChangedCallback(SStateChangedCallback callback) throw(CAudioError) {
438 if (__mIsInit == false) {
439 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
442 mStateChangedCallback = callback;
445 CAudioIO::SStateChangedCallback CAudioIO::getStateChangedCallback() throw(CAudioError) {
446 if (__mIsInit == false) {
447 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
450 return mStateChangedCallback;
453 void CAudioIO::setInterruptCallback(SInterruptCallback callback) throw(CAudioError) {
454 if (__mIsInit == false) {
455 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
458 mInterruptCallback = callback;
461 CAudioIO::SInterruptCallback CAudioIO::getInterruptCallback() throw(CAudioError) {
462 if (__mIsInit == false) {
463 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
466 return mInterruptCallback;
469 void CAudioIO::ignoreSession() throw(CAudioError) {
470 if (__mIsInit == false)
471 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
476 if (mpPulseAudioClient != NULL && mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING)
477 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "An Operation is not permitted while started");
479 bool isSkip = mpAudioSessionHandler->isSkipSession();
480 if (isSkip == false && mpAudioSessionHandler->getId() >= 0)
481 mpAudioSessionHandler->unregisterSound();
483 mpAudioSessionHandler->finalize();
484 __mForceIgnore = true;
487 } catch (CAudioError e) {
493 void CAudioIO::setStreamInfo(sound_stream_info_h stream_info) throw(CAudioError) {
494 if (stream_info == NULL)
495 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "stream_info is NULL");
497 if (__mIsInit == false)
498 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
501 if (mState != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE)
502 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "it is not permitted while started");
504 int errorCode = SOUND_MANAGER_ERROR_NONE;
505 CAudioInfo::EAudioType audioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
510 if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE)
511 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode);
514 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Input stream is not supported");
516 if ((errorCode = sound_manager_get_type_from_stream_information(stream_info, &type)) != SOUND_MANAGER_ERROR_NONE)
517 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode);
519 if (mDirection == CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN)
520 getAudioInfo().convertInputStreamType2AudioType(type, &audioType);
522 getAudioInfo().convertOutputStreamType2AudioType(type, &audioType);
523 getAudioInfo().setAudioType(audioType);
525 if ((errorCode = sound_manager_get_index_from_stream_information(stream_info, &index)) != SOUND_MANAGER_ERROR_NONE)
526 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode);
528 getAudioInfo().setAudioIndex(index);
530 AUDIO_IO_LOGD("stream info(%p) is set", stream_info);
531 } catch (CAudioError e) {
536 void CAudioIO::setInternalStreamInfo() throw(CAudioError) {
537 if (__mIsInit == false)
538 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
541 if (mState != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE)
542 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "it is not permitted while started");
544 if (mpAudioSessionHandler &&
545 mpAudioSessionHandler->getMultimediaSession() == MM_SESSION_TYPE_VOIP) {
546 sound_stream_info_h stream_info = NULL;
547 mpAudioSessionHandler->getInternalVoipStreamInfo(&stream_info);
548 AUDIO_IO_LOGD("get internal VOIP stream info(%p)", stream_info);
549 setStreamInfo(stream_info);
551 } catch (CAudioError e) {