Jack2  1.9.13
JackAlsaDriver.cpp
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004 Grame
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 
21 #define __STDC_FORMAT_MACROS // For inttypes.h to work in C++
22 
23 #include <iostream>
24 #include <math.h>
25 #include <stdio.h>
26 #include <memory.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <signal.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <string.h>
35 
36 #include "JackAlsaDriver.h"
37 #include "JackEngineControl.h"
38 #include "JackClientControl.h"
39 #include "JackPort.h"
40 #include "JackGraphManager.h"
41 #include "JackLockedEngine.h"
42 #ifdef __ANDROID__
43 #include "JackAndroidThread.h"
44 #else
45 #include "JackPosixThread.h"
46 #endif
47 #include "JackCompilerDeps.h"
48 #include "JackServerGlobals.h"
49 
50 static struct jack_constraint_enum_str_descriptor midi_constraint_descr_array[] =
51 {
52  { "none", "no MIDI driver" },
53  { "seq", "ALSA Sequencer driver" },
54  { "raw", "ALSA RawMIDI driver" },
55  { 0 }
56 };
57 
58 static struct jack_constraint_enum_char_descriptor dither_constraint_descr_array[] =
59 {
60  { 'n', "none" },
61  { 'r', "rectangular" },
62  { 's', "shaped" },
63  { 't', "triangular" },
64  { 0 }
65 };
66 
67 namespace Jack
68 {
69 
70 int JackAlsaDriver::SetBufferSize(jack_nframes_t buffer_size)
71 {
72  jack_log("JackAlsaDriver::SetBufferSize %ld", buffer_size);
73  int res = alsa_driver_reset_parameters((alsa_driver_t *)fDriver, buffer_size,
74  ((alsa_driver_t *)fDriver)->user_nperiods,
75  ((alsa_driver_t *)fDriver)->frame_rate);
76 
77  if (res == 0) { // update fEngineControl and fGraphManager
78  JackAudioDriver::SetBufferSize(buffer_size); // Generic change, never fails
79  // ALSA specific
80  UpdateLatencies();
81  } else {
82  // Restore old values
83  alsa_driver_reset_parameters((alsa_driver_t *)fDriver, fEngineControl->fBufferSize,
84  ((alsa_driver_t *)fDriver)->user_nperiods,
85  ((alsa_driver_t *)fDriver)->frame_rate);
86  }
87 
88  return res;
89 }
90 
91 void JackAlsaDriver::UpdateLatencies()
92 {
94  alsa_driver_t* alsa_driver = (alsa_driver_t*)fDriver;
95 
96  for (int i = 0; i < fCaptureChannels; i++) {
97  range.min = range.max = alsa_driver->frames_per_cycle + alsa_driver->capture_frame_latency;
98  fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &range);
99  }
100 
101  for (int i = 0; i < fPlaybackChannels; i++) {
102  // Add one buffer more latency if "async" mode is used...
103  range.min = range.max = (alsa_driver->frames_per_cycle * (alsa_driver->user_nperiods - 1)) +
104  ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize) + alsa_driver->playback_frame_latency;
105  fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &range);
106  // Monitor port
107  if (fWithMonitorPorts) {
108  range.min = range.max = alsa_driver->frames_per_cycle;
109  fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &range);
110  }
111  }
112 }
113 
114 int JackAlsaDriver::Attach()
115 {
116  JackPort* port;
117  jack_port_id_t port_index;
118  unsigned long port_flags = (unsigned long)CaptureDriverFlags;
119  char name[REAL_JACK_PORT_NAME_SIZE+1];
120  char alias[REAL_JACK_PORT_NAME_SIZE+1];
121 
122  assert(fCaptureChannels < DRIVER_PORT_NUM);
123  assert(fPlaybackChannels < DRIVER_PORT_NUM);
124 
125  alsa_driver_t* alsa_driver = (alsa_driver_t*)fDriver;
126 
127  if (alsa_driver->has_hw_monitoring)
128  port_flags |= JackPortCanMonitor;
129 
130  // ALSA driver may have changed the values
131  JackAudioDriver::SetBufferSize(alsa_driver->frames_per_cycle);
132  JackAudioDriver::SetSampleRate(alsa_driver->frame_rate);
133 
134  jack_log("JackAlsaDriver::Attach fBufferSize %ld fSampleRate %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
135 
136  for (int i = 0; i < fCaptureChannels; i++) {
137  snprintf(alias, sizeof(alias), "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
138  snprintf(name, sizeof(name), "%s:capture_%d", fClientControl.fName, i + 1);
139  if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize, &port_index) < 0) {
140  jack_error("driver: cannot register port for %s", name);
141  return -1;
142  }
143  port = fGraphManager->GetPort(port_index);
144  port->SetAlias(alias);
145  fCapturePortList[i] = port_index;
146  jack_log("JackAlsaDriver::Attach fCapturePortList[i] %ld ", port_index);
147  }
148 
149  port_flags = (unsigned long)PlaybackDriverFlags;
150 
151  for (int i = 0; i < fPlaybackChannels; i++) {
152  snprintf(alias, sizeof(alias), "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
153  snprintf(name, sizeof(name), "%s:playback_%d", fClientControl.fName, i + 1);
154  if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize, &port_index) < 0) {
155  jack_error("driver: cannot register port for %s", name);
156  return -1;
157  }
158  port = fGraphManager->GetPort(port_index);
159  port->SetAlias(alias);
160  fPlaybackPortList[i] = port_index;
161  jack_log("JackAlsaDriver::Attach fPlaybackPortList[i] %ld ", port_index);
162 
163  // Monitor ports
164  if (fWithMonitorPorts) {
165  jack_log("Create monitor port");
166  snprintf(name, sizeof(name), "%s:monitor_%d", fClientControl.fName, i + 1);
167  if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, MonitorDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
168  jack_error("ALSA: cannot register monitor port for %s", name);
169  } else {
170  fMonitorPortList[i] = port_index;
171  }
172  }
173  }
174 
175  UpdateLatencies();
176 
177  if (alsa_driver->midi) {
178  int err = (alsa_driver->midi->attach)(alsa_driver->midi);
179  if (err)
180  jack_error ("ALSA: cannot attach MIDI: %d", err);
181  }
182 
183  return 0;
184 }
185 
186 int JackAlsaDriver::Detach()
187 {
188  alsa_driver_t* alsa_driver = (alsa_driver_t*)fDriver;
189  if (alsa_driver->midi)
190  (alsa_driver->midi->detach)(alsa_driver->midi);
191 
192  return JackAudioDriver::Detach();
193 }
194 
195 extern "C" char* get_control_device_name(const char * device_name)
196 {
197  char * ctl_name;
198  const char * comma;
199 
200  /* the user wants a hw or plughw device, the ctl name
201  * should be hw:x where x is the card identification.
202  * We skip the subdevice suffix that starts with comma */
203 
204  if (strncasecmp(device_name, "plughw:", 7) == 0) {
205  /* skip the "plug" prefix" */
206  device_name += 4;
207  }
208 
209  comma = strchr(device_name, ',');
210  if (comma == NULL) {
211  ctl_name = strdup(device_name);
212  if (ctl_name == NULL) {
213  jack_error("strdup(\"%s\") failed.", device_name);
214  }
215  } else {
216  ctl_name = strndup(device_name, comma - device_name);
217  if (ctl_name == NULL) {
218  jack_error("strndup(\"%s\", %u) failed.", device_name, (unsigned int)(comma - device_name));
219  }
220  }
221 
222  return ctl_name;
223 }
224 
225 static int card_to_num(const char* device)
226 {
227  int err;
228  char* ctl_name;
229  snd_ctl_card_info_t *card_info;
230  snd_ctl_t* ctl_handle;
231  int i = -1;
232 
233  snd_ctl_card_info_alloca (&card_info);
234 
235  ctl_name = get_control_device_name(device);
236  if (ctl_name == NULL) {
237  jack_error("get_control_device_name() failed.");
238  goto fail;
239  }
240 
241  if ((err = snd_ctl_open (&ctl_handle, ctl_name, 0)) < 0) {
242  jack_error ("control open \"%s\" (%s)", ctl_name,
243  snd_strerror(err));
244  goto free;
245  }
246 
247  if ((err = snd_ctl_card_info(ctl_handle, card_info)) < 0) {
248  jack_error ("control hardware info \"%s\" (%s)",
249  device, snd_strerror (err));
250  goto close;
251  }
252 
253  i = snd_ctl_card_info_get_card(card_info);
254 
255 close:
256  snd_ctl_close(ctl_handle);
257 
258 free:
259  free(ctl_name);
260 
261 fail:
262  return i;
263 }
264 
265 int JackAlsaDriver::Open(jack_nframes_t nframes,
266  jack_nframes_t user_nperiods,
267  jack_nframes_t samplerate,
268  bool hw_monitoring,
269  bool hw_metering,
270  bool capturing,
271  bool playing,
272  DitherAlgorithm dither,
273  bool soft_mode,
274  bool monitor,
275  int inchannels,
276  int outchannels,
277  bool shorts_first,
278  const char* capture_driver_name,
279  const char* playback_driver_name,
280  jack_nframes_t capture_latency,
281  jack_nframes_t playback_latency,
282  const char* midi_driver_name)
283 {
284  // Generic JackAudioDriver Open
285  if (JackAudioDriver::Open(nframes, samplerate, capturing, playing,
286  inchannels, outchannels, monitor, capture_driver_name, playback_driver_name,
287  capture_latency, playback_latency) != 0) {
288  return -1;
289  }
290 
291  alsa_midi_t *midi = 0;
292 #ifndef __ANDROID__
293  if (strcmp(midi_driver_name, "seq") == 0)
294  midi = alsa_seqmidi_new((jack_client_t*)this, 0);
295  else if (strcmp(midi_driver_name, "raw") == 0)
296  midi = alsa_rawmidi_new((jack_client_t*)this);
297 #endif
298 
299  if (JackServerGlobals::on_device_acquire != NULL) {
300  int capture_card = card_to_num(capture_driver_name);
301  int playback_card = card_to_num(playback_driver_name);
302  char audio_name[32];
303 
304  if (capture_card >= 0) {
305  snprintf(audio_name, sizeof(audio_name), "Audio%d", capture_card);
306  if (!JackServerGlobals::on_device_acquire(audio_name)) {
307  jack_error("Audio device %s cannot be acquired...", capture_driver_name);
308  return -1;
309  }
310  }
311 
312  if (playback_card >= 0 && playback_card != capture_card) {
313  snprintf(audio_name, sizeof(audio_name), "Audio%d", playback_card);
314  if (!JackServerGlobals::on_device_acquire(audio_name)) {
315  jack_error("Audio device %s cannot be acquired...", playback_driver_name);
316  if (capture_card >= 0) {
317  snprintf(audio_name, sizeof(audio_name), "Audio%d", capture_card);
318  JackServerGlobals::on_device_release(audio_name);
319  }
320  return -1;
321  }
322  }
323  }
324 
325  fDriver = alsa_driver_new ((char*)"alsa_pcm", (char*)playback_driver_name, (char*)capture_driver_name,
326  NULL,
327  nframes,
328  user_nperiods,
329  samplerate,
330  hw_monitoring,
331  hw_metering,
332  capturing,
333  playing,
334  dither,
335  soft_mode,
336  monitor,
337  inchannels,
338  outchannels,
339  shorts_first,
340  capture_latency,
341  playback_latency,
342  midi);
343  if (fDriver) {
344  // ALSA driver may have changed the in/out values
345  fCaptureChannels = ((alsa_driver_t *)fDriver)->capture_nchannels;
346  fPlaybackChannels = ((alsa_driver_t *)fDriver)->playback_nchannels;
347  return 0;
348  } else {
349  Close();
350  return -1;
351  }
352 }
353 
354 int JackAlsaDriver::Close()
355 {
356  // Generic audio driver close
357  int res = JackAudioDriver::Close();
358 
359  if (fDriver) {
360  alsa_driver_delete((alsa_driver_t*)fDriver);
361  }
362 
363  if (JackServerGlobals::on_device_release != NULL)
364  {
365  char audio_name[32];
366  int capture_card = card_to_num(fCaptureDriverName);
367  if (capture_card >= 0) {
368  snprintf(audio_name, sizeof(audio_name), "Audio%d", capture_card);
369  JackServerGlobals::on_device_release(audio_name);
370  }
371 
372  int playback_card = card_to_num(fPlaybackDriverName);
373  if (playback_card >= 0 && playback_card != capture_card) {
374  snprintf(audio_name, sizeof(audio_name), "Audio%d", playback_card);
375  JackServerGlobals::on_device_release(audio_name);
376  }
377  }
378 
379  return res;
380 }
381 
382 int JackAlsaDriver::Start()
383 {
384  int res = JackAudioDriver::Start();
385  if (res >= 0) {
386  res = alsa_driver_start((alsa_driver_t *)fDriver);
387  if (res < 0) {
388  JackAudioDriver::Stop();
389  }
390  }
391  return res;
392 }
393 
394 int JackAlsaDriver::Stop()
395 {
396  int res = alsa_driver_stop((alsa_driver_t *)fDriver);
397  if (JackAudioDriver::Stop() < 0) {
398  res = -1;
399  }
400  return res;
401 }
402 
403 int JackAlsaDriver::Read()
404 {
405  /* Taken from alsa_driver_run_cycle */
406  int wait_status;
407  jack_nframes_t nframes;
408  fDelayedUsecs = 0.f;
409 
410 retry:
411 
412  nframes = alsa_driver_wait((alsa_driver_t *)fDriver, -1, &wait_status, &fDelayedUsecs);
413 
414  if (wait_status < 0)
415  return -1; /* driver failed */
416 
417  if (nframes == 0) {
418  /* we detected an xrun and restarted: notify
419  * clients about the delay.
420  */
421  jack_log("ALSA XRun wait_status = %d", wait_status);
422  NotifyXRun(fBeginDateUst, fDelayedUsecs);
423  goto retry; /* recoverable error*/
424  }
425 
426  if (nframes != fEngineControl->fBufferSize)
427  jack_log("JackAlsaDriver::Read warning fBufferSize = %ld nframes = %ld", fEngineControl->fBufferSize, nframes);
428 
429  // Has to be done before read
430  JackDriver::CycleIncTime();
431 
432  return alsa_driver_read((alsa_driver_t *)fDriver, fEngineControl->fBufferSize);
433 }
434 
435 int JackAlsaDriver::Write()
436 {
437  return alsa_driver_write((alsa_driver_t *)fDriver, fEngineControl->fBufferSize);
438 }
439 
440 void JackAlsaDriver::ReadInputAux(jack_nframes_t orig_nframes, snd_pcm_sframes_t contiguous, snd_pcm_sframes_t nread)
441 {
442  for (int chn = 0; chn < fCaptureChannels; chn++) {
443  if (fGraphManager->GetConnectionsNum(fCapturePortList[chn]) > 0) {
444  jack_default_audio_sample_t* buf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[chn], orig_nframes);
445  alsa_driver_read_from_channel((alsa_driver_t *)fDriver, chn, buf + nread, contiguous);
446  }
447  }
448 }
449 
450 void JackAlsaDriver::MonitorInputAux()
451 {
452  for (int chn = 0; chn < fCaptureChannels; chn++) {
453  JackPort* port = fGraphManager->GetPort(fCapturePortList[chn]);
454  if (port->MonitoringInput()) {
455  ((alsa_driver_t *)fDriver)->input_monitor_mask |= (1 << chn);
456  }
457  }
458 }
459 
460 void JackAlsaDriver::ClearOutputAux()
461 {
462  for (int chn = 0; chn < fPlaybackChannels; chn++) {
463  jack_default_audio_sample_t* buf =
464  (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[chn], fEngineControl->fBufferSize);
465  memset(buf, 0, sizeof (jack_default_audio_sample_t) * fEngineControl->fBufferSize);
466  }
467 }
468 
469 void JackAlsaDriver::SetTimetAux(jack_time_t time)
470 {
471  fBeginDateUst = time;
472 }
473 
474 int JackAlsaDriver::PortSetDeviceMetadata(jack_port_id_t port_id, const char* pretty_name)
475 {
476  return fEngine->PortSetDeviceMetadata(fClientControl.fRefNum, port_id, pretty_name);
477 }
478 
479 void JackAlsaDriver::WriteOutputAux(jack_nframes_t orig_nframes, snd_pcm_sframes_t contiguous, snd_pcm_sframes_t nwritten)
480 {
481  for (int chn = 0; chn < fPlaybackChannels; chn++) {
482  // Output ports
483  if (fGraphManager->GetConnectionsNum(fPlaybackPortList[chn]) > 0) {
484  jack_default_audio_sample_t* buf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[chn], orig_nframes);
485  alsa_driver_write_to_channel(((alsa_driver_t *)fDriver), chn, buf + nwritten, contiguous);
486  // Monitor ports
487  if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[chn]) > 0) {
488  jack_default_audio_sample_t* monbuf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[chn], orig_nframes);
489  memcpy(monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
490  }
491  }
492  }
493 }
494 
495 int JackAlsaDriver::is_realtime() const
496 {
497  return fEngineControl->fRealTime;
498 }
499 
500 int JackAlsaDriver::create_thread(pthread_t *thread, int priority, int realtime, void *(*start_routine)(void*), void *arg)
501 {
502 #ifdef __ANDROID__
503  return JackAndroidThread::StartImp(thread, priority, realtime, start_routine, arg);
504 #else
505  return JackPosixThread::StartImp(thread, priority, realtime, start_routine, arg);
506 #endif
507 }
508 
509 jack_port_id_t JackAlsaDriver::port_register(const char *port_name, const char *port_type, unsigned long flags, unsigned long buffer_size)
510 {
511  jack_port_id_t port_index;
512  int res = fEngine->PortRegister(fClientControl.fRefNum, port_name, port_type, flags, buffer_size, &port_index);
513  return (res == 0) ? port_index : 0;
514 }
515 
516 int JackAlsaDriver::port_unregister(jack_port_id_t port_index)
517 {
518  return fEngine->PortUnRegister(fClientControl.fRefNum, port_index);
519 }
520 
521 void* JackAlsaDriver::port_get_buffer(int port, jack_nframes_t nframes)
522 {
523  return fGraphManager->GetBuffer(port, nframes);
524 }
525 
526 int JackAlsaDriver::port_set_alias(int port, const char* name)
527 {
528  return fGraphManager->GetPort(port)->SetAlias(name);
529 }
530 
531 jack_nframes_t JackAlsaDriver::get_sample_rate() const
532 {
533  return fEngineControl->fSampleRate;
534 }
535 
536 jack_nframes_t JackAlsaDriver::frame_time() const
537 {
538  JackTimer timer;
539  fEngineControl->ReadFrameTime(&timer);
540  return timer.Time2Frames(GetMicroSeconds(), fEngineControl->fBufferSize);
541 }
542 
543 jack_nframes_t JackAlsaDriver::last_frame_time() const
544 {
545  JackTimer timer;
546  fEngineControl->ReadFrameTime(&timer);
547  return timer.CurFrame();
548 }
549 
550 } // end of namespace
551 
552 
553 #ifdef __cplusplus
554 extern "C"
555 {
556 #endif
557 
558 static
560 enum_alsa_devices()
561 {
562  snd_ctl_t * handle;
563  snd_ctl_card_info_t * info;
564  snd_pcm_info_t * pcminfo_capture;
565  snd_pcm_info_t * pcminfo_playback;
566  int card_no = -1;
568  jack_driver_param_value_t device_id;
569  char description[64];
570  int device_no;
571  bool has_capture;
572  bool has_playback;
573  jack_driver_param_constraint_desc_t * constraint_ptr;
574  uint32_t array_size = 0;
575 
576  snd_ctl_card_info_alloca(&info);
577  snd_pcm_info_alloca(&pcminfo_capture);
578  snd_pcm_info_alloca(&pcminfo_playback);
579 
580  constraint_ptr = NULL;
581 
582  while(snd_card_next(&card_no) >= 0 && card_no >= 0)
583  {
584  snprintf(card_id.str, sizeof(card_id.str), "hw:%d", card_no);
585 
586  if (snd_ctl_open(&handle, card_id.str, 0) >= 0 &&
587  snd_ctl_card_info(handle, info) >= 0)
588  {
589  snprintf(card_id.str, sizeof(card_id.str), "hw:%s", snd_ctl_card_info_get_id(info));
590  if (!jack_constraint_add_enum(
591  &constraint_ptr,
592  &array_size,
593  &card_id,
594  snd_ctl_card_info_get_name(info)))
595  goto fail;
596 
597  device_no = -1;
598 
599  while (snd_ctl_pcm_next_device(handle, &device_no) >= 0 && device_no != -1)
600  {
601  snprintf(device_id.str, sizeof(device_id.str), "%s,%d", card_id.str, device_no);
602 
603  snd_pcm_info_set_device(pcminfo_capture, device_no);
604  snd_pcm_info_set_subdevice(pcminfo_capture, 0);
605  snd_pcm_info_set_stream(pcminfo_capture, SND_PCM_STREAM_CAPTURE);
606  has_capture = snd_ctl_pcm_info(handle, pcminfo_capture) >= 0;
607 
608  snd_pcm_info_set_device(pcminfo_playback, device_no);
609  snd_pcm_info_set_subdevice(pcminfo_playback, 0);
610  snd_pcm_info_set_stream(pcminfo_playback, SND_PCM_STREAM_PLAYBACK);
611  has_playback = snd_ctl_pcm_info(handle, pcminfo_playback) >= 0;
612 
613  if (has_capture && has_playback)
614  {
615  snprintf(description, sizeof(description),"%s (duplex)", snd_pcm_info_get_name(pcminfo_capture));
616  }
617  else if (has_capture)
618  {
619  snprintf(description, sizeof(description),"%s (capture)", snd_pcm_info_get_name(pcminfo_capture));
620  }
621  else if (has_playback)
622  {
623  snprintf(description, sizeof(description),"%s (playback)", snd_pcm_info_get_name(pcminfo_playback));
624  }
625  else
626  {
627  continue;
628  }
629 
630  if (!jack_constraint_add_enum(
631  &constraint_ptr,
632  &array_size,
633  &device_id,
634  description))
635  goto fail;
636  }
637 
638  snd_ctl_close(handle);
639  }
640  }
641 
642  return constraint_ptr;
643 fail:
644  jack_constraint_free(constraint_ptr);
645  return NULL;
646 }
647 
648 static int
649 dither_opt (char c, DitherAlgorithm* dither)
650 {
651  switch (c) {
652  case '-':
653  case 'n':
654  *dither = None;
655  break;
656 
657  case 'r':
658  *dither = Rectangular;
659  break;
660 
661  case 's':
662  *dither = Shaped;
663  break;
664 
665  case 't':
666  *dither = Triangular;
667  break;
668 
669  default:
670  fprintf (stderr, "ALSA driver: illegal dithering mode %c\n", c);
671  return -1;
672  }
673  return 0;
674 }
675 
676 SERVER_EXPORT const jack_driver_desc_t* driver_get_descriptor ()
677 {
678  jack_driver_desc_t * desc;
681 
682  desc = jack_driver_descriptor_construct("alsa", JackDriverMaster, "Linux ALSA API based audio backend", &filler);
683 
684  strcpy(value.str, "hw:0");
685 #ifdef __ANDROID__
686  jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "ALSA device name", NULL);
687 #else
688  jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, enum_alsa_devices(), "ALSA device name", NULL);
689 #endif
690 
691  strcpy(value.str, "none");
692  jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Provide capture ports. Optionally set device", NULL);
693  jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Provide playback ports. Optionally set device", NULL);
694 
695  value.ui = 48000U;
696  jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
697 
698  value.ui = 1024U;
699  jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
700 
701  value.ui = 2U;
702  jack_driver_descriptor_add_parameter(desc, &filler, "nperiods", 'n', JackDriverParamUInt, &value, NULL, "Number of periods of playback latency", NULL);
703 
704  value.i = 0;
705  jack_driver_descriptor_add_parameter(desc, &filler, "hwmon", 'H', JackDriverParamBool, &value, NULL, "Hardware monitoring, if available", NULL);
706 
707  value.i = 0;
708  jack_driver_descriptor_add_parameter(desc, &filler, "hwmeter", 'M', JackDriverParamBool, &value, NULL, "Hardware metering, if available", NULL);
709 
710  value.i = 1;
711  jack_driver_descriptor_add_parameter(desc, &filler, "duplex", 'D', JackDriverParamBool, &value, NULL, "Provide both capture and playback ports", NULL);
712 
713  value.i = 0;
714  jack_driver_descriptor_add_parameter(desc, &filler, "softmode", 's', JackDriverParamBool, &value, NULL, "Soft-mode, no xrun handling", NULL);
715 
716  value.i = 0;
717  jack_driver_descriptor_add_parameter(desc, &filler, "monitor", 'm', JackDriverParamBool, &value, NULL, "Provide monitor ports for the output", NULL);
718 
719  value.c = 'n';
720  jack_driver_descriptor_add_parameter(
721  desc,
722  &filler,
723  "dither",
724  'z',
725  JackDriverParamChar,
726  &value,
727  jack_constraint_compose_enum_char(
728  JACK_CONSTRAINT_FLAG_STRICT | JACK_CONSTRAINT_FLAG_FAKE_VALUE,
729  dither_constraint_descr_array),
730  "Dithering mode",
731  NULL);
732 
733  value.ui = 0;
734  jack_driver_descriptor_add_parameter(desc, &filler, "inchannels", 'i', JackDriverParamUInt, &value, NULL, "Number of capture channels (defaults to hardware max)", NULL);
735  jack_driver_descriptor_add_parameter(desc, &filler, "outchannels", 'o', JackDriverParamUInt, &value, NULL, "Number of playback channels (defaults to hardware max)", NULL);
736 
737  value.i = FALSE;
738  jack_driver_descriptor_add_parameter(desc, &filler, "shorts", 'S', JackDriverParamBool, &value, NULL, "Try 16-bit samples before 32-bit", NULL);
739 
740  value.ui = 0;
741  jack_driver_descriptor_add_parameter(desc, &filler, "input-latency", 'I', JackDriverParamUInt, &value, NULL, "Extra input latency (frames)", NULL);
742  jack_driver_descriptor_add_parameter(desc, &filler, "output-latency", 'O', JackDriverParamUInt, &value, NULL, "Extra output latency (frames)", NULL);
743 
744  strcpy(value.str, "none");
745  jack_driver_descriptor_add_parameter(
746  desc,
747  &filler,
748  "midi-driver",
749  'X',
750  JackDriverParamString,
751  &value,
752  jack_constraint_compose_enum_str(
753  JACK_CONSTRAINT_FLAG_STRICT | JACK_CONSTRAINT_FLAG_FAKE_VALUE,
754  midi_constraint_descr_array),
755  "ALSA MIDI driver",
756  NULL);
757 
758  return desc;
759 }
760 
761 static Jack::JackAlsaDriver* g_alsa_driver;
762 
763 SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
764 {
765  jack_nframes_t srate = 48000;
766  jack_nframes_t frames_per_interrupt = 1024;
767  unsigned long user_nperiods = 2;
768  const char *playback_pcm_name = "hw:0";
769  const char *capture_pcm_name = "hw:0";
770  int hw_monitoring = FALSE;
771  int hw_metering = FALSE;
772  int capture = FALSE;
773  int playback = FALSE;
774  int soft_mode = FALSE;
775  int monitor = FALSE;
776  DitherAlgorithm dither = None;
777  int user_capture_nchnls = 0;
778  int user_playback_nchnls = 0;
779  int shorts_first = FALSE;
780  jack_nframes_t systemic_input_latency = 0;
781  jack_nframes_t systemic_output_latency = 0;
782  const JSList * node;
783  const jack_driver_param_t * param;
784  const char *midi_driver = "none";
785 
786  for (node = params; node; node = jack_slist_next (node)) {
787  param = (const jack_driver_param_t *) node->data;
788 
789  switch (param->character) {
790 
791  case 'C':
792  capture = TRUE;
793  if (strcmp (param->value.str, "none") != 0) {
794  capture_pcm_name = strdup (param->value.str);
795  jack_log("capture device %s", capture_pcm_name);
796  }
797  break;
798 
799  case 'P':
800  playback = TRUE;
801  if (strcmp (param->value.str, "none") != 0) {
802  playback_pcm_name = strdup (param->value.str);
803  jack_log("playback device %s", playback_pcm_name);
804  }
805  break;
806 
807  case 'D':
808  playback = TRUE;
809  capture = TRUE;
810  break;
811 
812  case 'd':
813  if (strcmp (param->value.str, "none") != 0) {
814  playback_pcm_name = strdup (param->value.str);
815  capture_pcm_name = strdup (param->value.str);
816  jack_log("playback device %s", playback_pcm_name);
817  jack_log("capture device %s", capture_pcm_name);
818  }
819  break;
820 
821  case 'H':
822  hw_monitoring = param->value.i;
823  break;
824 
825  case 'm':
826  monitor = param->value.i;
827  break;
828 
829  case 'M':
830  hw_metering = param->value.i;
831  break;
832 
833  case 'r':
834  srate = param->value.ui;
835  jack_log("apparent rate = %d", srate);
836  break;
837 
838  case 'p':
839  frames_per_interrupt = param->value.ui;
840  jack_log("frames per period = %d", frames_per_interrupt);
841  break;
842 
843  case 'n':
844  user_nperiods = param->value.ui;
845  if (user_nperiods < 2) { /* enforce minimum value */
846  user_nperiods = 2;
847  }
848  break;
849 
850  case 's':
851  soft_mode = param->value.i;
852  break;
853 
854  case 'z':
855  if (dither_opt (param->value.c, &dither)) {
856  return NULL;
857  }
858  break;
859 
860  case 'i':
861  user_capture_nchnls = param->value.ui;
862  break;
863 
864  case 'o':
865  user_playback_nchnls = param->value.ui;
866  break;
867 
868  case 'S':
869  shorts_first = param->value.i;
870  break;
871 
872  case 'I':
873  systemic_input_latency = param->value.ui;
874  break;
875 
876  case 'O':
877  systemic_output_latency = param->value.ui;
878  break;
879 
880  case 'X':
881  midi_driver = strdup(param->value.str);
882  break;
883  }
884  }
885 
886  /* duplex is the default */
887  if (!capture && !playback) {
888  capture = TRUE;
889  playback = TRUE;
890  }
891 
892  g_alsa_driver = new Jack::JackAlsaDriver("system", "alsa_pcm", engine, table);
893  Jack::JackDriverClientInterface* threaded_driver = new Jack::JackThreadedDriver(g_alsa_driver);
894  // Special open for ALSA driver...
895  if (g_alsa_driver->Open(frames_per_interrupt, user_nperiods, srate, hw_monitoring, hw_metering, capture, playback, dither, soft_mode, monitor,
896  user_capture_nchnls, user_playback_nchnls, shorts_first, capture_pcm_name, playback_pcm_name,
897  systemic_input_latency, systemic_output_latency, midi_driver) == 0) {
898  return threaded_driver;
899  } else {
900  delete threaded_driver; // Delete the decorated driver
901  return NULL;
902  }
903 }
904 
905 // Code to be used in alsa_driver.c
906 
907 void ReadInput(jack_nframes_t orig_nframes, snd_pcm_sframes_t contiguous, snd_pcm_sframes_t nread)
908 {
909  g_alsa_driver->ReadInputAux(orig_nframes, contiguous, nread);
910 }
911 void MonitorInput()
912 {
913  g_alsa_driver->MonitorInputAux();
914 }
915 void ClearOutput()
916 {
917  g_alsa_driver->ClearOutputAux();
918 }
919 void WriteOutput(jack_nframes_t orig_nframes, snd_pcm_sframes_t contiguous, snd_pcm_sframes_t nwritten)
920 {
921  g_alsa_driver->WriteOutputAux(orig_nframes, contiguous, nwritten);
922 }
923 void SetTime(jack_time_t time)
924 {
925  g_alsa_driver->SetTimetAux(time);
926 }
927 
928 int Restart()
929 {
930  int res;
931  if ((res = g_alsa_driver->Stop()) == 0) {
932  res = g_alsa_driver->Start();
933  }
934  return res;
935 }
936 
937 #ifdef __cplusplus
938 }
939 #endif
940 
941 
The base class for threaded drivers using a "decorator" pattern. Threaded drivers are used with block...
Locked Engine, access to methods is serialized using a mutex.
Inter process synchronization using POSIX semaphore.
The ALSA driver.
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
jack_nframes_t min
Definition: types.h:270
jack_nframes_t max
Definition: types.h:274
The base interface for drivers clients.
Definition: JackDriver.h:114
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108