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"
23 #define AUDIO_IO_DEBUG
26 using namespace tizen_media_audio;
32 CAudioIO::CAudioIO() :
33 mpAudioSessionHandler(NULL),
34 mpPulseAudioClient(NULL),
35 __mMutex(PTHREAD_MUTEX_INITIALIZER),
36 __mCond(PTHREAD_COND_INITIALIZER),
38 __mForceIgnore(false) {
39 mState = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
40 mStatePrev = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
44 CAudioIO::CAudioIO(CAudioInfo& audioInfo) :
45 mpAudioSessionHandler(NULL),
46 mpPulseAudioClient(NULL),
47 __mMutex(PTHREAD_MUTEX_INITIALIZER),
48 __mCond(PTHREAD_COND_INITIALIZER),
50 __mForceIgnore(false) {
51 mAudioInfo = audioInfo;
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 AUDIO_IO_LOGD("current(%d), previous(%d), by_policy(%d)", mState, mStatePrev, mByPolicy);
190 if (mStateChangedCallback.onStateChanged != NULL) {
191 mStateChangedCallback.onStateChanged(mState, mStatePrev, mByPolicy, mStateChangedCallback.mUserData);
195 void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state) {
196 onStateChanged(state, false);
199 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) {
202 int session_option = pHandler->getOptions();
205 ///////////////////////////////////////
206 // Triggered by 'focus watch callback'
207 ///////////////////////////////////////
209 if (session_option & (MM_SESSION_OPTION_PAUSE_OTHERS | MM_SESSION_OPTION_UNINTERRUPTIBLE)) {
210 AUDIO_IO_LOGD("Session option is pausing others or uninterruptible, skip...");
214 if (state == FOCUS_IS_RELEASED) {
215 // Focus handle(id) of the other application was released, do resume if possible
218 mpPulseAudioClient->cork(false);
219 onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
223 // Focus watch callback doesn't have focus handle, but it need to convert & report to application for convenience
224 state = FOCUS_IS_ACQUIRED;
225 } else if (state == FOCUS_IS_ACQUIRED) {
226 // Focus handle(id) of the other application was acquired, do pause if possible
229 if (mpPulseAudioClient->getStreamDirection() == CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
230 if (mpPulseAudioClient->drain() == false) {
231 AUDIO_IO_LOGE("Failed CPulseAudioClient::drain()");
235 mpPulseAudioClient->cork(true);
236 onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
240 // Focus watch callback doesn't have focus handle, but it need to convert & report to application for convenience
241 state = FOCUS_IS_RELEASED;
244 ///////////////////////////////////////
245 // Triggered by 'focus callback'
246 ///////////////////////////////////////
248 if (pHandler->getId() != id) {
249 AUDIO_IO_LOGW("Id is different, why? [mId : %d]", pHandler->getId());
252 if (session_option & MM_SESSION_OPTION_UNINTERRUPTIBLE) {
253 AUDIO_IO_LOGD("Session option is uninterruptible, skip...");
257 if (state == FOCUS_IS_RELEASED) {
258 // Focus handle(id) was released, do pause here
261 if (mpPulseAudioClient->getStreamDirection() == CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
262 if (mpPulseAudioClient->drain() == false) {
263 AUDIO_IO_LOGE("Failed CPulseAudioClient::drain()");
267 mpPulseAudioClient->cork(true);
268 onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
271 } else if (state == FOCUS_IS_ACQUIRED) {
272 // Focus handle(id) was acquired again,
273 // check reason_for_change ("call-voice","call-video","voip","alarm","notification", ...)
274 // do resume here and call interrupt completed callback to application.
277 mpPulseAudioClient->cork(false);
278 onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
284 if (mInterruptCallback.onInterrupt != NULL) {
285 IAudioSessionEventListener::EInterruptCode e = IAudioSessionEventListener::EInterruptCode::INTERRUPT_COMPLETED;
286 e = IAudioSessionEventListener::convertInterruptedCode(state, reason_for_change);
287 mInterruptCallback.onInterrupt(e, mInterruptCallback.mUserData);
291 void CAudioIO::onSignal(CAudioSessionHandler* pHandler, mm_sound_signal_name_t signal, int value) {
294 if (signal == MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS) {
295 if (value == 1 && pHandler->getSubscribeId() >= 0) {
296 // Unregister focus watch callback & disable session handler
297 pHandler->disableSessionHandler();
298 AUDIO_IO_LOGD("Session handler disabled by signal");
299 } else if (value == 0) {
300 // Currently do nothing...
305 void CAudioIO::prepare() throw(CAudioError) {
306 if (__mIsInit == false) {
307 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
311 AUDIO_IO_LOGD("prepare");
313 } catch (CAudioError e) {
318 void CAudioIO::unprepare() throw(CAudioError) {
319 if (__mIsInit == false) {
320 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
324 AUDIO_IO_LOGD("unprepare");
326 } catch (CAudioError e) {
331 void CAudioIO::pause() throw(CAudioError) {
332 if (__mIsInit == false || IsReady() == false) {
333 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
338 AUDIO_IO_LOGD("pause");
339 mpPulseAudioClient->cork(true);
341 } catch (CAudioError e) {
347 void CAudioIO::resume() throw(CAudioError) {
348 if (__mIsInit == false || IsReady() == false) {
349 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
354 AUDIO_IO_LOGD("resume");
355 mpPulseAudioClient->cork(false);
357 } catch (CAudioError e) {
363 void CAudioIO::drain() throw(CAudioError) {
364 if (__mIsInit == false || IsReady() == false) {
365 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
370 AUDIO_IO_LOGD("drain");
371 mpPulseAudioClient->drain();
373 } catch (CAudioError e) {
379 void CAudioIO::flush() throw(CAudioError) {
380 if (__mIsInit == false || IsReady() == false) {
381 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
386 AUDIO_IO_LOGD("flush");
387 mpPulseAudioClient->flush();
389 } catch (CAudioError e) {
395 CAudioInfo& CAudioIO::getAudioInfo() throw(CAudioError) {
396 if (__mIsInit == false) {
397 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
403 void CAudioIO::setStreamCallback(SStreamCallback callback) throw(CAudioError) {
404 if (__mIsInit == false) {
405 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
408 mStreamCallback = callback;
411 CAudioIO::SStreamCallback CAudioIO::getStreamCallback() throw(CAudioError) {
412 if (__mIsInit == false) {
413 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
416 return mStreamCallback;
419 void CAudioIO::setStateChangedCallback(SStateChangedCallback callback) throw(CAudioError) {
420 if (__mIsInit == false) {
421 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
424 mStateChangedCallback = callback;
427 CAudioIO::SStateChangedCallback CAudioIO::getStateChangedCallback() throw(CAudioError) {
428 if (__mIsInit == false) {
429 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
432 return mStateChangedCallback;
435 void CAudioIO::setInterruptCallback(SInterruptCallback callback) throw(CAudioError) {
436 if (__mIsInit == false) {
437 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
440 mInterruptCallback = callback;
443 CAudioIO::SInterruptCallback CAudioIO::getInterruptCallback() throw(CAudioError) {
444 if (__mIsInit == false) {
445 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
448 return mInterruptCallback;
452 void CAudioIO::ignoreSession() throw(CAudioError) {
453 if (__mIsInit == false) {
454 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
460 if (mpPulseAudioClient != NULL && mpPulseAudioClient->isCorked() == false) {
461 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "An Operation is not permitted while started");
464 bool isSkip = mpAudioSessionHandler->isSkipSessionEvent();
465 if (isSkip == false && mpAudioSessionHandler->getId() >= 0) {
466 mpAudioSessionHandler->unregisterSound();
467 __mForceIgnore = true;
471 } catch (CAudioError e) {