1 /*
2 * JACK Audio Connection Kit input device
3 * Copyright (c) 2009 Samalyse
4 * Author: Olivier Guilyardi <olivier samalyse com>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "config.h"
25 #include <jack/jack.h>
26
38
39 /**
40 * Size of the internal FIFO buffers as a number of audio packets
41 */
42 #define FIFO_PACKETS_NUM 16
43
59
61 {
62 /* Warning: this function runs in realtime. One mustn't allocate memory here
63 * or do any other thing that could block. */
64
68 jack_nframes_t latency, cycle_delay;
70 float *pkt_data;
71 double cycle_time;
72
73 if (!self->client)
74 return 0;
75
76 /* The approximate delay since the hardware interrupt as a number of frames */
77 cycle_delay = jack_frames_since_cycle_start(self->client);
78
79 /* Retrieve filtered cycle time */
81 av_gettime() / 1000000.0 - (
double) cycle_delay / self->sample_rate,
82 self->buffer_size);
83
84 /* Check if an empty packet is available, and if there's enough space to send it back once filled */
87 self->pkt_xrun = 1;
88 return 0;
89 }
90
91 /* Retrieve empty (but allocated) packet */
93
95 latency = 0;
96
97 /* Copy and interleave audio data from the JACK buffer into the packet */
98 for (
i = 0;
i <
self->nports;
i++) {
99 jack_latency_range_t
range;
100 jack_port_get_latency_range(self->ports[
i], JackCaptureLatency, &
range);
101 latency +=
range.max;
102 buffer = jack_port_get_buffer(self->ports[
i], self->buffer_size);
103 for (j = 0; j < self->buffer_size; j++)
104 pkt_data[j * self->nports +
i] =
buffer[j];
105 }
106
107 /* Timestamp the packet with the cycle start time minus the average latency */
108 pkt.
pts = (cycle_time - (
double) latency / (self->nports * self->sample_rate)) * 1000000.0;
109
110 /* Send the now filled packet back, and increase packet counter */
113
114 return 0;
115 }
116
118 {
121 }
122
124 {
126 self->jack_xrun = 1;
128 return 0;
129 }
130
132 {
134 int test, pkt_size =
self->buffer_size *
self->nports *
sizeof(
float);
135
136 /* Supply the process callback with new empty packets, by filling the new
137 * packets FIFO buffer with as many packets as possible. process_callback()
138 * can't do this by itself, because it can't allocate memory in realtime. */
143 }
145 }
146 return 0;
147 }
148
150 {
154
155 /* Register as a JACK client, using the context url as client name. */
156 self->client = jack_client_open(
context->url, JackNullOption, &
status);
157 if (!self->client) {
160 }
161
162 sem_init(&self->packet_count, 0, 0);
163
164 self->sample_rate = jack_get_sample_rate(self->client);
166 if (!self->ports)
168 self->buffer_size = jack_get_buffer_size(self->client);
169
170 /* Register JACK ports */
171 for (
i = 0;
i <
self->nports;
i++) {
172 char str[32];
173 snprintf(str,
sizeof(str),
"input_%d",
i + 1);
174 self->ports[
i] = jack_port_register(self->client, str,
175 JACK_DEFAULT_AUDIO_TYPE,
176 JackPortIsInput, 0);
177 if (!self->ports[
i]) {
180 jack_client_close(self->client);
182 }
183 }
184
185 /* Register JACK callbacks */
189
190 /* Create time filter */
191 self->timefilter =
ff_timefilter_new (1.0 / self->sample_rate, self->buffer_size, 1.5);
192 if (!self->timefilter) {
193 jack_client_close(self->client);
195 }
196
197 /* Create FIFO buffers */
199 /* New packets FIFO with one extra packet for safety against underruns */
201 if (!self->new_pkts) {
202 jack_client_close(self->client);
204 }
206 jack_client_close(self->client);
208 }
209
210 return 0;
211
212 }
213
215 {
221 }
222
224 {
225 if (self->client) {
226 if (self->activated)
227 jack_deactivate(self->client);
228 jack_client_close(self->client);
229 }
235 }
236
238 {
242
245
247 if (!stream) {
250 }
251
253 #if HAVE_BIGENDIAN
255 #else
257 #endif
260
262 return 0;
263 }
264
266 {
268 struct timespec timeout = {0, 0};
270
271 /* Activate the JACK client on first packet read. Activating the JACK client
272 * means that process_callback() starts to get called at regular interval.
273 * If we activate it in audio_read_header(), we're actually reading audio data
274 * from the device before instructed to, and that may result in an overrun. */
275 if (!self->activated) {
276 if (!jack_activate(self->client)) {
277 self->activated = 1;
279 "JACK client registered and activated (rate=%dHz, buffer_size=%d frames)\n",
280 self->sample_rate, self->buffer_size);
281 } else {
284 }
285 }
286
287 /* Wait for a packet coming back from process_callback(), if one isn't available yet */
290 if (errno == ETIMEDOUT) {
292 "Input error: timed out when waiting for JACK process callback output\n");
293 } else {
297 }
298 if (!self->client)
300
302 }
303
304 if (self->pkt_xrun) {
306 self->pkt_xrun = 0;
307 }
308
309 if (self->jack_xrun) {
311 self->jack_xrun = 0;
312 }
313
314 /* Retrieve the packet filled with audio data by process_callback() */
316
319
320 return 0;
321 }
322
324 {
327 return 0;
328 }
329
330 #define OFFSET(x) offsetof(JackData, x)
334 };
335
342 };
343
353 };