summaryrefslogtreecommitdiff
path: root/drivers/dummy/dummy_driver.c
blob: de944794785c6bd646fa53c3265ebb7319506a35 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
    Copyright (C) 2003 Robert Ham <rah@bash.sh>
    Copyright (C) 2001 Paul Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    $Id$
*/

#include <stdio.h>
#include <memory.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>
#include <getopt.h>

#include <jack/types.h>
#include <jack/internal.h>
#include <jack/engine.h>
#include <jack/time.h>

#include "dummy_driver.h"

#undef DEBUG_WAKEUP


static int
dummy_driver_audio_start (dummy_driver_t *driver)
{
  return 0;
}

static int
dummy_driver_audio_stop (dummy_driver_t *driver)
{
  return 0;
}

static jack_nframes_t 
dummy_driver_wait (dummy_driver_t *driver, int extra_fd, int *status, float *delayed_usecs)
{
  jack_time_t processing_time;

  processing_time = driver->last_wait_ust
    ? jack_get_microseconds() - driver->last_wait_ust
    : 0;

  if (processing_time < driver->wait_time)
    usleep (driver->wait_time - processing_time);


  driver->last_wait_ust = jack_get_microseconds ();

  *status = 0;
  *delayed_usecs = 0.0;

  return driver->period_size;
}

static int
dummy_driver_null_cycle (dummy_driver_t* driver, jack_nframes_t nframes)
{
  return 0;
}

static int
dummy_driver_read (dummy_driver_t *driver, jack_nframes_t nframes)
{
  return 0;
}

static int
dummy_driver_write (dummy_driver_t* driver, jack_nframes_t nframes)
{
  return 0;
}


static int
dummy_driver_attach (dummy_driver_t *driver, jack_engine_t *engine)
{
  jack_port_t * port;
  char buf[32];
  unsigned int chn;
  int port_flags;

  driver->engine = engine;

  driver->engine->set_buffer_size (engine, driver->period_size);
  driver->engine->set_sample_rate (engine, driver->sample_rate);

  port_flags = JackPortIsOutput|JackPortIsPhysical|JackPortIsTerminal;

  for (chn = 0; chn < driver->capture_channels; chn++)
    {
      snprintf (buf, sizeof(buf) - 1, "capture_%u", chn+1);

      port = jack_port_register (driver->client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0);
      if (!port)
	{
	  jack_error ("DUMMY: cannot register port for %s", buf);
	  break;
	}

      driver->capture_ports = jack_slist_append (driver->capture_ports, port);
    }
	
  port_flags = JackPortIsInput|JackPortIsPhysical|JackPortIsTerminal;

  for (chn = 0; chn < driver->playback_channels; chn++)
    {
      snprintf (buf, sizeof(buf) - 1, "playback_%u", chn+1);

      port = jack_port_register (driver->client, buf, JACK_DEFAULT_AUDIO_TYPE, port_flags, 0);

      if (!port)
	{
	  jack_error ("DUMMY: cannot register port for %s", buf);
	  break;
	}

      driver->playback_ports = jack_slist_append (driver->playback_ports, port);
    }

  jack_activate (driver->client);

  return 0;
}

static void
dummy_driver_detach (dummy_driver_t *driver, jack_engine_t *engine)
{
  JSList * node;

  if (driver->engine == 0)
    return;

  for (node = driver->capture_ports; node; node = jack_slist_next (node))
    jack_port_unregister (driver->client, ((jack_port_t *) node->data));

  jack_slist_free (driver->capture_ports);
  driver->capture_ports = NULL;

		
  for (node = driver->playback_ports; node; node = jack_slist_next (node))
    jack_port_unregister (driver->client, ((jack_port_t *) node->data));

  jack_slist_free (driver->playback_ports);
  driver->playback_ports = NULL;
	
  driver->engine = NULL;
}

static void
dummy_driver_delete (dummy_driver_t *driver)
{
  free (driver);
}

