move __IsReady function into the critical section
[platform/core/api/audio-io.git] / include / CPulseAudioClient.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_CPULSEAUDIO_CLIENT_H__
18 #define __TIZEN_MEDIA_AUDIO_IO_CPULSEAUDIO_CLIENT_H__
19
20
21 #ifdef __cplusplus
22
23
24 #include <pulse/pulseaudio.h>
25
26
27 namespace tizen_media_audio {
28     /**
29      * PULSE ThreadMainloop Locker
30      */
31     class CPulseStreamSpec;
32
33     class CPulseAudioClient {
34     public:
35         enum class EStreamDirection : unsigned int {
36             STREAM_DIRECTION_RECORD,        /**< Record stream */
37             STREAM_DIRECTION_PLAYBACK       /**< Playback stream */
38         };
39
40         /* Constants */
41         static const char* CLIENT_NAME;
42
43         /* Constructor & Destructor */
44         CPulseAudioClient(EStreamDirection      direction,
45                           CPulseStreamSpec&     spec,
46                           IPulseStreamListener* listener);
47         ~CPulseAudioClient();
48
49         /* Implemented Methods */
50         void initialize();
51         void finalize();
52
53         /* Methods */
54         int read(void* buffer, size_t length);
55         int peek(const void** buffer, size_t* length);
56         int drop();
57         int write(const void* buffer, size_t length);
58
59         void cork(bool cork);
60         bool isCorked();
61
62         bool drain();
63         bool flush();
64
65         void checkRunningState();
66         bool isInThread() const;
67
68         size_t getWritableSize();
69         size_t getReadableSize();
70
71         size_t getBufferSize();
72
73         pa_usec_t getLatency();
74         pa_usec_t getFinalLatency();
75
76         void applyRecordVolume(double volume);
77
78     private:
79         /* Members */
80         EStreamDirection      __mDirection;
81         CPulseStreamSpec      __mSpec;
82         IPulseStreamListener* __mpListener;
83
84         pa_threaded_mainloop* __mpMainloop;
85         pa_context*           __mpContext;
86         pa_stream*            __mpStream;
87         pa_proplist*          __mpPropList;
88
89         bool                  __mIsInit;
90         bool                  __mIsOperationSuccess;
91
92         const void*           __mpSyncReadDataPtr;
93         size_t                __mSyncReadIndex;
94         size_t                __mSyncReadLength;
95         bool                  __mIsUsedSyncRead;
96         bool                  __mIsFirstStream;
97         bool                  __mIsDraining;
98         bool                  __mIsStarted;
99
100         /* Static Methods */
101
102         /* Private Method */
103
104         /* Private Callback Method */
105         static void __contextStateChangeCb(pa_context* c, void* user_data);
106         static void __streamStateChangeCb(pa_stream* s, void* user_data);
107         static void __streamCaptureCb(pa_stream* s, size_t length, void* user_data);
108         static void __streamPlaybackCb(pa_stream* s, size_t length, void* user_data);
109         static void __streamLatencyUpdateCb(pa_stream* s, void* user_data);
110         static void __streamStartedCb(pa_stream* s, void* user_data);
111         static void __streamUnderflowCb(pa_stream* s, void* user_data);
112         static void __streamEventCb(pa_stream* s, const char *name, pa_proplist *pl, void *user_data);
113         static void __successStreamCb(pa_stream* s, int success, void* user_data);
114         static void __successDrainCb(pa_stream* s, int success, void* user_data);
115         static void __successDrainCbInThread(pa_stream* s, int success, void* user_data);
116         static void __successVolumeCb(pa_context* c, int success, void* user_data);
117
118         class CPulseThreadLocker {
119         public:
120             explicit CPulseThreadLocker(pa_threaded_mainloop* mainloop);
121             virtual ~CPulseThreadLocker();
122
123         private:
124             const pa_threaded_mainloop* ml{};
125         };
126     };
127
128
129 } /* namespace tizen_media_audio */
130
131 #endif
132 #endif /* __TIZEN_MEDIA_AUDIO_IO_CPULSEAUDIO_CLIENT_H__ */