Debian Codegraph @

src/CaptureThreadImplALSA.cpp

raw
1 // Copyright 2004 "Gilles Degottex"
2
3 // This file is part of "Music"
4
5 // "Music" is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or
8 // (at your option) any later version.
9 //
10 // "Music" 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 Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19
20 #include "CaptureThread.h"
21
22 #include <cassert>
23 #include <time.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <iostream>
28 #include <fstream>
29 #include <vector>
30 using namespace std;
31
32 // ------------------------------ ALSA implementation ----------------------------
33 #ifdef CAPTURE_ALSA
34
35 #define ALSA_BUFF_SIZE 1024
36
37 void alsa_error_handler(const char *file, int line, const char *function, int err, const char *fmt, ...)
38 {
39 (void)fmt;
40 cout << "alsa_error_handler: " << file << ":" << line << " " << function << " err=" << err << endl;
41 }
42
43 CaptureThreadImplALSA::CaptureThreadImplALSA(CaptureThread* capture_thread)
44 : CaptureThreadImpl(capture_thread, "ALSA", QString("Advanced Linux Sound Architecture (lib:")+snd_asoundlib_version()+")")
45 {
46 m_alsa_capture_handle = NULL;
47 m_alsa_hw_params = NULL;
48 m_alsa_buffer = NULL;
49 m_format = SND_PCM_FORMAT_UNKNOWN;
50
51 m_source = "default"; // was hw:0
52
53 m_alive = true;
54 m_in_run = false;
55 m_loop = false;
56
57 // snd_lib_error_set_handler(alsa_error_handler);
58 }
59
60 bool CaptureThreadImplALSA::is_available()
61 {
62 if(m_alsa_capture_handle==NULL)
63 {
64 try
65 {
66 int err = -1;
67 if((err=snd_pcm_open(&m_alsa_capture_handle, getASCIISource().toLatin1().constData(), SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0)
68 {
69 if(err==-19) // TODO risks of changes for the error code
70 throw QString("invalid source '")+getASCIISource()+"'";
71 else if(err==-16)
72 throw QString("device '")+getASCIISource()+"' busy";
73 else
74 throw QString("cannot open pcm: ")+QString(snd_strerror(err));
75 }
76 }
77 catch(QString error)
78 {
79 m_alsa_capture_handle = NULL;
80
81 m_status = "N/A ("+error+")";
82
83 return false;
84 }
85
86 if(m_alsa_capture_handle!=NULL)
87 {
88 snd_pcm_close(m_alsa_capture_handle);
89 m_alsa_capture_handle = NULL;
90 }
91 }
92
93 m_status = "OK";
94
95 // cout << "CaptureThread: INFO: ALSA seems available" << endl;
96
97 return true;
98 }
99
100 void CaptureThreadImplALSA::startCapture()
101 {
102 if(!isRunning())
103 start();
104
105 m_loop = true;
106
107 m_wait_for_start = true;
108 while(m_wait_for_start) // some implementations take a long time to start
109 msleep(10);
110 }
111 void CaptureThreadImplALSA::stopCapture()
112 {
113 m_loop = false;
114
115 while(m_in_run)
116 msleep(10);
117 }
118
119 void CaptureThreadImplALSA::set_params(bool test)
120 {
121 // cout << "ALSA: Recognized sample formats are" << endl;
122 // for (int k = 0; k < SND_PCM_FORMAT_LAST; ++(unsigned long) k) {
123 // const char *s = snd_pcm_format_name((snd_pcm_format_t)k);
124 // if (s) cout << s << endl;
125 // }
126 int err=0;
127
128 if(m_source=="")
129 throw QString("ALSA: set the source first");
130 if((err=snd_pcm_open(&m_alsa_capture_handle, getASCIISource().toLatin1().constData(), SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0)
131 {
132 // cout << "err=" << err << ":" << snd_strerror(err) << endl;
133
134 if(err==-19) // TODO risks of changes for the error code
135 throw QString("ALSA: Invalid Source '")+getASCIISource()+"'";
136 else if(err==-16)
137 throw QString("ALSA: Device '")+getASCIISource()+"' busy";
138 else
139 throw QString("ALSA: Cannot open pcm: ")+QString(snd_strerror(err));
140 }
141
142 snd_pcm_hw_params_alloca(&m_alsa_hw_params);
143
144 if((err=snd_pcm_hw_params_any(m_alsa_capture_handle, m_alsa_hw_params)) < 0)
145 throw QString("ALSA: cannot initialize hardware parameter structure (")+QString(snd_strerror(err))+")";
146
147 if((err=snd_pcm_hw_params_set_access(m_alsa_capture_handle, m_alsa_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
148 throw QString("ALSA: cannot set access type (")+QString(snd_strerror(err))+")";
149
150 if(!test)
151 {
152 // Formats
153 list<snd_pcm_format_t> formats;
154 // Not a good idea to try 24b, since it is not always properly supported
155 // formats.push_back(SND_PCM_FORMAT_S24);
156 formats.push_back(SND_PCM_FORMAT_S16); formats.push_back(SND_PCM_FORMAT_U16);
157 formats.push_back(SND_PCM_FORMAT_S8); formats.push_back(SND_PCM_FORMAT_U8);
158
159 err = -1;
160 while(err<0)
161 {
162 if(formats.empty())
163 throw QString("ALSA: cannot set any format (")+QString(snd_strerror(err))+")";
164
165 m_format = formats.front();
166 cout << "CaptureThread: INFO: ALSA: try to set format to " << snd_pcm_format_description(m_format) << flush;
167 err=snd_pcm_hw_params_set_format(m_alsa_capture_handle, m_alsa_hw_params, m_format);
168
169 if(err<0) cout << " failed" << endl;
170 else cout << " success" << endl;
171
172 formats.pop_front();
173 }
174
175 // Channel count
176 unsigned int channel_count = 1;
177 if((err=snd_pcm_hw_params_set_channels_near(m_alsa_capture_handle, m_alsa_hw_params, &channel_count)) < 0)
178 {
179 QString err_msg = QString("ALSA: cannot set channel count (")+QString(snd_strerror(err))+")";
180 cout << "CaptureThread: WARNING: " << err_msg.toStdString() << endl;
181 }
182 if(channel_count>1)
183 {
184 QString err_msg = QString("ALSA: cannot set channel count to one (")+QString::number(channel_count)+" instead)";
185 cout << "CaptureThread: WARNING: " << err_msg.toStdString() << endl;
186 }
187
188 setFormatDescrsAndFns(snd_pcm_format_width(m_format)/8, snd_pcm_format_signed(m_format), false, channel_count);
189 }
190
191 if(m_sampling_rate==CaptureThread::SAMPLING_RATE_MAX || m_sampling_rate==CaptureThread::SAMPLING_RATE_UNKNOWN)
192 {
193 int old_sampling_rate = m_sampling_rate;
194
195 cout << "CaptureThread: INFO: ALSA: sampling rate set to max or undefined, try to determinate it." << endl;
196
197 list<int> sampling_rates;
198 sampling_rates.push_front(8000); sampling_rates.push_front(11025); sampling_rates.push_front(16000);
199 sampling_rates.push_front(22050); sampling_rates.push_front(24000); sampling_rates.push_front(32000);
200 sampling_rates.push_front(44100);
201
202 err = -1;
203 while(err<0)
204 {
205 if(sampling_rates.empty())
206 throw QString("ALSA: cannot set any sample rate (")+QString(snd_strerror(err))+")";
207
208 m_sampling_rate = sampling_rates.front();
209 cout << "CaptureThread: INFO: ALSA: try sampling rate " << m_sampling_rate << " ..." << flush;
210 unsigned int rrate = m_sampling_rate;
211 err = snd_pcm_hw_params_set_rate(m_alsa_capture_handle, m_alsa_hw_params, rrate, 0);
212
213 if(err<0) cout << " failed" << endl;
214 else cout << " success" << endl;
215
216 sampling_rates.pop_front();
217 }
218
219 if(old_sampling_rate!=m_sampling_rate)
220 m_capture_thread->emitSamplingRateChanged();
221 }
222 else
223 {
224 int err;
225 int dir = 0;
226 unsigned int rrate = m_sampling_rate;
227 if((err = snd_pcm_hw_params_set_rate_near(m_alsa_capture_handle, m_alsa_hw_params, &rrate, &dir))<0)
228 throw QString("ALSA: cannot set sampling rate (")+QString(snd_strerror(err))+")";
229 if(m_sampling_rate!=int(rrate))
230 m_capture_thread->emitSamplingRateChanged();
231 m_sampling_rate = rrate;
232 }
233
234 if((err=snd_pcm_hw_params(m_alsa_capture_handle, m_alsa_hw_params)) < 0)
235 throw QString("ALSA: cannot set parameters (")+QString(snd_strerror(err))+")";
236 }
237
238 void CaptureThreadImplALSA::setSamplingRate(int value)
239 {
240 // cout << "CaptureThreadImplALSA::setSamplingRate " << value << endl;
241
242 assert(value>0 || value==CaptureThread::SAMPLING_RATE_MAX);
243
244 if(m_sampling_rate!=value || value==CaptureThread::SAMPLING_RATE_MAX)
245 {
246 bool was_running = m_capture_thread->isCapturing();
247 if(was_running) m_capture_thread->stopCapture();
248
249 m_sampling_rate = value;
250
251 if(m_sampling_rate==CaptureThread::SAMPLING_RATE_MAX)
252 {
253 try
254 {
255 set_params(true);
256 }
257 catch(QString error)
258 {
259 cout << "CaptureThread: ERROR: " << error.toStdString() << endl;
260 m_capture_thread->emitError(error);
261 }
262
263 // it was just for testing
264 capture_finished();
265 }
266 else
267 m_capture_thread->emitSamplingRateChanged();
268
269 if(was_running) m_capture_thread->startCapture();
270 }
271
272 // cout << "~CaptureThreadImplALSA::setSamplingRate" << endl;
273 }
274
275 void CaptureThreadImplALSA::capture_init()
276 {
277 set_params();
278
279 snd_pcm_nonblock(m_alsa_capture_handle, 0);
280
281 m_alsa_buffer = new char[m_channel_count*ALSA_BUFF_SIZE*snd_pcm_format_width(m_format)/8];
282
283 int err=0;
284
285 if((err=snd_pcm_prepare(m_alsa_capture_handle)) < 0)
286 throw QString("ALSA: cannot prepare audio interface for use (")+QString(snd_strerror(err))+")";
287 }
288 void CaptureThreadImplALSA::capture_loop()
289 {
290 // cout << "CaptureThreadImplALSA::capture_loop" << endl;
291
292 m_wait_for_start = false;
293 while(m_loop)
294 {
295 int ret_val = snd_pcm_readi(m_alsa_capture_handle, m_alsa_buffer, ALSA_BUFF_SIZE);
296 if (ret_val == -ESTRPIPE)
297 continue;
298
299 if(ret_val<0)
300 {
301 cout << "CaptureThread: WARNING: ALSA: " << snd_strerror(ret_val) << endl;
302 while((ret_val = snd_pcm_prepare(m_alsa_capture_handle)) < 0)
303 {
304 if (ret_val == -ESTRPIPE)
305 continue;
306
307 msleep(1000);
308 cout << "ALSA: cannot prepare audio interface (" << snd_strerror(ret_val) << ")" << endl;
309 // throw QString("ALSA: cannot prepare audio interface (")+QString(snd_strerror(ret_val))+")";
310 }
311 }
312 else
313 {
314 if(!m_capture_thread->m_pause)
315 {
316 m_capture_thread->m_lock.lock();
317
318 // cout << "CaptureThreadImplALSA::capture_loop " << m_capture_thread->m_values.size() << endl;
319
320 for(int i=0; i<ret_val*m_channel_count; i++)
321 addValue(this, decodeValue(m_alsa_buffer, i), i);
322
323 m_capture_thread->m_packet_size = ret_val;
324 if(m_capture_thread->m_ext_lock)
325 {
326 m_capture_thread->m_packet_size_sll = 0;
327 m_capture_thread->m_ext_lock = false;
328 }
329 m_capture_thread->m_packet_size_sll += ret_val;
330
331 m_capture_thread->m_lock.unlock();
332 }
333 }
334 }
335
336 // cout << "~CaptureThreadImplALSA::capture_loop" << endl;
337 }
338 void CaptureThreadImplALSA::capture_finished()
339 {
340 if(m_alsa_buffer!=NULL)
341 {
342 delete[] m_alsa_buffer;
343 m_alsa_buffer = NULL;
344 }
345
346 if(m_alsa_capture_handle!=NULL)
347 {
348 snd_pcm_hw_free(m_alsa_capture_handle);
349 snd_pcm_close(m_alsa_capture_handle);
350 m_alsa_capture_handle = NULL;
351 }
352 }
353
354 void CaptureThreadImplALSA::run()
355 {
356 // cout << "CaptureThread: INFO: ALSA: capture thread entered" << endl;
357
358 // while(m_alive) // TODO need to keep alsa thread alive to let PortAudio working after ALSA !!
359 {
360 while(m_alive && !m_loop)
361 msleep(10);
362
363 m_in_run = true;
364
365 try
366 {
367 // cout << "CaptureThread: INFO: capture thread running" << endl;
368
369 capture_init();
370
371 m_capture_thread->m_capturing = true;
372 m_capture_thread->emitCaptureStarted();
373 m_capture_thread->emitCaptureToggled(true);
374
375 capture_loop();
376
377 m_capture_thread->m_capturing = false;
378 m_capture_thread->emitCaptureStoped();
379 m_capture_thread->emitCaptureToggled(false);
380 }
381 catch(QString error)
382 {
383 m_loop = false;
384 cout << "CaptureThread: ERROR: " << error.toStdString() << endl;
385 m_capture_thread->emitError(error);
386 }
387 m_wait_for_start = false;
388
389 capture_finished();
390
391 m_in_run = false;
392
393 // cout << "CaptureThread: INFO: capture thread stop running" << endl;
394 }
395
396 // cout << "CaptureThread: INFO: ALSA: capture thread exited" << endl;
397 }
398
399 CaptureThreadImplALSA::~CaptureThreadImplALSA()
400 {
401 // cout << "CaptureThreadImplALSA::~CaptureThreadImplALSA" << endl;
402
403 m_alive = false;
404
405 stopCapture();
406
407 while(isRunning())
408 msleep(10);
409
410 // cout << "~CaptureThreadImplALSA::~CaptureThreadImplALSA" << endl;
411 }
412
413 #endif