Use a separate function to create and connect a PulseAudio record stream
[openal-soft/openal-hmr.git] / Alc / helpers.c
blob7a55b8d9e636ed3db866a17e1a9538f7bf3fb1b4
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2011 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <stdlib.h>
24 #ifdef HAVE_DLFCN_H
25 #include <dlfcn.h>
26 #endif
28 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
29 #define INITGUID
30 #include <windows.h>
31 #ifdef HAVE_GUIDDEF_H
32 #include <guiddef.h>
33 #else
34 #include <initguid.h>
35 #endif
37 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
38 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
40 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
42 DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
43 DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
44 DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
45 DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
47 #ifdef HAVE_MMDEVAPI
48 #include <propkeydef.h>
49 #include <devpropdef.h>
51 DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_GUID, 0x1da5d803, 0xd492, 0x4edd, 0x8c,0x23, 0xe0,0xc0,0xff,0xee,0x7f,0x0e, 4);
52 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
53 #endif
55 #endif
57 #include "alMain.h"
59 #ifdef _WIN32
60 void pthread_once(pthread_once_t *once, void (*callback)(void))
62 LONG ret;
63 while((ret=InterlockedExchange(once, 1)) == 1)
64 sched_yield();
65 if(ret == 0)
66 callback();
67 InterlockedExchange(once, 2);
71 int pthread_key_create(pthread_key_t *key, void (*callback)(void*))
73 *key = TlsAlloc();
74 if(callback)
75 InsertUIntMapEntry(&TlsDestructor, *key, callback);
76 return 0;
79 int pthread_key_delete(pthread_key_t key)
81 InsertUIntMapEntry(&TlsDestructor, key, NULL);
82 TlsFree(key);
83 return 0;
86 void *pthread_getspecific(pthread_key_t key)
87 { return TlsGetValue(key); }
89 int pthread_setspecific(pthread_key_t key, void *val)
91 TlsSetValue(key, val);
92 return 0;
96 void *LoadLib(const char *name)
97 { return LoadLibraryA(name); }
98 void CloseLib(void *handle)
99 { FreeLibrary((HANDLE)handle); }
100 void *GetSymbol(void *handle, const char *name)
102 void *ret;
104 ret = (void*)GetProcAddress((HANDLE)handle, name);
105 if(ret == NULL)
106 ERR("Failed to load %s\n", name);
107 return ret;
110 #else
112 void InitializeCriticalSection(CRITICAL_SECTION *cs)
114 pthread_mutexattr_t attrib;
115 int ret;
117 ret = pthread_mutexattr_init(&attrib);
118 assert(ret == 0);
120 ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
121 #ifdef HAVE_PTHREAD_NP_H
122 if(ret != 0)
123 ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
124 #endif
125 assert(ret == 0);
126 ret = pthread_mutex_init(cs, &attrib);
127 assert(ret == 0);
129 pthread_mutexattr_destroy(&attrib);
131 void DeleteCriticalSection(CRITICAL_SECTION *cs)
133 int ret;
134 ret = pthread_mutex_destroy(cs);
135 assert(ret == 0);
137 void EnterCriticalSection(CRITICAL_SECTION *cs)
139 int ret;
140 ret = pthread_mutex_lock(cs);
141 assert(ret == 0);
143 void LeaveCriticalSection(CRITICAL_SECTION *cs)
145 int ret;
146 ret = pthread_mutex_unlock(cs);
147 assert(ret == 0);
150 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
151 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
152 * Additionally, Win32 is supposed to measure the time since Windows started,
153 * as opposed to the actual time. */
154 ALuint timeGetTime(void)
156 #if _POSIX_TIMERS > 0
157 struct timespec ts;
158 int ret = -1;
160 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
161 #if _POSIX_MONOTONIC_CLOCK == 0
162 static int hasmono = 0;
163 if(hasmono > 0 || (hasmono == 0 &&
164 (hasmono=sysconf(_SC_MONOTONIC_CLOCK)) > 0))
165 #endif
166 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
167 #endif
168 if(ret != 0)
169 ret = clock_gettime(CLOCK_REALTIME, &ts);
170 assert(ret == 0);
172 return ts.tv_nsec/1000000 + ts.tv_sec*1000;
173 #else
174 struct timeval tv;
175 int ret;
177 ret = gettimeofday(&tv, NULL);
178 assert(ret == 0);
180 return tv.tv_usec/1000 + tv.tv_sec*1000;
181 #endif
184 void Sleep(ALuint t)
186 struct timespec tv, rem;
187 tv.tv_nsec = (t*1000000)%1000000000;
188 tv.tv_sec = t/1000;
190 while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
191 tv = rem;
194 #ifdef HAVE_DLFCN_H
196 void *LoadLib(const char *name)
198 const char *err;
199 void *handle;
201 dlerror();
202 handle = dlopen(name, RTLD_NOW);
203 if((err=dlerror()) != NULL)
204 handle = NULL;
205 return handle;
207 void CloseLib(void *handle)
208 { dlclose(handle); }
209 void *GetSymbol(void *handle, const char *name)
211 const char *err;
212 void *sym;
214 dlerror();
215 sym = dlsym(handle, name);
216 if((err=dlerror()) != NULL)
218 WARN("Failed to load %s: %s\n", name, err);
219 sym = NULL;
221 return sym;
224 #endif
225 #endif
228 void al_print(const char *func, const char *fmt, ...)
230 char str[256];
231 int i;
233 i = snprintf(str, sizeof(str), "AL lib: %s: ", func);
234 if(i < (int)sizeof(str) && i > 0)
236 va_list ap;
237 va_start(ap, fmt);
238 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
239 va_end(ap);
241 str[sizeof(str)-1] = 0;
243 fprintf(LogFile, "%s", str);
244 fflush(LogFile);
248 void SetRTPriority(void)
250 ALboolean failed = AL_FALSE;
252 #ifdef _WIN32
253 if(RTPrioLevel > 0)
254 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
255 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
256 if(RTPrioLevel > 0)
258 struct sched_param param;
259 /* Use the minimum real-time priority possible for now (on Linux this
260 * should be 1 for SCHED_RR) */
261 param.sched_priority = sched_get_priority_min(SCHED_RR);
262 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
264 #else
265 /* Real-time priority not available */
266 failed = (RTPrioLevel>0);
267 #endif
268 if(failed)
269 ERR("Failed to set priority level for thread\n");
273 static void Lock(volatile ALenum *l)
275 while(ExchangeInt(l, AL_TRUE) == AL_TRUE)
276 sched_yield();
279 static void Unlock(volatile ALenum *l)
281 ExchangeInt(l, AL_FALSE);
284 void RWLockInit(RWLock *lock)
286 lock->read_count = 0;
287 lock->write_count = 0;
288 lock->read_lock = AL_FALSE;
289 lock->read_entry_lock = AL_FALSE;
290 lock->write_lock = AL_FALSE;
293 void ReadLock(RWLock *lock)
295 Lock(&lock->read_entry_lock);
296 Lock(&lock->read_lock);
297 if(IncrementRef(&lock->read_count) == 1)
298 Lock(&lock->write_lock);
299 Unlock(&lock->read_lock);
300 Unlock(&lock->read_entry_lock);
303 void ReadUnlock(RWLock *lock)
305 if(DecrementRef(&lock->read_count) == 0)
306 Unlock(&lock->write_lock);
309 void WriteLock(RWLock *lock)
311 if(IncrementRef(&lock->write_count) == 1)
312 Lock(&lock->read_lock);
313 Lock(&lock->write_lock);
316 void WriteUnlock(RWLock *lock)
318 Unlock(&lock->write_lock);
319 if(DecrementRef(&lock->write_count) == 0)
320 Unlock(&lock->read_lock);
324 void InitUIntMap(UIntMap *map, ALsizei limit)
326 map->array = NULL;
327 map->size = 0;
328 map->maxsize = 0;
329 map->limit = limit;
330 RWLockInit(&map->lock);
333 void ResetUIntMap(UIntMap *map)
335 WriteLock(&map->lock);
336 free(map->array);
337 map->array = NULL;
338 map->size = 0;
339 map->maxsize = 0;
340 WriteUnlock(&map->lock);
343 ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
345 ALsizei pos = 0;
347 WriteLock(&map->lock);
348 if(map->size > 0)
350 ALsizei low = 0;
351 ALsizei high = map->size - 1;
352 while(low < high)
354 ALsizei mid = low + (high-low)/2;
355 if(map->array[mid].key < key)
356 low = mid + 1;
357 else
358 high = mid;
360 if(map->array[low].key < key)
361 low++;
362 pos = low;
365 if(pos == map->size || map->array[pos].key != key)
367 if(map->size == map->limit)
369 WriteUnlock(&map->lock);
370 return AL_OUT_OF_MEMORY;
373 if(map->size == map->maxsize)
375 ALvoid *temp = NULL;
376 ALsizei newsize;
378 newsize = (map->maxsize ? (map->maxsize<<1) : 4);
379 if(newsize >= map->maxsize)
380 temp = realloc(map->array, newsize*sizeof(map->array[0]));
381 if(!temp)
383 WriteUnlock(&map->lock);
384 return AL_OUT_OF_MEMORY;
386 map->array = temp;
387 map->maxsize = newsize;
390 if(pos < map->size)
391 memmove(&map->array[pos+1], &map->array[pos],
392 (map->size-pos)*sizeof(map->array[0]));
393 map->size++;
395 map->array[pos].key = key;
396 map->array[pos].value = value;
397 WriteUnlock(&map->lock);
399 return AL_NO_ERROR;
402 ALvoid *RemoveUIntMapKey(UIntMap *map, ALuint key)
404 ALvoid *ptr = NULL;
405 WriteLock(&map->lock);
406 if(map->size > 0)
408 ALsizei low = 0;
409 ALsizei high = map->size - 1;
410 while(low < high)
412 ALsizei mid = low + (high-low)/2;
413 if(map->array[mid].key < key)
414 low = mid + 1;
415 else
416 high = mid;
418 if(map->array[low].key == key)
420 ptr = map->array[low].value;
421 if(low < map->size-1)
422 memmove(&map->array[low], &map->array[low+1],
423 (map->size-1-low)*sizeof(map->array[0]));
424 map->size--;
427 WriteUnlock(&map->lock);
428 return ptr;
431 ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
433 ALvoid *ptr = NULL;
434 ReadLock(&map->lock);
435 if(map->size > 0)
437 ALsizei low = 0;
438 ALsizei high = map->size - 1;
439 while(low < high)
441 ALsizei mid = low + (high-low)/2;
442 if(map->array[mid].key < key)
443 low = mid + 1;
444 else
445 high = mid;
447 if(map->array[low].key == key)
448 ptr = map->array[low].value;
450 ReadUnlock(&map->lock);
451 return ptr;