Revise cpp codes
[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 #include <sound_manager.h>
21
22 #ifdef __cplusplus
23
24
25 namespace tizen_media_audio {
26
27
28     /**
29      *  Abstract Class
30      *  You can't take this object directly.
31      */
32     class IPulseStreamListener;
33     class CAudioIO : public IPulseStreamListener {
34     public:
35         struct SStreamCallback {
36             void* mUserData;
37             void (*onStream)(size_t nbytes, void* user_data);
38
39             SStreamCallback() : mUserData(nullptr), onStream(nullptr)
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(nullptr), onStateChanged(nullptr)
48             { /* Empty Body */ }
49         };
50
51         /* Constructor & Destructor */
52         CAudioIO();
53         explicit CAudioIO(CAudioInfo& audioInfo);
54         virtual ~CAudioIO() = default;
55
56         /* Pure Virtual Methods */
57         virtual void initialize() = 0;
58         virtual void finalize() = 0;
59
60         virtual void prepare() = 0;
61         virtual void unprepare() = 0;
62
63         virtual void pause() = 0;
64         virtual void resume() = 0;
65
66         /* FIXME : drain needed to be moved to sub-class */
67         virtual void drain();
68         virtual void flush() = 0;
69
70         virtual int  getBufferSize() = 0;
71
72         /* Implemented Handlers */
73         virtual void onStream(CPulseAudioClient* pClient, size_t length);
74         virtual void onStateChanged(CAudioInfo::EAudioIOState state, bool byPolicy);
75         virtual void onStateChanged(CAudioInfo::EAudioIOState state);
76
77         /* Methods */
78         CAudioInfo& getAudioInfo();
79
80         virtual void setStreamCallback(SStreamCallback callback);
81         SStreamCallback getStreamCallback();
82
83         virtual void setStateChangedCallback(SStateChangedCallback callback);
84         SStateChangedCallback getStateChangedCallback();
85
86         void setStreamInfo(sound_stream_info_h stream_info);
87
88         CAudioInfo::EAudioIOState getState() noexcept;
89
90     protected:
91         /* Protected Methods */
92         virtual void setInit(bool flag);
93         virtual bool isInit();
94         virtual bool IsReady();
95
96         void internalLock();
97         void internalUnlock();
98         void internalWait();
99         void internalSignal();
100
101         CPulseAudioClient*    mpPulseAudioClient;
102         CAudioInfo            mAudioInfo;
103
104         SStreamCallback       mStreamCallback;
105         SStateChangedCallback mStateChangedCallback;
106
107         CAudioInfo::EAudioDirection mDirection;
108         CAudioInfo::EAudioIOState mState;
109         CAudioInfo::EAudioIOState mStatePrev;
110         bool                  mByPolicy;
111
112     private:
113         pthread_mutex_t       __mMutex;
114         pthread_mutex_t       __mCondMutex;
115         pthread_cond_t        __mCond;
116         bool                  __mIsInit;
117     };
118
119
120 } /* namespace tizen_media_audio */
121
122 #endif
123 #endif /* __TIZEN_MEDIA_AUDIO_IO_CAUDIO_IO_H__ */