Create Project for repo.or.cz
[vp.git] / src / audio / HLH_Audio.h
blobf417199b26fb9629c49e0b4ed3e3b0b8dfcce33e
1 /*******************************************************************************
2 * File : HLH_Audio.h
3 *
4 * Author : Henry He
5 * Created : 2009-10-16 10:03:49
6 * Description :
7 ******************************************************************************/
9 #ifndef __HLH_AUDIO_INC_20091016_100349_HENRY__
10 #define __HLH_AUDIO_INC_20091016_100349_HENRY__
13 /*******************************************************************************
14 * Desc : Includes Files
15 ******************************************************************************/
17 #include "utils/common.h"
20 #if (__USE_FAKE_AUDIO__ <= 0)
22 /*******************************************************************************
23 * Desc : Macro Definations
24 ******************************************************************************/
26 ////////////////////////////////////////////////////////////////////////////////
27 // Error Codes
29 #define HLH_AUDIO_ERR_FAILED (-1)
30 #define HLH_AUDIO_ERR_ALREADYCREATED (-2)
31 #define HLH_AUDIO_ERR_NOT_CREATED (-3)
33 ////////////////////////////////////////////////////////////////////////////////
34 // Default
36 #define HLH_AUDIO_DEFAULT_BUF_NUMBER 8
37 #define HLH_AUDIO_DEFAULT_BUF_SIZE_LOG2 8 /* 2^8 = 256 */
38 #define HLH_AUDIO_DEFAULT_SAMPLE_BITS AFMT_U8
39 #define HLH_AUDIO_DEFAULT_SAMPLE_RATE 8000
40 #define HLH_AUDIO_DEFAULT_CHANNELS 1
43 ////////////////////////////////////////////////////////////////////////////////
44 // Timeouts
46 #define HLH_AUDIO_TIMEOUT_POLLWAIT_SECS 2
48 ////////////////////////////////////////////////////////////////////////////////
49 // Poll Types
51 #define HLH_AUDIO_POLL_READ 0x01
52 #define HLH_AUDIO_POLL_WRITE 0x02
53 #define HLH_AUDIO_POLL_ERROR 0x04
57 /*******************************************************************************
58 * Desc : Type Definations
59 ******************************************************************************/
61 ////////////////////////////////////////////////////////////////////////////////
62 // Typedef of callback function called when audio read
63 typedef void (*OnAudioReadFunc) (void *pvParam, void *pvBuf, UINT32 unLen);
66 /*******************************************************************************
67 * Desc : Global Variables
68 ******************************************************************************/
74 /*******************************************************************************
75 * Desc : Classes
76 ******************************************************************************/
81 ////////////////////////////////////////////////////////////////////////////////
82 // Simple wrapper of audio device
84 class HLH_Audio
87 public:
89 ////////////////////////////////////////////////////////////////////////////////
90 // Constructor / Deconstructor
91 HLH_Audio ();
92 ~HLH_Audio ();
94 public:
96 ////////////////////////////////////////////////////////////////////////////////
97 // Create audio for read/write
98 int Create (
99 OnAudioReadFunc zoaOnRead = NULL,
100 void * pvOnReadParam = NULL,
101 UINT32 unSampleBits = HLH_AUDIO_DEFAULT_SAMPLE_BITS,
102 UINT32 unSampleRate = HLH_AUDIO_DEFAULT_SAMPLE_RATE,
103 UINT32 unChannels = HLH_AUDIO_DEFAULT_CHANNELS,
104 UINT16 unBufSizeLog2 = HLH_AUDIO_DEFAULT_BUF_SIZE_LOG2,
105 UINT16 unBufNum = HLH_AUDIO_DEFAULT_BUF_NUMBER
108 // Close the audio device and
109 void Destroy ();
111 // Whether this instance is created
112 bool IsCreated ();
114 // Read for audio device
115 int Read (void *pvBuf, UINT32 unLen);
117 // Write to audio device
118 int Write (void *pvBuf, UINT32 unLen);
120 ////////////////////////////////////////////////////////////////////////////////
121 // Poll for specific event
122 int Poll (UINT32 &unPollType);
124 // Poll for specific event util time run out
125 int PollWait (UINT32 &unPollType, HLH_Time &zhtTime);
127 private:
128 ////////////////////////////////////////////////////////////////////////////////
129 // Private operations
131 // Read thread to poll for read event and call callback function of read
132 void __ReadThreadFunc (HLH_Thread &zhtThread);
133 static void * ReadThreadFunc (HLH_Thread &zhtThread, void *pvThis);
135 private:
136 ////////////////////////////////////////////////////////////////////////////////
137 // Mutex for this instance
138 HLH_Mutex m_zhmMutex;
140 // Mutex for read
141 HLH_Mutex m_zhmReadMutex;
143 // Mutex for write
144 HLH_Mutex m_zhmWriteMutex;
146 // Whether this instance created
147 bool m_bCreated;
150 ////////////////////////////////////////////////////////////////////////////////
151 // Thread to read audio and call callback function of read
152 HLH_Thread m_zhtReadThread;
154 // Read Handler
155 OnAudioReadFunc m_zoaOnRead;
157 // Parameter to m_zoaOnRead;
158 void * m_pvOnReadParam;
160 char * m_pcReadBuf;
161 UINT32 m_unReadLen;
163 ////////////////////////////////////////////////////////////////////////////////
164 // File description of audio device
165 int m_fdAudio;
167 // Poll file discriptor set: initialized when created
168 fd_set m_fsReadSet;
169 fd_set m_fsWriteSet;
170 fd_set m_fsErrorSet;
174 #endif /* (__USE_FAKE_AUDIO__ <= 0) */
176 #endif /* __HLH_AUDIO_INC_20091016_100349_HENRY__ */