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
|
/*
Copyright (C) 2015 by Bernd Buschinski <b.buschinski@gmail.com>
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, see <http://www.gnu.org/licenses/>.
*/
#include "jack-wrapper.h"
#include <util/threading.h>
#include <stdio.h>
#include <util/platform.h>
#define blog(level, msg, ...) blog(level, "jack-input: " msg, ##__VA_ARGS__)
/**
* Get obs speaker layout from number of channels
*
* @param channels number of channels reported by jack
*
* @return obs speaker_layout id
*
* @note This *might* not work for some rather unusual setups, but should work
* fine for the majority of cases.
*/
static enum speaker_layout jack_channels_to_obs_speakers(uint_fast32_t channels)
{
switch (channels) {
case 1:
return SPEAKERS_MONO;
case 2:
return SPEAKERS_STEREO;
case 3:
return SPEAKERS_2POINT1;
case 4:
return SPEAKERS_4POINT0;
case 5:
return SPEAKERS_4POINT1;
case 6:
return SPEAKERS_5POINT1;
/* What should we do with 7 channels? */
/* case 7: return SPEAKERS_...; */
case 8:
return SPEAKERS_7POINT1;
}
return SPEAKERS_UNKNOWN;
}
int jack_process_callback(jack_nframes_t nframes, void *arg)
{
struct jack_data *data = (struct jack_data *)arg;
jack_nframes_t current_frames;
jack_time_t current_usecs, next_usecs;
float period_usecs;
uint64_t now = os_gettime_ns();
if (data == 0)
return 0;
struct obs_source_audio out;
out.speakers = jack_channels_to_obs_speakers(data->channels);
out.samples_per_sec = jack_get_sample_rate(data->jack_client);
/* format is always 32 bit float for jack */
out.format = AUDIO_FORMAT_FLOAT_PLANAR;
for (unsigned int i = 0; i < data->channels; ++i) {
jack_default_audio_sample_t *jack_buffer =
(jack_default_audio_sample_t *)jack_port_get_buffer(
data->jack_ports[i], nframes);
out.data[i] = (uint8_t *)jack_buffer;
}
out.frames = nframes;
if (!jack_get_cycle_times(data->jack_client, ¤t_frames,
¤t_usecs, &next_usecs, &period_usecs)) {
out.timestamp = now - (int64_t)(period_usecs * 1000);
} else {
out.timestamp = now - util_mul_div64(nframes, 1000000000ULL,
data->samples_per_sec);
blog(LOG_WARNING,
"jack_get_cycle_times error: guessing timestamp");
}
/* FIXME: this function is not realtime-safe, we should do something
* about this */
obs_source_output_audio(data->source, &out);
return 0;
}
int_fast32_t jack_init(struct jack_data *data)
{
pthread_mutex_lock(&data->jack_mutex);
if (data->jack_client != NULL)
goto good;
jack_options_t jack_option =
data->start_jack_server ? JackNullOption : JackNoStartServer;
data->jack_client = jack_client_open(data->device, jack_option, 0);
if (data->jack_client == NULL) {
blog(LOG_ERROR,
"jack_client_open Error:"
"Could not create JACK client! %s",
data->device);
goto error;
}
data->jack_ports =
(jack_port_t **)bzalloc(sizeof(jack_port_t *) * data->channels);
for (unsigned int i = 0; i < data->channels; ++i) {
char port_name[10] = {'\0'};
snprintf(port_name, sizeof(port_name), "in_%u", i + 1);
data->jack_ports[i] = jack_port_register(
data->jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE,
JackPortIsInput | JackPortIsTerminal, 0);
if (data->jack_ports[i] == NULL) {
blog(LOG_ERROR,
"jack_port_register Error:"
"Could not create JACK port! %s",
port_name);
goto error;
}
}
if (jack_set_process_callback(data->jack_client, jack_process_callback,
data) != 0) {
blog(LOG_ERROR, "jack_set_process_callback Error");
goto error;
}
if (jack_activate(data->jack_client) != 0) {
blog(LOG_ERROR, "jack_activate Error:"
"Could not activate JACK client!");
goto error;
}
good:
pthread_mutex_unlock(&data->jack_mutex);
return 0;
error:
pthread_mutex_unlock(&data->jack_mutex);
return 1;
}
void deactivate_jack(struct jack_data *data)
{
pthread_mutex_lock(&data->jack_mutex);
if (data->jack_client) {
jack_client_close(data->jack_client);
if (data->jack_ports != NULL) {
bfree(data->jack_ports);
data->jack_ports = NULL;
}
data->jack_client = NULL;
}
pthread_mutex_unlock(&data->jack_mutex);
}
|