Debian Codegraph @

src/CaptureThreadImplOSS.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 // ------------------------------ OSS implementation ----------------------------
33 #ifdef CAPTURE_OSS
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <sys/ioctl.h>
40 #include <sys/soundcard.h>
41
42 #define OSS_BUFF_SIZE 1024
43
44 CaptureThreadImplOSS::CaptureThreadImplOSS(CaptureThread* capture_thread)
45 : CaptureThreadImpl(capture_thread, "OSS", QString("Open Sound System"))
46 {
47 m_fd_in = 0;
48 m_oss_buffer = NULL;
49 m_format = -1;
50
51 m_source = "/dev/dsp";
52
53 // oss_sysinfo si; // definition ??
54 // ioctl(m_fd_in, OSS_SYSINFO, &si);
55 // m_fd_in = open(getASCIISource().toLatin1().constData(), O_RDONLY, 0));
56 // m_descr = QString("Open Sound System (")+QString::number(SOUND_VERSION, 16)+":"+si.version+")");
57 // close(m_fd_in);
58 // m_fd_in = 0;
59 m_descr = QString("Open Sound System (lib:")+QString::number(SOUND_VERSION, 16)+")";
60
61 m_alive = true;
62 m_in_run = false;
63 m_loop = false;
64 }
65
66 bool CaptureThreadImplOSS::is_available()
67 {
68 if(m_fd_in==0)
69 {
70 try
71 {
72 if((m_fd_in = open (getASCIISource().toLatin1().constData(), O_RDONLY, 0)) == -1)
73 throw QString(strerror(errno));
74 }
75 catch(QString error)
76 {
77 m_fd_in = 0;
78
79 m_status = "N/A ("+error+")";
80
81 return false;
82 }
83
84 capture_finished();
85 }
86
87 m_status = "OK";
88
89 // cout << "CaptureThread: INFO: OSS seems available" << endl;
90
91 return true;
92 }
93
94 void CaptureThreadImplOSS::startCapture()
95 {
96 if(!isRunning())
97 start();
98
99 m_loop = true;
100
101 m_wait_for_start = true;
102 while(m_wait_for_start) // some implementations take a long time to start
103 msleep(10);
104 }
105 void CaptureThreadImplOSS::stopCapture()
106 {
107 m_loop = false;
108
109 while(m_in_run)
110 msleep(10);
111 }
112
113 void CaptureThreadImplOSS::set_params(bool test)
114 {
115 if(getASCIISource()=="")
116 throw QString("OSS: set the source first");
117 if((m_fd_in = open (getASCIISource().toLatin1().constData(), O_RDONLY, 0))==-1)
118 throw QString("OSS: ")+QString(strerror(errno));
119
120 if(!test)
121 {
122 // Formats
123 m_format = AFMT_S16_NE; /* Native 16 bits */
124 if(ioctl(m_fd_in, SNDCTL_DSP_SETFMT, &m_format)==-1)
125 throw QString("OSS: cannot set format (%1)").arg(strerror(errno));
126
127 if(m_format != AFMT_S16_NE)
128 throw QString("OSS: cannot set format to signed 16bits");
129
130 m_format_size = 2;
131 m_format_signed = true;
132 m_format_float = false;
133
134 // Channel count
135 unsigned int channel_count = 1;
136 if(ioctl(m_fd_in, SNDCTL_DSP_CHANNELS, &channel_count)==-1)
137 throw QString("OSS: cannot set channel count to 1 (%1)").arg(strerror(errno));
138
139 if(channel_count != 1)
140 throw QString("OSS: the device doesn't support mono mode");
141
142 /* if(m_channel_count>1) // TODO
143 {
144 QString err_msg = QString("OSS: cannot set channel count to one (")+QString::number(m_channel_count)+" instead)";
145 cout << "CaptureThread: WARNING: " << err_msg << endl;
146 }*/
147
148 setFormatDescrsAndFns(2, true, false, channel_count);
149 }
150
151 if(m_sampling_rate==CaptureThread::SAMPLING_RATE_MAX || m_sampling_rate==CaptureThread::SAMPLING_RATE_UNKNOWN)
152 {
153 int old_sampling_rate = m_sampling_rate;
154
155 cout << "CaptureThread: INFO: OSS: sampling rate set to max or undefined, try to determinate it." << endl;
156
157 list<int> sampling_rates;
158 sampling_rates.push_front(8000); sampling_rates.push_front(11025); sampling_rates.push_front(16000);
159 sampling_rates.push_front(22050); sampling_rates.push_front(24000); sampling_rates.push_front(32000);
160 sampling_rates.push_front(44100);
161
162 int err = -1;
163 while(err<0)
164 {
165 if(sampling_rates.empty())
166 throw QString("OSS: cannot set any sample rate (%1)").arg(strerror(errno));
167
168 m_sampling_rate = sampling_rates.front();
169 cout << "CaptureThread: INFO: OSS: try sampling rate " << m_sampling_rate << " ..." << flush;
170 err = ioctl(m_fd_in, SNDCTL_DSP_SPEED, &m_sampling_rate);
171
172 if(err==-1) cout << " failed" << endl;
173 else cout << " success" << endl;
174
175 sampling_rates.pop_front();
176 }
177
178 if(old_sampling_rate!=m_sampling_rate)
179 m_capture_thread->emitSamplingRateChanged();
180 }
181 else
182 {
183 if(ioctl(m_fd_in, SNDCTL_DSP_SPEED, &m_sampling_rate)==-1)
184 throw QString("OSS: cannot set sampling rate (")+QString(strerror(errno))+")";
185 }
186 }
187
188 void CaptureThreadImplOSS::setSamplingRate(int value)
189 {
190 // cout << "CaptureThreadImplOSS::setSamplingRate " << value << endl;
191
192 assert(value>0 || value==CaptureThread::SAMPLING_RATE_MAX);
193
194 if(m_sampling_rate!=value || value==CaptureThread::SAMPLING_RATE_MAX)
195 {
196 bool was_running = m_capture_thread->isCapturing();
197 if(was_running) m_capture_thread->stopCapture();
198
199 m_sampling_rate = value;
200
201 if(m_sampling_rate==CaptureThread::SAMPLING_RATE_MAX)
202 {
203 try
204 {
205 set_params(true);
206 }
207 catch(QString error)
208 {
209 cout << "CaptureThread: ERROR: " << error.toStdString() << endl;
210 m_capture_thread->emitError(error);
211 }
212
213 // it was just for testing
214 capture_finished();
215 }
216 else
217 m_capture_thread->emitSamplingRateChanged();
218
219 if(was_running) m_capture_thread->startCapture();
220 }
221
222 // cout << "~CaptureThreadImplOSS::setSamplingRate" << endl;
223 }
224
225 void CaptureThreadImplOSS::capture_init()
226 {
227 set_params(false);
228
229 m_oss_buffer = new char[m_channel_count*OSS_BUFF_SIZE*16/8];
230 }
231 void CaptureThreadImplOSS::capture_loop()
232 {
233 // cout << "CaptureThreadImplOSS::capture_loop" << endl;
234
235 bool format_signed = true;
236 int l=0;
237
238 m_wait_for_start = false;
239 while(m_loop)
240 {
241 int ret_val = read(m_fd_in, m_oss_buffer, sizeof(m_oss_buffer));
242
243 if(ret_val==-1)
244 {
245 cout << "CaptureThread: WARNING: OSS: " << strerror(errno) << endl;
246 msleep(1000); // TODO which behavior ?
247 // m_loop = false;// TODO which behavior ?
248 }
249 else
250 {
251 ret_val /= m_format_size;
252
253 if(!m_capture_thread->m_pause)
254 {
255 m_capture_thread->m_lock.lock();
256
257 for(int i=0; i<ret_val*m_channel_count; i++)
258 addValue(this, decodeValue(m_oss_buffer, i), i);
259
260 m_capture_thread->m_packet_size = ret_val;
261 if(m_capture_thread->m_ext_lock)
262 {
263 m_capture_thread->m_packet_size_sll = 0;
264 m_capture_thread->m_ext_lock = false;
265 }
266 m_capture_thread->m_packet_size_sll += ret_val;
267
268 m_capture_thread->m_lock.unlock();
269 }
270 }
271 }
272
273 // cout << "~CaptureThreadImplOSS::capture_loop" << endl;
274 }
275 void CaptureThreadImplOSS::capture_finished()
276 {
277 if(m_oss_buffer!=NULL)
278 {
279 delete[] m_oss_buffer;
280 m_oss_buffer = NULL;
281 }
282
283 if(m_fd_in!=0)
284 {
285 close(m_fd_in);
286 m_fd_in = 0;
287 }
288 }
289
290 void CaptureThreadImplOSS::run()
291 {
292 // cout << "CaptureThread: INFO: OSS: capture thread entered" << endl;
293
294 // while(m_alive) // TODO ?? need to keep oss thread alive to let PortAudio working after ALSA ??
295 {
296 while(m_alive && !m_loop)
297 msleep(10);
298
299 m_in_run = true;
300
301 try
302 {
303 // cout << "CaptureThread: INFO: capture thread running" << endl;
304
305 capture_init();
306
307 m_capture_thread->m_capturing = true;
308 m_capture_thread->emitCaptureStarted();
309 m_capture_thread->emitCaptureToggled(true);
310
311 capture_loop();
312
313 m_capture_thread->m_capturing = false;
314 m_capture_thread->emitCaptureStoped();
315 m_capture_thread->emitCaptureToggled(false);
316 }
317 catch(QString error)
318 {
319 m_loop = false;
320 cout << "CaptureThread: ERROR: " << error.toStdString() << endl;
321 m_capture_thread->emitError(error);
322 }
323 m_wait_for_start = false;
324
325 capture_finished();
326
327 m_in_run = false;
328
329 // cout << "CaptureThread: INFO: capture thread stop running" << endl;
330 }
331
332 // cout << "CaptureThread: INFO: OSS: capture thread exited" << endl;
333 }
334
335 CaptureThreadImplOSS::~CaptureThreadImplOSS()
336 {
337 // cout << "CaptureThreadImplOSS::~CaptureThreadImplOSS" << endl;
338
339 m_alive = false;
340
341 stopCapture();
342
343 while(isRunning())
344 msleep(10);
345
346 // cout << "~CaptureThreadImplOSS::~CaptureThreadImplOSS" << endl;
347 }
348
349 #endif