00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "config.h"
00024 #include <semaphore.h>
00025 #include <jack/jack.h>
00026
00027 #include "libavutil/log.h"
00028 #include "libavutil/fifo.h"
00029 #include "libavutil/opt.h"
00030 #include "libavutil/time.h"
00031 #include "libavcodec/avcodec.h"
00032 #include "libavformat/avformat.h"
00033 #include "libavformat/internal.h"
00034 #include "timefilter.h"
00035 #include "avdevice.h"
00036
00040 #define FIFO_PACKETS_NUM 16
00041
00042 typedef struct {
00043 AVClass *class;
00044 jack_client_t * client;
00045 int activated;
00046 sem_t packet_count;
00047 jack_nframes_t sample_rate;
00048 jack_nframes_t buffer_size;
00049 jack_port_t ** ports;
00050 int nports;
00051 TimeFilter * timefilter;
00052 AVFifoBuffer * new_pkts;
00053 AVFifoBuffer * filled_pkts;
00054 int pkt_xrun;
00055 int jack_xrun;
00056 } JackData;
00057
00058 static int process_callback(jack_nframes_t nframes, void *arg)
00059 {
00060
00061
00062
00063 int i, j;
00064 JackData *self = arg;
00065 float * buffer;
00066 jack_nframes_t latency, cycle_delay;
00067 AVPacket pkt;
00068 float *pkt_data;
00069 double cycle_time;
00070
00071 if (!self->client)
00072 return 0;
00073
00074
00075 cycle_delay = jack_frames_since_cycle_start(self->client);
00076
00077
00078 cycle_time = ff_timefilter_update(self->timefilter,
00079 av_gettime() / 1000000.0 - (double) cycle_delay / self->sample_rate,
00080 self->buffer_size);
00081
00082
00083 if ((av_fifo_size(self->new_pkts) < sizeof(pkt)) || (av_fifo_space(self->filled_pkts) < sizeof(pkt))) {
00084 self->pkt_xrun = 1;
00085 return 0;
00086 }
00087
00088
00089 av_fifo_generic_read(self->new_pkts, &pkt, sizeof(pkt), NULL);
00090
00091 pkt_data = (float *) pkt.data;
00092 latency = 0;
00093
00094
00095 for (i = 0; i < self->nports; i++) {
00096 #if HAVE_JACK_PORT_GET_LATENCY_RANGE
00097 jack_latency_range_t range;
00098 jack_port_get_latency_range(self->ports[i], JackCaptureLatency, &range);
00099 latency += range.max;
00100 #else
00101 latency += jack_port_get_total_latency(self->client, self->ports[i]);
00102 #endif
00103 buffer = jack_port_get_buffer(self->ports[i], self->buffer_size);
00104 for (j = 0; j < self->buffer_size; j++)
00105 pkt_data[j * self->nports + i] = buffer[j];
00106 }
00107
00108
00109 pkt.pts = (cycle_time - (double) latency / (self->nports * self->sample_rate)) * 1000000.0;
00110
00111
00112 av_fifo_generic_write(self->filled_pkts, &pkt, sizeof(pkt), NULL);
00113 sem_post(&self->packet_count);
00114
00115 return 0;
00116 }
00117
00118 static void shutdown_callback(void *arg)
00119 {
00120 JackData *self = arg;
00121 self->client = NULL;
00122 }
00123
00124 static int xrun_callback(void *arg)
00125 {
00126 JackData *self = arg;
00127 self->jack_xrun = 1;
00128 ff_timefilter_reset(self->timefilter);
00129 return 0;
00130 }
00131
00132 static int supply_new_packets(JackData *self, AVFormatContext *context)
00133 {
00134 AVPacket pkt;
00135 int test, pkt_size = self->buffer_size * self->nports * sizeof(float);
00136
00137
00138
00139
00140 while (av_fifo_space(self->new_pkts) >= sizeof(pkt)) {
00141 if ((test = av_new_packet(&pkt, pkt_size)) < 0) {
00142 av_log(context, AV_LOG_ERROR, "Could not create packet of size %d\n", pkt_size);
00143 return test;
00144 }
00145 av_fifo_generic_write(self->new_pkts, &pkt, sizeof(pkt), NULL);
00146 }
00147 return 0;
00148 }
00149
00150 static int start_jack(AVFormatContext *context)
00151 {
00152 JackData *self = context->priv_data;
00153 jack_status_t status;
00154 int i, test;
00155
00156
00157 self->client = jack_client_open(context->filename, JackNullOption, &status);
00158 if (!self->client) {
00159 av_log(context, AV_LOG_ERROR, "Unable to register as a JACK client\n");
00160 return AVERROR(EIO);
00161 }
00162
00163 sem_init(&self->packet_count, 0, 0);
00164
00165 self->sample_rate = jack_get_sample_rate(self->client);
00166 self->ports = av_malloc(self->nports * sizeof(*self->ports));
00167 self->buffer_size = jack_get_buffer_size(self->client);
00168
00169
00170 for (i = 0; i < self->nports; i++) {
00171 char str[16];
00172 snprintf(str, sizeof(str), "input_%d", i + 1);
00173 self->ports[i] = jack_port_register(self->client, str,
00174 JACK_DEFAULT_AUDIO_TYPE,
00175 JackPortIsInput, 0);
00176 if (!self->ports[i]) {
00177 av_log(context, AV_LOG_ERROR, "Unable to register port %s:%s\n",
00178 context->filename, str);
00179 jack_client_close(self->client);
00180 return AVERROR(EIO);
00181 }
00182 }
00183
00184
00185 jack_set_process_callback(self->client, process_callback, self);
00186 jack_on_shutdown(self->client, shutdown_callback, self);
00187 jack_set_xrun_callback(self->client, xrun_callback, self);
00188
00189
00190 self->timefilter = ff_timefilter_new (1.0 / self->sample_rate, self->buffer_size, 1.5);
00191
00192
00193 self->filled_pkts = av_fifo_alloc(FIFO_PACKETS_NUM * sizeof(AVPacket));
00194
00195 self->new_pkts = av_fifo_alloc((FIFO_PACKETS_NUM + 1) * sizeof(AVPacket));
00196 if ((test = supply_new_packets(self, context))) {
00197 jack_client_close(self->client);
00198 return test;
00199 }
00200
00201 return 0;
00202
00203 }
00204
00205 static void free_pkt_fifo(AVFifoBuffer *fifo)
00206 {
00207 AVPacket pkt;
00208 while (av_fifo_size(fifo)) {
00209 av_fifo_generic_read(fifo, &pkt, sizeof(pkt), NULL);
00210 av_free_packet(&pkt);
00211 }
00212 av_fifo_free(fifo);
00213 }
00214
00215 static void stop_jack(JackData *self)
00216 {
00217 if (self->client) {
00218 if (self->activated)
00219 jack_deactivate(self->client);
00220 jack_client_close(self->client);
00221 }
00222 sem_destroy(&self->packet_count);
00223 free_pkt_fifo(self->new_pkts);
00224 free_pkt_fifo(self->filled_pkts);
00225 av_freep(&self->ports);
00226 ff_timefilter_destroy(self->timefilter);
00227 }
00228
00229 static int audio_read_header(AVFormatContext *context)
00230 {
00231 JackData *self = context->priv_data;
00232 AVStream *stream;
00233 int test;
00234
00235 if ((test = start_jack(context)))
00236 return test;
00237
00238 stream = avformat_new_stream(context, NULL);
00239 if (!stream) {
00240 stop_jack(self);
00241 return AVERROR(ENOMEM);
00242 }
00243
00244 stream->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00245 #if HAVE_BIGENDIAN
00246 stream->codec->codec_id = AV_CODEC_ID_PCM_F32BE;
00247 #else
00248 stream->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
00249 #endif
00250 stream->codec->sample_rate = self->sample_rate;
00251 stream->codec->channels = self->nports;
00252
00253 avpriv_set_pts_info(stream, 64, 1, 1000000);
00254 return 0;
00255 }
00256
00257 static int audio_read_packet(AVFormatContext *context, AVPacket *pkt)
00258 {
00259 JackData *self = context->priv_data;
00260 struct timespec timeout = {0, 0};
00261 int test;
00262
00263
00264
00265
00266
00267 if (!self->activated) {
00268 if (!jack_activate(self->client)) {
00269 self->activated = 1;
00270 av_log(context, AV_LOG_INFO,
00271 "JACK client registered and activated (rate=%dHz, buffer_size=%d frames)\n",
00272 self->sample_rate, self->buffer_size);
00273 } else {
00274 av_log(context, AV_LOG_ERROR, "Unable to activate JACK client\n");
00275 return AVERROR(EIO);
00276 }
00277 }
00278
00279
00280 timeout.tv_sec = av_gettime() / 1000000 + 2;
00281 if (sem_timedwait(&self->packet_count, &timeout)) {
00282 if (errno == ETIMEDOUT) {
00283 av_log(context, AV_LOG_ERROR,
00284 "Input error: timed out when waiting for JACK process callback output\n");
00285 } else {
00286 av_log(context, AV_LOG_ERROR, "Error while waiting for audio packet: %s\n",
00287 strerror(errno));
00288 }
00289 if (!self->client)
00290 av_log(context, AV_LOG_ERROR, "Input error: JACK server is gone\n");
00291
00292 return AVERROR(EIO);
00293 }
00294
00295 if (self->pkt_xrun) {
00296 av_log(context, AV_LOG_WARNING, "Audio packet xrun\n");
00297 self->pkt_xrun = 0;
00298 }
00299
00300 if (self->jack_xrun) {
00301 av_log(context, AV_LOG_WARNING, "JACK xrun\n");
00302 self->jack_xrun = 0;
00303 }
00304
00305
00306 av_fifo_generic_read(self->filled_pkts, pkt, sizeof(*pkt), NULL);
00307
00308 if ((test = supply_new_packets(self, context)))
00309 return test;
00310
00311 return 0;
00312 }
00313
00314 static int audio_read_close(AVFormatContext *context)
00315 {
00316 JackData *self = context->priv_data;
00317 stop_jack(self);
00318 return 0;
00319 }
00320
00321 #define OFFSET(x) offsetof(JackData, x)
00322 static const AVOption options[] = {
00323 { "channels", "Number of audio channels.", OFFSET(nports), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00324 { NULL },
00325 };
00326
00327 static const AVClass jack_indev_class = {
00328 .class_name = "JACK indev",
00329 .item_name = av_default_item_name,
00330 .option = options,
00331 .version = LIBAVUTIL_VERSION_INT,
00332 };
00333
00334 AVInputFormat ff_jack_demuxer = {
00335 .name = "jack",
00336 .long_name = NULL_IF_CONFIG_SMALL("JACK Audio Connection Kit"),
00337 .priv_data_size = sizeof(JackData),
00338 .read_header = audio_read_header,
00339 .read_packet = audio_read_packet,
00340 .read_close = audio_read_close,
00341 .flags = AVFMT_NOFILE,
00342 .priv_class = &jack_indev_class,
00343 };