d68e02ccc444ecbecd528471a50783ff1a49d499
[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
30     /**
31      * PULSE Thread
32      */
33     class CPulseAudioVolume;
34     class CPulseAudioPolicy;
35     class CPulseStreamSpec;
36     class CPulseAudioClient {
37     public:
38         enum class EStreamDirection : unsigned int {
39             STREAM_DIRECTION_RECORD,        /**< Record stream */
40             STREAM_DIRECTION_PLAYBACK       /**< Playback stream */
41         };
42
43         /* Constants */
44         static const char* CLIENT_NAME;
45
46         /* Constructor & Destructor */
47         CPulseAudioClient(EStreamDirection      direction,
48                           CPulseStreamSpec&     spec,
49                           IPulseStreamListener* listener);
50         ~CPulseAudioClient();
51
52         /* Implemented Methods */
53         void initialize();
54         void finalize();
55
56         /* Methods */
57         int read(void* buffer, size_t length);
58         int peek(const void** buffer, size_t* length);
59         int drop();
60         int write(const void* buffer, size_t length);
61
62         void cork(bool cork);
63         bool isCorked();
64
65         bool drain();
66         bool flush();
67
68         void checkRunningState();
69         bool isInThread();
70
71         size_t getWritableSize();
72         size_t getReadableSize();
73
74         size_t getBufferSize();
75
76         pa_usec_t getLatency();
77         pa_usec_t getFinalLatency();
78
79         /* Setter & Getter */
80         EStreamDirection getStreamDirection();
81         CPulseStreamSpec getStreamSpec();
82
83     private:
84         /* Members */
85         EStreamDirection      __mDirection;
86         CPulseStreamSpec      __mSpec;
87         IPulseStreamListener* __mpListener;
88
89         pa_threaded_mainloop* __mpMainloop;
90         pa_context*           __mpContext;
91         pa_stream*            __mpStream;
92         pa_proplist*          __mpPropList;
93
94         bool                  __mIsInit;
95         bool                  __mIsOperationSuccess;
96
97         const void*           __mpSyncReadDataPtr;
98         size_t                __mSyncReadIndex;
99         size_t                __mSyncReadLength;
100         bool                  __mIsUsedSyncRead;
101         bool                  __mIsFirstStream;
102         bool                  __mIsDraining;
103
104         /* Static Methods */
105
106         /* Private Method */
107
108         /* Private Calblack Method */
109         static void __contextStateChangeCb(pa_context* c, void* user_data);
110         static void __successContextCb(pa_context* c, int success, void* user_data);
111
112         static void __streamStateChangeCb(pa_stream* s, void* user_data);
113         static void __streamCaptureCb(pa_stream* s, size_t length, void* user_data);
114         static void __streamPlaybackCb(pa_stream* s, size_t length, void* user_data);
115         static void __streamLatencyUpdateCb(pa_stream* s, void* user_data);
116         static void __streamStartedCb(pa_stream* s, void* user_data);
117         static void __streamUnderflowCb(pa_stream* s, void* user_data);
118         static void __streamEventCb(pa_stream* s, const char *name, pa_proplist *pl, void *user_data);
119         static void __successStreamCb(pa_stream* s, int success, void* user_data);
120         static void __successDrainCb(pa_stream* s, int success, void* user_data);
121         static void __successDrainCbInThread(pa_stream* s, int success, void* user_data);
122     };
123
124
125 } /* namespace tizen_media_audio */
126
127 #endif
128 #endif /* __TIZEN_MEDIA_AUDIO_IO_CPULSEAUDIO_CLIENT_H__ */