summaryrefslogtreecommitdiff
path: root/src/device_alsa.cpp
blob: 00487ed16a4308d1a26518b1ba4041c07e483614 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <algorithm>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <alsa/asoundlib.h>
#include "device_alsa.h"
#include "debug.h"


namespace audiere {

  static snd_pcm_t* openDevice(const std::string &name,
			       unsigned int rate) {
    int status = 0;
    snd_pcm_t* pcm_handle = 0;

    status = snd_pcm_open(&pcm_handle, name.c_str (),
			  SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
    if (status < 0) {
      ADR_LOG("Coudn't open haredware.");
      return 0;
    }
    snd_pcm_hw_params_t* hwparams;
    snd_pcm_sw_params_t* swparams;

    snd_pcm_hw_params_alloca(&hwparams);
    snd_pcm_sw_params_alloca(&swparams);

    status = snd_pcm_hw_params_any(pcm_handle, hwparams);
    if (status < 0) {
      ADR_LOG("Coudn't get hardware parameters.");
      snd_pcm_close(pcm_handle);
      return 0;
    }

    status = snd_pcm_hw_params_set_access(pcm_handle, hwparams,
					  SND_PCM_ACCESS_RW_INTERLEAVED);
    if (status < 0) {
      ADR_LOG("Coudn't set access mode.");
      snd_pcm_close(pcm_handle);
      return 0;
    }
    status = snd_pcm_hw_params_set_format(pcm_handle, hwparams,
					  SND_PCM_FORMAT_S16_LE);
    if (status < 0) {
      ADR_LOG("Coudn't set pcm format.");
      snd_pcm_close(pcm_handle);
      return 0;
    }

    unsigned int channels = 2;
    status = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams,
						 &channels);
    if (status < 0) {
      ADR_LOG("Coudn't set channels.");
      snd_pcm_close(pcm_handle);
      return 0;
    }

    status = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams,
					     &rate, NULL);
    if (status < 0) {
      ADR_LOG("Coudn't set sample rate.");
      snd_pcm_close(pcm_handle);
      return 0;
    }

    snd_pcm_uframes_t frame_size = 1024;
    status = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams,
						    &frame_size, NULL);
    if (status < 0) {
      ADR_LOG("Coudn't set buffer size.");
      snd_pcm_close(pcm_handle);
      return 0;
    }

    unsigned frames = 8;
    status = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams,
						&frames, NULL);
    if (status < 0) {
      ADR_LOG("Coudn't set periods.");
      snd_pcm_close(pcm_handle);
      return 0;
    }

    status = snd_pcm_hw_params(pcm_handle, hwparams);
    if (status < 0) {
      ADR_LOG("Coudn't set hardware parameters.");
      snd_pcm_close(pcm_handle);
      return 0;
    }

    snd_pcm_uframes_t buffer_size;
    status = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
    if (status < 0) {
      ADR_LOG("Coudn't get buffer size.");
      snd_pcm_close(pcm_handle);
      return 0;
    }

    snd_pcm_uframes_t period_size;
    status = snd_pcm_hw_params_get_period_size(hwparams, &period_size, NULL);
    if (status < 0) {
      ADR_LOG("Coudn't get period size.");
      snd_pcm_close(pcm_handle);
      return 0;
    }

    status = snd_pcm_sw_params_current(pcm_handle, swparams);
    if (status < 0) {
      ADR_LOG("Coudn't get software parameters.");
      snd_pcm_close(pcm_handle);
      return 0;
    }

    snd_pcm_uframes_t boundary;
#if SND_LIB_VERSION >= 0x000901
    status = snd_pcm_sw_params_get_boundary(swparams, &boundary);
    if (status < 0) {
      ADR_LOG("Coudn't get boundary.");
      snd_pcm_close(pcm_handle);
      return 0;
    }
#else
    boundary = 0x7fffffff;
