1
0
Эх сурвалжийг харах

Handle audio channels correctly in pulseaudio plugin

This fixes a bug where the pulseaudio plugin always reported
a speaker layout of stereo to obs, regardless of how many channels
pulseaudio actually recorded.
If the default number of channels was different to 2 this would
cause audio distortion.
fryshorts 11 жил өмнө
parent
commit
964d6eb3db

+ 27 - 1
plugins/linux-pulseaudio/pulse-input.c

@@ -66,6 +66,32 @@ static enum audio_format pulse_to_obs_audio_format(
 	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;
+}
+
 /**
  * Get latency for a pulse audio stream
  */
@@ -184,6 +210,7 @@ static int_fast32_t pulse_start_recording(struct pulse_data *data)
 		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),
@@ -429,7 +456,6 @@ static void *pulse_create(obs_data_t settings, obs_source_t source)
 	struct pulse_data *data = bzalloc(sizeof(struct pulse_data));
 
 	data->source   = source;
-	data->speakers = SPEAKERS_STEREO;
 
 	pulse_init();
 	pulse_update(data, settings);