SDL_netbsdaudio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  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. #ifdef SDL_AUDIO_DRIVER_NETBSD
  20. // Driver for native NetBSD audio(4).
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <sys/time.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <sys/audioio.h>
  29. #include "../../core/unix/SDL_poll.h"
  30. #include "../SDL_audiodev_c.h"
  31. #include "SDL_netbsdaudio.h"
  32. //#define DEBUG_AUDIO
  33. static void NETBSDAUDIO_DetectDevices(SDL_AudioDevice **default_playback, SDL_AudioDevice **default_recording)
  34. {
  35. SDL_EnumUnixAudioDevices(false, NULL);
  36. }
  37. static void NETBSDAUDIO_Status(SDL_AudioDevice *device)
  38. {
  39. #ifdef DEBUG_AUDIO
  40. /* *INDENT-OFF* */ // clang-format off
  41. audio_info_t info;
  42. const struct audio_prinfo *prinfo;
  43. if (ioctl(device->hidden->audio_fd, AUDIO_GETINFO, &info) < 0) {
  44. fprintf(stderr, "AUDIO_GETINFO failed.\n");
  45. return;
  46. }
  47. prinfo = device->recording ? &info.record : &info.play;
  48. fprintf(stderr, "\n"
  49. "[%s info]\n"
  50. "buffer size : %d bytes\n"
  51. "sample rate : %i Hz\n"
  52. "channels : %i\n"
  53. "precision : %i-bit\n"
  54. "encoding : 0x%x\n"
  55. "seek : %i\n"
  56. "sample count : %i\n"
  57. "EOF count : %i\n"
  58. "paused : %s\n"
  59. "error occurred : %s\n"
  60. "waiting : %s\n"
  61. "active : %s\n"
  62. "",
  63. device->recording ? "record" : "play",
  64. prinfo->buffer_size,
  65. prinfo->sample_rate,
  66. prinfo->channels,
  67. prinfo->precision,
  68. prinfo->encoding,
  69. prinfo->seek,
  70. prinfo->samples,
  71. prinfo->eof,
  72. prinfo->pause ? "yes" : "no",
  73. prinfo->error ? "yes" : "no",
  74. prinfo->waiting ? "yes" : "no",
  75. prinfo->active ? "yes" : "no");
  76. fprintf(stderr, "\n"
  77. "[audio info]\n"
  78. "monitor_gain : %i\n"
  79. "hw block size : %d bytes\n"
  80. "hi watermark : %i\n"
  81. "lo watermark : %i\n"
  82. "audio mode : %s\n"
  83. "",
  84. info.monitor_gain,
  85. info.blocksize,
  86. info.hiwat, info.lowat,
  87. (info.mode == AUMODE_PLAY) ? "PLAY"
  88. : (info.mode == AUMODE_RECORD) ? "RECORD"
  89. : (info.mode == AUMODE_PLAY_ALL ? "PLAY_ALL" : "?"));
  90. fprintf(stderr, "\n"
  91. "[audio spec]\n"
  92. "format : 0x%x\n"
  93. "size : %u\n"
  94. "",
  95. device->spec.format,
  96. device->buffer_size);
  97. /* *INDENT-ON* */ // clang-format on
  98. #endif // DEBUG_AUDIO
  99. }
  100. static bool NETBSDAUDIO_WaitDevice(SDL_AudioDevice *device)
  101. {
  102. const bool recording = device->recording;
  103. while (!SDL_AtomicGet(&device->shutdown)) {
  104. audio_info_t info;
  105. const int rc = ioctl(device->hidden->audio_fd, AUDIO_GETINFO, &info);
  106. if (rc < 0) {
  107. if (errno == EAGAIN) {
  108. continue;
  109. }
  110. // Hmm, not much we can do - abort
  111. fprintf(stderr, "netbsdaudio WaitDevice ioctl failed (unrecoverable): %s\n", strerror(errno));
  112. return false;
  113. }
  114. const size_t remain = (size_t)((recording ? info.record.seek : info.play.seek) * SDL_AUDIO_BYTESIZE(device->spec.format));
  115. if (!recording && (remain >= device->buffer_size)) {
  116. SDL_Delay(10);
  117. } else if (recording && (remain < device->buffer_size)) {
  118. SDL_Delay(10);
  119. } else {
  120. break; // ready to go!
  121. }
  122. }
  123. return true;
  124. }
  125. static bool NETBSDAUDIO_PlayDevice(SDL_AudioDevice *device, const Uint8 *buffer, int buflen)
  126. {
  127. struct SDL_PrivateAudioData *h = device->hidden;
  128. const int written = write(h->audio_fd, buffer, buflen);
  129. if (written != buflen) { // Treat even partial writes as fatal errors.
  130. return false;
  131. }
  132. #ifdef DEBUG_AUDIO
  133. fprintf(stderr, "Wrote %d bytes of audio data\n", written);
  134. #endif
  135. return true;
  136. }
  137. static Uint8 *NETBSDAUDIO_GetDeviceBuf(SDL_AudioDevice *device, int *buffer_size)
  138. {
  139. return device->hidden->mixbuf;
  140. }
  141. static int NETBSDAUDIO_RecordDevice(SDL_AudioDevice *device, void *vbuffer, int buflen)
  142. {
  143. Uint8 *buffer = (Uint8 *)vbuffer;
  144. const int br = read(device->hidden->audio_fd, buffer, buflen);
  145. if (br == -1) {
  146. // Non recoverable error has occurred. It should be reported!!!
  147. perror("audio");
  148. return -1;
  149. }
  150. #ifdef DEBUG_AUDIO
  151. fprintf(stderr, "Recorded %d bytes of audio data\n", br);
  152. #endif
  153. return br;
  154. }
  155. static void NETBSDAUDIO_FlushRecording(SDL_AudioDevice *device)
  156. {
  157. struct SDL_PrivateAudioData *h = device->hidden;
  158. audio_info_t info;
  159. if (ioctl(device->hidden->audio_fd, AUDIO_GETINFO, &info) == 0) {
  160. size_t remain = (size_t)(info.record.seek * SDL_AUDIO_BYTESIZE(device->spec.format));
  161. while (remain > 0) {
  162. char buf[512];
  163. const size_t len = SDL_min(sizeof(buf), remain);
  164. const ssize_t br = read(h->audio_fd, buf, len);
  165. if (br <= 0) {
  166. break;
  167. }
  168. remain -= br;
  169. }
  170. }
  171. }
  172. static void NETBSDAUDIO_CloseDevice(SDL_AudioDevice *device)
  173. {
  174. if (device->hidden) {
  175. if (device->hidden->audio_fd >= 0) {
  176. close(device->hidden->audio_fd);
  177. }
  178. SDL_free(device->hidden->mixbuf);
  179. SDL_free(device->hidden);
  180. device->hidden = NULL;
  181. }
  182. }
  183. static bool NETBSDAUDIO_OpenDevice(SDL_AudioDevice *device)
  184. {
  185. const bool recording = device->recording;
  186. int encoding = AUDIO_ENCODING_NONE;
  187. audio_info_t info, hwinfo;
  188. struct audio_prinfo *prinfo = recording ? &info.record : &info.play;
  189. // Initialize all variables that we clean on shutdown
  190. device->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, sizeof(*device->hidden));
  191. if (!device->hidden) {
  192. return false;
  193. }
  194. // Open the audio device; we hardcode the device path in `device->name` for lack of better info, so use that.
  195. const int flags = ((device->recording) ? O_RDONLY : O_WRONLY);
  196. device->hidden->audio_fd = open(device->name, flags | O_CLOEXEC);
  197. if (device->hidden->audio_fd < 0) {
  198. return SDL_SetError("Couldn't open %s: %s", device->name, strerror(errno));
  199. }
  200. AUDIO_INITINFO(&info);
  201. #ifdef AUDIO_GETFORMAT // Introduced in NetBSD 9.0
  202. if (ioctl(device->hidden->audio_fd, AUDIO_GETFORMAT, &hwinfo) != -1) {
  203. // Use the device's native sample rate so the kernel doesn't have to resample.
  204. device->spec.freq = recording ? hwinfo.record.sample_rate : hwinfo.play.sample_rate;
  205. }
  206. #endif
  207. prinfo->sample_rate = device->spec.freq;
  208. prinfo->channels = device->spec.channels;
  209. SDL_AudioFormat test_format;
  210. const SDL_AudioFormat *closefmts = SDL_ClosestAudioFormats(device->spec.format);
  211. while ((test_format = *(closefmts++)) != 0) {
  212. switch (test_format) {
  213. case SDL_AUDIO_U8:
  214. encoding = AUDIO_ENCODING_ULINEAR;
  215. break;
  216. case SDL_AUDIO_S8:
  217. encoding = AUDIO_ENCODING_SLINEAR;
  218. break;
  219. case SDL_AUDIO_S16LE:
  220. encoding = AUDIO_ENCODING_SLINEAR_LE;
  221. break;
  222. case SDL_AUDIO_S16BE:
  223. encoding = AUDIO_ENCODING_SLINEAR_BE;
  224. break;
  225. case SDL_AUDIO_S32LE:
  226. encoding = AUDIO_ENCODING_SLINEAR_LE;
  227. break;
  228. case SDL_AUDIO_S32BE:
  229. encoding = AUDIO_ENCODING_SLINEAR_BE;
  230. break;
  231. default:
  232. continue;
  233. }
  234. break;
  235. }
  236. if (!test_format) {
  237. return SDL_SetError("%s: Unsupported audio format", "netbsd");
  238. }
  239. prinfo->encoding = encoding;
  240. prinfo->precision = SDL_AUDIO_BITSIZE(test_format);
  241. info.hiwat = 5;
  242. info.lowat = 3;
  243. if (ioctl(device->hidden->audio_fd, AUDIO_SETINFO, &info) < 0) {
  244. return SDL_SetError("AUDIO_SETINFO failed for %s: %s", device->name, strerror(errno));
  245. }
  246. if (ioctl(device->hidden->audio_fd, AUDIO_GETINFO, &info) < 0) {
  247. return SDL_SetError("AUDIO_GETINFO failed for %s: %s", device->name, strerror(errno));
  248. }
  249. // Final spec used for the device.
  250. device->spec.format = test_format;
  251. device->spec.freq = prinfo->sample_rate;
  252. device->spec.channels = prinfo->channels;
  253. SDL_UpdatedAudioDeviceFormat(device);
  254. if (!recording) {
  255. // Allocate mixing buffer
  256. device->hidden->mixlen = device->buffer_size;
  257. device->hidden->mixbuf = (Uint8 *)SDL_malloc(device->hidden->mixlen);
  258. if (!device->hidden->mixbuf) {
  259. return false;
  260. }
  261. SDL_memset(device->hidden->mixbuf, device->silence_value, device->buffer_size);
  262. }
  263. NETBSDAUDIO_Status(device);
  264. return true; // We're ready to rock and roll. :-)
  265. }
  266. static bool NETBSDAUDIO_Init(SDL_AudioDriverImpl *impl)
  267. {
  268. impl->DetectDevices = NETBSDAUDIO_DetectDevices;
  269. impl->OpenDevice = NETBSDAUDIO_OpenDevice;
  270. impl->WaitDevice = NETBSDAUDIO_WaitDevice;
  271. impl->PlayDevice = NETBSDAUDIO_PlayDevice;
  272. impl->GetDeviceBuf = NETBSDAUDIO_GetDeviceBuf;
  273. impl->CloseDevice = NETBSDAUDIO_CloseDevice;
  274. impl->WaitRecordingDevice = NETBSDAUDIO_WaitDevice;
  275. impl->RecordDevice = NETBSDAUDIO_RecordDevice;
  276. impl->FlushRecording = NETBSDAUDIO_FlushRecording;
  277. impl->HasRecordingSupport = true;
  278. return true;
  279. }
  280. AudioBootStrap NETBSDAUDIO_bootstrap = {
  281. "netbsd", "NetBSD audio", NETBSDAUDIO_Init, false
  282. };
  283. #endif // SDL_AUDIO_DRIVER_NETBSD