浏览代码

libobs: Add async video/audio decoupling functions

Decoupling the audio from the video causes the audio to be played right
when it's received rather than attempt to sync up to the video frames.
This is useful with certain async sources/devices when the audio/video
timestamps are not reliable.

Naturally because it plays audio right when it's received, this should
only be used when the async source is operating in unbuffered mode,
otherwise the video frame timing will be out of sync by the amount of
buffering the video currently has.
jp9000 8 年之前
父节点
当前提交
b54f70ef8d
共有 4 个文件被更改,包括 38 次插入4 次删除
  1. 4 1
      libobs/audio-monitoring/win32/wasapi-output.c
  2. 1 0
      libobs/obs-internal.h
  3. 27 3
      libobs/obs-source.c
  4. 6 0
      libobs/obs.h

+ 4 - 1
libobs/audio-monitoring/win32/wasapi-output.c

@@ -165,7 +165,10 @@ static void on_audio_playback(void *param, obs_source_t *source,
 	UINT32 pad = 0;
 	monitor->client->lpVtbl->GetCurrentPadding(monitor->client, &pad);
 
-	if (monitor->source_has_video) {
+	bool decouple_audio =
+		source->async_unbuffered && source->async_decoupled;
+
+	if (monitor->source_has_video && !decouple_audio) {
 		uint64_t ts = audio_data->timestamp - ts_offset;
 
 		if (!process_audio_delay(monitor, (float**)(&resample_data[0]),

+ 1 - 0
libobs/obs-internal.h

@@ -610,6 +610,7 @@ struct obs_source {
 	bool                            async_active;
 	bool                            async_update_texture;
 	bool                            async_unbuffered;
+	bool                            async_decoupled;
 	struct obs_source_frame         *async_preload_frame;
 	DARRAY(struct async_frame)      async_cache;
 	DARRAY(struct obs_source_frame*)async_frames;

+ 27 - 3
libobs/obs-source.c

@@ -1675,9 +1675,13 @@ static void obs_source_update_async_video(obs_source_t *source)
 
 		source->async_rendered = true;
 		if (frame) {
-			source->timing_adjust =
-				obs->video.video_time - frame->timestamp;
-			source->timing_set = true;
+			if (!source->async_decoupled ||
+			    !source->async_unbuffered) {
+				source->timing_adjust =
+					obs->video.video_time -
+					frame->timestamp;
+				source->timing_set = true;
+			}
 
 			if (source->async_update_texture) {
 				update_async_texture(source, frame,
@@ -4069,3 +4073,23 @@ obs_data_t *obs_source_get_private_settings(obs_source_t *source)
 	obs_data_addref(source->private_settings);
 	return source->private_settings;
 }
+
+void obs_source_set_async_decoupled(obs_source_t *source, bool decouple)
+{
+	if (!obs_ptr_valid(source, "obs_source_set_async_decoupled"))
+		return;
+
+	source->async_decoupled = decouple;
+	if (decouple) {
+		pthread_mutex_lock(&source->audio_buf_mutex);
+		source->timing_set = false;
+		reset_audio_data(source, 0);
+		pthread_mutex_unlock(&source->audio_buf_mutex);
+	}
+}
+
+bool obs_source_async_decoupled(const obs_source_t *source)
+{
+	return obs_source_valid(source, "obs_source_async_decoupled") ?
+		source->async_decoupled : false;
+}

+ 6 - 0
libobs/obs.h

@@ -1118,6 +1118,12 @@ EXPORT void obs_source_set_async_unbuffered(obs_source_t *source,
 		bool unbuffered);
 EXPORT bool obs_source_async_unbuffered(const obs_source_t *source);
 
+/** Used to decouple audio from video so that audio doesn't attempt to sync up
+ * with video.  I.E. Audio acts independently.  Only works when in unbuffered
+ * mode. */
+EXPORT void obs_source_set_async_decoupled(obs_source_t *source, bool decouple);
+EXPORT bool obs_source_async_decoupled(const obs_source_t *source);
+
 /* ------------------------------------------------------------------------- */
 /* Transition-specific functions */
 enum obs_transition_target {