From faa7f4d20ed0c5d02f84bfa1505d982cae5a73a0 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Fri, 10 Jan 2014 11:55:54 -0700 Subject: [PATCH] Properly position position mixed audio data - Audio data was just being popped to the "front" of the mix buffer, so instead it now properly pops into the correct position in the mix buffer (proper mixing still needs to be implemented) --- libobs/media-io/audio-io.c | 32 ++++++++++++++++++++++---------- libobs/obs-source.c | 2 +- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/libobs/media-io/audio-io.c b/libobs/media-io/audio-io.c index 5ac3444ab..3f6a6cfc6 100644 --- a/libobs/media-io/audio-io.c +++ b/libobs/media-io/audio-io.c @@ -111,10 +111,26 @@ static inline uint64_t min_uint64(uint64_t a, uint64_t b) } static inline void mix_audio_line(struct audio_output *audio, - struct audio_line *line, size_t size) + struct audio_line *line, size_t size, uint64_t timestamp) { /* TODO: this just overwrites, handle actual mixing */ - circlebuf_pop_front(&line->buffer, audio->mix_buffer.array, size); + if (!line->buffer.size) { + if (!line->alive) + audio_output_removeline(audio, line); + return; + } + + size_t time_offset = convert_to_sample_offset(audio, + line->base_timestamp - timestamp); + if (time_offset > size) + return; + + size -= time_offset; + + size_t pop_size = min_uint64(size, line->buffer.size); + circlebuf_pop_front(&line->buffer, + audio->mix_buffer.array + time_offset, + pop_size); } static void mix_audio_lines(struct audio_output *audio, uint64_t audio_time, @@ -122,7 +138,7 @@ static void mix_audio_lines(struct audio_output *audio, uint64_t audio_time, { struct audio_line *line = audio->first_line; uint64_t time_offset = audio_time - prev_time; - uint64_t byte_offset = convert_to_sample_offset(audio, time_offset); + size_t byte_offset = convert_to_sample_offset(audio, time_offset); da_resize(audio->mix_buffer, byte_offset); memset(audio->mix_buffer.array, 0, byte_offset); @@ -132,16 +148,12 @@ static void mix_audio_lines(struct audio_output *audio, uint64_t audio_time, if (line->buffer.size && line->base_timestamp < prev_time) { clear_excess_audio_data(line, - line->base_timestamp - prev_time); + prev_time - line->base_timestamp); line->base_timestamp = prev_time; } - size_t pop_size = min_uint64(byte_offset, line->buffer.size); - if (pop_size) - mix_audio_line(audio, line, pop_size); - else if (!line->alive) - audio_output_removeline(audio, line); - + mix_audio_line(audio, line, byte_offset, prev_time); + line->base_timestamp = audio_time; line = next; } diff --git a/libobs/obs-source.c b/libobs/obs-source.c index 8ee980447..e7175f29b 100644 --- a/libobs/obs-source.c +++ b/libobs/obs-source.c @@ -334,7 +334,7 @@ static void source_output_audio_line(obs_source_t source, if (!source->timing_set) { source->timing_set = true; - source->timing_adjust = in.timestamp - os_gettime_ns(); + source->timing_adjust = os_gettime_ns() - in.timestamp; /* detects 'directly' set timestamps as long as they're within * a certain threshold */