25 #include <jack/jack.h>
38 #if HAVE_DISPATCH_DISPATCH_H
39 #include <dispatch/dispatch.h>
40 #define sem_t dispatch_semaphore_t
41 #define sem_init(psem,x,val) *psem = dispatch_semaphore_create(val)
42 #define sem_post(psem) dispatch_semaphore_signal(*psem)
43 #define sem_wait(psem) dispatch_semaphore_wait(*psem, DISPATCH_TIME_FOREVER)
44 #define sem_timedwait(psem, val) dispatch_semaphore_wait(*psem, dispatch_walltime(val, 0))
45 #define sem_destroy(psem) dispatch_release(*psem)
51 #define FIFO_PACKETS_NUM 16
77 jack_nframes_t latency, cycle_delay;
86 cycle_delay = jack_frames_since_cycle_start(self->client);
90 av_gettime() / 1000000.0 - (
double) cycle_delay / self->sample_rate,
102 pkt_data = (
float *) pkt.
data;
106 for (i = 0; i <
self->nports; i++) {
107 #if HAVE_JACK_PORT_GET_LATENCY_RANGE
108 jack_latency_range_t range;
109 jack_port_get_latency_range(self->ports[i], JackCaptureLatency, &range);
110 latency += range.max;
112 latency += jack_port_get_total_latency(self->client, self->ports[i]);
114 buffer = jack_port_get_buffer(self->ports[i], self->buffer_size);
115 for (j = 0; j <
self->buffer_size; j++)
116 pkt_data[j * self->nports + i] = buffer[j];
120 pkt.
pts = (cycle_time - (double) latency / (self->nports * self->sample_rate)) * 1000000.0;
146 int test, pkt_size =
self->buffer_size *
self->nports *
sizeof(float);
164 jack_status_t status;
168 self->client = jack_client_open(context->
filename, JackNullOption, &status);
174 sem_init(&self->packet_count, 0, 0);
176 self->sample_rate = jack_get_sample_rate(self->client);
180 self->buffer_size = jack_get_buffer_size(self->client);
183 for (i = 0; i <
self->nports; i++) {
185 snprintf(str,
sizeof(str),
"input_%d", i + 1);
186 self->ports[i] = jack_port_register(self->client, str,
187 JACK_DEFAULT_AUDIO_TYPE,
189 if (!self->ports[i]) {
192 jack_client_close(self->client);
203 self->timefilter =
ff_timefilter_new (1.0 / self->sample_rate, self->buffer_size, 1.5);
204 if (!self->timefilter) {
205 jack_client_close(self->client);
213 if (!self->new_pkts) {
214 jack_client_close(self->client);
218 jack_client_close(self->client);
240 jack_deactivate(self->client);
241 jack_client_close(self->client);
281 struct timespec timeout = {0, 0};
288 if (!self->activated) {
289 if (!jack_activate(self->client)) {
292 "JACK client registered and activated (rate=%dHz, buffer_size=%d frames)\n",
293 self->sample_rate, self->buffer_size);
303 if (errno == ETIMEDOUT) {
305 "Input error: timed out when waiting for JACK process callback output\n");
319 if (self->pkt_xrun) {
324 if (self->jack_xrun) {
345 #define OFFSET(x) offsetof(JackData, x)
367 .priv_class = &jack_indev_class,
void ff_timefilter_destroy(TimeFilter *self)
Free all resources associated with the filter.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
void ff_timefilter_reset(TimeFilter *self)
Reset the filter.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Opaque type representing a time filter state.
static int supply_new_packets(JackData *self, AVFormatContext *context)
int av_fifo_space(const AVFifoBuffer *f)
Return the amount of space in bytes in the AVFifoBuffer, that is the amount of data you can write int...
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
AVInputFormat ff_jack_demuxer
AVFifoBuffer * filled_pkts
static void free_pkt_fifo(AVFifoBuffer **fifo)
static av_cold int read_close(AVFormatContext *ctx)
#define FIFO_PACKETS_NUM
Size of the internal FIFO buffers as a number of audio packets.
Main libavdevice API header.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
static const AVOption options[]
enum AVMediaType codec_type
General type of the encoded data.
#define sem_destroy(psem)
static int start_jack(AVFormatContext *context)
common internal API header
char filename[1024]
input or output filename
#define sem_timedwait(psem, val)
static int read_header(FFV1Context *f)
int64_t av_gettime(void)
Get the current time in microseconds.
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define AV_LOG_INFO
Standard information.
TimeFilter * ff_timefilter_new(double time_base, double period, double bandwidth)
Create a new Delay Locked Loop time filter.
Libavcodec external API header.
static int audio_read_packet(AVFormatContext *context, AVPacket *pkt)
int av_fifo_size(const AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
static void test(const char *pattern, const char *host)
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
jack_nframes_t sample_rate
a very simple circular buffer FIFO implementation
Describe the class of an AVClass context structure.
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
static int process_callback(jack_nframes_t nframes, void *arg)
static void shutdown_callback(void *arg)
AVFifoBuffer * av_fifo_alloc_array(size_t nmemb, size_t size)
Initialize an AVFifoBuffer.
static int audio_read_close(AVFormatContext *context)
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
int sample_rate
Audio only.
double ff_timefilter_update(TimeFilter *self, double system_time, double period)
Update the filter.
static int audio_read_header(AVFormatContext *context)
static int xrun_callback(void *arg)
static const AVClass jack_indev_class
void * priv_data
Format private data.
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
jack_nframes_t buffer_size
AVCodecParameters * codecpar
#define av_malloc_array(a, b)
This structure stores compressed data.
static void stop_jack(JackData *self)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...