audio_legacy.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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, bool *has_dst)
  61. {
  62. const char *val = getenv(env);
  63. if (val) {
  64. if (*has_dst) {
  65. g_free(*dst);
  66. }
  67. *dst = g_strdup(val);
  68. *has_dst = true;
  69. }
  70. }
  71. static void get_fmt(const char *env, AudioFormat *dst, bool *has_dst)
  72. {
  73. const char *val = getenv(env);
  74. if (val) {
  75. size_t i;
  76. for (i = 0; AudioFormat_lookup.size; ++i) {
  77. if (strcasecmp(val, AudioFormat_lookup.array[i]) == 0) {
  78. *dst = i;
  79. *has_dst = true;
  80. return;
  81. }
  82. }
  83. dolog("Invalid audio format `%s'\n", val);
  84. exit(1);
  85. }
  86. }
  87. static void get_millis_to_usecs(const char *env, uint32_t *dst, bool *has_dst)
  88. {
  89. const char *val = getenv(env);
  90. if (val) {
  91. *dst = toui32(val) * 1000;
  92. *has_dst = true;
  93. }
  94. }
  95. static uint32_t frames_to_usecs(uint32_t frames,
  96. AudiodevPerDirectionOptions *pdo)
  97. {
  98. uint32_t freq = pdo->has_frequency ? pdo->frequency : 44100;
  99. return (frames * 1000000 + freq / 2) / freq;
  100. }
  101. static void get_frames_to_usecs(const char *env, uint32_t *dst, bool *has_dst,
  102. AudiodevPerDirectionOptions *pdo)
  103. {
  104. const char *val = getenv(env);
  105. if (val) {
  106. *dst = frames_to_usecs(toui32(val), pdo);
  107. *has_dst = true;
  108. }
  109. }
  110. static uint32_t samples_to_usecs(uint32_t samples,
  111. AudiodevPerDirectionOptions *pdo)
  112. {
  113. uint32_t channels = pdo->has_channels ? pdo->channels : 2;
  114. return frames_to_usecs(samples / channels, pdo);
  115. }
  116. static void get_samples_to_usecs(const char *env, uint32_t *dst, bool *has_dst,
  117. AudiodevPerDirectionOptions *pdo)
  118. {
  119. const char *val = getenv(env);
  120. if (val) {
  121. *dst = samples_to_usecs(toui32(val), pdo);
  122. *has_dst = true;
  123. }
  124. }
  125. static uint32_t bytes_to_usecs(uint32_t bytes, AudiodevPerDirectionOptions *pdo)
  126. {
  127. AudioFormat fmt = pdo->has_format ? pdo->format : AUDIO_FORMAT_S16;
  128. uint32_t bytes_per_sample = audioformat_bytes_per_sample(fmt);
  129. return samples_to_usecs(bytes / bytes_per_sample, pdo);
  130. }
  131. static void get_bytes_to_usecs(const char *env, uint32_t *dst, bool *has_dst,
  132. AudiodevPerDirectionOptions *pdo)
  133. {
  134. const char *val = getenv(env);
  135. if (val) {
  136. *dst = bytes_to_usecs(toui32(val), pdo);
  137. *has_dst = true;
  138. }
  139. }
  140. /* backend specific functions */
  141. /* ALSA */
  142. static void handle_alsa_per_direction(
  143. AudiodevAlsaPerDirectionOptions *apdo, const char *prefix)
  144. {
  145. char buf[64];
  146. size_t len = strlen(prefix);
  147. bool size_in_usecs = false;
  148. bool dummy;
  149. memcpy(buf, prefix, len);
  150. strcpy(buf + len, "TRY_POLL");
  151. get_bool(buf, &apdo->try_poll, &apdo->has_try_poll);
  152. strcpy(buf + len, "DEV");
  153. get_str(buf, &apdo->dev, &apdo->has_dev);
  154. strcpy(buf + len, "SIZE_IN_USEC");
  155. get_bool(buf, &size_in_usecs, &dummy);
  156. strcpy(buf + len, "PERIOD_SIZE");
  157. get_int(buf, &apdo->period_length, &apdo->has_period_length);
  158. if (apdo->has_period_length && !size_in_usecs) {
  159. apdo->period_length = frames_to_usecs(
  160. apdo->period_length,
  161. qapi_AudiodevAlsaPerDirectionOptions_base(apdo));
  162. }
  163. strcpy(buf + len, "BUFFER_SIZE");
  164. get_int(buf, &apdo->buffer_length, &apdo->has_buffer_length);
  165. if (apdo->has_buffer_length && !size_in_usecs) {
  166. apdo->buffer_length = frames_to_usecs(
  167. apdo->buffer_length,
  168. qapi_AudiodevAlsaPerDirectionOptions_base(apdo));
  169. }
  170. }
  171. static void handle_alsa(Audiodev *dev)
  172. {
  173. AudiodevAlsaOptions *aopt = &dev->u.alsa;
  174. handle_alsa_per_direction(aopt->in, "QEMU_ALSA_ADC_");
  175. handle_alsa_per_direction(aopt->out, "QEMU_ALSA_DAC_");
  176. get_millis_to_usecs("QEMU_ALSA_THRESHOLD",
  177. &aopt->threshold, &aopt->has_threshold);
  178. }
  179. /* coreaudio */
  180. static void handle_coreaudio(Audiodev *dev)
  181. {
  182. get_frames_to_usecs(
  183. "QEMU_COREAUDIO_BUFFER_SIZE",
  184. &dev->u.coreaudio.out->buffer_length,
  185. &dev->u.coreaudio.out->has_buffer_length,
  186. qapi_AudiodevCoreaudioPerDirectionOptions_base(dev->u.coreaudio.out));
  187. get_int("QEMU_COREAUDIO_BUFFER_COUNT",
  188. &dev->u.coreaudio.out->buffer_count,
  189. &dev->u.coreaudio.out->has_buffer_count);
  190. }
  191. /* dsound */
  192. static void handle_dsound(Audiodev *dev)
  193. {
  194. get_millis_to_usecs("QEMU_DSOUND_LATENCY_MILLIS",
  195. &dev->u.dsound.latency, &dev->u.dsound.has_latency);
  196. get_bytes_to_usecs("QEMU_DSOUND_BUFSIZE_OUT",
  197. &dev->u.dsound.out->buffer_length,
  198. &dev->u.dsound.out->has_buffer_length,
  199. dev->u.dsound.out);
  200. get_bytes_to_usecs("QEMU_DSOUND_BUFSIZE_IN",
  201. &dev->u.dsound.in->buffer_length,
  202. &dev->u.dsound.in->has_buffer_length,
  203. dev->u.dsound.in);
  204. }
  205. /* OSS */
  206. static void handle_oss_per_direction(
  207. AudiodevOssPerDirectionOptions *opdo, const char *try_poll_env,
  208. const char *dev_env)
  209. {
  210. get_bool(try_poll_env, &opdo->try_poll, &opdo->has_try_poll);
  211. get_str(dev_env, &opdo->dev, &opdo->has_dev);
  212. get_bytes_to_usecs("QEMU_OSS_FRAGSIZE",
  213. &opdo->buffer_length, &opdo->has_buffer_length,
  214. qapi_AudiodevOssPerDirectionOptions_base(opdo));
  215. get_int("QEMU_OSS_NFRAGS", &opdo->buffer_count,
  216. &opdo->has_buffer_count);
  217. }
  218. static void handle_oss(Audiodev *dev)
  219. {
  220. AudiodevOssOptions *oopt = &dev->u.oss;
  221. handle_oss_per_direction(oopt->in, "QEMU_AUDIO_ADC_TRY_POLL",
  222. "QEMU_OSS_ADC_DEV");
  223. handle_oss_per_direction(oopt->out, "QEMU_AUDIO_DAC_TRY_POLL",
  224. "QEMU_OSS_DAC_DEV");
  225. get_bool("QEMU_OSS_MMAP", &oopt->try_mmap, &oopt->has_try_mmap);
  226. get_bool("QEMU_OSS_EXCLUSIVE", &oopt->exclusive, &oopt->has_exclusive);
  227. get_int("QEMU_OSS_POLICY", &oopt->dsp_policy, &oopt->has_dsp_policy);
  228. }
  229. /* pulseaudio */
  230. static void handle_pa_per_direction(
  231. AudiodevPaPerDirectionOptions *ppdo, const char *env)
  232. {
  233. get_str(env, &ppdo->name, &ppdo->has_name);
  234. }
  235. static void handle_pa(Audiodev *dev)
  236. {
  237. handle_pa_per_direction(dev->u.pa.in, "QEMU_PA_SOURCE");
  238. handle_pa_per_direction(dev->u.pa.out, "QEMU_PA_SINK");
  239. get_samples_to_usecs(
  240. "QEMU_PA_SAMPLES", &dev->u.pa.in->buffer_length,
  241. &dev->u.pa.in->has_buffer_length,
  242. qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.in));
  243. get_samples_to_usecs(
  244. "QEMU_PA_SAMPLES", &dev->u.pa.out->buffer_length,
  245. &dev->u.pa.out->has_buffer_length,
  246. qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.out));
  247. get_str("QEMU_PA_SERVER", &dev->u.pa.server, &dev->u.pa.has_server);
  248. }
  249. /* SDL */
  250. static void handle_sdl(Audiodev *dev)
  251. {
  252. /* SDL is output only */
  253. get_samples_to_usecs("QEMU_SDL_SAMPLES", &dev->u.sdl.out->buffer_length,
  254. &dev->u.sdl.out->has_buffer_length, dev->u.sdl.out);
  255. }
  256. /* wav */
  257. static void handle_wav(Audiodev *dev)
  258. {
  259. get_int("QEMU_WAV_FREQUENCY",
  260. &dev->u.wav.out->frequency, &dev->u.wav.out->has_frequency);
  261. get_fmt("QEMU_WAV_FORMAT", &dev->u.wav.out->format,
  262. &dev->u.wav.out->has_format);
  263. get_int("QEMU_WAV_DAC_FIXED_CHANNELS",
  264. &dev->u.wav.out->channels, &dev->u.wav.out->has_channels);
  265. get_str("QEMU_WAV_PATH", &dev->u.wav.path, &dev->u.wav.has_path);
  266. }
  267. /* general */
  268. static void handle_per_direction(
  269. AudiodevPerDirectionOptions *pdo, const char *prefix)
  270. {
  271. char buf[64];
  272. size_t len = strlen(prefix);
  273. memcpy(buf, prefix, len);
  274. strcpy(buf + len, "FIXED_SETTINGS");
  275. get_bool(buf, &pdo->fixed_settings, &pdo->has_fixed_settings);
  276. strcpy(buf + len, "FIXED_FREQ");
  277. get_int(buf, &pdo->frequency, &pdo->has_frequency);
  278. strcpy(buf + len, "FIXED_FMT");
  279. get_fmt(buf, &pdo->format, &pdo->has_format);
  280. strcpy(buf + len, "FIXED_CHANNELS");
  281. get_int(buf, &pdo->channels, &pdo->has_channels);
  282. strcpy(buf + len, "VOICES");
  283. get_int(buf, &pdo->voices, &pdo->has_voices);
  284. }
  285. static AudiodevListEntry *legacy_opt(const char *drvname)
  286. {
  287. AudiodevListEntry *e = g_malloc0(sizeof(AudiodevListEntry));
  288. e->dev = g_malloc0(sizeof(Audiodev));
  289. e->dev->id = g_strdup(drvname);
  290. e->dev->driver = qapi_enum_parse(
  291. &AudiodevDriver_lookup, drvname, -1, &error_abort);
  292. audio_create_pdos(e->dev);
  293. handle_per_direction(audio_get_pdo_in(e->dev), "QEMU_AUDIO_ADC_");
  294. handle_per_direction(audio_get_pdo_out(e->dev), "QEMU_AUDIO_DAC_");
  295. /* Original description: Timer period in HZ (0 - use lowest possible) */
  296. get_int("QEMU_AUDIO_TIMER_PERIOD",
  297. &e->dev->timer_period, &e->dev->has_timer_period);
  298. if (e->dev->has_timer_period && e->dev->timer_period) {
  299. e->dev->timer_period = NANOSECONDS_PER_SECOND / 1000 /
  300. e->dev->timer_period;
  301. }
  302. switch (e->dev->driver) {
  303. case AUDIODEV_DRIVER_ALSA:
  304. handle_alsa(e->dev);
  305. break;
  306. case AUDIODEV_DRIVER_COREAUDIO:
  307. handle_coreaudio(e->dev);
  308. break;
  309. case AUDIODEV_DRIVER_DSOUND:
  310. handle_dsound(e->dev);
  311. break;
  312. case AUDIODEV_DRIVER_OSS:
  313. handle_oss(e->dev);
  314. break;
  315. case AUDIODEV_DRIVER_PA:
  316. handle_pa(e->dev);
  317. break;
  318. case AUDIODEV_DRIVER_SDL:
  319. handle_sdl(e->dev);
  320. break;
  321. case AUDIODEV_DRIVER_WAV:
  322. handle_wav(e->dev);
  323. break;
  324. default:
  325. break;
  326. }
  327. return e;
  328. }
  329. AudiodevListHead audio_handle_legacy_opts(void)
  330. {
  331. const char *drvname = getenv("QEMU_AUDIO_DRV");
  332. AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head);
  333. if (drvname) {
  334. AudiodevListEntry *e;
  335. audio_driver *driver = audio_driver_lookup(drvname);
  336. if (!driver) {
  337. dolog("Unknown audio driver `%s'\n", drvname);
  338. exit(1);
  339. }
  340. e = legacy_opt(drvname);
  341. QSIMPLEQ_INSERT_TAIL(&head, e, next);
  342. } else {
  343. for (int i = 0; audio_prio_list[i]; i++) {
  344. audio_driver *driver = audio_driver_lookup(audio_prio_list[i]);
  345. if (driver && driver->can_be_default) {
  346. AudiodevListEntry *e = legacy_opt(driver->name);
  347. QSIMPLEQ_INSERT_TAIL(&head, e, next);
  348. }
  349. }
  350. if (QSIMPLEQ_EMPTY(&head)) {
  351. dolog("Internal error: no default audio driver available\n");
  352. exit(1);
  353. }
  354. }
  355. return head;
  356. }
  357. /* visitor to print -audiodev option */
  358. typedef struct {
  359. Visitor visitor;
  360. bool comma;
  361. GList *path;
  362. } LegacyPrintVisitor;
  363. static bool lv_start_struct(Visitor *v, const char *name, void **obj,
  364. size_t size, Error **errp)
  365. {
  366. LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
  367. lv->path = g_list_append(lv->path, g_strdup(name));
  368. return true;
  369. }
  370. static void lv_end_struct(Visitor *v, void **obj)
  371. {
  372. LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
  373. lv->path = g_list_delete_link(lv->path, g_list_last(lv->path));
  374. }
  375. static void lv_print_key(Visitor *v, const char *name)
  376. {
  377. GList *e;
  378. LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
  379. if (lv->comma) {
  380. putchar(',');
  381. } else {
  382. lv->comma = true;
  383. }
  384. for (e = lv->path; e; e = e->next) {
  385. if (e->data) {
  386. printf("%s.", (const char *) e->data);
  387. }
  388. }
  389. printf("%s=", name);
  390. }
  391. static bool lv_type_int64(Visitor *v, const char *name, int64_t *obj,
  392. Error **errp)
  393. {
  394. lv_print_key(v, name);
  395. printf("%" PRIi64, *obj);
  396. return true;
  397. }
  398. static bool lv_type_uint64(Visitor *v, const char *name, uint64_t *obj,
  399. Error **errp)
  400. {
  401. lv_print_key(v, name);
  402. printf("%" PRIu64, *obj);
  403. return true;
  404. }
  405. static bool lv_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
  406. {
  407. lv_print_key(v, name);
  408. printf("%s", *obj ? "on" : "off");
  409. return true;
  410. }
  411. static bool lv_type_str(Visitor *v, const char *name, char **obj, Error **errp)
  412. {
  413. const char *str = *obj;
  414. lv_print_key(v, name);
  415. while (*str) {
  416. if (*str == ',') {
  417. putchar(',');
  418. }
  419. putchar(*str++);
  420. }
  421. return true;
  422. }
  423. static void lv_complete(Visitor *v, void *opaque)
  424. {
  425. LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
  426. assert(lv->path == NULL);
  427. }
  428. static void lv_free(Visitor *v)
  429. {
  430. LegacyPrintVisitor *lv = (LegacyPrintVisitor *) v;
  431. g_list_free_full(lv->path, g_free);
  432. g_free(lv);
  433. }
  434. static Visitor *legacy_visitor_new(void)
  435. {
  436. LegacyPrintVisitor *lv = g_malloc0(sizeof(LegacyPrintVisitor));
  437. lv->visitor.start_struct = lv_start_struct;
  438. lv->visitor.end_struct = lv_end_struct;
  439. /* lists not supported */
  440. lv->visitor.type_int64 = lv_type_int64;
  441. lv->visitor.type_uint64 = lv_type_uint64;
  442. lv->visitor.type_bool = lv_type_bool;
  443. lv->visitor.type_str = lv_type_str;
  444. lv->visitor.type = VISITOR_OUTPUT;
  445. lv->visitor.complete = lv_complete;
  446. lv->visitor.free = lv_free;
  447. return &lv->visitor;
  448. }
  449. void audio_legacy_help(void)
  450. {
  451. AudiodevListHead head;
  452. AudiodevListEntry *e;
  453. printf("Environment variable based configuration deprecated.\n");
  454. printf("Please use the new -audiodev option.\n");
  455. head = audio_handle_legacy_opts();
  456. printf("\nEquivalent -audiodev to your current environment variables:\n");
  457. if (!getenv("QEMU_AUDIO_DRV")) {
  458. printf("(Since you didn't specify QEMU_AUDIO_DRV, I'll list all "
  459. "possibilities)\n");
  460. }
  461. QSIMPLEQ_FOREACH(e, &head, next) {
  462. Visitor *v;
  463. Audiodev *dev = e->dev;
  464. printf("-audiodev ");
  465. v = legacy_visitor_new();
  466. visit_type_Audiodev(v, NULL, &dev, &error_abort);
  467. visit_free(v);
  468. printf("\n");
  469. }
  470. audio_free_audiodev_list(&head);
  471. }