| 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 |
// ------------------------------ JACK implementation ---------------------------- |
| 33 |
#ifdef CAPTURE_JACK |
| 34 |
|
| 35 |
#define JACK_BUFF_SIZE 1024 |
| 36 |
|
| 37 |
const size_t g_jack_sample_size = sizeof(jack_default_audio_sample_t); |
| 38 |
|
| 39 |
CaptureThreadImplJACK::CaptureThreadImplJACK(CaptureThread* capture_thread) |
| 40 |
: CaptureThreadImpl(capture_thread, "JACK", "Jack Audio Connection Kit") |
| 41 |
{ |
| 42 |
m_jack_client = NULL; |
| 43 |
m_jack_port = NULL; |
| 44 |
m_ringbuffer = NULL; |
| 45 |
|
| 46 |
m_alive = true; |
| 47 |
m_in_run = false; |
| 48 |
m_loop = false; |
| 49 |
} |
| 50 |
|
| 51 |
bool CaptureThreadImplJACK::is_available() |
| 52 |
{ |
| 53 |
if(m_jack_client==NULL) |
| 54 |
{ |
| 55 |
try |
| 56 |
{ |
| 57 |
//m_jack_client = jack_client_new((m_capture_thread->m_name+"_test").latin1()); |
| 58 |
m_jack_client = jack_client_open((m_capture_thread->m_name+"_test").toLatin1().constData(), JackNoStartServer, NULL); |
| 59 |
if(m_jack_client==NULL) |
| 60 |
throw QString("unknown reason"); |
| 61 |
} |
| 62 |
catch(QString error) |
| 63 |
{ |
| 64 |
m_jack_client = NULL; |
| 65 |
m_status = "N/A"; |
| 66 |
return false; |
| 67 |
} |
| 68 |
capture_finished(); |
| 69 |
} |
| 70 |
|
| 71 |
m_status = "available"; |
| 72 |
|
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
void CaptureThreadImplJACK::setSamplingRate(int value) |
| 77 |
{ |
| 78 |
(void)value; |
| 79 |
cout << "CaptureThread: ERROR: JACK: setSamplingRate not available with JACK ! change the JACK server sampling rate instead" << endl; |
| 80 |
} |
| 81 |
|
| 82 |
void CaptureThreadImplJACK::startCapture() |
| 83 |
{ |
| 84 |
if(!isRunning()) |
| 85 |
start(); |
| 86 |
|
| 87 |
m_loop = true; |
| 88 |
|
| 89 |
m_wait_for_start = true; |
| 90 |
while(m_wait_for_start) // some implementations take a long time to start |
| 91 |
msleep(50); |
| 92 |
} |
| 93 |
void CaptureThreadImplJACK::stopCapture() |
| 94 |
{ |
| 95 |
m_loop = false; |
| 96 |
|
| 97 |
while(m_in_run) |
| 98 |
msleep(50); |
| 99 |
} |
| 100 |
|
| 101 |
void CaptureThreadImplJACK::JackShutdown(void* arg){((CaptureThreadImplJACK*)arg)->jackShutdown();} |
| 102 |
void CaptureThreadImplJACK::jackShutdown() |
| 103 |
{ |
| 104 |
// cout << "CaptureThreadImplJACK::jackShutdown" << endl; |
| 105 |
|
| 106 |
m_jack_client = NULL; |
| 107 |
m_jack_port = NULL; |
| 108 |
if(m_ringbuffer) |
| 109 |
{ |
| 110 |
jack_ringbuffer_free(m_ringbuffer); |
| 111 |
m_ringbuffer = NULL; |
| 112 |
} |
| 113 |
|
| 114 |
m_capture_thread->emitError("JACK: server shutdown !"); |
| 115 |
// cout << "~CaptureThreadImplJACK::jackShutdown" << endl; |
| 116 |
} |
| 117 |
|
| 118 |
int CaptureThreadImplJACK::JackSampleRate(jack_nframes_t nframes, void* arg){return ((CaptureThreadImplJACK*)arg)->jackSampleRate(nframes);} |
| 119 |
int CaptureThreadImplJACK::jackSampleRate(jack_nframes_t nframes) |
| 120 |
{ |
| 121 |
if(m_sampling_rate!=int(nframes)) |
| 122 |
{ |
| 123 |
if(m_ringbuffer) |
| 124 |
jack_ringbuffer_free(m_ringbuffer); |
| 125 |
m_ringbuffer = jack_ringbuffer_create(g_jack_sample_size*nframes); // one second buffer |
| 126 |
|
| 127 |
m_sampling_rate = nframes; |
| 128 |
m_capture_thread->emitSamplingRateChanged(); |
| 129 |
} |
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
int CaptureThreadImplJACK::JackProcess(jack_nframes_t nframes, void* arg){return ((CaptureThreadImplJACK*)arg)->jackProcess(nframes);} |
| 135 |
int CaptureThreadImplJACK::jackProcess(jack_nframes_t nframes) |
| 136 |
{ |
| 137 |
// cout << "CaptureThreadImplJACK::jackProcess '" << nframes << "'" << endl; |
| 138 |
|
| 139 |
if(!m_jack_client || !m_jack_port || !m_ringbuffer) return 0; |
| 140 |
if(m_capture_thread->m_pause || !m_capture_thread->m_capturing || nframes<=0) return 0; |
| 141 |
|
| 142 |
void* pin = jack_port_get_buffer(m_jack_port, nframes); |
| 143 |
|
| 144 |
if(!pin) return 0; |
| 145 |
|
| 146 |
jack_default_audio_sample_t* in = (jack_default_audio_sample_t*)pin; |
| 147 |
|
| 148 |
int characters_written = jack_ringbuffer_write(m_ringbuffer, (const char *) ((void *) (in)), g_jack_sample_size*nframes); |
| 149 |
if (characters_written < int(g_jack_sample_size*nframes)) |
| 150 |
cout << "CaptureThreadImplJACK::jackProcess Can not write all frames: ringbuffer full?" << endl; |
| 151 |
|
| 152 |
// int toread = jack_ringbuffer_read_space(m_ringbuffer); |
| 153 |
// cout << "CaptureThreadImplJACK::jackProcess " << characters_written/g_jack_sample_size << " values written, " << toread/g_jack_sample_size << " to read" << endl; |
| 154 |
|
| 155 |
// cout << "~CaptureThreadImplJACK::jackProcess" << endl; |
| 156 |
|
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
void CaptureThreadImplJACK::capture_init() |
| 161 |
{ |
| 162 |
m_jack_client = jack_client_open(m_capture_thread->m_name.toLatin1().constData(), JackNoStartServer, NULL); |
| 163 |
if(!m_jack_client) |
| 164 |
throw QString("JACK: cannot create client, JACK deamon is running ?"); |
| 165 |
|
| 166 |
jack_set_process_callback(m_jack_client, JackProcess, (void*)this); |
| 167 |
jack_on_shutdown(m_jack_client, JackShutdown, (void*)this); |
| 168 |
jack_set_sample_rate_callback(m_jack_client, JackSampleRate, (void*)this); |
| 169 |
|
| 170 |
cout << "CaptureThread: INFO: JACK: sampling rate=" << jack_get_sample_rate(m_jack_client) << endl; |
| 171 |
m_ringbuffer = jack_ringbuffer_create(g_jack_sample_size*jack_get_sample_rate(m_jack_client)); // one second buffer |
| 172 |
|
| 173 |
int err=0; |
| 174 |
if((err=jack_activate(m_jack_client))!=0) |
| 175 |
throw QString("JACK: cannot activate client"); |
| 176 |
|
| 177 |
setFormatDescrsAndFns(g_jack_sample_size, true, true, 1); |
| 178 |
|
| 179 |
m_jack_port = jack_port_register(m_jack_client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput,0); |
| 180 |
|
| 181 |
if(getASCIISource()!="") |
| 182 |
{ |
| 183 |
QString dest = QString(jack_get_client_name(m_jack_client))+":input"; |
| 184 |
cout << "CaptureThread: INFO: JACK: auto-connect '" << getASCIISource().toStdString() << "' with '" << dest.toStdString() << "'" << endl; |
| 185 |
if((err=jack_connect(m_jack_client, getASCIISource().toLatin1().constData(), dest.toLatin1().constData()))!=0) |
| 186 |
m_capture_thread->emitError(QString("JACK: Invalid source '")+getASCIISource()+"'"); |
| 187 |
} |
| 188 |
|
| 189 |
int old_sampling_rate = m_sampling_rate; |
| 190 |
m_sampling_rate = jack_get_sample_rate(m_jack_client); |
| 191 |
if(m_sampling_rate!=old_sampling_rate) |
| 192 |
m_capture_thread->emitSamplingRateChanged(); |
| 193 |
|
| 194 |
m_capture_thread->m_capturing = true; |
| 195 |
m_capture_thread->emitCaptureStarted(); |
| 196 |
m_capture_thread->emitCaptureToggled(true); |
| 197 |
} |
| 198 |
void CaptureThreadImplJACK::capture_loop() |
| 199 |
{ |
| 200 |
// cout << "CaptureThreadImplJACK::capture_loop " << g_jack_sample_size << endl; |
| 201 |
|
| 202 |
char dest[JACK_BUFF_SIZE*g_jack_sample_size]; |
| 203 |
|
| 204 |
m_wait_for_start = false; |
| 205 |
while(m_loop && m_jack_client && m_jack_port && m_ringbuffer) |
| 206 |
{ |
| 207 |
// cout << "CaptureThreadImplJACK::capture_loop toread=" << toread/g_jack_sample_size << endl; |
| 208 |
int read = jack_ringbuffer_read(m_ringbuffer, dest, JACK_BUFF_SIZE*g_jack_sample_size); |
| 209 |
if(read>0) |
| 210 |
{ |
| 211 |
if(!m_capture_thread->m_pause) |
| 212 |
{ |
| 213 |
// cout << "CaptureThreadImplJACK::capture_loop locking" << endl; |
| 214 |
m_capture_thread->m_lock.lock(); |
| 215 |
|
| 216 |
// int toread = jack_ringbuffer_read_space(m_ringbuffer); |
| 217 |
// cout << "CaptureThreadImplJACK::capture_loop in deque='" << m_capture_thread->m_values.size() << "' still toread=" << toread/g_jack_sample_size << endl; |
| 218 |
|
| 219 |
if(g_jack_sample_size==4) |
| 220 |
for(int i=0; i<int(read/g_jack_sample_size); i++) |
| 221 |
addValue(this, (double)(((float*)dest)[i]), i); |
| 222 |
else if(g_jack_sample_size==8) |
| 223 |
for(int i=0; i<int(read/g_jack_sample_size); i++) |
| 224 |
addValue(this, (double)(((double*)dest)[i]), i); |
| 225 |
|
| 226 |
m_capture_thread->m_packet_size = (read/g_jack_sample_size); |
| 227 |
if(m_capture_thread->m_ext_lock) |
| 228 |
{ |
| 229 |
m_capture_thread->m_packet_size_sll = 0; |
| 230 |
m_capture_thread->m_ext_lock = false; |
| 231 |
} |
| 232 |
m_capture_thread->m_packet_size_sll += (read/g_jack_sample_size); |
| 233 |
|
| 234 |
m_capture_thread->m_lock.unlock(); |
| 235 |
// cout << "CaptureThreadImplJACK::capture_loop unlocked" << endl; |
| 236 |
} |
| 237 |
} |
| 238 |
else |
| 239 |
msleep(10); |
| 240 |
} |
| 241 |
|
| 242 |
// cout << "~CaptureThreadImplJACK::capture_loop" << endl; |
| 243 |
} |
| 244 |
void CaptureThreadImplJACK::capture_finished() |
| 245 |
{ |
| 246 |
// cout << "CaptureThreadImplJACK::capture_finished" << endl; |
| 247 |
|
| 248 |
if(m_jack_client!=NULL) |
| 249 |
{ |
| 250 |
jack_client_close(m_jack_client); |
| 251 |
m_jack_client = NULL; |
| 252 |
if(m_ringbuffer) |
| 253 |
{ |
| 254 |
jack_ringbuffer_free(m_ringbuffer); |
| 255 |
m_ringbuffer = NULL; |
| 256 |
} |
| 257 |
|
| 258 |
m_capture_thread->m_capturing = false; |
| 259 |
m_capture_thread->emitCaptureStoped(); |
| 260 |
m_capture_thread->emitCaptureToggled(false); |
| 261 |
} |
| 262 |
} |
| 263 |
void CaptureThreadImplJACK::run() |
| 264 |
{ |
| 265 |
// cout << "CaptureThread: INFO: JACK: capture thread entered" << endl; |
| 266 |
|
| 267 |
// while(m_alive) // TODO need to keep alsa thread alive to let PortAudio working after ALSA !! TODO same for JACK ??? |
| 268 |
{ |
| 269 |
while(m_alive && !m_loop) |
| 270 |
msleep(50); |
| 271 |
|
| 272 |
m_in_run = true; |
| 273 |
|
| 274 |
try |
| 275 |
{ |
| 276 |
// cout << "CaptureThread: INFO: JACK: capture thread running" << endl; |
| 277 |
|
| 278 |
capture_init(); |
| 279 |
|
| 280 |
m_capture_thread->m_capturing = true; |
| 281 |
m_capture_thread->emitCaptureStarted(); |
| 282 |
m_capture_thread->emitCaptureToggled(true); |
| 283 |
|
| 284 |
capture_loop(); |
| 285 |
|
| 286 |
m_capture_thread->m_capturing = false; |
| 287 |
m_capture_thread->emitCaptureStoped(); |
| 288 |
m_capture_thread->emitCaptureToggled(false); |
| 289 |
} |
| 290 |
catch(QString error) |
| 291 |
{ |
| 292 |
m_loop = false; |
| 293 |
cout << "CaptureThread: ERROR: " << error.toStdString() << endl; |
| 294 |
m_capture_thread->emitError(error); |
| 295 |
} |
| 296 |
m_wait_for_start = false; |
| 297 |
|
| 298 |
capture_finished(); |
| 299 |
|
| 300 |
m_in_run = false; |
| 301 |
|
| 302 |
// cout << "CaptureThread: INFO: JACK: capture thread stop running" << endl; |
| 303 |
} |
| 304 |
|
| 305 |
// cout << "CaptureThread: INFO: JACK: capture thread exited" << endl; |
| 306 |
} |
| 307 |
CaptureThreadImplJACK::~CaptureThreadImplJACK() |
| 308 |
{ |
| 309 |
// cout << "CaptureThreadImplJACK::~CaptureThreadImplJACK" << endl; |
| 310 |
|
| 311 |
m_alive = false; |
| 312 |
|
| 313 |
stopCapture(); |
| 314 |
|
| 315 |
while(isRunning()) |
| 316 |
msleep(50); |
| 317 |
|
| 318 |
// cout << "~CaptureThreadImplJACK::~CaptureThreadImplJACK" << endl; |
| 319 |
} |
| 320 |
#endif |