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
28 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
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);
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);
60 void pthread_once(pthread_once_t
*once
, void (*callback
)(void))
63 while((ret
=InterlockedExchange(once
, 1)) == 1)
67 InterlockedExchange(once
, 2);
71 int pthread_key_create(pthread_key_t
*key
, void (*callback
)(void*))
75 InsertUIntMapEntry(&TlsDestructor
, *key
, callback
);
79 int pthread_key_delete(pthread_key_t key
)
81 InsertUIntMapEntry(&TlsDestructor
, key
, NULL
);
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
);
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
)
104 ret
= (void*)GetProcAddress((HANDLE
)handle
, name
);
106 ERR("Failed to load %s\n", name
);
112 void InitializeCriticalSection(CRITICAL_SECTION
*cs
)
114 pthread_mutexattr_t attrib
;
117 ret
= pthread_mutexattr_init(&attrib
);
120 ret
= pthread_mutexattr_settype(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
121 #ifdef HAVE_PTHREAD_NP_H
123 ret
= pthread_mutexattr_setkind_np(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
126 ret
= pthread_mutex_init(cs
, &attrib
);
129 pthread_mutexattr_destroy(&attrib
);
131 void DeleteCriticalSection(CRITICAL_SECTION
*cs
)
134 ret
= pthread_mutex_destroy(cs
);
137 void EnterCriticalSection(CRITICAL_SECTION
*cs
)
140 ret
= pthread_mutex_lock(cs
);
143 void LeaveCriticalSection(CRITICAL_SECTION
*cs
)
146 ret
= pthread_mutex_unlock(cs
);
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
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))
166 ret
= clock_gettime(CLOCK_MONOTONIC
, &ts
);
169 ret
= clock_gettime(CLOCK_REALTIME
, &ts
);
172 return ts
.tv_nsec
/1000000 + ts
.tv_sec
*1000;
177 ret
= gettimeofday(&tv
, NULL
);
180 return tv
.tv_usec
/1000 + tv
.tv_sec
*1000;
186 struct timespec tv
, rem
;
187 tv
.tv_nsec
= (t
*1000000)%1000000000;
190 while(nanosleep(&tv
, &rem
) == -1 && errno
== EINTR
)
196 void *LoadLib(const char *name
)
202 handle
= dlopen(name
, RTLD_NOW
);
203 if((err
=dlerror()) != NULL
)
207 void CloseLib(void *handle
)
209 void *GetSymbol(void *handle
, const char *name
)
215 sym
= dlsym(handle
, name
);
216 if((err
=dlerror()) != NULL
)
218 WARN("Failed to load %s: %s\n", name
, err
);
228 void al_print(const char *func
, const char *fmt
, ...)
233 i
= snprintf(str
, sizeof(str
), "AL lib: %s: ", func
);
234 if(i
< (int)sizeof(str
) && i
> 0)
238 vsnprintf(str
+i
, sizeof(str
)-i
, fmt
, ap
);
241 str
[sizeof(str
)-1] = 0;
243 fprintf(LogFile
, "%s", str
);
248 void SetRTPriority(void)
250 ALboolean failed
= AL_FALSE
;
254 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
255 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
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
, ¶m
);
265 /* Real-time priority not available */
266 failed
= (RTPrioLevel
>0);
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
)
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
)
330 RWLockInit(&map
->lock
);
333 void ResetUIntMap(UIntMap
*map
)
335 WriteLock(&map
->lock
);
340 WriteUnlock(&map
->lock
);
343 ALenum
InsertUIntMapEntry(UIntMap
*map
, ALuint key
, ALvoid
*value
)
347 WriteLock(&map
->lock
);
351 ALsizei high
= map
->size
- 1;
354 ALsizei mid
= low
+ (high
-low
)/2;
355 if(map
->array
[mid
].key
< key
)
360 if(map
->array
[low
].key
< key
)
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
)
378 newsize
= (map
->maxsize
? (map
->maxsize
<<1) : 4);
379 if(newsize
>= map
->maxsize
)
380 temp
= realloc(map
->array
, newsize
*sizeof(map
->array
[0]));
383 WriteUnlock(&map
->lock
);
384 return AL_OUT_OF_MEMORY
;
387 map
->maxsize
= newsize
;
391 memmove(&map
->array
[pos
+1], &map
->array
[pos
],
392 (map
->size
-pos
)*sizeof(map
->array
[0]));
395 map
->array
[pos
].key
= key
;
396 map
->array
[pos
].value
= value
;
397 WriteUnlock(&map
->lock
);
402 ALvoid
*RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
405 WriteLock(&map
->lock
);
409 ALsizei high
= map
->size
- 1;
412 ALsizei mid
= low
+ (high
-low
)/2;
413 if(map
->array
[mid
].key
< key
)
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]));
427 WriteUnlock(&map
->lock
);
431 ALvoid
*LookupUIntMapKey(UIntMap
*map
, ALuint key
)
434 ReadLock(&map
->lock
);
438 ALsizei high
= map
->size
- 1;
441 ALsizei mid
= low
+ (high
-low
)/2;
442 if(map
->array
[mid
].key
< key
)
447 if(map
->array
[low
].key
== key
)
448 ptr
= map
->array
[low
].value
;
450 ReadUnlock(&map
->lock
);