Fix a tiny memory-leak when calling vfork
[ardour.git] / libs / ardouralsautil / deviceparams.cc
1 /*
2  * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2013 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <alsa/asoundlib.h>
21 #include "pbd/convert.h"
22 #include "ardouralsautil/deviceinfo.h"
23
24 using namespace std;
25
26 int
27 ARDOUR::get_alsa_device_parameters (const char* device_name, const bool play, ALSADeviceInfo *nfo)
28 {
29         snd_pcm_t *pcm;
30         snd_pcm_hw_params_t *hw_params;
31         std::string errmsg;
32         int err;
33
34         nfo->valid = false;
35
36         err = snd_pcm_open (&pcm, device_name,
37                         play ? SND_PCM_STREAM_PLAYBACK : SND_PCM_STREAM_CAPTURE,
38                         SND_PCM_NONBLOCK);
39
40         if (err < 0) {
41                 fprintf (stderr, "ALSA: Cannot open device '%s': %s\n", device_name, snd_strerror (err));
42                 return 1;
43         }
44
45         snd_pcm_hw_params_alloca (&hw_params);
46         err = snd_pcm_hw_params_any (pcm, hw_params);
47         if (err < 0) {
48                 errmsg = "Cannot get hardware parameters";
49                 goto error_out;
50         }
51
52         err = snd_pcm_hw_params_get_channels_max (hw_params, &nfo->max_channels);
53         if (err < 0) {
54                 errmsg = "Cannot get maximum channels count";
55                 goto error_out;
56         }
57
58         err = snd_pcm_hw_params_get_rate_min (hw_params, &nfo->min_rate, NULL);
59         if (err < 0) {
60                 errmsg = "Cannot get minimum rate";
61                 goto error_out;
62         }
63         err = snd_pcm_hw_params_get_rate_max (hw_params, &nfo->max_rate, NULL);
64         if (err < 0) {
65                 errmsg = "Cannot get maximum rate";
66                 goto error_out;
67         }
68
69         err = snd_pcm_hw_params_get_buffer_size_min (hw_params, &nfo->min_size);
70         if (err < 0) {
71                 errmsg = "Cannot get minimum buffer size";
72                 goto error_out;
73         }
74         err = snd_pcm_hw_params_get_buffer_size_max (hw_params, &nfo->max_size);
75         if (err < 0) {
76                 errmsg = "Cannot get maximum buffer size";
77                 goto error_out;
78         }
79         snd_pcm_close (pcm);
80         nfo->valid = true;
81         return 0;
82
83 error_out:
84         fprintf (stderr, "ALSA: %s: %s\n", errmsg.c_str(), snd_strerror (err));
85         snd_pcm_close (pcm);
86         return 1;
87
88 }