#endif
    status = snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams,
						   period_size);
    if (status < 0) {
      ADR_LOG("Coudn't get hardware parameters.");
      snd_pcm_close(pcm_handle);
      return 0;
    }

    status = snd_pcm_sw_params_set_stop_threshold(pcm_handle, swparams,
						  boundary);
    if (status < 0) {
      ADR_LOG("Coudn't set stop threshold.");
      snd_pcm_close(pcm_handle);
      return 0;
    }

#if SND_LIB_VERSION >= 0x000901
    status = snd_pcm_sw_params_set_silence_size(pcm_handle, swparams,
						boundary);
    if (status < 0) {
      ADR_LOG("Coudn't set silence size.");
      snd_pcm_close(pcm_handle);
      return 0;
    }
#endif
    status = snd_pcm_sw_params(pcm_handle, swparams);
    if (status < 0) {
      ADR_LOG("Couldn't set audio device parameters.");
      snd_pcm_close(pcm_handle);
      return 0;
    }

    return pcm_handle;
  }

  ALSAAudioDevice*
  ALSAAudioDevice::create(const ParameterList& parameters) {
    std::string devices[] = {
      "default", "plughw:0,0", "hw:0,0", ""
    };
    std::string default_device = parameters.getValue("device", devices[0].c_str());
    if (default_device != devices[0])
      devices[0] = default_device;

    int i, rate;
    snd_pcm_t* pcm_handle = 0;

    for (i = 0; devices[i].length() > 0; i++) {
      rate = 48000;
      pcm_handle = openDevice(devices[i], rate);
      if (!pcm_handle) {
	rate = 44100;
	pcm_handle = openDevice(devices[i], rate);
      }
      if (pcm_handle)
	break;
    }
    if (!pcm_handle) {
      ADR_LOG("Coudn't open haredware.");
      return 0;
    }

    return new ALSAAudioDevice(pcm_handle, devices[i], rate, 4096);
  }


  ALSAAudioDevice::ALSAAudioDevice(snd_pcm_t* pcm_handle,
				   const std::string &name,
                                   int rate,
                                   int buffer_size)
    : MixerDevice(rate), m_pcm_name(name), m_rate(rate)
  {
    m_pcm_handle = pcm_handle;
    m_buffer_size = buffer_size;
    m_buffer = new char [buffer_size];
  }


  ALSAAudioDevice::~ALSAAudioDevice() {
    ADR_GUARD("ALSAAudioDevice::~ALSAAudioDevice");
    if (m_pcm_handle) {
      snd_pcm_drain(m_pcm_handle);
      snd_pcm_close(m_pcm_handle);
    }
    delete [] m_buffer;
  }


  void ADR_CALL
  ALSAAudioDevice::update() {
    int           ret;
    int           sample_len;
    int           sample_left;
    char*         sample_buf;
    int           retry = 0;

    // reopen a pcm
    if (!m_pcm_handle) {
      m_pcm_handle = openDevice(m_pcm_name, m_rate);
      if (!m_pcm_handle) {
	usleep(100);
	return;
      }
    }

    sample_buf = m_buffer;
    sample_len = m_buffer_size / 4;

    sample_left = read(sample_len, sample_buf);
    while (sample_left > 0 && m_pcm_handle) {
      ret = snd_pcm_writei(m_pcm_handle, sample_buf, sample_left);
      if (ret == -EAGAIN) {
        snd_pcm_wait(m_pcm_handle, 1000);
	if (++retry > 5) {
	  snd_pcm_close(m_pcm_handle);
	  m_pcm_handle = 0;
	  break;
	}
      } else if (ret == -ESTRPIPE) {
        do {
          snd_pcm_wait(m_pcm_handle, 10);
          ret = snd_pcm_resume(m_pcm_handle);
        } while (ret == -EAGAIN);
        snd_pcm_prepare(m_pcm_handle);
      } else if (ret == -EPIPE) {
        snd_pcm_prepare(m_pcm_handle);
      }
      if (ret > 0) {
        sample_buf += ret * 4;
        sample_left -= ret;
      }
    }
  }


  const char* ADR_CALL
  ALSAAudioDevice::getName() {
    return "alsa";
  }

}