static jack_driver_t *
dummy_driver_new (jack_client_t * client,
		  char *name,
		  unsigned int capture_ports,
		  unsigned int playback_ports,
		  jack_nframes_t sample_rate,
		  jack_nframes_t period_size,
		  unsigned long wait_time)
{
  dummy_driver_t * driver;

  printf ("creating dummy driver ... %s|%" PRIu32 "|%" PRIu32
	  "|%lu|%u|%u\n", name, sample_rate, period_size, wait_time,
	  capture_ports, playback_ports);

  driver = (dummy_driver_t *) calloc (1, sizeof (dummy_driver_t));

  jack_driver_init ((jack_driver_t *) driver);

  driver->attach     = (JackDriverAttachFunction)    dummy_driver_attach;
  driver->detach     = (JackDriverDetachFunction)    dummy_driver_detach;
  driver->wait       = (JackDriverWaitFunction)      dummy_driver_wait;
  driver->read       = (JackDriverReadFunction)      dummy_driver_read;
  driver->write      = (JackDriverReadFunction)      dummy_driver_write;
  driver->null_cycle = (JackDriverNullCycleFunction) dummy_driver_null_cycle;
  driver->start      = (JackDriverStartFunction)     dummy_driver_audio_start;
  driver->stop       = (JackDriverStopFunction)      dummy_driver_audio_stop;

  driver->period_usecs = (((float)period_size) / ((float)sample_rate)) / 1000000.0;

  driver->sample_rate = sample_rate;
  driver->period_size = period_size;
  driver->wait_time   = wait_time;
  driver->last_wait_ust = 0;

  driver->capture_channels  = capture_ports;
  driver->capture_ports     = NULL;
  driver->playback_channels = playback_ports;
  driver->playback_ports    = NULL;

  driver->client = client;
  driver->engine = NULL;

  return (jack_driver_t *) driver;
}

static void
dummy_usage ()
{
	fprintf (stderr, "\n"
"dummy driver arguments:\n"
"    -h,--help    \tprint this message\n"
"    -r,--rate <n>      \tsample rate (default: 48000)\n"
"    -p,--period <n>    \tframes per period (default: 1024)\n"
"    -C,--capture <n> \tnumber of capture ports (default: 2)\n"
"    -P,--playback <n> \tnumber of playback ports (default: 2)\n"
"    -w,--wait <usecs> \tnumber of usecs to wait between engine processes (default: 21333)\n"
"\n");
}

static void
dummy_error (char *type, char *value)
{
	fprintf (stderr, "dummy driver: unknown %s: `%s'\n", type, value);
	dummy_usage();
}



/* DRIVER "PLUGIN" INTERFACE */

const char driver_client_name[] = "dummy_pcm";

jack_driver_t *
driver_initialize (jack_client_t *client, int argc, char **argv)
{
	jack_nframes_t sample_rate = 48000;
	jack_nframes_t period_size = 1024;
	unsigned int capture_ports = 2;
	unsigned int playback_ports = 2;
	int wait_time_set = 0;
	unsigned long wait_time;

	int opt;
	char optstring[2];		/* string made from opt char */
	struct option long_options[] = 
	{ 
		{ "help",	no_argument, NULL, 'h' },
		{ "rate",	required_argument, NULL, 'r' },
		{ "period",	required_argument, NULL, 'p' },
		{ "capture",	required_argument, NULL, 'C' },
		{ "playback",	required_argument, NULL, 'P' },
		{ "wait",	required_argument, NULL, 'w' },
		{ 0, 0, 0, 0 }
	};

		
	/*
	 * Setting optind back to zero is a hack to reinitialize a new
	 * getopts() loop.  See declaration in <getopt.h>.
	 */

	optind = 0;
	opterr = 0;

	while ((opt = getopt_long(argc, argv, "C::P::p:r:w:h",
				  long_options, NULL))
	       != EOF) {
		switch (opt) {

		case 'C':
		  capture_ports = atoi (optarg);
		  break;

		case 'P':
		  playback_ports = atoi (optarg);
		  break;

		case 'p':
		  period_size = atoi(optarg);
		  break;
				
		case 'r':
		  sample_rate = atoi(optarg);
		  break;
				
		case 'w':
		  wait_time = strtoul(optarg, NULL, 10);
		  wait_time_set = 1;
		  break;
				
		case 'h':
		  dummy_usage();
		  return NULL;

		/* the rest is error handling: */
		case 1:			/* not an option */
		  dummy_error("parameter", optarg);
		  return NULL;
				
		default:		/* unrecognized option */
		  optstring[0] = (char) optopt;
		  optstring[1] = '\0';
		  dummy_error("option", optstring);
		  return NULL;
		}
	}

        if (!wait_time_set)
	  wait_time = (((float)period_size) / ((float)sample_rate)) * 1000000.0;

	return dummy_driver_new (client, "dummy_pcm", capture_ports, playback_ports,
				 sample_rate, period_size, wait_time);
}

void
driver_finish (jack_driver_t *driver)
{
	dummy_driver_delete ((dummy_driver_t *) driver);
}