Imported Upstream version 0.13.2+dsfg1

This commit is contained in:
Sebastian Ramacher 2016-02-24 00:16:51 +01:00
commit fb3990e9e5
2036 changed files with 287360 additions and 0 deletions

View file

@ -0,0 +1,35 @@
project(linux-pulseaudio)
if(DISABLE_PULSEAUDIO)
message(STATUS "PulseAudio support disabled")
return()
endif()
find_package(PulseAudio)
if(NOT PULSEAUDIO_FOUND AND ENABLE_PULSEAUDIO)
message(FATAL_ERROR "PulseAudio not found but set as enabled")
elseif(NOT PULSEAUDIO_FOUND)
message(STATUS "PulseAudio not found, disabling PulseAudio plugin")
return()
endif()
include_directories(
SYSTEM "${CMAKE_SOURCE_DIR}/libobs"
${PULSEAUDIO_INCLUDE_DIR}
)
set(linux-pulseaudio_SOURCES
linux-pulseaudio.c
pulse-wrapper.c
pulse-input.c
)
add_library(linux-pulseaudio MODULE
${linux-pulseaudio_SOURCES}
)
target_link_libraries(linux-pulseaudio
libobs
${PULSEAUDIO_LIBRARY}
)
install_obs_plugin_with_data(linux-pulseaudio data)

View file

@ -0,0 +1,4 @@
PulseInput="التقاط مدخل الصوت (PulseAudio)"
PulseOutput="التقاط مخرج الصوت (PulseAudio)"
Device="الجهاز"

View file

@ -0,0 +1,4 @@
PulseInput="Аудио вход (PulseAudio)"
PulseOutput="Аудио изход (PulseAudio)"
Device="Устройство"

View file

@ -0,0 +1,4 @@
PulseInput="Captura d'àudio d'entrada (PulseAudio)"
PulseOutput="Captura d'àudio de sortida (PulseAudio)"
Device="Dispositiu"

View file

@ -0,0 +1,4 @@
PulseInput="Záznam vstupu zvuku (PulseAudio)"
PulseOutput="Záznam výstupu zvuku (PulseAudio)"
Device="Zařízení"

View file

@ -0,0 +1,4 @@
PulseInput="Indfang Lyd ind (PulseAudio)"
PulseOutput="Indfang Lyd Ud (PulseAudio)"
Device="Enhed"

View file

@ -0,0 +1,4 @@
PulseInput="Audio Eingabe Aufnahme (PulseAudio)"
PulseOutput="Audio Ausgabe Aufnahme (PulseAudio)"
Device="Gerät"

View file

@ -0,0 +1,4 @@
PulseInput="Σύλληψη Ήχου Είσοδου (PulseAudio)"
PulseOutput="Σύλληψη Ήχου Εξόδου (PulseAudio)"
Device="Συσκευή"

View file

@ -0,0 +1,3 @@
PulseInput="Audio Input Capture (PulseAudio)"
PulseOutput="Audio Output Capture (PulseAudio)"
Device="Device"

View file

@ -0,0 +1,4 @@
PulseInput="Captura de entrada audio (PulseAudio)"
PulseOutput="Captura de salida de audio (PulseAudio)"
Device="Dispositivo"

View file

@ -0,0 +1,4 @@
PulseInput="Audio Sarrera Harpena (PulseAudio)"
PulseOutput="Audio Irteera Harpena (PulseAudio)"
Device="Gailua"

View file

@ -0,0 +1,4 @@
PulseInput="Äänen sisääntulo (PulseAudio)"
PulseOutput="Äänen ulostulo (PulseAudio)"
Device="Laite"

View file

@ -0,0 +1,4 @@
PulseInput="Capture d'Audio Entrant (PulseAudio)"
PulseOutput="Capture d'Audio Sortant (PulseAudio)"
Device="Périphérique"

View file

@ -0,0 +1,4 @@
PulseInput="Captura de entrada audio (PulseAudio)"
PulseOutput="Captura de saída de audio (PulseAudio)"
Device="Dispositivo"

View file

@ -0,0 +1,4 @@
PulseInput="Zvučni ulaz (PulseAudio)"
PulseOutput="Zvučni izlaz (PulseAudio)"
Device="Uređaj"

