Revise exception handling
[platform/core/api/audio-io.git] / include / CAudioIO.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_AUDIO_IO_CAUDIO_IO_H__
18 #define __TIZEN_MEDIA_AUDIO_IO_CAUDIO_IO_H__
19
20
21 #ifdef __cplusplus
22
23
24 namespace tizen_media_audio {
25
26
27     /**
28      *  Abstract Class
29      *  You can't take this object directly.
30      */
31     class IPulseStreamListener;
32     class IAudioSessionEventListener;
33     class CAudioIO : public IPulseStreamListener, public IAudioSessionEventListener {
34     public:
35         struct SStreamCallback {
36             void* mUserData;
37             void (*onStream)(size_t nbytes, void* user_data);
38
39             SStreamCallback() : mUserData(NULL), onStream(NULL)
40             { /* Empty Body */ }
41         };
42
43         struct SStateChangedCallback {
44             void* mUserData;
45             void (*onStateChanged)(CAudioInfo::EAudioIOState state, CAudioInfo::EAudioIOState statePrev, bool byPolicy, void* user_data);
46
47             SStateChangedCallback() : mUserData(NULL), onStateChanged(NULL)
48             { /* Empty Body */ }
49         };
50
51         struct SInterruptCallback {
52             void* mUserData;
53             void (*onInterrupt)(IAudioSessionEventListener::EInterruptCode code, void* user_data);
54
55             SInterruptCallback() : mUserData(NULL), onInterrupt(NULL)
56             { /* Empty Body */ }
57         };
58
59         /* Constructor & Destructor */
60         CAudioIO();
61         CAudioIO(CAudioInfo& audioInfo);
62         virtual ~CAudioIO();
63
64         /* Pure Virtual Methods */
65         virtual void initialize() = 0;
66         virtual void finalize() = 0;
67
68         virtual void prepare() = 0;
69         virtual void unprepare() = 0;
70
71         virtual void pause() = 0;
72         virtual void resume() = 0;
73
74         virtual void drain() = 0;
75         virtual void flush() = 0;
76
77         virtual int  getBufferSize() = 0;
78
79         /* Implemented Handlers */
80         virtual void onStream(CPulseAudioClient* pClient, size_t length);
81         virtual void onStateChanged(CAudioInfo::EAudioIOState state, bool byPolicy);
82         virtual void onStateChanged(CAudioInfo::EAudioIOState state);
83         virtual void onInterrupt(CAudioSessionHandler* pHandler, int id, mm_sound_focus_type_e focus_type,
84                                  mm_sound_focus_state_e state, const char *reason_for_change, const char *additional_info);
85         virtual void onSignal(CAudioSessionHandler* pHandler, mm_sound_signal_name_t signal, int value);
86
87         /* Methods */
88         CAudioInfo& getAudioInfo();
89
90         virtual void setStreamCallback(SStreamCallback callback);
91         SStreamCallback getStreamCallback();
92
93         virtual void setStateChangedCallback(SStateChangedCallback callback);
94         SStateChangedCallback getStateChangedCallback();
95
96         void setInterruptCallback(SInterruptCallback callback);
97         SInterruptCallback getInterruptCallback();
98
99         void ignoreSession();
100
101         void setStreamInfo(sound_stream_info_h stream_info);
102
103         void setInternalStreamInfo(); /* this is for session backward compatibility and will be removed later */
104
105         CAudioInfo::EAudioIOState getState();
106
107     protected:
108         /* Protected Methods */
109         virtual void setInit(bool flag);
110         virtual bool isInit();
111         virtual bool IsReady();
112
113         void internalLock();
114         void internalUnlock();
115         void internalWait();
116         void internalSignal();
117
118         bool isForceIgnore();
119
120         CAudioSessionHandler* mpAudioSessionHandler;
121         CPulseAudioClient*    mpPulseAudioClient;
122         CAudioInfo            mAudioInfo;
123
124         SStreamCallback       mStreamCallback;
125         SStateChangedCallback mStateChangedCallback;
126         SInterruptCallback    mInterruptCallback;
127
128         CAudioInfo::EAudioDirection mDirection;
129         CAudioInfo::EAudioIOState mState;
130         CAudioInfo::EAudioIOState mStatePrev;
131         bool                  mByPolicy;
132
133     private:
134         pthread_mutex_t       __mMutex;
135         pthread_cond_t        __mCond;
136         bool                  __mIsInit;
137         bool                  __mForceIgnore;
138
139         void abandonInternalFocus(); /* this is for session backward compatibility and will be removed later */
140     };
141
142
143 } /* namespace tizen_media_audio */
144
145 #endif
146 #endif /* __TIZEN_MEDIA_AUDIO_IO_CAUDIO_IO_H__ */