SDL_esdaudio.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_AUDIO_DRIVER_ESD
  20. /* Allow access to an ESD network stream mixing buffer */
  21. #include <sys/types.h>
  22. #include <unistd.h>
  23. #include <signal.h>
  24. #include <errno.h>
  25. #include <esd.h>
  26. #include "SDL_timer.h"
  27. #include "SDL_audio.h"
  28. #include "../SDL_audio_c.h"
  29. #include "SDL_esdaudio.h"
  30. #ifdef SDL_AUDIO_DRIVER_ESD_DYNAMIC
  31. #include "SDL_name.h"
  32. #include "SDL_loadso.h"
  33. #else
  34. #define SDL_NAME(X) X
  35. #endif
  36. #ifdef SDL_AUDIO_DRIVER_ESD_DYNAMIC
  37. static const char *esd_library = SDL_AUDIO_DRIVER_ESD_DYNAMIC;
  38. static void *esd_handle = NULL;
  39. static int (*SDL_NAME(esd_open_sound)) (const char *host);
  40. static int (*SDL_NAME(esd_close)) (int esd);
  41. static int (*SDL_NAME(esd_play_stream)) (esd_format_t format, int rate,
  42. const char *host, const char *name);
  43. #define SDL_ESD_SYM(x) { #x, (void **) (char *) &SDL_NAME(x) }
  44. static struct
  45. {
  46. const char *name;
  47. void **func;
  48. } const esd_functions[] = {
  49. SDL_ESD_SYM(esd_open_sound),
  50. SDL_ESD_SYM(esd_close), SDL_ESD_SYM(esd_play_stream),
  51. };
  52. #undef SDL_ESD_SYM
  53. static void
  54. UnloadESDLibrary()
  55. {
  56. if (esd_handle != NULL) {
  57. SDL_UnloadObject(esd_handle);
  58. esd_handle = NULL;
  59. }
  60. }
  61. static int
  62. LoadESDLibrary(void)
  63. {
  64. int i, retval = -1;
  65. if (esd_handle == NULL) {
  66. esd_handle = SDL_LoadObject(esd_library);
  67. if (esd_handle) {
  68. retval = 0;
  69. for (i = 0; i < SDL_arraysize(esd_functions); ++i) {
  70. *esd_functions[i].func =
  71. SDL_LoadFunction(esd_handle, esd_functions[i].name);
  72. if (!*esd_functions[i].func) {
  73. retval = -1;
  74. UnloadESDLibrary();
  75. break;
  76. }
  77. }
  78. }
  79. }
  80. return retval;
  81. }
  82. #else
  83. static void
  84. UnloadESDLibrary()
  85. {
  86. return;
  87. }
  88. static int
  89. LoadESDLibrary(void)
  90. {
  91. return 0;
  92. }
  93. #endif /* SDL_AUDIO_DRIVER_ESD_DYNAMIC */
  94. /* This function waits until it is possible to write a full sound buffer */
  95. static void
  96. ESD_WaitDevice(_THIS)
  97. {
  98. Sint32 ticks;
  99. /* Check to see if the thread-parent process is still alive */
  100. {
  101. static int cnt = 0;
  102. /* Note that this only works with thread implementations
  103. that use a different process id for each thread.
  104. */
  105. /* Check every 10 loops */
  106. if (this->hidden->parent && (((++cnt) % 10) == 0)) {
  107. if (kill(this->hidden->parent, 0) < 0 && errno == ESRCH) {
  108. SDL_OpenedAudioDeviceDisconnected(this);
  109. }
  110. }
  111. }
  112. /* Use timer for general audio synchronization */
  113. ticks = ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS;
  114. if (ticks > 0) {
  115. SDL_Delay(ticks);
  116. }
  117. }
  118. static void
  119. ESD_PlayDevice(_THIS)
  120. {
  121. int written = 0;
  122. /* Write the audio data, checking for EAGAIN on broken audio drivers */
  123. do {
  124. written = write(this->hidden->audio_fd,
  125. this->hidden->mixbuf, this->hidden->mixlen);
  126. if ((written < 0) && ((errno == 0) || (errno == EAGAIN))) {
  127. SDL_Delay(1); /* Let a little CPU time go by */
  128. }
  129. } while ((written < 0) &&
  130. ((errno == 0) || (errno == EAGAIN) || (errno == EINTR)));
  131. /* Set the next write frame */
  132. this->hidden->next_frame += this->hidden->frame_ticks;
  133. /* If we couldn't write, assume fatal error for now */
  134. if (written < 0) {
  135. SDL_OpenedAudioDeviceDisconnected(this);
  136. }
  137. }
  138. static Uint8 *
  139. ESD_GetDeviceBuf(_THIS)
  140. {
  141. return (this->hidden->mixbuf);
  142. }
  143. static void
  144. ESD_CloseDevice(_THIS)
  145. {
  146. if (this->hidden->audio_fd >= 0) {
  147. SDL_NAME(esd_close) (this->hidden->audio_fd);
  148. }
  149. SDL_free(this->hidden->mixbuf);
  150. SDL_free(this->hidden);
  151. }
  152. /* Try to get the name of the program */
  153. static char *
  154. get_progname(void)
  155. {
  156. char *progname = NULL;
  157. #ifdef __LINUX__
  158. FILE *fp;
  159. static char temp[BUFSIZ];
  160. SDL_snprintf(temp, SDL_arraysize(temp), "/proc/%d/cmdline", getpid());
  161. fp = fopen(temp, "r");
  162. if (fp != NULL) {
  163. if (fgets(temp, sizeof(temp) - 1, fp)) {
  164. progname = SDL_strrchr(temp, '/');
  165. if (progname == NULL) {
  166. progname = temp;
  167. } else {
  168. progname = progname + 1;
  169. }
  170. }
  171. fclose(fp);
  172. }
  173. #endif
  174. return (progname);
  175. }
  176. static int
  177. ESD_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
  178. {
  179. esd_format_t format = (ESD_STREAM | ESD_PLAY);
  180. SDL_AudioFormat test_format = 0;
  181. int found = 0;
  182. /* Initialize all variables that we clean on shutdown */
  183. this->hidden = (struct SDL_PrivateAudioData *)
  184. SDL_malloc((sizeof *this->hidden));
  185. if (this->hidden == NULL) {
  186. return SDL_OutOfMemory();
  187. }
  188. SDL_zerop(this->hidden);
  189. this->hidden->audio_fd = -1;
  190. /* Convert audio spec to the ESD audio format */
  191. /* Try for a closest match on audio format */
  192. for (test_format = SDL_FirstAudioFormat(this->spec.format);
  193. !found && test_format; test_format = SDL_NextAudioFormat()) {
  194. #ifdef DEBUG_AUDIO
  195. fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
  196. #endif
  197. found = 1;
  198. switch (test_format) {
  199. case AUDIO_U8:
  200. format |= ESD_BITS8;
  201. break;
  202. case AUDIO_S16SYS:
  203. format |= ESD_BITS16;
  204. break;
  205. default:
  206. found = 0;
  207. break;
  208. }
  209. }
  210. if (!found) {
  211. return SDL_SetError("Couldn't find any hardware audio formats");
  212. }
  213. if (this->spec.channels == 1) {
  214. format |= ESD_MONO;
  215. } else {
  216. format |= ESD_STEREO;
  217. }
  218. #if 0
  219. this->spec.samples = ESD_BUF_SIZE; /* Darn, no way to change this yet */
  220. #endif
  221. /* Open a connection to the ESD audio server */
  222. this->hidden->audio_fd =
  223. SDL_NAME(esd_play_stream) (format, this->spec.freq, NULL,
  224. get_progname());
  225. if (this->hidden->audio_fd < 0) {
  226. return SDL_SetError("Couldn't open ESD connection");
  227. }
  228. /* Calculate the final parameters for this audio specification */
  229. SDL_CalculateAudioSpec(&this->spec);
  230. this->hidden->frame_ticks =
  231. (float) (this->spec.samples * 1000) / this->spec.freq;
  232. this->hidden->next_frame = SDL_GetTicks() + this->hidden->frame_ticks;
  233. /* Allocate mixing buffer */
  234. this->hidden->mixlen = this->spec.size;
  235. this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen);
  236. if (this->hidden->mixbuf == NULL) {
  237. return SDL_OutOfMemory();
  238. }
  239. SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
  240. /* Get the parent process id (we're the parent of the audio thread) */
  241. this->hidden->parent = getpid();
  242. /* We're ready to rock and roll. :-) */
  243. return 0;
  244. }
  245. static void
  246. ESD_Deinitialize(void)
  247. {
  248. UnloadESDLibrary();
  249. }
  250. static int
  251. ESD_Init(SDL_AudioDriverImpl * impl)
  252. {
  253. if (LoadESDLibrary() < 0) {
  254. return 0;
  255. } else {
  256. int connection = 0;
  257. /* Don't start ESD if it's not running */
  258. SDL_setenv("ESD_NO_SPAWN", "1", 0);
  259. connection = SDL_NAME(esd_open_sound) (NULL);
  260. if (connection < 0) {
  261. UnloadESDLibrary();
  262. SDL_SetError("ESD: esd_open_sound failed (no audio server?)");
  263. return 0;
  264. }
  265. SDL_NAME(esd_close) (connection);
  266. }
  267. /* Set the function pointers */
  268. impl->OpenDevice = ESD_OpenDevice;
  269. impl->PlayDevice = ESD_PlayDevice;
  270. impl->WaitDevice = ESD_WaitDevice;
  271. impl->GetDeviceBuf = ESD_GetDeviceBuf;
  272. impl->CloseDevice = ESD_CloseDevice;
  273. impl->Deinitialize = ESD_Deinitialize;
  274. impl->OnlyHasDefaultOutputDevice = 1;
  275. return 1; /* this audio target is available. */
  276. }
  277. AudioBootStrap ESD_bootstrap = {
  278. "esd", "Enlightened Sound Daemon", ESD_Init, 0
  279. };
  280. #endif /* SDL_AUDIO_DRIVER_ESD */
  281. /* vi: set ts=4 sw=4 expandtab: */