Apply tizen coding convention for C++ and header
[platform/core/api/audio-io.git] / src / cpp / CAudioIO.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <mm.h>
19 #include <pthread.h>
20 #include <assert.h>
21 #include "CAudioIODef.h"
22
23 #define AUDIO_IO_DEBUG
24
25 using namespace std;
26 using namespace tizen_media_audio;
27
28
29 /**
30  * class CAudioIO
31  */
32 CAudioIO::CAudioIO() :
33     mpAudioSessionHandler(NULL),
34     mpPulseAudioClient(NULL),
35     __mMutex(PTHREAD_MUTEX_INITIALIZER),
36     __mCond(PTHREAD_COND_INITIALIZER),
37     __mIsInit(false),
38     __mForceIgnore(false) {
39     mState = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
40     mStatePrev = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
41     mByPolicy = false;
42 }
43
44 CAudioIO::CAudioIO(CAudioInfo& audioInfo) :
45     mpAudioSessionHandler(NULL),
46     mpPulseAudioClient(NULL),
47     __mMutex(PTHREAD_MUTEX_INITIALIZER),
48     __mCond(PTHREAD_COND_INITIALIZER),
49     __mIsInit(false),
50     __mForceIgnore(false) {
51     mAudioInfo = audioInfo;
52     mState = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
53     mStatePrev = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
54     mByPolicy = false;
55 }
56
57 CAudioIO::~CAudioIO() {
58 }
59
60 void CAudioIO::setInit(bool flag) {
61     __mIsInit = flag;
62 }
63
64 bool CAudioIO::isInit() {
65     return __mIsInit;
66 }
67
68 bool CAudioIO::IsReady() {
69     return ((mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING || mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED)? true : false);
70 }
71
72 void CAudioIO::internalLock() throw(CAudioError) {
73     if (__mIsInit == false) {
74         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
75     }
76
77     if (pthread_mutex_lock(&__mMutex) != 0) {
78         THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_lock()");
79     }
80 #ifdef _AUDIO_IO_DEBUG_TIMING_
81     AUDIO_IO_LOGD(COLOR_RED "LOCK" COLOR_END);
82 #endif
83 }
84
85 void CAudioIO::internalUnlock() throw(CAudioError) {
86     if (__mIsInit == false) {
87         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
88     }
89
90     if (pthread_mutex_unlock(&__mMutex) != 0) {
91         THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_lock()");
92     }
93 #ifdef _AUDIO_IO_DEBUG_TIMING_
94     AUDIO_IO_LOGD(COLOR_GREEN "UNLOCK" COLOR_END);
95 #endif
96 }
97
98 void CAudioIO::internalWait() throw(CAudioError) {
99     if (__mIsInit == false) {
100         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
101     }
102
103 #ifdef _AUDIO_IO_DEBUG_TIMING_
104     AUDIO_IO_LOGD(COLOR_RED "WAIT" COLOR_END);
105 #endif
106
107     pthread_cond_wait(&__mCond, &__mMutex);
108 }
109
110 void CAudioIO::internalSignal() throw(CAudioError) {
111     if (__mIsInit == false) {
112         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
113     }
114
115 #ifdef _AUDIO_IO_DEBUG_TIMING_
116     AUDIO_IO_LOGD(COLOR_GREEN "SIGNAL" COLOR_END);
117 #endif
118
119     pthread_cond_signal(&__mCond);
120 }
121
122 bool CAudioIO::isForceIgnore() {
123     return __mForceIgnore;
124 }
125
126 void CAudioIO::initialize() throw(CAudioError) {
127     if (__mIsInit == true) {
128         return;
129     }
130
131     AUDIO_IO_LOGD("initialize");
132
133     int ret = pthread_mutex_init(&__mMutex, NULL);
134     if (ret != 0) {
135         THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pthread_mutex_init()");
136     }
137
138     ret = pthread_cond_init(&__mCond, NULL);
139     if (ret != 0) {
140         THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pthread_cond_init()");
141     }
142
143     __mIsInit = true;
144 }
145
146 void CAudioIO::finalize() {
147     if (__mIsInit == false) {
148         return;
149     }
150
151     AUDIO_IO_LOGD("finalize");
152
153     int ret = pthread_mutex_destroy(&__mMutex);
154     if (ret != 0) {
155         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pthread_mutex_destroy() ret:%d", ret);
156     }
157
158     ret = pthread_cond_destroy(&__mCond);
159     if (ret != 0) {
160         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pthread_cond_destroy() ret:%d", ret);
161     }
162
163     __mIsInit = false;
164 }
165
166 void CAudioIO::onStream(CPulseAudioClient* pClient, size_t length) {
167     assert(__mIsInit == true);
168     assert(pClient != NULL);
169     assert(length > 0);
170
171 #ifdef _AUDIO_IO_DEBUG_TIMING_
172     AUDIO_IO_LOGD("mStreamCallback.onStream(%p), pClient(%p), length(%zu)", mStreamCallback.onStream, pClient, length);
173 #endif
174
175     if (mStreamCallback.onStream != NULL) {
176         mStreamCallback.onStream(length, mStreamCallback.mUserData);
177     }
178 }
179
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);
183
184     mStatePrev = mState;
185     mState     = state;
186     mByPolicy  = byPolicy;
187
188     AUDIO_IO_LOGD("current(%d), previous(%d), by_policy(%d)", mState, mStatePrev, mByPolicy);
189
190     if (mStateChangedCallback.onStateChanged != NULL) {
191         mStateChangedCallback.onStateChanged(mState, mStatePrev, mByPolicy, mStateChangedCallback.mUserData);
192     }
193 }
194
195 void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state) {
196     onStateChanged(state, false);
197 }
198
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) {
200     assert(pHandler);
201
202     int session_option = pHandler->getOptions();
203
204     if (id == -1) {
205         ///////////////////////////////////////
206         // Triggered by 'focus watch callback'
207         ///////////////////////////////////////
208
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...");
211             return;
212         }
213
214         if (state == FOCUS_IS_RELEASED) {
215             // Focus handle(id) of the other application was released, do resume if possible
216             internalLock();
217
218             mpPulseAudioClient->cork(false);
219             onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
220
221             internalUnlock();
222
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
227             internalLock();
228
229             if (mpPulseAudioClient->getStreamDirection() == CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
230                 if (mpPulseAudioClient->drain() == false) {
231                     AUDIO_IO_LOGE("Failed CPulseAudioClient::drain()");
232                 }
233             }
234
235             mpPulseAudioClient->cork(true);
236             onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
237
238             internalUnlock();
239
240             // Focus watch callback doesn't have focus handle, but it need to convert & report to application for convenience
241             state = FOCUS_IS_RELEASED;
242         }
243     } else {
244         ///////////////////////////////////////
245         // Triggered by 'focus callback'
246         ///////////////////////////////////////
247
248         if (pHandler->getId() != id) {
249             AUDIO_IO_LOGW("Id is different, why? [mId : %d]", pHandler->getId());
250         }
251
252         if (session_option & MM_SESSION_OPTION_UNINTERRUPTIBLE) {
253             AUDIO_IO_LOGD("Session option is uninterruptible, skip...");
254             return;
255         }
256
257         if (state == FOCUS_IS_RELEASED) {
258             // Focus handle(id) was released, do pause here
259             internalLock();
260
261             if (mpPulseAudioClient->getStreamDirection() == CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
262                 if (mpPulseAudioClient->drain() == false) {
263                     AUDIO_IO_LOGE("Failed CPulseAudioClient::drain()");
264                 }
265             }
266
267             mpPulseAudioClient->cork(true);
268             onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
269
270             internalUnlock();
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.
275             internalLock();
276
277             mpPulseAudioClient->cork(false);
278             onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
279
280             internalUnlock();
281         }
282     }
283
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);
288     }
289 }
290
291 void CAudioIO::onSignal(CAudioSessionHandler* pHandler, mm_sound_signal_name_t signal, int value) {
292     assert(pHandler);
293
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...
301         }
302     }
303 }
304
305 void CAudioIO::prepare() throw(CAudioError) {
306     if (__mIsInit == false) {
307         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
308     }
309
310     try {
311         AUDIO_IO_LOGD("prepare");
312         /* Do nothing */
313     } catch (CAudioError e) {
314         throw e;
315     }
316 }
317
318 void CAudioIO::unprepare() throw(CAudioError) {
319     if (__mIsInit == false) {
320         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
321     }
322
323     try {
324         AUDIO_IO_LOGD("unprepare");
325         /* Do nothing */
326     } catch (CAudioError e) {
327         throw e;
328     }
329 }
330
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");
334     }
335
336     try {
337         internalLock();
338         AUDIO_IO_LOGD("pause");
339         mpPulseAudioClient->cork(true);
340         internalUnlock();
341     } catch (CAudioError e) {
342         internalUnlock();
343         throw e;
344     }
345 }
346
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");
350     }
351
352     try {
353         internalLock();
354         AUDIO_IO_LOGD("resume");
355         mpPulseAudioClient->cork(false);
356         internalUnlock();
357     } catch (CAudioError e) {
358         internalUnlock();
359         throw e;
360     }
361 }
362
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");
366     }
367
368     try {
369         internalLock();
370         AUDIO_IO_LOGD("drain");
371         mpPulseAudioClient->drain();
372         internalUnlock();
373     } catch (CAudioError e) {
374         internalUnlock();
375         throw e;
376     }
377 }
378
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");
382     }
383
384     try {
385         internalLock();
386         AUDIO_IO_LOGD("flush");
387         mpPulseAudioClient->flush();
388         internalUnlock();
389     } catch (CAudioError e) {
390         internalUnlock();
391         throw e;
392     }
393 }
394
395 CAudioInfo& CAudioIO::getAudioInfo() throw(CAudioError) {
396     if (__mIsInit == false) {
397         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
398     }
399
400     return mAudioInfo;
401 }
402
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");
406     }
407
408     mStreamCallback = callback;
409 }
410
411 CAudioIO::SStreamCallback CAudioIO::getStreamCallback() throw(CAudioError) {
412     if (__mIsInit == false) {
413         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
414     }
415
416     return mStreamCallback;
417 }
418
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");
422     }
423
424     mStateChangedCallback = callback;
425 }
426
427 CAudioIO::SStateChangedCallback CAudioIO::getStateChangedCallback() throw(CAudioError) {
428     if (__mIsInit == false) {
429         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
430     }
431
432     return mStateChangedCallback;
433 }
434
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");
438     }
439
440     mInterruptCallback = callback;
441 }
442
443 CAudioIO::SInterruptCallback CAudioIO::getInterruptCallback() throw(CAudioError) {
444     if (__mIsInit == false) {
445         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
446     }
447
448     return mInterruptCallback;
449 }
450
451
452 void CAudioIO::ignoreSession() throw(CAudioError) {
453     if (__mIsInit == false) {
454         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
455     }
456
457     try {
458         internalLock();
459
460         if (mpPulseAudioClient != NULL && mpPulseAudioClient->isCorked() == false) {
461             THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "An Operation is not permitted while started");
462         }
463
464         bool isSkip = mpAudioSessionHandler->isSkipSessionEvent();
465         if (isSkip == false && mpAudioSessionHandler->getId() >= 0) {
466             mpAudioSessionHandler->unregisterSound();
467             __mForceIgnore = true;
468         }
469
470         internalUnlock();
471     } catch (CAudioError e) {
472         internalUnlock();
473         throw e;
474     }
475 }