Fix messed up audio on some distros (those using pulseaudio?)

This commit is contained in:
dec05eba
2024-04-11 18:46:34 +02:00
parent a6b1d13beb
commit ddac6acaf2
3 changed files with 57 additions and 31 deletions

View File

@@ -24,7 +24,6 @@
typedef struct { typedef struct {
void *handle; void *handle;
unsigned int frames; unsigned int frames;
double latency_seconds;
} SoundDevice; } SoundDevice;
struct AudioInput { struct AudioInput {
@@ -54,7 +53,7 @@ void sound_device_close(SoundDevice *device);
/* /*
Returns the next chunk of audio into @buffer. Returns the next chunk of audio into @buffer.
Returns the number of bytes read, or a negative value on failure. Returns the number of frames read, or a negative value on failure.
*/ */
int sound_device_read_next_chunk(SoundDevice *device, void **buffer, double timeout_sec); int sound_device_read_next_chunk(SoundDevice *device, void **buffer, double timeout_sec);

View File

@@ -2435,7 +2435,6 @@ int main(int argc, char **argv) {
if(new_pts == audio_device.frame->pts) if(new_pts == audio_device.frame->pts)
continue; continue;
audio_device.frame->pts = new_pts; audio_device.frame->pts = new_pts;
//audio_device.frame->linesize[0] = sound_buffer_size / 2;
if(audio_track.graph) { if(audio_track.graph) {
std::lock_guard<std::mutex> lock(audio_filter_mutex); std::lock_guard<std::mutex> lock(audio_filter_mutex);
@@ -2468,7 +2467,6 @@ int main(int argc, char **argv) {
if(new_pts == audio_device.frame->pts) if(new_pts == audio_device.frame->pts)
continue; continue;
audio_device.frame->pts = new_pts; audio_device.frame->pts = new_pts;
//audio_device.frame->linesize[0] = sound_buffer_size / 2;
if(audio_track.graph) { if(audio_track.graph) {
std::lock_guard<std::mutex> lock(audio_filter_mutex); std::lock_guard<std::mutex> lock(audio_filter_mutex);
@@ -2526,7 +2524,6 @@ int main(int argc, char **argv) {
if(new_pts == aframe->pts) if(new_pts == aframe->pts)
continue; continue;
aframe->pts = new_pts; aframe->pts = new_pts;
//aframe->linesize[0] = sound_buffer_size / 2;
err = avcodec_send_frame(audio_track.codec_context, aframe); err = avcodec_send_frame(audio_track.codec_context, aframe);
if(err >= 0){ if(err >= 0){
// TODO: Move to separate thread because this could write to network (for example when livestreaming) // TODO: Move to separate thread because this could write to network (for example when livestreaming)

View File

@@ -41,7 +41,6 @@ struct pa_handle {
size_t output_index, output_length; size_t output_index, output_length;
int operation_success; int operation_success;
double latency_seconds;
}; };
static void pa_sound_device_free(pa_handle *s) { static void pa_sound_device_free(pa_handle *s) {
@@ -80,7 +79,6 @@ static pa_handle* pa_sound_device_new(const char *server,
p->read_data = NULL; p->read_data = NULL;
p->read_length = 0; p->read_length = 0;
p->read_index = 0; p->read_index = 0;
p->latency_seconds = 0;
const int buffer_size = attr->maxlength; const int buffer_size = attr->maxlength;
void *buffer = malloc(buffer_size); void *buffer = malloc(buffer_size);
@@ -160,36 +158,71 @@ static int pa_sound_device_read(pa_handle *p, double timeout_seconds) {
const double start_time = clock_get_monotonic_seconds(); const double start_time = clock_get_monotonic_seconds();
bool success = false;
int r = 0; int r = 0;
//pa_usec_t latency = 0;
//int negative = 0;
int *rerror = &r; int *rerror = &r;
CHECK_DEAD_GOTO(p, rerror, fail); CHECK_DEAD_GOTO(p, rerror, fail);
while(clock_get_monotonic_seconds() - start_time < timeout_seconds) { while (p->output_index < p->output_length) {
pa_mainloop_prepare(p->mainloop, 1 * 1000); if(clock_get_monotonic_seconds() - start_time >= timeout_seconds)
pa_mainloop_poll(p->mainloop); return -1;
pa_mainloop_dispatch(p->mainloop);
if(pa_stream_peek(p->stream, &p->read_data, &p->read_length) < 0) if(!p->read_data) {
goto fail; pa_mainloop_prepare(p->mainloop, 1 * 1000); // 1 ms
pa_mainloop_poll(p->mainloop);
pa_mainloop_dispatch(p->mainloop);
if(!p->read_data && p->read_length == 0) if(pa_stream_peek(p->stream, &p->read_data, &p->read_length) < 0)
continue; goto fail;
// pa_operation_unref(pa_stream_update_timing_info(p->stream, NULL, NULL)); if(!p->read_data && p->read_length == 0)
// if (pa_stream_get_latency(p->stream, &latency, &negative) >= 0) { continue;
// fprintf(stderr, "latency: %lu ms, negative: %d, extra delay: %f ms\n", latency / 1000, negative, (clock_get_monotonic_seconds() - start_time) * 1000.0);
// }
memcpy(p->output_data, p->read_data, p->read_length); if(!p->read_data && p->read_length > 0) {
pa_stream_drop(p->stream); // There is a hole in the stream :( drop it. Maybe we should generate silence instead? TODO
p->latency_seconds = clock_get_monotonic_seconds() - start_time; if(pa_stream_drop(p->stream) != 0)
return p->read_length; goto fail;
continue;
}
if(p->read_length <= 0) {
p->read_data = NULL;
if(pa_stream_drop(p->stream) != 0)
goto fail;
CHECK_DEAD_GOTO(p, rerror, fail);
continue;
}
}
const size_t space_free_in_output_buffer = p->output_length - p->output_index;
if(space_free_in_output_buffer < p->read_length) {
memcpy(p->output_data + p->output_index, (const uint8_t*)p->read_data + p->read_index, space_free_in_output_buffer);
p->output_index = 0;
p->read_index += space_free_in_output_buffer;
p->read_length -= space_free_in_output_buffer;
break;
} else {
memcpy(p->output_data + p->output_index, (const uint8_t*)p->read_data + p->read_index, p->read_length);
p->output_index += p->read_length;
p->read_data = NULL;
p->read_length = 0;
p->read_index = 0;
if(pa_stream_drop(p->stream) != 0)
goto fail;
if(p->output_index == p->output_length) {
p->output_index = 0;
break;
}
}
} }
success = true;
fail: fail:
return -1; return success ? 0 : -1;
} }
static pa_sample_format_t audio_format_to_pulse_audio_format(AudioFormat audio_format) { static pa_sample_format_t audio_format_to_pulse_audio_format(AudioFormat audio_format) {
@@ -234,7 +267,6 @@ int sound_device_get_by_name(SoundDevice *device, const char *device_name, const
device->handle = handle; device->handle = handle;
device->frames = period_frame_size; device->frames = period_frame_size;
device->latency_seconds = 0.0;
return 0; return 0;
} }
@@ -246,14 +278,12 @@ void sound_device_close(SoundDevice *device) {
int sound_device_read_next_chunk(SoundDevice *device, void **buffer, double timeout_sec) { int sound_device_read_next_chunk(SoundDevice *device, void **buffer, double timeout_sec) {
pa_handle *pa = (pa_handle*)device->handle; pa_handle *pa = (pa_handle*)device->handle;
int size = pa_sound_device_read(pa, timeout_sec); if(pa_sound_device_read(pa, timeout_sec) < 0) {
if(size < 0) {
//fprintf(stderr, "pa_simple_read() failed: %s\n", pa_strerror(error)); //fprintf(stderr, "pa_simple_read() failed: %s\n", pa_strerror(error));
return -1; return -1;
} }
*buffer = pa->output_data; *buffer = pa->output_data;
device->latency_seconds = pa->latency_seconds; return device->frames;
return size;
} }
static void pa_state_cb(pa_context *c, void *userdata) { static void pa_state_cb(pa_context *c, void *userdata) {