Throw invalid operation error when unprepare() is called at inside of pulse-thread
[platform/core/api/audio-io.git] / include / CAudioSessionHandler.h
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 #ifndef __TIZEN_MEDIA_AUDIOIO_IO_CAUDIO_SESSION_HANDLER_H__
18 #define __TIZEN_MEDIA_AUDIOIO_IO_CAUDIO_SESSION_HANDLER_H__
19
20
21 #ifdef __cplusplus
22
23 #include <pthread.h>
24 #include <mm_session.h>
25 #include <mm_session_private.h>
26 #include <mm_sound.h>
27 #include <mm_sound_focus.h>
28 #include <sound_manager.h>
29 #include <sound_manager_internal.h>
30
31 namespace tizen_media_audio {
32
33
34     /**
35      * ASM Thread
36      */
37     class CAudioSessionHandler {
38     public:
39         enum class EAudioSessionType : unsigned int {
40             AUDIO_SESSION_TYPE_CAPTURE,
41             AUDIO_SESSION_TYPE_PLAYBACK
42         };
43
44         /* Constructor & Destructor */
45         CAudioSessionHandler(EAudioSessionType sessionType, CAudioInfo& audioInfo, IAudioSessionEventListener* listener);
46         virtual ~CAudioSessionHandler();
47
48         /* Methods */
49         virtual void initialize();
50         virtual void finalize();
51
52         bool isSkipSession();
53
54         void registerSound();
55         void unregisterSound();
56
57         void updatePlaying();
58         void updateStop();
59         void disableSessionHandler();
60
61         /* Setter & Getter */
62         int getId();
63         int getOptions();
64         EAudioSessionType  getAudioSession();
65         MMSessionType      getMultimediaSession();
66         void getInternalVoipStreamInfo(sound_stream_info_h *stream_info);
67         unsigned int getSubscribeId();
68         CAudioInfo getAudioInfo();
69
70     private:
71         struct __streamTypeTable {
72             const char* name;
73             MMSessionType type;
74         };
75         const struct __streamTypeTable __STREAM_TYPE_TABLE_IN[MM_SESSION_TYPE_NUM] = {
76             {"media",        MM_SESSION_TYPE_MEDIA},
77             {"media",        MM_SESSION_TYPE_MEDIA_RECORD},
78             {"media",        MM_SESSION_TYPE_ALARM},
79             {"media",        MM_SESSION_TYPE_NOTIFY},
80             {"media",        MM_SESSION_TYPE_EMERGENCY},
81             {"media",        MM_SESSION_TYPE_CALL},
82             {"media",        MM_SESSION_TYPE_VIDEOCALL},
83             {"voip",         MM_SESSION_TYPE_VOIP},
84             {"media",        MM_SESSION_TYPE_VOICE_RECOGNITION},
85             {"media",        MM_SESSION_TYPE_RECORD_AUDIO},
86             {"media",        MM_SESSION_TYPE_RECORD_VIDEO}
87         };
88         const struct __streamTypeTable __STREAM_TYPE_TABLE_OUT[MM_SESSION_TYPE_NUM] = {
89             {"media",        MM_SESSION_TYPE_MEDIA},
90             {"media",        MM_SESSION_TYPE_MEDIA_RECORD},
91             {"alarm",        MM_SESSION_TYPE_ALARM},
92             {"notification", MM_SESSION_TYPE_NOTIFY},
93             {"emergency",    MM_SESSION_TYPE_EMERGENCY},
94             {"media",        MM_SESSION_TYPE_CALL},
95             {"media",        MM_SESSION_TYPE_VIDEOCALL},
96             {"voip",         MM_SESSION_TYPE_VOIP},
97             {"media",        MM_SESSION_TYPE_VOICE_RECOGNITION},
98             {"media",        MM_SESSION_TYPE_RECORD_AUDIO},
99             {"media",        MM_SESSION_TYPE_RECORD_VIDEO}
100         };
101
102         /* Private Static Methods */
103         static int __pcmCaptureCountInc();
104         static int __pcmCaptureCountDec();
105         static int __pcmCaptureCountGet();
106         static int __focusIdCountInc();
107         static int __focusIdCountDec();
108         static int __focusIdCountGet();
109
110         static void __sound_pcm_signal_cb(mm_sound_signal_name_t signal, int value, void *user_data);
111         static void __sound_pcm_focus_cb(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state, const char *reason_for_change, int option, const char *additional_info, void *user_data);
112         static void __sound_pcm_focus_watch_cb(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state, const char *reason_for_change, const char *additional_info, void *user_data);
113
114         /* Private Method */
115         CAudioError __convertStreamType(EAudioSessionType type1, MMSessionType type2, int *index);
116         CAudioError __getAsmInformation(MMSessionType *type, int *options);
117         bool __isFocusRequired(MMSessionType type, int options);
118         bool __isFocusDisableReacquisitionRequired(MMSessionType type, int options);
119         void __lockFocusIdMutex();
120         void __unlockFocusIdMutex();
121         void __lockFocusCBMutex();
122         void __unlockFocusCBMutex();
123         bool __checkNeedBlock(const char *focus_acquired_by);
124
125         /* Static Member */
126         static int                  __sCaptureRef;
127         static int                  __sFocusRef;
128
129         /* Members */
130         int                         __mId;
131         int                         __mOptions;
132
133         EAudioSessionType           __mAudioSession;
134         MMSessionType               __mMultimediaSession;
135
136         CAudioInfo                  __mAudioInfo;        /* Referenced from CAudioIO */
137
138         IAudioSessionEventListener* __mpEventListener;
139
140         bool                        __mIsInit;
141         unsigned int                __mSubscribeId;
142
143         bool                        __mUseFocus;
144         int                         __mAcquiredFocus;    /* For audio focus */
145         char*                       __mReasonForChange;  /* For audio focus */
146         char*                       __mAdditionalInfo;   /* For audio focus */
147
148         pthread_mutex_t             __mFocusIdMutex;
149         pthread_mutex_t             __mFocusCBMutex;
150     };
151
152
153 } /* namespace tizen_media_audio */
154
155 #endif
156 #endif /* __TIZEN_MEDIA_AUDIOIO_IO_CAUDIO_SESSION_HANDLER_H__ */