audio_legacy.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. * QEMU Audio subsystem: legacy configuration handling
  3. *
  4. * Copyright (c) 2015-2019 Zoltán Kővágó <DirtY.iCE.hu@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "audio.h"
  26. #include "audio_int.h"
  27. #include "qemu/cutils.h"
  28. #include "qemu/timer.h"
  29. #include "qapi/error.h"
  30. #include "qapi/qapi-visit-audio.h"
  31. #include "qapi/visitor-impl.h"
  32. #define AUDIO_CAP "audio-legacy"
  33. #include "audio_int.h"
  34. static uint32_t toui32(const char *str)
  35. {
  36. unsigned long long ret;
  37. if (parse_uint_full(str, &ret, 10) || ret > UINT32_MAX) {
  38. dolog("Invalid integer value `%s'\n", str);
  39. exit(1);
  40. }
  41. return ret;
  42. }
  43. /* helper functions to convert env variables */
  44. static void get_bool(const char *env, bool *dst, bool *has_dst)
  45. {
  46. const char *val = getenv(env);
  47. if (val) {
  48. *dst = toui32(val) != 0;
  49. *has_dst = true;
  50. }
  51. }
  52. static void get_int(const char *env, uint32_t *dst, bool *has_dst)
  53. {
  54. const char *val = getenv(env);
  55. if (val) {
  56. *dst = toui32(val);
  57. *has_dst = true;
  58. }
  59. }
  60. static void get_str(const char *env, char **dst)
  61. {
  62. const char *val = getenv(env);
  63. if (val) {
  64. g_free(*dst);
  65. *dst = g_strdup(val);
  66. }
  67. }
  68. static void get_fmt(const char *env, AudioFormat *dst, bool *has_dst)
  69. {
  70. const char *val = getenv(env);
  71. if (val) {
  72. size_t i;
  73. for (i = 0; AudioFormat_lookup.size; ++i) {
  74. if (strcasecmp(val, AudioFormat_lookup.array[i]) == 0) {
  75. *dst = i;
  76. *has_dst = true;
  77. return;
  78. }
  79. }
  80. dolog("Invalid audio format `%s'\n", val);
  81. exit(1);
  82. }
  83. }
  84. #if defined(CONFIG_AUDIO_ALSA) || defined(CONFIG_AUDIO_DSOUND)
  85. static void get_millis_to_usecs(const char *env, uint32_t *dst, bool *has_dst)
  86. {
  87. const char *val = getenv(env);
  88. if (val) {
  89. *dst = toui32(val) * 1000;
  90. *has_dst = true;
  91. }
  92. }
  93. #endif
  94. #if defined(CONFIG_AUDIO_ALSA) || defined(CONFIG_AUDIO_COREAUDIO) || \
  95. defined(CONFIG_AUDIO_PA) || defined(CONFIG_AUDIO_SDL) || \
  96. defined(CONFIG_AUDIO_DSOUND) || defined(CONFIG_AUDIO_OSS)
  97. static uint32_t frames_to_usecs(uint32_t frames,
  98. AudiodevPerDirectionOptions *pdo)
  99. {
  100. uint32_t freq = pdo->has_frequency ? pdo->frequency : 44100;
  101. return (frames * 1000000 + freq / 2) / freq;
  102. }
  103. #endif
  104. #ifdef CONFIG_AUDIO_COREAUDIO
  105. static void get_frames_to_usecs(const char *env, uint32_t *dst, bool *has_dst,
  106. AudiodevPerDirectionOptions *pdo)
  107. {
  108. const char *val = getenv(env);
  109. if (val) {
  110. *dst = frames_to_usecs(toui32(val), pdo);
  111. *has_dst = true;
  112. }
  113. }
  114. #endif
  115. #if defined(CONFIG_AUDIO_PA) || defined(CONFIG_AUDIO_SDL) || \
  116. defined(CONFIG_AUDIO_DSOUND) || defined(CONFIG_AUDIO_OSS)
  117. static uint32_t samples_to_usecs(uint32_t samples,
  118. AudiodevPerDirectionOptions *pdo)
  119. {
  120. uint32_t channels = pdo->has_channels ? pdo->channels : 2;
  121. return frames_to_usecs(samples / channels, pdo);
  122. }
  123. #endif
  124. #if defined(CONFIG_AUDIO_PA) || defined(CONFIG_AUDIO_SDL)
  125. static void get_samples_to_usecs(const char *env, uint32_t *dst, bool *has_dst,
  126. AudiodevPerDirectionOptions *pdo)
  127. {
  128. const char *val = getenv(env);
  129. if (val) {
  130. *dst = samples_to_usecs(toui32(val), pdo);
  131. *has_dst = true;
  132. }
  133. }
  134. #endif
  135. #if defined(CONFIG_AUDIO_DSOUND) || defined(CONFIG_AUDIO_OSS)
  136. static uint32_t bytes_to_usecs(uint32_t bytes, AudiodevPerDirectionOptions *pdo)
  137. {
  138. AudioFormat fmt = pdo->has_format ? pdo->format : AUDIO_FORMAT_S16;
  139. uint32_t bytes_per_sample = audioformat_bytes_per_sample(fmt);
  140. return samples_to_usecs(bytes / bytes_per_sample, pdo);
  141. }
  142. static void get_bytes_to_usecs(const char *env, uint32_t *dst, bool *has_dst,
  143. AudiodevPerDirectionOptions *pdo)
  144. {
  145. const char *val = getenv(env);
  146. if (val) {
  147. *dst = bytes_to_usecs(toui32(val), pdo);
  148. *has_dst = true;
  149. }
  150. }
  151. #endif
  152. /* backend specific functions */
  153. #ifdef CONFIG_AUDIO_ALSA
  154. /* ALSA */
  155. static void handle_alsa_per_direction(
  156. AudiodevAlsaPerDirectionOptions *apdo, const char *prefix)
  157. {
  158. char buf[64];
  159. size_t len = strlen(prefix);
  160. bool size_in_usecs = false;
  161. bool dummy;
  162. memcpy(buf, prefix, len);
  163. strcpy(buf + len, "TRY_POLL");
  164. get_bool(buf, &apdo->try_poll, &apdo->has_try_poll);
  165. strcpy(buf + len, "DEV");
  166. get_str(buf, &apdo->dev);
  167. strcpy(buf + len, "SIZE_IN_USEC");
  168. get_bool(buf, &size_in_usecs, &dummy);
  169. strcpy(buf + len, "PERIOD_SIZE");
  170. get_int(buf, &apdo->period_length, &apdo->has_period_length);
  171. if (apdo->has_period_length && !size_in_usecs) {
  172. apdo->period_length = frames_to_usecs(
  173. apdo->period_length,
  174. qapi_AudiodevAlsaPerDirectionOptions_base(apdo));
  175. }
  176. strcpy(buf + len, "BUFFER_SIZE");
  177. get_int(buf, &apdo->buffer_length, &apdo->has_buffer_length);
  178. if (apdo->has_buffer_length && !size_in_usecs) {
  179. apdo->buffer_length = frames_to_usecs(
  180. apdo->buffer_length,
  181. qapi_AudiodevAlsaPerDirectionOptions_base(apdo));
  182. }
  183. }
  184. static void handle_alsa(Audiodev *dev)
  185. {
  186. AudiodevAlsaOptions *aopt = &dev->u.alsa;
  187. handle_alsa_per_direction(aopt->in, "QEMU_ALSA_ADC_");
  188. handle_alsa_per_direction(aopt->out, "QEMU_ALSA_DAC_");
  189. get_millis_to_usecs("QEMU_ALSA_THRESHOLD",
  190. &aopt->threshold, &aopt->has_threshold);
  191. }
  192. #endif
  193. #ifdef CONFIG_AUDIO_COREAUDIO
  194. /* coreaudio */
  195. static void handle_coreaudio(Audiodev *dev)
  196. {
  197. get_frames_to_usecs(
  198. "QEMU_COREAUDIO_BUFFER_SIZE",
  199. &dev->u.coreaudio.out->buffer_length,
  200. &dev->u.coreaudio.out->has_buffer_length,
  201. qapi_AudiodevCoreaudioPerDirectionOptions_base(dev->u.coreaudio.out));
  202. get_int("QEMU_COREAUDIO_BUFFER_COUNT",
  203. &dev->u.coreaudio.out->buffer_count,
  204. &dev->u.coreaudio.out->has_buffer_count);
  205. }
  206. #endif
  207. #ifdef CONFIG_AUDIO_DSOUND
  208. /* dsound */
  209. static void handle_dsound(Audiodev *dev)
  210. {
  211. get_millis_to_usecs("QEMU_DSOUND_LATENCY_MILLIS",
  212. &dev->u.dsound.latency, &dev->u.dsound.has_latency);
  213. get_bytes_to_usecs("QEMU_DSOUND_BUFSIZE_OUT",
  214. &dev->u.dsound.out->buffer_length,
  215. &dev->u.dsound.out->has_buffer_length,
  216. dev->u.dsound.out);
  217. get_bytes_to_usecs("QEMU_DSOUND_BUFSIZE_IN",
  218. &dev->u.dsound.in->buffer_length,
  219. &dev->u.dsound.in->has_buffer_length,
  220. dev->u.dsound.in);
  221. }
  222. #endif
  223. #ifdef CONFIG_AUDIO_OSS
  224. /* OSS */
  225. static void handle_oss_per_direction(
  226. AudiodevOssPerDirectionOptions *opdo, const char *try_poll_env,
  227. const char *dev_env)
  228. {
  229. get_bool(try_poll_env, &opdo->try_poll, &opdo->has_try_poll);
  230. get_str(dev_env, &opdo->dev);
  231. get_bytes_to_usecs("QEMU_OSS_FRAGSIZE",
  232. &opdo->buffer_length, &opdo->has_buffer_length,
  233. qapi_AudiodevOssPerDirectionOptions_base(opdo));
  234. get_int("QEMU_OSS_NFRAGS", &opdo->buffer_count,
  235. &opdo->has_buffer_count);
  236. }
  237. static void handle_oss(Audiodev *dev)
  238. {
  239. AudiodevOssOptions *oopt = &dev->u.oss;
  240. handle_oss_per_direction(oopt->in, "QEMU_AUDIO_ADC_TRY_POLL",
  241. "QEMU_OSS_ADC_DEV");
  242. handle_oss_per_direction(oopt->out, "QEMU_AUDIO_DAC_TRY_POLL",
  243. "QEMU_OSS_DAC_DEV");
  244. get_bool("QEMU_OSS_MMAP", &oopt->try_mmap, &oopt->has_try_mmap);
  245. get_bool("QEMU_OSS_EXCLUSIVE", &oopt->exclusive, &oopt->has_exclusive);
  246. get_int("QEMU_OSS_POLICY", &oopt->dsp_policy, &oopt->has_dsp_policy);
  247. }
  248. #endif
  249. #ifdef CONFIG_AUDIO_PA
  250. /* pulseaudio */
  251. static void handle_pa_per_direction(
  252. AudiodevPaPerDirectionOptions *ppdo, const char *env)
  253. {
  254. get_str(env, &ppdo->name);
  255. }
  256. static void handle_pa(Audiodev *dev)
  257. {
  258. handle_pa_per_direction(dev->u.pa.in, "QEMU_PA_SOURCE");
  259. handle_pa_per_direction(dev->u.pa.out, "QEMU_PA_SINK");
  260. get_samples_to_usecs(
  261. "QEMU_PA_SAMPLES", &dev->u.pa.in->buffer_length,
  262. &dev->u.pa.in->has_buffer_length,
  263. qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.in));
  264. get_samples_to_usecs(
  265. "QEMU_PA_SAMPLES", &dev->u.pa.out->buffer_length,
  266. &dev->u.pa.out->has_buffer_length,
  267. qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.out));
  268. get_str("QEMU_PA_SERVER", &dev->u.pa.server);
  269. }
  270. #endif
  271. #ifdef CONFIG_AUDIO_SDL
  272. /* SDL */
  273. static void handle_sdl(Audiodev *dev)
  274. {
  275. /* SDL is output only */
  276. get_samples_to_usecs("QEMU_SDL_SAMPLES", &dev->u.sdl.out->buffer_length,
  277. &dev->u.sdl.out->has_buffer_length,
  278. qapi_AudiodevSdlPerDirectionOptions_base(dev->u.sdl.out));
  279. }
  280. #endif
  281. /* wav */
  282. static void handle_wav(Audiodev *dev)
  283. {
  284. get_int("QEMU_WAV_FREQUENCY",
  285. &dev->u.wav.out->frequency, &dev->u.wav.out->has_frequency);
  286. get_fmt("QEMU_WAV_FORMAT", &dev->u.wav.out->format,
  287. &dev->u.wav.out->has_format);
  288. get_int("QEMU_WAV_DAC_FIXED_CHANNELS",
  289. &dev->u.wav.out->channels, &dev->u.wav.out->has_channels);
  290. get_str("QEMU_WAV_PATH", &dev->u.wav.path);
  291. }
  292. /* general */
  293. static void handle_per_direction(
  294. AudiodevPerDirectionOptions *pdo, const char *prefix)
  295. {
  296. char buf[64];
  297. size_t len = strlen(prefix);
  298. memcpy(buf, prefix, len);
  299. strcpy(buf + len, "FIXED_SETTINGS");
  300. get_bool(buf, &pdo->fixed_settings, &pdo->has_fixed_settings);
  301. strcpy(buf + len, "FIXED_FREQ");
  302. get_int(buf, &pdo->frequency, &pdo->has_frequency);
  303. strcpy(buf + len, "FIXED_FMT");
  304. get_fmt(buf, &pdo->format, &pdo->has_format);
  305. strcpy(buf + len, "FIXED_CHANNELS");
  306. get_int(buf, &pdo->channels, &pdo->has_channels);
  307. strcpy(buf + len, "VOICES");
  308. get_int(buf, &pdo->voices, &pdo->has_voices);
  309. }
  310. static AudiodevListEntry *legacy_opt(const char *drvname)
  311. {
  312. AudiodevListEntry *e = g_new0(AudiodevListEntry, 1);
  313. e->dev = g_new0(Audiodev, 1);
  314. e->dev->id = g_strdup(drvname);
  315. e->dev->driver = qapi_enum_parse(
  316. &AudiodevDriver_lookup, drvname, -1, &error_abort);
  317. audio_create_pdos(e->dev);
  318. handle_per_direction(audio_get_pdo_in(e->dev), "QEMU_AUDIO_ADC_");
  319. handle_per_direction(audio_get_pdo_out(e->dev), "QEMU_AUDIO_DAC_");
  320. /* Original description: Timer period in HZ (0 - use lowest possible) */
  321. get_int("QEMU_AUDIO_TIMER_PERIOD",
  322. &e->dev->timer_period, &e->dev->has_timer_period);
  323. if (e->dev->has_timer_period && e->dev->timer_period) {
  324. e->dev->timer_period = NANOSECONDS_PER_SECOND / 1000 /
  325. e->dev->timer_period;
  326. }
  327. switch (e->dev->driver) {
  328. #ifdef CONFIG_AUDIO_ALSA
  329. case AUDIODEV_DRIVER_ALSA:
  330. handle_alsa(e->dev);
  331. break;
  332. #endif
  333. #ifdef CONFIG_AUDIO_COREAUDIO
  334. case AUDIODEV_DRIVER_COREAUDIO:
  335. handle_coreaudio(e->dev);
  336. break;
  337. #endif
  338. #ifdef CONFIG_AUDIO_DSOUND
  339. case AUDIODEV_DRIVER_DSOUND:
  340. handle_dsound(e->dev);
  341. break;
  342. #endif
  343. #ifdef CONFIG_AUDIO_OSS
  344. case AUDIODEV_DRIVER_OSS:
  345. handle_oss(e->dev);
  346. break;
  347. #endif
  348. #ifdef CONFIG_AUDIO_PA
  349. case AUDIODEV_DRIVER_PA:
  350. handle_pa(e->dev);
  351. break;
  352. #endif
  353. #ifdef CONFIG_AUDIO_SDL
  354. case AUDIODEV_DRIVER_SDL:
  355. handle_sdl(e->dev);
  356. break;
  357. #endif
  358. case AUDIODEV_DRIVER_WAV:
  359. handle_wav(e->dev);
  360. break;
  361. default:
  362. break;
  363. }
  364. return e;
  365. }
  366. AudiodevListHead audio_handle_legacy_opts(void)
  367. {
  368. const char *drvname = getenv("QEMU_AUDIO_DRV");
  369. AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head);
  370. if (drvname) {
  371. AudiodevListEntry *e;
  372. audio_driver *driver = audio_driver_lookup(drvname);
  373. if (!driver) {
  374. dolog("Unknown audio driver `%s'\n", drvname);
  375. exit(1);
  376. }
  377. e = legacy_opt(drvname);
  378. QSIMPLEQ_INSERT_TAIL(&head, e, next);
  379. } else {
  380. for (int i = 0; audio_prio_list[i]; i++) {
  381. audio_driver *driver = audio_driver_lookup(audio_prio_list[i]);
  382. if (driver && driver->can_be_default) {
  383. AudiodevListEntry *e = legacy_opt(driver->name);
  384. QSIMPLEQ_INSERT_TAIL(&head, e, next);
  385. }
  386. }
  387. if (QSIMPLEQ_EMPTY(&head)) {
  388. dolog("Internal error: no default audio driver available\n");
  389. exit(1);
  390. }
  391. }
  392. return head;
  393. }
  394. /* visitor to print -audiodev option */
  395. typedef struct {
  396. Visitor visitor;
  397. bool comma;
  398. GList *path;
  399. } LegacyPrintVisitor;
  400. static bool lv_start_struct(Visitor *v, const char *name, void **obj,
  401. size_t size, Error **errp)
  402. {
  403. LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
  404. lv->path = g_list_append(lv->path, g_strdup(name));
  405. return true;
  406. }
  407. static void lv_end_struct(Visitor *v, void **obj)
  408. {
  409. LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
  410. lv->path = g_list_delete_link(lv->path, g_list_last(lv->path));
  411. }
  412. static void lv_print_key(Visitor *v, const char *name)
  413. {
  414. GList *e;
  415. LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
  416. if (lv->comma) {
  417. putchar(',');
  418. } else {
  419. lv->comma = true;
  420. }
  421. for (e = lv->path; e; e = e->next) {
  422. if (e->data) {
  423. printf("%s.", (const char *) e->data);
  424. }
  425. }
  426. printf("%s=", name);
  427. }
  428. static bool lv_type_int64(Visitor *v, const char *name, int64_t *obj,
  429. Error **errp)
  430. {
  431. lv_print_key(v, name);
  432. printf("%" PRIi64, *obj);
  433. return true;
  434. }
  435. static bool lv_type_uint64(Visitor *v, const char *name, uint64_t *obj,
  436. Error **errp)
  437. {
  438. lv_print_key(v, name);
  439. printf("%" PRIu64, *obj);
  440. return true;
  441. }
  442. static bool lv_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
  443. {
  444. lv_print_key(v, name);
  445. printf("%s", *obj ? "on" : "off");
  446. return true;
  447. }
  448. static bool lv_type_str(Visitor *v, const char *name, char **obj, Error **errp)
  449. {
  450. const char *str = *obj;
  451. lv_print_key(v, name);
  452. while (*str) {
  453. if (*str == ',') {
  454. putchar(',');
  455. }
  456. putchar(*str++);
  457. }
  458. return true;
  459. }
  460. static void lv_complete(Visitor *v, void *opaque)
  461. {
  462. LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
  463. assert(lv->path == NULL);
  464. }
  465. static void lv_free(Visitor *v)
  466. {
  467. LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
  468. g_list_free_full(lv->path, g_free);
  469. g_free(lv);
  470. }
  471. static Visitor *legacy_visitor_new(void)
  472. {
  473. LegacyPrintVisitor *lv = g_new0(LegacyPrintVisitor, 1);
  474. lv->visitor.start_struct = lv_start_struct;
  475. lv->visitor.end_struct = lv_end_struct;
  476. /* lists not supported */
  477. lv->visitor.type_int64 = lv_type_int64;
  478. lv->visitor.type_uint64 = lv_type_uint64;
  479. lv->visitor.type_bool = lv_type_bool;
  480. lv->visitor.type_str = lv_type_str;
  481. lv->visitor.type = VISITOR_OUTPUT;
  482. lv->visitor.complete = lv_complete;
  483. lv->visitor.free = lv_free;
  484. return &lv->visitor;
  485. }
  486. void audio_legacy_help(void)
  487. {
  488. AudiodevListHead head;
  489. AudiodevListEntry *e;
  490. printf("Environment variable based configuration deprecated.\n");
  491. printf("Please use the new -audiodev option.\n");
  492. head = audio_handle_legacy_opts();
  493. printf("\nEquivalent -audiodev to your current environment variables:\n");
  494. if (!getenv("QEMU_AUDIO_DRV")) {
  495. printf("(Since you didn't specify QEMU_AUDIO_DRV, I'll list all "
  496. "possibilities)\n");
  497. }
  498. QSIMPLEQ_FOREACH(e, &head, next) {
  499. Visitor *v;
  500. Audiodev *dev = e->dev;
  501. printf("-audiodev ");
  502. v = legacy_visitor_new();
  503. visit_type_Audiodev(v, NULL, &dev, &error_abort);
  504. visit_free(v);
  505. printf("\n");
  506. }
  507. audio_free_audiodev_list(&head);
  508. }