View file

@ -0,0 +1,4 @@
PulseInput="Bemeneti Hangrögzítő (PulseAudio)"
PulseOutput="Kimeneti Hangrögzítő (PulseAudio)"
Device="Eszköz"

View file

@ -0,0 +1,4 @@
PulseInput="Input di acquisizione audio (PulseAudio)"
PulseOutput="Output acquisizione audio (PulseAudio)"
Device="Dispositivo"

View file

@ -0,0 +1,4 @@
PulseInput="音声入力キャプチャ (PulseAudio)"
PulseOutput="音声出力キャプチャ (PulseAudio)"
Device="デバイス"

View file

@ -0,0 +1,4 @@
PulseInput="오디오 입력 캡쳐(PulseAudio)"
PulseOutput="오디오 출력 캡쳐(PulseAudio)"
Device="장치"

View file

@ -0,0 +1,4 @@
PulseInput="Lydinngangopptak (PulseAudio)"
PulseOutput="Lydutgangopptak (PulseAudio)"
Device="Enhet"

View file

@ -0,0 +1,4 @@
PulseInput="Audioinvoer-capture (PulseAudio)"
PulseOutput="Audiouitvoer-capture (PulseAudio)"
Device="Apparaat"

View file

@ -0,0 +1,4 @@
PulseInput="Przechwytywanie wejścia dźwięku (PulseAudio)"
PulseOutput="Przechwytywanie wyjścia dźwięku (PulseAudio)"
Device="Urządzenie"

View file

@ -0,0 +1,4 @@
PulseInput="Dispositivo de Entrada de Áudio (PulseAudio)"
PulseOutput="Dispotitivo de Saída de Áudio (PulseAudio)"
Device="Dispotivo"

View file

@ -0,0 +1,4 @@
PulseInput="Captura de entrada de áudio (PulseAudio)"
PulseOutput="Captura de saída de áudio (PulseAudio)"
Device="Dispositivo"

View file

@ -0,0 +1,4 @@
PulseInput="Captură Intrare Audio (PulseAudio)"
PulseOutput="Captură Ieşire Audio (PulseAudio)"
Device="Dispozitiv"

View file

@ -0,0 +1,4 @@
PulseInput="Захват входного потока аудио (PulseAudio)"
PulseOutput="Захват выходного потока аудио (PulseAudio)"
Device="Устройство"

View file

@ -0,0 +1,4 @@
PulseInput="Zachytávanie audio vstupu (PulseAudio)"
PulseOutput="Zachytávanie audio výstupu (PulseAudio)"
Device="Zariadenie"

View file

@ -0,0 +1,4 @@
PulseInput="Zajemanje zvoka (PulseAudio)"
PulseOutput="Izhod zvoka zajemanja (PulseAudio)"
Device="Naprava"

View file

@ -0,0 +1,4 @@
PulseInput="Zvučni ulaz (PulseAudio)"
PulseOutput="Zvučni izlaz (PulseAudio)"
Device="Uređaj"

View file

@ -0,0 +1,4 @@
PulseInput="Звучни улаз (PulseAudio)"
PulseOutput="Звучни излаз (PulseAudio)"
Device="Уређај"

View file

@ -0,0 +1,4 @@
PulseInput="Ljudinmatningsenhet (PulseAudio)"
PulseOutput="Ljuduppspelningsenhet (PulseAudio)"
Device="Enhet"

View file

@ -0,0 +1,2 @@
Device="อุปกรณ์"

View file

@ -0,0 +1,4 @@
PulseInput="Ses Giriş Yakalayıcısı (PulseAudio)"
PulseOutput="Ses Çıkış Yakalayıcısı (PulseAudio)"
Device="Aygıt"

View file

@ -0,0 +1,4 @@
PulseInput="音频输入捕获(脉冲音频)"
PulseOutput="音频输出捕获(脉冲音频)"
Device="设备"

View file

@ -0,0 +1,4 @@
PulseInput="截取音效輸入PulseAudio"
PulseOutput="截取音效輸出PulseAudio"
Device="裝置"

View file

