2014-02-13 22:05:23 +01: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 13:45:46 +01:00
|
|
|
|
2014-04-28 21:10:17 +02:00
|
|
|
#include <util/platform.h>
|
2014-02-13 22:05:23 +01:00
|
|
|
#include <util/bmem.h>
|
2020-03-21 10:55:12 +01:00
|
|
|
#include <util/util_uint64.h>
|
2014-07-09 22:12:57 -07:00
|
|
|
#include <obs-module.h>
|
2014-02-13 22:05:23 +01:00
|
|
|
|
2014-03-12 15:59:18 +01:00
|
|
|
#include "pulse-wrapper.h"
|
2014-02-13 22:05:23 +01:00
|
|
|
|
2014-10-06 05:39:02 -04:00
|
|
|
#define NSEC_PER_SEC 1000000000LL
|
|
|
|
#define NSEC_PER_MSEC 1000000L
|
|
|
|
|
2014-03-12 15:59:18 +01:00
|
|
|
#define PULSE_DATA(voidptr) struct pulse_data *data = voidptr;
|
2014-08-29 17:17:22 +02:00
|
|
|
#define blog(level, msg, ...) blog(level, "pulse-input: " msg, ##__VA_ARGS__)
|
2014-02-13 22:05:23 +01:00
|
|
|
|
|
|
|
struct pulse_data {
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_source_t *source;
|
2014-05-19 21:24:30 +02:00
|
|
|
pa_stream *stream;
|
|
|
|
|
|
|
|
/* user settings */
|
2014-03-12 15:59:18 +01:00
|
|
|
char *device;
|
2017-09-23 00:33:40 -03:00
|
|
|
bool input;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-05-19 21:24:30 +02:00
|
|
|
/* server info */
|
2014-02-16 13:45:46 +01:00
|
|
|
enum speaker_layout speakers;
|
2014-02-18 21:46:39 +01:00
|
|
|
pa_sample_format_t format;
|
2014-02-22 14:48:11 +01:00
|
|
|
uint_fast32_t samples_per_sec;
|
|
|
|
uint_fast32_t bytes_per_frame;
|
2014-05-19 21:24:30 +02:00
|
|
|
uint_fast8_t channels;
|
2014-10-06 05:39:02 -04:00
|
|
|
uint64_t first_ts;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-05-19 21:24:30 +02:00
|
|
|
/* statistics */
|
|
|
|
uint_fast32_t packets;
|
|
|
|
uint_fast64_t frames;
|
2014-02-22 14:48:11 +01:00
|
|
|
};
|
|
|
|
|
2014-04-25 21:26:53 +02:00
|
|
|
static void pulse_stop_recording(struct pulse_data *data);
|
|
|
|
|
|
|
|
/**
|
2014-02-18 21:46:39 +01:00
|
|
|
* get obs from pulse audio format
|
|
|
|
*/
|
|
|
|
static enum audio_format pulse_to_obs_audio_format(pa_sample_format_t format)
|
|
|
|
{
|
|
|
|
switch (format) {
|
2014-08-29 17:19:40 +02:00
|
|
|
case PA_SAMPLE_U8:
|
|
|
|
return AUDIO_FORMAT_U8BIT;
|
|
|
|
case PA_SAMPLE_S16LE:
|
|
|
|
return AUDIO_FORMAT_16BIT;
|
2014-10-07 21:10:06 +02:00
|
|
|
case PA_SAMPLE_S32LE:
|
|
|
|
return AUDIO_FORMAT_32BIT;
|
2014-08-29 17:19:40 +02:00
|
|
|
case PA_SAMPLE_FLOAT32LE:
|
|
|
|
return AUDIO_FORMAT_FLOAT;
|
|
|
|
default:
|
|
|
|
return AUDIO_FORMAT_UNKNOWN;
|
2014-02-18 21:46:39 +01:00
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 21:46:39 +01:00
|
|
|
return AUDIO_FORMAT_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2014-08-28 20:23:28 +02:00
|
|
|
/**
|
|
|
|
* 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) {
|
libobs: Add surround sound audio support
(This commit also modifies the following modules: UI,
deps/media-playback, coreaudio-encoder, decklink, linux-alsa,
linux-pulseaudio, mac-capture, obs-ffmpeg, obs-filters, obs-libfdk,
obs-outputs, win-dshow, and win-wasapi)
Adds surround sound audio support to the core, core plugins, and user
interface.
Compatible streaming services: Twitch, FB 360 live
Compatible protocols: rtmp / mpeg-ts tcp udp
Compatible file formats: mkv mp4 ts (others untested)
Compatible codecs: ffmpeg aac, fdk_aac, CoreAudio aac,
opus, vorbis, pcm (others untested).
Tested streaming servers: wowza, nginx
HLS, mpeg-dash : surround passthrough
Html5 players tested with live surround:
videojs, mediaelement, viblast (hls+dash), hls.js
Decklink: on win32, swap channels order for 5.1 7.1
(due to different channel mapping on wav, mpeg, ffmpeg)
Audio filters: surround working.
Monitoring: surround working (win macOs linux (pulse-audio)).
VST: stereo plugins keep in general only the first two channels.
surround plugins should work (e.g. mcfx does).
OS: win, macOs, linux (alsa, pulse-audio).
Misc: larger audio bitrates unlocked to accommodate more channels
NB: mf-aac only supports mono and stereo + 5.1 on win 10
(not implemented due to lack of usefulness)
Closes jp9000/obs-studio#968
2017-05-27 02:15:54 +02:00
|
|
|
case 1:
|
|
|
|
return SPEAKERS_MONO;
|
|
|
|
case 2:
|
|
|
|
return SPEAKERS_STEREO;
|
|
|
|
case 3:
|
|
|
|
return SPEAKERS_2POINT1;
|
2017-12-01 17:10:05 +01:00
|
|
|
case 4:
|
|
|
|
return SPEAKERS_4POINT0;
|
libobs: Add surround sound audio support
(This commit also modifies the following modules: UI,
deps/media-playback, coreaudio-encoder, decklink, linux-alsa,
linux-pulseaudio, mac-capture, obs-ffmpeg, obs-filters, obs-libfdk,
obs-outputs, win-dshow, and win-wasapi)
Adds surround sound audio support to the core, core plugins, and user
interface.
Compatible streaming services: Twitch, FB 360 live
Compatible protocols: rtmp / mpeg-ts tcp udp
Compatible file formats: mkv mp4 ts (others untested)
Compatible codecs: ffmpeg aac, fdk_aac, CoreAudio aac,
opus, vorbis, pcm (others untested).
Tested streaming servers: wowza, nginx
HLS, mpeg-dash : surround passthrough
Html5 players tested with live surround:
videojs, mediaelement, viblast (hls+dash), hls.js
Decklink: on win32, swap channels order for 5.1 7.1
(due to different channel mapping on wav, mpeg, ffmpeg)
Audio filters: surround working.
Monitoring: surround working (win macOs linux (pulse-audio)).
VST: stereo plugins keep in general only the first two channels.
surround plugins should work (e.g. mcfx does).
OS: win, macOs, linux (alsa, pulse-audio).
Misc: larger audio bitrates unlocked to accommodate more channels
NB: mf-aac only supports mono and stereo + 5.1 on win 10
(not implemented due to lack of usefulness)
Closes jp9000/obs-studio#968
2017-05-27 02:15:54 +02:00
|
|
|
case 5:
|
|
|
|
return SPEAKERS_4POINT1;
|
|
|
|
case 6:
|
|
|
|
return SPEAKERS_5POINT1;
|
|
|
|
case 8:
|
|
|
|
return SPEAKERS_7POINT1;
|
2014-08-28 20:23:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return SPEAKERS_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2018-01-08 21:27:28 +01:00
|
|
|
static pa_channel_map pulse_channel_map(enum speaker_layout layout)
|
|
|
|
{
|
|
|
|
pa_channel_map ret;
|
|
|
|
|
|
|
|
ret.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
|
|
|
|
ret.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
|
|
|
|
ret.map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
|
|
|
|
ret.map[3] = PA_CHANNEL_POSITION_LFE;
|
|
|
|
ret.map[4] = PA_CHANNEL_POSITION_REAR_LEFT;
|
|
|
|
ret.map[5] = PA_CHANNEL_POSITION_REAR_RIGHT;
|
|
|
|
ret.map[6] = PA_CHANNEL_POSITION_SIDE_LEFT;
|
|
|
|
ret.map[7] = PA_CHANNEL_POSITION_SIDE_RIGHT;
|
|
|
|
|
|
|
|
switch (layout) {
|
|
|
|
case SPEAKERS_MONO:
|
|
|
|
ret.channels = 1;
|
|
|
|
ret.map[0] = PA_CHANNEL_POSITION_MONO;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SPEAKERS_STEREO:
|
|
|
|
ret.channels = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SPEAKERS_2POINT1:
|
|
|
|
ret.channels = 3;
|
|
|
|
ret.map[2] = PA_CHANNEL_POSITION_LFE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SPEAKERS_4POINT0:
|
|
|
|
ret.channels = 4;
|
|
|
|
ret.map[3] = PA_CHANNEL_POSITION_REAR_CENTER;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SPEAKERS_4POINT1:
|
|
|
|
ret.channels = 5;
|
|
|
|
ret.map[4] = PA_CHANNEL_POSITION_REAR_CENTER;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SPEAKERS_5POINT1:
|
|
|
|
ret.channels = 6;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SPEAKERS_7POINT1:
|
|
|
|
ret.channels = 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SPEAKERS_UNKNOWN:
|
|
|
|
default:
|
|
|
|
ret.channels = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-06 05:39:02 -04:00
|
|
|
static inline uint64_t samples_to_ns(size_t frames, uint_fast32_t rate)
|
2014-02-18 21:46:39 +01:00
|
|
|
{
|
2020-03-21 10:55:12 +01:00
|
|
|
return util_mul_div64(frames, NSEC_PER_SEC, rate);
|
2014-10-06 05:39:02 -04:00
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-10-06 05:39:02 -04:00
|
|
|
static inline uint64_t get_sample_time(size_t frames, uint_fast32_t rate)
|
|
|
|
{
|
|
|
|
return os_gettime_ns() - samples_to_ns(frames, rate);
|
2014-02-18 21:46:39 +01:00
|
|
|
}
|
|
|
|
|
2014-10-06 05:39:02 -04:00
|
|
|
#define STARTUP_TIMEOUT_NS (500 * NSEC_PER_MSEC)
|
|
|
|
|
2014-04-25 21:26:53 +02:00
|
|
|
/**
|
2014-03-12 15:59:18 +01:00
|
|
|
* Callback for pulse which gets executed when new audio data is available
|
2014-04-25 21:26:53 +02:00
|
|
|
*
|
|
|
|
* @warning The function may be called even after disconnecting the stream
|
2014-02-22 14:48:11 +01:00
|
|
|
*/
|
2014-03-12 15:59:18 +01:00
|
|
|
static void pulse_stream_read(pa_stream *p, size_t nbytes, void *userdata)
|
2014-02-22 14:48:11 +01:00
|
|
|
{
|
2014-03-12 15:59:18 +01:00
|
|
|
UNUSED_PARAMETER(p);
|
|
|
|
UNUSED_PARAMETER(nbytes);
|
2014-02-22 14:48:11 +01:00
|
|
|
PULSE_DATA(userdata);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-03-12 15:59:18 +01:00
|
|
|
const void *frames;
|
|
|
|
size_t bytes;
|
2014-02-22 14:48:11 +01:00
|
|
|
|
2014-04-25 21:26:53 +02:00
|
|
|
if (!data->stream)
|
|
|
|
goto exit;
|
|
|
|
|
2014-03-12 15:59:18 +01:00
|
|
|
pa_stream_peek(data->stream, &frames, &bytes);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-03-12 15:59:18 +01:00
|
|
|
// check if we got data
|
|
|
|
if (!bytes)
|
|
|
|
goto exit;
|
2014-02-22 14:48:11 +01:00
|
|
|
|
2014-03-12 15:59:18 +01:00
|
|
|
if (!frames) {
|
2014-08-29 17:17:22 +02:00
|
|
|
blog(LOG_ERROR, "Got audio hole of %u bytes",
|
2014-03-12 15:59:18 +01:00
|
|
|
(unsigned int)bytes);
|
|
|
|
pa_stream_drop(data->stream);
|
|
|
|
goto exit;
|
2014-02-16 13:45:46 +01:00
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-08-02 08:42:04 -07:00
|
|
|
struct obs_source_audio out;
|
2014-03-12 15:59:18 +01:00
|
|
|
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;
|
2014-10-06 05:39:02 -04:00
|
|
|
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);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-05-19 21:24:30 +02:00
|
|
|
data->packets++;
|
|
|
|
data->frames += out.frames;
|
2014-03-12 15:59:18 +01:00
|
|
|
|
2014-05-19 21:24:30 +02:00
|
|
|
pa_stream_drop(data->stream);
|
2014-03-12 15:59:18 +01:00
|
|
|
exit:
|
|
|
|
pulse_signal(0);
|
2014-02-13 22:05:23 +01:00
|
|
|
}
|
|
|
|
|
2014-04-25 21:26:53 +02:00
|
|
|
/**
|
2014-03-12 15:59:18 +01:00
|
|
|
* Server info callback
|
2014-02-16 13:45:46 +01:00
|
|
|
*/
|
2014-03-12 15:59:18 +01:00
|
|
|
static void pulse_server_info(pa_context *c, const pa_server_info *i,
|
|
|
|
void *userdata)
|
2014-02-13 22:05:23 +01:00
|
|
|
{
|
2014-03-12 15:59:18 +01:00
|
|
|
UNUSED_PARAMETER(c);
|
2017-09-23 00:33:40 -03:00
|
|
|
PULSE_DATA(userdata);
|
2014-03-12 15:59:18 +01:00
|
|
|
|
2014-08-29 17:17:22 +02:00
|
|
|
blog(LOG_INFO, "Server name: '%s %s'", i->server_name,
|
2014-05-18 14:31:37 +02:00
|
|
|
i->server_version);
|
|
|
|
|
2017-09-23 00:33:40 -03:00
|
|
|
if (data->device && strcmp("default", data->device) == 0) {
|
|
|
|
if (data->input) {
|
|
|
|
bfree(data->device);
|
|
|
|
data->device = bstrdup(i->default_source_name);
|
|
|
|
|
|
|
|
blog(LOG_DEBUG, "Default input device: '%s'",
|
|
|
|
data->device);
|
|
|
|
} else {
|
|
|
|
char *monitor =
|
|
|
|
bzalloc(strlen(i->default_sink_name) + 9);
|
|
|
|
strcat(monitor, i->default_sink_name);
|
|
|
|
strcat(monitor, ".monitor");
|
|
|
|
|
|
|
|
bfree(data->device);
|
|
|
|
data->device = bstrdup(monitor);
|
|
|
|
|
|
|
|
blog(LOG_DEBUG, "Default output device: '%s'",
|
|
|
|
data->device);
|
|
|
|
bfree(monitor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-29 17:12:17 +02:00
|
|
|
pulse_signal(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Source info callback
|
2014-10-18 20:25:49 +02:00
|
|
|
*
|
|
|
|
* We use the default stream settings for recording here unless pulse is
|
|
|
|
* configured to something obs can't deal with.
|
2014-08-29 17:12:17 +02:00
|
|
|
*/
|
|
|
|
static void pulse_source_info(pa_context *c, const pa_source_info *i, int eol,
|
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(c);
|
|
|
|
PULSE_DATA(userdata);
|
2016-08-28 02:56:59 +02:00
|
|
|
// An error occured
|
|
|
|
if (eol < 0) {
|
|
|
|
data->format = PA_SAMPLE_INVALID;
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
// Terminating call for multi instance callbacks
|
|
|
|
if (eol > 0)
|
2014-08-29 17:12:17 +02:00
|
|
|
goto skip;
|
|
|
|
|
2014-10-07 21:55:24 +02:00
|
|
|
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) {
|
2018-01-08 07:34:40 +01:00
|
|
|
format = PA_SAMPLE_FLOAT32LE;
|
2014-10-07 21:55:24 +02:00
|
|
|
|
2014-10-18 20:25:49 +02:00
|
|
|
blog(LOG_INFO,
|
|
|
|
"Sample format %s not supported by OBS,"
|
|
|
|
"using %s instead for recording",
|
2014-10-07 21:55:24 +02:00
|
|
|
pa_sample_format_to_string(i->sample_spec.format),
|
|
|
|
pa_sample_format_to_string(format));
|
|
|
|
}
|
|
|
|
|
2014-10-18 20:25:49 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-10-07 21:55:24 +02:00
|
|
|
data->format = format;
|
2014-03-12 15:59:18 +01:00
|
|
|
data->samples_per_sec = i->sample_spec.rate;
|
2014-10-18 20:25:49 +02:00
|
|
|
data->channels = channels;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-08-29 17:12:17 +02:00
|
|
|
skip:
|
2014-03-12 15:59:18 +01:00
|
|
|
pulse_signal(0);
|
2014-02-13 22:05:23 +01:00
|
|
|
}
|
|
|
|
|
2014-04-25 21:26:53 +02:00
|
|
|
/**
|
2014-05-21 22:26:46 +02:00
|
|
|
* Start recording
|
|
|
|
*
|
|
|
|
* We request the default format used by pulse here because the data will be
|
|
|
|
* converted and possibly re-sampled by obs anyway.
|
2014-10-06 08:35:56 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2014-02-16 13:45:46 +01:00
|
|
|
*/
|
2014-03-12 15:59:18 +01:00
|
|
|
static int_fast32_t pulse_start_recording(struct pulse_data *data)
|
2014-02-13 22:05:23 +01:00
|
|
|
{
|
2014-03-12 15:59:18 +01:00
|
|
|
if (pulse_get_server_info(pulse_server_info, (void *)data) < 0) {
|
2014-08-29 17:17:22 +02:00
|
|
|
blog(LOG_ERROR, "Unable to get server info !");
|
2014-03-12 15:59:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-08-29 17:12:17 +02:00
|
|
|
if (pulse_get_source_info(pulse_source_info, data->device,
|
|
|
|
(void *)data) < 0) {
|
2014-08-29 17:17:22 +02:00
|
|
|
blog(LOG_ERROR, "Unable to get source info !");
|
2014-08-29 17:12:17 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2016-08-28 02:56:59 +02:00
|
|
|
if (data->format == PA_SAMPLE_INVALID) {
|
|
|
|
blog(LOG_ERROR,
|
|
|
|
"An error occurred while getting the source info!");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-08-29 17:12:17 +02:00
|
|
|
|
2014-02-16 13:45:46 +01:00
|
|
|
pa_sample_spec spec;
|
2014-03-12 15:59:18 +01:00
|
|
|
spec.format = data->format;
|
|
|
|
spec.rate = data->samples_per_sec;
|
|
|
|
spec.channels = data->channels;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-18 21:46:39 +01:00
|
|
|
if (!pa_sample_spec_valid(&spec)) {
|
2014-08-29 17:17:22 +02:00
|
|
|
blog(LOG_ERROR, "Sample spec is not valid");
|
2014-02-18 21:46:39 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-08-28 20:23:28 +02:00
|
|
|
data->speakers = pulse_channels_to_obs_speakers(spec.channels);
|
2014-02-22 14:48:11 +01:00
|
|
|
data->bytes_per_frame = pa_frame_size(&spec);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2018-01-08 21:27:28 +01:00
|
|
|
pa_channel_map channel_map = pulse_channel_map(data->speakers);
|
|
|
|
|
2014-08-04 08:41:15 -07:00
|
|
|
data->stream = pulse_stream_new(obs_source_get_name(data->source),
|
2018-01-08 21:27:28 +01:00
|
|
|
&spec, &channel_map);
|
2014-02-16 13:45:46 +01:00
|
|
|
if (!data->stream) {
|
2014-08-29 17:17:22 +02:00
|
|
|
blog(LOG_ERROR, "Unable to create stream");
|
2014-02-16 13:45:46 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-12 15:59:18 +01:00
|
|
|
|
|
|
|
pulse_lock();
|
|
|
|
pa_stream_set_read_callback(data->stream, pulse_stream_read,
|
|
|
|
(void *)data);
|
|
|
|
pulse_unlock();
|
|
|
|
|
|
|
|
pa_buffer_attr attr;
|
2014-10-06 08:35:56 +02:00
|
|
|
attr.fragsize = pa_usec_to_bytes(25000, &spec);
|
2014-03-12 15:59:18 +01:00
|
|
|
attr.maxlength = (uint32_t)-1;
|
2014-10-18 14:45:05 -07:00
|
|
|
attr.minreq = (uint32_t)-1;
|
|
|
|
attr.prebuf = (uint32_t)-1;
|
|
|
|
attr.tlength = (uint32_t)-1;
|
2014-03-12 15:59:18 +01:00
|
|
|
|
2014-10-06 05:39:02 -04:00
|
|
|
pa_stream_flags_t flags = PA_STREAM_ADJUST_LATENCY;
|
2014-03-12 15:59:18 +01:00
|
|
|
|
|
|
|
pulse_lock();
|
|
|
|
int_fast32_t ret = pa_stream_connect_record(data->stream, data->device,
|
|
|
|
&attr, flags);
|
|
|
|
pulse_unlock();
|
|
|
|
if (ret < 0) {
|
2014-04-25 21:26:53 +02:00
|
|
|
pulse_stop_recording(data);
|
2014-08-29 17:17:22 +02:00
|
|
|
blog(LOG_ERROR, "Unable to connect to stream");
|
2014-02-16 13:45:46 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-08-29 17:17:22 +02:00
|
|
|
blog(LOG_INFO, "Started recording from '%s'", data->device);
|
2014-02-16 13:45:46 +01:00
|
|
|
return 0;
|
2014-02-13 22:05:23 +01:00
|
|
|
}
|
|
|
|
|
2014-04-25 21:26:53 +02:00
|
|
|
/**
|
2014-03-12 15:59:18 +01:00
|
|
|
* stop recording
|
2014-02-16 13:45:46 +01:00
|
|
|
*/
|
2014-03-12 15:59:18 +01:00
|
|
|
static void pulse_stop_recording(struct pulse_data *data)
|
2014-02-13 22:05:23 +01:00
|
|
|
{
|
2014-02-16 13:45:46 +01:00
|
|
|
if (data->stream) {
|
2014-03-12 15:59:18 +01:00
|
|
|
pulse_lock();
|
2014-02-16 13:45:46 +01:00
|
|
|
pa_stream_disconnect(data->stream);
|
|
|
|
pa_stream_unref(data->stream);
|
2014-04-25 21:26:53 +02:00
|
|
|
data->stream = NULL;
|
2014-03-12 15:59:18 +01:00
|
|
|
pulse_unlock();
|
2014-02-16 13:45:46 +01:00
|
|
|
}
|
2014-05-18 14:31:37 +02:00
|
|
|
|
2014-08-29 17:17:22 +02:00
|
|
|
blog(LOG_INFO, "Stopped recording from '%s'", data->device);
|
|
|
|
blog(LOG_INFO,
|
|
|
|
"Got %" PRIuFAST32 " packets with %" PRIuFAST64 " frames",
|
2014-05-19 21:24:30 +02:00
|
|
|
data->packets, data->frames);
|
2014-09-02 19:35:17 +02:00
|
|
|
|
2014-10-06 05:39:02 -04:00
|
|
|
data->first_ts = 0;
|
2014-05-21 20:55:09 +02:00
|
|
|
data->packets = 0;
|
|
|
|
data->frames = 0;
|
2014-02-13 22:05:23 +01:00
|
|
|
}
|
|
|
|
|
2014-04-25 21:26:53 +02:00
|
|
|
/**
|
2014-03-12 15:59:18 +01:00
|
|
|
* input info callback
|
2014-02-22 14:48:11 +01:00
|
|
|
*/
|
2014-03-12 15:59:18 +01:00
|
|
|
static void pulse_input_info(pa_context *c, const pa_source_info *i, int eol,
|
|
|
|
void *userdata)
|
2014-02-22 14:48:11 +01:00
|
|
|
{
|
2014-03-12 15:59:18 +01:00
|
|
|
UNUSED_PARAMETER(c);
|
|
|
|
if (eol != 0 || i->monitor_of_sink != PA_INVALID_INDEX)
|
|
|
|
goto skip;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_property_list_add_string((obs_property_t *)userdata, i->description,
|
2014-03-12 15:59:18 +01:00
|
|
|
i->name);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-03-12 15:59:18 +01:00
|
|
|
skip:
|
|
|
|
pulse_signal(0);
|
2014-02-22 14:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-12 15:59:18 +01:00
|
|
|
* output info callback
|
2014-02-22 14:48:11 +01:00
|
|
|
*/
|
2017-08-08 14:26:38 +02:00
|
|
|
static void pulse_output_info(pa_context *c, const pa_sink_info *i, int eol,
|
2014-02-22 14:48:11 +01:00
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(c);
|
2017-08-08 14:26:38 +02:00
|
|
|
if (eol != 0 || i->monitor_source == PA_INVALID_INDEX)
|
2014-03-12 15:59:18 +01:00
|
|
|
goto skip;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_property_list_add_string((obs_property_t *)userdata, i->description,
|
2017-08-08 14:26:38 +02:00
|
|
|
i->monitor_source_name);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-03-12 15:59:18 +01:00
|
|
|
skip:
|
|
|
|
pulse_signal(0);
|
2014-02-22 14:48:11 +01:00
|
|
|
}
|
|
|
|
|
2014-04-25 21:26:53 +02:00
|
|
|
/**
|
|
|
|
* Get plugin properties
|
2014-02-22 14:48:11 +01:00
|
|
|
*/
|
2014-09-25 17:44:05 -07:00
|
|
|
static obs_properties_t *pulse_properties(bool input)
|
2014-02-22 14:48:11 +01:00
|
|
|
{
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_properties_t *props = obs_properties_create();
|
|
|
|
obs_property_t *devices = obs_properties_add_list(
|
2014-07-09 22:12:57 -07:00
|
|
|
props, "device_id", obs_module_text("Device"),
|
|
|
|
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-03-12 15:59:18 +01:00
|
|
|
pulse_init();
|
2017-08-08 14:26:38 +02:00
|
|
|
if (input)
|
|
|
|
pulse_get_source_info_list(pulse_input_info, (void *)devices);
|
|
|
|
else
|
|
|
|
pulse_get_sink_info_list(pulse_output_info, (void *)devices);
|
2014-03-12 15:59:18 +01:00
|
|
|
pulse_unref();
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2017-09-23 00:33:40 -03:00
|
|
|
size_t count = obs_property_list_item_count(devices);
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
obs_property_list_insert_string(
|
|
|
|
devices, 0, obs_module_text("Default"), "default");
|
|
|
|
|
2014-02-22 14:48:11 +01:00
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
2014-09-29 17:36:13 +02:00
|
|
|
static obs_properties_t *pulse_input_properties(void *unused)
|
2014-02-22 14:48:11 +01:00
|
|
|
{
|
2014-09-29 17:36:13 +02:00
|
|
|
UNUSED_PARAMETER(unused);
|
|
|
|
|
2014-06-25 00:13:00 -07:00
|
|
|
return pulse_properties(true);
|
2014-02-22 14:48:11 +01:00
|
|
|
}
|
|
|
|
|
2014-09-29 17:36:13 +02:00
|
|
|
static obs_properties_t *pulse_output_properties(void *unused)
|
2014-02-22 14:48:11 +01:00
|
|
|
{
|
2014-09-29 17:36:13 +02:00
|
|
|
UNUSED_PARAMETER(unused);
|
|
|
|
|
2014-06-25 00:13:00 -07:00
|
|
|
return pulse_properties(false);
|
2014-02-22 14:48:11 +01:00
|
|
|
}
|
|
|
|
|
2014-04-25 21:26:53 +02:00
|
|
|
/**
|
|
|
|
* Get plugin defaults
|
2014-02-22 14:48:11 +01:00
|
|
|
*/
|
2017-09-23 00:33:40 -03:00
|
|
|
static void pulse_defaults(obs_data_t *settings)
|
2014-04-28 20:44:01 +02:00
|
|
|
{
|
2017-09-23 00:33:40 -03:00
|
|
|
obs_data_set_default_string(settings, "device_id", "default");
|
2014-02-22 14:48:11 +01:00
|
|
|
}
|
|
|
|
|
2014-04-25 21:26:53 +02:00
|
|
|
/**
|
2014-02-16 13:45:46 +01:00
|
|
|
* Returns the name of the plugin
|
|
|
|
*/
|
2015-09-16 01:30:51 -07:00
|
|
|
static const char *pulse_input_getname(void *unused)
|
2014-02-13 22:05:23 +01:00
|
|
|
{
|
2015-09-16 01:30:51 -07:00
|
|
|
UNUSED_PARAMETER(unused);
|
2014-07-09 22:12:57 -07:00
|
|
|
return obs_module_text("PulseInput");
|
2014-02-22 14:48:11 +01:00
|
|
|
}
|
|
|
|
|
2015-09-16 01:30:51 -07:00
|
|
|
static const char *pulse_output_getname(void *unused)
|
2014-02-22 14:48:11 +01:00
|
|
|
{
|
2015-09-16 01:30:51 -07:00
|
|
|
UNUSED_PARAMETER(unused);
|
2014-07-09 22:12:57 -07:00
|
|
|
return obs_module_text("PulseOutput");
|
2014-02-13 22:05:23 +01:00
|
|
|
}
|
|
|
|
|
2014-04-25 21:26:53 +02:00
|
|
|
/**
|
2014-02-16 13:45:46 +01:00
|
|
|
* Destroy the plugin object and free all memory
|
|
|
|
*/
|
2014-02-13 22:05:23 +01:00
|
|
|
static void pulse_destroy(void *vptr)
|
|
|
|
{
|
2014-02-16 13:45:46 +01:00
|
|
|
PULSE_DATA(vptr);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-16 13:45:46 +01:00
|
|
|
if (!data)
|
|
|
|
return;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-25 21:26:53 +02:00
|
|
|
if (data->stream)
|
|
|
|
pulse_stop_recording(data);
|
2014-03-12 15:59:18 +01:00
|
|
|
pulse_unref();
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-03-12 15:59:18 +01:00
|
|
|
if (data->device)
|
|
|
|
bfree(data->device);
|
|
|
|
bfree(data);
|
2014-02-13 22:05:23 +01:00
|
|
|
}
|
|
|
|
|
2014-04-25 21:26:53 +02:00
|
|
|
/**
|
|
|
|
* Update the input settings
|
|
|
|
*/
|
2014-09-25 17:44:05 -07:00
|
|
|
static void pulse_update(void *vptr, obs_data_t *settings)
|
2014-04-25 21:26:53 +02:00
|
|
|
{
|
|
|
|
PULSE_DATA(vptr);
|
2014-04-28 21:10:17 +02:00
|
|
|
bool restart = false;
|
2014-05-19 20:57:49 +02:00
|
|
|
const char *new_device;
|
2014-04-25 21:26:53 +02:00
|
|
|
|
2014-08-05 11:09:29 -07:00
|
|
|
new_device = obs_data_get_string(settings, "device_id");
|
2014-04-28 21:10:17 +02:00
|
|
|
if (!data->device || strcmp(data->device, new_device) != 0) {
|
|
|
|
if (data->device)
|
|
|
|
bfree(data->device);
|
2014-05-19 20:57:49 +02:00
|
|
|
data->device = bstrdup(new_device);
|
2014-04-28 21:10:17 +02:00
|
|
|
restart = true;
|
|
|
|
}
|
2014-04-25 21:26:53 +02:00
|
|
|
|
2014-04-28 21:10:17 +02:00
|
|
|
if (!restart)
|
|
|
|
return;
|
2014-04-25 21:26:53 +02:00
|
|
|
|
|
|
|
if (data->stream)
|
|
|
|
pulse_stop_recording(data);
|
|
|
|
pulse_start_recording(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-16 13:45:46 +01:00
|
|
|
* Create the plugin object
|
|
|
|
*/
|
2017-09-23 00:33:40 -03:00
|
|
|
static void *pulse_create(obs_data_t *settings, obs_source_t *source,
|
|
|
|
bool input)
|
2014-02-13 22:05:23 +01:00
|
|
|
{
|
2014-03-12 15:59:18 +01:00
|
|
|
struct pulse_data *data = bzalloc(sizeof(struct pulse_data));
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2017-09-23 00:33:40 -03:00
|
|
|
data->input = input;
|
2014-03-12 15:59:18 +01:00
|
|
|
data->source = source;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-03-12 15:59:18 +01:00
|
|
|
pulse_init();
|
2014-04-25 21:26:53 +02:00
|
|
|
pulse_update(data, settings);
|
|
|
|
|
2016-08-28 02:56:59 +02:00
|
|
|
return data;
|
2014-02-13 22:05:23 +01:00
|
|
|
}
|
|
|
|
|
2017-09-23 00:33:40 -03:00
|
|
|
static void *pulse_input_create(obs_data_t *settings, obs_source_t *source)
|
|
|
|
{
|
|
|
|
return pulse_create(settings, source, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *pulse_output_create(obs_data_t *settings, obs_source_t *source)
|
|
|
|
{
|
|
|
|
return pulse_create(settings, source, false);
|
|
|
|
}
|
|
|
|
|
2014-02-22 14:48:11 +01:00
|
|
|
struct obs_source_info pulse_input_capture = {
|
2014-08-04 21:27:52 -07:00
|
|
|
.id = "pulse_input_capture",
|
|
|
|
.type = OBS_SOURCE_TYPE_INPUT,
|
2016-01-12 16:25:47 -08:00
|
|
|
.output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_DO_NOT_DUPLICATE,
|
2014-08-04 21:27:52 -07:00
|
|
|
.get_name = pulse_input_getname,
|
2017-09-23 00:33:40 -03:00
|
|
|
.create = pulse_input_create,
|
2014-08-04 21:27:52 -07:00
|
|
|
.destroy = pulse_destroy,
|
|
|
|
.update = pulse_update,
|
2017-09-23 00:33:40 -03:00
|
|
|
.get_defaults = pulse_defaults,
|
2014-08-04 21:27:52 -07:00
|
|
|
.get_properties = pulse_input_properties,
|
2019-07-27 23:59:16 -05:00
|
|
|
.icon_type = OBS_ICON_TYPE_AUDIO_INPUT,
|
2014-02-22 14:48:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct obs_source_info pulse_output_capture = {
|
2014-08-04 21:27:52 -07:00
|
|
|
.id = "pulse_output_capture",
|
|
|
|
.type = OBS_SOURCE_TYPE_INPUT,
|
2016-01-12 16:25:47 -08:00
|
|
|
.output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_DO_NOT_DUPLICATE |
|
2017-04-04 20:14:11 -03:00
|
|
|
OBS_SOURCE_DO_NOT_SELF_MONITOR,
|
2014-08-04 21:27:52 -07:00
|
|
|
.get_name = pulse_output_getname,
|
2017-09-23 00:33:40 -03:00
|
|
|
.create = pulse_output_create,
|
2014-08-04 21:27:52 -07:00
|
|
|
.destroy = pulse_destroy,
|
|
|
|
.update = pulse_update,
|
2017-09-23 00:33:40 -03:00
|
|
|
.get_defaults = pulse_defaults,
|
2014-08-04 21:27:52 -07:00
|
|
|
.get_properties = pulse_output_properties,
|
2019-07-27 23:59:16 -05:00
|
|
|
.icon_type = OBS_ICON_TYPE_AUDIO_OUTPUT,
|
2014-02-13 22:05:23 +01:00
|
|
|
};
|