2014-02-13 13:05:23 -08:00
|
|
|
/*
|
|
|
|
Copyright (C) 2014 by Leonhard Oelke <leonhard@in-verted.de>
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
2014-02-16 04:45:46 -08:00
|
|
|
|
2014-02-13 13:05:23 -08:00
|
|
|
#include <util/bmem.h>
|
|
|
|
#include <util/threading.h>
|
2014-02-18 12:46:39 -08:00
|
|
|
#include <util/platform.h>
|
2014-02-13 13:05:23 -08:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
#include <pulse/thread-mainloop.h>
|
2014-02-13 13:05:23 -08:00
|
|
|
#include <pulse/mainloop.h>
|
|
|
|
#include <pulse/context.h>
|
|
|
|
#include <pulse/introspect.h>
|
|
|
|
#include <pulse/stream.h>
|
|
|
|
#include <pulse/error.h>
|
|
|
|
|
|
|
|
#include <obs.h>
|
|
|
|
|
|
|
|
#define PULSE_DATA(voidptr) struct pulse_data *data = voidptr;
|
|
|
|
|
|
|
|
/*
|
2014-02-16 04:49:28 -08:00
|
|
|
* delay in usecs before starting to record, this eliminates problems with
|
2014-02-13 13:05:23 -08:00
|
|
|
* pulse audio sending weird data/timestamps when the stream is connected
|
2014-03-11 10:12:54 -07:00
|
|
|
*
|
2014-02-13 13:05:23 -08:00
|
|
|
* for more information see:
|
|
|
|
* github.com/MaartenBaert/ssr/blob/master/src/AV/Input/PulseAudioInput.cpp
|
|
|
|
*/
|
2014-02-16 04:49:28 -08:00
|
|
|
const uint64_t pulse_start_delay = 100000;
|
2014-02-13 13:05:23 -08:00
|
|
|
|
|
|
|
struct pulse_data {
|
2014-02-16 04:45:46 -08:00
|
|
|
pthread_t thread;
|
2014-03-11 06:16:03 -07:00
|
|
|
os_event_t event;
|
2014-02-16 04:45:46 -08:00
|
|
|
obs_source_t source;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
enum speaker_layout speakers;
|
2014-02-18 12:46:39 -08:00
|
|
|
pa_sample_format_t format;
|
2014-02-22 05:48:11 -08:00
|
|
|
uint_fast32_t samples_per_sec;
|
|
|
|
uint_fast8_t channels;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
uint_fast32_t bytes_per_frame;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
pa_mainloop *mainloop;
|
|
|
|
pa_context *context;
|
|
|
|
pa_stream *stream;
|
2014-02-18 12:46:39 -08:00
|
|
|
pa_proplist *props;
|
2014-02-13 13:05:23 -08:00
|
|
|
};
|
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
struct pulse_context_change {
|
|
|
|
pa_threaded_mainloop *mainloop;
|
|
|
|
pa_context_state_t state;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pulse_enumerate {
|
|
|
|
pa_threaded_mainloop *mainloop;
|
|
|
|
obs_property_t devices;
|
|
|
|
bool input;
|
|
|
|
};
|
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
/*
|
|
|
|
* get obs from pulse audio format
|
|
|
|
*/
|
|
|
|
static enum audio_format pulse_to_obs_audio_format(
|
|
|
|
pa_sample_format_t format)
|
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case PA_SAMPLE_U8:
|
|
|
|
return AUDIO_FORMAT_U8BIT;
|
|
|
|
case PA_SAMPLE_S16LE:
|
|
|
|
return AUDIO_FORMAT_16BIT;
|
|
|
|
case PA_SAMPLE_S24_32LE:
|
|
|
|
return AUDIO_FORMAT_32BIT;
|
|
|
|
case PA_SAMPLE_FLOAT32LE:
|
|
|
|
return AUDIO_FORMAT_FLOAT;
|
|
|
|
default:
|
|
|
|
return AUDIO_FORMAT_UNKNOWN;
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
return AUDIO_FORMAT_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get the number of frames from bytes and current format
|
|
|
|
*/
|
2014-02-22 05:48:11 -08:00
|
|
|
static uint_fast32_t frames_to_bytes(struct pulse_data *data, size_t bytes)
|
2014-02-18 12:46:39 -08:00
|
|
|
{
|
2014-02-22 05:48:11 -08:00
|
|
|
return (bytes / data->bytes_per_frame);
|
2014-02-18 12:46:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get the buffer size needed for length msec with current settings
|
|
|
|
*/
|
2014-02-22 05:48:11 -08:00
|
|
|
static uint_fast32_t get_buffer_size(struct pulse_data *data,
|
|
|
|
uint_fast32_t length)
|
2014-02-18 12:46:39 -08:00
|
|
|
{
|
2014-02-22 05:48:11 -08:00
|
|
|
return (length * data->samples_per_sec * data->bytes_per_frame) / 1000;
|
2014-02-18 12:46:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get latency for a pulse audio stream
|
|
|
|
*/
|
|
|
|
static int pulse_get_stream_latency(pa_stream *stream, int64_t *latency)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int sign;
|
|
|
|
pa_usec_t abs;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
ret = pa_stream_get_latency(stream, &abs, &sign);
|
|
|
|
*latency = (sign) ? -(int64_t) abs : (int64_t) abs;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate the mainloop
|
2014-03-11 10:12:54 -07:00
|
|
|
*
|
2014-02-18 12:46:39 -08:00
|
|
|
* The custom implementation gives better performance than the function
|
|
|
|
* provided by pulse audio, maybe due to the timeout set in prepare ?
|
|
|
|
*/
|
|
|
|
static void pulse_iterate(struct pulse_data *data)
|
|
|
|
{
|
|
|
|
if (pa_mainloop_prepare(data->mainloop, 1000) < 0) {
|
|
|
|
blog(LOG_ERROR, "Unable to prepare main loop");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pa_mainloop_poll(data->mainloop) < 0) {
|
|
|
|
blog(LOG_ERROR, "Unable to poll main loop");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pa_mainloop_dispatch(data->mainloop) < 0)
|
|
|
|
blog(LOG_ERROR, "Unable to dispatch main loop");
|
|
|
|
}
|
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
/*
|
|
|
|
* Server info callback, this is called from pa_mainloop_dispatch
|
|
|
|
* TODO: how to free the server info struct ?
|
|
|
|
*/
|
|
|
|
static void pulse_get_server_info_cb(pa_context *c, const pa_server_info *i,
|
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(c);
|
|
|
|
PULSE_DATA(userdata);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
const pa_sample_spec *spec = &i->sample_spec;
|
|
|
|
data->format = spec->format;
|
|
|
|
data->samples_per_sec = spec->rate;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
blog(LOG_DEBUG, "pulse-input: Default format: %s, %u Hz, %u channels",
|
|
|
|
pa_sample_format_to_string(spec->format),
|
|
|
|
spec->rate,
|
|
|
|
spec->channels);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Request pulse audio server info
|
|
|
|
* TODO: handle failures ?
|
|
|
|
*/
|
|
|
|
static int pulse_get_server_info(struct pulse_data *data)
|
|
|
|
{
|
|
|
|
pa_server_info_cb_t cb = pulse_get_server_info_cb;
|
|
|
|
pa_operation *op = pa_context_get_server_info(data->context, cb, data);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
for(;;) {
|
|
|
|
pulse_iterate(data);
|
|
|
|
pa_operation_state_t state = pa_operation_get_state(op);
|
|
|
|
if (state == PA_OPERATION_DONE) {
|
|
|
|
pa_operation_unref(op);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
/*
|
|
|
|
* Create a new pulse audio main loop and connect to the server
|
2014-03-11 10:12:54 -07:00
|
|
|
*
|
2014-02-16 04:45:46 -08:00
|
|
|
* Returns a negative value on error
|
|
|
|
*/
|
2014-02-13 13:05:23 -08:00
|
|
|
static int pulse_connect(struct pulse_data *data)
|
|
|
|
{
|
2014-02-16 04:45:46 -08:00
|
|
|
data->mainloop = pa_mainloop_new();
|
|
|
|
if (!data->mainloop) {
|
2014-02-18 12:46:39 -08:00
|
|
|
blog(LOG_ERROR, "pulse-input: Unable to create main loop");
|
2014-02-16 04:45:46 -08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
data->context = pa_context_new_with_proplist(
|
|
|
|
pa_mainloop_get_api(data->mainloop), "OBS Studio", data->props);
|
2014-02-16 04:45:46 -08:00
|
|
|
if (!data->context) {
|
2014-02-18 12:46:39 -08:00
|
|
|
blog(LOG_ERROR, "pulse-input: Unable to create context");
|
2014-02-16 04:45:46 -08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
int status = pa_context_connect(
|
|
|
|
data->context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
|
|
|
|
if (status < 0) {
|
2014-02-18 12:46:39 -08:00
|
|
|
blog(LOG_ERROR, "pulse-input: Unable to connect! Status: %d",
|
|
|
|
status);
|
2014-02-16 04:45:46 -08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
// wait until connected
|
|
|
|
for (;;) {
|
2014-02-18 12:46:39 -08:00
|
|
|
pulse_iterate(data);
|
2014-02-16 04:45:46 -08:00
|
|
|
pa_context_state_t state = pa_context_get_state(data->context);
|
|
|
|
if (state == PA_CONTEXT_READY) {
|
2014-02-18 12:46:39 -08:00
|
|
|
blog(LOG_DEBUG, "pulse-input: Context ready");
|
2014-02-16 04:45:46 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!PA_CONTEXT_IS_GOOD(state)) {
|
2014-02-18 12:46:39 -08:00
|
|
|
blog(LOG_ERROR, "pulse-input: Context connect failed");
|
2014-02-16 04:45:46 -08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
return 0;
|
2014-02-13 13:05:23 -08:00
|
|
|
}
|
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
/*
|
|
|
|
* Disconnect from the pulse audio server and destroy the main loop
|
|
|
|
*/
|
2014-02-13 13:05:23 -08:00
|
|
|
static void pulse_disconnect(struct pulse_data *data)
|
|
|
|
{
|
2014-02-16 04:45:46 -08:00
|
|
|
if (data->context) {
|
|
|
|
pa_context_disconnect(data->context);
|
|
|
|
pa_context_unref(data->context);
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
if (data->mainloop)
|
|
|
|
pa_mainloop_free(data->mainloop);
|
2014-02-13 13:05:23 -08:00
|
|
|
}
|
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
/*
|
|
|
|
* Create a new pulse audio stream and connect to it
|
2014-03-11 10:12:54 -07:00
|
|
|
*
|
2014-02-16 04:45:46 -08:00
|
|
|
* Return a negative value on error
|
|
|
|
*/
|
2014-02-13 13:05:23 -08:00
|
|
|
static int pulse_connect_stream(struct pulse_data *data)
|
|
|
|
{
|
2014-02-16 04:45:46 -08:00
|
|
|
pa_sample_spec spec;
|
2014-02-18 12:46:39 -08:00
|
|
|
spec.format = data->format;
|
2014-02-16 04:45:46 -08:00
|
|
|
spec.rate = data->samples_per_sec;
|
2014-02-18 12:46:39 -08:00
|
|
|
spec.channels = get_audio_channels(data->speakers);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
if (!pa_sample_spec_valid(&spec)) {
|
|
|
|
blog(LOG_ERROR, "pulse-input: Sample spec is not valid");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
data->bytes_per_frame = pa_frame_size(&spec);
|
|
|
|
blog(LOG_DEBUG, "pulse-input: %u bytes per frame",
|
|
|
|
(unsigned int) data->bytes_per_frame);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
pa_buffer_attr attr;
|
2014-02-18 12:46:39 -08:00
|
|
|
attr.fragsize = get_buffer_size(data, 250);
|
2014-02-16 04:45:46 -08:00
|
|
|
attr.maxlength = (uint32_t) -1;
|
|
|
|
attr.minreq = (uint32_t) -1;
|
|
|
|
attr.prebuf = (uint32_t) -1;
|
|
|
|
attr.tlength = (uint32_t) -1;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
data->stream = pa_stream_new_with_proplist(data->context,
|
|
|
|
obs_source_getname(data->source), &spec, NULL, data->props);
|
2014-02-16 04:45:46 -08:00
|
|
|
if (!data->stream) {
|
2014-02-18 12:46:39 -08:00
|
|
|
blog(LOG_ERROR, "pulse-input: Unable to create stream");
|
2014-02-16 04:45:46 -08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pa_stream_flags_t flags =
|
|
|
|
PA_STREAM_INTERPOLATE_TIMING
|
|
|
|
| PA_STREAM_AUTO_TIMING_UPDATE
|
|
|
|
| PA_STREAM_ADJUST_LATENCY;
|
|
|
|
if (pa_stream_connect_record(data->stream, NULL, &attr, flags) < 0) {
|
2014-02-18 12:46:39 -08:00
|
|
|
blog(LOG_ERROR, "pulse-input: Unable to connect to stream");
|
2014-02-16 04:45:46 -08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
for (;;) {
|
2014-02-18 12:46:39 -08:00
|
|
|
pulse_iterate(data);
|
2014-02-16 04:45:46 -08:00
|
|
|
pa_stream_state_t state = pa_stream_get_state(data->stream);
|
|
|
|
if (state == PA_STREAM_READY) {
|
2014-02-18 12:46:39 -08:00
|
|
|
blog(LOG_DEBUG, "pulse-input: Stream ready");
|
2014-02-16 04:45:46 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!PA_STREAM_IS_GOOD(state)) {
|
2014-02-18 12:46:39 -08:00
|
|
|
blog(LOG_ERROR, "pulse-input: Stream connect failed");
|
2014-02-16 04:45:46 -08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
return 0;
|
2014-02-13 13:05:23 -08:00
|
|
|
}
|
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
/*
|
|
|
|
* Disconnect from the pulse audio stream
|
|
|
|
*/
|
2014-02-13 13:05:23 -08:00
|
|
|
static void pulse_diconnect_stream(struct pulse_data *data)
|
|
|
|
{
|
2014-02-16 04:45:46 -08:00
|
|
|
if (data->stream) {
|
|
|
|
pa_stream_disconnect(data->stream);
|
|
|
|
pa_stream_unref(data->stream);
|
|
|
|
}
|
2014-02-13 13:05:23 -08:00
|
|
|
}
|
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
/*
|
|
|
|
* Loop to skip the first few samples of a stream
|
|
|
|
*/
|
|
|
|
static int pulse_skip(struct pulse_data *data)
|
|
|
|
{
|
|
|
|
uint64_t skip = 1;
|
|
|
|
const void *frames;
|
|
|
|
size_t bytes;
|
|
|
|
uint64_t pa_time;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-03-11 06:16:03 -07:00
|
|
|
while (os_event_try(data->event) == EAGAIN) {
|
2014-02-18 12:46:39 -08:00
|
|
|
pulse_iterate(data);
|
|
|
|
pa_stream_peek(data->stream, &frames, &bytes);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
if (!bytes)
|
|
|
|
continue;
|
|
|
|
if (!frames || pa_stream_get_time(data->stream, &pa_time) < 0) {
|
|
|
|
pa_stream_drop(data->stream);
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
if (skip == 1 && pa_time)
|
|
|
|
skip = pa_time;
|
|
|
|
if (skip + pulse_start_delay < pa_time)
|
|
|
|
return 0;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
pa_stream_drop(data->stream);
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
/*
|
|
|
|
* Worker thread to get audio data
|
2014-03-11 10:12:54 -07:00
|
|
|
*
|
2014-02-16 04:45:46 -08:00
|
|
|
* Will run until signaled
|
|
|
|
*/
|
2014-02-13 13:05:23 -08:00
|
|
|
static void *pulse_thread(void *vptr)
|
|
|
|
{
|
2014-02-16 04:45:46 -08:00
|
|
|
PULSE_DATA(vptr);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
if (pulse_connect(data) < 0)
|
|
|
|
return NULL;
|
2014-02-22 05:48:11 -08:00
|
|
|
if (pulse_get_server_info(data) < 0)
|
|
|
|
return NULL;
|
2014-02-16 04:45:46 -08:00
|
|
|
if (pulse_connect_stream(data) < 0)
|
|
|
|
return NULL;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
if (pulse_skip(data) < 0)
|
|
|
|
return NULL;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
blog(LOG_DEBUG, "pulse-input: Start recording");
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
const void *frames;
|
|
|
|
size_t bytes;
|
|
|
|
uint64_t pa_time;
|
|
|
|
int64_t pa_latency;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
struct source_audio out;
|
|
|
|
out.speakers = data->speakers;
|
|
|
|
out.samples_per_sec = data->samples_per_sec;
|
|
|
|
out.format = pulse_to_obs_audio_format(data->format);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-03-11 06:16:03 -07:00
|
|
|
while (os_event_try(data->event) == EAGAIN) {
|
2014-02-18 12:46:39 -08:00
|
|
|
pulse_iterate(data);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
pa_stream_peek(data->stream, &frames, &bytes);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
// check if we got data
|
2014-02-18 12:46:39 -08:00
|
|
|
if (!bytes)
|
|
|
|
continue;
|
|
|
|
if (!frames) {
|
|
|
|
blog(LOG_DEBUG,
|
|
|
|
"pulse-input: Got audio hole of %u bytes",
|
|
|
|
(unsigned int) bytes);
|
2014-02-16 04:45:46 -08:00
|
|
|
pa_stream_drop(data->stream);
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
if (pa_stream_get_time(data->stream, &pa_time) < 0) {
|
|
|
|
blog(LOG_ERROR,
|
|
|
|
"pulse-input: Failed to get timing info !");
|
2014-02-16 04:45:46 -08:00
|
|
|
pa_stream_drop(data->stream);
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
pulse_get_stream_latency(data->stream, &pa_latency);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
out.data[0] = (uint8_t *) frames;
|
2014-02-22 05:48:11 -08:00
|
|
|
out.frames = frames_to_bytes(data, bytes);
|
2014-02-18 12:46:39 -08:00
|
|
|
out.timestamp = (pa_time - pa_latency) * 1000;
|
2014-02-16 04:45:46 -08:00
|
|
|
obs_source_output_audio(data->source, &out);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
pa_stream_drop(data->stream);
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
pulse_diconnect_stream(data);
|
|
|
|
pulse_disconnect(data);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
return NULL;
|
2014-02-13 13:05:23 -08:00
|
|
|
}
|
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
/*
|
|
|
|
* Create a new pulseaudio context
|
|
|
|
*/
|
|
|
|
static pa_context *pulse_context_create(pa_threaded_mainloop *m)
|
|
|
|
{
|
|
|
|
pa_context *c;
|
|
|
|
pa_proplist *p;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
p = pa_proplist_new();
|
|
|
|
pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, "OBS Studio");
|
|
|
|
pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, "application-exit");
|
|
|
|
pa_proplist_sets(p, PA_PROP_MEDIA_ROLE, "production");
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
pa_threaded_mainloop_lock(m);
|
|
|
|
c = pa_context_new_with_proplist(pa_threaded_mainloop_get_api(m),
|
|
|
|
"OBS Studio", p);
|
|
|
|
pa_threaded_mainloop_unlock(m);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
pa_proplist_free(p);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Context state callback
|
|
|
|
*/
|
|
|
|
static void pulse_context_state_changed(pa_context *c, void *userdata)
|
|
|
|
{
|
|
|
|
struct pulse_context_change *ctx =
|
|
|
|
(struct pulse_context_change *) userdata;
|
|
|
|
ctx->state = pa_context_get_state(c);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
pa_threaded_mainloop_signal(ctx->mainloop, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Connect context
|
|
|
|
*/
|
|
|
|
static int pulse_context_connect(pa_threaded_mainloop *m, pa_context *c)
|
|
|
|
{
|
|
|
|
int status = 0;
|
|
|
|
struct pulse_context_change ctx;
|
|
|
|
ctx.mainloop = m;
|
|
|
|
ctx.state = PA_CONTEXT_UNCONNECTED;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
pa_threaded_mainloop_lock(m);
|
|
|
|
pa_context_set_state_callback(c, pulse_context_state_changed,
|
|
|
|
(void *) &ctx);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
status = pa_context_connect(c, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
|
|
|
|
if (status < 0) {
|
|
|
|
blog(LOG_ERROR, "pulse-input: Unable to connect! Status: %d",
|
|
|
|
status);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (;;) {
|
|
|
|
if (ctx.state == PA_CONTEXT_READY) {
|
|
|
|
blog(LOG_DEBUG, "pulse-input: Context Ready");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!PA_CONTEXT_IS_GOOD(ctx.state)) {
|
|
|
|
blog(LOG_ERROR,
|
|
|
|
"pulse-input: Context connect failed !");
|
|
|
|
status = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pa_threaded_mainloop_wait(m);
|
|
|
|
}
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
pa_threaded_mainloop_unlock(m);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Source properties callback
|
|
|
|
*/
|
|
|
|
static void pulse_source_info(pa_context *c, const pa_source_info *i, int eol,
|
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(c);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
if (eol != 0)
|
|
|
|
return;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
struct pulse_enumerate *e = (struct pulse_enumerate *) userdata;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
if ((e->input) ^ (i->monitor_of_sink == PA_INVALID_INDEX))
|
|
|
|
return;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
blog(LOG_DEBUG, "pulse-input: Got source #%u '%s'",
|
|
|
|
i->index, i->description);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-04 00:30:37 -07:00
|
|
|
obs_property_list_add_string(e->devices, i->description, i->name);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
pa_threaded_mainloop_signal(e->mainloop, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* enumerate input/output devices
|
|
|
|
*/
|
|
|
|
static void pulse_enumerate_devices(obs_properties_t props, bool input)
|
|
|
|
{
|
|
|
|
pa_context *c;
|
|
|
|
pa_operation *op;
|
|
|
|
pa_threaded_mainloop *m = pa_threaded_mainloop_new();
|
|
|
|
struct pulse_enumerate e;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
e.mainloop = m;
|
|
|
|
e.devices = obs_properties_add_list(props, "device_id", "Device",
|
|
|
|
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
|
|
|
|
e.input = input;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
pa_threaded_mainloop_start(m);
|
|
|
|
c = pulse_context_create(m);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
if (pulse_context_connect(m, c) < 0)
|
|
|
|
goto fail;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
pa_threaded_mainloop_lock(m);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
op = pa_context_get_source_info_list(c, pulse_source_info, (void *) &e);
|
|
|
|
while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
|
|
|
|
pa_threaded_mainloop_wait(m);
|
|
|
|
pa_operation_unref(op);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
pa_threaded_mainloop_unlock(m);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
pa_context_disconnect(c);
|
|
|
|
fail:
|
|
|
|
pa_context_unref(c);
|
|
|
|
pa_threaded_mainloop_stop(m);
|
|
|
|
pa_threaded_mainloop_free(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get plugin properties
|
|
|
|
*/
|
|
|
|
static obs_properties_t pulse_properties(const char *locale, bool input)
|
|
|
|
{
|
|
|
|
blog(LOG_DEBUG, "pulse-input: properties requested !");
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-04 00:30:37 -07:00
|
|
|
obs_properties_t props = obs_properties_create(locale);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
pulse_enumerate_devices(props, input);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
|
|
|
static obs_properties_t pulse_input_properties(const char *locale)
|
|
|
|
{
|
|
|
|
return pulse_properties(locale, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static obs_properties_t pulse_output_properties(const char *locale)
|
|
|
|
{
|
|
|
|
return pulse_properties(locale, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get plugin defaults
|
|
|
|
*/
|
|
|
|
static void pulse_defaults(obs_data_t settings)
|
|
|
|
{
|
|
|
|
obs_data_set_default_string(settings, "device_id", "default");
|
|
|
|
}
|
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
/*
|
|
|
|
* Returns the name of the plugin
|
|
|
|
*/
|
2014-02-22 05:48:11 -08:00
|
|
|
static const char *pulse_input_getname(const char *locale)
|
2014-02-13 13:05:23 -08:00
|
|
|
{
|
2014-02-16 04:45:46 -08:00
|
|
|
UNUSED_PARAMETER(locale);
|
2014-02-22 05:48:11 -08:00
|
|
|
return "Pulse Audio Input Capture";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *pulse_output_getname(const char *locale)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(locale);
|
|
|
|
return "Pulse Audio Output Capture";
|
2014-02-13 13:05:23 -08:00
|
|
|
}
|
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
/*
|
|
|
|
* Destroy the plugin object and free all memory
|
|
|
|
*/
|
2014-02-13 13:05:23 -08:00
|
|
|
static void pulse_destroy(void *vptr)
|
|
|
|
{
|
2014-02-16 04:45:46 -08:00
|
|
|
PULSE_DATA(vptr);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
if (!data)
|
|
|
|
return;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
if (data->thread) {
|
|
|
|
void *ret;
|
2014-03-11 06:16:03 -07:00
|
|
|
os_event_signal(data->event);
|
2014-02-16 04:45:46 -08:00
|
|
|
pthread_join(data->thread, &ret);
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-03-11 06:16:03 -07:00
|
|
|
os_event_destroy(data->event);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
pa_proplist_free(data->props);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
blog(LOG_DEBUG, "pulse-input: Input destroyed");
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
bfree(data);
|
2014-02-13 13:05:23 -08:00
|
|
|
}
|
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
/*
|
|
|
|
* Create the plugin object
|
|
|
|
*/
|
2014-02-13 13:05:23 -08:00
|
|
|
static void *pulse_create(obs_data_t settings, obs_source_t source)
|
|
|
|
{
|
2014-02-16 04:45:46 -08:00
|
|
|
UNUSED_PARAMETER(settings);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
struct pulse_data *data = bmalloc(sizeof(struct pulse_data));
|
|
|
|
memset(data, 0, sizeof(struct pulse_data));
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
data->source = source;
|
|
|
|
data->speakers = SPEAKERS_STEREO;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
blog(LOG_DEBUG, "pulse-input: obs wants '%s'",
|
|
|
|
obs_data_getstring(settings, "device_id"));
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 12:46:39 -08:00
|
|
|
/* TODO: use obs-studio icon */
|
|
|
|
data->props = pa_proplist_new();
|
|
|
|
pa_proplist_sets(data->props, PA_PROP_APPLICATION_NAME,
|
|
|
|
"OBS Studio");
|
|
|
|
pa_proplist_sets(data->props, PA_PROP_APPLICATION_ICON_NAME,
|
|
|
|
"application-exit");
|
|
|
|
pa_proplist_sets(data->props, PA_PROP_MEDIA_ROLE,
|
|
|
|
"production");
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-03-11 06:16:03 -07:00
|
|
|
if (os_event_init(&data->event, OS_EVENT_TYPE_MANUAL) != 0)
|
2014-02-16 04:45:46 -08:00
|
|
|
goto fail;
|
|
|
|
if (pthread_create(&data->thread, NULL, pulse_thread, data) != 0)
|
|
|
|
goto fail;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 04:45:46 -08:00
|
|
|
return data;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-13 13:05:23 -08:00
|
|
|
fail:
|
2014-02-16 04:45:46 -08:00
|
|
|
pulse_destroy(data);
|
|
|
|
return NULL;
|
2014-02-13 13:05:23 -08:00
|
|
|
}
|
|
|
|
|
2014-02-22 05:48:11 -08:00
|
|
|
struct obs_source_info pulse_input_capture = {
|
|
|
|
.id = "pulse_input_capture",
|
|
|
|
.type = OBS_SOURCE_TYPE_INPUT,
|
|
|
|
.output_flags = OBS_SOURCE_AUDIO,
|
|
|
|
.getname = pulse_input_getname,
|
|
|
|
.create = pulse_create,
|
|
|
|
.destroy = pulse_destroy,
|
|
|
|
.defaults = pulse_defaults,
|
|
|
|
.properties = pulse_input_properties
|
|
|
|
};
|
|
|
|
|
|
|
|
struct obs_source_info pulse_output_capture = {
|
|
|
|
.id = "pulse_output_capture",
|
2014-02-16 04:45:46 -08:00
|
|
|
.type = OBS_SOURCE_TYPE_INPUT,
|
|
|
|
.output_flags = OBS_SOURCE_AUDIO,
|
2014-02-22 05:48:11 -08:00
|
|
|
.getname = pulse_output_getname,
|
2014-02-16 04:45:46 -08:00
|
|
|
.create = pulse_create,
|
2014-02-22 05:48:11 -08:00
|
|
|
.destroy = pulse_destroy,
|
|
|
|
.defaults = pulse_defaults,
|
|
|
|
.properties = pulse_output_properties
|
2014-02-13 13:05:23 -08:00
|
|
|
};
|