@ -0,0 +1,30 @@
/*
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/>.
*/
#include <obs-module.h>
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("linux-pulseaudio", "en-US")
extern struct obs_source_info pulse_input_capture;
extern struct obs_source_info pulse_output_capture;
bool obs_module_load(void)
{
obs_register_source(&pulse_input_capture);
obs_register_source(&pulse_output_capture);
return true;
}

View file

@ -0,0 +1,541 @@
/*
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/>.
*/
#include <util/platform.h>
#include <util/bmem.h>
#include <obs-module.h>
#include "pulse-wrapper.h"
#define NSEC_PER_SEC 1000000000LL
#define NSEC_PER_MSEC 1000000L
#define PULSE_DATA(voidptr) struct pulse_data *data = voidptr;
#define blog(level, msg, ...) blog(level, "pulse-input: " msg, ##__VA_ARGS__)
struct pulse_data {
obs_source_t *source;
pa_stream *stream;
/* user settings */
char *device;
/* server info */
enum speaker_layout speakers;
pa_sample_format_t format;
uint_fast32_t samples_per_sec;
uint_fast32_t bytes_per_frame;
uint_fast8_t channels;
uint64_t first_ts;
/* statistics */
uint_fast32_t packets;
uint_fast64_t frames;
};
static void pulse_stop_recording(struct pulse_data *data);
/**
* 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_S32LE: return AUDIO_FORMAT_32BIT;
case PA_SAMPLE_FLOAT32LE: return AUDIO_FORMAT_FLOAT;
default: return AUDIO_FORMAT_UNKNOWN;
}
return AUDIO_FORMAT_UNKNOWN;
}
/**
* Get obs speaker layout from number of channels
*
* @param channels number of channels reported by pulseaudio
*
* @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 pulse_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_SURROUND;
case 5: return SPEAKERS_4POINT1;
case 6: return SPEAKERS_5POINT1;
case 8: return SPEAKERS_7POINT1;
}
return SPEAKERS_UNKNOWN;
}
static inline uint64_t samples_to_ns(size_t frames, uint_fast32_t rate)
{
return frames * NSEC_PER_SEC / rate;
}
static inline uint64_t get_sample_time(size_t frames, uint_fast32_t rate)
{
return os_gettime_ns() - samples_to_ns(frames, rate);
}
#define STARTUP_TIMEOUT_NS (500 * NSEC_PER_MSEC)
/**
* Callback for pulse which gets executed when new audio data is available
*
* @warning The function may be called even after disconnecting the stream
*/
static void pulse_stream_read(pa_stream *p, size_t nbytes, void *userdata)
{
UNUSED_PARAMETER(p);
UNUSED_PARAMETER(nbytes);
PULSE_DATA(userdata);
const void *frames;
size_t bytes;
if (!data->stream)
goto exit;
pa_stream_peek(data->stream, &frames, &bytes);
// check if we got data
if (!bytes)
goto exit;
if (!frames) {
blog(LOG_ERROR, "Got audio hole of %u bytes",
(unsigned int) bytes);
pa_stream_drop(data->stream);
goto exit;
}
struct obs_source_audio out;
out.speakers = data->speakers;
out.samples_per_sec = data->samples_per_sec;
out.format = pulse_to_obs_audio_format(data->format);
out.data[0] = (uint8_t *) frames;
out.frames = bytes / data->bytes_per_frame;
out.timestamp = get_sample_time(out.frames,
out.samples_per_sec);
if (!data->first_ts)
data->first_ts = out.timestamp + STARTUP_TIMEOUT_NS;
if (out.timestamp > data->first_ts)
obs_source_output_audio(data->source, &out);
data->packets++;
data->frames += out.frames;
pa_stream_drop(data->stream);
exit:
pulse_signal(0);
}
/**
* Server info callback
*/
static void pulse_server_info(pa_context *c, const pa_server_info *i,
void *userdata)
{
UNUSED_PARAMETER(c);
UNUSED_PARAMETER(userdata);
blog(LOG_INFO, "Server name: '%s %s'",
i->server_name, i->server_version);
pulse_signal(0);
}
/**
* Source info callback
*
* We use the default stream settings for recording here unless pulse is
* configured to something obs can't deal with.
*/
static void pulse_source_info(pa_context *c, const pa_source_info *i, int eol,
void *userdata)
{
UNUSED_PARAMETER(c);
PULSE_DATA(userdata);
if (eol != 0)
goto skip;
blog(LOG_INFO, "Audio format: %s, %"PRIu32" Hz"
", %"PRIu8" channels",
pa_sample_format_to_string(i->sample_spec.format),
i->sample_spec.rate,
i->sample_spec.channels);
pa_sample_format_t format = i->sample_spec.format;
if (pulse_to_obs_audio_format(format) == AUDIO_FORMAT_UNKNOWN) {
format = PA_SAMPLE_S16LE;
blog(LOG_INFO, "Sample format %s not supported by OBS,"
"using %s instead for recording",
pa_sample_format_to_string(i->sample_spec.format),
pa_sample_format_to_string(format));
}
uint8_t channels = i->sample_spec.channels;
if (pulse_channels_to_obs_speakers(channels) == SPEAKERS_UNKNOWN) {
channels = 2;
blog(LOG_INFO, "%c channels not supported by OBS,"
"using %c instead for recording",
i->sample_spec.channels,
channels);
}
data->format = format;
data->samples_per_sec = i->sample_spec.rate;
data->channels = channels;
skip:
pulse_signal(0);
}
/**
* Start recording
*
* We request the default format used by pulse here because the data will be
* converted and possibly re-sampled by obs anyway.
*
* For now we request a buffer length of 25ms although pulse seems to ignore
* this setting for monitor streams. For "real" input streams this should work
* fine though.
*/
static int_fast32_t pulse_start_recording(struct pulse_data *data)
{
if (pulse_get_server_info(pulse_server_info, (void *) data) < 0) {
blog(LOG_ERROR, "Unable to get server info !");
return -1;
}
if (pulse_get_source_info(pulse_source_info, data->device,
(void *) data) < 0) {
blog(LOG_ERROR, "Unable to get source info !");
return -1;
}
pa_sample_spec spec;
spec.format = data->format;
spec.rate = data->samples_per_sec;
spec.channels = data->channels;
if (!pa_sample_spec_valid(&spec)) {
blog(LOG_ERROR, "Sample spec is not valid");
return -1;
}
data->speakers = pulse_channels_to_obs_speakers(spec.channels);
data->bytes_per_frame = pa_frame_size(&spec);
data->stream = pulse_stream_new(obs_source_get_name(data->source),
&spec, NULL);
if (!data->stream) {
blog(LOG_ERROR, "Unable to create stream");
return -1;
}
pulse_lock();
pa_stream_set_read_callback(data->stream, pulse_stream_read,
(void *) data);
pulse_unlock();
pa_buffer_attr attr;
attr.fragsize = pa_usec_to_bytes(25000, &spec);
attr.maxlength = (uint32_t) -1;
attr.minreq = (uint32_t) -1;
attr.prebuf = (uint32_t) -1;
attr.tlength = (uint32_t) -1;
pa_stream_flags_t flags = PA_STREAM_ADJUST_LATENCY;
pulse_lock();
int_fast32_t ret = pa_stream_connect_record(data->stream, data->device,
&attr, flags);
pulse_unlock();
if (ret < 0) {
pulse_stop_recording(data);
blog(LOG_ERROR, "Unable to connect to stream");
return -1;
}
blog(LOG_INFO, "Started recording from '%s'", data->device);
return 0;
}
/**
* stop recording
*/
static void pulse_stop_recording(struct pulse_data *data)
{
if (data->stream) {
pulse_lock();
pa_stream_disconnect(data->stream);
pa_stream_unref(data->stream);
data->stream = NULL;
pulse_unlock();
}
blog(LOG_INFO, "Stopped recording from '%s'", data->device);
blog(LOG_INFO, "Got %"PRIuFAST32" packets with %"PRIuFAST64" frames",
data->packets, data->frames);
data->first_ts = 0;
data->packets = 0;
data->frames = 0;
}
/**
* input info callback
*/
static void pulse_input_info(pa_context *c, const pa_source_info *i, int eol,
void *userdata)
{
UNUSED_PARAMETER(c);
if (eol != 0 || i->monitor_of_sink != PA_INVALID_INDEX)
goto skip;
obs_property_list_add_string((obs_property_t*) userdata,
i->description, i->name);
skip:
pulse_signal(0);
}
/**
* output info callback
*/
static void pulse_output_info(pa_context *c, const pa_source_info *i, int eol,
void *userdata)
{
UNUSED_PARAMETER(c);
if (eol != 0 || i->monitor_of_sink == PA_INVALID_INDEX)
goto skip;
obs_property_list_add_string((obs_property_t*) userdata,
i->description, i->name);
skip:
pulse_signal(0);
}
/**
* Get plugin properties
*/
static obs_properties_t *pulse_properties(bool input)
{
obs_properties_t *props = obs_properties_create();
obs_property_t *devices = obs_properties_add_list(props, "device_id",
obs_module_text("Device"), OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_STRING);
pulse_init();
pa_source_info_cb_t cb = (input) ? pulse_input_info : pulse_output_info;
pulse_get_source_info_list(cb, (void *) devices);
pulse_unref();
return props;
}
static obs_properties_t *pulse_input_properties(void *unused)
{
UNUSED_PARAMETER(unused);
return pulse_properties(true);
}
static obs_properties_t *pulse_output_properties(void *unused)
{
UNUSED_PARAMETER(unused);
return pulse_properties(false);
}
/**
* Server info callback
*/
static void pulse_input_device(pa_context *c, const pa_server_info *i,
void *userdata)
{
UNUSED_PARAMETER(c);
obs_data_t *settings = (obs_data_t*) userdata;
obs_data_set_default_string(settings, "device_id",
i->default_source_name);
blog(LOG_DEBUG, "Default input device: '%s'", i->default_source_name);
pulse_signal(0);
}
static void pulse_output_device(pa_context *c, const pa_server_info *i,
void *userdata)
{
UNUSED_PARAMETER(c);
obs_data_t *settings = (obs_data_t*) userdata;
char *monitor = bzalloc(strlen(i->default_sink_name) + 9);
strcat(monitor, i->default_sink_name);
strcat(monitor, ".monitor");
obs_data_set_default_string(settings, "device_id", monitor);
blog(LOG_DEBUG, "Default output device: '%s'", monitor);
bfree(monitor);
pulse_signal(0);
}
/**
* Get plugin defaults
*/
static void pulse_defaults(obs_data_t *settings, bool input)
{
pulse_init();
pa_server_info_cb_t cb = (input)
? pulse_input_device : pulse_output_device;
pulse_get_server_info(cb, (void *) settings);
pulse_unref();
}
static void pulse_input_defaults(obs_data_t *settings)
{
return pulse_defaults(settings, true);
}
static void pulse_output_defaults(obs_data_t *settings)
{
return pulse_defaults(settings, false);
}
/**
* Returns the name of the plugin
*/
static const char *pulse_input_getname(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_module_text("PulseInput");
}
static const char *pulse_output_getname(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_module_text("PulseOutput");
}
/**
* Destroy the plugin object and free all memory
*/
static void pulse_destroy(void *vptr)
{
PULSE_DATA(vptr);
if (!data)
return;
if (data->stream)
pulse_stop_recording(data);
pulse_unref();
if (data->device)
bfree(data->device);
bfree(data);
}
/**
* Update the input settings
*/
static void pulse_update(void *vptr, obs_data_t *settings)
{
PULSE_DATA(vptr);
bool restart = false;
const char *new_device;
new_device = obs_data_get_string(settings, "device_id");
if (!data->device || strcmp(data->device, new_device) != 0) {
if (data->device)
bfree(data->device);
data->device = bstrdup(new_device);
restart = true;
}
if (!restart)
return;
if (data->stream)
pulse_stop_recording(data);
pulse_start_recording(data);
}
/**
* Create the plugin object
*/
static void *pulse_create(obs_data_t *settings, obs_source_t *source)
{
struct pulse_data *data = bzalloc(sizeof(struct pulse_data));
data->source = source;
pulse_init();
pulse_update(data, settings);
if (data->stream)
return data;
pulse_destroy(data);
return NULL;
}
struct obs_source_info pulse_input_capture = {
.id = "pulse_input_capture",
.type = OBS_SOURCE_TYPE_INPUT,
.output_flags = OBS_SOURCE_AUDIO |
OBS_SOURCE_DO_NOT_DUPLICATE,
.get_name = pulse_input_getname,
.create = pulse_create,
.destroy = pulse_destroy,
.update = pulse_update,
.get_defaults = pulse_input_defaults,
.get_properties = pulse_input_properties
};
struct obs_source_info pulse_output_capture = {
.id = "pulse_output_capture",
.type = OBS_SOURCE_TYPE_INPUT,
.output_flags = OBS_SOURCE_AUDIO |
OBS_SOURCE_DO_NOT_DUPLICATE,
.get_name = pulse_output_getname,
.create = pulse_create,
.destroy = pulse_destroy,
.update = pulse_update,
.get_defaults = pulse_output_defaults,
.get_properties = pulse_output_properties
};

View file

@ -0,0 +1,236 @@
/*
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/>.
*/
#include <pthread.h>
#include <pulse/thread-mainloop.h>
#include <util/base.h>
#include <obs.h>
#include "pulse-wrapper.h"
/* global data */
static uint_fast32_t pulse_refs = 0;
static pthread_mutex_t pulse_mutex = PTHREAD_MUTEX_INITIALIZER;
static pa_threaded_mainloop *pulse_mainloop = NULL;
static pa_context *pulse_context = NULL;
/**
* context status change callback
*
* @todo this is currently a noop, we want to reconnect here if the connection
* is lost ...
*/
static void pulse_context_state_changed(pa_context *c, void *userdata)
{
UNUSED_PARAMETER(userdata);
UNUSED_PARAMETER(c);
pulse_signal(0);
}
/**
* get the default properties
*/
static pa_proplist *pulse_properties()
{
pa_proplist *p = pa_proplist_new();
pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, "OBS");
pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, "obs");
pa_proplist_sets(p, PA_PROP_MEDIA_ROLE, "production");
return p;
}
/**
* Initialize the pulse audio context with properties and callback
*/
static void pulse_init_context()
{
pulse_lock();
pa_proplist *p = pulse_properties();
pulse_context = pa_context_new_with_proplist(
pa_threaded_mainloop_get_api(pulse_mainloop), "OBS", p);
pa_context_set_state_callback(pulse_context,
pulse_context_state_changed, NULL);
pa_context_connect(pulse_context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
pa_proplist_free(p);
pulse_unlock();
}
/**
* wait for context to be ready
*/
static int_fast32_t pulse_context_ready()
{
pulse_lock();
if (!PA_CONTEXT_IS_GOOD(pa_context_get_state(pulse_context))) {
pulse_unlock();
return -1;
}
while (pa_context_get_state(pulse_context) != PA_CONTEXT_READY)
pulse_wait();
pulse_unlock();
return 0;
}
int_fast32_t pulse_init()
{
pthread_mutex_lock(&pulse_mutex);
if (pulse_refs == 0) {
pulse_mainloop = pa_threaded_mainloop_new();
pa_threaded_mainloop_start(pulse_mainloop);
pulse_init_context();
}
pulse_refs++;
pthread_mutex_unlock(&pulse_mutex);
return 0;
}
void pulse_unref()
{
pthread_mutex_lock(&pulse_mutex);
if (--pulse_refs == 0) {
pulse_lock();
if (pulse_context != NULL) {
pa_context_disconnect(pulse_context);
pa_context_unref(pulse_context);
pulse_context = NULL;
}
pulse_unlock();
if (pulse_mainloop != NULL) {
pa_threaded_mainloop_stop(pulse_mainloop);
pa_threaded_mainloop_free(pulse_mainloop);
pulse_mainloop = NULL;
}
}
pthread_mutex_unlock(&pulse_mutex);
}
void pulse_lock()
{
pa_threaded_mainloop_lock(pulse_mainloop);
}
void pulse_unlock()
{
pa_threaded_mainloop_unlock(pulse_mainloop);
}
void pulse_wait()
{
pa_threaded_mainloop_wait(pulse_mainloop);
}
void pulse_signal(int wait_for_accept)
{
pa_threaded_mainloop_signal(pulse_mainloop, wait_for_accept);
}
void pulse_accept()
{
pa_threaded_mainloop_accept(pulse_mainloop);
}
int_fast32_t pulse_get_source_info_list(pa_source_info_cb_t cb, void* userdata)
{
if (pulse_context_ready() < 0)
return -1;
pulse_lock();
pa_operation *op = pa_context_get_source_info_list(
pulse_context, cb, userdata);
while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
pulse_wait();
pa_operation_unref(op);
pulse_unlock();
return 0;
}
int_fast32_t pulse_get_source_info(pa_source_info_cb_t cb, const char *name,
void *userdata)
{
if (pulse_context_ready() < 0)
return -1;
pulse_lock();
pa_operation *op = pa_context_get_source_info_by_name(
pulse_context, name, cb, userdata);
while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
pulse_wait();
pa_operation_unref(op);
pulse_unlock();
return 0;
}
int_fast32_t pulse_get_server_info(pa_server_info_cb_t cb, void* userdata)
{
if (pulse_context_ready() < 0)
return -1;
pulse_lock();
pa_operation *op = pa_context_get_server_info(
pulse_context, cb, userdata);
while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
pulse_wait();
pa_operation_unref(op);
pulse_unlock();
return 0;
}
pa_stream* pulse_stream_new(const char* name, const pa_sample_spec* ss,
const pa_channel_map* map)
{
if (pulse_context_ready() < 0)
return NULL;
pulse_lock();
pa_proplist *p = pulse_properties();
pa_stream *s = pa_stream_new_with_proplist(
pulse_context, name, ss, map, p);
pa_proplist_free(p);
pulse_unlock();
return s;
}

View file

@ -0,0 +1,137 @@
/*
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/>.
*/
#include <inttypes.h>
#include <pulse/stream.h>
#include <pulse/context.h>
#include <pulse/introspect.h>
#pragma once
/**
* Initialize the pulseaudio mainloop and increase the reference count
*/
int_fast32_t pulse_init();
/**
* Unreference the pulseaudio mainloop, when the reference count reaches
* zero the mainloop will automatically be destroyed
*/
void pulse_unref();
/**
* Lock the mainloop
*
* In order to allow for multiple threads to use the same mainloop pulseaudio
* provides it's own locking mechanism. This function should be called before
* using any pulseaudio function that is in any way related to the mainloop or
* context.
*
* @note use of this function may cause deadlocks
*
* @warning do not use with pulse_ wrapper functions
*/
void pulse_lock();
/**
* Unlock the mainloop
*
* @see pulse_lock()
*/
void pulse_unlock();
/**
* Wait for events to happen
*
* This function should be called when waiting for an event to happen.
*/
void pulse_wait();
/**
* Wait for accept signal from calling thread
*
* This function tells the pulseaudio mainloop wheter the data provided to
* the callback should be retained until the calling thread executes
* pulse_accept()
*
* If wait_for_accept is 0 the function returns and the data is freed.
*/
void pulse_signal(int wait_for_accept);
/**
* Signal the waiting callback to return
*
* This function is used in conjunction with pulse_signal()
*/
void pulse_accept();
/**
* Request source information
*
* The function will block until the operation was executed and the mainloop
* called the provided callback function.
*
* @return negative on error
*
* @note The function will block until the server context is ready.
*
* @warning call without active locks
*/
int_fast32_t pulse_get_source_info_list(pa_source_info_cb_t cb, void *userdata);
/**
* Request source information from a specific source
*
* The function will block until the operation was executed and the mainloop
* called the provided callback function.
*
* @param cb pointer to the callback function
* @param name the source name to get information for
* @param userdata pointer to userdata the callback will be called with
*
* @return negative on error
*
* @note The function will block until the server context is ready.
*
* @warning call without active locks
*/
int_fast32_t pulse_get_source_info(pa_source_info_cb_t cb, const char *name,
void *userdata);
/**
* Request server information
*
* The function will block until the operation was executed and the mainloop
* called the provided callback function.
*
* @return negative on error
*
* @note The function will block until the server context is ready.
*
* @warning call without active locks
*/
int_fast32_t pulse_get_server_info(pa_server_info_cb_t cb, void *userdata);
/**
* Create a new stream with the default properties
*
* @note The function will block until the server context is ready.
*
* @warning call without active locks
*/
pa_stream *pulse_stream_new(const char *name, const pa_sample_spec *ss,
const pa_channel_map *map);